1 | /* Copyright 2022 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 | #ifndef TENSORFLOW_LITE_PROFILING_ROOT_PROFILER_H_ |
16 | #define TENSORFLOW_LITE_PROFILING_ROOT_PROFILER_H_ |
17 | |
18 | #include <cstdint> |
19 | #include <map> |
20 | #include <memory> |
21 | #include <vector> |
22 | |
23 | #include "tensorflow/lite/core/api/profiler.h" |
24 | |
25 | namespace tflite { |
26 | namespace profiling { |
27 | |
28 | /// A root profiler instance installed in TFLite runtime. |
29 | /// It's capable to dispatching profiling events to all child profilers attached |
30 | /// to it. Child profilers can either accept for discard the events based on the |
31 | /// event type. |
32 | class RootProfiler : public Profiler { |
33 | public: |
34 | RootProfiler() = default; |
35 | ~RootProfiler() override = default; |
36 | |
37 | // Not copiable. |
38 | RootProfiler(const RootProfiler&) = delete; |
39 | RootProfiler& operator=(const RootProfiler&) = delete; |
40 | |
41 | // Movable. |
42 | RootProfiler(RootProfiler&&) = default; |
43 | RootProfiler& operator=(RootProfiler&&) = default; |
44 | |
45 | /// Adds a profiler to root profiler. |
46 | /// Added `profiler` should not be nullptr or it will be ignored. |
47 | /// Caller must retains the ownership. The lifetime should exceed the |
48 | /// lifetime of the RootProfiler. |
49 | void AddProfiler(Profiler* profiler); |
50 | |
51 | /// Adds a profiler to RootProfiler. |
52 | /// Added `profiler` should not be nullptr or it will be ignored. |
53 | /// Transfers the ownership of `profiler` to RootProfiler. |
54 | void AddProfiler(std::unique_ptr<Profiler>&& profiler); |
55 | |
56 | /// Signals the beginning of an event to all child profilers. |
57 | /// The `tag`, `event_metadata1` and `event_metadata2` arguments have |
58 | /// different interpretations based on the actual Profiler instance |
59 | /// and the `event_type`. |
60 | /// Returns a handle to the profile event which can be used in a later |
61 | /// `EndEvent` call. |
62 | uint32_t BeginEvent(const char* tag, EventType event_type, |
63 | int64_t event_metadata1, |
64 | int64_t event_metadata2) override; |
65 | |
66 | /// Signals an end to the specified profile event to all child profilers with |
67 | /// 'event_metadata's. |
68 | /// An invalid event handle (e.g. not a value returned from BeginEvent call or |
69 | /// a handle invalidated by RemoveChildProfilers) will be ignored. |
70 | void EndEvent(uint32_t event_handle, int64_t event_metadata1, |
71 | int64_t event_metadata2) override; |
72 | /// Signals an end to the specified profile event to all child profilers. |
73 | /// An invalid event handle (e.g. not a value returned from BeginEvent call or |
74 | /// a handle invalidated by RemoveChildProfilers) will be ignored. |
75 | void EndEvent(uint32_t event_handle) override; |
76 | |
77 | /// Appends an event of type 'event_type' with 'tag' and 'event_metadata' |
78 | /// The `tag`, `metric`, `event_metadata1` and `event_metadata2` arguments |
79 | /// have different interpretations based on the actual Profiler instance and |
80 | /// the `event_type`. |
81 | void AddEvent(const char* tag, EventType event_type, uint64_t metric, |
82 | int64_t event_metadata1, int64_t event_metadata2) override; |
83 | |
84 | // Adds a profiler event with data. |
85 | // Data will be a const TelemetrySettings* for TELEMETRY_REPORT_SETTINGS |
86 | // and TELEMETRY_DELEGATE_REPORT_SETTINGS. |
87 | void AddEventWithData(const char* tag, EventType event_type, |
88 | const void* data) override; |
89 | |
90 | /// Removes all child profilers and releases the child profiler if it's owned |
91 | /// by the root profiler. Also invalidates all event handles generated |
92 | /// from previous `BeginEvent` calls. |
93 | void RemoveChildProfilers(); |
94 | |
95 | private: |
96 | uint32_t next_event_id_ = 1; |
97 | std::vector<std::unique_ptr<Profiler>> owned_profilers_; |
98 | std::vector<Profiler*> profilers_; |
99 | std::map<uint32_t, std::vector<uint32_t>> events_; |
100 | }; |
101 | |
102 | } // namespace profiling |
103 | } // namespace tflite |
104 | |
105 | #endif // TENSORFLOW_LITE_PROFILING_ROOT_PROFILER_H_ |
106 | |