summaryrefslogtreecommitdiff
path: root/src/examples/memslice-bench/memslice-bench.c
blob: a5c3cf6f06937e225178c7a98dcc0de7275e160a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define timersub(tvp, uvp, vvp)	\
	do \
	{ \
		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
		if ((vvp)->tv_usec < 0)	\
		{ \
			(vvp)->tv_sec--; \
			(vvp)->tv_usec += 1000000; \
		} \
	} while (0)

#include <libmowgli-2/mowgli.h>

mowgli_allocation_policy_t *memslice;
mowgli_allocation_policy_t *sysmalloc;

int
main(int argc, char *argv[])
{
	size_t i;

	size_t objects;
	size_t *obj_sizes;
	void **ptrs;
	struct timeval ts, te;

	mowgli_thread_set_policy(MOWGLI_THREAD_POLICY_DISABLED);

	objects = 128000;
	ptrs = mowgli_alloc_array(sizeof(void *), objects);
	obj_sizes = mowgli_alloc_array(sizeof(size_t), objects);

	memslice = mowgli_allocation_policy_lookup("memslice");
	sysmalloc = mowgli_allocation_policy_lookup("malloc");

	if (sysmalloc == NULL)
	{
		printf("Couldn't find a sysmalloc component which implements contract 'mowgli.core.allocation_policy' :(\n");
		return EXIT_FAILURE;
	}

	if (memslice == NULL)
	{
		printf("Couldn't find a memslice component which implements contract 'mowgli.core.allocation_policy' :(\n");
		return EXIT_FAILURE;
	}

	printf("Going to allocate %zu objects of random sizes < 256\n", objects);

	printf("Assigning sizes...\n");

	for (i = 0; i < objects; i++)
		obj_sizes[i] = rand() % 256;

	printf("Done!  Lets benchmark.\n");

	/* allocate using sysmalloc */
	gettimeofday(&ts, NULL);

	for (i = 0; i < objects; i++)
		ptrs[i] = mowgli_alloc_using_policy(sysmalloc, obj_sizes[i]);

	gettimeofday(&te, NULL);
	timersub(&te, &ts, &ts);

	printf("sysmalloc alloc time: %ld usec\n",
	       ts.tv_sec * 1000000L + ts.tv_usec);

	gettimeofday(&ts, NULL);

	for (i = 0; i < objects; i++)
		mowgli_free(ptrs[i]);

	gettimeofday(&te, NULL);
	timersub(&te, &ts, &ts);

	printf("sysmalloc free time: %ld usec\n",
	       ts.tv_sec * 1000000L + ts.tv_usec);

	/* allocate using memslice */
	gettimeofday(&ts, NULL);

	for (i = 0; i < objects; i++)
		ptrs[i] = mowgli_alloc_using_policy(memslice, obj_sizes[i]);

	gettimeofday(&te, NULL);
	timersub(&te, &ts, &ts);

	printf("memslice alloc time: %ld usec\n",
	       ts.tv_sec * 1000000L + ts.tv_usec);

	gettimeofday(&ts, NULL);

	for (i = 0; i < objects; i++)
		mowgli_free(ptrs[i]);

	gettimeofday(&te, NULL);
	timersub(&te, &ts, &ts);

	printf("memslice free time: %ld usec\n",
	       ts.tv_sec * 1000000L + ts.tv_usec);

	return EXIT_SUCCESS;
}