Release 18.08
[platform/upstream/armnn.git] / src / armnn / WallClockTimer.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5
6 #pragma once
7
8 #include "Instrument.hpp"
9 #include <chrono>
10
11 namespace armnn
12 {
13
14 // Clock class that uses the same timestamp function as the Mali DDK.
15 class monotonic_clock_raw {
16 public:
17     using duration = std::chrono::nanoseconds;
18     using time_point = std::chrono::time_point<monotonic_clock_raw, duration>;
19
20     static std::chrono::time_point<monotonic_clock_raw, std::chrono::nanoseconds> now() noexcept
21     {
22         timespec ts;
23         clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
24         return time_point(std::chrono::nanoseconds(ts.tv_sec*1000000000 + ts.tv_nsec));
25     }
26 };
27
28 // Implementation of an instrument to measure elapsed wall-clock time in milliseconds.
29 class WallClockTimer : public Instrument
30 {
31 public:
32     // Construct a Wall Clock Timer
33     WallClockTimer() = default;
34     ~WallClockTimer() = default;
35
36     // Start the Wall clock timer
37     void Start() override;
38
39     // Stop the Wall clock timer
40     void Stop() override;
41
42     // Get the name of the timer
43     const char* GetName() const override;
44
45     // Get the recorded measurements
46     std::vector<Measurement> GetMeasurements() const override;
47
48 #if defined(CLOCK_MONOTONIC_RAW)
49     using clock = monotonic_clock_raw;
50 #else
51     using clock = std::chrono::steady_clock;
52 #endif
53
54     static const std::string WALL_CLOCK_TIME;
55     static const std::string WALL_CLOCK_TIME_START;
56     static const std::string WALL_CLOCK_TIME_STOP;
57
58 private:
59     clock::time_point m_Start;
60     clock::time_point m_Stop;
61 };
62
63 } //namespace armnn