-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
191 lines (173 loc) · 5.53 KB
/
main.c
File metadata and controls
191 lines (173 loc) · 5.53 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <limits.h>
#if defined(__linux__)
#include <time.h>
#include <stdint.h>
static inline uint64_t
ns(void)
{
struct timespec t;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
return t.tv_sec * 1000ULL * 1000ULL * 1000ULL + t.tv_nsec;
}
#elif defined(__APPLE__)
#include <mach/mach_time.h>
static inline uint64_t
ns(void)
{
return mach_absolute_time();
}
#endif
#define LAST_INSTRUCTION 100
typedef unsigned long long VALUE;
static const VALUE TWO_MB = 2 * 1024 * 1024;
#define OPT_CONTEXT_THREADING 1
#include "test.h"
#undef OPT_CONTEXT_THREADING
#define OPT_DIRECT_THREADING 1
#include "test.h"
#undef OPT_DIRECT_THREADING
static int*
sequence(int count, int limit)
{
int * result = (int *) malloc(sizeof(int) * count);
int i;
for (i = 0; i < count; i++) {
result[i] = (int) random() % limit;
}
result[count - 1] = LAST_INSTRUCTION;
return result;
}
static inline unsigned char *
call_instruction_body(unsigned char *code, void * body) /* size: 5 */
{
*code++ = 0xe8;
*(int *)code = (int)((unsigned char *)(body) - code - 4);
return code + 4;
}
static inline unsigned char *
jump_to_instruction_body(unsigned char *code, void * body) /* size: 5 */
{
*code++ = 0xe9;
*(int *)code = (int)((unsigned char *)(body) - code - 4);
return code + 4;
}
static void
get_instruction_target_range(VALUE *start_address, VALUE *end_address)
{
static VALUE first_instruction_address = 0;
static VALUE last_instruction_address = 0;
if (0 == first_instruction_address) {
int i;
void ** table = test_context_threading(NULL);
first_instruction_address = last_instruction_address = (VALUE) table[0];
for (i = 1; i <= LAST_INSTRUCTION; i++) {
if (first_instruction_address > (VALUE) table[0]) {
first_instruction_address = (VALUE) table[0];
}
if (last_instruction_address < (VALUE) table[0]) {
last_instruction_address = (VALUE) table[0];
}
}
}
*start_address = first_instruction_address;
*end_address = last_instruction_address;
}
static int
is_valid_context_threading_region(void *start, VALUE size)
{
VALUE s = (VALUE) start;
VALUE e = s + size;
VALUE first_instruction_address, last_instruction_address;
get_instruction_target_range(&first_instruction_address, &last_instruction_address);
if (s > last_instruction_address) {
return (e - first_instruction_address) < INT_MAX;
} else {
return (last_instruction_address - s) < INT_MAX;
}
}
static void *
alloc_context_threading_table(void)
{
VALUE start_region_range, end_region_range, region_address;
VALUE first_instruction_address, last_instruction_address;
get_instruction_target_range(&first_instruction_address, &last_instruction_address);
start_region_range = (last_instruction_address + INT_MIN + TWO_MB - 1) & ~(TWO_MB - 1);
end_region_range = (first_instruction_address + INT_MAX - TWO_MB) & ~(TWO_MB - 1);
if (start_region_range > last_instruction_address) {
start_region_range = 0;
}
if (end_region_range < first_instruction_address) {
end_region_range = (SIZE_MAX - TWO_MB) & ~(TWO_MB - 1);
}
printf("trying to find space between %llx and %llx\n", start_region_range, end_region_range);
for (region_address = start_region_range; region_address != end_region_range; region_address += TWO_MB) {
printf("trying %llx\n", region_address);
void *address = mmap((void *) region_address, TWO_MB, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (address == (void *) region_address || is_valid_context_threading_region(address, TWO_MB)) {
printf("using %p\n", address);
return address;
} else if (address != MAP_FAILED) {
printf("unmapping %p\n", address);
munmap(address, TWO_MB);
}
}
return NULL;
}
static void **
context_threading_table(int * seq, unsigned int count)
{
void ** table = test_context_threading(NULL);
void * ctt = alloc_context_threading_table();
void ** result = (void **) malloc(sizeof(void *));
unsigned char * code = (unsigned char *) ctt;
int i;
for (i = 0; i < count; i++) {
if (seq[i] == LAST_INSTRUCTION) {
code = jump_to_instruction_body(code, table[seq[i]]);
} else {
code = call_instruction_body(code, table[seq[i]]);
}
}
mprotect(ctt, TWO_MB, PROT_READ | PROT_EXEC);
*result = ctt;
return result;
}
static void **
direct_threading_table(int * seq, unsigned int count)
{
void ** table = test_direct_threading(NULL);
void ** result = (void **) malloc(sizeof(void *) * count);
int i;
for (i = 0; i < count; i++) {
result[i] = table[seq[i]];
}
return result;
}
static uint64_t
bench(void ** stream, void ** (*test)(void **))
{
uint64_t start, end;
int i;
test(stream);
start = ns();
for (i = 0; i < 1000000; i++) {
test(stream);
}
end = ns();
return end - start;
}
int main(int argc, char ** argv)
{
const unsigned int SEQUENCE_COUNT = 1000;
int * seq = sequence(SEQUENCE_COUNT, LAST_INSTRUCTION);
printf("testing direct threading\n");fflush(NULL);
uint64_t dt_time = bench(direct_threading_table(seq, SEQUENCE_COUNT), test_direct_threading);
printf("testing context threading\n");fflush(NULL);
uint64_t ct_time = bench(context_threading_table(seq, SEQUENCE_COUNT), test_context_threading);
printf("context threading: %lld ns\n", ct_time / 1000000ULL);
printf("direct threading: %lld ns\n", dt_time / 1000000ULL);
return 0;
}