Skip to content

Commit c9f4837

Browse files
committed
Updates requested in the PR:
* move of includes to CPP file * rename of structures * unwrapped 'enabled' boolean from function to global scope * macro TMPROF_ENABLED() to TMPROF_ENABLED * making name and line const members * added default argument to enable function * Rename: tmprof_dump_in_json -> tmprof_dump_as_json * __tmprof_detail_add_record replaced by tmprof_detail_datat::create * push_back replaced by emplace_back * added getters to tmprof_detail_datat's attributes. * records list moved into the struct tmprof_detail_datat * added Doxygen tokens. * use of the time profiler only when TMPROF_ENABLED macro is set to 1.
1 parent 2c71f3f commit c9f4837

File tree

3 files changed

+132
-64
lines changed

3 files changed

+132
-64
lines changed

src/taint-analysis/taint_security_scanner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ bool taint_do_security_scan(
337337

338338
logger.status() << "The security scanner has finished successfully."
339339
<< messaget::eom;
340-
tmprof_dump_in_json(
340+
tmprof_dump_as_json(
341341
boost::filesystem::path(config.get_output_root_directory()) /
342342
"TMPROF.json");
343343
return true;

src/util/tmprof.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,69 @@
1+
// Copyright 2018 Diffblue Limited. All Rights Reserved.
2+
3+
/// \file
4+
/// Micro time profiler with code block granularity.
5+
16
#include <util/tmprof.h>
27
#include <boost/filesystem.hpp>
8+
#include <iostream>
9+
#include <sstream>
310

4-
static bool &get_enabled_ref()
5-
{
6-
static bool enabled = true;
7-
return enabled;
8-
}
11+
static bool enabled = true;
912

1013
bool is_time_profiling_enabled()
1114
{
12-
return get_enabled_ref();
15+
return enabled;
1316
}
1417

1518
void enable_time_profiling(const bool state)
1619
{
17-
get_enabled_ref() = state;
20+
enabled = state;
1821
}
1922

20-
static std::list<__tmprof_detail_data> &get_records_list()
23+
std::list<tmprof_detail_datat> tmprof_detail_datat::records;
24+
25+
tmprof_detail_datat *
26+
tmprof_detail_datat::create(const char *name, const unsigned long line)
2127
{
22-
static std::list<__tmprof_detail_data> records;
23-
return records;
28+
records.emplace_back(name, line);
29+
return &records.back();
2430
}
2531

26-
__tmprof_detail_data *
27-
__tmprof_detail_add_record(const __tmprof_detail_data &data)
32+
void tmprof_detail_datat::on_hit(const double duration)
2833
{
29-
get_records_list().push_back(data);
30-
return &get_records_list().back();
34+
total_time += duration;
35+
if(max_time < duration)
36+
max_time = duration;
37+
++hit_count;
3138
}
3239

33-
void tmprof_dump_in_json(std::ostream &ostr)
40+
json_objectt tmprof_detail_datat::dump_as_json()
3441
{
3542
json_objectt root;
36-
for(const auto &record : get_records_list())
37-
if(record.hit_count != 0ULL)
43+
for(const auto &record : records)
44+
if(record.get_hit_count() != 0ULL)
3845
{
3946
json_objectt table;
40-
table["total_time"] = json_numbert(std::to_string(record.total_time));
41-
table["max_time"] = json_numbert(std::to_string(record.max_time));
42-
table["hit_count"] = json_numbert(std::to_string(record.hit_count));
43-
root[record.name + " [" + std::to_string(record.line) + "]"] = table;
47+
table["total_time"] =
48+
json_numbert(std::to_string(record.get_total_time()));
49+
table["max_time"] = json_numbert(std::to_string(record.get_max_time()));
50+
table["hit_count"] = json_numbert(std::to_string(record.get_hit_count()));
51+
std::stringstream sstr;
52+
sstr << record.get_name() << " [" << record.get_line() << "]";
53+
root[sstr.str()] = table;
4454
}
45-
ostr << root;
55+
return root;
56+
}
57+
58+
void tmprof_dump_as_json(std::ostream &ostr)
59+
{
60+
ostr << tmprof_detail_datat::dump_as_json();
4661
}
4762

48-
void tmprof_dump_in_json(const boost::filesystem::path &out_file_pathname)
63+
void tmprof_dump_as_json(const boost::filesystem::path &out_file_pathname)
4964
{
5065
if(!boost::filesystem::exists(out_file_pathname.parent_path()))
5166
boost::filesystem::create_directory(out_file_pathname.parent_path());
5267
std::ofstream ostr(out_file_pathname.native());
53-
tmprof_dump_in_json(ostr);
68+
tmprof_dump_as_json(ostr);
5469
}

src/util/tmprof.h

Lines changed: 92 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
// Copyright 2018 Diffblue Limited. All Rights Reserved.
2+
3+
/// \file
4+
/// Micro time profiler with code block granularity.
5+
16
#ifndef CPROVER_SECURITY_ANALYSER_UTIL_TMPROF_H
27
#define CPROVER_SECURITY_ANALYSER_UTIL_TMPROF_H
38

49
#include <util/json.h>
510
#include <boost/filesystem/path.hpp>
11+
#include <list>
612
#include <chrono>
713
#include <string>
8-
#include <list>
9-
#include <iostream>
14+
#include <iosfwd>
1015

11-
/// Usage:
12-
/// In order to measure a time performance of a C++ block of code
13-
/// simply place 'TMPROF_BLOCK();' at the beginning of the block.
14-
/// (Do not forget to include this header file to the module.)
15-
/// Example:
16+
/// \remarks
17+
/// Usage:
18+
/// In order to measure a time performance of a C++ block of code
19+
/// simply place 'TMPROF_BLOCK();' at the beginning of the block.
20+
/// (Do not forget to include this header file to the module.)
21+
/// \remarks
22+
/// Example:
1623
/// Let us assume you add TMPROF_BLOCK() calls to function 'foo()' as follows:
24+
/// \code
1725
/// 1 void foo()
1826
/// 2 {
1927
/// 3 TMPROF_BLOCK(); // This one will give us statistic of whole foo(),
@@ -31,10 +39,12 @@
3139
/// 15
3240
/// 16 int main()
3341
/// 17 {
34-
//// 18 foo();
42+
/// 18 foo();
3543
/// 19 tmprof_dump_in_json(std::cout);
3644
/// 20 }
45+
/// \endcode
3746
/// Then, the program will print to 'std::cout' JSON on this form:
47+
/// \code
3848
/// {
3949
/// {
4050
/// "foo [3]": {
@@ -49,68 +59,111 @@
4959
/// "total_time": 50
5060
/// },
5161
/// },
52-
/// NOTE: Recursive calls are trait as independent ones.
53-
/// NOTE: A C++ code block may have at most one TMPROF_BLOCK() call.
54-
/// NOTE: The implementation is NOT thread-safe.
55-
/// NOTE: You can temporarily disable time-profiler by calling:
56-
/// enable_time_profiling(false);
57-
/// To check enable/disable state of the time-profiler call:
58-
/// is_time_profiling_enabled();
59-
/// The time-profiler is enabled by default.
62+
/// \endcode
63+
/// \remarks
64+
/// In order to activate the system you need to set the value of the
65+
/// macro TMPROF_ENABLED below to 1.
66+
/// \remarks
67+
/// Recursive calls are treated as independent ones.
68+
/// \remarks
69+
/// A C++ code block may have at most one TMPROF_BLOCK() call.
70+
/// \remarks
71+
/// The implementation is NOT thread-safe.
72+
/// \remarks
73+
/// You can temporarily disable time-profiler by calling:
74+
/// enable_time_profiling(false);
75+
/// To check enable/disable state of the time-profiler call:
76+
/// is_time_profiling_enabled();
77+
/// The time-profiler is enabled by default.
6078

79+
//#define TMPROF_ENABLED
80+
#ifdef TMPROF_ENABLED
6181
#define TMPROF_BLOCK() \
62-
static __tmprof_detail_data* const __tmprof_detail_record_ptr = \
63-
__tmprof_detail_add_record(__tmprof_detail_data(__FUNCTION__,__LINE__)); \
64-
const __tmprof_detail_on_hit __tmprof_detail_hit( \
65-
is_time_profiling_enabled() ? __tmprof_detail_record_ptr : nullptr)
82+
static tmprof_detail_datat *const __tmprof_detail_record_ptr = \
83+
tmprof_detail_datat::create(__FUNCTION__, __LINE__); \
84+
const tmprof_detail_on_hitt __tmprof_detail_hit(__tmprof_detail_record_ptr)
85+
#else
86+
#define TMPROF_BLOCK()
87+
#endif
6688

6789
bool is_time_profiling_enabled();
68-
void enable_time_profiling(const bool state);
90+
void enable_time_profiling(const bool state = true);
6991

70-
void tmprof_dump_in_json(std::ostream &ostr);
71-
void tmprof_dump_in_json(const boost::filesystem::path &out_file_pathname);
92+
void tmprof_dump_as_json(std::ostream &ostr);
93+
void tmprof_dump_as_json(const boost::filesystem::path &out_file_pathname);
7294

7395
// Next follow PRIVATE IMPLEMENTATION DETAILS. Do not use them directly !!!
7496

75-
struct __tmprof_detail_data
97+
struct tmprof_detail_datat
7698
{
77-
__tmprof_detail_data(const std::string &name, const unsigned long line)
99+
static tmprof_detail_datat *
100+
create(const char *name, const unsigned long line);
101+
102+
tmprof_detail_datat(const char *const name, const unsigned long line)
78103
: name(name), line(line), total_time(0.0), max_time(0.0), hit_count(0ULL)
79104
{
80105
}
81106

82-
std::string name;
83-
unsigned long line;
107+
const char *get_name() const
108+
{
109+
return name;
110+
}
111+
112+
const unsigned long get_line() const
113+
{
114+
return line;
115+
}
116+
117+
double get_total_time() const
118+
{
119+
return total_time;
120+
}
121+
122+
double get_max_time() const
123+
{
124+
return max_time;
125+
}
126+
127+
unsigned long long get_hit_count() const
128+
{
129+
return hit_count;
130+
}
131+
132+
void on_hit(const double duration);
133+
134+
static json_objectt dump_as_json();
135+
136+
private:
137+
const char *name;
138+
const unsigned long line;
84139
double total_time;
85140
double max_time;
86141
unsigned long long hit_count;
142+
143+
static std::list<tmprof_detail_datat> records;
87144
};
88145

89-
struct __tmprof_detail_on_hit
146+
struct tmprof_detail_on_hitt
90147
{
91-
explicit __tmprof_detail_on_hit(__tmprof_detail_data *const data)
148+
explicit tmprof_detail_on_hitt(tmprof_detail_datat *const data)
92149
: data(data), start_time(std::chrono::high_resolution_clock::now())
93150
{
94151
}
95-
~__tmprof_detail_on_hit()
152+
~tmprof_detail_on_hitt()
96153
{
97-
if(data != nullptr)
154+
if(is_time_profiling_enabled())
98155
{
99156
auto const duration =
100157
std::chrono::duration<double>(
101158
std::chrono::high_resolution_clock::now() - start_time)
102159
.count();
103-
data->total_time += duration;
104-
if(data->max_time < duration)
105-
data->max_time = duration;
106-
++data->hit_count;
160+
data->on_hit(duration);
107161
}
108162
}
109-
__tmprof_detail_data *data;
163+
164+
private:
165+
tmprof_detail_datat *data;
110166
std::chrono::high_resolution_clock::time_point start_time;
111167
};
112168

113-
__tmprof_detail_data *
114-
__tmprof_detail_add_record(const __tmprof_detail_data &data);
115-
116169
#endif //CPROVER_SECURITY_ANALYSER_UTIL_TMPROF_H

0 commit comments

Comments
 (0)