xref: /aosp_15_r20/external/pigweed/pw_perf_test/public/pw_perf_test/internal/timer.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "pw_perf_test/internal/duration_unit.h"
17 #include "pw_perf_test_timer_backend/timer.h"
18 
19 namespace pw::perf_test::internal {
20 
21 using Timestamp = backend::Timestamp;  // implementation-defined type
22 
23 // Returns true when the timer is ready to measure.
TimerPrepare()24 [[nodiscard]] inline bool TimerPrepare() { return backend::TimerPrepare(); }
25 
26 // Performs any necessary cleanup for the timer.
TimerCleanup()27 inline void TimerCleanup() { return backend::TimerCleanup(); }
28 
29 // Returns the current timestamp
GetCurrentTimestamp()30 inline Timestamp GetCurrentTimestamp() {
31   return backend::GetCurrentTimestamp();
32 }
33 
34 // Obtains the testing unit from the backend.
35 inline constexpr DurationUnit kDurationUnit =
36     backend::kDurationUnit;  // <cycles, ns, etc.>
37 
38 // Returns the duration in the specified unit.
GetDuration(Timestamp begin,Timestamp end)39 inline int64_t GetDuration(Timestamp begin, Timestamp end) {
40   return backend::GetDuration(begin, end);
41 }
42 
GetDurationUnitStr()43 constexpr const char* GetDurationUnitStr() {
44   switch (kDurationUnit) {
45     case DurationUnit::kNanoseconds:
46       return "ns";
47     case DurationUnit::kClockCycle:
48       return "clock cycles";
49     default:
50       return "unknown";
51   }
52 }
53 }  // namespace pw::perf_test::internal
54