1 // Copyright (c) 2012 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.
5 #define LOG_TAG "perf_reader"
7 #include "perf_utils.h"
15 #include <fstream> // NOLINT(readability/streams)
19 #include "base/logging.h"
20 #include "base/macros.h"
24 // Number of hex digits in a byte.
25 const int kNumHexDigitsInByte = 2;
31 event_t* CallocMemoryForEvent(size_t size) {
32 event_t* event = reinterpret_cast<event_t*>(calloc(1, size));
37 build_id_event* CallocMemoryForBuildID(size_t size) {
38 build_id_event* event = reinterpret_cast<build_id_event*>(calloc(1, size));
43 string HexToString(const u8* array, size_t length) {
44 // Convert the bytes to hex digits one at a time.
45 // There will be kNumHexDigitsInByte hex digits, and 1 char for NUL.
46 char buffer[kNumHexDigitsInByte + 1];
48 for (size_t i = 0; i < length; ++i) {
49 snprintf(buffer, sizeof(buffer), "%02x", array[i]);
55 bool StringToHex(const string& str, u8* array, size_t length) {
56 const int kHexRadix = 16;
58 // Loop through kNumHexDigitsInByte characters at a time (to get one byte)
59 // Stop when there are no more characters, or the array has been filled.
61 (i + 1) * kNumHexDigitsInByte <= str.size() && i < length;
63 string one_byte = str.substr(i * kNumHexDigitsInByte, kNumHexDigitsInByte);
64 array[i] = strtol(one_byte.c_str(), &err, kHexRadix);
71 uint64_t AlignSize(uint64_t size, uint32_t align_size) {
72 return ((size + align_size - 1) / align_size) * align_size;
75 // In perf data, strings are packed into the smallest number of 8-byte blocks
76 // possible, including the null terminator.
78 // "0123" -> 5 bytes -> packed into 8 bytes
79 // "0123456" -> 8 bytes -> packed into 8 bytes
80 // "01234567" -> 9 bytes -> packed into 16 bytes
81 // "0123456789abcd" -> 15 bytes -> packed into 16 bytes
82 // "0123456789abcde" -> 16 bytes -> packed into 16 bytes
83 // "0123456789abcdef" -> 17 bytes -> packed into 24 bytes
85 // Returns the size of the 8-byte-aligned memory for storing |string|.
86 size_t GetUint64AlignedStringLength(const string& str) {
87 return AlignSize(str.size() + 1, sizeof(uint64_t));
90 uint64_t GetSampleFieldsForEventType(uint32_t event_type,
91 uint64_t sample_type) {
92 uint64_t mask = kuint64max;
94 case PERF_RECORD_MMAP:
95 case PERF_RECORD_LOST:
96 case PERF_RECORD_COMM:
97 case PERF_RECORD_EXIT:
98 case PERF_RECORD_THROTTLE:
99 case PERF_RECORD_UNTHROTTLE:
100 case PERF_RECORD_FORK:
101 case PERF_RECORD_READ:
102 case PERF_RECORD_MMAP2:
103 // See perf_event.h "struct" sample_id and sample_id_all.
104 mask = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_ID |
105 PERF_SAMPLE_STREAM_ID | PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER;
107 case PERF_RECORD_SAMPLE:
108 case SIMPLE_PERF_RECORD_KERNEL_SYMBOL:
111 LOG(FATAL) << "Unknown event type " << event_type;
113 return sample_type & mask;
116 uint64_t GetPerfSampleDataOffset(const event_t& event) {
117 uint64_t offset = kuint64max;
118 switch (event.header.type) {
119 case PERF_RECORD_SAMPLE:
120 offset = offsetof(event_t, sample.array);
122 case PERF_RECORD_MMAP:
123 offset = sizeof(event.mmap) - sizeof(event.mmap.filename) +
124 GetUint64AlignedStringLength(event.mmap.filename);
126 case PERF_RECORD_FORK:
127 case PERF_RECORD_EXIT:
128 offset = sizeof(event.fork);
130 case PERF_RECORD_COMM:
131 offset = sizeof(event.comm) - sizeof(event.comm.comm) +
132 GetUint64AlignedStringLength(event.comm.comm);
134 case PERF_RECORD_LOST:
135 offset = sizeof(event.lost);
137 case PERF_RECORD_THROTTLE:
138 case PERF_RECORD_UNTHROTTLE:
139 offset = sizeof(event.throttle);
141 case PERF_RECORD_READ:
142 offset = sizeof(event.read);
144 case PERF_RECORD_MMAP2:
145 offset = sizeof(event.mmap2) - sizeof(event.mmap2.filename) +
146 GetUint64AlignedStringLength(event.mmap2.filename);
148 case SIMPLE_PERF_RECORD_KERNEL_SYMBOL:
152 LOG(FATAL) << "Unknown/unsupported event type " << event.header.type;
155 // Make sure the offset was valid
156 CHECK_NE(offset, kuint64max);
157 CHECK_EQ(offset % sizeof(uint64_t), 0U);
161 bool ReadFileToData(const string& filename, std::vector<char>* data) {
162 std::ifstream in(filename.c_str(), std::ios::binary);
164 LOG(ERROR) << "Failed to open file " << filename;
168 size_t length = in.tellg();
170 data->resize(length);
172 in.read(&(*data)[0], length);
175 LOG(ERROR) << "Error reading from file " << filename;
181 bool WriteDataToFile(const std::vector<char>& data, const string& filename) {
182 std::ofstream out(filename.c_str(), std::ios::binary);
183 out.seekp(0, std::ios::beg);
184 out.write(&data[0], data.size());
188 } // namespace quipper