Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (c) 2016, Google Inc.
      3  * All rights reserved.
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      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"
     15 #include "string_compat.h"
     16 #include "profile.pb.h"
     17 
     18 namespace quipper {
     19 class PerfDataProto;
     20 }  // namespace quipper
     21 
     22 namespace perftools {
     23 
     24 
     25 // Sample label options.
     26 enum SampleLabels {
     27   kNoLabels = 0,
     28   // Adds label with key PidLabelKey and number value set to the process ID.
     29   kPidLabel = 1,
     30   // Adds label with key TidLabelKey and number value set to the thread ID.
     31   kTidLabel = 2,
     32   // Equivalent to kPidLabel | kTidLabel
     33   kPidAndTidLabels = 3,
     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.
     36   kTimestampNsLabel = 4,
     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,
     43 };
     44 
     45 // Sample label key names.
     46 const char PidLabelKey[] = "pid";
     47 const char TidLabelKey[] = "tid";
     48 const char TimestampNsLabelKey[] = "timestamp_ns";
     49 const char ExecutionModeLabelKey[] = "execution_mode";
     50 const char CommLabelKey[] = "comm";
     51 
     52 // Execution mode label values.
     53 const char ExecutionModeHostKernel[] = "Host Kernel";
     54 const char ExecutionModeHostUser[] = "Host User";
     55 const char ExecutionModeGuestKernel[] = "Guest Kernel";
     56 const char ExecutionModeGuestUser[] = "Guest User";
     57 const char ExecutionModeHypervisor[] = "Hypervisor";
     58 
     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 
     72 
     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.
     89 //
     90 // sample_labels is the OR-product of all SampleLabels desired in the output
     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.
     94 //
     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);
     99 
    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);
    104 
    105 }  // namespace perftools
    106 
    107 #endif  // PERFTOOLS_PERF_DATA_CONVERTER_H_
    108