forked from hnes/libaco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_aco_benchmark.c
295 lines (260 loc) Β· 8.14 KB
/
test_aco_benchmark.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2018 Sen Han <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define _GNU_SOURCE
#include "aco.h"
#include <alloca.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "aco_assert_override.h"
aco_cofuncp_t gl_co_fp;
#define PRINT_BUF_SZ 64
char gl_benchmark_print_str_buf[64];
void co_fp_alloca(){
size_t sz = (size_t)((uintptr_t)aco_get_arg());
uint8_t* ptr = NULL;
assert(sz > 0);
ptr = alloca(sz);
assertptr(ptr);
memset(ptr, 0, sz);
while(1){
aco_yield();
}
aco_exit();
}
void co_fp_stksz_128(){
int ip[28];
memset(ip, 1, sizeof(ip));
while(1){
aco_yield();
}
aco_exit();
}
void co_fp_stksz_64(){
int ip[12];
memset(ip, 1, sizeof(ip));
while(1){
aco_yield();
}
aco_exit();
}
void co_fp_stksz_40(){
int ip[8];
memset(ip, 1, sizeof(ip));
while(1){
aco_yield();
}
aco_exit();
}
void co_fp_stksz_24(){
int ip[4];
memset(ip, 1, sizeof(ip));
while(1){
aco_yield();
}
aco_exit();
}
void co_fp_stksz_8(){
while(1){
aco_yield();
}
aco_exit();
}
void co_fp0(){
while(1){
aco_yield();
}
aco_exit();
}
void benchmark_copystack(size_t co_amount,size_t stksz, size_t loopct){
struct timespec tstart={0,0}, tend={0,0};
int print_sz = 0;
double delta_t;
// create co
assert(co_amount > 0);
assertptr((void*)gl_co_fp);
aco_t* main_co = aco_create(NULL, NULL, 0, NULL, NULL);
aco_share_stack_t* sstk = aco_share_stack_new(0);
// NOTE: size_t_safe_mul
aco_t** coarray = (aco_t**) malloc(sizeof(void*) * co_amount);
assertptr(coarray);
memset(coarray, 0, sizeof(void*) * co_amount);
size_t ct = 0;
assert(0 == clock_gettime(CLOCK_MONOTONIC, &tstart));
while(ct < co_amount){
coarray[ct] = aco_create(
main_co, sstk, 0, gl_co_fp,
(void*)((uintptr_t)stksz)
);
ct++;
}
assert(0 == clock_gettime(CLOCK_MONOTONIC, &tend));
delta_t = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
//aco_create/init_save_stk_sz=64B 10000000 140.43 ns/op 7126683.67 op/s
print_sz = snprintf(
gl_benchmark_print_str_buf, PRINT_BUF_SZ,
"aco_create/init_save_stk_sz=64B"
);
assert(print_sz > 0 && print_sz < PRINT_BUF_SZ);
printf("%-50s %11zu %9.3f s %11.2f ns/op %13.2f op/s\n",
gl_benchmark_print_str_buf,
co_amount, delta_t,
(1.0e+9) / (co_amount / delta_t),
co_amount / delta_t);
fflush(stdout);
// warm-up
ct = 0;
while(ct < co_amount){
aco_resume(coarray[ct]);
ct++;
}
// copystack ctxsw
assert(0 == clock_gettime(CLOCK_MONOTONIC, &tstart));
size_t glct = 0;
while(glct < loopct){
ct = 0;
while(ct < co_amount){
aco_resume(coarray[ct]);
ct++;
glct++;
}
}
assert(0 == clock_gettime(CLOCK_MONOTONIC, &tend));
delta_t = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
//aco_resume/copy_stack_size=8B 20000000 36.23 ns/op 27614644.57 op/s
print_sz = snprintf(
gl_benchmark_print_str_buf, PRINT_BUF_SZ,
"aco_resume/co_amount=%zu/copy_stack_size=%zuB",
co_amount, coarray[0]->save_stack.max_cpsz
);
assert(print_sz > 0 && print_sz < PRINT_BUF_SZ);
printf("%-50s %11zu %9.3f s %11.2f ns/op %13.2f op/s\n",
gl_benchmark_print_str_buf, glct,
delta_t, (1.0e+9) / (glct / delta_t),
glct / delta_t);
if(co_amount == 1 && coarray[0]->save_stack.max_cpsz == 0){
printf("%-50s %11zu %9.3f s %11.2f ns/op %13.2f op/s\n",
" -> acosw", glct*2,
delta_t, (1.0e+9) / (glct*2 / delta_t),
glct*2 / delta_t);
}
fflush(stdout);
// co cleaning
assert(0 == clock_gettime(CLOCK_MONOTONIC, &tstart));
ct = 0;
while(ct < co_amount){
aco_destroy(coarray[ct]);
coarray[ct] = NULL;
ct++;
}
assert(0 == clock_gettime(CLOCK_MONOTONIC, &tend));
aco_share_stack_destroy(sstk);
sstk = NULL;
aco_destroy(main_co);
main_co = NULL;
free(coarray);
delta_t = ((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
//aco_destroy 20000000 21.22 ns/op 47616496.16 op/s
print_sz = snprintf(
gl_benchmark_print_str_buf, PRINT_BUF_SZ,
"aco_destroy"
);
assert(print_sz > 0 && print_sz < PRINT_BUF_SZ);
printf("%-50s %11zu %9.3f s %11.2f ns/op %13.2f op/s\n\n",
gl_benchmark_print_str_buf,
co_amount, delta_t,
(1.0e+9) / (co_amount / delta_t),
co_amount / delta_t);
fflush(stdout);
}
int main() {
#ifdef ACO_USE_VALGRIND
if(1){
printf("%s doesn't have valgrind test yet, "
"so bypass this test right now.\n",__FILE__
);
exit(0);
}
#endif
aco_thread_init(NULL);
printf("warm-up:\n");
gl_co_fp = co_fp_stksz_8;
benchmark_copystack(200*10000, 10, 20000000);
#ifdef __i386__
printf("+build:i386\n");
#elif __x86_64__
printf("+build:x86_64\n");
#endif
#ifdef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
printf("+build:-DACO_CONFIG_SHARE_FPU_MXCSR_ENV\n");
printf("+build:share fpu & mxcsr control words between coroutines\n");
#else
printf("+build:undefined ACO_CONFIG_SHARE_FPU_MXCSR_ENV\n");
printf("+build:each coroutine maintain each own fpu & mxcsr control words\n");
#endif
#ifdef ACO_USE_VALGRIND
printf("+build:-DACO_USE_VALGRIND\n");
printf("+build:valgrind memcheck friendly support enabled\n");
#else
printf("+build:undefined ACO_USE_VALGRIND\n");
printf("+build:without valgrind memcheck friendly support\n");
#endif
printf("\nsizeof(aco_t)=%zu:\n\n", sizeof(aco_t));
printf("\nstart-test:\n\n");
printf("%-50s %15s %15s %15s %15s\n\n",
"comment", "task_amount", "all_time_cost", "ns_per_op", "speed"
);
gl_co_fp = co_fp_stksz_8;
benchmark_copystack(1, 10, 20000000);
gl_co_fp = co_fp_stksz_8;
benchmark_copystack(1, 10, 20000000);
gl_co_fp = co_fp_stksz_8;
benchmark_copystack(200*10000, 10, 20000000);
gl_co_fp = co_fp_stksz_24;
benchmark_copystack(200*10000, 10, 20000000);
gl_co_fp = co_fp_stksz_40;
benchmark_copystack(200*10000, 10, 20000000);
gl_co_fp = co_fp_stksz_64;
benchmark_copystack(200*10000, 10, 20000000);
gl_co_fp = co_fp_stksz_128;
benchmark_copystack(200*10000, 10, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(200*10000, 150 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(200*10000, 158 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(200*10000, 166 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(200*10000, 256 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(200*10000, 512 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(200*10000, 512 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(100*10000, 1024 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(100*10000, 1024 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(10*10000, 1024 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(10*10000, 2048 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(10*10000, 4096 - 64, 20000000);
gl_co_fp = co_fp_alloca;
benchmark_copystack(10*10000, 8012 - 64, 20000000);
return 0;
}