Jpp  18.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | Friends | List of all members
JEEP::JTimer Class Reference

Auxiliary class for CPU timing and usage. More...

#include <JTimer.hh>

Inheritance diagram for JEEP::JTimer:
JLANG::JTitle

Public Types

enum  JStatus_t { CLK_STOPPED = 0, CLK_RUNNING = 1 }
 Status of clock. More...
 
typedef unsigned long long ull
 

Public Member Functions

 JTimer ()
 Default constructor. More...
 
 JTimer (const JTitle &title)
 Constructor. More...
 
void reset ()
 Reset timer. More...
 
void start ()
 Start timer. More...
 
void stop ()
 Stop timer. More...
 
unsigned int timing_get_cpu_percentage () const
 Get CPU usage. More...
 
void print (std::ostream &out, const JScale_t scale=milli_t) const
 Print timer data. More...
 
void print (std::ostream &out, const bool normalise, const JScale_t scale=milli_t) const
 Print timer data. More...
 
void print (std::ostream &out, const double factor, const JScale_t scale=milli_t) const
 Print timer data. More...
 
const std::stringgetTitle () const
 Get title. More...
 
void setTitle (const std::string &title)
 Set title. More...
 

Static Public Member Functions

static unsigned long long timeval_to_usec (const struct timeval &tv)
 Convert timeval to micro-seconds. More...
 

Public Attributes

unsigned long long usec_wall
 
unsigned long long usec_ucpu
 
unsigned long long usec_scpu
 
unsigned long long counter
 

Protected Attributes

JStatus_t status
 
std::string title
 

Friends

std::ostream & operator<< (std::ostream &out, const JTimer &timer)
 Print timer data. More...
 

Detailed Description

Auxiliary class for CPU timing and usage.

Definition at line 32 of file JTimer.hh.

Member Typedef Documentation

typedef unsigned long long JEEP::JTimer::ull

Definition at line 37 of file JTimer.hh.

Member Enumeration Documentation

Status of clock.

Enumerator
CLK_STOPPED 
CLK_RUNNING 

Definition at line 42 of file JTimer.hh.

Constructor & Destructor Documentation

JEEP::JTimer::JTimer ( )
inline

Default constructor.

Definition at line 48 of file JTimer.hh.

48  :
49  JTitle(),
50  usec_wall(0ULL),
51  usec_ucpu(0ULL),
52  usec_scpu(0ULL),
53  counter (0ULL),
55  {}
JStatus_t status
Definition: JTimer.hh:230
unsigned long long usec_ucpu
Definition: JTimer.hh:225
unsigned long long usec_scpu
Definition: JTimer.hh:226
JTitle()
Default constructor.
Definition: JTitle.hh:24
unsigned long long counter
Definition: JTimer.hh:227
unsigned long long usec_wall
Definition: JTimer.hh:224
JEEP::JTimer::JTimer ( const JTitle title)
inline

Constructor.

Parameters
titletitle

Definition at line 63 of file JTimer.hh.

63  :
64  JTitle(title),
65  usec_wall(0ULL),
66  usec_ucpu(0ULL),
67  usec_scpu(0ULL),
68  counter (0ULL),
70  {}
JStatus_t status
Definition: JTimer.hh:230
unsigned long long usec_ucpu
Definition: JTimer.hh:225
unsigned long long usec_scpu
Definition: JTimer.hh:226
JTitle()
Default constructor.
Definition: JTitle.hh:24
unsigned long long counter
Definition: JTimer.hh:227
std::string title
Definition: JTitle.hh:73
unsigned long long usec_wall
Definition: JTimer.hh:224

Member Function Documentation

void JEEP::JTimer::reset ( )
inline

Reset timer.

Definition at line 76 of file JTimer.hh.

77  {
78  usec_wall = 0;
79  usec_ucpu = 0;
80  usec_scpu = 0;
81  counter = 0;
83  }
JStatus_t status
Definition: JTimer.hh:230
unsigned long long usec_ucpu
Definition: JTimer.hh:225
unsigned long long usec_scpu
Definition: JTimer.hh:226
unsigned long long counter
Definition: JTimer.hh:227
unsigned long long usec_wall
Definition: JTimer.hh:224
void JEEP::JTimer::start ( )
inline

Start timer.

Definition at line 89 of file JTimer.hh.

90  {
91  if (status == CLK_STOPPED) {
92 
93  struct timeval tv;
94  struct rusage usage;
95 
96  gettimeofday(&tv, NULL);
97  getrusage(RUSAGE_SELF, &usage);
98 
100  usec_ucpu -= timeval_to_usec(usage.ru_utime);
101  usec_scpu -= timeval_to_usec(usage.ru_stime);
102 
104 
105  ++counter;
106  }
107  }
JStatus_t status
Definition: JTimer.hh:230
unsigned long long usec_ucpu
Definition: JTimer.hh:225
unsigned long long usec_scpu
Definition: JTimer.hh:226
unsigned long long counter
Definition: JTimer.hh:227
* usage
static unsigned long long timeval_to_usec(const struct timeval &tv)
Convert timeval to micro-seconds.
Definition: JTimer.hh:149
unsigned long long usec_wall
Definition: JTimer.hh:224
void JEEP::JTimer::stop ( )
inline

Stop timer.

Definition at line 113 of file JTimer.hh.

