aboutsummaryrefslogtreecommitdiff
path: root/src/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/timer.c')
-rw-r--r--src/timer.c108
1 files changed, 36 insertions, 72 deletions
diff --git a/src/timer.c b/src/timer.c
index a0b54e0..80917b6 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -21,8 +21,7 @@
#include <string.h>
#include <ctype.h>
-struct timer_slot {
- char *name;
+typedef struct timer_slot {
size_t refcnt;
double real;
double self_user; /* user time in sec */
@@ -32,25 +31,7 @@ struct timer_slot {
struct timeval real_mark;
struct rusage self_mark;
struct rusage children_mark;
-};
-
-static unsigned
-hash_string_ci(const char *string, unsigned long n_buckets)
-{
- size_t value = 0;
- unsigned char ch;
-
- for (; (ch = *string); string++)
- value = (value * 31 + tolower(ch)) % n_buckets;
- return value;
-}
-
-static unsigned
-timer_hasher(void *ptr, unsigned long n_buckets)
-{
- struct timer_slot *tp = ptr;
- return hash_string_ci(tp->name, n_buckets);
-}
+} WY_TIMER;
static pthread_key_t key;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
@@ -58,7 +39,7 @@ static pthread_once_t key_once = PTHREAD_ONCE_INIT;
static void
timer_free(void *f)
{
- grecs_symtab_free(f);
+ free(f);
}
static void
@@ -67,42 +48,34 @@ make_key(void)
pthread_key_create(&key, timer_free);
}
-struct grecs_symtab *
+#define MAX_TIMERS (MY_TIMER_SPOOL_FIRST + spool_count)
+
+WY_TIMER *
get_thread_timer_table(void)
{
- struct grecs_symtab *timer_table;
+ WY_TIMER *timer_table;
pthread_once(&key_once, make_key);
if ((timer_table = pthread_getspecific(key)) == NULL) {
- timer_table = grecs_symtab_create(sizeof(struct timer_slot),
- timer_hasher,
- NULL, NULL, NULL, NULL);
- if (!timer_table)
- grecs_alloc_die();
+ timer_table = grecs_calloc(MAX_TIMERS,
+ sizeof(timer_table[0]));
pthread_setspecific(key, timer_table);
}
return timer_table;
}
-/* Lookup a timer by its name. If it does not exist, create it. */
-struct timer_slot *
-timer_slot_lookup(const char *name)
+wydawca_timer_t
+get_thread_timer(int n)
{
- struct timer_slot slot, *ret;
- int install = 1;
-
- slot.name = (char *)name;
- ret = grecs_symtab_lookup_or_install(get_thread_timer_table(),
- &slot, &install);
- if (!ret)
- grecs_alloc_die();
- return ret;
+ if (n < 0 || n >= MAX_TIMERS)
+ abort();
+ return get_thread_timer_table() + n;
}
wydawca_timer_t
-timer_start(const char *name)
+timer_start(int idx)
{
- wydawca_timer_t t = timer_slot_lookup(name);
+ wydawca_timer_t t = get_thread_timer(idx);
if (t->refcnt++ == 0) {
gettimeofday(&t->real_mark, NULL);
@@ -136,27 +109,27 @@ _timer_compute(wydawca_timer_t t)
}
wydawca_timer_t
-timer_get(const char *name)
+timer_get(int idx)
{
- wydawca_timer_t t = timer_slot_lookup(name);
+ wydawca_timer_t t = get_thread_timer(idx);
if (t->refcnt)
_timer_compute(t);
return t;
}
wydawca_timer_t
-timer_stop(const char *name)
+timer_stop(int idx)
{
- wydawca_timer_t t = timer_get(name);
+ wydawca_timer_t t = get_thread_timer(idx);
if (t->refcnt)
--t->refcnt;
return t;
}
wydawca_timer_t
-timer_reset(const char *name)
+timer_reset(int idx)
{
- wydawca_timer_t t = timer_get(name);
+ wydawca_timer_t t = get_thread_timer(idx);
t->real = 0.0;
t->self_user = 0.0;
t->self_system = 0.0;
@@ -216,7 +189,7 @@ timer_format_time(double t)
/* Cumulative statistic counters and timers */
WY_STAT_COUNTER *wydawca_global_stats;
-static struct grecs_symtab *wydawca_global_timer_table;
+WY_TIMER *wydawca_global_timer_table;
pthread_mutex_t global_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
/* wydawca_stats_export must be called from the main thread in order to
@@ -229,26 +202,6 @@ wydawca_stats_export(void)
wydawca_global_timer_table = get_thread_timer_table();
}
-static int
-timer_copy(void *sym, void *data)
-{
- struct timer_slot *ts = sym, *ret;
- int install;
-
- ret = grecs_symtab_lookup_or_install(wydawca_global_timer_table,
- ts, &install);
- if (!ret)
- grecs_alloc_die();
-
- ret->real += ts->real;
- ret->self_user += ts->self_user;
- ret->self_system += ts->self_system;
- ret->children_user += ts->children_user;
- ret->children_system += ts->children_system;
-
- return 0;
-}
-
/* When running in cron mode, this function is called by the triplet
processing thread when it has finished processing. The function
incorporates the thread-specific statistics into the cumulative
@@ -258,10 +211,21 @@ wydawca_stats_update(void)
{
int i;
WY_STAT_COUNTER *ctr = wy_get_stat_array();
- struct grecs_symtab *thread_timers = get_thread_timer_table();
+ WY_TIMER *thread_timers = get_thread_timer_table();
pthread_mutex_lock(&global_stats_mutex);
for (i = 0; i < WY_MAX_STAT; i++)
wydawca_global_stats[i] += ctr[i];
- grecs_symtab_foreach(thread_timers, timer_copy, NULL);
+ for (i = 0; i < MAX_TIMERS; i++) {
+ wydawca_global_timer_table[i].real
+ += thread_timers[i].real;
+ wydawca_global_timer_table[i].self_user
+ += thread_timers[i].self_user;
+ wydawca_global_timer_table[i].self_system
+ += thread_timers[i].self_system;
+ wydawca_global_timer_table[i].children_user
+ += thread_timers[i].children_user;
+ wydawca_global_timer_table[i].children_system
+ += thread_timers[i].children_system;
+ }
pthread_mutex_unlock(&global_stats_mutex);
}

Return to:

Send suggestions and report system problems to the System administrator.