From 8b074a4e58692c02bb57bec89350e6555f641b80 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sat, 20 Jun 2020 23:20:20 +0200 Subject: [PATCH] update to upcoming timer API of gint 2.1 --- README.md | 14 +++++++------- libprof.c | 17 ++++++++++------- libprof.h | 13 ++++++------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ca54c0a..3e05fae 100644 --- a/README.md +++ b/README.md @@ -47,15 +47,15 @@ For each function you want to time, libprof will create a counter. At the start of the program, you need to specify how many functions (libprof calls them *contexts*) you will be timing, so that libprof can allocate enough memory. -libprof also needs one of gint's timer to actually measure time; it must be one -of timers 0, 1 and 2, which are the only one precise enough to do this job. You -can use any timer which you are not already using for something else. +libprof also needs one of gint's timer to actually measure time; it selects one +of TMU0, TMU1 and TMU2 which are the only ones suited for this precise job. The +available timer with the smallest interrupt priority is selected. -These settings are specified with the `prof_init()` function. +This initialization happens in the `prof_init()` function. ```c -/* Initialize libprof for 13 contexts using timer 0 */ -prof_init(13, 0); +/* Initialize libprof for 13 contexts (automatically selects a timer) */ +prof_init(13); ``` You can then measure the execution time of a function by calling `prof_enter()` @@ -121,7 +121,7 @@ one each time. So for example here `PROFCTX_FUNCTION2` is equal to `1` and the number of contexts, which makes it simple to initialize the library: ```c -prof_init(PROFCTX_COUNT, 0); +prof_init(PROFCTX_COUNT); ``` Then you can use context names instead of numbers: diff --git a/libprof.c b/libprof.c index 4ab4ad9..552373a 100644 --- a/libprof.c +++ b/libprof.c @@ -17,16 +17,19 @@ uint32_t volatile *prof_tcnt = NULL; static int prof_timer; /* prof_init(): Initialize the profiler's data and timer */ -int prof_init(int n, int timer) +int prof_init(int context_count) { - if((unsigned)timer >= 3) return 1; + prof_rec = malloc(context_count * sizeof *prof_rec); + prof_elapsed = malloc(context_count * sizeof *prof_elapsed); - prof_rec = malloc(n * sizeof *prof_rec); - prof_elapsed = malloc(n * sizeof *prof_elapsed); + /* Get a TMU with the exact constant 0xffffffff */ + int timer = -1; + for(int t = 2; t >= 0 && timer == -1; t--) + { + timer = timer_setup(t, 0xffffffff, NULL); + } - int status = timer_setup(timer, 0xffffffff, timer_Po_4, NULL, NULL); - - if(!prof_rec || !prof_elapsed || status < 0) + if(!prof_rec || !prof_elapsed || timer == -1) { prof_quit(); return 1; diff --git a/libprof.h b/libprof.h index 804ea26..ec1a1e8 100644 --- a/libprof.h +++ b/libprof.h @@ -17,14 +17,13 @@ hold all the context IDs. Context IDs should be numbered from 0 to [n-1]; due to speed requirements array bounds are not checked so be careful. - Also starts a timer to count time. The timer ID must be set to 0, 1 or 2 as - the standard TMU is the most tweakable and precise. libprof automatically - selects an accurate timer configuration. + Also starts a timer to count time. libprof automatically selects a TMU and + tries to use TMU2 before TMU1 before TMU0 so that high-priority interrupts + remain available, and sets an accurate clock configuration. - @n Number of different contexts (functions) that will be measured - @timer Timer ID, see to select one - Returns non-zero if a setup error occurs. */ -int prof_init(int n, int timer); + @context_count Number of different contexts that will be measured + Returns non-zero if a setup error occurs or no timer is available. */ +int prof_init(int context_count); /* prof_quit(): Free the profiler's data and timer */ void prof_quit(void);