summaryrefslogtreecommitdiff
path: root/simpleperf/event_selection_set.cpp
blob: 5a98fd28db249c5a856b63174c6fde22a38ef399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "event_selection_set.h"

#include <poll.h>

#include <android-base/logging.h>
#include <android-base/stringprintf.h>

#include "environment.h"
#include "event_attr.h"
#include "event_type.h"
#include "perf_regs.h"

bool IsBranchSamplingSupported() {
  const EventType* type = FindEventTypeByName("cpu-cycles");
  if (type == nullptr) {
    return false;
  }
  perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
  attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
  attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY;
  return IsEventAttrSupportedByKernel(attr);
}

bool IsDwarfCallChainSamplingSupported() {
  const EventType* type = FindEventTypeByName("cpu-cycles");
  if (type == nullptr) {
    return false;
  }
  perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
  attr.sample_type |=
      PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER;
  attr.exclude_callchain_user = 1;
  attr.sample_regs_user = GetSupportedRegMask(GetBuildArch());
  attr.sample_stack_user = 8192;
  return IsEventAttrSupportedByKernel(attr);
}

bool EventSelectionSet::BuildAndCheckEventSelection(
    const std::string& event_name, EventSelection* selection) {
  std::unique_ptr<EventTypeAndModifier> event_type = ParseEventType(event_name);
  if (event_type == nullptr) {
    return false;
  }
  selection->event_type_modifier = *event_type;
  selection->event_attr = CreateDefaultPerfEventAttr(event_type->event_type);
  selection->event_attr.exclude_user = event_type->exclude_user;
  selection->event_attr.exclude_kernel = event_type->exclude_kernel;
  selection->event_attr.exclude_hv = event_type->exclude_hv;
  selection->event_attr.exclude_host = event_type->exclude_host;
  selection->event_attr.exclude_guest = event_type->exclude_guest;
  selection->event_attr.precise_ip = event_type->precise_ip;
  if (!IsEventAttrSupportedByKernel(selection->event_attr)) {
    LOG(ERROR) << "Event type '" << event_type->name
               << "' is not supported by the kernel";
    return false;
  }
  selection->event_fds.clear();

  for (const auto& group : groups_) {
    for (const auto& sel : group) {
      if (sel.event_type_modifier.name == selection->event_type_modifier.name) {
        LOG(ERROR) << "Event type '" << sel.event_type_modifier.name
                   << "' appears more than once";
        return false;
      }
    }
  }
  return true;
}

bool EventSelectionSet::AddEventType(const std::string& event_name) {
  return AddEventGroup(std::vector<std::string>(1, event_name));
}

bool EventSelectionSet::AddEventGroup(
    const std::vector<std::string>& event_names) {
  EventSelectionGroup group;
  for (const auto& event_name : event_names) {
    EventSelection selection;
    if (!BuildAndCheckEventSelection(event_name, &selection)) {
      return false;
    }
    selection.selection_id = group.size();
    selection.group_id = groups_.size();
    group.push_back(std::move(selection));
  }
  groups_.push_back(std::move(group));
  UnionSampleType();
  return true;
}

// Union the sample type of different event attrs can make reading sample
// records in perf.data
// easier.
void EventSelectionSet::UnionSampleType() {
  uint64_t sample_type = 0;
  for (const auto& group : groups_) {
    for (const auto& selection : group) {
      sample_type |= selection.event_attr.sample_type;
    }
  }
  for (auto& group : groups_) {
    for (auto& selection : group) {
      selection.event_attr.sample_type = sample_type;
    }
  }
}

void EventSelectionSet::SetEnableOnExec(bool enable) {
  for (auto& group : groups_) {
    for (auto& selection : group) {
      // If sampling is enabled on exec, then it is disabled at startup,
      // otherwise
      // it should be enabled at startup. Don't use ioctl(PERF_EVENT_IOC_ENABLE)
      // to enable it after perf_event_open(). Because some android kernels
      // can't
      // handle ioctl() well when cpu-hotplug happens. See http://b/25193162.
      if (enable) {
        selection.event_attr.enable_on_exec = 1;
        selection.event_attr.disabled = 1;
      } else {
        selection.event_attr.enable_on_exec = 0;
        selection.event_attr.disabled = 0;
      }
    }
  }
}

bool EventSelectionSet::GetEnableOnExec() {
  for (const auto& group : groups_) {
    for (const auto& selection : group) {
      if (selection.event_attr.enable_on_exec == 0) {
        return false;
      }
    }
  }
  return true;
}

void EventSelectionSet::SampleIdAll() {
  for (auto& group : groups_) {
    for (auto& selection : group) {
      selection.event_attr.sample_id_all = 1;
    }
  }
}

void EventSelectionSet::SetSampleFreq(const EventSelection& selection,
                                      uint64_t sample_freq) {
  EventSelection& sel = groups_[selection.group_id][selection.selection_id];
  sel.event_attr.freq = 1;
  sel.event_attr.sample_freq = sample_freq;
}

