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 #include "VtsCoverageProcessor.h"
     24 
     25 namespace android {
     26 namespace vts {
     27 
     28 class VtsTraceProcessor {
     29  public:
     30   VtsTraceProcessor(VtsCoverageProcessor* coverage_processor)
     31       : coverage_processor_(coverage_processor){};
     32   virtual ~VtsTraceProcessor(){};
     33 
     34   enum TraceSelectionMetric {
     35     MAX_COVERAGE,
     36     MAX_COVERAGE_SIZE_RATIO,
     37   };
     38   // Cleanups the given trace file/all trace files under the given directory to
     39   // be used for replaying. Current cleanup depends on the trace type:
     40   //   1. For sever side trace, remove client side and passthrough records.
     41   //   2. For client side trace, remove server side and passthrough records.
     42   //   3. For passthrough trace, remove server and client side records.
     43   void CleanupTraces(const std::string& path);
     44   // Parses the given trace file and outputs the latency for each API call.
     45   void ProcessTraceForLatencyProfiling(const std::string& trace_file);
     46   // Parses all trace files under the the given trace directory and remove
     47   // duplicate trace file.
     48   void DedupTraces(const std::string& trace_dir);
     49   // Selects a subset of trace files from a give trace set based on their
     50   // corresponding coverage data that maximize the total coverage.
     51   // coverage_file_dir: directory that stores all the coverage data files.
     52   // trace_file_dir: directory that stores the corresponding trace files.
     53   // metric: metric used to select traces, currently support two metrics:
     54   //   1. MAX_COVERAGE: select trace that leads to the maximum coverage lines.
     55   //   2. MAX_COVERAGE_SIZE_RATIO: select trace that has the maximum coverage
     56   //      lines/trace size.
     57   void SelectTraces(
     58       const std::string& coverage_file_dir, const std::string& trace_file_dir,
     59       TraceSelectionMetric metric = TraceSelectionMetric::MAX_COVERAGE);
     60   // Reads a binary trace file, parse each trace event and print the proto.
     61   void ParseTrace(const std::string& trace_file);
     62   // Reads a text trace file, parse each trace event and convert it into a
     63   // binary trace file.
     64   void ConvertTrace(const std::string& trace_file);
     65   // Parse all trace files under test_trace_dir and create a list of test
     66   // modules for each hal@version that access all apis covered by the whole test
     67   // set. (i.e. such list should be a subset of the whole test list that access
     68   // the corresponding hal@version)
     69   void GetTestListForHal(const std::string& test_trace_dir,
     70                          const std::string& output_file,
     71                          bool verbose_output = false);
     72 
     73  private:
     74   // Reads a binary trace file and parse each trace event into
     75   // VtsProfilingRecord.
     76   bool ParseBinaryTrace(const std::string& trace_file, bool ignore_timestamp,
     77                         bool entry_only, bool summary_only,
     78                         VtsProfilingMessage* profiling_msg);
     79 
     80   // Reads a text trace file and parse each trace event into
     81   // VtsProfilingRecord.
     82   bool ParseTextTrace(const std::string& trace_file,
     83                       VtsProfilingMessage* profiling_msg);
     84 
     85   // Writes the given VtsProfilingMessage into an output file.
     86   bool WriteProfilingMsg(const std::string& output_file,
     87                          const VtsProfilingMessage& profiling_msg);
     88 
     89   // Internal method to cleanup a trace file.
     90   void CleanupTraceFile(const std::string& trace_file);
     91   // Reads a test report file that contains the coverage data and parse it into
     92   // TestReportMessage.
     93   bool ParseCoverageData(const std::string& coverage_file,
     94                          TestReportMessage* report_msg);
     95   // Updates msg_to_be_updated by removing all the covered lines in ref_msg
     96   // and recalculates the count of covered lines accordingly.
     97   void UpdateCoverageData(const CoverageReportMessage& ref_msg,
     98                           CoverageReportMessage* msg_to_be_updated);
     99   // Helper method to calculate total coverage line in the given report message.
    100   long GetTotalCoverageLine(const TestReportMessage& msg);
    101   // Helper method to calculate total code line in the given report message.
    102   long GetTotalLine(const TestReportMessage& msg);
    103   // Helper method to extract the trace file name from the given file name.
    104   std::string GetTraceFileName(const std::string& coverage_file_name);
    105   // Helper method to check whether the given event is an entry event.
    106   bool isEntryEvent(const InstrumentationEventType& event);
    107   // Helper method to check whether the given two records are paired records.
    108   // Paired records means the two records are for the same hal interface, and
    109   // have corresponding entry/exit events.
    110   bool isPairedRecord(const VtsProfilingRecord& entry_record,
    111                       const VtsProfilingRecord& exit_record);
    112 
    113   // Struct to store the coverage data.
    114   struct CoverageInfo {
    115     TestReportMessage coverage_msg;
    116     std::string trace_file_name;
    117     long trace_file_size;
    118   };
    119 
    120   // Struct to store the trace summary data.
    121   struct TraceSummary {
    122     // Name of test module that generates the trace. e.g. CtsUsbTests.
    123     std::string test_name;
    124     // Hal package name. e.g. android.hardware.light
    125     std::string package;
    126     // Hal version e.g. 1.0
    127     float version;
    128     // Total number of API calls recorded in the trace.
    129     long total_api_count;
    130     // Total number of different APIs recorded in the trace.
    131     long unique_api_count;
    132     // Call statistics for each API: <API_name, number_called>
    133     std::map<std::string, long> api_stats;
    134 
    135     TraceSummary(std::string test_name, std::string package, float version,
    136                  long total_api_count, long unique_api_count,
    137                  std::map<std::string, long> api_stats)
    138         : test_name(test_name),
    139           package(package),
    140           version(version),
    141           total_api_count(total_api_count),
    142           unique_api_count(unique_api_count),
    143           api_stats(api_stats){};
    144   };
    145 
    146   // Internal method to parse all trace files under test_trace_dir and create
    147   // the mapping from each hal@version to the list of test that access it.
    148   void GetHalTraceMapping(
    149       const std::string& test_trace_dir,
    150       std::map<std::string, std::vector<TraceSummary>>* hal_trace_mapping);
    151 
    152   // Internal method to parse a trace file and create the corresponding
    153   // TraceSummary from it.
    154   void GetHalTraceSummary(const std::string& trace_file,
    155                           const std::string& test_name,
    156                           std::vector<TraceSummary>* trace_summaries);
    157 
    158   // A class to process coverage reports. Not owned.
    159   VtsCoverageProcessor* coverage_processor_;
    160 
    161   DISALLOW_COPY_AND_ASSIGN(VtsTraceProcessor);
    162 };
    163 
    164 }  // namespace vts
    165 }  // namespace android
    166 #endif  // TOOLS_TRACE_PROCESSOR_VTSTRACEPROCESSOR_H_
    167