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 | #include "tensorflow/lite/profiling/root_profiler.h" |
16 | |
17 | #include <memory> |
18 | #include <utility> |
19 | #include <vector> |
20 | |
21 | namespace tflite { |
22 | namespace profiling { |
23 | |
24 | void RootProfiler::AddProfiler(Profiler* profiler) { |
25 | if (profiler == nullptr) return; |
26 | profilers_.push_back(profiler); |
27 | } |
28 | |
29 | void RootProfiler::AddProfiler(std::unique_ptr<Profiler>&& profiler) { |
30 | if (profiler == nullptr) return; |
31 | owned_profilers_.emplace_back(std::move(profiler)); |
32 | profilers_.push_back(owned_profilers_.back().get()); |
33 | } |
34 | |
35 | uint32_t RootProfiler::BeginEvent(const char* tag, EventType event_type, |
36 | int64_t event_metadata1, |
37 | int64_t event_metadata2) { |
38 | // TODO(b/238913100): Remove the workaround. |
39 | if (profilers_.size() == 1) { |
40 | return profilers_[0]->BeginEvent(tag, event_type, event_metadata1, |
41 | event_metadata2); |
42 | } |
43 | auto id = next_event_id_++; |
44 | std::vector<uint32_t> event_ids; |
45 | event_ids.reserve(profilers_.size()); |
46 | for (auto* profiler : profilers_) { |
47 | event_ids.push_back(profiler->BeginEvent(tag, event_type, event_metadata1, |
48 | event_metadata2)); |
49 | } |
50 | events_.emplace(id, std::move(event_ids)); |
51 | return id; |
52 | } |
53 | |
54 | void RootProfiler::EndEvent(uint32_t event_handle, int64_t event_metadata1, |
55 | int64_t event_metadata2) { |
56 | // TODO(b/238913100): Remove the workaround. |
57 | if (profilers_.size() == 1) { |
58 | return profilers_[0]->EndEvent(event_handle, event_metadata1, |
59 | event_metadata2); |
60 | } |
61 | if (const auto it = events_.find(event_handle); it != events_.end()) { |
62 | const auto& event_ids = it->second; |
63 | for (auto idx = 0; idx < event_ids.size(); idx++) { |
64 | profilers_[idx]->EndEvent(event_ids[idx], event_metadata1, |
65 | event_metadata2); |
66 | } |
67 | events_.erase(it); |
68 | } |
69 | } |
70 | |
71 | void RootProfiler::EndEvent(uint32_t event_handle) { |
72 | // TODO(b/238913100): Remove the workaround. |
73 | if (profilers_.size() == 1) { |
74 | return profilers_[0]->EndEvent(event_handle); |
75 | } |
76 | if (const auto it = events_.find(event_handle); it != events_.end()) { |
77 | const auto& event_ids = it->second; |
78 | for (auto idx = 0; idx < event_ids.size(); idx++) { |
79 | profilers_[idx]->EndEvent(event_ids[idx]); |
80 | } |
81 | events_.erase(it); |
82 | } |
83 | } |
84 | |
85 | void RootProfiler::AddEvent(const char* tag, EventType event_type, |
86 | uint64_t metric, int64_t event_metadata1, |
87 | int64_t event_metadata2) { |
88 | for (auto* profiler : profilers_) { |
89 | profiler->AddEvent(tag, event_type, metric, event_metadata1, |
90 | event_metadata2); |
91 | } |
92 | } |
93 | |
94 | void RootProfiler::AddEventWithData(const char* tag, EventType event_type, |
95 | const void* data) { |
96 | for (auto* profiler : profilers_) { |
97 | profiler->AddEventWithData(tag, event_type, data); |
98 | } |
99 | } |
100 | |
101 | void RootProfiler::RemoveChildProfilers() { |
102 | owned_profilers_.clear(); |
103 | profilers_.clear(); |
104 | // Previous `BeginEvent` calls will be discarded. |
105 | events_.clear(); |
106 | } |
107 | |
108 | } // namespace profiling |
109 | } // namespace tflite |
110 | |