GCC Middle and Back End API Reference
timevar.h
Go to the documentation of this file.
1/* Timing variables for measuring compiler performance.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Alex Samuel <samuel@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_TIMEVAR_H
22#define GCC_TIMEVAR_H
23
24namespace json { class value; }
25
26/* Timing variables are used to measure elapsed time in various
27 portions of the compiler. Each measures elapsed user, system, and
28 wall-clock time, as appropriate to and supported by the host
29 system.
30
31 Timing variables are defined using the DEFTIMEVAR macro in
32 timevar.def. Each has an enumeral identifier, used when referring
33 to the timing variable in code, and a character string name.
34
35 Timing variables can be used in two ways:
36
37 - On the timing stack, using timevar_push and timevar_pop.
38 Timing variables may be pushed onto the stack; elapsed time is
39 attributed to the topmost timing variable on the stack. When
40 another variable is pushed on, the previous topmost variable is
41 `paused' until the pushed variable is popped back off.
42
43 - As a standalone timer, using timevar_start and timevar_stop.
44 All time elapsed between the two calls is attributed to the
45 variable.
46*/
47
48/* This structure stores the various varieties of time that can be
49 measured. Times are stored in nanoseconds. The time may be an
50 absolute time or a time difference; in the former case, the time
51 base is undefined, except that the difference between two times
52 produces a valid time difference. */
53
55{
56 /* User time in this process. */
57 uint64_t user;
58
59 /* System time (if applicable for this host platform) in this process. */
60 uint64_t sys;
61
62 /* Wall clock time. */
63 uint64_t wall;
64
65 /* Garbage collector memory. */
66 size_t ggc_mem;
67};
68
69/* An enumeration of timing variable identifiers. Constructed from
70 the contents of timevar.def. */
71
72#define DEFTIMEVAR(identifier__, name__) \
73 identifier__,
74typedef enum
75{
77#include "timevar.def"
79}
81#undef DEFTIMEVAR
82
83/* A class to hold all state relating to timing. */
84
85class timer;
86
87/* The singleton instance of timing state.
88
89 This is non-NULL if timevars should be used. In GCC, this happens with
90 the -ftime-report flag. Hence this is NULL for the common,
91 needs-to-be-fast case, with an early reject happening for this being
92 NULL. */
93extern timer *g_timer;
94
95/* Total amount of memory allocated by garbage collector. */
96extern size_t timevar_ggc_mem_total;
97
98extern void timevar_init (void);
99extern void timevar_start (timevar_id_t);
100extern void timevar_stop (timevar_id_t);
101extern bool timevar_cond_start (timevar_id_t);
102extern void timevar_cond_stop (timevar_id_t, bool);
103
104/* The public (within GCC) interface for timing. */
105
106class timer
107{
108 public:
109 timer ();
110 ~timer ();
111
112 void start (timevar_id_t tv);
113 void stop (timevar_id_t tv);
114 void push (timevar_id_t tv);
115 void pop (timevar_id_t tv);
116 bool cond_start (timevar_id_t tv);
117 void cond_stop (timevar_id_t tv);
118
119 void push_client_item (const char *item_name);
120 void pop_client_item ();
121
122 void print (FILE *fp);
123 json::value *make_json () const;
124
125 const char *get_topmost_item_name () const;
126
127 private:
128 /* Private member functions. */
129 void validate_phases (FILE *fp) const;
130
131 struct timevar_def;
132 void push_internal (struct timevar_def *tv);
133 void pop_internal ();
134 static void print_row (FILE *fp,
135 const timevar_time_def *total,
136 const char *name, const timevar_time_def &elapsed);
137 static bool all_zero (const timevar_time_def &elapsed);
138
139 private:
141
142 /* Private type: a timing variable. */
144 {
145 json::value *make_json () const;
146
147 /* Elapsed time for this variable. */
149
150 /* If this variable is timed independently of the timing stack,
151 using timevar_start, this contains the start time. */
153
154 /* The name of this timing variable. */
155 const char *name;
156
157 /* Nonzero if this timing variable is running as a standalone
158 timer. */
159 unsigned standalone : 1;
160
161 /* Nonzero if this timing variable was ever started or pushed onto
162 the timing stack. */
163 unsigned used : 1;
164
166 };
167
168 /* Private type: an element on the timing stack
169 Elapsed time is attributed to the topmost timing variable on the
170 stack. */
172 {
173 /* The timing variable at this stack level. */
175
176 /* The next lower timing variable context in the stack. */
178 };
179
180 /* A class for managing a collection of named timing items, for use
181 e.g. by libgccjit for timing client code. This class is declared
182 inside timevar.cc to avoid everything using timevar.h
183 from needing vec and hash_map. */
184 class named_items;
185
186 private:
187
188 /* Data members (all private). */
189
190 /* Declared timing variables. Constructed from the contents of
191 timevar.def. */
193
194 /* The top of the timing stack. */
196
197 /* A list of unused (i.e. allocated and subsequently popped)
198 timevar_stack_def instances. */
200
201 /* The time at which the topmost element on the timing stack was
202 pushed. Time elapsed since then is attributed to the topmost
203 element. */
205
206 /* If non-NULL, for use when timing libgccjit's client code. */
208
209 friend class named_items;
210};
211
212/* Provided for backward compatibility. */
213inline void
215{
216 if (g_timer)
217 g_timer->push (tv);
218}
219
220inline void
222{
223 if (g_timer)
224 g_timer->pop (tv);
225}
226
227// This is a simple timevar wrapper class that pushes a timevar in its
228// constructor and pops the timevar in its destructor.
230{
231 public:
233 : m_timer (t),
234 m_tv (tv)
235 {
236 if (m_timer)
237 m_timer->push (m_tv);
238 }
239
241 : m_timer (g_timer)
242 , m_tv (tv)
243 {
244 if (m_timer)
245 m_timer->push (m_tv);
246 }
247
249 {
250 if (m_timer)
251 m_timer->pop (m_tv);
252 }
253
254 // Disallow copies.
255 auto_timevar (const auto_timevar &) = delete;
256
257 private:
260};
261
262// As above, but use cond_start/stop.
264{
265 public:
267 : m_timer (t),
268 m_tv (tv)
269 {
270 start ();
271 }
272
274 : m_timer (g_timer)
275 , m_tv (tv)
276 {
277 start ();
278 }
279
281 {
282 if (m_timer && !already_running)
284 }
285
286 // Disallow copies.
288
289 private:
290 void start()
291 {
292 if (m_timer)
294 else
295 already_running = false;
296 }
297
301};
302
303extern void print_time (const char *, long);
304
305#endif /* ! GCC_TIMEVAR_H */
Definition timevar.h:264
timer * m_timer
Definition timevar.h:298
~auto_cond_timevar()
Definition timevar.h:280
void start()
Definition timevar.h:290
timevar_id_t m_tv
Definition timevar.h:299
bool already_running
Definition timevar.h:300
auto_cond_timevar(timer *t, timevar_id_t tv)
Definition timevar.h:266
auto_cond_timevar(timevar_id_t tv)
Definition timevar.h:273
auto_cond_timevar(const auto_cond_timevar &)=delete
Definition timevar.h:230
~auto_timevar()
Definition timevar.h:248
auto_timevar(timevar_id_t tv)
Definition timevar.h:240
timevar_id_t m_tv
Definition timevar.h:259
timer * m_timer
Definition timevar.h:258
auto_timevar(const auto_timevar &)=delete
auto_timevar(timer *t, timevar_id_t tv)
Definition timevar.h:232
Definition hash-map.h:40
Definition json.h:88
Definition timevar.cc:132
Definition timevar.h:107
void validate_phases(FILE *fp) const
Definition timevar.cc:620
void push(timevar_id_t tv)
Definition timevar.cc:360
void pop(timevar_id_t tv)
Definition timevar.cc:418
void stop(timevar_id_t tv)
Definition timevar.cc:512
static void print_row(FILE *fp, const timevar_time_def *total, const char *name, const timevar_time_def &elapsed)
Definition timevar.cc:676
named_items * m_jit_client_items
Definition timevar.h:207
void print(FILE *fp)
Definition timevar.cc:732
static bool all_zero(const timevar_time_def &elapsed)
Definition timevar.cc:717
hash_map< timevar_def *, timevar_time_def > child_map_t
Definition timevar.h:140
void pop_client_item()
Definition timevar.cc:611
void cond_stop(timevar_id_t tv)
Definition timevar.cc:579
timevar_stack_def * m_unused_stack_instances
Definition timevar.h:199
timevar_stack_def * m_stack
Definition timevar.h:195
const char * get_topmost_item_name() const
Definition timevar.cc:971
void start(timevar_id_t tv)
Definition timevar.cc:482
void push_client_item(const char *item_name)
Definition timevar.cc:597
json::value * make_json() const
Definition timevar.cc:899
timer()
Definition timevar.cc:294
timevar_def m_timevars[TIMEVAR_LAST]
Definition timevar.h:192
void push_internal(struct timevar_def *tv)
Definition timevar.cc:370
~timer()
Definition timevar.cc:321
bool cond_start(timevar_id_t tv)
Definition timevar.cc:544
void pop_internal()
Definition timevar.cc:429
timevar_time_def m_start_time
Definition timevar.h:204
Definition diagnostic.h:223
Definition timevar.h:144
unsigned used
Definition timevar.h:163
const char * name
Definition timevar.h:155
struct timevar_time_def start_time
Definition timevar.h:152
struct timevar_time_def elapsed
Definition timevar.h:148
child_map_t * children
Definition timevar.h:165
unsigned standalone
Definition timevar.h:159
json::value * make_json() const
Definition timevar.cc:858
Definition timevar.h:172
struct timevar_stack_def * next
Definition timevar.h:177
struct timevar_def * timevar
Definition timevar.h:174
Definition timevar.h:55
uint64_t wall
Definition timevar.h:63
uint64_t sys
Definition timevar.h:60
size_t ggc_mem
Definition timevar.h:66
uint64_t user
Definition timevar.h:57
void timevar_stop(timevar_id_t)
Definition timevar.cc:501
bool timevar_cond_start(timevar_id_t)
Definition timevar.cc:533
timer * g_timer
Definition timevar.cc:110
size_t timevar_ggc_mem_total
Definition timevar.cc:114
void timevar_pop(timevar_id_t tv)
Definition timevar.h:221
void timevar_push(timevar_id_t tv)
Definition timevar.h:214
void print_time(const char *, long)
Definition timevar.cc:983
timevar_id_t
Definition timevar.h:75
@ TV_NONE
Definition timevar.h:76
@ TIMEVAR_LAST
Definition timevar.h:429
void timevar_init(void)
Definition timevar.cc:344
void timevar_cond_stop(timevar_id_t, bool)
Definition timevar.cc:568
void timevar_start(timevar_id_t)
Definition timevar.cc:471