📄 tvtimer.c
字号:
/* COPYRIGHT NOTICE This material was developed by Christos Faloutsos and King-Ip Linat the University of Maryland, College Park, Department of Computer Science.Permission is granted to copy this software, to redistribute iton a nonprofit basis, and to use it for any purpose, subject tothe following restrictions and understandings. 1. Any copy made of this software must include this copyright noticein full. 2. All materials developed as a consequence of the use of thissoftware shall duly acknowledge such use, in accordance with the usualstandards of acknowledging credit in academic research. 3. The authors have made no warranty or representation that theoperation of this software will be error-free or suitable for anyapplication, and they are under under no obligation to provide anyservices, by way of maintenance, update, or otherwise. The softwareis an experimental prototype offered on an as-is basis. 4. Redistribution for profit requires the express, written permissionof the authors. */// Author : $Author$// Date : $Date$// Id : $Id$#include <iostream.h>#include "TVtimer.h"extern "C" {// For some reason, getrusage() is not prototyped in sys/resouce.hint getrusage(int, struct rusage*);}//// Some inline functions for timeval and rusage arithmetic//inline float timevaltof(const struct timeval &tp){ return float(tp.tv_sec) + (float(tp.tv_usec) / 1000000.0);}inline void timevalclr(struct timeval &tp){ tp.tv_sec = tp.tv_usec = 0;}inline void timevalsub(struct timeval &t1, const struct timeval &t2){ t1.tv_sec -= t2.tv_sec; t1.tv_usec -= t2.tv_usec; if (t1.tv_usec < 0) { t1.tv_sec--; t1.tv_usec += 1000000; }}inline void timevaladd(struct timeval &t1, const struct timeval &t2){ t1.tv_sec += t2.tv_sec; t1.tv_usec += t2.tv_usec; if (t1.tv_usec >= 1000000) { t1.tv_sec++; t1.tv_usec -= 1000000; }}inline void rusageclr(struct rusage &ru){ timevalclr(ru.ru_utime); timevalclr(ru.ru_stime); ru.ru_minflt = ru.ru_majflt = ru.ru_nswap = ru.ru_nvcsw = ru.ru_nivcsw = 0;}inline void rusagesub(struct rusage &r1, const struct rusage &r2){ timevalsub(r1.ru_utime, r2.ru_utime); timevalsub(r1.ru_stime, r2.ru_stime); r1.ru_minflt -= r2.ru_minflt; r1.ru_majflt -= r2.ru_majflt; r1.ru_nswap -= r2.ru_nswap; r1.ru_nvcsw -= r2.ru_nvcsw; r1.ru_nivcsw -= r2.ru_nivcsw;}inline void rusageadd(struct rusage &r1, const struct rusage &r2){ timevaladd(r1.ru_utime, r2.ru_utime); timevaladd(r1.ru_stime, r2.ru_stime); r1.ru_minflt += r2.ru_minflt; r1.ru_majflt += r2.ru_majflt; r1.ru_nswap += r2.ru_nswap; r1.ru_nvcsw += r2.ru_nvcsw; r1.ru_nivcsw += r2.ru_nivcsw;}//----------------------------------------------------//// Timer class////----------------------------------------------------Timer Timer::_global("Global Timer", Timer::kRunning);//// Create a new timer with no accumulated time and start it running//Timer::Timer(char *name, char mode){ // Assign name if valid if (name) { _name = new char [strlen(name) + 1]; strcpy(_name, name); } // Clear the accumulated time rusageclr(_ru_sum); timevalclr(_tp_sum); if (mode & kRunning) { // Set start of timer getrusage(RUSAGE_SELF, &_ru_start); gettimeofday(&_tp_start, (struct timezone *) NULL); // Start timer running _state = kRunning; }}float Timer::real(){ struct timeval tp; if (_state & Timer::kRunning) { // Determine current "time" gettimeofday(&tp, (struct timezone *) NULL); // Calculate how much time has passed since we last started running timevalsub(tp, _tp_start); // Add to this the time accumulated so far by the timer timevaladd(tp, _tp_sum); } else { // We are not running. Just display the time accumulated tp = _tp_sum; } return timevaltof(tp);}float Timer::user(){ struct rusage ru; if (_state & Timer::kRunning) { // Determine current "time" getrusage(RUSAGE_SELF, &ru); // Calculate how much time has passed since we last started running rusagesub(ru, _ru_start); // Add to this the time accumulated so far by the timer rusageadd(ru, _ru_sum); } else { // We are not running. Just display the time accumulated ru = _ru_sum; } return timevaltof(ru.ru_utime);}float Timer::system(){ struct rusage ru; if (_state & Timer::kRunning) { // Determine current "time" getrusage(RUSAGE_SELF, &ru); // Calculate how much time has passed since we last started running rusagesub(ru, _ru_start); // Add to this the time accumulated so far by the timer rusageadd(ru, _ru_sum); } else { // We are not running. Just display the time accumulated ru = _ru_sum; } return timevaltof(ru.ru_stime);}//// Synchronize this timer with the global timer (this means turnining the timer// on if it is currently stopped)//void Timer::sync(){ *this = _global;}//// Reset this timer to zero//void Timer::reset(){ // Clear any accumulated time rusageclr(_ru_sum); timevalclr(_tp_sum); if (_state & kRunning) { // Reset start of timer if timer is running getrusage(RUSAGE_SELF, &_ru_start); gettimeofday(&_tp_start, (struct timezone *) NULL); }}//// Stop this timer from counting (add elapsed time to total accumulated so far)//void Timer::stop(){ if (_state & kRunning) { // Turn off timer _state &= ~kRunning; struct rusage ru; struct timeval tp; // Determine current "time" getrusage(RUSAGE_SELF, &ru); gettimeofday(&tp, (struct timezone *) NULL); // Find time elapsed since we last started running; rusagesub(ru, _ru_start); timevalsub(tp, _tp_start); // Add this elapsed time to the time accumulated so far rusageadd(_ru_sum, ru); timevaladd(_tp_sum, tp); }}// // Start the timer running again//void Timer::run(){ if (_state & kRunning) return; // Turn on timer _state |= kRunning; // Reset start of timer to current time getrusage(RUSAGE_SELF, &_ru_start); gettimeofday(&_tp_start, (struct timezone *) NULL);}ostream& operator<<(ostream& os, Timer &t){ struct rusage ru; struct timeval tp; if (t._state & Timer::kRunning) { // Determine current "time" getrusage(RUSAGE_SELF, &ru); gettimeofday(&tp, (struct timezone *) NULL); // Calculate how much time has passed since we last started running rusagesub(ru, t._ru_start); timevalsub(tp, t._tp_start); // Add to this the time accumulated so far by the timer rusageadd(ru, t._ru_sum); timevaladd(tp, t._tp_sum); } else { // We are not running. Just display the time accumulated ru = t._ru_sum; tp = t._tp_sum; } os << " user time = " << timevaltof(ru.ru_utime) << " seconds.\n"; os << " sys. time = " << timevaltof(ru.ru_stime) << " seconds.\n"; os << " clock time = " << timevaltof(tp) << " seconds.\n\n"; os << " ru_minflt = " << ru.ru_minflt << " (page reclaims)\n"; os << " ru_majflt = " << ru.ru_majflt << " (page faults)\n"; os << " ru_nswap = " << ru.ru_nswap << " (swaps)\n"; os << " ru_nvcsw = " << ru.ru_nvcsw << " (volunt. context switches)\n"; os << " ru_nivcsw = " << ru.ru_nivcsw << " (invol. context switches)\n\n"; return os;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -