1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/profiling/atrace_profiler.h"
16 
17 #include <dlfcn.h>
18 #if defined(__ANDROID__)
19 #include <sys/system_properties.h>
20 #endif
21 
22 #include <type_traits>
23 
24 #include <string>
25 
26 namespace tflite {
27 namespace profiling {
28 
29 // Profiler reporting to ATrace.
30 class ATraceProfiler : public tflite::Profiler {
31  public:
32   using FpIsEnabled = std::add_pointer<bool()>::type;
33   using FpBeginSection = std::add_pointer<void(const char*)>::type;
34   using FpEndSection = std::add_pointer<void()>::type;
35 
ATraceProfiler()36   ATraceProfiler() {
37     handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
38     if (handle_) {
39       // Use dlsym() to prevent crashes on devices running Android 5.1
40       // (API level 22) or lower.
41       atrace_is_enabled_ =
42           reinterpret_cast<FpIsEnabled>(dlsym(handle_, "ATrace_isEnabled"));
43       atrace_begin_section_ = reinterpret_cast<FpBeginSection>(
44           dlsym(handle_, "ATrace_beginSection"));
45       atrace_end_section_ =
46           reinterpret_cast<FpEndSection>(dlsym(handle_, "ATrace_endSection"));
47 
48       if (!atrace_is_enabled_ || !atrace_begin_section_ ||
49           !atrace_end_section_) {
50         dlclose(handle_);
51         handle_ = nullptr;
52       }
53     }
54   }
55 
~ATraceProfiler()56   ~ATraceProfiler() override {
57     if (handle_) {
58       dlclose(handle_);
59     }
60   }
61 
BeginEvent(const char * tag,EventType event_type,int64_t event_metadata1,int64_t event_metadata2)62   uint32_t BeginEvent(const char* tag, EventType event_type,
63                       int64_t event_metadata1,
64                       int64_t event_metadata2) override {
65     if (handle_ && atrace_is_enabled_()) {
66       // Note: When recording an OPERATOR_INVOKE_EVENT, we have recorded the op
67       // name
68       // as tag, node index as event_metadata1 and subgraph index as
69       // event_metadata2. See the macro TFLITE_SCOPED_TAGGED_OPERATOR_PROFILE
70       // defined in tensorflow/lite/core/api/profiler.h for details.
71       // Regardless the 'event_type', we encode the perfetto event name as
72       // tag@event_metadata1/event_metadata2. In case of OPERATOR_INVOKE_EVENT,
73       // the perfetto event name will be op_name@node_index/subgraph_index
74       std::string trace_event_tag = std::string(tag) + "@" +
75                                     std::to_string(event_metadata1) + "/" +
76                                     std::to_string(event_metadata2);
77       atrace_begin_section_(trace_event_tag.c_str());
78     }
79     return 0;
80   }
81 
EndEvent(uint32_t event_handle)82   void EndEvent(uint32_t event_handle) override {
83     if (handle_) {
84       atrace_end_section_();
85     }
86   }
87 
88  private:
89   // Handle to libandroid.so library. Null if not supported.
90   void* handle_;
91   FpIsEnabled atrace_is_enabled_;
92   FpBeginSection atrace_begin_section_;
93   FpEndSection atrace_end_section_;
94 };
95 
MaybeCreateATraceProfiler()96 std::unique_ptr<tflite::Profiler> MaybeCreateATraceProfiler() {
97 #if defined(TFLITE_ENABLE_DEFAULT_PROFILER)
98   return std::unique_ptr<tflite::Profiler>(new ATraceProfiler());
99 #else  // TFLITE_ENABLE_DEFAULT_PROFILER
100 #if defined(__ANDROID__)
101   constexpr char kTraceProp[] = "debug.tflite.trace";
102   char trace_enabled[PROP_VALUE_MAX] = "";
103   int length = __system_property_get(kTraceProp, trace_enabled);
104   if (length == 1 && trace_enabled[0] == '1') {
105     return std::unique_ptr<tflite::Profiler>(new ATraceProfiler());
106   }
107 #endif  // __ANDROID__
108   return nullptr;
109 #endif  // TFLITE_ENABLE_DEFAULT_PROFILER
110 }
111 
112 }  // namespace profiling
113 }  // namespace tflite
114