void EventSelectionSet::SetSamplePeriod(const EventSelection& selection,
                                        uint64_t sample_period) {
  EventSelection& sel = groups_[selection.group_id][selection.selection_id];
  sel.event_attr.freq = 0;
  sel.event_attr.sample_period = sample_period;
}

bool EventSelectionSet::SetBranchSampling(uint64_t branch_sample_type) {
  if (branch_sample_type != 0 &&
      (branch_sample_type &
       (PERF_SAMPLE_BRANCH_ANY | PERF_SAMPLE_BRANCH_ANY_CALL |
        PERF_SAMPLE_BRANCH_ANY_RETURN | PERF_SAMPLE_BRANCH_IND_CALL)) == 0) {
    LOG(ERROR) << "Invalid branch_sample_type: 0x" << std::hex
               << branch_sample_type;
    return false;
  }
  if (branch_sample_type != 0 && !IsBranchSamplingSupported()) {
    LOG(ERROR) << "branch stack sampling is not supported on this device.";
    return false;
  }
  for (auto& group : groups_) {
    for (auto& selection : group) {
      perf_event_attr& attr = selection.event_attr;
      if (branch_sample_type != 0) {
        attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
      } else {
        attr.sample_type &= ~PERF_SAMPLE_BRANCH_STACK;
      }
      attr.branch_sample_type = branch_sample_type;
    }
  }
  return true;
}

void EventSelectionSet::EnableFpCallChainSampling() {
  for (auto& group : groups_) {
    for (auto& selection : group) {
      selection.event_attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
    }
  }
}

bool EventSelectionSet::EnableDwarfCallChainSampling(uint32_t dump_stack_size) {
  if (!IsDwarfCallChainSamplingSupported()) {
    LOG(ERROR) << "dwarf callchain sampling is not supported on this device.";
    return false;
  }
  for (auto& group : groups_) {
    for (auto& selection : group) {
      selection.event_attr.sample_type |= PERF_SAMPLE_CALLCHAIN |
                                          PERF_SAMPLE_REGS_USER |
                                          PERF_SAMPLE_STACK_USER;
      selection.event_attr.exclude_callchain_user = 1;
      selection.event_attr.sample_regs_user =
          GetSupportedRegMask(GetBuildArch());
      selection.event_attr.sample_stack_user = dump_stack_size;
    }
  }
  return true;
}

void EventSelectionSet::SetInherit(bool enable) {
  for (auto& group : groups_) {
    for (auto& selection : group) {
      selection.event_attr.inherit = (enable ? 1 : 0);
    }
  }
}

static bool CheckIfCpusOnline(const std::vector<int>& cpus) {
  std::vector<int> online_cpus = GetOnlineCpus();
  for (const auto& cpu : cpus) {
    if (std::find(online_cpus.begin(), online_cpus.end(), cpu) ==
        online_cpus.end()) {
      LOG(ERROR) << "cpu " << cpu << " is not online.";
      return false;
    }
  }
  return true;
}

bool EventSelectionSet::OpenEventFilesForCpus(const std::vector<int>& cpus) {
  return OpenEventFilesForThreadsOnCpus({-1}, cpus);
}

bool EventSelectionSet::OpenEventFilesForThreadsOnCpus(
    const std::vector<pid_t>& threads, std::vector<int> cpus) {
  if (!cpus.empty()) {
    // cpus = {-1} means open an event file for all cpus.
    if (!(cpus.size() == 1 && cpus[0] == -1) && !CheckIfCpusOnline(cpus)) {
      return false;
    }
  } else {
    cpus = GetOnlineCpus();
  }
  return OpenEventFiles(threads, cpus);
}

bool EventSelectionSet::OpenEventFiles(const std::vector<pid_t>& threads,
                                       const std::vector<int>& cpus) {
  for (auto& group : groups_) {
    for (const auto& tid : threads) {
      size_t open_per_thread = 0;
      std::string failed_event_type;
      for (const auto& cpu : cpus) {
        std::vector<std::unique_ptr<EventFd>> event_fds;
        // Given a tid and cpu, events on the same group should be all opened
        // successfully or all failed to open.
        for (auto& selection : group) {
          EventFd* group_fd = nullptr;
          if (selection.selection_id != 0) {
            group_fd = event_fds[0].get();
          }
          std::unique_ptr<EventFd> event_fd =
              EventFd::OpenEventFile(selection.event_attr, tid, cpu, group_fd);
          if (event_fd != nullptr) {
            LOG(VERBOSE) << "OpenEventFile for " << event_fd->Name();
            event_fds.push_back(std::move(event_fd));
          } else {
            failed_event_type = selection.event_type_modifier.name;
            break;
          }
        }
        if (event_fds.size() == group.size()) {
          for (size_t i = 0; i < group.size(); ++i) {
            group[i].event_fds.push_back(std::move(event_fds[i]));
          }
          ++open_per_thread;
        }
      }
      // 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 (open_per_thread == 0) {
        PLOG(ERROR) << "failed to open perf event file for event_type "
                    << failed_event_type << " for "
                    << (tid == -1 ? "all threads" : android::base::StringPrintf(
                                                        " thread %d", tid));
        return false;
      }
    }
  }
  return true;
}

