xref: /aosp_15_r20/external/stg/runtime.h (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2021-2023 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Giuliano Procida
19 
20 #ifndef STG_RUNTIME_H_
21 #define STG_RUNTIME_H_
22 
23 #include <cstddef>
24 #include <cstdint>
25 #include <ctime>
26 #include <map>
27 #include <ostream>
28 
29 namespace stg {
30 
31 struct Nanoseconds {
NanosecondsNanoseconds32   explicit Nanoseconds(uint64_t ns) : ns(ns) {}
33   uint64_t ns;
34 };
35 
36 struct Frequencies {
37   std::map<size_t, size_t> counts;
38 };
39 
40 struct Runtime {
RuntimeRuntime41   Runtime(std::ostream& output, bool print_metrics)
42       : output(output), print_metrics(print_metrics) {}
43   template <typename V>
PrintMetricRuntime44   void PrintMetric(const char* name, const V& value) {
45     if (print_metrics) {
46       output << name << ": " << value << '\n';
47     }
48   }
49   std::ostream& output;
50   bool print_metrics;
51 };
52 
53 // These objects only record values on destruction, so scope them!
54 
55 class Time {
56  public:
57   Time(Runtime& runtime, const char* name);
58   Time(Time&& other) = delete;
59   Time& operator=(Time&& other) = delete;
60   ~Time();
61 
62  private:
63   Runtime& runtime_;
64   const char* name_;
65   struct timespec start_;
66 };
67 
68 class Counter {
69  public:
70   Counter(Runtime& runtime, const char* name);
71   Counter(Counter&& other) = delete;
72   Counter& operator=(Counter&& other) = delete;
73   ~Counter();
74 
75   Counter& operator=(size_t x) {
76     value_ = x;
77     return *this;
78   }
79 
80   Counter& operator+=(size_t x) {
81     value_ += x;
82     return *this;
83   }
84 
85   Counter& operator++() {
86     ++value_;
87     return *this;
88   }
89 
90  private:
91   Runtime& runtime_;
92   const char* name_;
93   size_t value_;
94 };
95 
96 class Histogram {
97  public:
98   Histogram(Runtime& runtime, const char* name);
99   Histogram(Histogram&& other) = delete;
100   Histogram& operator=(Histogram&& other) = delete;
101   ~Histogram();
102 
Add(size_t item)103   void Add(size_t item) {
104     ++frequencies_.counts[item];
105   }
106 
107  private:
108   Runtime& runtime_;
109   const char* name_;
110   Frequencies frequencies_;
111 };
112 
113 }  // namespace stg
114 
115 #endif  // STG_RUNTIME_H_
116