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_STREAM_EXECUTOR_TIMER_H_ 17 #define TENSORFLOW_STREAM_EXECUTOR_TIMER_H_ 18 19 #include <memory> 20 21 #include "tensorflow/stream_executor/platform/port.h" 22 23 namespace perftools { 24 namespace gputools { 25 26 namespace internal { 27 class TimerInterface; 28 } // namespace internal 29 30 class StreamExecutor; 31 32 // An interval timer, suitable for use in timing the operations which occur in 33 // streams. 34 // 35 // Thread-hostile: CUDA associates a CUDA-context with a particular thread in 36 // the system. Any operation that a user attempts to perform by using a Timer 37 // on a thread not-associated with the CUDA-context has unknown behavior at the 38 // current time; see b/13176597 39 class Timer { 40 public: 41 // Instantiate a timer tied to parent as a platform executor. 42 explicit Timer(StreamExecutor *parent); 43 44 // Deallocates any timer resources that the parent StreamExecutor has bestowed 45 // upon this object. 46 ~Timer(); 47 48 // Returns the elapsed number of microseconds for a completed timer. 49 // Completed means has been through a start/stop lifecycle. 50 uint64 Microseconds() const; 51 52 // Returns the elapsed number of nanoseconds for a completed timer. 53 // Completed means has been through a start/stop lifecycle. 54 uint64 Nanoseconds() const; 55 56 // Returns the (opaque) backing platform ITimer instance. Ownership is 57 // not transferred to the caller. 58 internal::TimerInterface *implementation() { return implementation_.get(); } 59 60 private: 61 // The StreamExecutor that manages the platform-specific internals for this 62 // timer. 63 StreamExecutor *parent_; 64 65 // Platform-dependent implementation of the timer internals for the underlying 66 // platform. This class just delegates to this opaque instance. 67 std::unique_ptr<internal::TimerInterface> implementation_; 68 69 SE_DISALLOW_COPY_AND_ASSIGN(Timer); 70 }; 71 72 } // namespace gputools 73 } // namespace perftools 74 75 #endif // TENSORFLOW_STREAM_EXECUTOR_TIMER_H_ 76