-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrambw.c
974 lines (844 loc) · 25.2 KB
/
rambw.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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#ifdef __linux__
/* for sched_getaffinity() */
#define _GNU_SOURCE
#include <sched.h>
#endif
#ifdef __SSE2__
#include <x86intrin.h>
#endif
#include <sys/mman.h>
#include <sys/time.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_THREADS 1024
#if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
#define HAS_MANY_REGISTERS 0
#else
#define HAS_MANY_REGISTERS 1
#endif
/* some archs provide a shorter encoding for short negative offsets, let's make
* use of them when possible.
*/
#if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__x86_64__)
#define RELATIVE_OFS -128
#else
#define RELATIVE_OFS 0
#endif
/* some archs have good branch prediction and prefer short loops.
* This must be a power of two between 256 and 2048 inclusive.
*/
#if defined(__x86_64__)
#define BYTES_PER_ROUND 256
#else
#define BYTES_PER_ROUND 2048
#endif
#define USE_GENERIC 0
#define USE_SSE 1
#define USE_VFP 2
#define USE_ARMV7 4
#define USE_ARMV8 8
#define USE_AVX 16
struct stats {
unsigned long last; // copy at interrupt time
unsigned long prev; // copy of previous last
unsigned long rnd; // work value
void *area;
size_t size;
size_t mask;
pthread_t pth; // pthread of the thread
int thr;
} __attribute__((aligned(64)));
struct stats stats[MAX_THREADS];
/* set once the end is reached, reset when setting an alarm */
static volatile int slowstart;
static volatile int stop_now;
static volatile unsigned int meas_count;
static volatile unsigned int skip_measures;
static volatile uint64_t start_time;
static unsigned int interval_usec;
static int nbthreads = 1;
static int ready_threads;
static __thread int thread_num;
static int no_hugepages;
void *(*run)(void *private);
void set_alarm(unsigned int usec);
#if (_POSIX_MEMORY_PROTECTION - 0 < 200112L)
static inline int posix_memalign(void **memptr, size_t alignment, size_t size)
{
*(memptr) = memalign(alignment, size);
return (*memptr) ? 0 : ENOMEM;
}
#endif
/* Prepare startup, report thread as ready and wait for notification. Thread
* zero is supposed to be already initialized (main thread). Just call with
* thread_num < 0 to initialize without waiting for end of slowstart.
*/
static void thread_sync_startup(void *area, size_t size, int thread_num)
{
if (!thread_num)
return;
memset(area, 0, size);
__atomic_add_fetch(&ready_threads, 1, __ATOMIC_SEQ_CST);
/* wait for all threads to be started */
while (__atomic_load_n(&ready_threads, __ATOMIC_ACQUIRE) != nbthreads)
;
if (thread_num < 0)
return;
while (slowstart)
;
}
static inline void read512(const char *addr, const unsigned long ofs)
{
if (HAS_MANY_REGISTERS) {
asm volatile("" : :
"r" (*(uint64_t *)(addr + ofs + 0)), "r" (*(uint64_t *)(addr + ofs + 8)),
"r" (*(uint64_t *)(addr + ofs + 16)), "r" (*(uint64_t *)(addr + ofs + 24)));
asm volatile("" : :
"r" (*(uint64_t *)(addr + ofs + 32)), "r" (*(uint64_t *)(addr + ofs + 40)),
"r" (*(uint64_t *)(addr + ofs + 48)), "r" (*(uint64_t *)(addr + ofs + 56)));
}
else {
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 0)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 8)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 16)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 24)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 32)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 40)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 48)));
asm volatile("" : : "r" (*(uint64_t *)(addr + ofs + 56)));
}
}
/* runs the 512-bit test */
void *run512_generic(void *private)
{
struct stats *ctx = private;
size_t size = ctx->size;
size_t mask = ctx->mask;
void *area = ctx->area;
const char *addr;
unsigned long rnd;
thread_num = ctx->thr;
thread_sync_startup(area, size, thread_num);
area -= RELATIVE_OFS;
for (rnd = ctx->rnd; !stop_now; ) {
__atomic_store_n(&ctx->rnd, rnd, __ATOMIC_RELEASE);
addr = area + (rnd & mask);
rnd += BYTES_PER_ROUND;
__builtin_prefetch(addr + 512 + RELATIVE_OFS, 0);
read512(addr, 0 + RELATIVE_OFS);
read512(addr, 64 + RELATIVE_OFS);
read512(addr, 128 + RELATIVE_OFS);
read512(addr, 192 + RELATIVE_OFS);
#if BYTES_PER_ROUND > 256
read512(addr, 256);
read512(addr, 320);
read512(addr, 384);
read512(addr, 448);
#endif
#if BYTES_PER_ROUND > 512
addr += 512;
read512(addr, 0);
read512(addr, 64);
read512(addr, 128);
read512(addr, 192);
read512(addr, 256);
read512(addr, 320);
read512(addr, 384);
read512(addr, 448);
#endif
#if BYTES_PER_ROUND > 1024
addr += 512;
read512(addr, 0);
read512(addr, 64);
read512(addr, 128);
read512(addr, 192);
read512(addr, 256);
read512(addr, 320);
read512(addr, 384);
read512(addr, 448);
addr += 512;
read512(addr, 0);
read512(addr, 64);
read512(addr, 128);
read512(addr, 192);
read512(addr, 256);
read512(addr, 320);
read512(addr, 384);
read512(addr, 448);
#endif
}
return NULL;
}
#ifdef __SSE2__
static inline void read512_sse(const char *addr, const unsigned long ofs)
{
__m128i xmm0, xmm1, xmm2, xmm3;
asm volatile("" : "=xm" (xmm0), "=xm" (xmm1), "=xm" (xmm2), "=xm" (xmm3) :
"0" (_mm_load_si128((void *)(addr + ofs + 0))),
"1" (_mm_load_si128((void *)(addr + ofs + 16))),
"2" (_mm_load_si128((void *)(addr + ofs + 32))),
"3" (_mm_load_si128((void *)(addr + ofs + 48))));
}
/* runs the 512-bit test */
void *run512_sse(void *private)
{
struct stats *ctx = private;
size_t size = ctx->size;
size_t mask = ctx->mask;
void *area = ctx->area;
const char *addr;
unsigned long rnd;
thread_num = ctx->thr;
thread_sync_startup(area, size, thread_num);
area -= RELATIVE_OFS;
for (rnd = ctx->rnd; !stop_now; ) {
__atomic_store_n(&ctx->rnd, rnd, __ATOMIC_RELEASE);
addr = area + (rnd & mask);
rnd += BYTES_PER_ROUND;
__builtin_prefetch(addr + 1024 + RELATIVE_OFS, 0);
read512_sse(addr, 0 + RELATIVE_OFS);
read512_sse(addr, 64 + RELATIVE_OFS);
read512_sse(addr, 128 + RELATIVE_OFS);
read512_sse(addr, 192 + RELATIVE_OFS);
#if BYTES_PER_ROUND > 256
read512_sse(addr, 256 + RELATIVE_OFS);
read512_sse(addr, 320 + RELATIVE_OFS);
read512_sse(addr, 384 + RELATIVE_OFS);
read512_sse(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 512
addr += 512;
read512_sse(addr, 0 + RELATIVE_OFS);
read512_sse(addr, 64 + RELATIVE_OFS);
read512_sse(addr, 128 + RELATIVE_OFS);
read512_sse(addr, 192 + RELATIVE_OFS);
read512_sse(addr, 256 + RELATIVE_OFS);
read512_sse(addr, 320 + RELATIVE_OFS);
read512_sse(addr, 384 + RELATIVE_OFS);
read512_sse(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 1024
addr += 512;
read512_sse(addr, 0 + RELATIVE_OFS);
read512_sse(addr, 64 + RELATIVE_OFS);
read512_sse(addr, 128 + RELATIVE_OFS);
read512_sse(addr, 192 + RELATIVE_OFS);
read512_sse(addr, 256 + RELATIVE_OFS);
read512_sse(addr, 320 + RELATIVE_OFS);
read512_sse(addr, 384 + RELATIVE_OFS);
read512_sse(addr, 448 + RELATIVE_OFS);
addr += 512;
read512_sse(addr, 0 + RELATIVE_OFS);
read512_sse(addr, 64 + RELATIVE_OFS);
read512_sse(addr, 128 + RELATIVE_OFS);
read512_sse(addr, 192 + RELATIVE_OFS);
read512_sse(addr, 256 + RELATIVE_OFS);
read512_sse(addr, 320 + RELATIVE_OFS);
read512_sse(addr, 384 + RELATIVE_OFS);
read512_sse(addr, 448 + RELATIVE_OFS);
#endif
}
return NULL;
}
#endif
#ifdef __AVX__
static inline void read512_avx(const char *addr, const unsigned long ofs)
{
__m256i xmm0, xmm1;
asm volatile("" : "=xm" (xmm0), "=xm" (xmm1) :
"0" (_mm256_load_si256((void *)(addr + ofs + 0))),
"1" (_mm256_load_si256((void *)(addr + ofs + 32))));
}
static inline void read1024_avx(const char *addr, const unsigned long ofs)
{
__m256i xmm0, xmm1, xmm2, xmm3;
asm volatile("" : "=xm" (xmm0), "=xm" (xmm1), "=xm" (xmm2), "=xm" (xmm3) :
"0" (_mm256_load_si256((void *)(addr + ofs + 0))),
"1" (_mm256_load_si256((void *)(addr + ofs + 32))),
"2" (_mm256_load_si256((void *)(addr + ofs + 64))),
"3" (_mm256_load_si256((void *)(addr + ofs + 96))));
}
/* runs the 512-bit test */
void *run512_avx(void *private)
{
struct stats *ctx = private;
size_t size = ctx->size;
size_t mask = ctx->mask;
void *area = ctx->area;
const char *addr;
unsigned long rnd;
thread_num = ctx->thr;
thread_sync_startup(area, size, thread_num);
area -= RELATIVE_OFS;
for (rnd = ctx->rnd; !stop_now; ) {
__atomic_store_n(&ctx->rnd, rnd, __ATOMIC_RELEASE);
addr = area + (rnd & mask);
rnd += BYTES_PER_ROUND;
__builtin_prefetch(addr + 1024 + RELATIVE_OFS, 0);
read512_avx(addr, 0 + RELATIVE_OFS);
read512_avx(addr, 64 + RELATIVE_OFS);
read512_avx(addr, 128 + RELATIVE_OFS);
read512_avx(addr, 192 + RELATIVE_OFS);
#if BYTES_PER_ROUND > 256
read512_avx(addr, 256 + RELATIVE_OFS);
read512_avx(addr, 320 + RELATIVE_OFS);
read512_avx(addr, 384 + RELATIVE_OFS);
read512_avx(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 512
addr += 512;
read512_avx(addr, 0 + RELATIVE_OFS);
read512_avx(addr, 64 + RELATIVE_OFS);
read512_avx(addr, 128 + RELATIVE_OFS);
read512_avx(addr, 192 + RELATIVE_OFS);
read512_avx(addr, 256 + RELATIVE_OFS);
read512_avx(addr, 320 + RELATIVE_OFS);
read512_avx(addr, 384 + RELATIVE_OFS);
read512_avx(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 1024
addr += 512;
read512_avx(addr, 0 + RELATIVE_OFS);
read512_avx(addr, 64 + RELATIVE_OFS);
read512_avx(addr, 128 + RELATIVE_OFS);
read512_avx(addr, 192 + RELATIVE_OFS);
read512_avx(addr, 256 + RELATIVE_OFS);
read512_avx(addr, 320 + RELATIVE_OFS);
read512_avx(addr, 384 + RELATIVE_OFS);
read512_avx(addr, 448 + RELATIVE_OFS);
addr += 512;
read512_avx(addr, 0 + RELATIVE_OFS);
read512_avx(addr, 64 + RELATIVE_OFS);
read512_avx(addr, 128 + RELATIVE_OFS);
read512_avx(addr, 192 + RELATIVE_OFS);
read512_avx(addr, 256 + RELATIVE_OFS);
read512_avx(addr, 320 + RELATIVE_OFS);
read512_avx(addr, 384 + RELATIVE_OFS);
read512_avx(addr, 448 + RELATIVE_OFS);
#endif
}
return NULL;
}
#endif
#if defined (__VFP_FP__) && defined(__ARM_ARCH_7A__)
static inline void read512_vfp(const char *addr, const unsigned long ofs)
{
asm volatile("vldr %%d0, [%0,%1]\n\t"
"vldr %%d1, [%0,%1+8]\n\t"
"vldr %%d2, [%0,%1+16]\n\t"
"vldr %%d3, [%0,%1+24]\n\t"
: /* no output */
: "r" (addr), "I" (ofs)
: "%d0", "%d1", "%d2", "%d3");
asm volatile("vldr %%d4, [%0,%1+32]\n\t"
"vldr %%d5, [%0,%1+40]\n\t"
"vldr %%d6, [%0,%1+48]\n\t"
"vldr %%d7, [%0,%1+56]\n\t"
: /* no output */
: "r" (addr), "I" (ofs)
: "%d4", "%d5", "%d6", "%d7");
}
/* runs the 512-bit test */
void *run512_vfp(void *private)
{
struct stats *ctx = private;
size_t size = ctx->size;
size_t mask = ctx->mask;
void *area = ctx->area;
const char *addr;
unsigned long rnd;
thread_num = ctx->thr;
thread_sync_startup(area, size, thread_num);
area -= RELATIVE_OFS;
for (rnd = ctx->rnd; !stop_now; ) {
__atomic_store_n(&ctx->rnd, rnd, __ATOMIC_RELEASE);
addr = area + (rnd & mask);
rnd += BYTES_PER_ROUND;
read512_vfp(addr, 0 + RELATIVE_OFS);
read512_vfp(addr, 64 + RELATIVE_OFS);
read512_vfp(addr, 128 + RELATIVE_OFS);
read512_vfp(addr, 192 + RELATIVE_OFS);
#if BYTES_PER_ROUND > 256
read512_vfp(addr, 256 + RELATIVE_OFS);
read512_vfp(addr, 320 + RELATIVE_OFS);
read512_vfp(addr, 384 + RELATIVE_OFS);
read512_vfp(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 512
addr += 512;
read512_vfp(addr, 0 + RELATIVE_OFS);
read512_vfp(addr, 64 + RELATIVE_OFS);
read512_vfp(addr, 128 + RELATIVE_OFS);
read512_vfp(addr, 192 + RELATIVE_OFS);
read512_vfp(addr, 256 + RELATIVE_OFS);
read512_vfp(addr, 320 + RELATIVE_OFS);
read512_vfp(addr, 384 + RELATIVE_OFS);
read512_vfp(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 1024
addr += 512;
read512_vfp(addr, 0 + RELATIVE_OFS);
read512_vfp(addr, 64 + RELATIVE_OFS);
read512_vfp(addr, 128 + RELATIVE_OFS);
read512_vfp(addr, 192 + RELATIVE_OFS);
read512_vfp(addr, 256 + RELATIVE_OFS);
read512_vfp(addr, 320 + RELATIVE_OFS);
read512_vfp(addr, 384 + RELATIVE_OFS);
read512_vfp(addr, 448 + RELATIVE_OFS);
addr += 512;
read512_vfp(addr, 0 + RELATIVE_OFS);
read512_vfp(addr, 64 + RELATIVE_OFS);
read512_vfp(addr, 128 + RELATIVE_OFS);
read512_vfp(addr, 192 + RELATIVE_OFS);
read512_vfp(addr, 256 + RELATIVE_OFS);
read512_vfp(addr, 320 + RELATIVE_OFS);
read512_vfp(addr, 384 + RELATIVE_OFS);
read512_vfp(addr, 448 + RELATIVE_OFS);
#endif
}
return NULL;
}
#endif
#if defined(__ARM_ARCH_7A__)
static inline void read512_armv7(const char *addr, const unsigned long ofs)
{
asm volatile("ldmia %0, { r4-r11 }" :: "r" (addr + ofs + 0) : "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11");
asm volatile("ldmia %0, { r4-r11 }" :: "r" (addr + ofs + 32) : "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11");
}
/* runs the 512-bit test */
void *run512_armv7(void *private)
{
struct stats *ctx = private;
size_t size = ctx->size;
size_t mask = ctx->mask;
void *area = ctx->area;
const char *addr;
unsigned long rnd;
thread_num = ctx->thr;
thread_sync_startup(area, size, thread_num);
area -= RELATIVE_OFS;
for (rnd = ctx->rnd; !stop_now; ) {
__atomic_store_n(&ctx->rnd, rnd, __ATOMIC_RELEASE);
addr = area + (rnd & mask);
rnd += BYTES_PER_ROUND;
read512_armv7(addr, 0 + RELATIVE_OFS);
read512_armv7(addr, 64 + RELATIVE_OFS);
read512_armv7(addr, 128 + RELATIVE_OFS);
read512_armv7(addr, 192 + RELATIVE_OFS);
#if BYTES_PER_ROUND > 256
read512_armv7(addr, 256 + RELATIVE_OFS);
read512_armv7(addr, 320 + RELATIVE_OFS);
read512_armv7(addr, 384 + RELATIVE_OFS);
read512_armv7(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 512
addr += 512;
read512_armv7(addr, 0 + RELATIVE_OFS);
read512_armv7(addr, 64 + RELATIVE_OFS);
read512_armv7(addr, 128 + RELATIVE_OFS);
read512_armv7(addr, 192 + RELATIVE_OFS);
read512_armv7(addr, 256 + RELATIVE_OFS);
read512_armv7(addr, 320 + RELATIVE_OFS);
read512_armv7(addr, 384 + RELATIVE_OFS);
read512_armv7(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 1024
addr += 512;
read512_armv7(addr, 0 + RELATIVE_OFS);
read512_armv7(addr, 64 + RELATIVE_OFS);
read512_armv7(addr, 128 + RELATIVE_OFS);
read512_armv7(addr, 192 + RELATIVE_OFS);
read512_armv7(addr, 256 + RELATIVE_OFS);
read512_armv7(addr, 320 + RELATIVE_OFS);
read512_armv7(addr, 384 + RELATIVE_OFS);
read512_armv7(addr, 448 + RELATIVE_OFS);
addr += 512;
read512_armv7(addr, 0 + RELATIVE_OFS);
read512_armv7(addr, 64 + RELATIVE_OFS);
read512_armv7(addr, 128 + RELATIVE_OFS);
read512_armv7(addr, 192 + RELATIVE_OFS);
read512_armv7(addr, 256 + RELATIVE_OFS);
read512_armv7(addr, 320 + RELATIVE_OFS);
read512_armv7(addr, 384 + RELATIVE_OFS);
read512_armv7(addr, 448 + RELATIVE_OFS);
#endif
}
return NULL;
}
#endif
#if defined(__ARM_ARCH_8A) || defined(__AARCH64EL__)
static inline void read512_armv8(const char *addr, const unsigned long ofs)
{
asm volatile("ldnp q0, q1, [%0,#%1]\n\t"
"ldnp q2, q3, [%0,#%1+32]\n\t"
: /* no output */
: "r" (addr), "I" (ofs)
: "q0", "q1", "q2", "q3");
}
/* runs the 512-bit test using ARMv8 optimizations */
void *run512_armv8(void *private)
{
struct stats *ctx = private;
size_t size = ctx->size;
size_t mask = ctx->mask;
void *area = ctx->area;
const char *addr;
unsigned long rnd;
thread_num = ctx->thr;
thread_sync_startup(area, size, thread_num);
area -= RELATIVE_OFS;
for (rnd = ctx->rnd; !stop_now; ) {
__atomic_store_n(&ctx->rnd, rnd, __ATOMIC_RELEASE);
addr = area + (rnd & mask);
rnd += BYTES_PER_ROUND;
read512_armv8(addr, 0 + RELATIVE_OFS);
read512_armv8(addr, 64 + RELATIVE_OFS);
read512_armv8(addr, 128 + RELATIVE_OFS);
read512_armv8(addr, 192 + RELATIVE_OFS);
#if BYTES_PER_ROUND > 256
read512_armv8(addr, 256 + RELATIVE_OFS);
read512_armv8(addr, 320 + RELATIVE_OFS);
read512_armv8(addr, 384 + RELATIVE_OFS);
read512_armv8(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 512
addr += 512;
read512_armv8(addr, 0 + RELATIVE_OFS);
read512_armv8(addr, 64 + RELATIVE_OFS);
read512_armv8(addr, 128 + RELATIVE_OFS);
read512_armv8(addr, 192 + RELATIVE_OFS);
read512_armv8(addr, 256 + RELATIVE_OFS);
read512_armv8(addr, 320 + RELATIVE_OFS);
read512_armv8(addr, 384 + RELATIVE_OFS);
read512_armv8(addr, 448 + RELATIVE_OFS);
#endif
#if BYTES_PER_ROUND > 1024
addr += 512;
read512_armv8(addr, 0 + RELATIVE_OFS);
read512_armv8(addr, 64 + RELATIVE_OFS);
read512_armv8(addr, 128 + RELATIVE_OFS);
read512_armv8(addr, 192 + RELATIVE_OFS);
read512_armv8(addr, 256 + RELATIVE_OFS);
read512_armv8(addr, 320 + RELATIVE_OFS);
read512_armv8(addr, 384 + RELATIVE_OFS);
read512_armv8(addr, 448 + RELATIVE_OFS);
addr += 512;
read512_armv8(addr, 0 + RELATIVE_OFS);
read512_armv8(addr, 64 + RELATIVE_OFS);
read512_armv8(addr, 128 + RELATIVE_OFS);
read512_armv8(addr, 192 + RELATIVE_OFS);
read512_armv8(addr, 256 + RELATIVE_OFS);
read512_armv8(addr, 320 + RELATIVE_OFS);
read512_armv8(addr, 384 + RELATIVE_OFS);
read512_armv8(addr, 448 + RELATIVE_OFS);
#endif
}
return NULL;
}
#endif
/*****************************************************************************
* measurements *
*****************************************************************************/
/* returns a timestamp in microseconds */
static inline uint64_t rdtsc()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
/* sets the start_time() value as accurately as possible */
static inline void set_start_time()
{
uint64_t before, after;
after = rdtsc();
before = rdtsc();
// compensate for the syscall time
before += before - after;
start_time = before;
}
/* If interval_usec is zero, just marks the alarm as received, otherwise
* prints stats and rearms the timer for the same duration.
*/
void alarm_handler(int sig)
{
uint64_t now, usec, rounds;
int thr;
//printf("thread_num=%d\n", thread_num);
if (slowstart) {
/* that was the pre-heating phase */
slowstart = 0;
return;
}
/* measure the time since last pass, only on first thread */
now = rdtsc();
for (rounds = thr = 0; thr < nbthreads; thr++) {
stats[thr].last = stats[thr].rnd;
rounds += (unsigned long)(stats[thr].last - stats[thr].prev);
stats[thr].prev = stats[thr].last;
}
/* speed = rounds per microsecond. Use 64-bit computations to avoid
* overflows.
*/
usec = now - start_time;
if (usec < 1)
usec = 1;
rounds /= usec; // express it in B/us = MB/s
if (!skip_measures)
printf("%llu\n", (unsigned long long)rounds);
else
skip_measures--;
//printf("now=%llu usec=%llu %llu intv=%d\n", now, usec, (unsigned long long)rounds);
if (meas_count && --meas_count) {
/* rearm the timer for another measure */
start_time = now;
set_alarm(interval_usec);
}
if (!meas_count)
stop_now = 1;
}
/* sets an alarm to trigger after <usec> microseconds. 0 disables it */
void set_alarm(unsigned int usec)
{
struct itimerval timer = {
.it_value.tv_usec = usec % 1000000,
.it_value.tv_sec = usec / 1000000,
};
if (usec) {
stop_now = 0;
signal(SIGALRM, alarm_handler);
signal(SIGVTALRM, alarm_handler);
}
setitimer(ITIMER_REAL, &timer, NULL);
}
/* returns a mask to cover the nearest lower power of two for <size> */
static size_t mask_rounded_down(size_t size)
{
size_t mask = size;
unsigned int shift = 1;
while (shift < 8 * sizeof(mask) && (size = mask >> shift)) {
mask |= size;
shift <<= 1;
}
return mask >> 1;
}
/* Access aligned words of optimal size over <size> bytes for each thread.
* Note: size is rounded down to the lower power of two, and must be at
* least 4kB.
*/
unsigned int random_read_over_area(size_t size)
{
size_t mask;
int thr;
mask = mask_rounded_down(size);
if (!run)
return 0;
/* create threads for thread 1 and above */
for (thr = 0; thr < nbthreads; thr++) {
stats[thr].size = size;
stats[thr].mask = mask;
stats[thr].thr = thr;
stats[thr].rnd = 0;
if (posix_memalign(&stats[thr].area, size / 4, size) != 0 || !stats[thr].area) {
printf("Failed to allocate memory for thread %d\n", thr);
exit(1);
}
#ifdef MADV_DONTDUMP
madvise(stats[thr].area, size, MADV_DONTDUMP);
#endif
#ifdef MADV_HUGEPAGE
if (no_hugepages)
madvise(stats[thr].area, size, MADV_NOHUGEPAGE);
else
madvise(stats[thr].area, size, MADV_HUGEPAGE);
#endif
if (thr > 0 && pthread_create(&stats[thr].pth, NULL, run, &stats[thr]) < 0) {
fprintf(stderr, "Failed to start thread #%d; aborting.\n", thr);
exit(1);
}
}
/* initialize thread 0's area and do not wait for slowstart */
thread_sync_startup(stats[0].area, size, -1);
if (slowstart) {
set_alarm(500000);
while (slowstart)
;
set_alarm(0);
}
set_alarm(interval_usec);
set_start_time();
run(&stats[0]);
set_alarm(0);
return 0;
}
/* return the default thread count based on the detected affinity settings. */
int default_thread_count()
{
#if defined(__linux__) && defined(CPU_COUNT)
cpu_set_t mask;
if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
return CPU_COUNT(&mask);
#endif
return 1;
}
int main(int argc, char **argv)
{
unsigned int usec;
size_t size, size_thr;
int implementation __attribute__((unused));
/* set default implementation bits */
implementation = USE_GENERIC;
#ifdef __SSE4_1__
/* don't enable it by default when there's only SSE2, as it's slower
* than generic.
*/
implementation |= USE_SSE;
#endif
#ifdef __AVX__
implementation = USE_AVX;
#endif
#if defined (__VFP_FP__) && defined(__ARM_ARCH_7A__)
implementation |= USE_VFP;
#endif
#if defined(__ARM_ARCH_7A__)
implementation |= USE_ARMV7;
#endif
#if defined(__ARM_ARCH_8A) || defined(__AARCH64EL__)
implementation |= USE_ARMV8;
#endif
usec = 100000;
size = 0;
nbthreads = default_thread_count();
while (argc > 1 && *argv[1] == '-') {
if (strcmp(argv[1], "-s") == 0) {
slowstart = 1;
}
else if (strcmp(argv[1], "-H") == 0) {
no_hugepages = 1;
}
else if (strcmp(argv[1], "-t") == 0 && argc > 2) {
nbthreads = atoi(argv[2]);
argc--; argv++;
}
else if (strcmp(argv[1], "-G") == 0) {
implementation = USE_GENERIC;
}
#ifdef __SSE2__
else if (strcmp(argv[1], "-S") == 0) {
implementation = USE_SSE;
}
#endif
#ifdef __AVX__
else if (strcmp(argv[1], "-A") == 0) {
implementation = USE_AVX;
}
#endif
#if defined (__VFP_FP__) && defined(__ARM_ARCH_7A__)
else if (strcmp(argv[1], "-V") == 0) {
implementation = USE_VFP;
}
#endif
#if defined(__ARM_ARCH_7A__)
else if (strcmp(argv[1], "-7") == 0) {
implementation = USE_ARMV7;
}
#endif
#if defined(__ARM_ARCH_8A) || defined(__AARCH64EL__)
else if (strcmp(argv[1], "-8") == 0) {
implementation = USE_ARMV8;
}
#endif
else {
fprintf(stderr,
"Usage: prog [options]* [<time_ms> [<count> [<size_kB>]]]\n"
" -t <threads> : start this number of threads (default: %d)\n"
" -s : slowstart : pre-heat for 500ms to let cpufreq adapt and skip 1st value.\n"
#ifdef MADV_HUGEPAGE
" -H : disable Huge Pages when supported\n"
#endif
" -h : show this help\n"
" -G : use generic code only\n"
#ifdef __SSE2__
" -S : use SSE\n"
#endif
#ifdef __AVX__
" -A : use AVX\n"
#endif
#if defined (__VFP_FP__) && defined(__ARM_ARCH_7A__)
" -V : use VFP\n"
#endif
#if defined(__ARM_ARCH_7A__)
" -7 : use ARMv7\n"
#endif
#if defined(__ARM_ARCH_8A) || defined(__AARCH64EL__)
" -8 : use ARMv8\n"
#endif
"Defaults: time=100ms, count=1, size=16MB per thread\n",
default_thread_count());
exit(!!strcmp(argv[1], "-h"));
}
argc--;
argv++;
}
if (argc > 1)
usec = atoi(argv[1]) * 1000;
if (argc > 2)
meas_count = atoi(argv[2]);
if (argc > 3)
size = atol(argv[3]) * 1024;
if (!size)
size = nbthreads * 16 * 1048576;
run = run512_generic;
#ifdef __SSE2__
if (implementation & USE_SSE) {
run = run512_sse;
}
#endif
#ifdef __AVX__
if (implementation & USE_AVX) {
run = run512_avx;
}
#endif
#if defined(__ARM_ARCH_7A__)
if (implementation & USE_ARMV7) {
run = run512_armv7;
}
#endif
#if defined (__VFP_FP__) && defined(__ARM_ARCH_7A__)
if (implementation & USE_VFP) {
run = run512_vfp;
}
#endif
#if defined(__ARM_ARCH_8A) || defined(__AARCH64EL__)
if (implementation & USE_ARMV8) {
run = run512_armv8;
}
#endif
interval_usec = usec;
if (slowstart)
skip_measures = 1;
meas_count = meas_count > 0 ? meas_count : 1;
meas_count += skip_measures;
if (nbthreads < 1 || nbthreads > MAX_THREADS) {
fprintf(stderr, "Fatal: invalid number of threads, accepted range is 1..%d.\n", MAX_THREADS);
exit(1);
}
size_thr = size / nbthreads;
/* round it down to the largest power of 2 */
while (size_thr & (size_thr - 1))
size_thr = size_thr & (size_thr - 1);
if (size_thr < 1024) {
fprintf(stderr, "Fatal: too small area size, minimum is 1kB per thread\n");
exit(1);
}
if (size_thr * nbthreads != size)
fprintf(stderr, "Notice: using %lu bytes per thread (%lu kB total)\n",
(unsigned long)size_thr, (unsigned long)(size_thr * nbthreads) / 1024);
random_read_over_area(size_thr);
exit(0);
}