-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc-test.cpp
More file actions
160 lines (132 loc) · 3.48 KB
/
malloc-test.cpp
File metadata and controls
160 lines (132 loc) · 3.48 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* malloc-test
* cel - Thu Jan 7 15:49:16 EST 1999
*
* Benchmark libc's malloc, and check how well it
* can handle malloc requests from multiple threads.
*
* Syntax:
* malloc-test [ size [ iterations [ thread count ]]]
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <moses.h>
#define USECSPERSEC 1000000
#define pthread_attr_default NULL
#define MAX_THREADS 50
void * run_test(void*);
void * dummy(unsigned);
static unsigned size = 512;
static unsigned iteration_count = 1000000;
std::map<std::string, moses::Place> places = {
{"default", moses::Place("/mnt/moses/default", "default", moses::contention::LOW)},
{"threadlol", moses::Place("/mnt/moses/threadlol", "threadlol", moses::contention::LOW)}};
int main(int argc, char *argv[])
{
moses::Moses::Initialize(&places);
moses::PlaceGuard guard(&places.at("default"));
unsigned i;
unsigned thread_count = 1;
unsigned thread_nums[MAX_THREADS];
pthread_t thread[MAX_THREADS];
/*
* Parse our arguments
*/
switch (argc) {
case 4:
/* size, iteration count, and thread count were specified */
thread_count = atoi(argv[3]);
if (thread_count > MAX_THREADS) thread_count = MAX_THREADS;
case 3:
/* size and iteration count were specified; others default */
iteration_count = atoi(argv[2]);
case 2:
/* size was specified; others default */
size = atoi(argv[1]);
case 1:
/* use default values */
break;
default:
printf("Unrecognized arguments.\n");
exit(1);
}
/*
* Invoke the tests
*/
printf("Starting test...\n");
for (i=1; i<=thread_count; i++) {
thread_nums[i] = i;
if (pthread_create(&(thread[i]), pthread_attr_default, &run_test, &(thread_nums[i])))
printf("failed.\n");
}
/*
* Wait for tests to finish
*/
for (i=1; i<=thread_count; i++)
pthread_join(thread[i], NULL);
exit(0);
}
void* run_test(void *thread_num_p)
{
moses::PlaceGuard guard(&places.at("threadlol"));
register unsigned int i;
register unsigned request_size = size;
register unsigned total_iterations = iteration_count;
struct timeval start, end, null, elapsed, adjusted;
unsigned thread_num = *(unsigned*)thread_num_p;
/*
* Time a null loop. We'll subtract this from the final
* malloc loop results to get a more accurate value.
*/
gettimeofday(&start, NULL);
for (i = 0; i < total_iterations; i++) {
register void * volatile buf;
buf = dummy(i);
buf = dummy(i);
}
gettimeofday(&end, NULL);
null.tv_sec = end.tv_sec - start.tv_sec;
null.tv_usec = end.tv_usec - start.tv_usec;
if (null.tv_usec < 0) {
null.tv_sec--;
null.tv_usec += USECSPERSEC;
}
/*
* Run the real malloc test
*/
gettimeofday(&start, NULL);
for (i = 0; i < total_iterations; i++) {
register void * volatile buf;
buf = malloc(request_size);
free(buf);
}
gettimeofday(&end, NULL);
elapsed.tv_sec = end.tv_sec - start.tv_sec;
elapsed.tv_usec = end.tv_usec - start.tv_usec;
if (elapsed.tv_usec < 0) {
elapsed.tv_sec--;
elapsed.tv_usec += USECSPERSEC;
}
/*
* Adjust elapsed time by null loop time
*/
adjusted.tv_sec = elapsed.tv_sec - null.tv_sec;
adjusted.tv_usec = elapsed.tv_usec - null.tv_usec;
if (adjusted.tv_usec < 0) {
adjusted.tv_sec--;
adjusted.tv_usec += USECSPERSEC;
}
printf("Thread %d adjusted timing: %ld.%06ld seconds for %d requests"
" of %d bytes.\n", thread_num,
adjusted.tv_sec, adjusted.tv_usec, total_iterations,
request_size);
pthread_exit(NULL);
}
void * dummy(unsigned i)
{
return NULL;
}