blob: acf76003775ad7d3b40f4cf9c4c5b5ada17496fd [file] [log] [blame]
Raul Silvera0c3c35e2016-09-21 13:14:55 -07001/*
2 * Copyright (c) 2016, Google Inc.
3 * All rights reserved.
Googler3a8b0ae2017-07-11 18:19:34 -07004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
Raul Silvera0c3c35e2016-09-21 13:14:55 -07006 */
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 Silvera0c3c35e2016-09-21 13:14:55 -070015#include "string_compat.h"
Googler3a8b0ae2017-07-11 18:19:34 -070016#include "profile.pb.h"
Raul Silvera0c3c35e2016-09-21 13:14:55 -070017
18namespace quipper {
19class PerfDataProto;
20} // namespace quipper
21
22namespace perftools {
23
cjiang48a540d2017-05-26 12:14:22 -070024
25// Sample label options.
Raul Silvera0c3c35e2016-09-21 13:14:55 -070026enum SampleLabels {
27 kNoLabels = 0,
cjiang48a540d2017-05-26 12:14:22 -070028 // Adds label with key PidLabelKey and number value set to the process ID.
Raul Silvera0c3c35e2016-09-21 13:14:55 -070029 kPidLabel = 1,
cjiang48a540d2017-05-26 12:14:22 -070030 // Adds label with key TidLabelKey and number value set to the thread ID.
Raul Silvera0c3c35e2016-09-21 13:14:55 -070031 kTidLabel = 2,
Alexey Alexandrove18bc452016-11-04 14:55:48 -070032 // Equivalent to kPidLabel | kTidLabel
Raul Silvera0c3c35e2016-09-21 13:14:55 -070033 kPidAndTidLabels = 3,
cjiang48a540d2017-05-26 12:14:22 -070034 // 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 Silvera0c3c35e2016-09-21 13:14:55 -070036 kTimestampNsLabel = 4,
cjiang48a540d2017-05-26 12:14:22 -070037 // 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 Silvera0c3c35e2016-09-21 13:14:55 -070043};
44
cjiang48a540d2017-05-26 12:14:22 -070045// Sample label key names.
Raul Silvera0c3c35e2016-09-21 13:14:55 -070046const char PidLabelKey[] = "pid";
47const char TidLabelKey[] = "tid";
48const char TimestampNsLabelKey[] = "timestamp_ns";
49const char ExecutionModeLabelKey[] = "execution_mode";
cjiang48a540d2017-05-26 12:14:22 -070050const char CommLabelKey[] = "comm";
Raul Silvera0c3c35e2016-09-21 13:14:55 -070051
cjiang48a540d2017-05-26 12:14:22 -070052// Execution mode label values.
Raul Silvera0c3c35e2016-09-21 13:14:55 -070053const char ExecutionModeHostKernel[] = "Host Kernel";
54const char ExecutionModeHostUser[] = "Host User";
Alexey Alexandrove18bc452016-11-04 14:55:48 -070055const char ExecutionModeGuestKernel[] = "Guest Kernel";
Raul Silvera0c3c35e2016-09-21 13:14:55 -070056const char ExecutionModeGuestUser[] = "Guest User";
57const char ExecutionModeHypervisor[] = "Hypervisor";
58
cjiang48a540d2017-05-26 12:14:22 -070059// Perf data conversion options.
60enum 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
Googler3a8b0ae2017-07-11 18:19:34 -070072
cjiang48a540d2017-05-26 12:14:22 -070073struct 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.
86using ProcessProfiles = std::vector<std::unique_ptr<ProcessProfile>>;
87
88// Converts raw Linux perf data to a vector of process profiles.
Raul Silvera0c3c35e2016-09-21 13:14:55 -070089//
90// sample_labels is the OR-product of all SampleLabels desired in the output
cjiang48a540d2017-05-26 12:14:22 -070091// 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 Silvera0c3c35e2016-09-21 13:14:55 -070094//
cjiang48a540d2017-05-26 12:14:22 -070095// Returns a vector of process profiles, empty if any error occurs.
96extern 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 Silvera0c3c35e2016-09-21 13:14:55 -070099
cjiang48a540d2017-05-26 12:14:22 -0700100// Converts a PerfDataProto to a vector of process profiles.
101extern ProcessProfiles PerfDataProtoToProfiles(
102 const quipper::PerfDataProto* perf_data, uint32 sample_labels = kNoLabels,
103 uint32 options = kGroupByPids);
Raul Silvera0c3c35e2016-09-21 13:14:55 -0700104
Raul Silvera0c3c35e2016-09-21 13:14:55 -0700105} // namespace perftools
106
107#endif // PERFTOOLS_PERF_DATA_CONVERTER_H_