3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
8 * Full author contact details are available in file CREDITS.
12 * ==== HOW TO USE THIS TRIVIAL PROFILER:
14 * * at the beginning of the interesting block, just add:
15 * PROFILE_THIS_BLOCK(some_identifier)
17 * A trailing semicolon can be added at your discretion.
19 * * when the program ends, statistics will be sent to standard error, like:
21 * #pmprof# some_identifier: 6.51usec, count=7120, total=46.33msec
23 * * It is also possible to profile caching schemes. All it takes is an additional
24 * PROFILE_CACHE_MISS(some_identifier)
25 * in the place that takes care of cache misses. Then the output at the end will change to
27 * #pmprof# some_identifier: 6.51usec, count=7120, total=46.33msec
28 * hit: 96%, 4.36usec, count=6849, total=29.89msec
29 * miss: 3%, 60.65usec, count=271, total=16.43msec
31 * * if DISABLE_PMPROF is defined before including pmprof.h, the
32 * profiler is replaced by empty macros. This is useful for quickly
33 * checking the overhead.
35 * ==== ABOUT PROFILING SCOPE:
37 * The code measured by the profiler corresponds to the lifetime of a
38 * local variable declared by the PROFILE_THIS_BLOCK macro.
40 * Some examples of profiling scope: In the snippets below, c1, c2...
41 * designate code chunks, and the identifiers of profiling blocks are
42 * chosen to reflect what they count.
46 * PROFILE_THIS_BLOCK(c2)
52 * PROFILE_THIS_BLOCK(c1_c2)
54 * PROFILE_THIS_BLOCK(c2)
61 * PROFILE_THIS_BLOCK(c1)
64 * PROFILE_THIS_BLOCK(c2)
70 * PROFILE_THIS_BLOCK(c1_c2_c3)
73 * PROFILE_THIS_BLOCK(c2)
79 * Influence of identifier names: they are mainly used for display
80 * purpose, but the same name should not be used twice in the same
84 * PROFILE_THIS_BLOCK(foo)
86 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
90 * In the example below, c1+c2 and c2 are counted separately, but in
91 * the output, both are confusingly labelled `foo'.
94 * PROFILE_THIS_BLOCK(foo)
97 * PROFILE_THIS_BLOCK(foo) // error: identifier clash
107 #if defined(DISABLE_PMPROF)
109 // Make pmprof an empty shell
110 #define PROFILE_THIS_BLOCK(a)
111 #define PROFILE_CACHE_MISS(a)
120 #if defined(__GNUG__) && defined(_GLIBCXX_DEBUG)
121 #error Profiling is not usable when run-time debugging is in effect
127 using namespace chrono;
131 void dump_time(system_clock::duration value)
133 auto musec = duration_cast<microseconds>(value).count();
134 cerr << fixed << setprecision(2);
135 if (musec >= 1000000)
136 cerr << musec / 1000000.0 << " s";
137 else if (musec >= 1000)
138 cerr << musec / 1000.0 << " ms";
140 cerr << musec << " us";
143 void dump(system_clock::duration total, unsigned long long count) {
145 dump_time(total / count);
146 cerr << ", count=" << count << ", total=";
149 std::cerr << "no data";
156 /* Helper class for gathering data. Instantiate this as a static
157 * variable, so that its destructor will be executed when the program
162 stat(char const * name) : name_(name) {}
165 if (count_ + miss_count_ > 0) {
166 if (miss_count_ == 0) {
167 cerr << "#pmprof# " << name_ << ": ";
171 cerr << "#pmprof# " << name_ << ": ";
172 dump(dur_ + miss_dur_, count_ + miss_count_);
173 cerr << " hit: " << 100 * count_ / (count_ + miss_count_) << "%, ";
175 cerr << " miss: " << 100 * miss_count_ / (count_ + miss_count_) << "%, ";
176 dump(miss_dur_, miss_count_);
179 std::cerr << "#pmprof# " << name_ << ": no data" << std::endl;
182 void add(system_clock::duration d, const bool hit) {
194 system_clock::duration dur_, miss_dur_;
195 unsigned long long count_ = 0, miss_count_ = 0;
199 /* Helper class which gathers data at the end of the scope. One
200 * instance of this one should be created at each execution of the
201 * block. At the end of the block, it sends statistics to the static
206 instance(stat * stat) : hit(true), stat_(stat)
208 before_ = system_clock::now();
212 stat_->add(system_clock::now() - before_, hit);
218 system_clock::time_point before_;
224 #define PROFILE_THIS_BLOCK(a) \
225 static _pmprof::stat _pmps_##a(#a); \
226 _pmprof::instance _pmpi_##a(&_pmps_##a);
228 #define PROFILE_CACHE_MISS(a) \
229 _pmpi_##a.hit = false;
231 #endif // !defined(DISABLE_PMPROF)