1 /* Copyright 2018 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_CORE_PROFILER_INTERNAL_CPU_HOST_TRACER_H_
16 #define TENSORFLOW_CORE_PROFILER_INTERNAL_CPU_HOST_TRACER_H_
17 
18 #include "tensorflow/core/common_runtime/step_stats_collector.h"
19 #include "tensorflow/core/lib/core/status.h"
20 #include "tensorflow/core/profiler/internal/profiler_interface.h"
21 #include "tensorflow/core/profiler/internal/traceme_recorder.h"
22 #include "tensorflow/core/protobuf/config.pb.h"
23 
24 namespace tensorflow {
25 namespace profiler {
26 namespace cpu {
27 
28 // Controls TraceMeRecorder and converts TraceMeRecorder::Events into
29 // RunMetadata messages.
30 //
31 // Thread-safety: This class is go/thread-compatible.
32 class HostTracer : public ProfilerInterface {
33  public:
34   static std::unique_ptr<HostTracer> Create(int host_trace_level);
35 
36   ~HostTracer();
37 
38   // Starts recording TraceMes.
39   Status Start() override;
40 
41   // Stops recording TraceMes.
42   Status Stop() override;
43 
44   // Populates user traces and thread names in response.
45   // The user traces and thread names are in no particular order.
46   Status CollectData(RunMetadata* run_metadata) override;
47 
48   Status CollectDataToCollector(StepStatsCollector* step_stats_collector);
49 
50  private:
51   explicit HostTracer(int host_trace_level);
52 
53   // Level of host tracing.
54   const int host_trace_level_;
55 
56   // True if currently recording.
57   bool recording_ = false;
58 
59   // Container of all traced events.
60   TraceMeRecorder::Events events_;
61 };
62 
63 }  // namespace cpu
64 }  // namespace profiler
65 }  // namespace tensorflow
66 
67 #endif  // TENSORFLOW_CORE_PROFILER_INTERNAL_CPU_HOST_TRACER_H_
68