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 dumpTime(long long value)
150 std::cerr << std::fixed << std::setprecision(2);
151 if (value >= 1000000)
152 std::cerr << value / 1000000 << "sec";
153 else if (value >= 1000)
154 std::cerr << value / 1000 << "msec";
156 std::cerr << value << "usec";
159 void dump(long long sec, long long usec, unsigned long long count) {
160 double const total = sec * 1000000 + usec;
161 dumpTime(total / count);
162 std::cerr << ", count=" << count
165 std::cerr << std::endl;
171 /* Helper class for gathering data. Instantiate this as a static
172 * variable, so that its destructor will be executed when the program
179 PMProfStat(char const * name) : name_(name), sec_(0), usec_(0), count_(0),
180 miss_sec_(0), miss_usec_(0), miss_count_(0) {}
184 if (miss_count_ == 0) {
185 std::cerr << "#pmprof# " << name_ << ": ";
186 dump(sec_, usec_, count_);
189 std::cerr << "#pmprof# " << name_ << ": ";
190 dump(sec_ + miss_sec_, usec_ + miss_usec_, count_ + miss_count_);
191 std::cerr << " hit: " << 100 * count_ / (count_ + miss_count_) << "%, ";
192 dump(sec_, usec_, count_);
193 std::cerr << " miss: " << 100 * miss_count_ / (count_ + miss_count_) << "%, ";
194 dump(miss_sec_, miss_usec_, miss_count_);
199 void add(const long long s, const long long u, const bool hit) {
213 long long sec_, usec_;
214 unsigned long long count_;
215 long long miss_sec_, miss_usec_;
216 unsigned long long miss_count_;
220 /* Helper class which gathers data at the end of the scope. One
221 * instance of this one should be created at each execution of the
222 * block. At the end of the block, it sends statistics to the static
225 class PMProfInstance {
227 PMProfInstance(PMProfStat * stat) : hit(true), stat_(stat)
229 gettimeofday(&before_, 0);
233 gettimeofday(&after_, 0);
234 stat_->add(after_.tv_sec - before_.tv_sec,
235 after_.tv_usec - before_.tv_usec, hit);
241 timeval before_, after_;
246 #define PROFILE_THIS_BLOCK(a) \
247 static PMProfStat PMPS_##a(#a);\
248 PMProfInstance PMPI_##a(&PMPS_##a);
250 #define PROFILE_CACHE_MISS(a) \
251 PMPI_##a.hit = false;
253 #endif // !defined(DISABLE_PMPROF)