ATLAS Offline Software
Loading...
Searching...
No Matches
Tools
PmbCxxUtils
PmbCxxUtils
CustomBenchmark.h
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3
*/
4
6
// //
7
// Header file for class CustomBenchmark //
8
// //
9
// Description: Helper class for performing custom //
10
// bench-marking of CPU. This is a simple //
11
// implementation without support for nested //
12
// bench-markings (i.e. a call to begin(..) //
13
// must always be followed by a call to end(), //
14
// never another call to begin(..)). //
15
// //
16
// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
17
// Initial version: January 2012 //
18
// //
20
21
#ifndef CUSTOMBENCHMARK_H
22
#define CUSTOMBENCHMARK_H
23
24
#include <ctime>
25
#include "stdint.h"
//<cstdint>
26
#include <cassert>
27
#include <limits>
28
29
namespace
PMonUtils
{
30
class
CustomBenchmark
{
31
public
:
32
33
CustomBenchmark
(
unsigned
nmax
);
34
~CustomBenchmark
();
35
36
//For benchmarking
37
void
begin
(
unsigned
id
);
38
void
end
(
int
count
);
39
40
//For accessing the results:
41
void
getData
(
unsigned
id
, uint64_t&
count
,
double
& time_ms)
const
;
42
unsigned
nMax
()
const
{
return
m_nmax
; }
43
unsigned
size
()
const
{
return
m_nmax
; }
44
45
private
:
46
struct
Data
{
47
Data
() {
init
(); }
48
void
init
() {
time_spent
= 0;
time_at_begin
= 0;
count
= 0; }
49
int64_t
time_spent
;
50
int64_t
time_at_begin
;
51
unsigned
count
;
52
};
53
54
const
unsigned
m_nmax
;
55
Data
*
m_data
;
56
Data
*
m_data_current
;
57
58
// It is illegal to copy/assign a CustomBenchmark:
59
CustomBenchmark
(
const
CustomBenchmark
& );
60
CustomBenchmark
&
operator=
(
const
CustomBenchmark
& );
61
62
//A non-overflowing "clock()":
63
static
int64_t
clock_nooverflow
();
64
65
};
66
67
inline
void
CustomBenchmark::begin
(
unsigned
id
)
68
{
69
assert(
id
<
m_nmax
);
70
m_data_current
= &(
m_data
[id]);
71
m_data_current
->time_at_begin =
clock_nooverflow
();
72
}
73
inline
void
CustomBenchmark::end
(
int
count
=1)
74
{
75
assert(
m_data_current
);
76
m_data_current
->count +=
count
;
77
m_data_current
->time_spent +=
clock_nooverflow
() -
m_data_current
->time_at_begin;
78
m_data_current
=0;
79
}
80
81
inline
void
CustomBenchmark::getData
(
unsigned
id
, uint64_t&
count
,
double
& time_ms)
const
82
{
83
assert(
id
<
m_nmax
);
84
Data
* data = &(
m_data
[id]);
85
count
= data->count;
86
time_ms = data->time_spent * (1000.0/CLOCKS_PER_SEC);
87
}
88
89
inline
int64_t
CustomBenchmark::clock_nooverflow
() {
90
//Copied from PerfMonComps/trunk/src/SemiDetMisc.h
91
//
92
//In gnu, clock_t is a long and CLOCKS_PER_SECOND 1000000 so clock()
93
//will overflow in 32bit builds after a few thousands of seconds. To
94
//avoid this, we have the following method instead which notices when
95
//overflow occurs and corrects for it (it won't notice if it doesn't
96
//get called for >4000s, but this should be ok for almost all of our
97
//use cases):
98
assert(std::numeric_limits<clock_t>::is_integer);
99
if
(
sizeof
(clock_t)>=
sizeof
(int64_t))
100
return
clock();
//64bit builds shouldn't have overflow issues.
101
102
//not so clean with statics i guess:
103
static
clock_t last=clock();
104
static
int64_t offset=0;
105
clock_t c=clock();
106
if
(c<last)
107
offset+=int64_t(std::numeric_limits<unsigned>::max())-int64_t(std::numeric_limits<unsigned>::min());
108
last=c;
109
return
offset+c;
110
}
111
112
//A class which makes for instance makes it easier to guarantee that
113
//an exception in the benchmarked code will still result in
114
//CustomBenchmark::end() will be called properly. Will do nothing if
115
//called with a null CustomBenchmark*:
116
117
class
CustomBenchmarkGuard
118
{
119
public
:
120
CustomBenchmarkGuard
(
CustomBenchmark
* cb,
unsigned
id
,
int
count
=1) :
m_cb
(cb),
m_count
(
count
) {
if
(
m_cb
)
m_cb
->begin(
id
); }
121
~CustomBenchmarkGuard
() {
if
(
m_cb
)
m_cb
->end(
m_count
); }
122
private
:
123
CustomBenchmark
*
m_cb
;
124
int
m_count
;
125
};
126
127
}
128
129
130
#endif
nmax
const int nmax(200)
PMonUtils::CustomBenchmarkGuard::CustomBenchmarkGuard
CustomBenchmarkGuard(CustomBenchmark *cb, unsigned id, int count=1)
Definition
CustomBenchmark.h:120
PMonUtils::CustomBenchmarkGuard::m_cb
CustomBenchmark * m_cb
Definition
CustomBenchmark.h:123
PMonUtils::CustomBenchmarkGuard::m_count
int m_count
Definition
CustomBenchmark.h:124
PMonUtils::CustomBenchmarkGuard::~CustomBenchmarkGuard
~CustomBenchmarkGuard()
Definition
CustomBenchmark.h:121
PMonUtils::CustomBenchmark
Definition
CustomBenchmark.h:30
PMonUtils::CustomBenchmark::m_data_current
Data * m_data_current
Definition
CustomBenchmark.h:56
PMonUtils::CustomBenchmark::operator=
CustomBenchmark & operator=(const CustomBenchmark &)
PMonUtils::CustomBenchmark::end
void end(int count)
Definition
CustomBenchmark.h:73
PMonUtils::CustomBenchmark::~CustomBenchmark
~CustomBenchmark()
Definition
CustomBenchmark.cxx:19
PMonUtils::CustomBenchmark::CustomBenchmark
CustomBenchmark(const CustomBenchmark &)
PMonUtils::CustomBenchmark::begin
void begin(unsigned id)
Definition
CustomBenchmark.h:67
PMonUtils::CustomBenchmark::CustomBenchmark
CustomBenchmark(unsigned nmax)
Definition
CustomBenchmark.cxx:9
PMonUtils::CustomBenchmark::size
unsigned size() const
Definition
CustomBenchmark.h:43
PMonUtils::CustomBenchmark::m_nmax
const unsigned m_nmax
Definition
CustomBenchmark.h:54
PMonUtils::CustomBenchmark::m_data
Data * m_data
Definition
CustomBenchmark.h:55
PMonUtils::CustomBenchmark::nMax
unsigned nMax() const
Definition
CustomBenchmark.h:42
PMonUtils::CustomBenchmark::getData
void getData(unsigned id, uint64_t &count, double &time_ms) const
Definition
CustomBenchmark.h:81
PMonUtils::CustomBenchmark::clock_nooverflow
static int64_t clock_nooverflow()
Definition
CustomBenchmark.h:89
count
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
Definition
hcg.cxx:148
PMonUtils
Definition
SimKernel.h:33
PMonUtils::CustomBenchmark::Data
Definition
CustomBenchmark.h:46
PMonUtils::CustomBenchmark::Data::time_at_begin
int64_t time_at_begin
Definition
CustomBenchmark.h:50
PMonUtils::CustomBenchmark::Data::init
void init()
Definition
CustomBenchmark.h:48
PMonUtils::CustomBenchmark::Data::count
unsigned count
Definition
CustomBenchmark.h:51
PMonUtils::CustomBenchmark::Data::Data
Data()
Definition
CustomBenchmark.h:47
PMonUtils::CustomBenchmark::Data::time_spent
int64_t time_spent
Definition
CustomBenchmark.h:49
Generated on
for ATLAS Offline Software by
1.16.1