1 /* Copyright 2015 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
16 #include "tensorflow/core/platform/tracing.h"
17
18 #include <array>
19 #include <atomic>
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include "tensorflow/core/lib/hash/hash.h"
24 #include "tensorflow/core/lib/strings/str_util.h"
25 #include "tensorflow/core/lib/strings/strcat.h"
26 #include "tensorflow/core/platform/logging.h"
27
28 namespace tensorflow {
29 namespace tracing {
30 namespace {
31 std::atomic<uint64> unique_arg{1};
32 std::atomic<const TraceCollector*> trace_collector;
33 } // namespace
34
GetEventCategoryName(EventCategory category)35 const char* GetEventCategoryName(EventCategory category) {
36 switch (category) {
37 case EventCategory::kScheduleClosure:
38 return "ScheduleClosure";
39 case EventCategory::kRunClosure:
40 return "RunClosure";
41 case EventCategory::kCompute:
42 return "Compute";
43 default:
44 return "Unknown";
45 }
46 }
47
48 std::array<const EventCollector*, GetNumEventCategories()>
49 EventCollector::instances_;
50
SetEventCollector(EventCategory category,const EventCollector * collector)51 void SetEventCollector(EventCategory category,
52 const EventCollector* collector) {
53 EventCollector::instances_[static_cast<unsigned>(category)] = collector;
54 }
55
GetUniqueArg()56 uint64 GetUniqueArg() {
57 return unique_arg.fetch_add(1, std::memory_order_relaxed);
58 }
59
GetArgForName(StringPiece name)60 uint64 GetArgForName(StringPiece name) {
61 return Hash64(name.data(), name.size());
62 }
63
ConcatenateNames(StringPiece first,StringPiece second)64 string TraceCollector::ConcatenateNames(StringPiece first, StringPiece second) {
65 std::string result;
66 bool has_two_parts = !first.empty() && !second.empty();
67 result.reserve(first.size() + second.size() +
68 static_cast<int>(has_two_parts));
69 result.append(first.data(), first.size());
70 if (has_two_parts) result.append({':'});
71 result.append(second.data(), second.size());
72 return result;
73 }
74
SetTraceCollector(const TraceCollector * collector)75 void SetTraceCollector(const TraceCollector* collector) {
76 return trace_collector.store(collector, std::memory_order_release);
77 }
78
GetTraceCollector()79 const TraceCollector* GetTraceCollector() {
80 return trace_collector.load(std::memory_order_acquire);
81 }
82
83 } // namespace tracing
84 } // namespace tensorflow
85