-
Notifications
You must be signed in to change notification settings - Fork 8
/
interbench.c
1782 lines (1563 loc) · 42.4 KB
/
interbench.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
/*******************************************
*
* Interbench - Interactivity benchmark
*
* Author: Con Kolivas <[email protected]>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*******************************************/
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64 /* Large file support */
#define INTERBENCH_VERSION "0.31"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <time.h>
#include <errno.h>
#include <semaphore.h>
#include <pthread.h>
#include <math.h>
#include <fenv.h>
#include <signal.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "interbench.h"
#define MAX_UNAME_LENGTH 100
#define MAX_LOG_LENGTH ((MAX_UNAME_LENGTH) + 4)
#define MIN_BLK_SIZE 1024
#define DEFAULT_RESERVE 64
#define MB (1024 * 1024) /* 2^20 bytes */
#define KB 1024
#define MAX_MEM_IN_MB (1024 * 64) /* 64 GB */
struct user_data {
unsigned long loops_per_ms;
unsigned long ram, swap;
unsigned long filesize;
int duration;
int do_rt;
int bench_nice;
int load_nice;
unsigned long custom_run;
unsigned long custom_interval;
unsigned long cpu_load;
char logfilename[MAX_LOG_LENGTH];
int log;
char unamer[MAX_UNAME_LENGTH];
char datestamp[13];
cpu_set_t cpumask;
FILE *logfile;
} ud = {
.duration = 30,
.log = 1,
};
/* Pipes main to/from load and bench processes */
static int m2l[2], l2m[2], m2b[2], b2m[2];
/* Which member of becnhmarks is used when not benchmarking */
#define NOT_BENCHING (THREADS)
#define CUSTOM (THREADS - 1)
/*
* To add another load or a benchmark you need to increment the value of
* THREADS, add a function prototype for your function and add an entry to
* the threadlist. To specify whether the function is a benchmark or a load
* set the benchmark and/or load flag as appropriate. The basic requirements
* of a new load can be seen by using emulate_none as a template.
*/
void emulate_none(struct thread *th);
void emulate_audio(struct thread *th);
void emulate_video(struct thread *th);
void emulate_x(struct thread *th);
void emulate_game(struct thread *th);
void emulate_burn(struct thread *th);
void emulate_write(struct thread *th);
void emulate_read(struct thread *th);
void emulate_ring(struct thread *th);
void emulate_compile(struct thread *th);
void emulate_memload(struct thread *th);
void emulate_hackbench(struct thread *th);
void emulate_custom(struct thread *th);
struct thread threadlist[THREADS] = {
{.label = "None", .name = emulate_none, .load = 1, .rtload = 1},
{.label = "Audio", .name = emulate_audio, .bench = 1, .rtbench = 1},
{.label = "Video", .name = emulate_video, .bench = 1, .rtbench = 1, .load = 1, .rtload = 1},
{.label = "X", .name = emulate_x, .bench = 1, .load = 1, .rtload = 1},
{.label = "Gaming", .name = emulate_game, .nodeadlines = 1, .bench = 1},
{.label = "Burn", .name = emulate_burn, .load = 1, .rtload = 1},
{.label = "Write", .name = emulate_write, .load = 1, .rtload = 1},
{.label = "Read", .name = emulate_read, .load = 1, .rtload = 1},
{.label = "Ring", .name = emulate_ring, .load = 1, .rtload = 1},
{.label = "Compile", .name = emulate_compile, .load = 1, .rtload = 1},
{.label = "Memload", .name = emulate_memload, .load = 1, .rtload = 1},
{.label = "Hack", .name = emulate_hackbench, .load = 0, .rtload = 0},
{.label = "Custom", .name = emulate_custom}, /* Leave custom as last entry */
};
void init_sem(sem_t *sem);
void init_all_sems(struct sems *s);
void initialise_thread(int i);
void start_thread(struct thread *th);
void stop_thread(struct thread *th);
void terminal_error(const char *name)
{
fprintf(stderr, "\n");
perror(name);
exit (1);
}
void terminal_fileopen_error(FILE *fp, char *name)
{
if (fclose(fp) == -1)
terminal_error("fclose");
terminal_error(name);
}
#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
#endif
unsigned long long get_nsecs(struct timespec *myts)
{
if (clock_gettime(CLOCK_MONOTONIC_RAW, myts))
terminal_error("clock_gettime");
return (myts->tv_sec * 1000000000 + myts->tv_nsec );
}
unsigned long get_usecs(struct timespec *myts)
{
if (clock_gettime(CLOCK_MONOTONIC_RAW, myts))
terminal_error("clock_gettime");
return (myts->tv_sec * 1000000 + myts->tv_nsec / 1000 );
}
void set_fifo(int prio)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = prio;
if (sched_setscheduler(0, SCHED_FIFO, &sp) == -1) {
if (errno != EPERM)
terminal_error("sched_setscheduler");
}
}
void set_mlock(void)
{
int mlockflags;
mlockflags = MCL_CURRENT | MCL_FUTURE;
mlockall(mlockflags); /* Is not critical if this fails */
}
void set_munlock(void)
{
if (munlockall() == -1)
terminal_error("munlockall");
}
void set_thread_fifo(pthread_t pthread, int prio)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = prio;
if (pthread_setschedparam(pthread, SCHED_FIFO, &sp) == -1)
terminal_error("pthread_setschedparam");
}
void set_normal(void)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = 0;
if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
fprintf(stderr, "Weird, could not unset RT scheduling!\n");
}
}
void set_nice(int prio)
{
if (setpriority(PRIO_PROCESS, 0, prio) == -1)
terminal_error("setpriority");
}
int test_fifo(void)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = 99;
if (sched_setscheduler(0, SCHED_FIFO, &sp) == -1) {
if (errno != EPERM)
terminal_error("sched_setscheduler");
goto out_fail;
}
if (sched_getscheduler(0) != SCHED_FIFO)
goto out_fail;
set_normal();
return 1;
out_fail:
set_normal();
return 0;
}
void set_thread_normal(pthread_t pthread)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = 0;
if (pthread_setschedparam(pthread, SCHED_OTHER, &sp) == -1)
terminal_error("pthread_setschedparam");
}
void sync_flush(void)
{
if ((fflush(NULL)) == EOF)
terminal_error("fflush");
sync();
sync();
sync();
}
unsigned long compute_allocable_mem(void)
{
unsigned long total = ud.ram + ud.swap;
unsigned long usage = ud.ram * 110 / 100 ;
/* Leave at least DEFAULT_RESERVE free space and check for maths overflow. */
if (total - DEFAULT_RESERVE < usage)
usage = total - DEFAULT_RESERVE;
usage /= 1024; /* to megabytes */
if (usage > 2930)
usage = 2930;
return usage;
}
void burn_loops(unsigned long loops)
{
unsigned long i;
/*
* We need some magic here to prevent the compiler from optimising
* this loop away. Otherwise trying to emulate a fixed cpu load
* with this loop will not work.
*/
for (i = 0 ; i < loops ; i++)
asm volatile("" : : : "memory");
}
/* Use this many usecs of cpu time */
void burn_usecs(unsigned long usecs)
{
unsigned long ms_loops;
ms_loops = ud.loops_per_ms / 1000 * usecs;
burn_loops(ms_loops);
}
void microsleep(unsigned long long usecs)
{
struct timespec req, rem;
rem.tv_sec = rem.tv_nsec = 0;
req.tv_sec = usecs / 1000000;
req.tv_nsec = (usecs - (req.tv_sec * 1000000)) * 1000;
continue_sleep:
if ((nanosleep(&req, &rem)) == -1) {
if (errno == EINTR) {
if (rem.tv_sec || rem.tv_nsec) {
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
goto continue_sleep;
}
goto out;
}
terminal_error("nanosleep");
}
out:
return;
}
/*
* Yes, sem_post and sem_wait shouldn't return -1 but they do so we must
* handle it.
*/
void post_sem(sem_t *s)
{
retry:
if ((sem_post(s)) == -1) {
if (errno == EINTR)
goto retry;
terminal_error("sem_post");
}
}
void wait_sem(sem_t *s)
{
retry:
if ((sem_wait(s)) == -1) {
if (errno == EINTR)
goto retry;
terminal_error("sem_wait");
}
}
int trywait_sem(sem_t *s)
{
int ret;
retry:
if ((ret = sem_trywait(s)) == -1) {
if (errno == EINTR)
goto retry;
if (errno != EAGAIN)
terminal_error("sem_trywait");
}
return ret;
}
ssize_t Read(int fd, void *buf, size_t count)
{
ssize_t retval;
retry:
retval = read(fd, buf, count);
if (retval == -1) {
if (errno == EINTR)
goto retry;
terminal_error("read");
}
return retval;
}
ssize_t Write(int fd, const void *buf, size_t count)
{
ssize_t retval;
retry:
retval = write(fd, &buf, count);
if (retval == -1) {
if (errno == EINTR)
goto retry;
terminal_error("write");
}
return retval;
}
unsigned long periodic_schedule(struct thread *th, unsigned long run_usecs,
unsigned long interval_usecs, unsigned long long deadline)
{
unsigned long long latency, missed_latency;
unsigned long long current_time;
struct tk_thread *tk;
struct data_table *tb;
struct timespec myts;
latency = 0;
tb = th->dt;
tk = &th->tkthread;
current_time = get_usecs(&myts);
if (current_time > deadline + tk->slept_interval)
latency = current_time - deadline- tk->slept_interval;
/* calculate the latency for missed frames */
missed_latency = 0;
current_time = get_usecs(&myts);
if (interval_usecs && current_time > deadline + interval_usecs) {
/* We missed the deadline even before we consumed cpu */
unsigned long intervals;
deadline += interval_usecs;
intervals = (current_time - deadline) /
interval_usecs + 1;
tb->missed_deadlines += intervals;
missed_latency = intervals * interval_usecs;
deadline += intervals * interval_usecs;
tb->missed_burns += intervals;
goto bypass_burn;
}
burn_usecs(run_usecs);
current_time = get_usecs(&myts);
tb->achieved_burns++;
/*
* If we meet the deadline we move the deadline forward, otherwise
* we consider it a missed deadline and dropped frame etc.
*/
deadline += interval_usecs;
if (deadline >= current_time) {
tb->deadlines_met++;
} else {
if (interval_usecs) {
unsigned long intervals = (current_time - deadline) /
interval_usecs + 1;
tb->missed_deadlines += intervals;
missed_latency = intervals * interval_usecs;
deadline += intervals * interval_usecs;
if (intervals > 1)
tb->missed_burns += intervals;
} else {
deadline = current_time;
goto out_nosleep;
}
}
bypass_burn:
tk->sleep_interval = deadline - current_time;
post_sem(&tk->sem.start);
wait_sem(&tk->sem.complete);
out_nosleep:
/*
* Must add missed_latency to total here as this function may not be
* called again and the missed latency can be lost
*/
latency += missed_latency;
if (latency > tb->max_latency)
tb->max_latency = latency;
tb->total_latency += latency;
tb->sum_latency_squared += latency * latency;
tb->nr_samples++;
return deadline;
}
void initialise_thread_data(struct data_table *tb)
{
tb->max_latency =
tb->total_latency =
tb->sum_latency_squared =
tb->deadlines_met =
tb->missed_deadlines =
tb->missed_burns =
tb->nr_samples = 0;
}
void create_pthread(pthread_t * thread, pthread_attr_t * attr,
void * (*start_routine)(void *), void *arg)
{
if (pthread_create(thread, attr, start_routine, arg))
terminal_error("pthread_create");
}
void join_pthread(pthread_t th, void **thread_return)
{
if (pthread_join(th, thread_return))
terminal_error("pthread_join");
}
void emulate_none(struct thread *th)
{
sem_t *s = &th->sem.stop;
wait_sem(s);
}
#define AUDIO_INTERVAL (50000)
#define AUDIO_RUN (AUDIO_INTERVAL / 20)
/* We emulate audio by using 5% cpu and waking every 50ms */
void emulate_audio(struct thread *th)
{
unsigned long long deadline;
sem_t *s = &th->sem.stop;
struct timespec myts;
th->decasecond_deadlines = 1000000 / AUDIO_INTERVAL * 10;
deadline = get_usecs(&myts);
while (1) {
deadline = periodic_schedule(th, AUDIO_RUN, AUDIO_INTERVAL,
deadline);
if (!trywait_sem(s))
return;
}
}
/* We emulate video by using 40% cpu and waking for 60fps */
#define VIDEO_INTERVAL (1000000 / 60)
#define VIDEO_RUN (VIDEO_INTERVAL * 40 / 100)
void emulate_video(struct thread *th)
{
unsigned long long deadline;
sem_t *s = &th->sem.stop;
struct timespec myts;
th->decasecond_deadlines = 1000000 / VIDEO_INTERVAL * 10;
deadline = get_usecs(&myts);
while (1) {
deadline = periodic_schedule(th, VIDEO_RUN, VIDEO_INTERVAL,
deadline);
if (!trywait_sem(s))
return;
}
}
/*
* We emulate X by running for a variable percentage of cpu from 0-100%
* in 1ms chunks.
*/
void emulate_x(struct thread *th)
{
unsigned long long deadline;
sem_t *s = &th->sem.stop;
struct timespec myts;
th->decasecond_deadlines = 100;
deadline = get_usecs(&myts);
while (1) {
int i, j;
for (i = 0 ; i <= 100 ; i++) {
j = 100 - i;
deadline = periodic_schedule(th, i * 1000, j * 1000,
deadline);
deadline += i * 1000;
if (!trywait_sem(s))
return;
}
}
}
/*
* We emulate gaming by using 100% cpu and seeing how many frames (jobs
* completed) we can do in that time. Deadlines are meaningless with
* unlocked frame rates. We do not use periodic schedule because for
* this load because this never wants to sleep.
*/
#define GAME_INTERVAL (100000)
#define GAME_RUN (GAME_INTERVAL)
void emulate_game(struct thread *th)
{
unsigned long long deadline, current_time, latency;
sem_t *s = &th->sem.stop;
struct timespec myts;
struct data_table *tb;
tb = th->dt;
th->decasecond_deadlines = 1000000 / GAME_INTERVAL * 10;
while (1) {
deadline = get_usecs(&myts) + GAME_INTERVAL;
burn_usecs(GAME_RUN);
current_time = get_usecs(&myts);
/* use usecs instead of simple count for game burn statistics */
tb->achieved_burns += GAME_RUN;
if (current_time > deadline) {
latency = current_time - deadline;
tb->missed_burns += latency;
} else
latency = 0;
if (latency > tb->max_latency)
tb->max_latency = latency;
tb->total_latency += latency;
tb->sum_latency_squared += latency * latency;
tb->nr_samples++;
if (!trywait_sem(s))
return;
}
}
void *burn_thread(void *t)
{
struct thread *th;
sem_t *s;
long i = (long)t;
th = &threadlist[i];
s = &th->sem.stopchild;
while (1) {
burn_loops(ud.loops_per_ms);
if (!trywait_sem(s)) {
post_sem(s);
break;
}
}
return NULL;
}
/* Have ud.cpu_load threads burn cpu continuously */
void emulate_burn(struct thread *th)
{
sem_t *s = &th->sem.stop;
unsigned long i;
long t;
pthread_t burnthreads[ud.cpu_load];
t = th->threadno;
for (i = 0 ; i < ud.cpu_load ; i++)
create_pthread(&burnthreads[i], NULL, burn_thread,
(void*)(long) t);
wait_sem(s);
post_sem(&th->sem.stopchild);
for (i = 0 ; i < ud.cpu_load ; i++)
join_pthread(burnthreads[i], NULL);
}
/* Write a file the size of ram continuously */
void emulate_write(struct thread *th)
{
sem_t *s = &th->sem.stop;
FILE *fp;
char *name = "interbench.write";
void *buf = NULL;
struct stat statbuf;
unsigned long mem;
if (!(fp = fopen(name, "w")))
terminal_error("fopen");
if (stat(name, &statbuf) == -1)
terminal_fileopen_error(fp, "stat");
if (statbuf.st_blksize < MIN_BLK_SIZE)
statbuf.st_blksize = MIN_BLK_SIZE;
mem = ud.filesize / (statbuf.st_blksize / 1024); /* kilobytes to blocks */
if (!(buf = calloc(1, statbuf.st_blksize)))
terminal_fileopen_error(fp, "calloc");
if (fclose(fp) == -1)
terminal_error("fclose");
while (1) {
unsigned int i;
if (!(fp = fopen(name, "w")))
terminal_error("fopen");
if (stat(name, &statbuf) == -1)
terminal_fileopen_error(fp, "stat");
for (i = 0 ; i < mem; i++) {
if (fwrite(buf, statbuf.st_blksize, 1, fp) != 1)
terminal_fileopen_error(fp, "fwrite");
if (!trywait_sem(s))
goto out;
}
if (fclose(fp) == -1)
terminal_error("fclose");
}
out:
if (fclose(fp) == -1)
terminal_error("fclose");
if (remove(name) == -1)
terminal_error("remove");
sync_flush();
}
/* Read a file the size of ram continuously */
void emulate_read(struct thread *th)
{
sem_t *s = &th->sem.stop;
char *name = "interbench.read";
void *buf = NULL;
struct stat statbuf;
unsigned long bsize;
int tmp;
if ((tmp = open(name, O_RDONLY)) == -1)
terminal_error("open");
if (stat(name, &statbuf) == -1)
terminal_error("stat");
bsize = statbuf.st_blksize;
if (!(buf = malloc(bsize)))
terminal_error("malloc");
while (1) {
int rd;
/*
* We have to read the whole file before quitting the load
* to prevent the data being cached for the next read. This
* is also the reason the file is the size of physical ram.
*/
while ((rd = Read(tmp , buf, bsize)) > 0);
if(!trywait_sem(s))
return;
if (lseek(tmp, (off_t)0, SEEK_SET) == -1)
terminal_error("lseek");
}
}
#define RINGTHREADS (ud.cpu_load)
struct thread *ringthreads;
void *ring_thread(void *t)
{
unsigned int post_to;
struct thread *th;
struct sems *s;
int i;
i = (long)t;
th = &ringthreads[i];
s = &th->sem;
post_to = i + 1;
if (post_to == RINGTHREADS)
post_to = 0;
if (i == 0)
post_sem(&s->ready);
while (1) {
wait_sem(&s->start);
post_sem(&ringthreads[post_to].sem.start);
if (!trywait_sem(&s->stop))
goto out;
}
out:
post_sem(&ringthreads[post_to].sem.start);
post_sem(&s->complete);
return NULL;
}
/* Create a ring of 4 processes that wake each other up in a circle */
void emulate_ring(struct thread *th)
{
sem_t *s = &th->sem.stop;
unsigned int i;
ringthreads = calloc(sizeof(struct thread), RINGTHREADS + 1);
for (i = 0 ; i < RINGTHREADS ; i++) {
init_all_sems(&ringthreads[i].sem);
create_pthread(&ringthreads[i].pthread, NULL,
ring_thread, (void*)(long) i);
}
wait_sem(&ringthreads[0].sem.ready);
post_sem(&ringthreads[0].sem.start);
wait_sem(s);
for (i = 0 ; i < RINGTHREADS ; i++)
post_sem(&ringthreads[i].sem.stop);
for (i = 0 ; i < RINGTHREADS ; i++) {
wait_sem(&ringthreads[i].sem.complete);
join_pthread(ringthreads[i].pthread, NULL);
}
free(ringthreads);
}
/* We emulate a compile by running burn, write and read threads simultaneously */
void emulate_compile(struct thread *th)
{
sem_t *s = &th->sem.stop;
unsigned long i, threads[3];
for (i = 0 ; i < THREADS ; i++) {
if (!strcmp(threadlist[i].label, "Burn"))
threads[0] = i;
if (!strcmp(threadlist[i].label, "Write"))
threads[1] = i;
if (!strcmp(threadlist[i].label, "Read"))
threads[2] = i;
}
for (i = 0 ; i < 3 ; i++) {
if (!threads[i]) {
fprintf(stderr, "Can't find all threads for compile load\n");
exit(1);
}
}
for (i = 0 ; i < 3 ; i++) {
initialise_thread(threads[i]);
start_thread(&threadlist[threads[i]]);
}
wait_sem(s);
for (i = 0 ; i < 3 ; i++)
stop_thread(&threadlist[threads[i]]);
}
int *grab_and_touch (char *block[], int i)
{
block[i] = (char *) malloc(MB);
if (!block[i])
return NULL;
return (memset(block[i], 1, MB));
}
/* We emulate a memory load by allocating and torturing 110% of available ram */
void emulate_memload(struct thread *th)
{
sem_t *s = &th->sem.stop;
unsigned long touchable_mem, i;
char *mem_block[MAX_MEM_IN_MB];
void *success;
touchable_mem = compute_allocable_mem();
/* loop until we're killed, frobbing memory in various perverted ways */
while (1) {
for (i = 0; i < touchable_mem; i++) {
success = grab_and_touch(mem_block, i);
if (!success) {
touchable_mem = i-1;
break;
}
}
if (!trywait_sem(s))
goto out_freemem;
for (i = 0; i < touchable_mem; i++) {
memcpy(mem_block[i], mem_block[(i + touchable_mem / 2) %
touchable_mem], MB);
if (!trywait_sem(s))
goto out_freemem;
}
for (i = 0; i < touchable_mem; i++) {
free(mem_block[i]);
}
if (!trywait_sem(s))
goto out;
}
out_freemem:
for (i = 0; i < touchable_mem; i++)
free(mem_block[i]);
out:
return;
}
struct thread hackthread;
void emulate_hackbench(struct thread *th)
{
sem_t *s = &th->sem.stop;
init_all_sems(&hackthread.sem);
create_pthread(&hackthread.pthread, NULL, hackbench_thread, &ud.cpu_load);
wait_sem(s);
post_sem(&hackthread.sem.stop);
wait_sem(&hackthread.sem.complete);
join_pthread(hackthread.pthread, NULL);
}
#define CUSTOM_INTERVAL (ud.custom_interval)
#define CUSTOM_RUN (ud.custom_run)
void emulate_custom(struct thread *th)
{
unsigned long long deadline;
sem_t *s = &th->sem.stop;
struct timespec myts;
th->decasecond_deadlines = 1000000 / CUSTOM_INTERVAL * 10;
deadline = get_usecs(&myts);
while (1) {
deadline = periodic_schedule(th, CUSTOM_RUN, CUSTOM_INTERVAL,
deadline);
if (!trywait_sem(s))
return;
}
}
void *timekeeping_thread(void *t)
{
struct thread *th;
struct tk_thread *tk;
struct sems *s;
struct timespec myts;
long i = (long)t;
/*
* Set affinity back to normal in case it was set on our parent
* process.
*/
sched_setaffinity(0, sizeof(ud.cpumask), &ud.cpumask);
th = &threadlist[i];
tk = &th->tkthread;
s = &th->tkthread.sem;
/*
* If this timekeeping thread is that of a benchmarked thread we run
* even higher priority than the benched thread is if running real
* time. Otherwise, the load timekeeping thread, which does not need
* accurate accounting remains SCHED_NORMAL;
*/
if (th->dt != &th->benchmarks[NOT_BENCHING])
set_fifo(96);
/* These values must be changed at the appropriate places or race */
tk->sleep_interval = tk->slept_interval = 0;
post_sem(&s->ready);
while (1) {
unsigned long start_time, now;
if (!trywait_sem(&s->stop))
goto out;
wait_sem(&s->start);
tk->slept_interval = 0;
start_time = get_usecs(&myts);
if (!trywait_sem(&s->stop))
goto out;
if (tk->sleep_interval) {
unsigned long diff = 0;
microsleep(tk->sleep_interval);
now = get_usecs(&myts);
diff = now - start_time;
if (diff > tk->sleep_interval)
tk->slept_interval = diff - tk->sleep_interval;
}
tk->sleep_interval = 0;
post_sem(&s->complete);
}
out:
return NULL;
}
/*
* All the sleep functions such as nanosleep can only guarantee that they
* sleep for _at least_ the time requested. We work around this by having
* a high priority real time thread that accounts for the extra time slept
* in nanosleep. This allows wakeup latency of the tested thread to be
* accurate and reflect true scheduling delays.
*/
void *emulation_thread(void *t)
{
struct thread *th;
struct tk_thread *tk;
struct sems *s, *tks;
long i = (long)t;
th = &threadlist[i];
tk = &th->tkthread;
s = &th->sem;
tks = &tk->sem;
init_all_sems(tks);
/* Start the timekeeping thread */
create_pthread(&th->tk_pthread, NULL, timekeeping_thread,
(void*)(long) i);
/* Wait for timekeeping thread to be ready */
wait_sem(&tks->ready);
/* Tell main we're ready to start*/
post_sem(&s->ready);
/* Wait for signal from main to start thread */
wait_sem(&s->start);
/* Start the actual function being benched/or running as load */
th->name(th);
/* Stop the timekeeping thread */
post_sem(&tks->stop);
post_sem(&tks->start);
join_pthread(th->tk_pthread, NULL);
/* Tell main we've finished */
post_sem(&s->complete);
return NULL;
}
/*
* In an unoptimised loop we try to benchmark how many meaningless loops
* per second we can perform on this hardware to fairly accurately
* reproduce certain percentage cpu usage
*/
void calibrate_loop(void)
{
unsigned long long start_time, loops_per_msec, run_time = 0;
struct timespec myts;
unsigned long loops;
cpu_set_t cpumask;
CPU_ZERO(&cpumask);
CPU_SET(0, &cpumask);
/*
* Perform loop calibration on one CPU only as switching CPUs may
* make the value fluctuate too much to get a stable reading
*/
if (sched_setaffinity(0, sizeof(cpumask), &cpumask) == -1) {
if (errno != EPERM)