Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
sampling.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Stanford University, NVIDIA Corporation
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18// sampling profiler for Realm
19
20#ifndef REALM_SAMPLING_H
21#define REALM_SAMPLING_H
22
23#include <limits.h>
24#include <vector>
25#include <set>
26#include <map>
27
28#include "realm/bytearray.h"
29#include "realm/processor.h"
30#include "realm/memory.h"
31#include "realm/instance.h"
32#include "realm/faults.h"
33#include "realm/atomics.h"
34
35namespace Realm {
36
37 class SamplingProfiler;
38 class GaugeSampler;
39 class CoreReservationSet;
40
41 namespace ProfilingGauges {
42
43 // these gauges are created by "user" code and are intended to be nearly free
44 // when profiling is not active so that they don't need to be guarded by
45 // compile-time ifdef's
46
47 // parent class for all gauges
48 class Gauge {
49 public:
57
58 // if profiler==0, the gauge will be connected to the global default profiler
59 Gauge(const std::string &_name);
60 ~Gauge(void);
61
62 const std::string name;
63
64 private:
65 Gauge(const Gauge &copy_from) {}
66 Gauge &operator=(const Gauge &copy_from) { return *this; }
67
68 protected:
69 // add_gauge is templated to preserve static type info during delayed construction
70 template <typename T>
71 static void add_gauge(T *gauge, SamplingProfiler *_profiler);
72 void remove_gauge(void);
73
75
76 public:
77 // don't actually call this - it's here for linker fun
78 static size_t instantiate_templates(void);
79 };
80
81 template <typename T>
82 class AbsoluteGauge : public Gauge {
83 public:
84 static const int GAUGE_TYPE = GTYPE_ABSOLUTE;
85 typedef T DATA_TYPE;
86
87 // if profiler==0, the gauge will be connected to the global default profiler
88 AbsoluteGauge(const std::string &_name, T initval = T(),
89 SamplingProfiler *_profiler = 0);
90
92
93 operator T(void) const;
97
98 struct Sample {
99 bool operator==(const Sample &other) const { return value == other.value; }
100
102 };
103
104 protected:
106
107 atomic<T> curval; // current gauge value
108 };
109
110 template <typename T>
111 class AbsoluteRangeGauge : public Gauge {
112 public:
113 static const int GAUGE_TYPE = GTYPE_ABSOLUTERANGE;
114 typedef T DATA_TYPE;
115
116 // if profiler==0, the gauge will be connected to the global default profiler
117 AbsoluteRangeGauge(const std::string &_name, T initval = T(),
118 SamplingProfiler *_profiler = 0);
119
121
122 operator T(void) const;
126
127 struct Sample {
128 bool operator==(const Sample &other) const
129 {
130 return ((value == other.value) && (minval == other.minval) &&
131 (maxval == other.maxval));
132 }
133
137 };
138
139 protected:
141
142 atomic<T> curval; // current gauge value
143 atomic<T> minval; // max value seen since last sample
144 atomic<T> maxval; // min value seen since last sample
145 };
146
147 template <typename T = int>
148 class EventCounter : public Gauge {
149 public:
150 static const int GAUGE_TYPE = GTYPE_EVENTCOUNT;
151 typedef T DATA_TYPE;
152
153 // if profiler==0, the gauge will be connected to the global default profiler
154 EventCounter(const std::string &_name, SamplingProfiler *_profiler = 0);
155
156 operator T(void) const;
158
159 struct Sample {
160 bool operator==(const Sample &other) const { return count == other.count; }
161
163 };
164
165 protected:
167
168 atomic<T> events; // events recorded since last sample
169 };
170
171 }; // namespace ProfilingGauges
172
174 public:
175 // there can be only one "default" sampling profiler - attempts to create a second
176 // will cause a fatal error
177 SamplingProfiler(bool is_default);
179
180 void configure_from_cmdline(std::vector<std::string> &cmdline,
181 CoreReservationSet &crs);
182 void flush_data(void);
183 void shutdown(void);
184
185 protected:
187
188 template <typename T>
190
191 void *impl;
192 };
193
194 namespace SampleFile {
195
196 // no actual code in here - just structure definitions for the file that
197 // gets written by the sampling profiler
198
206 unsigned packet_type;
207 unsigned packet_size; // individual packets <= 4GB
208 // size does NOT include this 8 byte header
209 };
210
212 int gauge_id; // assigned by the profiler
213 int gauge_type; // from ProfilingGauges::Gauge::GaugeType
214 char gauge_dtype[8];
215 char name[48];
216 };
217
223 // the type/size of a Sample is determined by gauge_type/dtype above
224 // Sample samples[compressed_len]
225 // unsigned short run_lengths[compressed_len]
226 };
227
228 }; // namespace SampleFile
229
230}; // namespace Realm
231
232#include "realm/sampling.inl"
233
234#endif // ifdef REALM_SAMPLING_H
Definition threads.h:382
Definition sampling_impl.h:54
T DATA_TYPE
Definition sampling.h:85
AbsoluteGauge< T > & operator=(T to_set)
AbsoluteGauge< T > & operator=(const AbsoluteGauge< T > &copy_from)
static const int GAUGE_TYPE
Definition sampling.h:84
atomic< T > curval
Definition sampling.h:107
AbsoluteGauge< T > & operator-=(T to_sub)
AbsoluteGauge(const std::string &_name, T initval=T(), SamplingProfiler *_profiler=0)
AbsoluteGauge< T > & operator+=(T to_add)
T DATA_TYPE
Definition sampling.h:114
AbsoluteRangeGauge< T > & operator-=(T to_sub)
atomic< T > minval
Definition sampling.h:143
AbsoluteRangeGauge< T > & operator+=(T to_add)
AbsoluteRangeGauge< T > & operator=(T to_set)
AbsoluteRangeGauge(const std::string &_name, T initval=T(), SamplingProfiler *_profiler=0)
atomic< T > curval
Definition sampling.h:142
atomic< T > maxval
Definition sampling.h:144
static const int GAUGE_TYPE
Definition sampling.h:113
AbsoluteRangeGauge< T > & operator=(const AbsoluteRangeGauge< T > &copy_from)
Definition sampling.h:148
EventCounter(const std::string &_name, SamplingProfiler *_profiler=0)
EventCounter< T > & operator+=(T to_add)
static const int GAUGE_TYPE
Definition sampling.h:150
atomic< T > events
Definition sampling.h:168
T DATA_TYPE
Definition sampling.h:151
Definition sampling.h:48
GaugeSampler * sampler
Definition sampling.h:74
static size_t instantiate_templates(void)
static void add_gauge(T *gauge, SamplingProfiler *_profiler)
const std::string name
Definition sampling.h:62
Gauge(const std::string &_name)
GaugeType
Definition sampling.h:51
@ GTYPE_ABSOLUTERANGE
Definition sampling.h:54
@ GTYPE_ABSOLUTE
Definition sampling.h:53
@ GTYPE_UNKNOWN
Definition sampling.h:52
@ GTYPE_EVENTCOUNT
Definition sampling.h:55
Definition sampling.h:173
void * impl
Definition sampling.h:191
SamplingProfiler(bool is_default)
void configure_from_cmdline(std::vector< std::string > &cmdline, CoreReservationSet &crs)
GaugeSampler * add_gauge(T *gauge)
Definition atomics.h:31
Definition activemsg.h:38
bool operator==(const Sample &other) const
Definition sampling.h:99
bool operator==(const Sample &other) const
Definition sampling.h:128
bool operator==(const Sample &other) const
Definition sampling.h:160
Definition sampling.h:199
unsigned packet_type
Definition sampling.h:206
unsigned packet_size
Definition sampling.h:207
PacketTypes
Definition sampling.h:201
@ PACKET_NEWGAUGE
Definition sampling.h:203
@ PACKET_EMPTY
Definition sampling.h:202
@ PACKET_SAMPLES
Definition sampling.h:204
Definition sampling.h:211
int gauge_type
Definition sampling.h:213
int gauge_id
Definition sampling.h:212
char name[48]
Definition sampling.h:215
char gauge_dtype[8]
Definition sampling.h:214
Definition sampling.h:218
int first_sample
Definition sampling.h:221
int gauge_id
Definition sampling.h:219
int compressed_len
Definition sampling.h:220
int last_sample
Definition sampling.h:222