-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick-timer
93 lines (84 loc) · 2.94 KB
/
brick-timer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (C) 2020 Petr Rockai <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <x86intrin.h>
#include <atomic>
#include <array>
namespace brq
{
/* This class provides a low-overhead, thread-safe profiling timer &
* counter. On recent hardware, the typical single-threaded overhead (for
* entry + exit) is 100-200 cycles (or about 40-80ns), exact numbers
* depending on the CPU model. Each timer is global and distinct timers can
* be created by instantiating the 'timer' template with different tag
* types. E.g. `struct foo_timer_tag; using foo_timer = brq::timer<
* foo_timer_tag >;` Instances of such a type work as RAII interval timers
* that sum up over the lifetime of the program. Nesting is *not* guarded
* and will result in double-counting. */
template< typename >
struct timer
{
struct alignas( 64 ) counter_t /* 64 = expected cache line size */
{
std::atomic< long > time, hits;
void reset() { time.store( 0 ); hits.store( 0 ); }
};
using counters_t = std::array< counter_t, 32 >;
counter_t *_ctr;
[[gnu::always_inline]] timer( bool measure = true )
{
static counters_t ctr;
if ( measure )
{
auto t = __rdtsc();
_ctr = &ctr[ t & 0x1f ];
_ctr->time -= t;
_ctr->hits ++;
}
else
_ctr = ctr.begin();
}
static std::pair< long, long > read()
{
timer x( false );
long time = 0, hits = 0;
for ( int i = 0; i < 32; ++i )
{
time += x._ctr[ i ].time;
hits += x._ctr[ i ].hits;
}
x._ctr = nullptr;
return { time, hits };
}
static void reset()
{
timer x( false );
for ( int i = 0; i < 32; ++i )
x._ctr[ i ].reset();
x._ctr = nullptr;
}
void stop()
{
_ctr->time += __rdtsc();
_ctr = nullptr;
}
[[gnu::always_inline]] ~timer()
{
if ( _ctr )
_ctr->time += __rdtsc();
}
};
}