Home | History | Annotate | Download | only in trace_processor
      1 /*
      2  * Copyright (C) 2017 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 TOOLS_TRACE_PROCESSOR_VTSTRACEPROCESSOR_H_
     18 #define TOOLS_TRACE_PROCESSOR_VTSTRACEPROCESSOR_H_
     19 
     20 #include <android-base/macros.h>
     21 #include <test/vts/proto/VtsProfilingMessage.pb.h>
     22 #include <test/vts/proto/VtsReportMessage.pb.h>
     23 
     24 namespace android {
     25 namespace vts {
     26 
     27 class VtsTraceProcessor {
     28  public:
     29   VtsTraceProcessor() {};
     30   virtual ~VtsTraceProcessor() {};
     31 
     32   enum TraceSelectionMetric {
     33     MAX_COVERAGE,
     34     MAX_COVERAGE_SIZE_RATIO,
     35   };
     36   // Cleanups the given trace file/all trace files under the given directory to
     37   // be used for replaying. Current cleanup depends on the trace type:
     38   //   1. For sever side trace, remove client side and passthrough records.
     39   //   2. For client side trace, remove server side and passthrough records.
     40   //   3. For passthrough trace, remove server and client side records.
     41   void CleanupTraces(const std::string& path);
     42   // Parses the given trace file and outputs the latency for each API call.
     43   void ProcessTraceForLatencyProfiling(const std::string& trace_file);
     44   // Parses all trace files under the the given trace directory and remove
     45   // duplicate trace file.
     46   void DedupTraces(const std::string& trace_dir);
     47   // Selects a subset of trace files from a give trace set based on their
     48   // corresponding coverage data that maximize the total coverage.
     49   // coverage_file_dir: directory that stores all the coverage data files.
     50   // trace_file_dir: directory that stores the corresponding trace files.
     51   // metric: metric used to select traces, currently support two metrics:
     52   //   1. MAX_COVERAGE: select trace that leads to the maximum coverage lines.
     53   //   2. MAX_COVERAGE_SIZE_RATIO: select trace that has the maximum coverage
     54   //      lines/trace size.
     55   void SelectTraces(
     56       const std::string& coverage_file_dir, const std::string& trace_file_dir,
     57       TraceSelectionMetric metric = TraceSelectionMetric::MAX_COVERAGE);
     58   // Reads a binary trace file, parse each trace event and print the proto.
     59   void ParseTrace(const std::string& trace_file);
     60   // Reads a text trace file, parse each trace event and convert it into a
     61   // binary trace file.
     62   void ConvertTrace(const std::string& trace_file);
     63 
     64  private:
     65   // Reads a binary trace file and parse each trace event into
     66   // VtsProfilingRecord.
     67   bool ParseBinaryTrace(const std::string& trace_file, bool ignore_timestamp,
     68                         bool entry_only, VtsProfilingMessage* profiling_msg);
     69 
     70   // Reads a text trace file and parse each trace event into
     71   // VtsProfilingRecord.
     72   bool ParseTextTrace(const std::string& trace_file,
     73                       VtsProfilingMessage* profiling_msg);
     74 
     75   // Writes the given VtsProfilingMessage into an output file.
     76   bool WriteProfilingMsg(const std::string& output_file,
     77                          const VtsProfilingMessage& profiling_msg);
     78 
     79   // Internal method to cleanup a trace file.
     80   void CleanupTraceFile(const std::string& trace_file);
     81   // Reads a test report file that contains the coverage data and parse it into
     82   // TestReportMessage.
     83   bool ParseCoverageData(const std::string& coverage_file,
     84                          TestReportMessage* report_msg);
     85   // Updates msg_to_be_updated by removing all the covered lines in ref_msg
     86   // and recalculates the count of covered lines accordingly.
     87   void UpdateCoverageData(const CoverageReportMessage& ref_msg,
     88                           CoverageReportMessage* msg_to_be_updated);
     89   // Helper method to calculate total coverage line in the given report message.
     90   long GetTotalCoverageLine(const TestReportMessage& msg);
     91   // Helper method to calculate total code line in the given report message.
     92   long GetTotalLine(const TestReportMessage& msg);
     93   // Helper method to extract the trace file name from the given coverage file
     94   // name.
     95   std::string GetTraceFileName(const std::string& coverage_file_name);
     96   // Helper method to check whether the given event is an entry event.
     97   bool isEntryEvent(const InstrumentationEventType& event);
     98   // Helper method to check whether the given two records are paired records.
     99   // Paired records means the two records are for the same hal interface, and
    100   // have corresponding entry/exit events.
    101   bool isPairedRecord(const VtsProfilingRecord& entry_record,
    102                       const VtsProfilingRecord& exit_record);
    103 
    104   // Struct to store the coverage data.
    105   struct CoverageInfo {
    106     TestReportMessage coverage_msg;
    107     std::string trace_file_name;
    108     long trace_file_size;
    109   };
    110 
    111   DISALLOW_COPY_AND_ASSIGN (VtsTraceProcessor);
    112 };
    113 
    114 }  // namespace vts
    115 }  // namespace android
    116 #endif  // TOOLS_TRACE_PROCESSOR_VTSTRACEPROCESSOR_H_
    117