00001 /*!@file Util/FpsTimer.H Utility class for monitoring framerates and cpu usage ratios */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/FpsTimer.H $ 00035 // $Id: FpsTimer.H 8668 2007-08-07 18:58:15Z rjpeters $ 00036 // 00037 00038 #ifndef UTIL_FPSTIMER_H_DEFINED 00039 #define UTIL_FPSTIMER_H_DEFINED 00040 00041 #include "rutz/time.h" 00042 00043 #include <deque> 00044 00045 #include <sys/resource.h> 00046 #include <sys/time.h> 00047 00048 /// Utility class for monitoring framerates and cpu usage ratios 00049 class FpsTimer 00050 { 00051 public: 00052 FpsTimer(); 00053 00054 /// update frame count and times 00055 void nextFrame(); 00056 00057 rutz::time getElapsedTime() const; 00058 00059 int frameNumber() const; 00060 double getRecentFps() const; 00061 double getRecentCpuUsage() const; 00062 double getTotalFps() const; 00063 double getTotalCpuUsage() const; 00064 00065 struct State 00066 { 00067 rutz::time elapsed_time; 00068 unsigned int frame_number; 00069 double recent_fps; 00070 double recent_cpu_usage; 00071 double total_fps; 00072 double total_cpu_usage; 00073 }; 00074 00075 State getState() const; 00076 00077 private: 00078 unsigned int itsFrameNumber; 00079 00080 rutz::time itsWallStart; 00081 rutz::time itsUserStart; 00082 rutz::time itsSysStart; 00083 00084 std::deque<rutz::time> itsWallTimes; 00085 std::deque<rutz::time> itsUserTimes; 00086 std::deque<rutz::time> itsSysTimes; 00087 }; 00088 00089 // ###################################################################### 00090 inline FpsTimer::FpsTimer() 00091 : 00092 itsFrameNumber(0) 00093 { 00094 itsWallTimes.push_back(rutz::time::wall_clock_now()); 00095 00096 struct rusage ru; 00097 getrusage(RUSAGE_SELF, &ru); 00098 itsUserTimes.push_back(rutz::time(ru.ru_utime)); 00099 itsSysTimes.push_back(rutz::time(ru.ru_stime)); 00100 00101 itsWallStart = itsWallTimes.back(); 00102 itsUserStart = itsUserTimes.back(); 00103 itsSysStart = itsSysTimes.back(); 00104 } 00105 00106 // ###################################################################### 00107 void FpsTimer::nextFrame() 00108 { 00109 ++itsFrameNumber; 00110 00111 itsWallTimes.push_back(rutz::time::wall_clock_now()); 00112 00113 struct rusage ru; 00114 getrusage(RUSAGE_SELF, &ru); 00115 itsUserTimes.push_back(rutz::time(ru.ru_utime)); 00116 itsSysTimes.push_back(rutz::time(ru.ru_stime)); 00117 00118 if (itsWallTimes.size() > 31) 00119 { 00120 itsWallTimes.pop_front(); 00121 itsUserTimes.pop_front(); 00122 itsSysTimes.pop_front(); 00123 } 00124 } 00125 00126 // ###################################################################### 00127 rutz::time FpsTimer::getElapsedTime() const 00128 { 00129 return itsWallTimes.back() - itsWallStart; 00130 } 00131 00132 // ###################################################################### 00133 int FpsTimer::frameNumber() const 00134 { return itsFrameNumber; } 00135 00136 // ###################################################################### 00137 double FpsTimer::getRecentFps() const 00138 { 00139 return (itsWallTimes.size() - 1) 00140 / (itsWallTimes.back() - itsWallTimes.front()).sec(); 00141 } 00142 00143 // ###################################################################### 00144 double FpsTimer::getRecentCpuUsage() const 00145 { 00146 if (itsWallTimes.back() == itsWallTimes.front()) 00147 return 0.0; 00148 00149 return 00150 (itsUserTimes.back() + itsSysTimes.back() 00151 - itsUserTimes.front() - itsSysTimes.front()).sec() 00152 / 00153 (itsWallTimes.back() - itsWallTimes.front()).sec(); 00154 } 00155 00156 // ###################################################################### 00157 double FpsTimer::getTotalFps() const 00158 { 00159 if (itsFrameNumber < 1) 00160 return 0.0; 00161 00162 return itsFrameNumber 00163 / (itsWallTimes.back() - itsWallStart).sec(); 00164 } 00165 00166 // ###################################################################### 00167 double FpsTimer::getTotalCpuUsage() const 00168 { 00169 if (itsWallTimes.back() == itsWallStart) 00170 return 0.0; 00171 00172 return 00173 (itsUserTimes.back() + itsSysTimes.back() 00174 - itsUserStart - itsSysStart).sec() 00175 / 00176 (itsWallTimes.back() - itsWallStart).sec(); 00177 } 00178 00179 // ###################################################################### 00180 FpsTimer::State FpsTimer::getState() const 00181 { 00182 State result; 00183 result.elapsed_time = this->getElapsedTime(); 00184 result.frame_number = this->frameNumber(); 00185 result.recent_fps = this->getRecentFps(); 00186 result.recent_cpu_usage = this->getRecentCpuUsage(); 00187 result.total_fps = this->getTotalFps(); 00188 result.total_cpu_usage = this->getTotalCpuUsage(); 00189 return result; 00190 } 00191 00192 // ###################################################################### 00193 /* So things look consistent in everyone's emacs... */ 00194 /* Local Variables: */ 00195 /* mode: c++ */ 00196 /* indent-tabs-mode: nil */ 00197 /* End: */ 00198 00199 #endif // UTIL_FPSTIMER_H_DEFINED