common/cputimer.h

///////////////////////////////////////////////////////////////////////////////
// Filename: cputimer.h
///////////////////////////////////////////////////////////////////////////////
// Purpose: declares a class "cputimer" that measures the cpu time
//          (user+system, without children)
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/06 01:08:26 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


#ifndef __CPUTIMER_H__
#define __CPUTIMER_H__

// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <stdlib.h>
#include <iostream.h>
#include <sys/times.h>



// Local headers ///////////////////////////////////////////// Local headers //

#include "../common.h"



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //

/*
 * The class "cputimer" is useful for measuring the used cpu time of a process.
 * It uses the UNIX function times() to get the needed information
 * The measured time is the sum of used user and system time 
 */

class cputimer
{

public:

    // constructor, initialize everything
    cputimer(void);

    // destructor, nothing to do
    ~cputimer(void){}

    // return the number of measured seconds or -1 for an error
    long secs(void);

    // return the number of measured microseconds or -1 for an error
    long usecs(void);

    // start measurement
    bool start(void);

    // end measurement 
    bool end(void);

private:
    bool gottime;           // do we have a measured time we can return?
    bool started;           // is the timer started?
    tms tstart;             // the start time
    tms tresult;            // the result time

    long ticks_per_sec;     // number of ticks per second, derived with sysconf
};