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:
VtsTraceProcessor(VtsCoverageProcessor * coverage_processor)30   explicit VtsTraceProcessor(VtsCoverageProcessor* coverage_processor)
31       : coverage_processor_(coverage_processor){};
~VtsTraceProcessor()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   // Util method to get the string representing the full API name, e.g.
113   // android.hardware.foo@1.0::IFoo:open
114   std::string GetFullApiStr(const VtsProfilingRecord& record);
115 
116   // Struct to store the coverage data.
117   struct CoverageInfo {
118     TestReportMessage coverage_msg;
119     std::string trace_file_name;
120     long trace_file_size;
121   };
122 
123   // Struct to store the trace summary data.
124   struct TraceSummary {
125     // Name of test module that generates the trace. e.g. CtsUsbTests.
126     std::string test_name;
127     // Hal package name. e.g. android.hardware.light
128     std::string package;
129     // Hal major version, e.g. 1.0 -> 1
130     int version_major;
131     // Hal minor version, e.g. 1.0 -> 0
132     int version_minor;
133     // Total number of API calls recorded in the trace.
134     long total_api_count;
135     // Total number of different APIs recorded in the trace.
136     long unique_api_count;
137     // Call statistics for each API: <API_name, number_called>
138     std::map<std::string, long> api_stats;
139 
TraceSummaryTraceSummary140     TraceSummary(std::string test_name, std::string package, int version_major,
141                  int version_minor, long total_api_count, long unique_api_count,
142                  std::map<std::string, long> api_stats)
143         : test_name(test_name),
144           package(package),
145           version_major(version_major),
146           version_minor(version_minor),
147           total_api_count(total_api_count),
148           unique_api_count(unique_api_count),
149           api_stats(api_stats){};
150   };
151 
152   // Internal method to parse all trace files under test_trace_dir and create
153   // the mapping from each hal@version to the list of test that access it.
154   void GetHalTraceMapping(
155       const std::string& test_trace_dir,
156       std::map<std::string, std::vector<TraceSummary>>* hal_trace_mapping);
157 
158   // Internal method to parse a trace file and create the corresponding
159   // TraceSummary from it.
160   void GetHalTraceSummary(const std::string& trace_file,
161                           const std::string& test_name,
162                           std::vector<TraceSummary>* trace_summaries);
163 
164   // A class to process coverage reports. Not owned.
165   VtsCoverageProcessor* coverage_processor_;
166 
167   DISALLOW_COPY_AND_ASSIGN(VtsTraceProcessor);
168 };
169 
170 }  // namespace vts
171 }  // namespace android
172 #endif  // TOOLS_TRACE_PROCESSOR_VTSTRACEPROCESSOR_H_
173