Main Page | Class Hierarchy | Class List | File List | Class Members

Resources.h

00001 // Resources.h -- Interface to a class used to measure resource usage.
00002 
00003 /*
00004  * Copyright (C) 1997, 1998, 2003 Tudor Hulubei <tudor@hulubei.net>.
00005  *
00006  * This library is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as
00008  * published by the Free Software Foundation; either version 2, or (at
00009  * your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA,
00019  * 02111-1307, USA.
00020  */
00021 
00022 /*
00023  * NOTE: This is an interal header file.
00024  * You should not attempt to use it directly.
00025  */
00026 
00027 // $Id: Resources_8h-source.html,v 1.1 2005/05/25 12:37:18 tudor Exp $
00028 
00029 #ifndef _CSP_KERNEL_Resources_H
00030 #define _CSP_KERNEL_Resources_H
00031 
00032 
00033 #ifndef _WIN32
00034 #ifdef TIME_WITH_SYS_TIME
00035 #include <sys/time.h>
00036 #include <time.h>
00037 #else
00038 #ifdef CSP_HAVE_SYS_TIME_H
00039 #include <sys/time.h>
00040 #else
00041 #include <time.h>
00042 #endif
00043 #endif
00044 #endif // !_WIN32
00045 
00046 #ifndef _WIN32
00047 #include <sys/resource.h>
00048 #endif // !_WIN32
00049 
00050 
00051 #ifdef _MSC_VER
00052 // Visual C++ doesn't provide rusage.  I copied it here from Linux.
00053 struct rusage
00054 {
00055     struct timeval ru_utime; /* user time used */
00056     struct timeval ru_stime; /* system time used */
00057     long ru_maxrss; /* maximum resident set size */
00058     long ru_ixrss; /* integral shared memory size */
00059     long ru_idrss; /* integral unshared data size */
00060     long ru_isrss; /* integral unshared stack size */
00061     long ru_minflt; /* page reclaims */
00062     long ru_majflt; /* page faults */
00063     long ru_nswap; /* swaps */
00064     long ru_inblock; /* block input operations */
00065     long ru_oublock; /* block output operations */
00066     long ru_msgsnd; /* messages sent */
00067     long ru_msgrcv; /* messages received */
00068     long ru_nsignals; /* signals received */
00069     long ru_nvcsw; /* voluntary context switches */
00070     long ru_nivcsw; /* involuntary context switches */
00071 };
00072 #endif // _MSC_VER
00073 
00074 
00075 CSP_NAMESPACE_BEGIN(csp);
00076 
00077 
00100 class CSP_API Resources
00101 {
00102   public:
00111     Resources(bool max = false);
00112 
00113     /*
00114      * Start recording resource usage.  All previously collected
00115      * resource usages are deleted.
00116      */
00117     void start();
00118 
00125     void record();
00126 
00132     void stop();
00133 
00140     void resume(bool countBreakResources = false);
00141 
00146     const timeval userTime() const;
00147 
00152     const timeval systemTime() const;
00153 
00158     const timeval totalTime() const;
00159 
00164     const timeval realTime() const;
00165 
00166     long memory() const;
00167     long text() const;
00168     long data() const;
00169     long stack() const;
00170     long minorPageFaults() const;
00171     long majorPageFaults() const;
00172     long swaps() const;
00173     long inputBlocks() const;
00174     long outputBlocks() const;
00175     long messagesSent() const;
00176     long messagesReceived() const;
00177     long signalsReceived() const;
00178     long voluntaryContextSwitches() const;
00179     long involuntaryContextSwitches() const;
00180 
00182     static void add(
00183         const timeval& time1,
00184         const timeval& time2,
00185         timeval& result);
00186 
00188     static void substract(
00189         const timeval& final,
00190         const timeval& initial,
00191         timeval& result);
00192 
00194     static int compare(
00195         const timeval& t1,
00196         const timeval& t2);
00197 
00204     wostream& print(wostream& wos) const;
00205 
00206     friend CSP_API wostream& operator<<(
00207         wostream& wos, const Resources& resources)
00208     {
00209         return resources.print(wos);
00210     }
00211 
00212   protected:
00214     enum state_t { Started, Stopped };
00215 
00217     void read(timeval& timeStamp, rusage& resourceUsage, bool initialReading);
00218 
00220     static void move(
00221         timeval& initial,
00222         timeval& final,
00223         timeval& newFinal);
00224 
00225     static wostream& printTime(wostream& wos, const timeval& tv);
00226 
00227   private:
00229     rusage m_initialResourceUsage;
00230 
00232     rusage m_finalResourceUsage;
00233 
00235     timeval m_initialTimeStamp;
00236 
00238     timeval m_finalTimeStamp;
00239 
00241     state_t m_state;
00242 };
00243 
00244 
00245 CSP_NAMESPACE_END(csp);
00246 
00247 
00248 #endif // _CSP_KERNEL_Resources_H
00249 
00250 // Local Variables:
00251 // mode: C++
00252 // End:

Generated on Wed May 25 12:21:15 2005 for csp.kdevelop by  doxygen 1.3.9.1