This HTML automatically generated with rman for NEMO
Table of Contents

Name

init_timers, stamp_timers, diff_timers - routines to time your code

Synopsis


#include <timers.h>void init_timers(int maxtimers);void init_timers2(int maxtimers,
int mode);void stamp_timers(int slot);long long diff_timers(int slot1,
int slot2);double diff_timers2(int slot1, int slot2);

Description

init_timers is used to allocate a set of maxtimers slots to hold timer information. stamp_timers is used remember the timer info in a slot. The difference between slot2 and slot1 (slot2>slot1 is normally assumed) is returned by diff_timers.

init_timers2 (mode=1) was introduced to also use cputime(3NEMO) , at the slight cost of more ticks. diff_timers2 will report the difference in CPU seconds (not minutes!).

Timers use a low level assembly routine to directly accces the TSC (Time Stamp Counter), and have the usual flaws on multi proccesing cores and multi-tasking operating systems how to translate this to a CPU usage. For long term stability routines such as cputime(3NEMO) should be used,for short fine grained understanding, these routines could be useful.

Example


    init_timers(n+1);
    for (i=0; i<n; i++)
    stamp_timers(i);
    stamp_timers(n);
    for (i=0; i<n; i++)
    printf("Method-1: %Ld0,diff_timers(i,i+1));
    stamp_timers(0);
    for (i=0; i<n; i++) {
    stamp_timers(i+1);
    printf("Method-2: %Ld0,diff_timers(i,i+1));
    }

Caveats

Currently this function is only implemented on Intel hardware, where it uses the Time Stamp Counter. It seems to work on AMD as well. For other processors 0 is returned.

long long is assumed to hold 64 bits, where unsigned is assumed 32 bit.

See Also

cputime(3NEMO) , clock(2) , time(2)

Author

Peter Teuben

Files


~/src/kernel/misc      timers.c

History


23-apr-04    created        PJT
25-feb-2024    added the cputime option    PJT


Table of Contents