Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Google Inc. |
| 3 | * All rights reserved. |
Googler | 3a8b0ae | 2017-07-11 18:19:34 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef PERFTOOLS_PERF_DATA_CONVERTER_H_ |
| 9 | #define PERFTOOLS_PERF_DATA_CONVERTER_H_ |
| 10 | |
| 11 | #include <memory> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "int_compat.h" |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 15 | #include "string_compat.h" |
Googler | 3a8b0ae | 2017-07-11 18:19:34 -0700 | [diff] [blame] | 16 | #include "profile.pb.h" |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 17 | |
| 18 | namespace quipper { |
| 19 | class PerfDataProto; |
| 20 | } // namespace quipper |
| 21 | |
| 22 | namespace perftools { |
| 23 | |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 24 | |
| 25 | // Sample label options. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 26 | enum SampleLabels { |
| 27 | kNoLabels = 0, |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 28 | // Adds label with key PidLabelKey and number value set to the process ID. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 29 | kPidLabel = 1, |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 30 | // Adds label with key TidLabelKey and number value set to the thread ID. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 31 | kTidLabel = 2, |
Alexey Alexandrov | e18bc45 | 2016-11-04 14:55:48 -0700 | [diff] [blame] | 32 | // Equivalent to kPidLabel | kTidLabel |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 33 | kPidAndTidLabels = 3, |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 34 | // Adds label with key TimestampNsLabelKey and number value set to the number |
| 35 | // of nanoseconds since the system boot that this sample was taken. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 36 | kTimestampNsLabel = 4, |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 37 | // Adds label with key ExecutionModeLabelKey and string value set to one of |
| 38 | // the ExecutionMode* values. |
| 39 | kExecutionModeLabel = 8, |
| 40 | // Adds a label with key CommLabelKey and string value set to the sample's |
| 41 | // process's command. If no command is known, no label is added. |
| 42 | kCommLabel = 16, |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 45 | // Sample label key names. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 46 | const char PidLabelKey[] = "pid"; |
| 47 | const char TidLabelKey[] = "tid"; |
| 48 | const char TimestampNsLabelKey[] = "timestamp_ns"; |
| 49 | const char ExecutionModeLabelKey[] = "execution_mode"; |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 50 | const char CommLabelKey[] = "comm"; |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 51 | |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 52 | // Execution mode label values. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 53 | const char ExecutionModeHostKernel[] = "Host Kernel"; |
| 54 | const char ExecutionModeHostUser[] = "Host User"; |
Alexey Alexandrov | e18bc45 | 2016-11-04 14:55:48 -0700 | [diff] [blame] | 55 | const char ExecutionModeGuestKernel[] = "Guest Kernel"; |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 56 | const char ExecutionModeGuestUser[] = "Guest User"; |
| 57 | const char ExecutionModeHypervisor[] = "Hypervisor"; |
| 58 | |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 59 | // Perf data conversion options. |
| 60 | enum ConversionOptions { |
| 61 | // Default options. |
| 62 | kNoOptions = 0, |
| 63 | // Whether to produce multiple, per-process profiles from the single input |
| 64 | // perf data file. If not set, a single profile will be produced ((but you do |
| 65 | // still get a list of profiles back; it just has only one entry). |
| 66 | kGroupByPids = 1, |
| 67 | // Whether the conversion should fail if there is a detected mismatch between |
| 68 | // the main mapping in the sample data vs. mapping data. |
| 69 | kFailOnMainMappingMismatch = 2, |
| 70 | }; |
| 71 | |
Googler | 3a8b0ae | 2017-07-11 18:19:34 -0700 | [diff] [blame] | 72 | |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 73 | struct ProcessProfile { |
| 74 | // Process PID or 0 if no process grouping was requested. |
| 75 | // PIDs can duplicate if there was a PID reuse during the profiling session. |
| 76 | uint32 pid = 0; |
| 77 | // Profile proto data. |
| 78 | perftools::profiles::Profile data; |
| 79 | // Min timestamp of a sample, in nanoseconds since boot, or 0 if unknown. |
| 80 | int64 min_sample_time_ns = 0; |
| 81 | // Max timestamp of a sample, in nanoseconds since boot, or 0 if unknown. |
| 82 | int64 max_sample_time_ns = 0; |
| 83 | }; |
| 84 | |
| 85 | // Type alias for a random access sequence of owned ProcessProfile objects. |
| 86 | using ProcessProfiles = std::vector<std::unique_ptr<ProcessProfile>>; |
| 87 | |
| 88 | // Converts raw Linux perf data to a vector of process profiles. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 89 | // |
| 90 | // sample_labels is the OR-product of all SampleLabels desired in the output |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 91 | // profiles. options governs other conversion options such as whether per-PID |
| 92 | // profiles should be returned or all processes should be merged into the same |
| 93 | // profile. |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 94 | // |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 95 | // Returns a vector of process profiles, empty if any error occurs. |
| 96 | extern ProcessProfiles RawPerfDataToProfiles( |
| 97 | const void* raw, int raw_size, const std::map<string, string>& build_ids, |
| 98 | uint32 sample_labels = kNoLabels, uint32 options = kGroupByPids); |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 99 | |
cjiang | 48a540d | 2017-05-26 12:14:22 -0700 | [diff] [blame] | 100 | // Converts a PerfDataProto to a vector of process profiles. |
| 101 | extern ProcessProfiles PerfDataProtoToProfiles( |
| 102 | const quipper::PerfDataProto* perf_data, uint32 sample_labels = kNoLabels, |
| 103 | uint32 options = kGroupByPids); |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 104 | |
Raul Silvera | 0c3c35e | 2016-09-21 13:14:55 -0700 | [diff] [blame] | 105 | } // namespace perftools |
| 106 | |
| 107 | #endif // PERFTOOLS_PERF_DATA_CONVERTER_H_ |