From: Gonzalo A. <ga...@us...> - 2006-09-12 18:05:04
|
Update of /cvsroot/mod-c/ehtml/include In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv22962/include Added Files: Profiling.h Log Message: Profiling classes & macro ("ProfileMe"). --- NEW FILE: Profiling.h --- #ifndef __PROFILING_H_ #define __PROFILING_H_ ////////////////////////////////////////////////////////////////////////////// // // Profiling declarations // typedef int64_t hrtime_t; static inline hrtime_t get_tick(void) { hrtime_t regs; asm volatile ("rdtsc":"=A" (regs)); return regs; } class ProfileFunction { hrtime_t _accum; int64_t _ncalls; int _overflows; int _depth; const char* _name; const char* _file; int _line; ProfileFunction *_next, *_prev; static ProfileFunction *_head; friend class ProfileRun; public: ProfileFunction(const char* name, const char* file, int line): _accum(0), _ncalls(0), _overflows(0), _depth(0), _name(name), _file(file), _line(line) { _next = _head; _prev = NULL; if (_next) _next->_prev = this; _head = this; } ~ProfileFunction() { if (_next) _next->_prev = _prev; if (_prev) _prev->_next = _next; else _head = _next; } static ProfileFunction* Head() { return _head; } ProfileFunction* Next() { return _next; } hrtime_t Accum() const { return _accum; } int64_t NCalls() const { return _ncalls; } int Overflows() const { return _overflows; } const char* Function() const { return _name; } const char* File() const { return _file; } int Line() const { return _line; } }; class ProfileRun { hrtime_t _started; ProfileFunction* _f; public: ProfileRun(ProfileFunction* f): _started(get_tick()), _f(f) { ; } ~ProfileRun() { hrtime_t diff = get_tick() - _started; if (diff < 0) { ++_f->_overflows; return; } _f->_accum += diff; ++_f->_ncalls; } }; #define ProfileMe \ static ProfileFunction(__PRETTY_FUNCTION__, __FILE__, __LINE__) __profile;\ ProfileRun __profile_run(&__profileme); #endif |