aboutsummaryrefslogtreecommitdiff
path: root/src/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/timer.c')
-rw-r--r--src/timer.c36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/timer.c b/src/timer.c
index 268b096..864415c 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -23,7 +23,7 @@
struct timer_slot {
char *name;
- enum { STOPPED, RUNNING } state;
+ size_t refcnt;
double real;
double self_user; /* user time in sec */
double self_system; /* system time in sec */
@@ -85,15 +85,15 @@ get_thread_timer_table(void)
}
/* Lookup a timer by its name. If it does not exist, create it. */
-wydawca_timer_t
-timer_get(const char *name)
+struct timer_slot *
+timer_slot_lookup(const char *name)
{
struct timer_slot slot, *ret;
int install = 1;
- struct grecs_symtab *timer_table = get_thread_timer_table();
slot.name = (char *)name;
- ret = grecs_symtab_lookup_or_install(timer_table, &slot, &install);
+ ret = grecs_symtab_lookup_or_install(get_thread_timer_table(),
+ &slot, &install);
if (!ret)
grecs_alloc_die();
return ret;
@@ -102,12 +102,13 @@ timer_get(const char *name)
wydawca_timer_t
timer_start(const char *name)
{
- wydawca_timer_t t = timer_get(name);
+ wydawca_timer_t t = timer_slot_lookup(name);
- gettimeofday(&t->real_mark, NULL);
- getrusage(RUSAGE_SELF, &t->self_mark);
- getrusage(RUSAGE_CHILDREN, &t->children_mark);
- t->state = RUNNING;
+ if (t->refcnt++ == 0) {
+ gettimeofday(&t->real_mark, NULL);
+ getrusage(RUSAGE_SELF, &t->self_mark);
+ getrusage(RUSAGE_CHILDREN, &t->children_mark);
+ }
return t;
}
@@ -135,13 +136,20 @@ _timer_compute(wydawca_timer_t t)
}
wydawca_timer_t
+timer_get(const char *name)
+{
+ wydawca_timer_t t = timer_slot_lookup(name);
+ if (t->refcnt)
+ _timer_compute(t);
+ return t;
+}
+
+wydawca_timer_t
timer_stop(const char *name)
{
wydawca_timer_t t = timer_get(name);
- if (t->state == RUNNING) {
- _timer_compute(t);
- t->state = STOPPED;
- }
+ if (t->refcnt)
+ --t->refcnt;
return t;
}

Return to:

Send suggestions and report system problems to the System administrator.