// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

syntax = "proto2";
option cc_enable_arenas = true;

package quipper;

// Stores information from a perf session generated via running:
// "perf record"
//
// See $kernel/tools/perf/design.txt for more details.

// Next tag: 17
message PerfDataProto {
  // Perf event attribute. Stores the event description.
  // This data structure is defined in the linux kernel:
  // $kernel/include/uapi/linux/perf_event.h.
  // Next tag: 42
  message PerfEventAttr {
    // Type of the event. Type is an enumeration and can be one of the values
    // described at: $kernel/include/linux/perf_event.h.
    // Example types are:
    // PERF_TYPE_HARDWARE
    // PERF_TYPE_SOFTWARE, etc.
    optional uint32 type = 1;

    // Size of the event data in bytes.
    optional uint32 size = 2;

    // The config stores the CPU-specific counter information.
    optional uint64 config = 3;

    // Sample period of the event. Indicates how often the event is
    // triggered in terms of # of events. After |sample_period| events, an event
    // will be recorded and stored.
    optional uint64 sample_period = 4;

    // Sample frequency of the event. Indicates how often the event is
    // triggered in terms of # per second. The kernel will try to record
    // |sample_freq| events per second.
    optional uint64 sample_freq = 5;

    // Sample type is a bitfield that records attributes of the sample. Example,
    // whether an entire callchain was recorded, etc.
    optional uint64 sample_type = 6;

    // Bitfield that indicates whether reads on the counter will return the
    // total time enabled and total time running.
    optional uint64 read_format = 7;

    // Indicates whether the counter starts off disabled.
    optional bool disabled = 8;

    // Indicates whether child processes inherit the counter.
    optional bool inherit = 9;

    // Indicates whether the counter is pinned to a particular CPU.
    optional bool pinned = 10;

    // Indicates whether this counter's group has exclusive access to the CPU's
    // counters.
    optional bool exclusive = 11;

    // The following bits restrict events to be counted when the CPU is in user,
    // kernel, hypervisor or idle modes.
    optional bool exclude_user = 12;
    optional bool exclude_kernel = 13;
    optional bool exclude_hv = 14;
    optional bool exclude_idle = 15;

    // Indicates whether mmap events should be recorded.
    optional bool mmap = 16;

    // Indicates whether process comm information should be recorded upon
    // process creation.
    optional bool comm = 17;

    // Indicates that we are in frequency mode, not period mode.
    optional bool freq = 18;

    // Indicates whether we have per-task counts.
    optional bool inherit_stat = 19;

    // Indicates whether we enable perf events after an exec() function call.
    optional bool enable_on_exec = 20;

    // Indicates whether we trace fork/exit.
    optional bool task = 21;

    // Indicates whether we are using a watermark to wake up.
    optional bool watermark = 22;

    // CPUs often "skid" when recording events. That means the instruction
    // pointer may not be the same as the one that caused the counter overflow.
    // Indicates the capabilities of the CPU in terms of recording precise
    // instruction pointer.
    optional uint32 precise_ip = 23;

    // Indicates whether we have non-exec mmap data.
    optional bool mmap_data = 24;

    // If set, all the event types will have the same sample_type.
    optional bool sample_id_all = 25;

    // Indicates whether we are counting events from the host (when running a
    // VM).
    optional bool exclude_host = 26;

    // Exclude events that happen on a guest OS.
    optional bool exclude_guest = 27;

    // Exclude kernel callchains.
    optional bool exclude_callchain_kernel = 36;

    // Exclude user callchains.
    optional bool exclude_callchain_user = 37;

    // Include mmap2 events that have inode data.
    optional bool mmap2 = 38;

    // Flag comm events that are due to an exec.
    optional bool comm_exec = 39;

    // Contains the number of events after which we wake up.
    optional uint32 wakeup_events = 28;

    // Contains the number of bytes after which we wake up.
    optional uint32 wakeup_watermark = 29;

    // Information about the type of the breakpoint.
    optional uint32 bp_type = 30;

    // Contains the breakpoint address.
    optional uint64 bp_addr = 31;

    // This is an extension of config (see above).
    optional uint64 config1 = 32;

    // The length of the breakpoint data in bytes.
    optional uint64 bp_len = 33;

    // This is an extension of config (see above).
    optional uint64 config2 = 34;

    // Contains the type of branch, example: user, kernel, call, return, etc.
    optional uint64 branch_sample_type = 35;

    // Defines set of user regs to dump on samples.
    optional uint64 sample_regs_user = 40;

    // Defines size of the user stack to dump on samples.
    optional uint32 sample_stack_user = 41;
  }

  // Describes a perf.data file attribute.
  // Next tag: 3
  message PerfFileAttr {
    optional PerfEventAttr attr = 1;

    // List of perf file attribute ids. Each id describes an event.
    repeated uint64 ids = 2;
  }

  // Protobuf version of the perf_event_type struct found in perf/util/event.h.
  // Contains the name of the event (such as "cycles" or "branch-misses") and
  // the event id (which is not unique).
  // Next tag: 4
  message PerfEventType {
    // Event id.  This is not unique across event types.
    // The combination of the event id and the type field in PerfEventAttr is
    // unique across event types.
    optional uint64 id = 1;

    // Event name.
    optional string name = 2;

    // Event name's md5 prefix.
    optional uint64 name_md5_prefix = 3;
  }

  // This message contains information about a perf sample itself, as opposed to
  // a perf event captured by a sample.
  // Next tag: 7
  message SampleInfo {
    // Process ID / thread ID from which this sample was taken.
    optional uint32 pid = 1;
    optional uint32 tid = 2;

    // Time this sample was taken (NOT the same as an event time).
    // It is the number of nanoseconds since bootup.
    optional uint64 sample_time_ns = 3;

    // The ID of the sample's event type (cycles, instructions, etc).
    // The event type IDs are defined in PerfFileAttr.
    optional uint64 id = 4;

    // The CPU on which this sample was taken.
    optional uint32 cpu = 5;

    // The stream id of the sample.
    optional uint64 stream_id = 6;
  }

  // Next tag: 7
  message CommEvent {
    // Process id.
    optional uint32 pid = 1;

    // Thread id.
    optional uint32 tid = 2;

    // Comm string.
    optional string comm = 3;

    // Comm string's md5 prefix.
    optional uint64 comm_md5_prefix = 4;

    // Time the sample was taken.
    // Deprecated, use |sample_info| instead.
    optional uint64 sample_time = 5 [deprecated = true];

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 6;
  }

  // Represents both mmap_event and mmap2_event.
  // Next tag: 15
  message MMapEvent {
    // Process id.
    optional uint32 pid = 1;

    // Thread id.
    optional uint32 tid = 2;

    // Start address.
    optional uint64 start = 3;

    // Length.
    optional uint64 len = 4;

    // PG Offset.
    optional uint64 pgoff = 5;

    // Only in MMAP2 events, information about the mapped inode:
    // Major/minor numbers
    optional uint32 maj = 9;
    optional uint32 min = 10;
    // Inode number and generation.
    optional uint64 ino = 11;
    optional uint64 ino_generation = 12;
    // Protection bits and flags.
    optional uint32 prot = 13;
    optional uint32 flags = 14;

    // In both MMAP and MMAP2 events:

    // Filename.
    optional string filename = 6;

    // Filename's md5 prefix.
    optional uint64 filename_md5_prefix = 7;

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 8;
  }

  // Next tag: 4
  message ReadInfo {
    optional uint64 time_enabled = 1;

    optional uint64 time_running = 2;

    message ReadValue {
      optional uint64 value = 1;
      optional uint64 id = 2;
    }

    // Based on the value of |PerfEventAttr::read_format & PERF_FORMAT_GROUP|,
    // the read info could contain one or multiple read values and IDs. If the
    // format is non-grouped, the repeated field will have only one entry.
    repeated ReadValue read_value = 3;
  }

  // Next tag: 4
  message BranchStackEntry {
    // Branch source address.
    optional uint64 from_ip = 1;

    // Branch destination address.
    optional uint64 to_ip = 2;

    // Indicates a mispredicted branch.
    optional bool mispredicted = 3;
  }

  // Next tag: 19
  message SampleEvent {
    // Instruction pointer.
    optional uint64 ip = 1;

    // Process id.
    optional uint32 pid = 2;

    // Thread id.
    optional uint32 tid = 3;

    // The time after boot when the sample was recorded, in nanoseconds.
    optional uint64 sample_time_ns = 4;

    // The address of the sample.
    optional uint64 addr = 5;

    // The id of the sample.
    optional uint64 id = 6;

    // The stream id of the sample.
    optional uint64 stream_id = 7;

    // The period of the sample.
    optional uint64 period = 8;

    // The CPU where the event was recorded.
    optional uint32 cpu = 9;

    // The raw size of the event in bytes.
    optional uint32 raw_size = 10;

    // The read field.
    optional ReadInfo read_info = 18;

    // Sample callchain info.
    repeated uint64 callchain = 11;

    // Branch stack info.
    repeated BranchStackEntry branch_stack = 12;

    // These are not yet implemented, but are listed as placeholders.
    //
    // optional RegsUser regs_user = 13;
    // optional StackUser stack_user = 14;

    // Sample weight for special events.
    optional uint64 weight = 15;

    // Sample data source flags.
    optional uint64 data_src = 16;

    // Sample transaction flags for special events.
    optional uint64 transaction = 17;
  }

  // ForkEvent is used for both FORK and EXIT events, which have the same data
  // format.  We don't want to call this "ForkOrExitEvent", in case a separate
  // exit event is introduced in the future.
  // Next tag: 12
  message ForkEvent {
    // Forked process ID.
    optional uint32 pid = 1;

    // Parent process ID.
    optional uint32 ppid = 2;

    // Forked process thread ID.
    optional uint32 tid = 3;

    // Parent process thread ID.
    optional uint32 ptid = 4;

    // Time of fork event in nanoseconds since bootup.
    optional uint64 fork_time_ns = 5;

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 11;
  }

  // Next tag: 4
  message LostEvent {
    // Id of the event which has been lost.  This should be an id found in a
    // PerfFileAttr.
    optional uint64 id = 1;

    // Number of events that were lost.
    optional uint64 lost = 2;

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 3;
  }

  // Next tag: 5
  message ThrottleEvent {
    // Time of throttle event, in nanoseconds since system startup.
    optional uint64 time_ns = 1;

    // Event ID.
    optional uint64 id = 2;

    // Stream ID.
    optional uint64 stream_id = 3;

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 4;
  }

  // Next tag: 8
  message ReadEvent {
    // Process ID.
    optional uint32 pid = 1;

    // Thread ID.
    optional uint32 tid = 2;

    // Value of the event counter when it was queried.
    optional uint64 value = 3;

    // Time enabled.
    optional uint64 time_enabled = 4;

    // Time running.
    optional uint64 time_running = 5;

    // ID.
    optional uint64 id = 6;

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 7;
  }

  // Next tag: 7
  message AuxEvent {
    // Aux offset.
    optional uint64 aux_offset = 1;

    // Aux size.
    optional uint64 aux_size = 2;

    // Is the record was truncated to fit.
    optional bool is_truncated = 3;

    // Does the record contain snapshot from overwrite mode.
    optional bool is_overwrite = 4;

    // Does the record contain gaps.
    optional bool is_partial = 5;

    // Info about the perf sample containing this event.
    optional SampleInfo sample_info = 6;
  }

  // Next tag: 8
  message AuxtraceEvent {
    // Size of AUX area tracing buffer.
    optional uint64 size = 1;

    // Offset as determined by aux_head / aux_tail members of struct
    // perf_event_mmap_page.
    optional uint64 offset = 2;

    // Implementation specific reference determined when the data is recorded.
    optional uint64 reference = 3;

    // Index of AUX area tracing data buffer.
    optional uint32 idx = 4;

    // In per-thread mode, the tid this buffer is associated with.
    optional uint32 tid = 5;

    // In per-cpu mode, the cpu this buffer is associated with.
    optional uint32 cpu = 6;

    // The trace data.
    optional bytes trace_data = 7;
  }

  // Next tag: 4
  message EventHeader {
    // Type of event.
    optional uint32 type = 1;
    optional uint32 misc = 2;
    // Size of event.
    optional uint32 size = 3;
  }

  // Next tag: 13
  message PerfEvent {
    optional EventHeader header = 1;
    oneof event_type {
      MMapEvent mmap_event = 2;
      SampleEvent sample_event = 3;
      CommEvent comm_event = 4;
      // FORK and EXIT events are structurally identical. They only differ by
      // the event type. But using two distinct fields allows us to
      // differentiate between them without having to check the event type under
      // |header|.
      ForkEvent fork_event = 5;
      ForkEvent exit_event = 9;
      LostEvent lost_event = 6;
      ThrottleEvent throttle_event = 7;
      ReadEvent read_event = 8;
      AuxEvent aux_event = 11;
      AuxtraceEvent auxtrace_event = 12;
    }
    // Time after boot in nanoseconds corresponding to the event.
    optional uint64 timestamp = 10;
  }

  // Next tag: 8
  message PerfEventStats {
    // Total number of events read from perf data.
    optional uint32 num_events_read = 1;

    // Total number of various types of events.
    optional uint32 num_sample_events = 2;
    optional uint32 num_mmap_events = 3;
    optional uint32 num_fork_events = 4;
    optional uint32 num_exit_events = 5;

    // Number of sample events that were successfully mapped by the address
    // mapper, a quipper module that is used to obscure addresses and convert
    // them to DSO name + offset.  Sometimes it fails to process sample events.
    // This field allows us to track the success rate of the address mapper.
    optional uint32 num_sample_events_mapped = 6;

    // Whether address remapping was enabled.
    optional bool did_remap = 7;
  }

  // Next tag: 3
  message PerfUint32Metadata {
    // Type of metadata, such as nrcpus.
    optional uint32 type = 1;

    // uint32 data.
    repeated uint32 data = 2;
  }

  // Next tag: 3
  message PerfUint64Metadata {
    // Type of metadata, such as totalmem.
    optional uint32 type = 1;

    // uint64 data.
    repeated uint64 data = 2;
  }

  // Next tag: 3
  message PerfTracingMetadata {
    // The trace event metadata.
    optional bytes tracing_data = 1;

    // Trace event metedata Md5sum prefix.
    optional uint64 tracing_data_md5_prefix = 2;
  }

  // Next tag: 6
  message PerfBuildID {
    // Misc field in perf_event_header.
    optional uint32 misc = 1;

    // Process ID.
    optional uint32 pid = 2;

    // Build id.  Should always contain kBuildIDArraySize bytes of data.
    // perf_reader.h defines kBuildIDArraySize = 20.
    optional bytes build_id_hash = 3;

    // Filename.
    optional string filename = 4;

    // Filename Md5sum prefix.
    optional uint64 filename_md5_prefix = 5;
  }

  // Next tag: 5
  message PerfCPUTopologyMetadata {
    // Core siblings.
    repeated string core_siblings = 1;

    // Core siblings' md5 prefixes.
    repeated uint64 core_siblings_md5_prefix = 2;

    // Thread siblings.
    repeated string thread_siblings = 3;

    // Thread siblings' md5 prefixes.
    repeated uint64 thread_siblings_md5_prefix = 4;
  }

  // Next tag: 6
  message PerfNodeTopologyMetadata {
    // Node id.
    optional uint32 id = 1;

    // Total memory of the node.
    optional uint64 total_memory = 2;

    // Free memory of the node.
    optional uint64 free_memory = 3;

    // List of CPUs in the node.
    optional string cpu_list = 4;

    // CPU list's md5 prefix.
    optional uint64 cpu_list_md5_prefix = 5;
  }

  // Next tag: 4
  message PerfPMUMappingsMetadata {
    // Mapping type.
    optional uint32 type = 1;

    // Mapping name.
    optional string name = 2;

    // Mapping name's md5 prefix.
    optional uint64 name_md5_prefix = 3;
  }

  // Next tag: 5
  message PerfGroupDescMetadata {
    // Group name.
    optional string name = 1;

    // Group name's md5 prefix.
    optional uint64 name_md5_prefix = 2;

    // Group's leader index.
    optional uint32 leader_idx = 3;

    // Number of members in the group.
    optional uint32 num_members = 4;
  }

  repeated PerfFileAttr file_attrs = 1;
  repeated PerfEvent events = 2;

  repeated PerfEventType event_types = 10;

  // Time when quipper generated this perf data / protobuf, given as seconds
  // since the epoch.
  optional uint64 timestamp_sec = 3;

  // Records some stats about the serialized perf events.
  optional PerfEventStats stats = 4;

  // Bit mask used to determine what metadata has been included.
  // At the moment, only the first number is actually used.
  // See adds_features in perf_reader.cc
  repeated uint64 metadata_mask = 5;

  optional PerfTracingMetadata tracing_data = 14;

  repeated PerfBuildID build_ids = 7;

  repeated PerfUint32Metadata uint32_metadata = 8;

  repeated PerfUint64Metadata uint64_metadata = 9;

  optional PerfCPUTopologyMetadata cpu_topology = 11;

  repeated PerfNodeTopologyMetadata numa_topology = 12;

  repeated PerfPMUMappingsMetadata pmu_mappings = 15;

  repeated PerfGroupDescMetadata group_desc = 16;

  // Next tag: 9
  message StringMetadata {
    // Next tag: 3
    message StringAndMd5sumPrefix {
      // The string value.
      optional string value = 1;

      // The string value's md5sum prefix.
      optional uint64 value_md5_prefix = 2;
    }

    // Name of the machine, e.g. "localhost".
    optional StringAndMd5sumPrefix hostname = 1;

    // Kernel version, e.g. "3.4.0".
    optional StringAndMd5sumPrefix kernel_version = 2;

    // Perf version, e.g. "3.4.2642.g0aa604".
    optional StringAndMd5sumPrefix perf_version = 3;

    // CPU architecture family, e.g. "x86_64".
    optional StringAndMd5sumPrefix architecture = 4;

    // CPU description, e.g. "Intel(R) Celeron(R) CPU 867 @ 1.30GHz".
    optional StringAndMd5sumPrefix cpu_description = 5;

    // CPU ID string, with the format: "$VENDOR,$FAMILY,$MODEL,$STEP"
    optional StringAndMd5sumPrefix cpu_id = 6;

    // Command line used to run perf to collect this profile.
    // This is split into string tokens to reflect the way it is stored in the
    // raw perf data.  e.g. "perf record -a -- sleep 2" become stored as:
    //   { "perf", "record", "-a", "--", "sleep", "2" }
    repeated StringAndMd5sumPrefix perf_command_line_token = 7;

    // The command line stored as a single string.
    optional StringAndMd5sumPrefix perf_command_line_whole = 8;
  }

  optional StringMetadata string_metadata = 13;

  extensions 32 to 100;
}