1 // Copyright 2015 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.
4 
5 #ifndef CHROMIUMOS_WIDE_PROFILING_SAMPLE_INFO_READER_H_
6 #define CHROMIUMOS_WIDE_PROFILING_SAMPLE_INFO_READER_H_
7 
8 #include <stdint.h>
9 
10 #include "kernel/perf_event.h"
11 
12 namespace quipper {
13 
14 // Forward declarations of structures.
15 union perf_event;
16 typedef perf_event event_t;
17 struct perf_sample;
18 
19 class SampleInfoReader {
20  public:
21   SampleInfoReader(struct perf_event_attr event_attr, bool read_cross_endian)
22       : event_attr_(event_attr), read_cross_endian_(read_cross_endian) {}
23 
24   bool ReadPerfSampleInfo(const event_t& event,
25                           struct perf_sample* sample) const;
26   bool WritePerfSampleInfo(const perf_sample& sample, event_t* event) const;
27 
28   // Given a general perf sample format |sample_type|, return the fields of that
29   // format that are present in a sample for an event of type |event_type|.
30   //
31   // e.g. FORK and EXIT events have the fields {time, pid/tid, cpu, id}.
32   // Given a sample type with fields {ip, time, pid/tid, and period}, return
33   // the intersection of these two field sets: {time, pid/tid}.
34   //
35   // All field formats are bitfields, as defined by enum
36   // perf_event_sample_format in kernel/perf_event.h.
37   static uint64_t GetSampleFieldsForEventType(uint32_t event_type,
38                                               uint64_t sample_type);
39 
40   // Returns the offset in bytes within a perf event structure at which the raw
41   // perf sample data is located.
42   static uint64_t GetPerfSampleDataOffset(const event_t& event);
43 
44   const perf_event_attr& event_attr() const { return event_attr_; }
45 
46  private:
47   // Event attribute info, which determines the contents of some perf_sample
48   // data.
49   struct perf_event_attr event_attr_;
50 
51   // Set this flag if values (uint32s and uint64s) should be endian-swapped
52   // during reads.
53   bool read_cross_endian_;
54 };
55 
56 }  // namespace quipper
57 
58 #endif  // CHROMIUMOS_WIDE_PROFILING_SAMPLE_INFO_READER_H_
59