-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_teststack.c
More file actions
173 lines (148 loc) · 3.97 KB
/
example_teststack.c
File metadata and controls
173 lines (148 loc) · 3.97 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
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <alloca.h>
#include <sys/time.h>
#include "compiler.h"
#include "co.h"
static int g_verbose = 0;
static int g_loop = 100;
static int g_us = 0;
#if defined(__i386__) || defined(__x86_64__)
unsigned long long counter(void)
{
register uint32_t lo, hi;
register unsigned long long o;
__asm__ __volatile__ (
"rdtscp" : "=a"(lo), "=d"(hi)::"%rcx"
);
o = hi;
o <<= 32;
return (o | lo);
}
#elif defined(__aarch64__)
unsigned long long counter(void)
{
return 0;
}
#endif
#define debug(...) \
if(g_verbose) printf(__VA_ARGS__);
#define C(exp) do{ \
unsigned long long c1; \
c1 = counter(); \
exp; \
debug(" %"PRIu64, counter()-c1); \
}while(0);
#define STEP 4096
void test_mmap_stack(void *d)
{
size_t i, j, size;
uint8_t *stack;
size = (size_t)d - (CO_STACK_BOTTOM - (size_t)&stack);
stack = alloca(size);
debug("coid %d, ", coid());
for(i=0; i<size; i+=STEP) { C(stack[i] = 5); }
debug("\n");
for(j = 0; j<g_loop; j++) {
schedule();
debug("coid %d, ", coid());
for(i=0; i<size; i+=STEP) { C(stack[i] = 5); }
debug("\n");
}
}
void test_copy_stack(void *d)
{
size_t i, j, size;
uint8_t *stack;
size = (size_t)d - (CO_STACK_BOTTOM - (size_t)&stack) - 128;
stack = alloca(size);
debug("coid %d, ", coid());
for(i=0; i<size; i+=STEP) { C(stack[i] = 5); }
debug("\n");
for(j = 0; j<g_loop; j++) {
schedule();
debug("coid %d, ", coid());
for(i=0; i<size; i+=STEP) { C(stack[i] = 5); }
debug("\n");
}
}
void main(int argc, char *argv[])
{
int i, n = 100, copy = 0;
int stack_size = COPY_STACK;
struct timeval tv, tv1;
unsigned long long c1, c2;
int opt;
while ((opt = getopt(argc, argv, "vn:l:uch")) != -1) {
switch (opt) {
case 'v':
g_verbose = 1;
break;
case 'n':
n = atoi(optarg);
break;
case 'l':
g_loop = atoi(optarg);
break;
case 'u':
g_us = 1;
break;
case 'c':
copy = 1;
break;
default: /* '?' */
case 'h':
fprintf(stderr, "Usage: %s [-v] [-n co_num] [-l loop] [-u] [-c] [stack_size]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
if(optind < argc) {
stack_size = atoi(argv[optind]);
COPY_STACK = stack_size = round_up(stack_size, getpagesize());
}
printf("COPY_STACK %d\n", COPY_STACK);
if(copy == 1) goto _copy;
//MMAP test
for(i=0; i<n; i++)
cocreate(AUTOSTACK, test_mmap_stack, (void*)(size_t)stack_size);
if(g_us) {
gettimeofday(&tv, NULL);
while(coloop());
gettimeofday(&tv1, NULL);
printf("mmap_stack cost %u us\n", (tv1.tv_sec - tv.tv_sec)*1000000 + tv1.tv_usec - tv.tv_usec);
}else{
c1 = counter();
while(coloop());
c2 = counter();
printf("mmap_stack cost %u cycle\n", c2 - c1);
}
goto _end;
return;
_copy:
//COPY test
for(i=0; i<n; i++)
cocreate(AUTOSTACK, test_copy_stack, (void*)(size_t)stack_size);
if(g_us) {
gettimeofday(&tv, NULL);
while(coloop());
gettimeofday(&tv1, NULL);
printf("copy_stack cost %u us\n", (tv1.tv_sec - tv.tv_sec)*1000000 + tv1.tv_usec - tv.tv_usec);
}else{
c1 = counter();
while(coloop());
c2 = counter();
printf("copy_stack cost %u cycle\n", c2 - c1);
}
extern struct co_info {
unsigned long max_stack_consumption;
unsigned long co_num;
} co_info;
_end:
printf("max_stack_consumption %u co_num %u\n", co_info.max_stack_consumption, co_info.co_num);
}