2013-03-21 11:14:21 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/* \file pmprof.h
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2016-07-05 13:29:28 +00:00
|
|
|
/**
|
|
|
|
* ==== HOW TO USE THIS TRIVIAL PROFILER:
|
2013-03-21 11:14:21 +00:00
|
|
|
*
|
|
|
|
* * at the beginning of the interesting block, just add:
|
2013-03-22 10:23:48 +00:00
|
|
|
* PROFILE_THIS_BLOCK(some_identifier)
|
|
|
|
*
|
|
|
|
* A trailing semicolon can be added at your discretion.
|
2013-03-21 11:14:21 +00:00
|
|
|
*
|
|
|
|
* * when the program ends, statistics will be sent to standard error, like:
|
|
|
|
*
|
2016-07-05 13:29:28 +00:00
|
|
|
* #pmprof# some_identifier: 6.51usec, count=7120, total=46.33msec
|
|
|
|
*
|
|
|
|
* * It is also possible to profile caching schemes. All it takes is an additional
|
|
|
|
* PROFILE_CACHE_MISS(some_identifier)
|
|
|
|
* in the place that takes care of cache misses. Then the output at the end will change to
|
|
|
|
*
|
|
|
|
* #pmprof# some_identifier: 6.51usec, count=7120, total=46.33msec
|
|
|
|
* hit: 96%, 4.36usec, count=6849, total=29.89msec
|
|
|
|
* miss: 3%, 60.65usec, count=271, total=16.43msec
|
|
|
|
*
|
2016-12-09 14:44:45 +00:00
|
|
|
* * if DISABLE_PMPROF is defined before including pmprof.h, the
|
|
|
|
* profiler is replaced by empty macros. This is useful for quickly
|
|
|
|
* checking the overhead.
|
2016-07-05 13:29:28 +00:00
|
|
|
*
|
|
|
|
* ==== ABOUT PROFILING SCOPE:
|
2013-03-21 11:14:21 +00:00
|
|
|
*
|
|
|
|
* The code measured by the profiler corresponds to the lifetime of a
|
|
|
|
* local variable declared by the PROFILE_THIS_BLOCK macro.
|
2013-03-22 10:23:48 +00:00
|
|
|
*
|
|
|
|
* Some examples of profiling scope: In the snippets below, c1, c2...
|
|
|
|
* designate code chunks, and the identifiers of profiling blocks are
|
|
|
|
* chosen to reflect what they count.
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* c1
|
|
|
|
* PROFILE_THIS_BLOCK(c2)
|
|
|
|
* c2
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(c1_c2)
|
|
|
|
* c1
|
|
|
|
* PROFILE_THIS_BLOCK(c2)
|
|
|
|
* c2
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(c1)
|
|
|
|
* c1
|
|
|
|
* }
|
|
|
|
* PROFILE_THIS_BLOCK(c2)
|
|
|
|
* c2
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(c1_c2_c3)
|
|
|
|
* c1
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(c2)
|
|
|
|
* c2
|
|
|
|
* }
|
|
|
|
* c3
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Influence of identifier names: they are mainly used for display
|
|
|
|
* purpose, but the same name should not be used twice in the same
|
|
|
|
* scope.
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(foo)
|
|
|
|
* c1
|
|
|
|
* PROFILE_THIS_BLOCK(foo) // error: identifier clash
|
|
|
|
* c2
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* In the example below, c1+c2 and c2 are counted separately, but in
|
|
|
|
* the output, both are confusingly labelled `foo'.
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(foo)
|
|
|
|
* c1
|
|
|
|
* {
|
|
|
|
* PROFILE_THIS_BLOCK(foo) // error: identifier clash
|
|
|
|
* c2
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
|
2013-03-21 11:14:21 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-30 14:43:29 +00:00
|
|
|
#ifndef PMPROF_H
|
|
|
|
#define PMPROF_H
|
|
|
|
|
2016-12-09 14:44:45 +00:00
|
|
|
#if defined(DISABLE_PMPROF)
|
|
|
|
|
|
|
|
// Make pmprof an empty shell
|
|
|
|
#define PROFILE_THIS_BLOCK(a)
|
|
|
|
#define PROFILE_CACHE_MISS(a)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-03-17 12:57:44 +00:00
|
|
|
#include <chrono>
|
2013-04-30 14:43:29 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
#if defined(__GNUG__) && defined(_GLIBCXX_DEBUG)
|
|
|
|
#error Profiling is not usable when run-time debugging is in effect
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace _pmprof {
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace chrono;
|
2013-03-22 10:23:48 +00:00
|
|
|
|
2016-07-05 13:29:28 +00:00
|
|
|
namespace {
|
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
void dump_time(system_clock::duration value)
|
2018-03-05 09:28:27 +00:00
|
|
|
{
|
2022-05-19 09:35:39 +00:00
|
|
|
auto musec = duration_cast<microseconds>(value).count();
|
|
|
|
cerr << fixed << setprecision(2);
|
|
|
|
if (musec >= 1000000)
|
|
|
|
cerr << musec / 1000000.0 << " s";
|
|
|
|
else if (musec >= 1000)
|
|
|
|
cerr << musec / 1000.0 << " ms";
|
2018-03-05 09:28:27 +00:00
|
|
|
else
|
2022-05-19 09:35:39 +00:00
|
|
|
cerr << musec << " us";
|
2018-03-05 09:28:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
void dump(system_clock::duration total, unsigned long long count) {
|
|
|
|
if (count > 0) {
|
|
|
|
dump_time(total / count);
|
|
|
|
cerr << ", count=" << count << ", total=";
|
|
|
|
dump_time(total);
|
|
|
|
} else
|
|
|
|
std::cerr << "no data";
|
|
|
|
cerr << endl;
|
2016-07-05 13:29:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 11:11:54 +00:00
|
|
|
} // namespace
|
2016-07-05 13:29:28 +00:00
|
|
|
|
|
|
|
|
2013-03-21 11:14:21 +00:00
|
|
|
/* Helper class for gathering data. Instantiate this as a static
|
|
|
|
* variable, so that its destructor will be executed when the program
|
|
|
|
* ends.
|
|
|
|
*/
|
2022-05-19 09:35:39 +00:00
|
|
|
class stat {
|
2013-03-21 11:14:21 +00:00
|
|
|
public:
|
2022-05-19 09:35:39 +00:00
|
|
|
stat(char const * name) : name_(name) {}
|
2013-03-21 11:14:21 +00:00
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
~stat() {
|
|
|
|
if (count_ + miss_count_ > 0) {
|
2016-07-05 13:29:28 +00:00
|
|
|
if (miss_count_ == 0) {
|
2022-05-19 09:35:39 +00:00
|
|
|
cerr << "#pmprof# " << name_ << ": ";
|
2019-03-17 12:57:44 +00:00
|
|
|
dump(dur_, count_);
|
2016-07-05 13:29:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-05-19 09:35:39 +00:00
|
|
|
cerr << "#pmprof# " << name_ << ": ";
|
2019-03-17 12:57:44 +00:00
|
|
|
dump(dur_ + miss_dur_, count_ + miss_count_);
|
2022-05-19 09:35:39 +00:00
|
|
|
cerr << " hit: " << 100 * count_ / (count_ + miss_count_) << "%, ";
|
2019-03-17 12:57:44 +00:00
|
|
|
dump(dur_, count_);
|
2022-05-19 09:35:39 +00:00
|
|
|
cerr << " miss: " << 100 * miss_count_ / (count_ + miss_count_) << "%, ";
|
2019-03-17 12:57:44 +00:00
|
|
|
dump(miss_dur_, miss_count_);
|
2016-07-05 13:29:28 +00:00
|
|
|
}
|
2022-05-19 09:35:39 +00:00
|
|
|
} else
|
|
|
|
std::cerr << "#pmprof# " << name_ << ": no data" << std::endl;
|
2013-03-21 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
void add(system_clock::duration d, const bool hit) {
|
2016-07-05 13:29:28 +00:00
|
|
|
if (hit) {
|
2019-03-17 12:57:44 +00:00
|
|
|
dur_ += d;
|
2016-07-05 13:29:28 +00:00
|
|
|
count_++;
|
|
|
|
} else {
|
2019-03-17 12:57:44 +00:00
|
|
|
miss_dur_ += d;
|
2016-07-05 13:29:28 +00:00
|
|
|
miss_count_++;
|
|
|
|
}
|
2013-03-21 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char const * name_;
|
2022-05-19 09:35:39 +00:00
|
|
|
system_clock::duration dur_, miss_dur_;
|
|
|
|
unsigned long long count_ = 0, miss_count_ = 0;
|
2013-03-21 11:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Helper class which gathers data at the end of the scope. One
|
|
|
|
* instance of this one should be created at each execution of the
|
|
|
|
* block. At the end of the block, it sends statistics to the static
|
2022-05-19 09:35:39 +00:00
|
|
|
* stat object.
|
2013-03-21 11:14:21 +00:00
|
|
|
*/
|
2022-05-19 09:35:39 +00:00
|
|
|
class instance {
|
2013-03-21 11:14:21 +00:00
|
|
|
public:
|
2022-05-19 09:35:39 +00:00
|
|
|
instance(stat * stat) : hit(true), stat_(stat)
|
2013-03-21 11:14:21 +00:00
|
|
|
{
|
2022-05-19 09:35:39 +00:00
|
|
|
before_ = system_clock::now();
|
2013-03-21 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
~instance() {
|
|
|
|
stat_->add(system_clock::now() - before_, hit);
|
2013-03-21 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 13:29:28 +00:00
|
|
|
bool hit;
|
|
|
|
|
2013-03-21 11:14:21 +00:00
|
|
|
private:
|
2022-05-19 09:35:39 +00:00
|
|
|
system_clock::time_point before_;
|
|
|
|
stat * stat_;
|
2013-03-21 11:14:21 +00:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:35:39 +00:00
|
|
|
}
|
2013-03-21 11:14:21 +00:00
|
|
|
|
|
|
|
#define PROFILE_THIS_BLOCK(a) \
|
2022-05-19 09:35:39 +00:00
|
|
|
static _pmprof::stat _pmps_##a(#a); \
|
|
|
|
_pmprof::instance _pmpi_##a(&_pmps_##a);
|
2013-03-21 11:14:21 +00:00
|
|
|
|
2016-07-05 13:29:28 +00:00
|
|
|
#define PROFILE_CACHE_MISS(a) \
|
2022-05-19 09:35:39 +00:00
|
|
|
_pmpi_##a.hit = false;
|
2016-07-05 13:29:28 +00:00
|
|
|
|
2016-12-09 14:44:45 +00:00
|
|
|
#endif // !defined(DISABLE_PMPROF)
|
2013-03-21 11:14:21 +00:00
|
|
|
|
|
|
|
#endif
|