-
Notifications
You must be signed in to change notification settings - Fork 14
/
robsize.cc
656 lines (580 loc) · 27.6 KB
/
robsize.cc
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
Henry Wong <[email protected]>
http://blog.stuffedcow.net/2013/05/measuring-rob-capacity/
2014-10-14
*/
#include <assert.h>
#include <getopt.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define ADD_BYTE(val) do{ibuf[pbuf] = (val); pbuf++;} while(0)
#define ADD_WORD(val) do{*(unsigned short*)(&ibuf[pbuf]) = (val); pbuf+=2;} while(0)
#define ADD_DWORD(val) do{*(unsigned int*)(&ibuf[pbuf]) = (val); pbuf+=4;} while(0)
// constant configuration
static int its = 8192;
/* maximum number of filler instructions supported */
const int MAX_ICOUNT = 400;
/* we repeat the load/payload pattern "unroll" times */
static const int unroll = 17;
/* stack space created for some tests which read/write the stack */
const int STACK_SPACE = MAX_ICOUNT * unroll * 2 + 100; // 100 arbitrary magic number
// runtime configuration
static bool print_ibuf;
static bool plot_mode; // make csv output, extraneous output to stdout
static bool lfence_mode;
static int start_icount = 16;
static int stop_icount = 256;
static bool explicit_start;
enum FLAGS {
// doesn't need compensation for the load op, i.e., it uses different
// resources than a load to a GP register
NO_COMP = 1 << 0
};
struct test_info {
int flags;
const char *desc; // description
};
const test_info tests[] = {
{ 0, "parallel GP adds" }, // add (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
{ 0, "single-byte NOPs" }, // nop
{ 0, "GP same-reg mov" }, // mov (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
{ 0, "parallel cmp regN, regN" }, // cmp (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
{ 0, "two-byte NOPs" }, // two-byte nop 66 90
{ 0, "zeroing GP xor" }, // xor (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
{ 0, "parallel xor regN, regN+1" }, // xor (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
{ 0, "GP diff-ref mov regN, regN+1" }, // mov (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
{ NO_COMP, "movaps xmm, xmm" }, // movaps xmm, xmm
{ NO_COMP, "movdqa xmm, xmm" }, // movdqa xmm, xmm SSE2
{ NO_COMP, "zeroing xorps xmm, xmm" }, // 10 - xorps xmm, xmm
{ NO_COMP, "xorps xmmN, xmmN+1" }, // xorps xmm, xmm+1
{ NO_COMP, "movdqa xmm, xmm SSE (non-VEX)" }, // movdqa xmm, xmm SSE2
{ NO_COMP, "movdqa xmm, xmm AVX (VEX)" }, // movdqa xmm, xmm AVX
{ NO_COMP, "movdqa ymm, ymm AVX (VEX)" }, // movdqa ymm, ymm AVX
{ NO_COMP, "movdqa xmm, xmm+1 SSE (non-VEX)" }, // movdqa xmm, xmm+1 SSE2
{ NO_COMP, "movdqa xmm, xmm+1 AVX (VEX)" }, // movdqa xmm, xmm+1 AVX
{ NO_COMP, "movdqa ymm, ymm+1 AVX (VEX)" }, // movdqa ymm, ymm+1 AVX
{ NO_COMP, "vxorps ymm, ymm, ymm AVX" }, // vxorps ymm, ymm, ymm AVX
{ NO_COMP, "vxorps ymm, ymm, ymm+1 AVX" }, // vxorps ymm, ymm, ymm+1 AVX
{ NO_COMP, "alternating GP + SIMD, add & xorps" }, // 20
{ 0, "alternating GP + SIMD, add & vxorps" }, // TODO check difference vs 21
{ 0, "xor regN, regN+1" }, // xor (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
{ 0, "sub regN, N" }, // sub reg, val
{ 0, "add regN, regN" }, // add64 (rbx, rbp, rsi, rdi), (rbx, rbp, rsi, rdi)
{ 0, "mov regN, regN+1" }, // mov64 (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
{ NO_COMP, "vpxord zmmN, zmmN, zmmN+1" },
{ NO_COMP, "kaddd k1, k2, k3" }, // 27
{ NO_COMP, "kmovd k1, k2" },
{ 0, "alternating kaddd k1, k2, k3 and add reg32, reg32" }, // 29
{ 0, "mov regN, 0" }, // 30 "value matching" tests
{ 0, "mov regN, 1" },
{ 0, "loads: mov ebx, [rsp] (LB size)" },
{ NO_COMP, "stores: mov [rsp - 8], ebx (SB size)" },
{ 0, "loads: mov ebx, [r9 + N] (LB size)" },
{ NO_COMP, "alternating kaddd k1, k2, k3 and vpxor ymmN,ymmN,ymmN+1" }, // 35
{ NO_COMP, "pxor mmN, mmN" }, // 36
{ NO_COMP, "por mm0, mm0" }, // 37
{ NO_COMP, "por mmN, mmN+1" }, // 38
{ NO_COMP, "alternating xorps xmmN, xmmN+1 and por mmN, mmN+1" }, // 39
{ 0, "alternating add reg32N, reg32N+1 and por mmN, mmN+1" }, // 40
{ NO_COMP, "alternating kaddd kN, kN+1, kN+1 and por mmN, mmN+1" }, // 41
{ NO_COMP, "kaddb k1, k2, k3" }, // 42
{ NO_COMP, "kaddd kN, kN+1, kN+1" }, // 43
};
const int test_count = sizeof(tests) / sizeof(tests[0]);
const test_info* get_test(int i) {
if (i < 0 || i >= test_count) {
return 0;
}
return tests + i;
}
const char *test_name(int i) {
const test_info* info = get_test(i);
return info ? info->desc : 0;
}
/**
* Add the filler instructions, which vary by test.
* @param ibuf the buffer to write to
* @param insrt the test identifier
* @param i the index of the instruction within the "inner chunk", i.e., everything between two loads is one chunk
* @param k the index of the instruction within the unrolled loop, i.e., everything in one iteration of the inner loop is one chunk
*/
int add_filler(unsigned char* ibuf, int instr, int i, int k)
{
const int reg[4] = {3, 5, 6, 7};
int pbuf = 0;
switch (instr) {
case 0: ADD_BYTE(0x03); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // add (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
case 1: ADD_BYTE(0x90); break; // nop
case 2: ADD_BYTE(0x8b); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // mov (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
case 3: ADD_BYTE(0x39); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // cmp (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
case 4: ADD_WORD(0x9066); break; // two-byte nop 66 90
case 5: ADD_BYTE(0x31); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // xor (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
case 6: ADD_BYTE(0x31); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // xor (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
case 7: ADD_BYTE(0x8b); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // mov (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
case 8: ADD_WORD(0x280f); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // movaps xmm, xmm
case 9: ADD_WORD(0x0f66); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // movdqa xmm, xmm SSE2
case 10: ADD_WORD(0x570f); ADD_BYTE(0xc0 | (i&7)<<3 | (i&7)); break; // xorps xmm, xmm
case 11: ADD_WORD(0x570f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); break; // xorps xmm, xmm+1
case 12: ADD_WORD(0x0f66); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // movdqa xmm, xmm SSE2
case 13: ADD_WORD(0xf9c5); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // movdqa xmm, xmm AVX
case 14: ADD_WORD(0xfdc5); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // movdqa ymm, ymm AVX
case 15: ADD_WORD(0x0f66); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | (((i&3)+0)<<3) | (((i+1)&3)+0)); break; // movdqa xmm, xmm+1 SSE2
case 16: ADD_WORD(0xf9c5); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | (((i&3)+0)<<3) | (((i+1)&3)+0)); break; // movdqa xmm, xmm+1 AVX
case 17: ADD_WORD(0xfdc5); ADD_BYTE(0x6f); ADD_BYTE(0xc0 | (((i&3)+0)<<3) | (((i+1)&3)+0)); break; // movdqa ymm, ymm+1 AVX
case 18: ADD_WORD(0xfcc5 & ~((i&7)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((i&7)<<3) | (i&7)); break; // vxorps ymm, ymm, ymm AVX
case 19: ADD_WORD(0xfcc5 & ~((i&7)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // vxorps ymm, ymm, ymm+1 AVX
case 20:
if (i & 1) {
ADD_WORD(0x570f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); break; // xorps xmm, xmm+1
} else {
if (sizeof(void*)==4) {
ADD_BYTE(0x03); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // add (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
}
else {
ADD_WORD(0x0348); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // add64 (rbx, rbp, rsi, rdi), (rbx, rbp, rsi, rdi)
}
}
case 21: // Alternate between SSE/AVX and integer to see if they are allocated independently.
if (i & 1) {
ADD_WORD(0xfcc5 & ~((i&7)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // vxorps ymm, ymm, ymm+1 AVX-256
// ADD_WORD(0xfdc5 & ~((i&7)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // vxorps xmm, xmm, xmm+1 AVX-128
// ADD_WORD(0x570f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); break; // xorps xmm, xmm+1 SSE-128
}
else
{
ADD_WORD(0x0348); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // add64 (rbx, rbp, rsi, rdi), (rbx, rbp, rsi, rdi)
//ADD_BYTE(0x31); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // xor (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
}
case 22: ADD_BYTE(0x31); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // xor (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
case 23: ADD_WORD(0xe883 | (reg[i&3]<<8)); ADD_BYTE(i); break; // sub reg, val
case 24: ADD_WORD(0x0348); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); break; // add64 (rbx, rbp, rsi, rdi), (rbx, rbp, rsi, rdi)
case 25: ADD_WORD(0x8b48); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[(i+1)&3]); break; // mov64 (ebx, ebp, esi, edi), (edi, ebx, ebp, esi)
case 26: ADD_WORD(0xf162); ADD_BYTE(0x5 | (0xf - (i&7)) << 3); ADD_BYTE(0x48); ADD_BYTE(0xef); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); break; // vpxord zmm, zmm, zmm+1 AVX512
case 27: ADD_BYTE(0xc4); ADD_BYTE(0xe1); ADD_BYTE(0xed); ADD_BYTE(0x4a); ADD_BYTE(0xcb); break; // kaddd k1, k2, k3
case 28: ADD_BYTE(0xc4); ADD_BYTE(0xe1); ADD_BYTE(0xf9); ADD_BYTE(0x90); ADD_BYTE(0xca); break; // kmovd k1, k2
case 29: // mix test 0 and 27: alternate betweeen kaddd k1, k2, k3 and add (ebx, ebp, esi, edi), (ebx, ebp, esi, edi)
if (i & 1) { ADD_BYTE(0xc4); ADD_BYTE(0xe1); ADD_BYTE(0xed); ADD_BYTE(0x4a); ADD_BYTE(0xcb); }
else { ADD_BYTE(0x03); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); }
break;
case 30: ADD_BYTE(0xb8 | reg[i&3]); ADD_DWORD(0x0); break; // mov (ebx, ebp, esi, edi), 0
case 31: ADD_BYTE(0xb8 | reg[i&3]); ADD_DWORD(0x1); break; // mov (ebx, ebp, esi, edi), 1
case 32: ADD_BYTE(0x8b); ADD_BYTE(0x1c); ADD_BYTE(0x24); break; // mov ebx, [rsp]
case 33: ADD_BYTE(0x89); ADD_BYTE(0x5c); ADD_BYTE(0x24); ADD_BYTE(0xf8); break; // mov [rsp-0x8], ebx
case 34: assert(k + 4 <= STACK_SPACE); ADD_BYTE(0x41); ADD_BYTE(0x8B); ADD_BYTE(0x99); ADD_DWORD(k); break; // ebx, DWORD PTR [r9 + K]
case 35:
if (i & 1) { ADD_BYTE(0xc4); ADD_BYTE(0xe1); ADD_BYTE(0xed); ADD_BYTE(0x4a); ADD_BYTE(0xcb); }
else { ADD_WORD(0xfcc5 & ~((i&7)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((i&7)<<3) | ((i+1)&7)); }
break;
case 36: ADD_WORD(0xef0f); ADD_BYTE(0xc0 | (i&7)<<3 | (i&7)); break; // pxor mmN, mmN
case 37: ADD_WORD(0xeb0f); ADD_BYTE(0xc0); break; // por mm0, mm0
case 38: ADD_WORD(0xeb0f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); break; // por mmN, mmN+1
case 39:
// check if mmx and sse regs are shared
if (i & 1) {
ADD_WORD(0x570f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); // xorps xmm, xmm+1
} else {
ADD_WORD(0xeb0f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); // por mmN, mmN+1
}
break;
case 40:
// check if mmx and gp regs are shared
if (i & 1) {
ADD_BYTE(0x03); ADD_BYTE(0xc0 | reg[i&3]<<3 | reg[i&3]); // add reg, reg
} else {
ADD_WORD(0xeb0f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); // por mmN, mmN+1
}
break;
case 41:
// check if mmx and kregs are shared
if (i & 1) {
ADD_BYTE(0xc4); ADD_BYTE(0xe1); ADD_BYTE(0xfd & ~(((i+1)&7)<<3)); ADD_BYTE(0x4a);
ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); // kaddd kN, kN+1, kN+1
} else {
ADD_WORD(0xeb0f); ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); // por mmN, mmN+1
}
break;
case 42: ADD_BYTE(0xc5); ADD_BYTE(0xed); ADD_BYTE(0x4a); ADD_BYTE(0xcb); break; // kaddb k1, k2, k3
case 43: ADD_BYTE(0xc4); ADD_BYTE(0xe1); ADD_BYTE(0xfd & ~(((i+1)&7)<<3)); ADD_BYTE(0x4a);
ADD_BYTE(0xc0 | (i&7)<<3 | ((i+1)&7)); break; // kaddd kN, kN+1, kN+1
}
return pbuf;
}
/**
* icount - the number of instructions between loads
*/
void make_routine(unsigned char* ibuf, void *p1, void *p2, const int icount, const int instr)
{
assert(icount <= MAX_ICOUNT);
if (print_ibuf) {
// we are printing the ibug we just set the buffer pointers to arbitrary
// values 1 and 2, since the buffer will never be accessed and we don't
// want randomized buffer addresses showing up in the assembly since
// those lines would never never match
p1 = (void *)1;
p2 = (void *)2;
}
const test_info* info = get_test(instr);
if (!info) {
printf("invalid test ID %d\n", instr);
exit(EXIT_FAILURE);
}
if (icount < 3) {
printf("icount(%d) must be >= 3\n", icount);
exit(EXIT_FAILURE);
}
int pbuf = 0;
// cdecl calling convention: eax, ecx, and edx are caller-saved.
for (int i=0;i<8;i++)
ADD_WORD(0x9066); // 2-byte nop
ADD_BYTE(0x53); // push ebx
ADD_BYTE(0x55); // push ebp
ADD_BYTE(0x56); // push esi
ADD_BYTE(0x57); // push edi
ADD_WORD(0x5041); // push r8
ADD_WORD(0x5141); // push r9
ADD_BYTE(0x48); ADD_BYTE(0x81); ADD_BYTE(0xEC); ADD_DWORD(STACK_SPACE); // sub rsp, STACK_SPACE
ADD_BYTE(0x45); ADD_BYTE(0x31); ADD_BYTE(0xC0); // xor r8d
ADD_BYTE(0x4C); ADD_BYTE(0x8D); ADD_BYTE(0x0C); ADD_BYTE(0x24); // lea r9
if (sizeof(void*) == 4) {
ADD_BYTE(0xb9); // mov ecx, p1;
ADD_DWORD((unsigned long long)p1);
ADD_BYTE(0xba); // mov edx, p2;
ADD_DWORD((unsigned long long)p2);
ADD_BYTE(0xb8); // mov eax, its;
ADD_DWORD(its);
}
else
{
ADD_WORD(0xb948); // mov rcx, p1;
ADD_DWORD((unsigned long long)p1);
ADD_DWORD((unsigned long long)p1>>32LL);
ADD_WORD(0xba48); // mov rdx, p2;
ADD_DWORD((unsigned long long)p2);
ADD_DWORD((unsigned long long)p2>>32LL);
ADD_WORD(0xb848); // mov rax, its;
ADD_DWORD(its);
ADD_DWORD(0);
}
if (sizeof(void*)==4) {
ADD_WORD(0xe883 | (3<<8)); ADD_BYTE(1); // sub ebx, 1
ADD_WORD(0xe883 | (5<<8)); ADD_BYTE(2); // sub ebp, 2
ADD_WORD(0xe883 | (6<<8)); ADD_BYTE(3); // sub esi, 3
ADD_WORD(0xe883 | (7<<8)); ADD_BYTE(4); // sub edi, 4
}
else
{
ADD_DWORD(0x00e88348 | (3<<16)); // sub ebx, 0
ADD_DWORD(0x00e88348 | (5<<16)); // sub ebp, 0
ADD_DWORD(0x00e88348 | (6<<16)); // sub esi, 0
ADD_DWORD(0x00e88348 | (7<<16)); // sub edi, 0
ADD_DWORD(0x00e88349 | (0<<16)); // sub r8, 0
ADD_DWORD(0x00e88349 | (1<<16)); // sub r9, 0
ADD_DWORD(0x00e88349 | (2<<16)); // sub r10, 0
ADD_DWORD(0x00e88349 | (3<<16)); // sub r11, 0
ADD_DWORD(0x00e88349 | (4<<16)); // sub r12, 0
ADD_DWORD(0x00e88349 | (5<<16)); // sub r13, 0
ADD_DWORD(0x00e88349 | (6<<16)); // sub r14, 0
ADD_DWORD(0x00e88349 | (7<<16)); // sub r15, 0
}
// TODO: what is this doing?
if (instr ==13 || instr == 14 || instr == 16 || instr == 17 || instr == 18 || instr == 19 || instr == 21) {
/*ADD_WORD(0xfcc5 & ~(0<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (0<<3) | 0);
ADD_WORD(0xfcc5 & ~(1<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (1<<3) | 1);
ADD_WORD(0xfcc5 & ~(2<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (2<<3) | 2);
ADD_WORD(0xfcc5 & ~(3<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (3<<3) | 3);
ADD_WORD(0xfcc5 & ~(4<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (4<<3) | 4);
ADD_WORD(0xfcc5 & ~(5<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (5<<3) | 5);
ADD_WORD(0xfcc5 & ~(6<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (6<<3) | 6);
ADD_WORD(0xfcc5 & ~(7<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | (7<<3) | 7); */
for (int r = 0; r < 8; r++)
{
ADD_WORD(0xfcc5 & ~(r<<11)); ADD_BYTE(0xc2); ADD_BYTE(0xc0 | (r<<3) | (r)); ADD_BYTE(0);
if (sizeof(void*) == 8) { ADD_WORD(0x7cc5 & ~((8+r)<<11)); ADD_BYTE(0xc2); ADD_BYTE(0xc0 | (r<<3) | (r)); ADD_BYTE(0); }
}
/*for (int r = 0; r < 8; r++)
{
ADD_WORD(0xfcc5 & ~((r&7)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((r&7)<<3) | (r&7)); // vxorps ymm, ymm, ymm AVX
if (sizeof(void*)==8) { ADD_WORD(0xfcc5 & ~((r+8)<<11)); ADD_BYTE(0x57); ADD_BYTE(0xc0 | ((r&7)<<3) | (r&7)); } // vxorps ymm, ymm, ymm AVX
}*/
}
while (((unsigned long long)ibuf+pbuf) & 0xf) ADD_BYTE(0x90);
int loop_start = pbuf; // loop branch target.
// if the load instructions used to incur the cache misses compete with the filler instruction for
// resources for the given test, then we subtract 2 from the filler instructions since the load
// instructon at either end contribute to resource usage.
bool needs_comp = info->flags & NO_COMP;
const int adjusted_icount = icount - (needs_comp ? 0 : 2);
for (int u=unroll-1, k = 0; u>=0; u--) {
if (sizeof(void*) == 4) {
ADD_BYTE(0x8b); // mov r32, r/m32
ADD_BYTE(0x09); // ... ecx, [ecx]
}
else
{
ADD_WORD(0x8b48); // mov r64, r/m64
ADD_BYTE(0x09); // ... rcx, [rcx]
}
for (int j = 0; j < adjusted_icount; j++)
{
pbuf += add_filler(ibuf+pbuf, instr, j, k++);
}
if (sizeof(void*) == 4) {
ADD_BYTE(0x8b); // mov r32, r/m32
ADD_BYTE(0x12); // ... edx, [edx]
}
else
{
ADD_WORD(0x8b48); // mov r64, r/m64
ADD_BYTE(0x12); // ... edx, [edx]
}
if (lfence_mode) {
ADD_BYTE(0x0F); // lfence
ADD_BYTE(0xAE);
ADD_BYTE(0xE8);
} else {
// we also apply compensation on the last iteration, to allow for the
// sub instruction. In principle this is exact only for measuring the PRF
// size - for ROB size we should also compensate for the jump (unless macro
// fused), and for the load buffer we don't need compensation, unlike with
// the fencing loads, etc.
for (int j=0; j < adjusted_icount - (u == 0 && needs_comp); j++)
{
pbuf += add_filler(ibuf+pbuf, instr, j, k++);
}
}
}
// you use this add r9, r8 if you want to be very sure that the CPU can't know
// that adds of the form [r9 + N] have the same address across iterations, but
// since we unroll by a lot we have 100s of loads and that seems unlikely, so
// keep it out for now to avoid polluting (very slightly) the other results
// ADD_BYTE(0x4D); ADD_BYTE(0x01); ADD_BYTE(0xC1); // add r9, r8 (r9 += 0)
ADD_WORD(0xe883); // sub eax
ADD_BYTE(0x1); // 1
ADD_WORD(0x850f); // jne loop_start
ADD_DWORD(loop_start - pbuf - 4);
ADD_DWORD(0x90669066); // nop padding
ADD_DWORD(0x90669066); // nop padding
ADD_DWORD(0x90669066); // nop padding
ADD_DWORD(0x90669066); // nop padding
ADD_BYTE(0x48); ADD_BYTE(0x81); ADD_BYTE(0xC4); ADD_DWORD(STACK_SPACE); // add rsp, 64
ADD_WORD(0x5941); // pop r9
ADD_WORD(0x5841); // pop r8
ADD_BYTE(0x5f); // pop edi
ADD_BYTE(0x5e); // pop esi
ADD_BYTE(0x5d); // pop ebp
ADD_BYTE(0x5b); // pop ebx
ADD_WORD(0x770f); // emms
ADD_BYTE(0xc3); // c3 ret
if (print_ibuf) {
printf("going to print asm");
FILE *f = fopen("asm.bin", "w");
if (!f) {
fprintf(stdout, "Opening file asm.bin for write failed...\n");
exit(EXIT_FAILURE);
}
fwrite(ibuf, pbuf, 1, f);
fclose(f);
printf("Wrote raw assembly (%d bytes) to asm.bin\n", pbuf);
exit(EXIT_SUCCESS);
}
mprotect(ibuf, pbuf, PROT_READ|PROT_WRITE|PROT_EXEC);
}
inline unsigned long long int rdtsc()
{
unsigned int lo, hi;
__asm__ volatile (".byte 0x0f, 0x31" : "=a" (lo), "=d" (hi));
return (long long)(((unsigned long long)hi)<<32LL) | (unsigned long long) lo;
}
static inline unsigned long long my_rand (unsigned long long limit)
{
return ((unsigned long long)(((unsigned long long)rand()<<48)^((unsigned long long)rand()<<32)^((unsigned long long)rand()<<16)^(unsigned long long)rand())) % limit;
}
void init_dbuf(void ** dbuf, int size, int cycle_length)
{
for (int i=0;i<size;i++)
dbuf[i] = &dbuf[i];
for (int i=size-1;i>0;i--)
{
if (i & 0x1ff) continue;
if (i < cycle_length) continue;
unsigned int k = my_rand(i/cycle_length) * cycle_length + (i%cycle_length);
void* temp = dbuf[i];
dbuf[i] = dbuf[k];
dbuf[k] = temp;
}
}
//const int memsize = 536870912;
const int memsize = 268435456;
static int outer_its = 64;
static int instr_type = 4; // Default to two-byte nop
void print_usage() {
fprintf(stderr, "Usage: robsize [TEST_ID] [OPTIONS]\n\n"
"\t--csv \tOutput in csv format suitable for plotting\n"
"\t--lfence \tUse the lfence-based technique\n"
"\t--slow \tRun more iterations making the test slower but potentiallly more accurate\n"
"\t--fast \tRun fewer iterations making the test faster but potentiallly less accurate\n"
"\t--superfast \tRun at ludicrous speed which is even less accurate than --fast\n"
"\t--maxtest \tOuput the maxium test number and quit, useful for scripts\n"
"\t--write-asm \tPrint the raw generated instructions to a file and quit\n"
"\t--list \tList the available tests and their IDs\n"
"\t--start=START\tUse START to specify the initial value of filler instruction count (Default = 16)\n"
"\t--stop=STOP \tUse STOP to specify the maximum value of filler instruction count (Default = 256)\n"
);
}
void print_tests() {
printf("The following tests are supported, run the want you want with ./robsize <ID>\n");
printf("ID\tDescription\n");
for (int i = 0; test_name(i); i++) {
printf("%d\t%s\n", i, test_name(i));
}
}
/* Command line options for getopt_long() */
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"list", no_argument, NULL, 'l'},
{"csv", no_argument, NULL, 'c'},
{"write-asm", no_argument, NULL, 'w'},
{"lfence", no_argument, NULL, 'e'},
{"slow", no_argument, NULL, 's'},
{"fast", no_argument, NULL, 'f'},
{"superfast", no_argument, NULL, 'g'},
{"maxtest", no_argument, NULL, 'm'},
{"start", required_argument, NULL, 'i'},
{"stop", required_argument, NULL, 'j'},
{0, 0, 0, 0}
};
void handle_args(int argc, char *argv[]) {
int optval = 0;
int opt_idx = 0;
while ((optval = getopt_long(argc, argv, "", long_options, &opt_idx)) >= 0) {
switch (optval) {
case 'h': /* help */
print_usage();
exit(EXIT_SUCCESS);
break;
case 'l': /* list */
print_tests();
exit(EXIT_SUCCESS);
break;
case 'c': /* csv */
plot_mode = true;
break;
case 'w': /* write-asm */
print_ibuf = true;
break;
case 'e':
lfence_mode = true;
break;
case 's': /* slow */
outer_its <<= 1;
break;
case 'f': /* fast */
its >>= 2;
outer_its >>= 2;
break;
case 'g': /* super-fast */
its >>= 4;
outer_its >>= 3;
break;
case 'm': /* maxtest */
printf("%d", test_count - 1);
exit(EXIT_SUCCESS);
case 'i': /* start */
if (sscanf(optarg, "%d", &start_icount) <= 0) {
fprintf(stderr, "Unrecognized value for --start: %s\n", optarg);
print_usage();
exit(EXIT_FAILURE);
}
explicit_start = true;
break;
case 'j': /* stop */
if (sscanf(optarg, "%d", &stop_icount) <= 0) {
fprintf(stderr, "Unrecognized value for --stop: %s\n", optarg);
print_usage();
exit(EXIT_FAILURE);
}
break;
default:
print_usage();
exit(EXIT_FAILURE);
}
}
if (!explicit_start && print_ibuf) {
// if we are printing the ASM, use 33 as our filler count, unless overridden
start_icount = 33;
}
// At most one non-option argument is the positional arg for the test ID
// getopt moves this to the end, so we cannot sue argv[1] here!
if (optind < argc) {
optind += (sscanf(argv[optind], "%d", &instr_type) > 0);
}
if (start_icount > stop_icount) {
fprintf(stderr, "--start (%d) value should be less than or equal to --stop value (%d)\n",
start_icount, stop_icount);
print_usage();
exit(EXIT_FAILURE);
}
// any other non-option arg is not recognized
if (optind < argc) {
for (int i = optind; i < argc; i++) {
fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
}
print_usage();
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[])
{
handle_args(argc, argv);
FILE *verbose_file = plot_mode ? stderr : stdout;
fprintf(verbose_file, "Compiled %s %s\n", __DATE__, __TIME__);
const char *name = test_name(instr_type);
if (!name) {
fprintf(stderr, "Bad test: %d.\nUse --list to display the available tests.\n", instr_type);
return EXIT_FAILURE;
}
unsigned char *ibuf = (unsigned char*)valloc(1048576);
void ** dbuf = (void**)valloc(memsize);
fprintf(verbose_file, "ibuf at %p\n", (void *)ibuf);
fprintf(verbose_file, "dbuf at %p\n", (void *)dbuf);
init_dbuf(dbuf, memsize/sizeof(void*), 8192/sizeof(void*));
void(*routine)() = (void(*)())ibuf;
const char *delim = plot_mode ? "," : "\t";
fprintf(verbose_file, "Running test ID %d: %s\n", instr_type, name);
printf("%s%s%s%s%s%s%s\n", "ICOUNT", delim, "MIN", delim, "AVG", delim, "MAX");
// use 100 if we are printing the buffer because some things don't show up
// until more instructions are used
for (int icount = start_icount; icount <= stop_icount; icount += 1)
{
make_routine(ibuf, dbuf, dbuf+((8388608+4096)/sizeof(void*)), icount, instr_type);
routine();
long long min_diff = 0x7fffffffffffffffLL;
long long max_diff = 0x0;
long long sum_diff = 0;
//long long min_diff = 0;
for (int i=0;i<outer_its;i++) {
void(*routine_skip)() = (void(*)())((char *)routine + (i % 13));
long long start = rdtsc();
routine_skip();
long long stop = rdtsc();
sum_diff += (stop - start);
if (min_diff > (stop - start))
{
min_diff = stop-start;
}
if (max_diff < (stop - start))
{
max_diff = stop-start;
}
}
printf("%d%s%.2f%s%.2f%s%.2f\n", icount, delim, 0.5*min_diff/its/unroll, delim, 0.5*sum_diff/its/unroll/outer_its,
delim, 0.5*max_diff/its/unroll);
}
free (dbuf);
free (ibuf);
}