From ad03a650e68f42e8bfb0c48c7660adc60d0f6ddf Mon Sep 17 00:00:00 2001 From: Ben Gras Date: Fri, 9 Jan 2009 16:39:31 +0000 Subject: [PATCH] timing library from kernel into library --- lib/sysutil/Makefile.in | 1 + lib/sysutil/timing.c | 83 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 lib/sysutil/timing.c diff --git a/lib/sysutil/Makefile.in b/lib/sysutil/Makefile.in index 3cd2866ce..4f348804f 100644 --- a/lib/sysutil/Makefile.in +++ b/lib/sysutil/Makefile.in @@ -25,6 +25,7 @@ libsys_FILES=" \ ser_putc.c \ stacktrace.c \ sys_hz.c \ + timing.c \ profile_extern.c \ profile.c" diff --git a/lib/sysutil/timing.c b/lib/sysutil/timing.c new file mode 100644 index 000000000..4842dc087 --- /dev/null +++ b/lib/sysutil/timing.c @@ -0,0 +1,83 @@ + +#include +#include +#include +#include + +#define HIGHCOUNT 0 +#define LOWCOUNT 1 + +#define START 0 +#define END 1 + +void util_timer_start(util_timingdata_t *timingdata, char *name) +{ + unsigned long h, l; + int i; + + if(timingdata->names[0] == '\0') { + for(i = 0; i < sizeof(timingdata->names) && *name; i++) + timingdata->names[i] = *name++; + timingdata->names[sizeof(timingdata->names)-1] = '\0'; + } + + if (timingdata->starttimes[HIGHCOUNT]) { + panic(__FILE__, "restart timer?", NO_NUM); + return; + } + + read_tsc(&timingdata->starttimes[HIGHCOUNT], + &timingdata->starttimes[LOWCOUNT]); +} + +void util_timer_end(util_timingdata_t *timingdata) +{ + unsigned long h, l, d = 0, binsize; + int bin; + + read_tsc(&h, &l); + if (!timingdata->starttimes[HIGHCOUNT]) { + panic(__FILE__, "timer stopped but not started", NO_NUM); + return; + } + if (timingdata->starttimes[HIGHCOUNT] == h) { + d = (l - timingdata->starttimes[LOWCOUNT]); + } else if (timingdata->starttimes[HIGHCOUNT] == h-1 && + timingdata->starttimes[LOWCOUNT] > l) { + d = ((ULONG_MAX - timingdata->starttimes[LOWCOUNT]) + l); + } else { + timingdata->misses++; + return; + } + timingdata->starttimes[HIGHCOUNT] = 0; + if (!timingdata->lock_timings_range[START] || + d < timingdata->lock_timings_range[START] || + d > timingdata->lock_timings_range[END]) { + int t; + if (!timingdata->lock_timings_range[START] || + d < timingdata->lock_timings_range[START]) + timingdata->lock_timings_range[START] = d; + if (!timingdata->lock_timings_range[END] || + d > timingdata->lock_timings_range[END]) + timingdata->lock_timings_range[END] = d; + for(t = 0; t < TIMING_POINTS; t++) + timingdata->lock_timings[t] = 0; + timingdata->binsize = + (timingdata->lock_timings_range[END] - + timingdata->lock_timings_range[START])/(TIMING_POINTS+1); + if (timingdata->binsize < 1) + timingdata->binsize = 1; + timingdata->resets++; + } + bin = (d-timingdata->lock_timings_range[START]) / + timingdata->binsize; + if (bin < 0 || bin >= TIMING_POINTS) { + /* not serious, but can't happen, so shouldn't */ + panic(__FILE__, "bin out of range", bin); + } else { + timingdata->lock_timings[bin]++; + timingdata->measurements++; + } + + return; +} -- 2.44.0