1 /*
2  * Copyright 2016 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 __VTS_DRIVER_PROFILING_INTERFACE_H_
18 #define __VTS_DRIVER_PROFILING_INTERFACE_H_
19 
20 #include <android-base/macros.h>
21 #include <google/protobuf/io/zero_copy_stream_impl.h>
22 #include <hidl/HidlSupport.h>
23 #include <utils/Condition.h>
24 #include <fstream>
25 
26 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
27 
28 using namespace std;
29 
30 namespace android {
31 namespace vts {
32 
33 // Library class to trace, record and profile a HIDL HAL implementation.
34 class VtsProfilingInterface {
35  public:
36   explicit VtsProfilingInterface(const string& trace_file_path);
37 
38   virtual ~VtsProfilingInterface();
39 
40   // Get and create the VtsProfilingInterface singleton.
41   static VtsProfilingInterface& getInstance(const string& trace_file_path);
42 
43   // returns true if the given message is added to the tracing queue.
44   void AddTraceEvent(
45       android::hardware::details::HidlInstrumentor::InstrumentationEvent event,
46       const char* package, const char* version, const char* interface,
47       const FunctionSpecificationMessage& message);
48 
49  private:
50   // Internal method to get the corresponding trace file descriptor for a HAL
51   // with given package and version.
52   int GetTraceFile(const string& package, const string& version);
53   // Internal method to create a trace file based on the trace_file_path_prefix_
54   // the given package and version, the device info and the current time.
55   int CreateTraceFile(const string& package, const string& version);
56   // Get the current time in nano seconds.
57   int64_t NanoTime();
58 
59   // Prefix of all trace files.
60   string trace_file_path_prefix_;  // Prefix of the trace file.
61 
62   // map between the HAL to the trace file description.
63   map<string, int> trace_map_;
64   Mutex mutex_;  // Mutex used to synchronize the writing to the trace file.
65 
66   DISALLOW_COPY_AND_ASSIGN(VtsProfilingInterface);
67 };
68 
69 }  // namespace vts
70 }  // namespace android
71 
72 #endif  // __VTS_DRIVER_PROFILING_INTERFACE_H_
73