• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "perf_data_utils.h"
6 
7 #include <string>
8 
9 #include "base/logging.h"
10 #include "compat/proto.h"
11 
12 namespace quipper {
13 
CallocMemoryForEvent(size_t size)14 event_t* CallocMemoryForEvent(size_t size) {
15   event_t* event = reinterpret_cast<event_t*>(calloc(1, size));
16   CHECK(event);
17   return event;
18 }
19 
ReallocMemoryForEvent(event_t * event,size_t new_size)20 event_t* ReallocMemoryForEvent(event_t* event, size_t new_size) {
21   event_t* new_event = reinterpret_cast<event_t*>(realloc(event, new_size));
22   CHECK(new_event);  // NB: event is "leaked" if this CHECK fails.
23   return new_event;
24 }
25 
CallocMemoryForBuildID(size_t size)26 build_id_event* CallocMemoryForBuildID(size_t size) {
27   build_id_event* event = reinterpret_cast<build_id_event*>(calloc(1, size));
28   CHECK(event);
29   return event;
30 }
31 
PerfizeBuildIDString(string * build_id)32 void PerfizeBuildIDString(string* build_id) {
33   build_id->resize(kBuildIDStringLength, '0');
34 }
35 
TrimZeroesFromBuildIDString(string * build_id)36 void TrimZeroesFromBuildIDString(string* build_id) {
37   const size_t kPaddingSize = 8;
38   const string kBuildIDPadding = string(kPaddingSize, '0');
39 
40   // Remove kBuildIDPadding from the end of build_id until we cannot remove any
41   // more. The build ID string can be reduced down to an empty string. This
42   // could happen if the file did not have a build ID but was given a build ID
43   // of all zeroes. The empty build ID string would reflect the original lack of
44   // build ID.
45   while (build_id->size() >= kPaddingSize &&
46          build_id->substr(build_id->size() - kPaddingSize) == kBuildIDPadding) {
47     build_id->resize(build_id->size() - kPaddingSize);
48   }
49 }
50 
GetSampleInfoForEvent(const PerfDataProto_PerfEvent & event)51 const PerfDataProto_SampleInfo* GetSampleInfoForEvent(
52     const PerfDataProto_PerfEvent& event) {
53   switch (event.header().type()) {
54     case PERF_RECORD_MMAP:
55     case PERF_RECORD_MMAP2:
56       return &event.mmap_event().sample_info();
57     case PERF_RECORD_COMM:
58       return &event.comm_event().sample_info();
59     case PERF_RECORD_FORK:
60       return &event.fork_event().sample_info();
61     case PERF_RECORD_EXIT:
62       return &event.exit_event().sample_info();
63     case PERF_RECORD_LOST:
64       return &event.lost_event().sample_info();
65     case PERF_RECORD_THROTTLE:
66     case PERF_RECORD_UNTHROTTLE:
67       return &event.throttle_event().sample_info();
68     case PERF_RECORD_READ:
69       return &event.read_event().sample_info();
70     case PERF_RECORD_AUX:
71       return &event.aux_event().sample_info();
72   }
73   return nullptr;
74 }
75 
76 // Returns the correct |sample_time_ns| field of a PerfEvent.
GetTimeFromPerfEvent(const PerfDataProto_PerfEvent & event)77 uint64_t GetTimeFromPerfEvent(const PerfDataProto_PerfEvent& event) {
78   if (event.header().type() == PERF_RECORD_SAMPLE)
79     return event.sample_event().sample_time_ns();
80 
81   const auto* sample_info = GetSampleInfoForEvent(event);
82   if (sample_info) return sample_info->sample_time_ns();
83 
84   return 0;
85 }
86 
87 }  // namespace quipper
88