Home | History | Annotate | Download | only in simpleperf
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SIMPLE_PERF_RECORD_FILE_H_
     18 #define SIMPLE_PERF_RECORD_FILE_H_
     19 
     20 #include <stdio.h>
     21 
     22 #include <functional>
     23 #include <map>
     24 #include <memory>
     25 #include <string>
     26 #include <vector>
     27 
     28 #include <android-base/macros.h>
     29 
     30 #include "perf_event.h"
     31 #include "record.h"
     32 #include "record_file_format.h"
     33 
     34 struct AttrWithId {
     35   const perf_event_attr* attr;
     36   std::vector<uint64_t> ids;
     37 };
     38 
     39 // RecordFileWriter writes to a perf record file, like perf.data.
     40 class RecordFileWriter {
     41  public:
     42   static std::unique_ptr<RecordFileWriter> CreateInstance(const std::string& filename);
     43 
     44   ~RecordFileWriter();
     45 
     46   bool WriteAttrSection(const std::vector<AttrWithId>& attr_ids);
     47   bool WriteData(const void* buf, size_t len);
     48 
     49   bool WriteData(const std::vector<char>& data) {
     50     return WriteData(data.data(), data.size());
     51   }
     52 
     53   bool WriteFeatureHeader(size_t feature_count);
     54   bool WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records);
     55   bool WriteFeatureString(int feature, const std::string& s);
     56   bool WriteCmdlineFeature(const std::vector<std::string>& cmdline);
     57   bool WriteBranchStackFeature();
     58 
     59   // Normally, Close() should be called after writing. But if something
     60   // wrong happens and we need to finish in advance, the destructor
     61   // will take care of calling Close().
     62   bool Close();
     63 
     64  private:
     65   RecordFileWriter(const std::string& filename, FILE* fp);
     66   void GetHitModulesInBuffer(const char* p, const char* end,
     67                              std::vector<std::string>* hit_kernel_modules,
     68                              std::vector<std::string>* hit_user_files);
     69   bool WriteFileHeader();
     70   bool Write(const void* buf, size_t len);
     71   bool SeekFileEnd(uint64_t* file_end);
     72   bool WriteFeatureBegin(uint64_t* start_offset);
     73   bool WriteFeatureEnd(int feature, uint64_t start_offset);
     74 
     75   const std::string filename_;
     76   FILE* record_fp_;
     77 
     78   perf_event_attr event_attr_;
     79   uint64_t attr_section_offset_;
     80   uint64_t attr_section_size_;
     81   uint64_t data_section_offset_;
     82   uint64_t data_section_size_;
     83 
     84   std::vector<int> features_;
     85   int feature_count_;
     86   int current_feature_index_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(RecordFileWriter);
     89 };
     90 
     91 // RecordFileReader read contents from a perf record file, like perf.data.
     92 class RecordFileReader {
     93  public:
     94   static std::unique_ptr<RecordFileReader> CreateInstance(const std::string& filename);
     95 
     96   ~RecordFileReader();
     97 
     98   const PerfFileFormat::FileHeader& FileHeader() const {
     99     return header_;
    100   }
    101 
    102   const std::vector<PerfFileFormat::FileAttr>& AttrSection() const {
    103     return file_attrs_;
    104   }
    105 
    106   const std::map<int, PerfFileFormat::SectionDesc>& FeatureSectionDescriptors() const {
    107     return feature_section_descriptors_;
    108   }
    109 
    110   bool ReadIdsForAttr(const PerfFileFormat::FileAttr& attr, std::vector<uint64_t>* ids);
    111   // If sorted is true, sort records before passing them to callback function.
    112   bool ReadDataSection(std::function<bool(std::unique_ptr<Record>)> callback, bool sorted = true);
    113   std::vector<std::string> ReadCmdlineFeature();
    114   std::vector<BuildIdRecord> ReadBuildIdFeature();
    115   std::string ReadFeatureString(int feature);
    116   bool Close();
    117 
    118   // For testing only.
    119   std::vector<std::unique_ptr<Record>> DataSection();
    120 
    121  private:
    122   RecordFileReader(const std::string& filename, FILE* fp);
    123   bool ReadHeader();
    124   bool ReadAttrSection();
    125   bool ReadFeatureSectionDescriptors();
    126   bool ReadFeatureSection(int feature, std::vector<char>* data);
    127 
    128   const std::string filename_;
    129   FILE* record_fp_;
    130 
    131   PerfFileFormat::FileHeader header_;
    132   std::vector<PerfFileFormat::FileAttr> file_attrs_;
    133   std::map<int, PerfFileFormat::SectionDesc> feature_section_descriptors_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(RecordFileReader);
    136 };
    137 
    138 #endif  // SIMPLE_PERF_RECORD_FILE_H_
    139