bool EventSelectionSet::ReadCounters(std::vector<CountersInfo>* counters) {
  counters->clear();
  for (auto& group : groups_) {
    for (auto& selection : group) {
      CountersInfo counters_info;
      counters_info.selection = &selection;
      for (auto& event_fd : selection.event_fds) {
        CountersInfo::CounterInfo counter_info;
        if (!event_fd->ReadCounter(&counter_info.counter)) {
          return false;
        }
        counter_info.tid = event_fd->ThreadId();
        counter_info.cpu = event_fd->Cpu();
        counters_info.counters.push_back(counter_info);
      }
      counters->push_back(counters_info);
    }
  }
  return true;
}

bool EventSelectionSet::MmapEventFiles(size_t min_mmap_pages,
                                       size_t max_mmap_pages,
                                       std::vector<pollfd>* pollfds) {
  for (size_t i = max_mmap_pages; i >= min_mmap_pages; i >>= 1) {
    if (MmapEventFiles(i, pollfds, i == min_mmap_pages)) {
      LOG(VERBOSE) << "Mapped buffer size is " << i << " pages.";
      return true;
    }
    for (auto& group : groups_) {
      for (auto& selection : group) {
        for (auto& event_fd : selection.event_fds) {
          event_fd->DestroyMappedBuffer();
        }
      }
    }
  }
  return false;
}

bool EventSelectionSet::MmapEventFiles(size_t mmap_pages,
                                       std::vector<pollfd>* pollfds,
                                       bool report_error) {
  pollfds->clear();
  for (auto& group : groups_) {
    for (auto& selection : group) {
      // For each event, allocate a mapped buffer for each cpu.
      std::map<int, EventFd*> cpu_map;
      for (auto& event_fd : selection.event_fds) {
        auto it = cpu_map.find(event_fd->Cpu());
        if (it != cpu_map.end()) {
          if (!event_fd->ShareMappedBuffer(*(it->second), report_error)) {
            return false;
          }
        } else {
          pollfd poll_fd;
          if (!event_fd->CreateMappedBuffer(mmap_pages, &poll_fd,
                                            report_error)) {
            return false;
          }
          pollfds->push_back(poll_fd);
          cpu_map.insert(std::make_pair(event_fd->Cpu(), event_fd.get()));
        }
      }
    }
  }
  return true;
}

void EventSelectionSet::PrepareToReadMmapEventData(
    std::function<bool(Record*)> callback) {
  record_callback_ = callback;
  bool has_timestamp = true;
  for (const auto& group : groups_) {
    for (const auto& selection : group) {
      if (!IsTimestampSupported(selection.event_attr)) {
        has_timestamp = false;
        break;
      }
    }
  }
  record_cache_.reset(new RecordCache(has_timestamp));

  for (const auto& group : groups_) {
    for (const auto& selection : group) {
      for (const auto& event_fd : selection.event_fds) {
        int event_id = event_fd->Id();
        event_id_to_attr_map_[event_id] = &selection.event_attr;
      }
    }
  }
}

bool EventSelectionSet::ReadMmapEventData() {
  for (auto& group : groups_) {
    for (auto& selection : group) {
      for (auto& event_fd : selection.event_fds) {
        bool has_data = true;
        while (has_data) {
          if (!ReadMmapEventDataForFd(event_fd, selection.event_attr,
                                      &has_data)) {
            return false;
          }
        }
      }
    }
  }
  return true;
}

bool EventSelectionSet::ReadMmapEventDataForFd(
    std::unique_ptr<EventFd>& event_fd, const perf_event_attr& attr,
    bool* has_data) {
  *has_data = false;
  while (true) {
    char* data;
    size_t size = event_fd->GetAvailableMmapData(&data);
    if (size == 0) {
      break;
    }
    std::vector<std::unique_ptr<Record>> records =
        ReadRecordsFromBuffer(attr, data, size);
    record_cache_->Push(std::move(records));
    std::unique_ptr<Record> r = record_cache_->Pop();
    while (r != nullptr) {
      if (!record_callback_(r.get())) {
        return false;
      }
      r = record_cache_->Pop();
    }
    *has_data = true;
  }
  return true;
}

bool EventSelectionSet::FinishReadMmapEventData() {
  std::vector<std::unique_ptr<Record>> records = record_cache_->PopAll();
  for (auto& r : records) {
    if (!record_callback_(r.get())) {
      return false;
    }
  }
  return true;
}