114  {
115  if (status == CLK_RUNNING) {
116 
117  struct timeval tv;
118  struct rusage usage;
119 
120  gettimeofday(&tv, NULL);
121  getrusage(RUSAGE_SELF, &usage);
122 
123  usec_wall += timeval_to_usec(tv);
124  usec_ucpu += timeval_to_usec(usage.ru_utime);
125  usec_scpu += timeval_to_usec(usage.ru_stime);
126  }
127 
129  }
JStatus_t status
Definition: JTimer.hh:230
unsigned long long usec_ucpu
Definition: JTimer.hh:225
unsigned long long usec_scpu
Definition: JTimer.hh:226
* usage
static unsigned long long timeval_to_usec(const struct timeval &tv)
Convert timeval to micro-seconds.
Definition: JTimer.hh:149
unsigned long long usec_wall
Definition: JTimer.hh:224
unsigned int JEEP::JTimer::timing_get_cpu_percentage ( ) const
inline

Get CPU usage.

Returns
CPU usage [%]

Definition at line 137 of file JTimer.hh.

138  {
139  return (100ULL * (usec_ucpu + usec_scpu) / (usec_wall+1));
140  }
unsigned long long usec_ucpu
Definition: JTimer.hh:225
unsigned long long usec_scpu
Definition: JTimer.hh:226
unsigned long long usec_wall
Definition: JTimer.hh:224
static unsigned long long JEEP::JTimer::timeval_to_usec ( const struct timeval &  tv)
inlinestatic

Convert timeval to micro-seconds.

Parameters
tvtimeval
Returns
time [us]

Definition at line 149 of file JTimer.hh.

150  {
151  return (unsigned long long) tv.tv_sec * 1000000ULL + (unsigned long long) tv.tv_usec;
152  }
void JEEP::JTimer::print ( std::ostream &  out,
const JScale_t  scale = milli_t 
) const
inline

Print timer data.

Parameters
outoutput stream
scalescale

Definition at line 161 of file JTimer.hh.

162  {
163  print(out, 1.0, scale);
164  }
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:161
void scale(vector< double > &v, double c)
scale vector content
void JEEP::JTimer::print ( std::ostream &  out,
const bool  normalise,
const JScale_t  scale = milli_t 
) const
inline

Print timer data.

Parameters
outoutput stream
normalisenormalise to number of starts
scalescale

Definition at line 174 of file JTimer.hh.

175  {
176  double factor = 1.0;
177 
178  if (normalise && counter != 0) {
179  factor = 1.0 / (double) counter;
180  }
181 
182  print(out, factor, scale);
183  }
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:161
unsigned long long counter
Definition: JTimer.hh:227
void scale(vector< double > &v, double c)
scale vector content
void JEEP::JTimer::print ( std::ostream &  out,
const double  factor,
const JScale_t  scale = milli_t 
) const
inline

Print timer data.

Parameters
outoutput stream
factormultiplication factor
scalescale

Definition at line 193 of file JTimer.hh.

194  {
195  using namespace std;
196 
197  const double y = factor * 1e-3 * getValue(milli_t) / getValue(scale);
198  const char* u = getUnit(scale);
199 
200  out << getTitle() << endl;
201  out << FIXED(10,3) << usec_wall * y << " " << u << "s elapsed" << endl;
202  out << FIXED(10,3) << usec_ucpu * y << " " << u << "s user" << endl;
203  out << FIXED(10,3) << usec_scpu * y << " " << u << "s system" << endl;
204  out << setw( 3) << timing_get_cpu_percentage() << "%CPU" << endl;
205  }
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
unsigned int timing_get_cpu_percentage() const
Get CPU usage.
Definition: JTimer.hh:137
unsigned long long usec_ucpu
Definition: JTimer.hh:225
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
const std::string & getTitle() const
Get title.
Definition: JTitle.hh:55
unsigned long long usec_scpu
Definition: JTimer.hh:226
milli
Definition: JScale.hh:30
const char * getUnit(const int scale)
Get textual unit corresponding to scale.
Definition: JScale.hh:59
double u[N+1]
Definition: JPolint.hh:865
unsigned long long usec_wall
Definition: JTimer.hh:224
void scale(vector< double > &v, double c)
scale vector content
const std::string& JLANG::JTitle::getTitle ( ) const
inlineinherited

Get title.

Returns
title

Definition at line 55 of file JTitle.hh.

56  {
57  return this->title;
58  }
std::string title
Definition: JTitle.hh:73
void JLANG::JTitle::setTitle ( const std::string title)
inlineinherited

Set title.

Parameters
titletitle

Definition at line 66 of file JTitle.hh.

67  {
68  this->title = title;
69  }
std::string title
Definition: JTitle.hh:73

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const JTimer timer 
)
friend

Print timer data.

Parameters
outoutput stream
timertimer
Returns
output stream

Definition at line 215 of file JTimer.hh.

216  {
217  timer.print(out);
218 
219  return out;
220  }
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:161

Member Data Documentation

unsigned long long JEEP::JTimer::usec_wall

Definition at line 224 of file JTimer.hh.

unsigned long long JEEP::JTimer::usec_ucpu

Definition at line 225 of file JTimer.hh.

unsigned long long JEEP::JTimer::usec_scpu

Definition at line 226 of file JTimer.hh.

unsigned long long JEEP::JTimer::counter

Definition at line 227 of file JTimer.hh.

JStatus_t JEEP::JTimer::status
protected

Definition at line 230 of file JTimer.hh.

std::string JLANG::JTitle::title
protectedinherited

Definition at line 73 of file JTitle.hh.


The documentation for this class was generated from the following file: