Jpp  19.1.0
the software that should make you happy
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | Static Private Member Functions | Private 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::string & getTitle () const
 Get title. More...
 
void setTitle (const std::string &title)
 Set title. More...
 

Static Public Member Functions

static void enable ()
 Enable timers. More...
 
static void disable ()
 Disable timers. More...
 
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
 

Static Private Member Functions

static bool & get_status ()
 Get status. More...
 

Private Attributes

struct timeval tv
 
struct rusage usage
 

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 31 of file JTimer.hh.

Member Typedef Documentation

◆ ull

typedef unsigned long long JEEP::JTimer::ull

Definition at line 36 of file JTimer.hh.

Member Enumeration Documentation

◆ JStatus_t

Status of clock.

Enumerator
CLK_STOPPED 
CLK_RUNNING 

Definition at line 41 of file JTimer.hh.

41 { CLK_STOPPED = 0, CLK_RUNNING = 1 };
@ CLK_STOPPED
Definition: JTimer.hh:41
@ CLK_RUNNING
Definition: JTimer.hh:41

Constructor & Destructor Documentation

◆ JTimer() [1/2]

JEEP::JTimer::JTimer ( )
inline

Default constructor.

Definition at line 47 of file JTimer.hh.

47  :
48  JTitle(),
49  usec_wall(0ULL),
50  usec_ucpu(0ULL),
51  usec_scpu(0ULL),
52  counter (0ULL),
54  {}
unsigned long long usec_ucpu
Definition: JTimer.hh:239
unsigned long long usec_wall
Definition: JTimer.hh:238
unsigned long long counter
Definition: JTimer.hh:241
unsigned long long usec_scpu
Definition: JTimer.hh:240
JStatus_t status
Definition: JTimer.hh:244
JTitle()
Default constructor.
Definition: JTitle.hh:24

◆ JTimer() [2/2]

JEEP::JTimer::JTimer ( const JTitle title)
inline

Constructor.

Parameters
titletitle

Definition at line 62 of file JTimer.hh.

62  :
63  JTitle(title),
64  usec_wall(0ULL),
65  usec_ucpu(0ULL),
66  usec_scpu(0ULL),
67  counter (0ULL),
69  {}
std::string title
Definition: JTitle.hh:73

Member Function Documentation

◆ enable()

static void JEEP::JTimer::enable ( )
inlinestatic

Enable timers.

Definition at line 75 of file JTimer.hh.

76  {
77  get_status() = true;
78  }
static bool & get_status()
Get status.
Definition: JTimer.hh:255

◆ disable()

static void JEEP::JTimer::disable ( )
inlinestatic

Disable timers.

Definition at line 84 of file JTimer.hh.

85  {
86  get_status() = false;
87  }

◆ reset()

void JEEP::JTimer::reset ( )
inline

Reset timer.

Definition at line 93 of file JTimer.hh.

94  {
95  usec_wall = 0;
96  usec_ucpu = 0;
97  usec_scpu = 0;
98  counter = 0;
100  }

◆ start()

void JEEP::JTimer::start ( )
inline

Start timer.

Definition at line 106 of file JTimer.hh.

107  {
108  if (status == CLK_STOPPED && get_status()) {
109 
110  gettimeofday(&tv, NULL);
111  getrusage(RUSAGE_SELF, &usage);
112 
114  usec_ucpu -= timeval_to_usec(usage.ru_utime);
115  usec_scpu -= timeval_to_usec(usage.ru_stime);
116 
118 
119  ++counter;
120  }
121  }
static unsigned long long timeval_to_usec(const struct timeval &tv)
Convert timeval to micro-seconds.
Definition: JTimer.hh:160
struct timeval tv
Definition: JTimer.hh:247
struct rusage usage
Definition: JTimer.hh:248

◆ stop()

void JEEP::JTimer::stop ( )
inline

Stop timer.

Definition at line 127 of file JTimer.hh.

128  {
129  if (status == CLK_RUNNING && get_status()) {
130 
131  gettimeofday(&tv, NULL);
132  getrusage(RUSAGE_SELF, &usage);
133 
135  usec_ucpu += timeval_to_usec(usage.ru_utime);
136  usec_scpu += timeval_to_usec(usage.ru_stime);
137  }
138 
140  }

◆ timing_get_cpu_percentage()

unsigned int JEEP::JTimer::timing_get_cpu_percentage ( ) const
inline

Get CPU usage.

Returns
CPU usage [%]

Definition at line 148 of file JTimer.hh.

149  {
150  return (100ULL * (usec_ucpu + usec_scpu) / (usec_wall+1));
151  }

◆ timeval_to_usec()

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 160 of file JTimer.hh.

161  {
162  return (unsigned long long) tv.tv_sec * 1000000ULL + (unsigned long long) tv.tv_usec;
163  }

◆ print() [1/3]

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 172 of file JTimer.hh.

173  {
174  print(out, 1.0, scale);
175  }
void scale(vector< double > &v, double c)
scale vector content
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition: JTimer.hh:172

◆ print() [2/3]

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 185 of file JTimer.hh.

186  {
187  double factor = 1.0;
188 
189  if (normalise && counter != 0) {
190  factor = 1.0 / (double) counter;
191  }
192 
193  print(out, factor, scale);
194  }

◆ print() [3/3]

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 204 of file JTimer.hh.

205  {
206  using namespace std;
207  using namespace JPP;
208 
209  if (get_status()) {
210 
211  const double y = factor * 1e-3 * getValue(milli_t) / getValue(scale);
212  const char* u = getUnit(scale);
213 
214  out << getTitle() << endl;
215  out << FIXED(10,3) << usec_wall * y << " " << u << "s elapsed" << endl;
216  out << FIXED(10,3) << usec_ucpu * y << " " << u << "s user" << endl;
217  out << FIXED(10,3) << usec_scpu * y << " " << u << "s system" << endl;
218  out << setw( 3) << timing_get_cpu_percentage() << "%CPU" << endl;
219  }
220  }
unsigned int timing_get_cpu_percentage() const
Get CPU usage.
Definition: JTimer.hh:148
const std::string & getTitle() const
Get title.
Definition: JTitle.hh:55
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
@ milli_t
milli
Definition: JScale.hh:30
const char * getUnit(const int scale)
Get textual unit corresponding to scale.
Definition: JScale.hh:59
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
double u[N+1]
Definition: JPolint.hh:865
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448

◆ get_status()

static bool& JEEP::JTimer::get_status ( )
inlinestaticprivate

Get status.

Returns
status

Definition at line 255 of file JTimer.hh.

256  {
257  static bool status = true;
258 
259  return status;
260  }

◆ getTitle()

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  }

◆ setTitle()

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  }

Friends And Related Function Documentation

◆ operator<<

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

Print timer data.

Parameters
outoutput stream
timertimer
Returns
output stream

Definition at line 230 of file JTimer.hh.

231  {
232  timer.print(out);
233 
234  return out;
235  }

Member Data Documentation

◆ usec_wall

unsigned long long JEEP::JTimer::usec_wall

Definition at line 238 of file JTimer.hh.

◆ usec_ucpu

unsigned long long JEEP::JTimer::usec_ucpu

Definition at line 239 of file JTimer.hh.

◆ usec_scpu

unsigned long long JEEP::JTimer::usec_scpu

Definition at line 240 of file JTimer.hh.

◆ counter

unsigned long long JEEP::JTimer::counter

Definition at line 241 of file JTimer.hh.

◆ status

JStatus_t JEEP::JTimer::status
protected

Definition at line 244 of file JTimer.hh.

◆ tv

struct timeval JEEP::JTimer::tv
private

Definition at line 244 of file JTimer.hh.

◆ usage

struct rusage JEEP::JTimer::usage
private

Definition at line 244 of file JTimer.hh.

◆ title

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: