-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpft.c
1666 lines (1419 loc) · 38.5 KB
/
pft.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 (c) 2006, 2014 SGI. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* # along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Originally: Christoph Lameter's "Page Fault Test" tool.
*
* Posted to LKML: http://lkml.org/lkml/2006/8/29/294
*
* Modified by Lee Schermerhorn for mem policy testing
* Change to allocate single large region before creating worker
* threads/tasks.
* Then, carve up the region, giving each worker a piece to fault in.
* This will cause the workers to contend for the cache line[s]
* holding the in-kernel memory policy structure, the zone locks
* and page lists, ...
* In multi-thread mode, the workers will also contend for the
* single test task's mmap semaphore.
*
* See usage below.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <numa.h>
#include <numaif.h>
#include <pthread.h>
#include <sched.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "version.h"
#if defined(USE_RUSAGE_THREAD) && !defined(RUSAGE_THREAD)
#define RUSAGE_THREAD 1
#endif
#ifdef USE_NOCLEAR
/*
* N.B., make sure this matches the value used in the 'noclear' kernel patch
*/
#define MPOL_MF_NOCLEAR (MPOL_MF_MOVE_ALL << 1)
#endif
struct test_rusage {
struct timespec wall_start;
struct timespec wall_end;
struct rusage ruse_start;
struct rusage ruse_end;
};
enum test_state {
TEST_CREATED = 0,
TEST_READY,
TEST_DONE,
TEST_REAPED,
};
static struct test_info {
pthread_t ptid;
pid_t pid; /* not worth a union */
volatile enum test_state state;
int idx;
int cpu; /* if bound */
char *mem; /* this test's memory segment */
struct test_rusage rusage;
} **test_info;
static pthread_attr_t thread_attributes;
static pthread_key_t tip_key;
static int pft_sched_policy = SCHED_FIFO; /* if we use it */
static struct rusage ruse_start; /* earliest worker's start rusage */
/*
* parent/child communication - shared anon segment
*
* We extend this struct by nr_tests - 1 struct test_info, where
* nr_tests = nr_proc + nr_thread, one of which will be 0.
*
* Why do we need both this array of test_info in the comm area
* and the array of pointers to allocated test_infos?
* Well, we don't really -- for this test.
* We need this array in the comm area for the multi-task tests,
* to communicate the results back to the parent/launch task.
* The array of pointers to test_infos allocated locally to the
* thread/task is a hold over from another multi-threaded tool
* whose thread setup infrastructure I cloned for this test.
* The measured loop of the test doesn't actually touch the test_info,
* so we could use the array in the comm area directly.
* However, I chose to keep the per test local test_info and then
* push the results back to the comm area at the end, in case we
* ever DO want to access the test_info in the test loop. That
* and I was too lazy to rip it out. It does complicate things, tho'.
*/
struct pft_comm {
volatile long go;
struct timespec wall_end;
struct timespec wall_start;
struct rusage rusage;
int shmid;
int abort;
struct test_info test_info[1];
} *comm;
#define CACHELINE_SIZE_DEFAULT (128)
/*
* fairly arbitrary limits
*/
#define MAX_TESTS 128
#define ROUND_UP(x,y) (((x) + (y) - 1) & ~((y)-1))
static char* OPTSTR = "ac:fhlm:n:ps:t:vzCFLNPSTVZ";
static char *usage = "\n\
Usage: %s [-afhpvzCFLMNPSTVZ] [-c <cachelines>] [-m <size>[KMGP]]\n\
[-s <sleep_seconds>] [{-n <nr_proc>|-t <nr_thread>}] [<tag>]\n\
Where:\n\
-h = show help/usage.\n\n\
-m <size> = total size of test region. Optional scale factor:\n\
K = kilo, M = mega, G = giga, P = pages\n\
-p = use vma/shared memPolicy; else use sys default policy.\n\
-z = bzero the per-thread memory area; else touch cachelines.\n\
-c <cachelines> = number of cachelines to touch if !-z.\n\
configured cacheline size: %d bytes.\n\
-l = mlock() the region instead of bzero or touch.\n"
#if 0
"\
-M = mmap() separate regions for each test task/thread to avoid\n\
anon_vma sharing.\n"
#endif
" -S = use SysV shared memory test area; else anonymous test memory.\n\
-L = SHM_LOCK the SysV shared memory test area. Implies -S\n\n\
-n <nr_proc> = number of test processes.\n\
-t <nr_thread> = number of threads.\n\
'-t' and '-n' are mutually exclusive options\n\
<nr_proc>|<nr_thread> should <= nr_cpus\n\
Each process/thread will touch <size>/<nr_*> memory.\n"
#if 0
"\
-F = force use of <nr_proc>|nr_thread > nr_cpus_allowed\n"
#endif
" -a = affinitize test processes/threads to cpus.\n\
-f = use SCHED_FIFO policy for tests.\n\
-Z = mmap() /dev/zero for anonymous regions\n"
#ifdef USE_NOCLEAR
" -C = request kernel not to Clear pages to eliminate this\n\
overhead. Requires special kernel patch.\n"
#endif
"\n -N = dump numa_maps at end of test\n\
-P = pause after test to examine maps\n\
-T = emit title/header line and 'tag' for plots.\n\
-s <sleep_seconds> = sleep delay at end of tests.\n\
-v = enable verbosity.\n\
-V = just emit version/build-stamp and exit.\n\n\
<tag> = annotation, e.g., for plots.\n\n\
";
size_t bytes; /* total size of test memory */
unsigned long nr_proc = 0; // TODO: make this default?
unsigned long nr_thread = 0;
unsigned long nr_tests;
long cachelines = 1;
int do_cpubind = 0;
int do_mempol = 0;
int do_numa_maps = 0;
int do_pause = 0;
int do_shm = 0;
int do_shmlock = 0;
int do_title = 0;
int verbose = 0;
int do_bzero = 0;
int use_sched_fifo = 0;
int force = 0;
long sleepsec = 0;
int do_mlock = 0;
int no_clear = 0; /* define even when !defined(USE_NOCLEAR) */
size_t multimap = 0L; /* mmap() per test area */
int mmap_fd; /* for /dev/zero mappings */
int mmap_flags = MAP_ANONYMOUS; /* default */
int launch_cpu;
long faults;
long pages;
long pages_per_test;
size_t bytes_per_test;
char *test_memory;
char **test_memories;
pid_t lpid; /* launch() pid */
int rusage_who;
void
perrorx(char *mesg)
{
perror(mesg);
if (comm)
comm->abort++;
exit(1);
}
void
vprint(int level, char *format, ...)
{
va_list ap;
if (level > verbose)
goto out;
va_start(ap, format);
(void)vfprintf(stderr, format, ap);
fflush(stderr);
out:
va_end(ap);
return;
}
/*
* ============================================================================
* Support for determining allowed cpus, for distributing children over allowed
* cpus.
*/
typedef enum{false = 0, true} bool;
const unsigned int BITSPERINT = 8 * sizeof(int);
static int max_cpu_allowed = -1;
static int nr_cpus_allowed = -1;
static struct bitmask *cpus_allowed_mask; /* bit map */
static unsigned int *cpus_allowed; /* dense array */
/*
* ----------------------------------------------------------------------------
*/
static bool
cpu_allowed(int cpuid)
{
return numa_bitmask_isbitset(cpus_allowed_mask, cpuid);
}
/*
* ----------------------------------------------------------------------------
*/
/*
* cpus_allowed_init(): fetch task's Cpus_allowed mask using libnuma API
*/
#define CA_STRLEN 4096 /* large enough for most ? */
void
cpus_allowed_init(void)
{
unsigned int *prev_cpus_allowed;
int ret, max_cpus_allowed;
int i, cpuid, prev_nr_cpus_allowed;
/*
* libnuma wrapper returns bitmask
*/
cpus_allowed_mask = numa_allocate_cpumask();
ret = numa_sched_getaffinity(lpid, cpus_allowed_mask);
if (ret == -1)
perrorx("Can't fetch sched affinity");
// broken in numactl 2.0.3: wrong symbol name [numa_num_tread_cpus()]
// in sources and numa(3) man page. Requires patched libnuma and numa(3).
max_cpus_allowed = numa_num_task_cpus();
prev_cpus_allowed = cpus_allowed; /* for re-init */
prev_nr_cpus_allowed = nr_cpus_allowed;
/*
* Populate cpus_allowed[] dense array.
*/
nr_cpus_allowed = 0;
cpus_allowed = calloc(sizeof(int), max_cpus_allowed);
if (!cpus_allowed)
perrorx("Can't allocate cpus_allowed array");
for (i=0; i < max_cpus_allowed; ++i) {
if (cpu_allowed(i)) {
max_cpu_allowed = i;
cpus_allowed[nr_cpus_allowed++] = i;
}
}
/*
* TODO: on re-init, notify application of change in cpus_allowed:
* E.g., redistribute tasks over new set of allowed cpus.
*/
}
/*
* ----------------------------------------------------------------------------
*/
/*
* cpus_init() -- fetch/parse allowed cpus
*/
static void
cpus_init()
{
cpus_allowed_init();
launch_cpu = cpus_allowed[0];
}
/*
* ============================================================================
*/
/*
* cachline_size_init() - fetch cacheline size from kernel, if supported
*/
size_t cacheline_size = 0;
void
cacheline_size_init(void)
{
if(!cacheline_size) {
#ifdef _SC_LEVEL1_DCACHE_LINESIZE
long cls = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
if (cls > 0)
cacheline_size = (size_t)cls;
else
#endif
cacheline_size = CACHELINE_SIZE_DEFAULT;
}
}
/*
* Does the kernel support RUSAGE_THREAD?
*/
void
check_rusage_thread(void)
{
#ifdef RUSAGE_THREAD
struct rusage rusage;
if(!getrusage(RUSAGE_THREAD, &rusage)) {
rusage_who = RUSAGE_THREAD;
vprint(1, "Using RUSAGE_THREAD\n");
}
#endif
}
/*
* pagesize_init() - fetch pagesize when needed
*/
ssize_t pagesize = -1;
#define PAGE_ALIGN(addr) ROUND_UP((addr), pagesize)
static void
pagesize_init(void)
{
if (pagesize == -1) {
pagesize = sysconf(_SC_PAGESIZE);
if (pagesize <= 0) {
perror("sysconf(_SC_PAGESIZE) failed");
exit(1);
}
}
}
/*
* use_dev_zero() -- open /dev/zero for use by pft_mmap()
*/
static void
use_dev_zero(void)
{
int dzfd = open("/dev/zero", O_RDWR);
if (dzfd < 0)
perrorx("open of /dev/zero failed ");
mmap_fd = dzfd;
mmap_flags = 0; /* zap MAP_ANONYMOUS */
}
/*
* pft_mmap() - "allocate" page aligned memory using mmap(ANON)
* flags: MAP_PRIVATE or MAP_SHARED
*/
static void *
pft_mmap(size_t size, int flags, void *where)
{
char *addr;
pagesize_init();
if (!size)
size = pagesize;
if (where)
flags |= MAP_FIXED;
/*
* mmap(2) page aligns size/len
*/
addr = (char *)mmap(where, size,
PROT_READ|PROT_WRITE,
flags|mmap_flags,
mmap_fd, 0);
if (addr == MAP_FAILED)
addr = NULL; /* like valloc(3) */
return addr;
}
/*
* valloc_private() - "allocate" page-aligned, private anon memory.
*/
static void *
valloc_private(size_t size)
{
return pft_mmap(size, MAP_PRIVATE, NULL);
}
/*
* valloc_shared() - "allocate" page-aligned, shared anon memory.
*/
static void *
valloc_shared(size_t size)
{
return pft_mmap(size, MAP_SHARED, NULL);
}
/*
* pft_free() -- free memory "allocated" via pft_mmap() or valloc_*()
* need 'size' for munmap
*/
static void
pft_free(void *mem, size_t size)
{
munmap(mem, size);
}
/*
* bind_to_cpu() - bind calling thread [main program] to specified
* cpu before creating per cpu thread.
* Thread id 'tid' for debug printing only
*/
static int
bind_to_cpu(int cpu, int tid)
{
cpu_set_t cpu_mask;
if (!do_cpubind)
return 1;
if (nr_cpus_allowed == 1)
return 1; /* why bother? */
CPU_ZERO(&cpu_mask);
CPU_SET(cpu, &cpu_mask);
if (sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask) == -1) {
perror("sched_setaffinity");
return 0; /* assume no such cpu? */
}
vprint(2, "worker %d bound to cpu %d\n", tid, cpu);
return 1;
}
/*
* borrowed from memtoy
* size_kmgp() -- convert ascii arg to numeric and scale as requested
*/
#define BOGUS_SIZE ((size_t)-1) /* memtoy */
#define KILO_SHIFT 10 /* shift count to multiply by 1K */
static size_t
size_kmgp(char *arg)
{
size_t argval;
char *next;
argval = strtoul(arg, &next, 0);
if (*next == '\0')
return argval;
switch (tolower(*next)) {
case 'p': /* pages */
argval *= pagesize;
break;
case 'k':
argval <<= KILO_SHIFT;
break;
case 'm':
argval <<= KILO_SHIFT * 2;
break;
case 'g':
argval <<= KILO_SHIFT * 3;
break;
default:
return BOGUS_SIZE; /* bogus chars after number */
}
return argval;
}
/*
* choose an arbitrary priority for pft_sched_policy tests.
* use mid-point of pft_sched_policy priority range.
*/
static int get_run_priority(void)
{
int pri_min, pri_max, sched_pri;
pri_min = sched_get_priority_min(pft_sched_policy);
if (pri_min < 0) {
perror("sched_get_priority_min");
return 0;
}
pri_max = sched_get_priority_max(pft_sched_policy);
if (pri_max < 0) {
perror("sched_get_priority_max");
return 0;
}
sched_pri = (pri_min + pri_max) / 2;
vprint(2, "%s returning sched_pri = %d\n", __FUNCTION__, sched_pri);
return sched_pri;
}
/*
* Set scheduler for task to SCHED_FIFO. For launch() task/thread
* [test == 0], augment the priority by 1 to retain control while
* starting workers on each cpu.
*
* Returns:
* !0 on success
* 0 on failure.
*
*/
static void set_task_scheduler(int test)
{
struct sched_param sched_params;
if (!use_sched_fifo)
return;
memset(&sched_params, 0, sizeof(sched_params));
sched_params.sched_priority = get_run_priority() + !test;
vprint(2, "setting test %d scheduler to %d @ %d\n", test,
pft_sched_policy, sched_params.sched_priority);
if (sched_setscheduler(0, pft_sched_policy, &sched_params)) {
perror("sched_setscheduler");
}
}
/*
* Set scheduler to SCHED_FIFO and priorty high to minimize
* variability from other processes during test loop.
* Returns:
* !0 on success
* 0 on failure.
*
*/
static int create_thread_attributes(void)
{
pthread_attr_t *attr = &thread_attributes;
struct sched_param sched_params;
if (pthread_attr_init(attr)) {
perror("pthread_attr_init");
return 0;
}
if (!use_sched_fifo)
return 1;
if (pthread_attr_setschedpolicy(attr, pft_sched_policy)) {
perror("pthread_attr_setschedpolicy");
return 0;
}
sched_params.sched_priority = get_run_priority();
vprint(2, "setting thread scheduler to %d @ %d\n", pft_sched_policy,
sched_params.sched_priority);
if (pthread_attr_setschedparam(attr, &sched_params)) {
perror("pthread_attr_setschedparam");
return 0;
}
return 1;
}
/*
* show_tip_node() -- fetch numa node id of thread info struct
*/
static void show_tip_node(struct test_info *tip)
{
#ifdef MPOL_F_NODE
int rc, node;
rc = get_mempolicy(&node, NULL, 0, tip, MPOL_F_NODE|MPOL_F_ADDR);
if (rc)
return;
vprint(2, "test info struct for test %d [cpu %d] on node %d\n",
tip->idx, tip->cpu, node);
#endif
}
/*
* create_test_info() - allocate private, page-aligned per process/thread
* info for test 'tidx'. For NUMA platforms, the test info struct should
* be allocated locally to the cpu where the thread is running at the time.
* At end of test, the worker thread/task will dump its test info into the
* shared communication area.
* If the '-a' [affinitize] option was specified, the thread or process
* should already be bound to its run time cpu.
*/
struct test_info *
create_test_info(int tidx)
{
struct test_info *tip;
if (tidx) {
tip = test_info[tidx] = valloc_private(sizeof(*tip));
if (!tip)
perrorx("valloc_private(test_info)");
} else
tip = comm->test_info; /* use comm area directly for test 0 */
bzero(tip, sizeof(*tip));
tip->idx = tidx;
return tip;
}
char *
alloc_shm(size_t shmlen)
{
char *p, *locked = "";
vprint(3, "Try to allocate TOTAL shm segment of %ld bytes\n", shmlen);
if ((comm->shmid = shmget(IPC_PRIVATE, shmlen, SHM_R|SHM_W)) == -1)
perrorx("shmget failed");
p = (char*)shmat(comm->shmid, (void*)0, SHM_R|SHM_W);
if (do_shmlock) {
if (shmctl(comm->shmid, SHM_LOCK, NULL) == -1)
perrorx("shmctl(SHM_LOCK) failed");
locked = "/SHM_LOCKED";
}
vprint(3, "shm created, attached @ adr: 0x%lx\n", locked, (long)p);
return p;
}
/*
* do_mbind() -- apply vma policy to test memory region
* Use "explicit local" policy -- MPOL_PREFERRED w/ NULL nodemask
*/
void
do_mbind(char *start, size_t length)
{
if (!do_mempol)
return;
if (mbind(start, length, MPOL_PREFERRED, (void *)0, 0, 0) < 0)
perrorx("mbind failed");
}
#ifdef USE_NOCLEAR
void
do_noclear(char *start, size_t length)
{
if (!no_clear)
return;
/*
* length, policy, nodemask/maxnodes all ignored.
* this is just a "backdoor" to set "no clear" on
* the vma, if supported
*/
if (mbind(start, length, MPOL_PREFERRED, (void *)0, 0, MPOL_MF_NOCLEAR) < 0)
perrorx("mbind 'NOCLEAR failed/not supported");
vprint(1, "enabled 'NOCLEAR' on test memory\n");
}
#else
#define do_noclear(P, L) /* no-op, but should never be invoked */
#endif
/*
* alloc_test_memory: allocate the test memory region and divide up between
* threads.
*/
void
alloc_test_memory(void)
{
char *p = NULL;
int j;
if (do_shm) {
if (p = alloc_shm(bytes)) {
do_mbind(p, bytes);
do_noclear(p, bytes);
}
} else {
/*
* mmap()'ed test area[s].
*/
if (!multimap) {
/*
* one large test area => single anon_vma
*/
if (p = valloc_private(bytes)) {
do_mbind(p, bytes);
do_noclear(p, bytes);
}
} else {
#if 0
/ Not Ready for Prime Time -- maybe never
/*
* multimap: per test mmap area => separate anon_vmas
*/
void *where;
size_t abytes = bytes + (nr_tests + 1) * pagesize;
size_t tbytes = bytes_per_test + pagesize;
/*
* reserve VA range with room for "holes"
*/
where = valloc_private(abytes);
if(!where)
perrorx("valloc_private() of multimap region failed");
vprint(3, "multimap va range: 0x%lx - 0x%lx\n", where, where+abytes);
if (munmap(where, pagesize))
perrorx("munmap() of 1st test region page failed");
where += pagesize;
for (j = 0; j < nr_tests; ++j) {
/*
* unmap per test region + a 1 page hole
*/
if (munmap(where, tbytes))
perrorx("munmap() of per test region failed");
/*
* map per test region below the hole
*/
if (p = pft_mmap(bytes_per_test, MAP_PRIVATE, where)) {
vprint(3, "test %d memory @ 0x%lx - 0x%lx\n",
j, p, p+bytes_per_test);
do_mbind(p, bytes_per_test);
do_noclear(p, bytes_per_test);
test_memories[j] = p;
where += tbytes; /* advance past the hole */
} else
goto err;
}
#endif
}
}
if (p == 0) {
err:
printf("malloc of %Ld bytes failed.\n", bytes);
exit(1);
}
if (!multimap) {
test_memory = p;
vprint(3, "test memory @ 0x%lx\n", test_memory);
}
}
/*
* calc_elapsed_time() -- elapsed "wall clock" time
*/
static double
calc_elapsed_time(struct timespec *ws, struct timespec *we)
{
struct timespec wall;
wall.tv_sec = we->tv_sec - ws->tv_sec;
wall.tv_nsec = we->tv_nsec - ws->tv_nsec;
if (wall.tv_nsec <0 ) {
wall.tv_sec--;
wall.tv_nsec += 1000000000;
}
if (wall.tv_nsec >1000000000) {
wall.tv_sec++;
wall.tv_nsec -= 1000000000;
}
return ((double) wall.tv_sec + (double) wall.tv_nsec / 1000000000.0);
}
/*
* calc_cpu_time() -- user and/or system time for all workers
*/
static double
calc_cpu_time(struct timeval *tvp)
{
return ((double) tvp->tv_sec + (double) tvp->tv_usec / 1000000.0);
}
/*
* test_to_cpu() - distribute threads/processes, round robin, over cpus
*
* cpu_offset: used to prevent other worker threads from binding to
* the launch cpu as that causes startup problems.
*/
static int cpu_offset = 0;
static int
test_to_cpu(int t)
{
return (cpus_allowed[(t + cpu_offset) % nr_cpus_allowed]);
}
//TODO : temp for debug
void
show_rusage(char *tag, struct rusage *rusage)
{
fprintf(stderr, "%s %8d.%06d %8d.%06d %8d %8d\n", tag,
rusage->ru_utime.tv_sec, rusage->ru_utime.tv_usec,
rusage->ru_stime.tv_sec, rusage->ru_stime.tv_usec,
rusage->ru_minflt, rusage->ru_majflt);
}
/*
* actual measured test loop
*/
void
pft_loop(struct test_info *tip)
{
char *pe, *p = tip->mem;
int cl;
/*
* Start Measurement Interval and snap initial rusage.
* Note preemption window between 'gettime and getrusage
* that can skew results if we get preempted there.
* Fortunately, we only use the wall clock time to
* select the earliest/latest workers' rusage when
* running mult-thread test on kernel that doesn't
* support RUSAGE_THREAD.
*/
clock_gettime(CLOCK_REALTIME, &tip->rusage.wall_start);
getrusage(rusage_who, &tip->rusage.ruse_start);
if (do_mlock) {
mlock(p, bytes_per_test);
vprint(2, " mlocked\n");
} else if (do_bzero) {
bzero(p, bytes_per_test);
vprint(2, " zeroed\n");
} else {
/*
* Touch 'cachelines' every pagesize bytes.
* Use 'write' access to force anon page allocation.
* TODO: if we decide to add page cache [mapped file]
* tests, may want to select read or write access
* to test page cache minor read faults vs COW
*/
for(pe = p + bytes_per_test; p < pe; p += pagesize)
for(cl = 0; cl < cachelines; cl++)
p[cl * cacheline_size] = 'r';
}
/*
* End Thread Measurement Interval and snap ending rusage.
* Note preemption window.
*/
getrusage(rusage_who, &tip->rusage.ruse_end);
clock_gettime(CLOCK_REALTIME, &tip->rusage.wall_end);
}
void
check_wall_time(int id, struct timespec *tsp, char *what)
{
if (tsp->tv_sec)
return;
vprint(0, "!!! Test %d - %s time is zero\n", id, what);
verbose = 3;
}
/*
* per test "main-line" function
*/
void*
test_main(void *arg)
{
struct test_info *tip;
struct timespec sleepfor = { 0, 2500L }; /* 0.0000025 sec */
long id;
tip = (struct test_info *)arg;
id = tip->idx;
tip->state = TEST_READY;
/*
* push local test_info to comm area so that launch()
* sees state and ptid/pid.
*/
comm->test_info[id] = *tip;
while(!comm->go) {
//TODO: may need one of these if nr_tests > nr_cpus ...
#if 0
#if 0
if (tip->cpu == launch_cpu)
nanosleep(&sleepfor, NULL); /* relax... */
#else
sched_yield();
#endif
#endif
}
vprint (2, "test %d running\n", id);
pft_loop(tip);
if (sleepsec) {
vprint (2, "test %d sleeping\n", id);
sleep(sleepsec);
}
check_wall_time(tip->idx, &tip->rusage.wall_start, "test_main wall_start");
check_wall_time(tip->idx, &tip->rusage.wall_end, "test_main wall_end");
vprint (2, "test %d done\n", id);
tip->state = TEST_DONE;
/*
* push results back into comm area
*/
comm->test_info[id] = *tip;
if (nr_thread)
pthread_exit(0);
else {
comm = NULL; /* don't cleanup */
exit(0);
}
}
/*
* start workers -- start nr_tests-1 threads or tasks for test, distributed
* across cpus. launch() thread/task will run a test as well, for a total
* of nr_tests.
* Allocate test info structs local to test's cpu--i.e., after binding.
* Return !0 [nr tests created] on success; 0 on failure;
*/
static int
start_workers(void)