/src/WasmEdge/include/common/timer.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | //===-- wasmedge/common/timer.h - Timer class definition ------------------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the timer class used by statistics. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "errcode.h" |
17 | | |
18 | | #include <array> |
19 | | #include <chrono> |
20 | | #include <shared_mutex> |
21 | | #include <string> |
22 | | #include <thread> |
23 | | #include <unordered_map> |
24 | | |
25 | | namespace WasmEdge { |
26 | | namespace Timer { |
27 | | |
28 | | enum class TimerTag : uint32_t { Wasm, HostFunc, Max }; |
29 | | |
30 | | class Timer { |
31 | | public: |
32 | | using Clock = std::chrono::steady_clock; |
33 | | |
34 | 0 | Timer() noexcept { unsafeReset(); } |
35 | | |
36 | 0 | void startRecord(const TimerTag TT) noexcept { |
37 | 0 | assuming(TT < TimerTag::Max); |
38 | 0 | std::unique_lock Lock(Mutex); |
39 | 0 | const uint32_t Index = static_cast<uint32_t>(TT); |
40 | 0 | StartTime[Index].emplace(std::this_thread::get_id(), Clock::now()); |
41 | 0 | } |
42 | | |
43 | 0 | void stopRecord(const TimerTag TT) noexcept { |
44 | 0 | assuming(TT < TimerTag::Max); |
45 | 0 | std::unique_lock Lock(Mutex); |
46 | 0 | const uint32_t Index = static_cast<uint32_t>(TT); |
47 | 0 | auto &Map = StartTime[Index]; |
48 | 0 | if (auto Iter = Map.find(std::this_thread::get_id()); Iter != Map.end()) { |
49 | 0 | const auto Diff = Clock::now() - Iter->second; |
50 | 0 | RecTime[Index] += Diff; |
51 | 0 | Map.erase(Iter); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | 0 | void clearRecord(const TimerTag TT) noexcept { |
56 | 0 | assuming(TT < TimerTag::Max); |
57 | 0 | std::unique_lock Lock(Mutex); |
58 | 0 | const uint32_t Index = static_cast<uint32_t>(TT); |
59 | 0 | StartTime[Index].clear(); |
60 | 0 | RecTime[Index] = Clock::duration::zero(); |
61 | 0 | } |
62 | | |
63 | 0 | Clock::duration getRecord(const TimerTag TT) const noexcept { |
64 | 0 | assuming(TT < TimerTag::Max); |
65 | 0 | std::shared_lock Lock(Mutex); |
66 | 0 | const uint32_t Index = static_cast<uint32_t>(TT); |
67 | 0 | return RecTime[Index]; |
68 | 0 | } |
69 | | |
70 | 0 | void reset() noexcept { |
71 | 0 | std::unique_lock Lock(Mutex); |
72 | 0 | unsafeReset(); |
73 | 0 | } |
74 | | |
75 | | private: |
76 | 0 | void unsafeReset() noexcept { |
77 | 0 | for (auto &Start : StartTime) { |
78 | 0 | Start.clear(); |
79 | 0 | } |
80 | 0 | for (auto &Rec : RecTime) { |
81 | 0 | Rec = Clock::duration::zero(); |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | mutable std::shared_mutex Mutex; |
86 | | std::array<std::unordered_map<std::thread::id, Clock::time_point>, |
87 | | uint32_t(TimerTag::Max)> |
88 | | StartTime{}; |
89 | | std::array<Clock::duration, uint32_t(TimerTag::Max)> RecTime{}; |
90 | | }; |
91 | | |
92 | | } // namespace Timer |
93 | | } // namespace WasmEdge |