aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/blogc/funcvars.c11
-rw-r--r--src/blogc/funcvars.h2
-rw-r--r--src/blogc/rusage.c54
-rw-r--r--src/blogc/rusage.h7
4 files changed, 31 insertions, 43 deletions
diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c
index 8524568..4fc12cf 100644
--- a/src/blogc/funcvars.c
+++ b/src/blogc/funcvars.c
@@ -21,8 +21,8 @@ static const struct func_map {
} funcs[] = {
#ifdef HAVE_RUSAGE
- {"BLOGC_RUSAGE_CPU_TIME", blogc_rusage_cpu_time},
- {"BLOGC_RUSAGE_MEMORY", blogc_rusage_memory},
+ {"BLOGC_RUSAGE_CPU_TIME", blogc_rusage_inject},
+ {"BLOGC_RUSAGE_MEMORY", blogc_rusage_inject},
#endif
{NULL, NULL},
@@ -39,11 +39,8 @@ blogc_funcvars_lookup(const char *name, bc_trie_t *global)
for (size_t i = 0; funcs[i].variable != NULL; i++) {
if (0 == strcmp(name, funcs[i].variable)) {
- char *val = funcs[i].func();
- if (val == NULL)
- return NULL;
- bc_trie_insert(global, name, bc_strdup(val));
- return val;
+ funcs[i].func(global);
+ return bc_strdup(bc_trie_lookup(global, name));
}
}
diff --git a/src/blogc/funcvars.h b/src/blogc/funcvars.h
index d46266f..f554f8d 100644
--- a/src/blogc/funcvars.h
+++ b/src/blogc/funcvars.h
@@ -12,7 +12,7 @@
#include <stdbool.h>
#include "../common/utils.h"
-typedef char* (*blogc_funcvars_func_t) (void);
+typedef void (*blogc_funcvars_func_t) (bc_trie_t*);
char* blogc_funcvars_lookup(const char *name, bc_trie_t *global);
diff --git a/src/blogc/rusage.c b/src/blogc/rusage.c
index 972ae4f..7c04db3 100644
--- a/src/blogc/rusage.c
+++ b/src/blogc/rusage.c
@@ -18,24 +18,28 @@
#include <sys/resource.h>
#endif /* HAVE_SYS_RESOURCE_H */
+#include <stdlib.h>
#include "../common/utils.h"
#include "rusage.h"
-// FIXME: optimize to use a single syscall for both cpu time and memory?
-
-long long
-blogc_rusage_get_cpu_time(void)
+blogc_rusage_t*
+blogc_rusage_get(void)
{
#ifndef HAVE_RUSAGE
- return 0;
+ return NULL;
#else
struct rusage usage;
if (0 != getrusage(RUSAGE_SELF, &usage))
- return 0;
- return (
+ return NULL;
+
+ blogc_rusage_t *rv = bc_malloc(sizeof(blogc_rusage_t));
+ rv->cpu_time = (
(usage.ru_utime.tv_sec * 1000000) + usage.ru_utime.tv_usec +
(usage.ru_stime.tv_sec * 1000000) + usage.ru_stime.tv_usec);
+ rv->memory = usage.ru_maxrss;
+
+ return rv;
#endif
}
@@ -56,27 +60,6 @@ blogc_rusage_format_cpu_time(long long time)
char*
-blogc_rusage_cpu_time(void)
-{
- return blogc_rusage_format_cpu_time(blogc_rusage_get_cpu_time());
-}
-
-
-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
-}
-
-
-char*
blogc_rusage_format_memory(long mem)
{
if (mem > 1048576)
@@ -87,8 +70,17 @@ blogc_rusage_format_memory(long mem)
}
-char*
-blogc_rusage_memory(void)
+void
+blogc_rusage_inject(bc_trie_t *global)
{
- return blogc_rusage_format_memory(blogc_rusage_get_memory());
+ blogc_rusage_t *usage = blogc_rusage_get();
+ if (usage == NULL)
+ return;
+
+ bc_trie_insert(global, "BLOGC_RUSAGE_CPU_TIME",
+ blogc_rusage_format_cpu_time(usage->cpu_time));
+ bc_trie_insert(global, "BLOGC_RUSAGE_MEMORY",
+ blogc_rusage_format_memory(usage->memory));
+
+ free(usage);
}
diff --git a/src/blogc/rusage.h b/src/blogc/rusage.h
index 4065552..3441630 100644
--- a/src/blogc/rusage.h
+++ b/src/blogc/rusage.h
@@ -17,19 +17,18 @@
#define HAVE_RUSAGE 1
#endif /* HAVE_SYS_RESOURCE_H */
+#include "../common/utils.h"
+
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
char* blogc_rusage_format_cpu_time(long long time);
char* blogc_rusage_format_memory(long mem);
-char* blogc_rusage_cpu_time(void);
-char* blogc_rusage_memory(void);
+void blogc_rusage_inject(bc_trie_t *global);
#endif /* ___RUSAGE_H */