Commit c40a515eee8a8e361f69b2bf5f3f59362413d196

Authored by Georg Hopp
1 parent c319edf5

Add a general purpose timer for different time granularities (seconds, milliseco…

…nds, microseconds and nanoseconds).
@@ -4,6 +4,7 @@ nobase_include_HEADERS = trbase.h \ @@ -4,6 +4,7 @@ nobase_include_HEADERS = trbase.h \
4 tr/interface.h \ 4 tr/interface.h \
5 tr/memory.h \ 5 tr/memory.h \
6 tr/logger.h \ 6 tr/logger.h \
  7 + tr/timer.h \
7 tr/print_trace.h \ 8 tr/print_trace.h \
8 tr/sized_data.h \ 9 tr/sized_data.h \
9 tr/tree_macros.h \ 10 tr/tree_macros.h \
  1 +/**
  2 + * \file
  3 + * A timer with second, millisec, microsec or nanosec granularity.
  4 + *
  5 + * \author Georg Hopp
  6 + *
  7 + * \copyright
  8 + * Copyright © 2014 Georg Hopp
  9 + *
  10 + * This program is free software: you can redistribute it and/or modify
  11 + * it under the terms of the GNU General Public License as published by
  12 + * the Free Software Foundation, either version 3 of the License, or
  13 + * (at your option) any later version.
  14 + *
  15 + * This program is distributed in the hope that it will be useful,
  16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18 + * GNU General Public License for more details.
  19 + *
  20 + * You should have received a copy of the GNU General Public License
  21 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22 + */
  23 +
  24 +#ifndef __TR_TIMER_H__
  25 +#define __TR_TIMER_H__
  26 +
  27 +#include "trbase.h"
  28 +
  29 +typedef enum {
  30 + TR_TBASE_NAN = 0,
  31 + TR_TBASE_MIC,
  32 + TR_TBASE_MIL,
  33 + TR_TBASE_SEC
  34 +} TR_eTimeoutBase;
  35 +
  36 +TR_CLASS(TR_Timer) {
  37 + int sec;
  38 + int nsec;
  39 + TR_eTimeoutBase base;
  40 + unsigned long timeout;
  41 +};
  42 +
  43 +TR_INSTANCE_INIT(TR_Timer);
  44 +TR_CLASSVARS_DECL(TR_Timer) {};
  45 +
  46 +void TR_timerSet(TR_Timer, TR_eTimeoutBase, unsigned long);
  47 +unsigned long TR_timerGet(TR_Timer, unsigned long *); // 2nd arg gets amount of
  48 + // missed since last check.
  49 +
  50 +#define TR_timerSetNan(timer, val) (TR_timerSet((timer), TR_TBASE_NAN, (val)))
  51 +#define TR_timerSetMic(timer, val) (TR_timerSet((timer), TR_TBASE_MIC, (val)))
  52 +#define TR_timerSetMil(timer, val) (TR_timerSet((timer), TR_TBASE_MIL, (val)))
  53 +#define TR_timerSetSec(timer, val) (TR_timerSet((timer), TR_TBASE_SEC, (val)))
  54 +
  55 +#endif // __TR_TIMER_H__
  56 +
  57 +// vim: set ts=4 sw=4:
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 #include "tr/memory.h" 5 #include "tr/memory.h"
6 #include "tr/class.h" 6 #include "tr/class.h"
7 #include "tr/logger.h" 7 #include "tr/logger.h"
  8 +#include "tr/timer.h"
8 #include "tr/print_trace.h" 9 #include "tr/print_trace.h"
9 #include "tr/sized_data.h" 10 #include "tr/sized_data.h"
10 #include "tr/interface.h" 11 #include "tr/interface.h"
@@ -6,6 +6,9 @@ AM_CFLAGS += -I../include/ @@ -6,6 +6,9 @@ AM_CFLAGS += -I../include/
6 TR_CLASS = memory.c \ 6 TR_CLASS = memory.c \
7 interface.c \ 7 interface.c \
8 logger.c \ 8 logger.c \
  9 + timer.c \
  10 + timer_set.c \
  11 + timer_get.c \
9 print_trace.c \ 12 print_trace.c \
10 stderr.c \ 13 stderr.c \
11 syslog.c \ 14 syslog.c \
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdarg.h>
  24 +
  25 +#include "trbase.h"
  26 +
  27 +static
  28 +int
  29 +timerCtor(void * _this, va_list * params)
  30 +{
  31 + TR_eTimeoutBase base = va_arg(*params, TR_eTimeoutBase);
  32 + unsigned long timeout = va_arg(*params, unsigned long);
  33 +
  34 + TR_timerSet((TR_Timer)_this, base, timeout);
  35 +
  36 + return 0;
  37 +}
  38 +
  39 +static void timerDtor(void * _this) {}
  40 +
  41 +TR_INIT_IFACE(TR_Class, timerCtor, timerDtor, NULL);
  42 +TR_CREATE_CLASS(TR_Timer, NULL, NULL, TR_IF(TR_Class));
  43 +
  44 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <time.h>
  24 +#include <limits.h>
  25 +
  26 +#include "trbase.h"
  27 +
  28 +unsigned long
  29 +TR_timerGet(TR_Timer this, unsigned long * missed)
  30 +{
  31 + struct timespec now;
  32 + unsigned long long elapsed;
  33 + unsigned long sec_factor;
  34 + long nan_factor;
  35 + unsigned long _missed;
  36 +
  37 + clock_gettime(CLOCK_REALTIME, &now);
  38 +
  39 + switch (this->base) {
  40 + case TR_TBASE_NAN:
  41 + sec_factor = 1000 * 1000 * 1000;
  42 + break;
  43 + case TR_TBASE_MIC:
  44 + sec_factor = 1000 * 1000;
  45 + break;
  46 + case TR_TBASE_MIL:
  47 + sec_factor = 1000;
  48 + break;
  49 + case TR_TBASE_SEC:
  50 + sec_factor = 1;
  51 + break;
  52 + }
  53 +
  54 + nan_factor = 1000 * 1000 * 1000 / sec_factor;
  55 + elapsed = (now.tv_sec - this->sec) * sec_factor
  56 + + (now.tv_nsec - this->nsec) / nan_factor;
  57 +
  58 + _missed = elapsed / this->timeout;
  59 + if (_missed) {
  60 + this->sec += _missed * this->timeout * sec_factor / nan_factor;
  61 + this->nsec += _missed * this->timeout * sec_factor % nan_factor;
  62 + *missed = _missed;
  63 + } else {
  64 + *missed = 0;
  65 + }
  66 +
  67 + return this->timeout - (elapsed % this->timeout);
  68 +}
  69 +
  70 +// vim: set ts=4 sw=4:
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <time.h>
  24 +
  25 +#include "trbase.h"
  26 +
  27 +void
  28 +TR_timerSet(TR_Timer this, TR_eTimeoutBase base, unsigned long timeout)
  29 +{
  30 + struct timespec set;
  31 + clock_gettime(CLOCK_REALTIME, &set);
  32 + this->sec = set.tv_sec;
  33 + this->nsec = set.tv_nsec;
  34 + this->base = base;
  35 + this->timeout = timeout;
  36 +}
  37 +
  38 +// vim: set ts=4 sw=4:
Please register or login to post a comment