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
126 void dumpTime(std::chrono::duration<double> value)
128 double const val = value.count();
129 std::cerr << std::fixed << std::setprecision(2);
131 std::cerr << val << " s";
132 else if (val >= 0.001)
133 std::cerr << val * 1000 << " ms";
135 std::cerr << val * 1000000 << " us";
138 void dump(std::chrono::duration<double> total, unsigned long long count) {
139 dumpTime(total / count);
140 std::cerr << ", count=" << count
143 std::cerr << std::endl;
149 /* Helper class for gathering data. Instantiate this as a static
150 * variable, so that its destructor will be executed when the program
157 PMProfStat(char const * name) : name_(name), count_(0), miss_count_(0) {}
161 if (miss_count_ == 0) {
162 std::cerr << "#pmprof# " << name_ << ": ";
166 std::cerr << "#pmprof# " << name_ << ": ";
167 dump(dur_ + miss_dur_, count_ + miss_count_);
168 std::cerr << " hit: " << 100 * count_ / (count_ + miss_count_) << "%, ";
170 std::cerr << " miss: " << 100 * miss_count_ / (count_ + miss_count_) << "%, ";
171 dump(miss_dur_, miss_count_);
176 void add(std::chrono::duration<double> d, const bool hit) {
188 std::chrono::duration<double> dur_, miss_dur_;
189 unsigned long long count_, miss_count_;
193 /* Helper class which gathers data at the end of the scope. One
194 * instance of this one should be created at each execution of the
195 * block. At the end of the block, it sends statistics to the static
198 class PMProfInstance {
200 PMProfInstance(PMProfStat * stat) : hit(true), stat_(stat)
202 before_ = std::chrono::system_clock::now();
206 stat_->add(std::chrono::system_clock::now() - before_, hit);
212 std::chrono::system_clock::time_point before_;
217 #define PROFILE_THIS_BLOCK(a) \
218 static PMProfStat PMPS_##a(#a);\
219 PMProfInstance PMPI_##a(&PMPS_##a);
221 #define PROFILE_CACHE_MISS(a) \
222 PMPI_##a.hit = false;
224 #endif // !defined(DISABLE_PMPROF)