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 #ifndef TENSORFLOW_CORE_PLATFORM_DEVICE_TRACER_H_
17 #define TENSORFLOW_CORE_PLATFORM_DEVICE_TRACER_H_
18 
19 #include <memory>
20 
21 #include "tensorflow/core/lib/core/status.h"
22 
23 namespace tensorflow {
24 
25 class StepStatsCollector;
26 
27 // 'DeviceTracer' is an interface for collecting low-level execution timings
28 // of hardware accelerator (e.g. GPU) computation and DMA transfers.
29 //
30 // Typical usage pattern is as follows:
31 //
32 // DeviceTracer* tracer = CreateDeviceTracer();
33 // if (tracer) {
34 //   tracer->Start();
35 //
36 //   ... perform some computations on a hardware accelerator.
37 //
38 //   tracer->Stop();
39 //
40 //   StepStats stats;
41 //   StepStatsCollector collector(&stats);
42 //   tracer->Collect(&collector);
43 // }
44 //
45 // Notes:
46 // Tracing is not supported on all plaforms.  On platforms
47 // with no tracing support, 'CreateDeviceTracer' will return 'nullptr'.
48 // On most plaforms, hardware tracing will be a system-wide activity and
49 // a single 'DeviceTracer' will collect activity from all devices.
50 // It is also common that only a single tracer may be active at any
51 // given time.  The 'Start' method will return an error if tracing is
52 // already in progress elsewhere.
53 //
54 class DeviceTracer {
55  public:
~DeviceTracer()56   virtual ~DeviceTracer() {}
57 
58   // Start device tracing.
59   // Note that only a single trace can be active, in which case this
60   // methods will return an 'Unavailable' error.
61   virtual Status Start() = 0;
62 
63   // Stop device tracing.
64   // It is safe to call 'Stop' on a tracer which is not enabled.
65   virtual Status Stop() = 0;
66 
67   // Collect trace results.  Results are added to the specified
68   // StepStatsCollector.  Does not clear any existing stats.
69   // It is an error to call 'Collect' while a trace is running.
70   virtual Status Collect(StepStatsCollector* collector) = 0;
71 };
72 
73 // Creates a platform-specific DeviceTracer.
74 // Returns 'nullptr' on platforms where tracing is not supported.
75 std::unique_ptr<DeviceTracer> CreateDeviceTracer();
76 
77 }  // namespace tensorflow
78 
79 #endif  // TENSORFLOW_CORE_PLATFORM_DEVICE_TRACER_H_
80