-
Notifications
You must be signed in to change notification settings - Fork 77
/
multiload.c
1481 lines (1387 loc) · 50.2 KB
/
multiload.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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2015 Google Inc. All Rights Reserved.
* 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 <alloca.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "arena.h"
#include "cpu_util.h"
#include "expand.h"
#include "permutation.h"
#include "timer.h"
#include "util.h"
#if defined(__x86_64__)
#include <emmintrin.h>
#endif
// The total memory, stride, and TLB locality have been chosen carefully for
// the current generation of CPUs:
//
// - at stride of 64 bytes the L2 next-line prefetch on p-m/core/core2 gives a
// helping hand
//
// - at stride of 128 bytes the stream prefetcher on various p4 decides the
// random accesses sometimes look like a stream and gives a helping hand.
//
// - the TLB locality could have been raised beyond 4 pages to defeat various
// stream prefetchers, but you need to get out well past 32 pages before
// all existing hw prefetchers are defeated, and then you start exceding the
// TLB locality on several CPUs and incurring some TLB overhead.
// Hence, the default has been changed from 16 pages to 64 pages.
//
#define DEF_TOTAL_MEMORY ((size_t)256 * 1024 * 1024)
#define DEF_STRIDE ((size_t)256)
#define DEF_NR_SAMPLES ((size_t)5)
#define DEF_TLB_LOCALITY ((size_t)64)
#define DEF_NR_THREADS ((size_t)1)
#define DEF_CACHE_FLUSH ((size_t)64 * 1024 * 1024)
#define DEF_OFFSET ((size_t)0)
#define DEF_DELAY ((size_t)1)
#define DEF_CACHELINE ((size_t)64)
#define LOAD_DELAY_WARMUP_uS \
4000000 // Latency & Load thread warmup before data sampling starts
#define LOAD_DELAY_RUN_uS 2000000 // Data sampling request frequency
#define LOAD_DELAY_SAMPLE_uS \
10000 // Data sample polling loop delay waiting for load threads to update
// mutex variable
typedef enum { RUN_CHASE, RUN_BANDWIDTH, RUN_CHASE_LOADED } test_type_t;
static volatile uint64_t use_result_dummy = 0x0123456789abcdef;
static size_t default_page_size;
static size_t page_size;
static bool use_thp;
int verbosity;
int print_timestamp;
int is_weighted_mbind;
uint16_t mbind_weights[MAX_MEM_NODES];
#ifdef __i386__
#define MAX_PARALLEL (6) // maximum number of chases in parallel
#else
#define MAX_PARALLEL (10)
#endif
// forward declare
typedef struct chase_t chase_t;
// the arguments for the chase threads
typedef union {
char pad[AVOID_FALSE_SHARING];
struct {
unsigned thread_num; // which thread is this
volatile uint64_t count; // return thread measurement - need 64 bits when
// passing bandwidth
void *cycle[MAX_PARALLEL]; // initial address for the chases
const char *extra_args;
int dummy; // useful for confusing the compiler
const struct generate_chase_common_args *genchase_args;
size_t nr_threads;
const chase_t *chase;
void *flush_arena;
size_t cache_flush_size;
bool use_longer_chase;
test_type_t run_test_type; // test type: chase or memory bandwidth
const chase_t *memload; // memory bandwidth function
char *load_arena; // load memory buffer used by this thread
size_t load_total_memory; // load size of the arena
size_t load_offset; // load offset of the arena
size_t load_tlb_locality; // group accesses within this range in order to
// amortize TLB fills
volatile size_t sample_no; // flag from main thread to tell bandwdith
// thread to start the next sample.
size_t delay; // injection delay for load
} x;
} per_thread_t;
int always_zero;
static void chase_simple(per_thread_t *t) {
void *p = t->x.cycle[0];
do {
x200(p = *(void **)p;)
} while (__sync_add_and_fetch(&t->x.count, 200));
// we never actually reach here, but the compiler doesn't know that
t->x.dummy = (uintptr_t)p;
}
// parallel chases
#define declare(i) void *p##i = start[i];
#define cleanup(i) tmp += (uintptr_t)p##i;
#if MAX_PARALLEL == 6
#define parallel(foo) foo(0) foo(1) foo(2) foo(3) foo(4) foo(5)
#else
#define parallel(foo) \
foo(0) foo(1) foo(2) foo(3) foo(4) foo(5) foo(6) foo(7) foo(8) foo(9)
#endif
#define template(n, expand, inner) \
static void chase_parallel##n(per_thread_t *t) { \
void **start = t->x.cycle; \
parallel(declare) do { x##expand(inner) } \
while (__sync_add_and_fetch(&t->x.count, n * expand)) \
; \
\
uintptr_t tmp = 0; \
parallel(cleanup) t->x.dummy = tmp; \
}
#if defined(__x86_64__) || defined(__i386__)
#define D(n) asm volatile("mov (%1),%0" : "=r"(p##n) : "r"(p##n));
#else
#define D(n) p##n = *(void **)p##n;
#endif
template(2, 100, D(0) D(1));
template(3, 66, D(0) D(1) D(2));
template(4, 50, D(0) D(1) D(2) D(3));
template(5, 40, D(0) D(1) D(2) D(3) D(4));
template(6, 32, D(0) D(1) D(2) D(3) D(4) D(5));
#if MAX_PARALLEL > 6
template(7, 28, D(0) D(1) D(2) D(3) D(4) D(5) D(6));
template(8, 24, D(0) D(1) D(2) D(3) D(4) D(5) D(6) D(7));
template(9, 22, D(0) D(1) D(2) D(3) D(4) D(5) D(6) D(7) D(8));
template(10, 20, D(0) D(1) D(2) D(3) D(4) D(5) D(6) D(7) D(8) D(9));
#endif
#undef D
#undef parallel
#undef cleanup
#undef declare
static void chase_work(per_thread_t *t) {
void *p = t->x.cycle[0];
size_t extra_work = strtoul(t->x.extra_args, 0, 0);
size_t work = 0;
size_t i;
// the extra work is intended to be overlapped with a dereference,
// but we don't want it to skip past the next dereference. so
// we fold in the value of the pointer, and launch the deref then
// go into a loop performing extra work, hopefully while the
// deref occurs.
do {
x25(work += (uintptr_t)p; p = *(void **)p;
for (i = 0; i < extra_work; ++i) { work ^= i; })
} while (__sync_add_and_fetch(&t->x.count, 25));
// we never actually reach here, but the compiler doesn't know that
t->x.cycle[0] = p;
t->x.dummy = work;
}
struct incr_struct {
struct incr_struct *next;
unsigned incme;
};
static void chase_incr(per_thread_t *t) {
struct incr_struct *p = t->x.cycle[0];
do {
x50(++p->incme; p = *(void **)p;)
} while (__sync_add_and_fetch(&t->x.count, 50));
// we never actually reach here, but the compiler doesn't know that
t->x.cycle[0] = p;
}
#if defined(__x86_64__) || defined(__i386__)
#define chase_prefetch(type) \
static void chase_prefetch##type(per_thread_t *t) { \
void *p = t->x.cycle[0]; \
\
do { \
x100(asm volatile("prefetch" #type " %0" ::"m"(*(void **)p)); \
p = *(void **)p;) \
} while (__sync_add_and_fetch(&t->x.count, 100)); \
\
/* we never actually reach here, but the compiler doesn't know that */ \
t->x.cycle[0] = p; \
}
chase_prefetch(t0);
chase_prefetch(t1);
chase_prefetch(t2);
chase_prefetch(nta);
#undef chase_prefetch
#endif
#if defined(__x86_64__)
static void chase_movdqa(per_thread_t *t) {
void *p = t->x.cycle[0];
do {
x100(asm volatile("\n movdqa (%%rax),%%xmm0"
"\n movdqa 16(%%rax),%%xmm1"
"\n paddq %%xmm1,%%xmm0"
"\n movdqa 32(%%rax),%%xmm2"
"\n paddq %%xmm2,%%xmm0"
"\n movdqa 48(%%rax),%%xmm3"
"\n paddq %%xmm3,%%xmm0"
"\n movq %%xmm0,%%rax"
: "=a"(p)
: "0"(p));)
} while (__sync_add_and_fetch(&t->x.count, 100));
t->x.cycle[0] = p;
}
static void chase_movntdqa(per_thread_t *t) {
void *p = t->x.cycle[0];
do {
x100(asm volatile(
#ifndef BINUTILS_HAS_MOVNTDQA
"\n .byte 0x66,0x0f,0x38,0x2a,0x00"
"\n .byte 0x66,0x0f,0x38,0x2a,0x48,0x10"
"\n paddq %%xmm1,%%xmm0"
"\n .byte 0x66,0x0f,0x38,0x2a,0x50,0x20"
"\n paddq %%xmm2,%%xmm0"
"\n .byte 0x66,0x0f,0x38,0x2a,0x58,0x30"
"\n paddq %%xmm3,%%xmm0"
"\n movq %%xmm0,%%rax"
#else
"\n movntdqa (%%rax),%%xmm0"
"\n movntdqa 16(%%rax),%%xmm1"
"\n paddq %%xmm1,%%xmm0"
"\n movntdqa 32(%%rax),%%xmm2"
"\n paddq %%xmm2,%%xmm0"
"\n movntdqa 48(%%rax),%%xmm3"
"\n paddq %%xmm3,%%xmm0"
"\n movq %%xmm0,%%rax"
#endif
: "=a"(p)
: "0"(p));)
} while (__sync_add_and_fetch(&t->x.count, 100));
t->x.cycle[0] = p;
}
static void chase_critword2(per_thread_t *t) {
void *p = t->x.cycle[0];
size_t offset = strtoul(t->x.extra_args, 0, 0);
void *q = (char *)p + offset;
do {
x100(asm volatile("mov (%1),%0"
: "=r"(p)
: "r"(p));
asm volatile("mov (%1),%0"
: "=r"(q)
: "r"(q));)
} while (__sync_add_and_fetch(&t->x.count, 100));
t->x.cycle[0] = (void *)((uintptr_t)p + (uintptr_t)q);
}
#endif
struct chase_t {
void (*fn)(per_thread_t *t);
size_t base_object_size;
const char *name;
const char *usage1;
const char *usage2;
int requires_arg;
unsigned parallelism; // number of parallel chases (at least 1)
};
static const chase_t chases[] = {
// the default must be first
{
.fn = chase_simple,
.base_object_size = sizeof(void *),
.name = "simple ",
.usage1 = "simple",
.usage2 = "no frills pointer dereferencing",
.requires_arg = 0,
.parallelism = 1,
},
{
.fn = chase_simple,
.base_object_size = sizeof(void *),
.name = "chaseload",
.usage1 = "chaseload",
.usage2 = "runs simple chase with multiple memory bandwidth loads",
.requires_arg = 0,
.parallelism = 1,
},
{
.fn = chase_work,
.base_object_size = sizeof(void *),
.name = "work ",
.usage1 = "work:N",
.usage2 = "loop simple computation N times in between derefs",
.requires_arg = 1,
.parallelism = 1,
},
{
.fn = chase_incr,
.base_object_size = sizeof(struct incr_struct),
.name = "incr ",
.usage1 = "incr",
.usage2 = "modify the cache line after each deref",
.requires_arg = 0,
.parallelism = 1,
},
#if defined(__x86_64__) || defined(__i386__)
#define chase_prefetch(type) \
{ \
.fn = chase_prefetch##type, .base_object_size = sizeof(void *), \
.name = #type, .usage1 = #type, \
.usage2 = "perform prefetch" #type " before each deref", \
.requires_arg = 0, .parallelism = 1, \
}
chase_prefetch(t0),
chase_prefetch(t1),
chase_prefetch(t2),
chase_prefetch(nta),
#endif
#if defined(__x86_64__)
{
.fn = chase_movdqa,
.base_object_size = 64,
.name = "movdqa",
.usage1 = "movdqa",
.usage2 = "use movdqa to read from memory",
.requires_arg = 0,
.parallelism = 1,
},
{
.fn = chase_movntdqa,
.base_object_size = 64,
.name = "movntdqa",
.usage1 = "movntdqa",
.usage2 = "use movntdqa to read from memory",
.requires_arg = 0,
.parallelism = 1,
},
#endif
#define PAR(n) \
{ \
.fn = chase_parallel##n, .base_object_size = sizeof(void *), \
.name = "parallel" #n, .usage1 = "parallel" #n, \
.usage2 = "alternate " #n " non-dependent chases in each thread", \
.parallelism = n, \
}
PAR(2),
PAR(3),
PAR(4),
PAR(5),
PAR(6),
#if MAX_PARALLEL > 6
PAR(7),
PAR(8),
PAR(9),
PAR(10),
#endif
#undef PAR
#if defined(__x86_64__)
{
.fn = chase_critword2,
.base_object_size = 64,
.name = "critword2",
.usage1 = "critword2:N",
.usage2 = "a two-parallel chase which reads at X and X+N",
.requires_arg = 1,
.parallelism = 1,
},
#endif
{
.fn = chase_simple,
.base_object_size = 64,
.name = "critword",
.usage1 = "critword:N",
.usage2 = "a non-parallel chase which reads at X and X+N",
.requires_arg = 1,
.parallelism = 1,
},
};
//========================================================================================================
// Memory Bandwidth load generation
//========================================================================================================
#define LOAD_MEMORY_INIT_MIBPS \
register uint64_t loops = 0; \
size_t cur_sample = -1, nxt_sample = 0; \
double time0, time1, timetot; \
double bite_sum = 0, mibps = 0; /*mibps = MiB per sec.*/ \
time0 = (double)now_nsec();
#define LOAD_MEMORY_SAMPLE_MIBPS \
loops++; \
/* Main thread will increment x.sample_no when it wants a sample. */ \
nxt_sample = t->x.sample_no; \
/* printf("T(%d)%ld:%ld,%ld ", t->x.thread_num, cur_sample, nxt_sample, \
* t->x.count ); */ \
/* if ( (loops&0xF)==0xf ) printf("T(%d)%ld:%ld,%ld ", t->x.thread_num, \
* cur_sample, nxt_sample, t->x.count ); */ \
if ((cur_sample != nxt_sample) && (t->x.count == 0)) { \
time1 = (double)now_nsec(); \
bite_sum = loops * load_bites; \
timetot = time1 - time0; \
mibps = (bite_sum * 1000000000.0) / (timetot * 1024 * 1024); \
/* printf(" T(%d)=%.0f:%ld:%ld ", t->x.thread_num, mibps, cur_sample, \
* nxt_sample ); */ \
/* printf(" %ld,%ld,%ld,%.0f:%.0f(%.1fMiBs)\n", cur_sample, loops, \
* t->x.count, bite_sum, timetot, mibps); */ \
/* update the MiB/s count. Main thread will read and set to 0 so we know \
* this sample is done. */ \
__sync_add_and_fetch(&t->x.count, (uint64_t)mibps); \
cur_sample = nxt_sample; \
loops = 0; \
time0 = (double)now_nsec(); \
}
//--------------------------------------------------------------------------------------------------------
static void load_memcpy_libc(per_thread_t *t) {
#define LOOP_OPS 2
uint64_t load_loop = t->x.load_total_memory / LOOP_OPS;
uint64_t load_bites = load_loop * LOOP_OPS;
register char *a = (char *)t->x.load_arena;
register char *b = a + load_loop;
register char *tmp;
LOAD_MEMORY_INIT_MIBPS
do {
tmp = a;
a = b;
b = tmp;
memcpy((void *)a, (void *)b, load_loop);
LOAD_MEMORY_SAMPLE_MIBPS
} while (1);
#undef LOOP_OPS
}
//--------------------------------------------------------------------------------------------------------
static void load_memset_libc(per_thread_t *t) {
#define LOOP_OPS 1
uint64_t load_bites = t->x.load_total_memory * LOOP_OPS;
register char *a = (char *)t->x.load_arena;
LOAD_MEMORY_INIT_MIBPS
do {
memset((void *)a, 0xdeadbeef, load_bites);
LOAD_MEMORY_SAMPLE_MIBPS
} while (1);
#undef LOOP_OPS
}
//--------------------------------------------------------------------------------------------------------
static void load_memsetz_libc(per_thread_t *t) {
#define LOOP_OPS 1
uint64_t load_bites = t->x.load_total_memory * LOOP_OPS;
register char *a = (char *)t->x.load_arena;
LOAD_MEMORY_INIT_MIBPS
do {
memset((void *)a, 0, load_bites);
LOAD_MEMORY_SAMPLE_MIBPS
} while (1);
#undef LOOP_OPS
}
//--------------------------------------------------------------------------------------------------------
static void load_stream_triad(per_thread_t *t) {
#define LOOP_OPS 3
#define LOOP_ALIGN 16
uint64_t load_loop, load_bites;
register uint64_t N, i;
register double *a;
register double *b;
register double *c;
register double *tmp;
load_loop =
t->x.load_total_memory -
(LOOP_OPS * LOOP_ALIGN); // subract to allow aligning count/addresses
load_loop = (load_loop / LOOP_OPS) &
~(LOOP_ALIGN - 1); // divide by 3 buffers and align byte count on
// LOOP_ALIGN byte multiple
N = load_loop / sizeof(double);
load_bites = N * sizeof(double) * LOOP_OPS;
size_t aa = (((size_t)t->x.load_arena + LOOP_ALIGN) &
~(LOOP_ALIGN)); // align on 16 byte address
a = (double *)aa;
b = a + N;
c = b + N;
if (verbosity > 1) {
printf(
"load_arena=%p, load_total_memory=0x%lX, load_loop=0x%lX, N=0x%lX, "
"a=%p, b=%p, c=%p\n",
(char *)t->x.load_arena, t->x.load_total_memory, load_loop, N, a, b, c);
}
LOAD_MEMORY_INIT_MIBPS
do {
tmp = a;
a = b;
b = c;
c = tmp;
for (i = 0; i < N; ++i) {
a[i] = b[i] + c[i];
}
LOAD_MEMORY_SAMPLE_MIBPS
} while (1);
#undef LOOP_OPS
#undef LOOP_ALIGN
}
static inline void delay_until_iteration(uint64_t iteration) {
while (iteration--) {
asm volatile("nop");
}
}
//--------------------------------------------------------------------------------------------------------
// Stream triad pattern with nontermpoal hint and adjustable injection delay
static void load_stream_triad_nontemporal_injection_delay(per_thread_t *t) {
#define LOOP_OPS 3
#define LOOP_ALIGN 16
uint64_t load_loop, load_bites;
register uint64_t N, i;
register uint64_t *a;
register uint64_t *b;
register uint64_t *c;
register uint64_t *tmp;
const uint64_t num_elem_twocachelines = DEF_CACHELINE / sizeof(uint64_t) * 2;
load_loop =
t->x.load_total_memory -
(LOOP_OPS * LOOP_ALIGN); // subtract to allow aligning count/addresses
load_loop = (load_loop / LOOP_OPS) &
~(LOOP_ALIGN - 1); // divide by 3 buffers and align byte count on
// LOOP_ALIGN byte multiple
N = load_loop / sizeof(uint64_t);
load_bites = N * sizeof(uint64_t) * LOOP_OPS;
size_t aa = (((size_t)t->x.load_arena + LOOP_ALIGN) &
~(LOOP_ALIGN)); // align on 16 byte address
a = (uint64_t *)aa;
b = a + N;
c = b + N;
if (verbosity > 1) {
printf(
"load_arena=%p, load_total_memory=0x%lX, load_loop=0x%lX, N=0x%lX, "
"a=%p, b=%p, c=%p\n",
(char *)t->x.load_arena, t->x.load_total_memory, load_loop, N, a, b, c);
}
LOAD_MEMORY_INIT_MIBPS
do {
tmp = a;
a = b;
b = c;
c = tmp;
for (i = 0; i < N; i += 2) {
if (i % num_elem_twocachelines == 0) delay_until_iteration(t->x.delay);
#if defined(__aarch64__)
asm volatile ("stnp %0, %1, [%2]" :: "r"(b[i]+c[i]), "r"(b[i+1]+c[i+1]), "r" (a+i));
#elif defined(__x86_64__)
_mm_stream_si64(&((long long*)a)[i], b[i]+c[i]);
_mm_stream_si64(&((long long*)a)[i+1], b[i+1]+c[i+1]);
#else
a[i] = b[i] + c[i];
a[i+1] = b[i+1] + c[i+1];
#endif
}
LOAD_MEMORY_SAMPLE_MIBPS
} while (1);
#undef LOOP_OPS
#undef LOOP_ALIGN
}
//--------------------------------------------------------------------------------------------------------
static void load_stream_copy(per_thread_t *t) {
#define LOOP_OPS 2
uint64_t load_loop = t->x.load_total_memory / LOOP_OPS;
register uint64_t N = load_loop / sizeof(double);
uint64_t load_bites = N * sizeof(double) * LOOP_OPS;
register uint64_t i;
register double *a = (double *)t->x.load_arena;
register double *b = a + N;
register double *tmp;
LOAD_MEMORY_INIT_MIBPS
do {
tmp = a;
a = b;
b = tmp;
for (i = 0; i < N; ++i) {
b[i] = a[i];
}
LOAD_MEMORY_SAMPLE_MIBPS
} while (1);
#undef LOOP_OPS
}
//--------------------------------------------------------------------------------------------------------
static void load_stream_sum(per_thread_t *t) {
#define LOOP_OPS 1
uint64_t load_loop = t->x.load_total_memory / LOOP_OPS;
register uint64_t N = load_loop / sizeof(uint64_t);
uint64_t load_bites = N * sizeof(uint64_t) * LOOP_OPS;
register uint64_t i;
register uint64_t *a = (uint64_t *)t->x.load_arena;
register uint64_t s = 0;
LOAD_MEMORY_INIT_MIBPS
do {
for (i = 0; i < N; ++i) {
s += a[i];
}
LOAD_MEMORY_SAMPLE_MIBPS
use_result_dummy += s;
} while (1);
#undef LOOP_OPS
}
//--------------------------------------------------------------------------------------------------------
static const chase_t memloads[] = {
// the default must be first
{
.fn = load_memcpy_libc,
.base_object_size = sizeof(void *),
.name = "memcpy-libc",
.usage1 = "memcpy-libc",
.usage2 = "1:1 rd:wr - memcpy()",
.requires_arg = 0,
.parallelism = 0,
},
{
.fn = load_memset_libc,
.base_object_size = sizeof(void *),
.name = "memset-libc",
.usage1 = "memset-libc",
.usage2 = "0:1 rd:wr - memset() non-zero data",
.requires_arg = 0,
.parallelism = 0,
},
{
.fn = load_memsetz_libc,
.base_object_size = sizeof(void *),
.name = "memsetz-libc",
.usage1 = "memsetz-libc",
.usage2 = "0:1 rd:wr - memset() zero data",
.requires_arg = 0,
.parallelism = 0,
},
{
.fn = load_stream_copy,
.base_object_size = sizeof(void *),
.name = "stream-copy",
.usage1 = "stream-copy",
.usage2 = "1:1 rd:wr - lmbench stream copy ",
.requires_arg = 0,
.parallelism = 0,
},
{
.fn = load_stream_sum,
.base_object_size = sizeof(void *),
.name = "stream-sum",
.usage1 = "stream-sum",
.usage2 = "1:0 rd:wr - lmbench stream sum ",
.requires_arg = 0,
.parallelism = 0,
},
{
.fn = load_stream_triad,
.base_object_size = sizeof(void *),
.name = "stream-triad",
.usage1 = "stream-triad",
.usage2 = "2:1 rd:wr - lmbench stream triad a[i]=b[i]+(scalar*c[i])",
.requires_arg = 0,
.parallelism = 0,
},
{
.fn = load_stream_triad_nontemporal_injection_delay,
.base_object_size = sizeof(void *),
.name = "stream-triad-nontemporal-injection-delay",
.usage1 = "stream-triad-nontemporal-injection-delay",
.usage2 = "2:1 rd:wr - lmbench stream triad with nontemporal hint",
.requires_arg = 0,
.parallelism = 0,
}};
static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
static size_t nr_to_startup;
static int set_thread_affinity = 1;
static void *thread_start(void *data) {
per_thread_t *args = data;
// ensure every thread has a different RNG
rng_init(args->x.thread_num);
if (set_thread_affinity) {
// find out which cpus we can run on and move us to an appropriate cpu
cpu_set_t cpus;
if (sched_getaffinity(0, sizeof(cpus), &cpus)) {
perror("sched_getaffinity");
exit(1);
}
int my_cpu;
unsigned num = args->x.thread_num;
for (my_cpu = 0; my_cpu < CPU_SETSIZE; ++my_cpu) {
if (!CPU_ISSET(my_cpu, &cpus)) continue;
if (num == 0) break;
--num;
}
if (my_cpu == CPU_SETSIZE) {
fprintf(stderr, "error: more threads than cpus available\n");
exit(1);
}
CPU_ZERO(&cpus);
CPU_SET(my_cpu, &cpus);
if (sched_setaffinity(0, sizeof(cpus), &cpus)) {
perror("sched_setaffinity");
exit(1);
}
}
if (args->x.run_test_type == RUN_CHASE) {
// generate chases -- using a different mixer index for every
// thread and for every parallel chase within a thread
unsigned parallelism = args->x.chase->parallelism;
for (unsigned par = 0; par < parallelism; ++par) {
if (args->x.use_longer_chase) {
args->x.cycle[par] = generate_chase_long(
args->x.genchase_args, parallelism * args->x.thread_num + par,
parallelism);
} else {
args->x.cycle[par] = generate_chase(
args->x.genchase_args, parallelism * args->x.thread_num + par);
}
}
// handle critword2 chases
if (strcmp(args->x.chase->name, "critword2") == 0) {
size_t offset = strtoul(args->x.extra_args, 0, 0);
char *p = args->x.cycle[0];
char *q = p;
do {
char *next = *(char **)p;
*(void **)(p + offset) = next + offset;
p = next;
} while (p != q);
}
// handle critword chases
if (strcmp(args->x.chase->name, "critword") == 0) {
size_t offset = strtoul(args->x.extra_args, 0, 0);
char *p = args->x.cycle[0];
char *q = p;
do {
char *next = *(char **)p;
*(void **)(p + offset) = next;
*(void **)p = p + offset;
p = next;
} while (p != q);
}
// now flush our caches
if (args->x.cache_flush_size) {
size_t nr_elts = args->x.cache_flush_size / sizeof(size_t);
size_t *p = args->x.flush_arena;
size_t sum = 0;
while (nr_elts) {
sum += *p;
++p;
--nr_elts;
}
args->x.dummy += sum;
}
} else {
if (verbosity > 2)
printf("thread_start(%d) memload generate buffers\n", args->x.thread_num);
// generate buffers
args->x.load_arena = (char *)alloc_arena_mmap(
page_size, use_thp,
args->x.load_total_memory + args->x.load_offset,
-1) + args->x.load_offset;
memset(args->x.load_arena, 1,
args->x.load_total_memory); // ensure pages are mapped
}
if (verbosity > 2)
printf("thread_start(%d) wait and/or wake up everyone\n",
args->x.thread_num);
// wait and/or wake up everyone if we're all ready
pthread_mutex_lock(&wait_mutex);
--nr_to_startup;
if (nr_to_startup) {
pthread_cond_wait(&wait_cond, &wait_mutex);
} else {
pthread_cond_broadcast(&wait_cond);
}
pthread_mutex_unlock(&wait_mutex);
if (args->x.run_test_type == RUN_CHASE) {
if (verbosity > 2) printf("thread_start: C(%d)\n", args->x.thread_num);
args->x.chase->fn(data);
} else {
if (verbosity > 2) printf("thread_start: M(%d)\n", args->x.thread_num);
args->x.memload->fn(data);
}
return NULL;
}
static void timestamp(void) {
if (!print_timestamp) return;
struct timeval tv;
gettimeofday(&tv, NULL);
printf("%.6f ", tv.tv_sec + tv.tv_usec / 1000000.);
}
int main(int argc, char **argv) {
char *p;
int c;
size_t i;
size_t nr_threads = DEF_NR_THREADS;
size_t nr_samples = DEF_NR_SAMPLES;
size_t cache_flush_size = DEF_CACHE_FLUSH;
size_t delay = DEF_DELAY;
size_t offset = DEF_OFFSET;
bool use_longer_chase = false;
int print_average = 0;
const char *extra_args = NULL;
const char *chase_optarg = chases[0].name;
const chase_t *chase = &chases[0];
const char *memload_optarg = memloads[0].name;
const chase_t *memload = &memloads[0];
test_type_t run_test_type =
RUN_CHASE; // RUN_CHASE, RUN_BANDWIDTH, RUN_CHASE_LOADED
struct generate_chase_common_args genchase_args;
default_page_size = page_size = get_native_page_size();
genchase_args.total_memory = DEF_TOTAL_MEMORY;
genchase_args.stride = DEF_STRIDE;
genchase_args.tlb_locality = DEF_TLB_LOCALITY * default_page_size;
genchase_args.gen_permutation = gen_random_permutation;
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
while ((c = getopt(argc, argv, "ac:d:l:F:p:HLm:n:oO:S:s:T:t:vXyW:")) != -1) {
switch (c) {
case 'a':
print_average = 1;
break;
case 'c':
chase_optarg = optarg;
p = strchr(optarg, ':');
if (p == NULL) p = optarg + strlen(optarg);
for (i = 0; i < sizeof(chases) / sizeof(chases[0]); ++i) {
if (strncmp(optarg, chases[i].name, p - optarg) == 0) {
break;
}
}
if (i == sizeof(chases) / sizeof(chases[0])) {
fprintf(stderr, "Error: not a recognized chase name: %s\n", optarg);
goto usage;
}
chase = &chases[i];
if (strncmp("chaseload", chases[i].name, 12) == 0) {
run_test_type = RUN_CHASE_LOADED;
if (verbosity > 0) {
fprintf(stdout,
"Info: Loaded Latency chase selected. A -l memload can be "
"used to select a specific memory load\n");
}
break;
}
if (run_test_type == RUN_BANDWIDTH) {
fprintf(stderr,
"Error: When using -l memload, the only valid -c selection "
"is chaseload (ie. loaded latency)\n");
goto usage;
}
if (chase->requires_arg) {
if (p[0] != ':' || p[1] == 0) {
fprintf(stderr,
"Error: that chase requires an argument:\n-c %s\t%s\n",
chase->usage1, chase->usage2);
exit(1);
}
extra_args = p + 1;
} else if (*p != 0) {
fprintf(stderr,
"Error: that chase does not take an argument:\n-c %s\t%s\n",
chase->usage1, chase->usage2);
exit(1);
}
break;
case 'd':
delay = strtoul(optarg, &p, 0);
if (*p) {
fprintf(stderr, "Error: delay must be a non-negative integer\n");
exit(1);
}
break;
case 'F':
if (parse_mem_arg(optarg, &cache_flush_size)) {
fprintf(stderr,
"Error: cache_flush_size must be a non-negative integer "
"(suffixed with k, m, or g)\n");
exit(1);
}
break;
case 'p':
if (parse_mem_arg(optarg, &page_size)) {
fprintf(stderr,
"Error: page_size must be a non-negative integer (suffixed "
"with k, m, or g)\n");
exit(1);
}
break;
case 'H':
use_thp = true;
break;
case 'l':
memload_optarg = optarg;
p = strchr(optarg, ':');
if (p == NULL) p = optarg + strlen(optarg);
for (i = 0; i < sizeof(memloads) / sizeof(memloads[0]); ++i) {
if (strncmp(optarg, memloads[i].name, p - optarg) == 0) {
break;
}
}
if (i == sizeof(memloads) / sizeof(memloads[0])) {
fprintf(stderr, "Error: not a recognized memload name: %s\n", optarg);
goto usage;
}
memload = &memloads[i];
if (run_test_type != RUN_CHASE_LOADED) {
run_test_type = RUN_BANDWIDTH;
if (verbosity > 0) {
fprintf(stdout,
"Memory Bandwidth test selected. For loaded latency, -c "
"chaseload must also be selected\n");
}
}
if (memload->requires_arg) {
if (p[0] != ':' || p[1] == 0) {
fprintf(stderr,
"Error: that memload requires an argument:\n-c %s\t%s\n",
memload->usage1, memload->usage2);
exit(1);
}
extra_args = p + 1;
} else if (*p != 0) {
fprintf(stderr,
"Error: that memload does not take an argument:\n-c %s\t%s\n",
memload->usage1, memload->usage2);
exit(1);
}
break;
case 'L':
use_longer_chase = true;
break;