OSDN Git Service

simpleperf: fix check of opening perf event files.
authorYabin Cui <yabinc@google.com>
Fri, 25 Aug 2017 18:17:45 +0000 (11:17 -0700)
committerElliott Hughes <enh@google.com>
Mon, 28 Aug 2017 19:07:10 +0000 (19:07 +0000)
When asked to monitor a process, record/stat cmd checks if we can
open perf event files for all threads successfully. However, it
fails when the monitored process has an exiting thread. This makes
record_cmd.duration_option test flakey, when there are exiting
threads in other tests, like record_cmd.handle_SIGHUP.

To fix this, only check if we can open perf event files for at
least one thread for each monitored process.

Bug: http://b/65047463
Bug: 64709603 (presubmit balking at the line above)
Test: run CtsSimpleperfTestCases.
Change-Id: Ia02ed498f49c9c6c452023ad31abb01a58071152
(cherry picked from commit b3ae56e485b4d971a909a8b04da65713a3d56872)

simpleperf/event_selection_set.cpp

index 86f57b9..7458572 100644 (file)
@@ -342,7 +342,7 @@ bool EventSelectionSet::OpenEventFilesOnGroup(EventSelectionGroup& group,
   EventFd* group_fd = nullptr;
   for (auto& selection : group) {
     std::unique_ptr<EventFd> event_fd =
-        EventFd::OpenEventFile(selection.event_attr, tid, cpu, group_fd);
+        EventFd::OpenEventFile(selection.event_attr, tid, cpu, group_fd, false);
     if (event_fd != nullptr) {
       LOG(VERBOSE) << "OpenEventFile for " << event_fd->Name();
       event_fds.push_back(std::move(event_fd));
@@ -402,24 +402,25 @@ bool EventSelectionSet::OpenEventFiles(const std::vector<int>& on_cpus) {
       }
     } else {
       for (const auto& pair : process_map) {
+        size_t success_count = 0;
+        std::string failed_event_type;
         for (const auto& tid : pair.second) {
-          size_t success_cpu_count = 0;
-          std::string failed_event_type;
           for (const auto& cpu : cpus) {
             if (OpenEventFilesOnGroup(group, tid, cpu, &failed_event_type)) {
-              success_cpu_count++;
+              success_count++;
             }
           }
-          // As the online cpus can be enabled or disabled at runtime, we may not
-          // open event file for all cpus successfully. But we should open at
-          // least one cpu successfully.
-          if (success_cpu_count == 0) {
-            PLOG(ERROR) << "failed to open perf event file for event_type "
-                        << failed_event_type << " for "
-                        << (tid == -1 ? "all threads" : "thread " + std::to_string(tid))
-                        << " on all cpus";
-            return false;
-          }
+        }
+        // We can't guarantee to open perf event file successfully for each thread on each cpu.
+        // Because threads may exit between PrepareThreads() and OpenEventFilesOnGroup(), and
+        // cpus may be offlined between GetOnlineCpus() and OpenEventFilesOnGroup().
+        // So we only check that we can at least monitor one thread for each process.
+        if (success_count == 0) {
+          PLOG(ERROR) << "failed to open perf event file for event_type "
+                      << failed_event_type << " for "
+                      << (pair.first == -1 ? "all threads"
+                                           : "threads in process " + std::to_string(pair.first));
+          return false;
         }
       }
     }