]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
scripts: add a simple test utility to do some malloc() benchmarking/profiling
authorMartin Willi <martin@revosec.ch>
Fri, 5 Apr 2013 14:24:38 +0000 (16:24 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 6 May 2013 13:15:24 +0000 (15:15 +0200)
scripts/.gitignore
scripts/Makefile.am
scripts/malloc_speed.c [new file with mode: 0644]

index b97347fbd0131039b3ce809623b655b53ed93cc8..14579cbb476dffde751b86bf8c76700b04ddb8f1 100644 (file)
@@ -9,6 +9,7 @@ dh_speed
 pubkey_speed
 crypt_burn
 hash_burn
+malloc_speed
 tls_test
 fetch
 dnssec
index f7ecd9ef60f40587d287524af1157ab545b00a00..b9b3a70a84937e58eac3fae4af7e3a7e6985e82c 100644 (file)
@@ -4,7 +4,7 @@ AM_CFLAGS = \
 
 noinst_PROGRAMS = bin2array bin2sql id2sql key2keyid keyid2sql oid2der \
        thread_analysis dh_speed pubkey_speed crypt_burn hash_burn fetch \
-       dnssec
+       dnssec malloc_speed
 
 if USE_TLS
   noinst_PROGRAMS += tls_test
@@ -24,6 +24,7 @@ dh_speed_SOURCES = dh_speed.c
 pubkey_speed_SOURCES = pubkey_speed.c
 crypt_burn_SOURCES = crypt_burn.c
 hash_burn_SOURCES = hash_burn.c
+malloc_speed_SOURCES = malloc_speed.c
 fetch_SOURCES = fetch.c
 dnssec_SOURCES = dnssec.c
 id2sql_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
@@ -34,6 +35,7 @@ dh_speed_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la -lrt
 pubkey_speed_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la -lrt
 crypt_burn_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
 hash_burn_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
+malloc_speed_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
 fetch_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
 dnssec_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
 
diff --git a/scripts/malloc_speed.c b/scripts/malloc_speed.c
new file mode 100644 (file)
index 0000000..85d51a2
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec aG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include <stdio.h>
+#include <time.h>
+#include <library.h>
+#include <utils/debug.h>
+
+#ifdef HAVE_MALLINFO
+#include <malloc.h>
+#endif /* HAVE_MALLINFO */
+
+static void start_timing(struct timespec *start)
+{
+       clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
+}
+
+static double end_timing(struct timespec *start)
+{
+       struct timespec end;
+
+       clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
+       return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
+                       (end.tv_sec - start->tv_sec) * 1.0;
+}
+
+static void print_mallinfo()
+{
+#ifdef HAVE_MALLINFO
+       struct mallinfo mi = mallinfo();
+
+       printf("malloc: sbrk %d, mmap %d, used %d, free %d\n",
+                  mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
+#endif /* HAVE_MALLINFO */
+}
+
+#define ALLOCS 1024
+#define ROUNDS 2048
+
+int main(int argc, char *argv[])
+{
+       struct timespec timing;
+       int i, round;
+       void *m[ALLOCS];
+       /* a random set of allocations we test */
+       int sizes[16] = { 1, 13, 100, 1000, 16, 10000, 50, 17,
+                                         123, 32, 8, 64, 8096, 1024, 123, 9 };
+
+       library_init(NULL);
+       atexit(library_deinit);
+
+       print_mallinfo();
+
+       start_timing(&timing);
+
+       for (round = 0; round < ROUNDS; round++)
+       {
+               for (i = 0; i < ALLOCS; i++)
+               {
+                       m[i] = malloc(sizes[(round + i) % countof(sizes)]);
+               }
+               for (i = 0; i < ALLOCS; i++)
+               {
+                       free(m[i]);
+               }
+       }
+       printf("time for %d malloc/frees, repeating %d rounds: %.4fs\n",
+                  ALLOCS, ROUNDS, end_timing(&timing));
+
+       print_mallinfo();
+
+       return 0;
+}