lyx_mirror/src/support/pmprof.h

234 lines
5.0 KiB
C
Raw Normal View History

// -*- 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.
*/
/**
* ==== HOW TO USE THIS TRIVIAL PROFILER:
*
* * at the beginning of the interesting block, just add:
* PROFILE_THIS_BLOCK(some_identifier)
*
* A trailing semicolon can be added at your discretion.
*
* * when the program ends, statistics will be sent to standard error, like:
*
* #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
*
* * if DISABLE_PMPROF is defined before including pmprof.h, the
* profiler is replaced by empty macros. This is useful for quickly
* checking the overhead.
*
* ==== ABOUT PROFILING SCOPE:
*
* The code measured by the profiler corresponds to the lifetime of a
* local variable declared by the PROFILE_THIS_BLOCK macro.
*
* 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
* }
* }
*/
#ifndef PMPROF_H
#define PMPROF_H
#if defined(DISABLE_PMPROF)
// Make pmprof an empty shell
#define PROFILE_THIS_BLOCK(a)
#define PROFILE_CACHE_MISS(a)
#else
#include <chrono>
#include <iomanip>
#include <iostream>
#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;
namespace {
void dump_time(system_clock::duration value)
{
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";
else
cerr << musec << " us";
}
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;
}
Bulk cleanup/fix incorrect annotation at the end of namespaces. This commit does a bulk fix of incorrect annotations (comments) at the end of namespaces. The commit was generated by initially running clang-format, and then from the diff of the result extracting the hunks corresponding to fixes of namespace comments. The changes being applied and all the results have been manually reviewed. The source code successfully builds on macOS. Further details on the steps below, in case they're of interest to someone else in the future. 1. Checkout a fresh and up to date version of src/ git pull && git checkout -- src && git status src 2. Ensure there's a suitable .clang-format in place, i.e. with options to fix the comment at the end of namespaces, including: FixNamespaceComments: true SpacesBeforeTrailingComments: 1 and that clang-format is >= 5.0.0, by doing e.g.: clang-format -dump-config | grep Comments: clang-format --version 3. Apply clang-format to the source: clang-format -i $(find src -name "*.cpp" -or -name "*.h") 4. Create and filter out hunks related to fixing the namespace git diff -U0 src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace.patch 5. Filter out hunks corresponding to simple fixes into to a separate patch: pcregrep -M -e '^diff[^\n]+\nindex[^\n]+\n--- [^\n]+\n\+\+\+ [^\n]+\n' \ -e '^@@ -[0-9]+ \+[0-9]+ @@[^\n]*\n-\}[^\n]*\n\+\}[^\n]*\n' \ fix_namespace.patch > fix_namespace_simple.patch 6. Manually review the simple patch and then apply it, after first restoring the source. git checkout -- src patch -p1 < fix_namespace_simple.path 7. Manually review the (simple) changes and then stage the changes git diff src git add src 8. Again apply clang-format and filter out hunks related to any remaining fixes to the namespace, this time filter with more context. There will be fewer hunks as all the simple cases have already been handled: clang-format -i $(find src -name "*.cpp" -or -name "*.h") git diff src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace2.patch 9. Manually review/edit the resulting patch file to remove hunks for files which need to be dealt with manually, noting the file names and line numbers. Then restore files to as before applying clang-format and apply the patch: git checkout src patch -p1 < fix_namespace2.patch 10. Manually fix the files noted in the previous step. Stage files, review changes and commit.
2017-07-23 11:11:54 +00:00
} // namespace
/* Helper class for gathering data. Instantiate this as a static
* variable, so that its destructor will be executed when the program
* ends.
*/
class stat {
public:
stat(char const * name) : name_(name) {}
~stat() {
if (count_ + miss_count_ > 0) {
if (miss_count_ == 0) {
cerr << "#pmprof# " << name_ << ": ";
dump(dur_, count_);
}
else {
cerr << "#pmprof# " << name_ << ": ";
dump(dur_ + miss_dur_, count_ + miss_count_);
cerr << " hit: " << 100 * count_ / (count_ + miss_count_) << "%, ";
dump(dur_, count_);
cerr << " miss: " << 100 * miss_count_ / (count_ + miss_count_) << "%, ";
dump(miss_dur_, miss_count_);
}
} else
std::cerr << "#pmprof# " << name_ << ": no data" << std::endl;
}
void add(system_clock::duration d, const bool hit) {
if (hit) {
dur_ += d;
count_++;
} else {
miss_dur_ += d;
miss_count_++;
}
}
private:
char const * name_;
system_clock::duration dur_, miss_dur_;
unsigned long long count_ = 0, miss_count_ = 0;
};
/* 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
* stat object.
*/
class instance {
public:
instance(stat * stat) : hit(true), stat_(stat)
{
before_ = system_clock::now();
}
~instance() {
stat_->add(system_clock::now() - before_, hit);
}
bool hit;
private:
system_clock::time_point before_;
stat * stat_;
};
}
#define PROFILE_THIS_BLOCK(a) \
static _pmprof::stat _pmps_##a(#a); \
_pmprof::instance _pmpi_##a(&_pmps_##a);
#define PROFILE_CACHE_MISS(a) \
_pmpi_##a.hit = false;
#endif // !defined(DISABLE_PMPROF)
#endif