diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-02-02 14:04:24 +0100 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-02-02 14:04:24 +0100 |
commit | 1b651788129fd65fc6d0dc44f8397494c8eaec49 (patch) | |
tree | c52804e936a425f825400af23de921efccafb650 /src | |
parent | d9298a35b6741a918e21e5d04e742f05b784e3ea (diff) | |
download | blogc-1b651788129fd65fc6d0dc44f8397494c8eaec49.tar.gz blogc-1b651788129fd65fc6d0dc44f8397494c8eaec49.tar.bz2 blogc-1b651788129fd65fc6d0dc44f8397494c8eaec49.zip |
blogc: do not add rusage vars if not supported
Diffstat (limited to 'src')
-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 |