1: /*
2: * Copyright (C) 2013 Martin Willi
3: * Copyright (C) 2013 revosec aG
4: *
5: * This program is free software; you can redistribute it and/or modify it
6: * under the terms of the GNU General Public License as published by the
7: * Free Software Foundation; either version 2 of the License, or (at your
8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9: *
10: * This program is distributed in the hope that it will be useful, but
11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13: * for more details.
14: */
15:
16: #include <stdio.h>
17: #include <time.h>
18: #include <library.h>
19: #include <utils/debug.h>
20:
21: #ifdef HAVE_MALLINFO
22: #include <malloc.h>
23: #endif /* HAVE_MALLINFO */
24:
25: static void start_timing(struct timespec *start)
26: {
27: clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
28: }
29:
30: static double end_timing(struct timespec *start)
31: {
32: struct timespec end;
33:
34: clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
35: return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
36: (end.tv_sec - start->tv_sec) * 1.0;
37: }
38:
39: static void print_mallinfo()
40: {
41: #ifdef HAVE_MALLINFO
42: struct mallinfo mi = mallinfo();
43:
44: printf("malloc: sbrk %d, mmap %d, used %d, free %d\n",
45: mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
46: #endif /* HAVE_MALLINFO */
47: }
48:
49: #define ALLOCS 1024
50: #define ROUNDS 2048
51:
52: int main(int argc, char *argv[])
53: {
54: struct timespec timing;
55: int i, round;
56: void *m[ALLOCS];
57: /* a random set of allocations we test */
58: int sizes[16] = { 1, 13, 100, 1000, 16, 10000, 50, 17,
59: 123, 32, 8, 64, 8096, 1024, 123, 9 };
60:
61: library_init(NULL, "malloc_speed");
62: atexit(library_deinit);
63:
64: print_mallinfo();
65:
66: start_timing(&timing);
67:
68: for (round = 0; round < ROUNDS; round++)
69: {
70: for (i = 0; i < ALLOCS; i++)
71: {
72: m[i] = malloc(sizes[(round + i) % countof(sizes)]);
73: }
74: for (i = 0; i < ALLOCS; i++)
75: {
76: free(m[i]);
77: }
78: }
79: printf("time for %d malloc/frees, repeating %d rounds: %.4fs\n",
80: ALLOCS, ROUNDS, end_timing(&timing));
81:
82: print_mallinfo();
83:
84: return 0;
85: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>