summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorWill Newton <will.newton@linaro.org>2013-06-28 17:46:34 +0100
committerWill Newton <will.newton@linaro.org>2013-06-28 17:46:34 +0100
commit2fa326c108feb9ce587893c877efdefc16c98f7f (patch)
treee4256c513c0031fa3b3c379486f74a0373d58def /benchmarks
Initial commit - stub allocator, tests and reference code from glibc.
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/mallocbench.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/benchmarks/mallocbench.c b/benchmarks/mallocbench.c
new file mode 100644
index 0000000..57b5244
--- /dev/null
+++ b/benchmarks/mallocbench.c
@@ -0,0 +1,99 @@
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <config.h>
+
+/** Show basic usage */
+static void usage(const char *name)
+{
+ printf("%s %s: run a malloc benchmark.\n"
+ "usage: %s [-s alloc-size] [-l loop-count] [-r run-id]\n",
+ name, VERSION, name);
+
+ exit(-1);
+}
+
+static int parse_int_arg(const char *arg, const char *exe_name)
+{
+ long int ret;
+
+ errno = 0;
+ ret = strtol(arg, NULL, 0);
+
+ if (errno)
+ {
+ usage(exe_name);
+ }
+
+ return (int)ret;
+}
+
+int main(int argc, char **argv)
+{
+ size_t alloc_size = 512;
+ int loops = 10000000;
+ const char *run_id = "0";
+ int opt;
+
+ while ((opt = getopt(argc, argv, "s:l:r:h")) > 0)
+ {
+ switch (opt)
+ {
+ case 's':
+ alloc_size = parse_int_arg(optarg, argv[0]);
+ break;
+ case 'l':
+ loops = parse_int_arg(optarg, argv[0]);
+ break;
+ case 'r':
+ run_id = strdup(optarg);
+ break;
+ case 'h':
+ usage(argv[0]);
+ break;
+ default:
+ usage(argv[0]);
+ break;
+ }
+ }
+
+ struct timespec start, end;
+ int err = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
+ assert(err == 0);
+
+ for (int i = 0; i < loops; i++)
+ {
+ void *ptr = malloc(alloc_size);
+ free(ptr);
+ }
+
+ err = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
+ assert(err == 0);
+
+ /* Drop any leading path and pull the variant name out of the executable */
+ char *variant = strrchr(argv[0], '/');
+
+ if (variant == NULL)
+ {
+ variant = argv[0];
+ }
+
+ variant = strstr(variant, "try-");
+ assert(variant != NULL);
+
+ double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
+
+ /* Dump both machine and human readable versions */
+ printf("%s:%u:%u:%s:%.6f: took %.6f s for %u calls to malloc/free of %u bytes. ~%.3f us per call\n",
+ variant + 4, alloc_size, loops, run_id, elapsed,
+ elapsed, loops, alloc_size,
+ (double)(elapsed / loops) * 1e6);
+
+ return 0;
+}