diff options
-rw-r--r-- | src/blogc/funcvars.c | 4 | ||||
-rw-r--r-- | src/blogc/rusage.c | 9 | ||||
-rw-r--r-- | src/blogc/rusage.h | 10 |
3 files changed, 22 insertions, 1 deletions
diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c index a3af274..8524568 100644 --- a/src/blogc/funcvars.c +++ b/src/blogc/funcvars.c @@ -19,8 +19,12 @@ static const struct func_map { const char *variable; const blogc_funcvars_func_t func; } funcs[] = { + +#ifdef HAVE_RUSAGE {"BLOGC_RUSAGE_CPU_TIME", blogc_rusage_cpu_time}, {"BLOGC_RUSAGE_MEMORY", blogc_rusage_memory}, +#endif + {NULL, NULL}, }; diff --git a/src/blogc/rusage.c b/src/blogc/rusage.c index b04938e..972ae4f 100644 --- a/src/blogc/rusage.c +++ b/src/blogc/rusage.c @@ -24,16 +24,19 @@ // FIXME: optimize to use a single syscall for both cpu time and memory? -#include <stdio.h> long long blogc_rusage_get_cpu_time(void) { +#ifndef HAVE_RUSAGE + return 0; +#else struct rusage usage; if (0 != getrusage(RUSAGE_SELF, &usage)) return 0; return ( (usage.ru_utime.tv_sec * 1000000) + usage.ru_utime.tv_usec + (usage.ru_stime.tv_sec * 1000000) + usage.ru_stime.tv_usec); +#endif } @@ -62,10 +65,14 @@ blogc_rusage_cpu_time(void) long blogc_rusage_get_memory(void) { +#ifndef HAVE_RUSAGE + return 0; +#else struct rusage usage; if (0 != getrusage(RUSAGE_SELF, &usage)) return 0; return usage.ru_maxrss; +#endif } diff --git a/src/blogc/rusage.h b/src/blogc/rusage.h index cfdccbe..d94aebd 100644 --- a/src/blogc/rusage.h +++ b/src/blogc/rusage.h @@ -9,6 +9,16 @@ #ifndef ___RUSAGE_H #define ___RUSAGE_H +#ifdef HAVE_SYS_RESOURCE_H +#define HAVE_RUSAGE +#endif + +typedef struct { + long long cpu_time; // in microseconds + long memory; // in kilobytes +} blogc_rusage_t; + +blogc_rusage_t* blogc_rusage_get(void); long long blogc_rusage_get_cpu_time(void); // in microseconds long blogc_rusage_get_memory(void); // in kilobytes |