OSDN Git Service

Merge "Simpleperf: Fix perf_data_path config"
[android-x86/system-extras.git] / perfprofd / quipper / perf_reader.h
1 // Copyright (c) 2012 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_PERF_READER_H_
6 #define CHROMIUMOS_WIDE_PROFILING_PERF_READER_H_
7
8 #include <stdint.h>
9
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <type_traits>
14 #include <vector>
15
16 #include "base/macros.h"
17
18 #include "perf_internals.h"
19 #include "quipper_string.h"
20 #include "perf_utils.h"
21
22 namespace quipper {
23
24 struct PerfFileAttr {
25   struct perf_event_attr attr;
26   std::vector<u64> ids;
27 };
28
29 // Based on code in tools/perf/util/header.c, the metadata are of the following
30 // formats:
31
32 // Based on kernel/perf_internals.h
33 const size_t kBuildIDArraySize = 20;
34 const size_t kBuildIDStringLength = kBuildIDArraySize * 2;
35
36 struct CStringWithLength {
37   u32 len;
38   string str;
39 };
40
41 struct PerfStringMetadata {
42   u32 type;
43   std::vector<CStringWithLength> data;
44 };
45
46 struct PerfUint32Metadata {
47   u32 type;
48   std::vector<uint32_t> data;
49 };
50
51 struct PerfUint64Metadata {
52   u32 type;
53   std::vector<uint64_t> data;
54 };
55
56 typedef u32 num_siblings_type;
57
58 struct PerfCPUTopologyMetadata {
59   std::vector<CStringWithLength> core_siblings;
60   std::vector<CStringWithLength> thread_siblings;
61 };
62
63 struct PerfNodeTopologyMetadata {
64   u32 id;
65   u64 total_memory;
66   u64 free_memory;
67   CStringWithLength cpu_list;
68 };
69
70 struct BufferWithSize;
71 struct ConstBufferWithSize;
72
73 class PerfReader {
74  public:
75   PerfReader() : sample_type_(0),
76                  read_format_(0),
77                  is_cross_endian_(0) {}
78   ~PerfReader();
79
80   // Makes |build_id| fit the perf format, by either truncating it or adding
81   // zeros to the end so that it has length kBuildIDStringLength.
82   static void PerfizeBuildIDString(string* build_id);
83
84   // Changes |build_id| to the best guess of what the build id was before going
85   // through perf.  Specifically, it keeps removing trailing sequences of four
86   // zero bytes (or eight '0' characters) until there are no more such
87   // sequences, or the build id would be empty if the process were repeated.
88   static void UnperfizeBuildIDString(string* build_id);
89
90   bool ReadFile(const string& filename);
91   bool ReadFromVector(const std::vector<char>& data);
92   bool ReadFromString(const string& str);
93   bool ReadFromPointer(const char* perf_data, size_t size);
94
95   // TODO(rohinmshah): GetSize should not use RegenerateHeader (so that it can
96   // be const).  Ideally, RegenerateHeader would be deleted and instead of
97   // having out_header_ as an instance variable, it would be computed
98   // dynamically whenever needed.
99
100   // Returns the size in bytes that would be written by any of the methods that
101   // write the entire perf data file (WriteFile, WriteToPointer, etc).
102   size_t GetSize();
103
104   bool WriteFile(const string& filename);
105   bool WriteToVector(std::vector<char>* data);
106   bool WriteToString(string* str);
107   bool WriteToPointer(char* buffer, size_t size);
108
109   bool RegenerateHeader();
110
111   // Stores the mapping from filenames to build ids in build_id_events_.
112   // Returns true on success.
113   // Note: If |filenames_to_build_ids| contains a mapping for a filename for
114   // which there is already a build_id_event in build_id_events_, a duplicate
115   // build_id_event will be created, and the old build_id_event will NOT be
116   // deleted.
117   bool InjectBuildIDs(const std::map<string, string>& filenames_to_build_ids);
118
119   // Replaces existing filenames with filenames from |build_ids_to_filenames|
120   // by joining on build ids.  If a build id in |build_ids_to_filenames| is not
121   // present in this parser, it is ignored.
122   bool Localize(const std::map<string, string>& build_ids_to_filenames);
123
124   // Same as Localize, but joins on filenames instead of build ids.
125   bool LocalizeUsingFilenames(const std::map<string, string>& filename_map);
126
127   // Stores a list of unique filenames found in MMAP/MMAP2 events into
128   // |filenames|.  Any existing data in |filenames| will be lost.
129   void GetFilenames(std::vector<string>* filenames) const;
130   void GetFilenamesAsSet(std::set<string>* filenames) const;
131
132   // Uses build id events to populate |filenames_to_build_ids|.
133   // Any existing data in |filenames_to_build_ids| will be lost.
134   // Note:  A filename returned by GetFilenames need not be present in this map,
135   // since there may be no build id event corresponding to the MMAP/MMAP2.
136   void GetFilenamesToBuildIDs(
137       std::map<string, string>* filenames_to_build_ids) const;
138
139   static bool IsSupportedEventType(uint32_t type);
140
141   // If a program using PerfReader calls events(), it could work with the
142   // resulting events by importing kernel/perf_internals.h.  This would also
143   // apply to other forms of data (attributes, event types, build ids, etc.)
144   // However, there is no easy way to work with the sample info within events.
145   // The following two methods have been added for this purpose.
146
147   // Extracts from a perf event |event| info about the perf sample that
148   // contains the event.  Stores info in |sample|.
149   bool ReadPerfSampleInfo(const event_t& event,
150                           struct perf_sample* sample) const;
151   // Writes |sample| info back to a perf event |event|.
152   bool WritePerfSampleInfo(const perf_sample& sample,
153                            event_t* event) const;
154
155   // Accessor funcs.
156   const std::vector<PerfFileAttr>& attrs() const {
157     return attrs_;
158   }
159
160   const std::vector<malloced_unique_ptr<event_t>>& events() const {
161     return events_;
162   }
163
164   const std::vector<perf_trace_event_type>& event_types() const {
165     return event_types_;
166   }
167
168   const std::vector<build_id_event*>& build_id_events() const {
169     return build_id_events_;
170   }
171
172   const std::vector<char>& tracing_data() const {
173     return tracing_data_;
174   }
175
176  protected:
177   bool ReadHeader(const ConstBufferWithSize& data);
178
179   bool ReadAttrs(const ConstBufferWithSize& data);
180   bool ReadAttr(const ConstBufferWithSize& data, size_t* offset);
181   bool ReadEventAttr(const ConstBufferWithSize& data, size_t* offset,
182                      perf_event_attr* attr);
183   bool ReadUniqueIDs(const ConstBufferWithSize& data, size_t num_ids,
184                      size_t* offset, std::vector<u64>* ids);
185
186   bool ReadEventTypes(const ConstBufferWithSize& data);
187   bool ReadEventType(const ConstBufferWithSize& data, size_t* offset);
188
189   bool ReadData(const ConstBufferWithSize& data);
190
191   // Reads metadata in normal mode.
192   bool ReadMetadata(const ConstBufferWithSize& data);
193   bool ReadTracingMetadata(const ConstBufferWithSize& data,
194                            size_t offset, size_t size);
195   bool ReadBuildIDMetadata(const ConstBufferWithSize& data, u32 type,
196                            size_t offset, size_t size);
197   bool ReadStringMetadata(const ConstBufferWithSize& data, u32 type,
198                           size_t offset, size_t size);
199   bool ReadUint32Metadata(const ConstBufferWithSize& data, u32 type,
200                           size_t offset, size_t size);
201   bool ReadUint64Metadata(const ConstBufferWithSize& data, u32 type,
202                           size_t offset, size_t size);
203   bool ReadCPUTopologyMetadata(const ConstBufferWithSize& data, u32 type,
204                                size_t offset, size_t size);
205   bool ReadNUMATopologyMetadata(const ConstBufferWithSize& data, u32 type,
206                                 size_t offset, size_t size);
207
208   // Read perf data from piped perf output data.
209   bool ReadPipedData(const ConstBufferWithSize& data);
210   bool ReadTracingMetadataEvent(const ConstBufferWithSize& data, size_t offset);
211
212   // Like WriteToPointer, but does not check if the buffer is large enough.
213   bool WriteToPointerWithoutCheckingSize(char* buffer, size_t size);
214
215   bool WriteHeader(const BufferWithSize& data) const;
216   bool WriteAttrs(const BufferWithSize& data) const;
217   bool WriteEventTypes(const BufferWithSize& data) const;
218   bool WriteData(const BufferWithSize& data) const;
219   bool WriteMetadata(const BufferWithSize& data) const;
220
221   // For writing the various types of metadata.
222   bool WriteBuildIDMetadata(u32 type, size_t* offset,
223                             const BufferWithSize& data) const;
224   bool WriteStringMetadata(u32 type, size_t* offset,
225                            const BufferWithSize& data) const;
226   bool WriteUint32Metadata(u32 type, size_t* offset,
227                            const BufferWithSize& data) const;
228   bool WriteUint64Metadata(u32 type, size_t* offset,
229                            const BufferWithSize& data) const;
230   bool WriteEventDescMetadata(u32 type, size_t* offset,
231                               const BufferWithSize& data) const;
232   bool WriteCPUTopologyMetadata(u32 type, size_t* offset,
233                                 const BufferWithSize& data) const;
234   bool WriteNUMATopologyMetadata(u32 type, size_t* offset,
235                                  const BufferWithSize& data) const;
236
237   // For reading event blocks within piped perf data.
238   bool ReadAttrEventBlock(const ConstBufferWithSize& data, size_t offset,
239                           size_t size);
240   bool ReadPerfEventBlock(const event_t& event);
241
242   // Returns the number of types of metadata stored.
243   size_t GetNumMetadata() const;
244
245   // For computing the sizes of the various types of metadata.
246   size_t GetBuildIDMetadataSize() const;
247   size_t GetStringMetadataSize() const;
248   size_t GetUint32MetadataSize() const;
249   size_t GetUint64MetadataSize() const;
250   size_t GetEventDescMetadataSize() const;
251   size_t GetCPUTopologyMetadataSize() const;
252   size_t GetNUMATopologyMetadataSize() const;
253
254   // Returns true if we should write the number of strings for the string
255   // metadata of type |type|.
256   bool NeedsNumberOfStringData(u32 type) const;
257
258   // Replaces existing filenames in MMAP/MMAP2 events based on |filename_map|.
259   // This method does not change |build_id_events_|.
260   bool LocalizeMMapFilenames(const std::map<string, string>& filename_map);
261
262   std::vector<PerfFileAttr> attrs_;
263   std::vector<perf_trace_event_type> event_types_;
264   std::vector<malloced_unique_ptr<event_t>> events_;
265   std::vector<build_id_event*> build_id_events_;
266   std::vector<PerfStringMetadata> string_metadata_;
267   std::vector<PerfUint32Metadata> uint32_metadata_;
268   std::vector<PerfUint64Metadata> uint64_metadata_;
269   PerfCPUTopologyMetadata cpu_topology_;
270   std::vector<PerfNodeTopologyMetadata> numa_topology_;
271   std::vector<char> tracing_data_;
272   uint64_t sample_type_;
273   uint64_t read_format_;
274   uint64_t metadata_mask_;
275
276   // Indicates that the perf data being read is from machine with a different
277   // endianness than the current machine.
278   bool is_cross_endian_;
279
280  private:
281   u32 ReadPerfEventAttrSize(const ConstBufferWithSize& data,
282                             size_t attr_offset);
283
284   // The file header is either a normal header or a piped header.
285   union {
286     struct perf_file_header header_;
287     struct perf_pipe_file_header piped_header_;
288   };
289   struct perf_file_header out_header_;
290
291   DISALLOW_COPY_AND_ASSIGN(PerfReader);
292 };
293
294 }  // namespace quipper
295
296 #endif  // CHROMIUMOS_WIDE_PROFILING_PERF_READER_H_