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)
118 #include <sys/time.h>
125 #if defined(__GNUG__) && defined(_GLIBCXX_DEBUG)
126 #error Profiling is not usable when run-time debugging is in effect
130 /* This function does not really returns the "time of day",
131 * but it will suffice to evaluate elapsed times.
133 int gettimeofday(struct timeval * tv, struct timezone * /*tz*/)
135 LARGE_INTEGER frequency, t;
136 QueryPerformanceFrequency(&frequency);
137 QueryPerformanceCounter(&t);
139 tv->tv_sec = long(t.QuadPart / frequency.QuadPart);
140 tv->tv_usec = long((1000000.0 * (t.QuadPart % frequency.QuadPart)) / frequency.QuadPart);
148 void dump(long long sec, long long usec, unsigned long long count) {
149 double const total = sec * 1000000 + usec;
150 std::cerr << std::fixed << std::setprecision(2)
152 << "usec, count=" << count
153 << ", total=" << total * 0.001 << "msec"
160 /* Helper class for gathering data. Instantiate this as a static
161 * variable, so that its destructor will be executed when the program
168 PMProfStat(char const * name) : name_(name), sec_(0), usec_(0), count_(0),
169 miss_sec_(0), miss_usec_(0), miss_count_(0) {}
173 if (miss_count_ == 0) {
174 std::cerr << "#pmprof# " << name_ << ": ";
175 dump(sec_, usec_, count_);
178 std::cerr << "#pmprof# " << name_ << ": ";
179 dump(sec_ + miss_sec_, usec_ + miss_usec_, count_ + miss_count_);
180 std::cerr << " hit: " << 100 * count_ / (count_ + miss_count_) << "%, ";
181 dump(sec_, usec_, count_);
182 std::cerr << " miss: " << 100 * miss_count_ / (count_ + miss_count_) << "%, ";
183 dump(miss_sec_, miss_usec_, miss_count_);
188 void add(const long long s, const long long u, const bool hit) {
202 long long sec_, usec_;
203 unsigned long long count_;
204 long long miss_sec_, miss_usec_;
205 unsigned long long miss_count_;
209 /* Helper class which gathers data at the end of the scope. One
210 * instance of this one should be created at each execution of the
211 * block. At the end of the block, it sends statistics to the static
214 class PMProfInstance {
216 PMProfInstance(PMProfStat * stat) : hit(true), stat_(stat)
218 gettimeofday(&before_, 0);
222 gettimeofday(&after_, 0);
223 stat_->add(after_.tv_sec - before_.tv_sec,
224 after_.tv_usec - before_.tv_usec, hit);
230 timeval before_, after_;
235 #define PROFILE_THIS_BLOCK(a) \
236 static PMProfStat PMPS_##a(#a);\
237 PMProfInstance PMPI_##a(&PMPS_##a);
239 #define PROFILE_CACHE_MISS(a) \
240 PMPI_##a.hit = false;
242 #endif // !defined(DISABLE_PMPROF)