-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlb.c
1095 lines (985 loc) · 34 KB
/
lb.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
/* $Id: lb.c 795 2013-04-22 05:12:27Z aqua $
*
* lookbusy -- a tool for producing synthetic CPU, memory consumption and
* disk loads on a host.
*
* Copyright (c) 2006, Devin Carraway <lookbusy@devin.com>.
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/* and, redundantly ... */
static const char *copyright =
"Copyright (c) 2006-2008, by Devin Carraway <lookbusy@devin.com>\n"
"$Id: lb.c 795 2013-04-22 05:12:27Z aqua $\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
"";
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define VERSION "1.4"
#define PACKAGE "lookbusy"
#endif
#include <stdio.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define _GNU_SOURCE
#include <getopt.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <inttypes.h>
#include <ctype.h>
#include <regex.h>
#include <stdarg.h>
#include <math.h>
#ifndef HAVE_STRTOL
#define strtol(x,e,b) atol(x)
#endif
#ifndef HAVE_STRTOLL
#define strtoll(x,e,b) atol(x)
#endif
#ifdef HAVE_SYSCONF
#define LB_PAGE_SIZE sysconf(_SC_PAGESIZE)
#else
#define LB_PAGE_SIZE 4096
#endif
#ifdef _FILE_OFFSET_BITS
#if _FILE_OFFSET_BITS == 64
#define HAVE_LFS 1
#endif
#else
#define HAVE_LFS (sizeof(off_t) >= 8)
#endif
#define PI 3.14159265358979323846
enum cpu_util_mode {
UTIL_MODE_FIXED = 0,
UTIL_MODE_CURVE
};
enum cpu_util_mode c_cpu_util_mode = UTIL_MODE_FIXED;
static int utc = 0;
static int c_cpu_curve_period = 86400; /* seconds */
static int c_cpu_curve_peak = 60 * 60 * 13; /* 1PM local time */
static int c_cpu_util_l = 50, c_cpu_util_h = 50; /* percent */
static size_t c_mem_util = 0; /* bytes */
static long c_mem_stir_sleep = 1000; /* 1000 usec / 1 ms */
static off_t c_disk_util = 0; /* MB */
static char **c_disk_churn_paths;
static size_t c_disk_churn_paths_n;
static size_t c_disk_churn_block_size = 32 * 1024; /* bytes */
static size_t c_disk_churn_step_size = 4 * 1024; /* bytes */
static long c_disk_churn_sleep = 100; /* ms */
static int ncpus = -1; /* autodetect */
static int verbosity = 1;
static char *mem_stir_buffer;
static pid_t *cpu_pids;
static size_t n_cpu_pids;
static pid_t *disk_pids;
static size_t n_disk_pids;
static pid_t mem_pid;
typedef void (*spinner_fn)(long long, long long, long long, void*, void *);
static int say(int level, const char *fmt, ...)
{
va_list ap;
int r;
if (level > verbosity) return 0;
va_start(ap, fmt);
r = vprintf(fmt, ap);
va_end(ap);
return r;
}
static int err(const char *fmt, ...)
{
va_list ap;
int r;
va_start(ap, fmt);
r = vfprintf(stderr, fmt, ap);
va_end(ap);
return r;
}
static int parse_timespan(const char *str, int *r)
{
regex_t ex;
int e;
static const char *pattern =
"^[[:space:]]*([0-9]+)([dhms]?)[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[3];
if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*r = (int)strtol(str + matches[1].rm_so, NULL, 10) *
(matches[2].rm_so == -1 ? 1 :
*(str + matches[2].rm_so) == 'd' ? 86400 :
*(str + matches[2].rm_so) == 'h' ? 3600 :
*(str + matches[2].rm_so) == 'm' ? 60 : 1);
return 0;
}
static int parse_large_size(const char *str, off_t *r)
{
regex_t ex;
int e;
static const char *pattern = HAVE_LFS ?
"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?|tb?)?[[:space:]]*$" :
"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED | REG_ICASE)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[3];
if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*r = (off_t)strtoll(str + matches[1].rm_so, NULL, 10) *
(matches[2].rm_so == -1 ? 1 :
tolower(*(str + matches[2].rm_so)) == 't' ? (off_t)1 << 40 :
tolower(*(str + matches[2].rm_so)) == 'g' ? (off_t)1 << 30 :
tolower(*(str + matches[2].rm_so)) == 'm' ? (off_t)1 << 20 :
tolower(*(str + matches[2].rm_so)) == 'k' ? (off_t)1 << 10 : 1);
return 0;
}
static int parse_size(const char *str, size_t *r)
{
regex_t ex;
int e;
static const char *pattern =
"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED | REG_ICASE)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[3];
if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*r = (size_t)strtoll(str + matches[1].rm_so, NULL, 10) *
(matches[2].rm_so == -1 ? 1 :
tolower(*(str + matches[2].rm_so)) == 'g' ? 1024 * 1024 * 1024 :
tolower(*(str + matches[2].rm_so)) == 'm' ? 1024 * 1024 :
tolower(*(str + matches[2].rm_so)) == 'k' ? 1024 : 1);
return 0;
}
static int parse_int_range(const char *str, int *start, int *end)
{
regex_t ex;
int e;
static const char *pattern =
"^[[:space:]]*([0-9]+)[[:space:]]*(-([0-9]+))?[[:space:]]*$";
if ((e = regcomp(&ex,pattern, REG_EXTENDED)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
regmatch_t matches[4];
if ((e = regexec(&ex, str, 4, matches, 0)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
if (e != REG_NOMATCH)
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, str, errbuf);
return -1;
}
*start = (int)strtol(str + matches[1].rm_so, NULL, 10);
if (matches[3].rm_so != -1)
*end = (int)strtol(str + matches[3].rm_so, NULL, 10);
else
*end = *start;
return 0;
}
static void shutdown()
{
if (cpu_pids != NULL) {
int i;
for (i = 0; i < n_cpu_pids; i++) {
if (cpu_pids[i] != 0) {
say(1, "killing CPU spinner %d (PID %d)\n", i, cpu_pids[i]);
kill(cpu_pids[i], SIGTERM);
}
}
free(cpu_pids);
cpu_pids = NULL;
}
if (mem_pid != 0) {
say(1, "killing mem spinner %d\n", mem_pid);
kill(mem_pid, SIGTERM);
}
if (mem_stir_buffer != NULL) {
free(mem_stir_buffer);
mem_stir_buffer = NULL;
}
if (disk_pids != NULL) {
int i;
for (i = 0; i < n_disk_pids; i++) {
if (disk_pids[i] != 0) {
say(1,"killing disk churner %d (PID %d)\n", i, disk_pids[i]);
kill(disk_pids[i], SIGTERM);
}
}
free(disk_pids);
disk_pids = NULL;
}
if (c_disk_churn_paths != NULL) {
size_t i;
for (i = 0; i < c_disk_churn_paths_n; i++) {
if (c_disk_churn_paths[i] != NULL) {
if (unlink(c_disk_churn_paths[i]) == -1 && errno != ENOENT) {
perror(c_disk_churn_paths[i]);
}
}
}
}
exit(0);
}
static RETSIGTYPE sigchld_handler(int signum)
{
int status;
pid_t which = wait(&status);
err("got SIGCHLD for dead child %d", which);
if (WIFSIGNALED(status)) {
err("; exited with signal %d\n", WTERMSIG(status));
} else {
err("; exited with status %d\n", WEXITSTATUS(status));
}
shutdown();
}
static void sigterm_handler(int signum)
{
shutdown();
}
static uint64_t jiffies_to_usec(uint64_t jiffies)
{
return jiffies * (1000 / sysconf(_SC_CLK_TCK)) * 1000;
}
static int get_cpu_count()
{
/* FIXME: linux-specific */
FILE *f;
char s[128];
int n = 0;
static const char *pattern =
"^physical *id +: *0*[1-9]";
regex_t ex;
int e;
if ((e = regcomp(&ex, pattern, REG_EXTENDED | REG_ICASE)) != 0) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
return -1;
}
f = fopen("/proc/cpuinfo", "r");
if (f == NULL) {
perror("/proc/cpuinfo");
exit(1);
}
while (fgets(s, sizeof(s)-1, f) != NULL) {
s[sizeof(s)-1] = '\0';
if (!strncmp(s, "processor", strlen("processor"))) {
n++;
} else {
e = regexec(&ex, s, 0, NULL, 0);
if (e == 0) {
/* matched a phys-id other than zero, don't count it */
n--;
} else if (e != REG_NOMATCH) {
char errbuf[128];
regerror(e, &ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regexec: Couldn't match '%s' in '%s': %s\n",
pattern, s, errbuf);
return -1;
}
}
}
fclose(f);
if (n == 0) {
err("Couldn't get any CPU counts, assuming one CPU core\n");
err("If this is wrong, override with --ncpus\n");
ncpus = 1;
}
return n;
}
static uint64_t get_cpu_busy_time()
{
FILE *f;
char s[256];
uint64_t utime, ntime, stime;
static regex_t *ex = NULL;
static const char *pattern =
"^cpu[[:space:]]+([0-9]+)[[:space:]]+([0-9]+)[[:space:]]+([0-9]+)";
int r;
if (ex == NULL) {
ex = (regex_t *)malloc(sizeof(*ex));
if (ex == NULL) {
perror("malloc");
_exit(1);
}
if ((r = regcomp(ex,pattern, REG_EXTENDED)) != 0) {
char errbuf[128];
regerror(r, ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regcomp(%s): %s\n", pattern, errbuf);
_exit(1);
}
}
if ((f = fopen("/proc/stat", "r")) == NULL) {
perror("/proc/stat");
_exit(1);
}
if (fgets(s, sizeof(s)-1, f) == NULL) {
perror("fgets");
_exit(1);
}
regmatch_t matches[4];
if ((r = regexec(ex, s, 4, matches, 0)) != 0) {
char errbuf[128];
regerror(r, ex, errbuf, sizeof(errbuf)-1);
errbuf[sizeof(errbuf)-1] = '\0';
err("regexec: Couldn't match '%s' in '%s': %s\n", pattern, s, errbuf);
_exit(1);
}
utime = atol(s + matches[1].rm_so);
ntime = atol(s + matches[2].rm_so);
stime = atol(s + matches[3].rm_so);
say(3, "cpu_spin(%d): current utime=%"PRIu64
" ntime=%"PRIu64" stime=%"PRIu64" total=%"PRIu64"\n",
getpid(), utime, ntime, stime, utime + ntime + stime);
fclose(f);
return utime + ntime + stime;
}
static char cpu_spin_accumulator;
static char squander_time(uint64_t iteration)
{
return (cpu_spin_accumulator += (char)iteration);
}
static void cpu_spin_calibrate(int util, uint64_t *busycount, suseconds_t *sleeptime)
{
uint64_t counter;
struct timeval tv, tv2;
const uint64_t iterations = 10000000;
say(1, "cpu_spin (%d): measuring CPU\n", getpid());
if (gettimeofday(&tv, NULL) == -1) shutdown();
for (counter = 0; counter < iterations; counter++) {
squander_time(counter);
}
if (gettimeofday(&tv2, NULL) == -1) shutdown();
long long elapsed = (tv2.tv_sec - tv.tv_sec) * 1000000 +
(tv2.tv_usec - tv.tv_usec);
long long countspeed = (counter * 100000)/elapsed;
*busycount = (countspeed * util) / 100LL;
/* busy_count_time = busycount/(counter/elapsed)
* idle_time = 1sec - busy_count_time
*/
*sleeptime = 100000 - *busycount / (counter / elapsed);
say(3, "cpu_spin (%d): %"PRIu64" iterations in %lld usec (%lld/sec)\n",
getpid(), counter, elapsed, countspeed);
say(1, "cpu_spin (%d): est. %d%% util at %"PRIu64" cycles, %u usec sleep\n",
getpid(), util, *busycount, *sleeptime);
}
static double cpu_spin_compute_util(enum cpu_util_mode mode, int l, int h,
time_t curtime)
{
static long time_offset = -1;
if (time_offset == -1) {
if (!utc) {
#ifdef HAVE_TZSET
tzset();
#endif
time_offset = -timezone;
} else {
time_offset = 0;
}
time_offset += c_cpu_curve_peak;
}
if (mode == UTIL_MODE_FIXED) return l;
if (mode == UTIL_MODE_CURVE) {
if (curtime == 0)
curtime = time(NULL);
long offset_in_interval = (curtime + time_offset) % c_cpu_curve_period;
double fraction = (double)offset_in_interval / (double)c_cpu_curve_period;
/* model the CPU usage as a cosine function over [0,2pi], shifted up
* by min+1 to place the floor on min, and scaled so as to have its
* entire range between min and max. That is, the peak of the cosine
* curve is placed at X=0, and it oscillates over the selected CPU
* utilization range.
*
* This isn't a particularly realistic utilization curve and something
* better ought to be found. Or just bite the bullet and make it
* data-driven.
*/
double level = (double)l +
(double)(h - l) * (cos(fraction * PI * 2) + 1)/2;
return level;
}
/* shouldn't get here */
return -1;
}
static void cpu_spin(long long ncpus, long long util_l, long long util_h, void *dummy, void *dummy2)
{
uint64_t busycount;
const uint64_t minimum_cycles = 10000;
suseconds_t sleeptime;
int first = 1;
double util;
int64_t adjust = 0;
util = cpu_spin_compute_util(c_cpu_util_mode, util_l, util_h, 0);
cpu_spin_calibrate(util, &busycount, &sleeptime);
say(2, "cpu_spin (%d): spinning cpu\n", getpid());
while (1) {
struct timeval tv;
long long counter;
uint64_t busytime, busytime2;
uint64_t walltime, walltime2;
if (! first) {
uint64_t busy = jiffies_to_usec(busytime2 - busytime) / ncpus;
uint64_t wall = walltime2 - walltime;
double actual = (100 * busy) / wall;
int64_t oldadjust = adjust;
/* On a multi-CPU system there will be more than one spinner
* trying to hit the target utilization, so restrain the
* adjustment range so as to keep from collectively
* overcompensating
*/
adjust = (int64_t)(((util - actual) * busycount) / 100. / ncpus);
say(3, "cpu_spin (%d): last iter: count=%lld"
" (~%lld of %lld); adjust=%lld\n",
getpid(), busycount, busy, wall,
adjust);
if (adjust < 0 && busycount < -adjust) {
say(2, "cpu_spin (%d): usage at lower limit\n", getpid());
busycount = minimum_cycles;
} else if (adjust > 0 && UINT64_MAX - adjust < busycount) {
say(2, "cpu_spin (%d): usage at upper limit\n", getpid());
} else {
busycount += adjust;
if (busycount < minimum_cycles)
busycount = minimum_cycles;
}
if ((adjust < 0 && oldadjust > 0) ||
(adjust > 0 && oldadjust < 0)) {
say(3, "cpu_spin (%d): adjusted approximately correctly\n",
getpid());
say(3, "cpu_spin (%d): spin count %"PRIu64"\n", getpid(), busycount);
}
} else {
first = 0;
}
gettimeofday(&tv, NULL);
walltime = tv.tv_sec * 1000000 + tv.tv_usec;
busytime = get_cpu_busy_time();
say(3, "cpu_spin (%d): spinning (0 to %"PRIu64")...\n", getpid(), busycount);
for (counter = 0; counter < busycount; counter++) {
squander_time(counter);
}
say(3, "cpu_spin (%d): sleeping...\n", getpid());
usleep(sleeptime);
gettimeofday(&tv, NULL);
walltime2 = tv.tv_sec * 1000000 + tv.tv_usec;
busytime2 = get_cpu_busy_time();
util = cpu_spin_compute_util(c_cpu_util_mode, util_l, util_h, tv.tv_sec);
/* "elapsed" here doesn't necessarily refer only to our own usage */
say(2, "cpu_spin (%d): %"PRIu64" iterations; %"PRIu64" CPU-jiffies elapsed\n",
getpid(), counter, busytime2-busytime,
ncpus);
}
_exit(1);
}
static void mem_stir(long long asz, long long dummy, long long dummy2, void *dummyp, void *dummyp2)
{
char *mem_stir_buffer;
char *p;
const size_t pagesize = LB_PAGE_SIZE;
const size_t sz = asz;
say(1, "mem_stir (%d): stirring %llu bytes...\n", getpid(), sz);
if ((mem_stir_buffer = (char *)malloc(sz)) == NULL) {
perror("malloc");
_exit(1);
}
say(2, "mem_stir (%d): dirtying buffer...", getpid());
for (p = mem_stir_buffer; p < mem_stir_buffer + sz; p++) {
*p = (char)((uintptr_t)p & 0xff);
}
say(2, "done\n");
char *sp = mem_stir_buffer;
char *dp = mem_stir_buffer;
while (1) {
const size_t copysz = pagesize;
#ifdef HAVE_MEMMOVE
memmove(dp, sp, copysz);
#else
memcpy(dp, sp, copysz);
#endif
sp += pagesize * 1;
dp += pagesize * 5;
if (sp >= mem_stir_buffer + sz) {
say(2, "mem_stir (%d): read position wrapped\n", getpid());
sp = mem_stir_buffer;
}
if (dp >= mem_stir_buffer + sz) {
say(2, "mem_stir (%d): write position wrapped\n", getpid());
dp = mem_stir_buffer;
}
usleep(c_mem_stir_sleep);
}
_exit(1);
}
static void disk_churn(long long dummy0, long long dummy, long long dummy2, void *pathv, void *szv)
{
char *path = (char *)pathv;
off_t sz = *(off_t *)szv;
int fd;
int witer;
off_t rpos, wpos;
say(1, (sizeof(off_t) == 8 ?
"disk_churn (%d): churning disk on %s (%ld bytes)\n" :
"disk_churn (%d): churning disk on %s (%d bytes)\n"),
getpid(), path, sz);
if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600)) == -1) {
err("disk_churn (%d): Couldn't create %s: %s\n",
getpid(), path, strerror(errno));
exit(1);
}
if (0 && unlink(path) && errno != ENOENT) {
perror(path);
exit(1);
}
if (lseek(fd, sz - sizeof(fd), SEEK_SET) == (off_t)-1) {
perror("lseek");
exit(1);
}
if (write(fd, &fd, sizeof(fd)) == -1) {
perror("write");
exit(1);
}
char *block = malloc(c_disk_churn_block_size);
if (block == NULL) {
perror("malloc");
_exit(1);
}
witer = 0;
rpos = wpos = 0;
while (1) {
size_t p;
ssize_t r;
if (rpos >= sz) {
say(2, "disk_churn (%d) reader reached EOF at %ld\n",
getpid(), (long)rpos);
rpos = 0;
}
if (wpos >= sz) {
say(2, "disk_churn (%d) writer reached EOF at %ld\n",
getpid(), (long)wpos);
wpos = 0;
witer++;
}
for (p = 0; p < c_disk_churn_block_size; p++)
block[p] = (char)((witer | (int)p) & 0xff);
if (lseek(fd, wpos, SEEK_SET) == (off_t)-1) {
perror("lseek");
exit(1);
}
if (write(fd, block, c_disk_churn_block_size) == -1) {
perror("write");
exit(1);
}
wpos += c_disk_churn_step_size * 2;
if (lseek(fd, rpos, SEEK_SET) == (off_t)-1) {
perror("lseek");
exit(1);
}
r = read(fd, block, c_disk_churn_block_size);
if (r == -1) {
err("disk_churn (%d): error reading from %s at %ld: %s\n",
getpid(), path, (long)rpos, strerror(errno));
exit(1);
}
if (r == 0) {
say(1, "disk_churn (%d): reader reached EOF early (at %ld)\n",
getpid(), (long)rpos);
rpos = 0;
}
rpos += c_disk_churn_step_size;
usleep(c_disk_churn_sleep * 1000);
}
_exit(0);
}
static pid_t fork_and_call(char *desc, spinner_fn fn, long long arg1, long long arg2, long long arg3, void *argP, void *argP2)
{
pid_t p = fork();
if (p == -1) {
perror("fork");
return 0;
}
else if (p == 0) {
if (cpu_pids != NULL) {
free(cpu_pids);
cpu_pids = NULL;
}
if (disk_pids != NULL) {
free(disk_pids);
disk_pids = NULL;
}
mem_pid = 0;
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
(*fn)(arg1, arg2, arg3, argP, argP2);
exit(0);
} else {
say(1, "lookbusy (%d): %s started, PID %d\n", getpid(), desc, p);
return p;
}
}
static pid_t *start_cpu_spinners(int *ncpus, int util_l, int util_h)
{
pid_t *pids;
if (*ncpus <= 0)
*ncpus = get_cpu_count();
if ((pids = (pid_t *)malloc(sizeof(*pids) * *ncpus)) == NULL) {
perror("malloc");
return NULL;
}
int i;
say(1, "cpu_spin (%d): starting %d spinner(s) for %d%%-%d%% usage\n",
getpid(), *ncpus, util_l, util_h);
for (i = 0; i < *ncpus; i++) {
pids[i] = fork_and_call("CPU spinner", cpu_spin, *ncpus, util_l, util_h, NULL, NULL);
}
return pids;
}
static pid_t *start_disk_stirrer(off_t util, char **paths, size_t paths_n)
{
size_t i;
pid_t *p = malloc(sizeof(*p) * paths_n);
for (i = 0; i < paths_n; i++) {
struct stat st;
if (stat(paths[i], &st) == 0) {
if (S_ISDIR(st.st_mode)) {
if (access(paths[i], W_OK) != 0) {
err("disk path %s is not writable\n", paths[i]);
shutdown();
}
char *tmpl = (char *)malloc(strlen(paths[i]) + 32);
if (tmpl == NULL) {
perror("malloc");
shutdown();
}
snprintf(tmpl, strlen(paths[i]) + 31, "%s/lb.%d.XXXXXX",
paths[i], getpid());
int fd = mkstemp(tmpl);
if (fd == -1) {
perror("mkstemp");
free(tmpl);
shutdown();
}
free(paths[i]);
paths[i] = tmpl;
}
}
char *desc = (char *)malloc(32 + strlen(paths[i]));
if (desc == NULL) {
perror("malloc");
shutdown();
}
snprintf(desc, 31 + strlen(paths[i]), "disk churn: %s", paths[i]);
p[i] = fork_and_call(desc, disk_churn, 0, 0, 0, paths[i], &util);
}
return p;
}
static pid_t start_mem_whisker(size_t sz)
{
return fork_and_call("mem stirrer", mem_stir, sz, 0, 0, NULL, NULL);
}
static void usage()
{
static const char *msg =
"lookbusy [ -h ] [ options ]\n"
"General options:\n"
" -h, --help Commandline help (you're reading it)\n"
" -v, --verbose Verbose output (may be repeated)\n"
" -q, --quiet Be quiet, produce output on errors only\n"
"CPU usage options:\n"
" -c, --cpu-util=PCT, Desired utilization of each CPU, in percent (default\n"
" --cpu-util=RANGE 50%). If 'curve' CPU usage mode is chosen, a range\n"
" of the form MIN-MAX should be given.\n"
" -n, --ncpus=NUM Number of CPUs to keep busy (default: autodetected)\n"
" -r, --cpu-mode=MODE Utilization mode ('fixed' or 'curve', see lookbusy(1))\n"
" -p, --cpu-curve-peak=TIME\n"
" Offset of peak utilization within curve period, in\n"
" seconds (append 'm', 'h', 'd' for other units)\n"
" -P, --cpu-curve-period=TIME\n"
" Duration of utilization curve period, in seconds (append\n"
" 'm', 'h', 'd' for other units)\n"
"Memory usage options:\n"
" -m, --mem-util=SIZE Amount of memory to use (in bytes, followed by KB, MB,\n"
" or GB for other units; see lookbusy(1))\n"
" -M, --mem-sleep=TIME Time to sleep between iterations, in usec (default 1000)\n"
"Disk usage options:\n"
" -d, --disk-util=SIZE Size of files to use for disk churn (in bytes,\n"
" followed by KB, MB, GB or TB for other units)\n"
" -b, --disk-block-size=SIZE\n"
" Size of blocks to use for I/O (in bytes, followed\n"
" by KB, MB or GB)\n"
" -D, --disk-sleep=TIME\n"
" Time to sleep between iterations, in msec (default 100)\n"
" -f, --disk-path=PATH Path to a file/directory to use as a buffer (default\n"
" /tmp); specify multiple times for additional paths\n"
"";
printf("usage: %s", msg);
exit(0);
}
int main(int argc, char **argv)
{
int c;
size_t disk_paths_cap = 0;
static const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "verbose", 0, NULL, 'v' },
{ "quiet", 0, NULL, 'q' },
{ "version", 0, NULL, 'V' },
{ "cpu-util", 1, NULL, 'c' },
{ "cpu-mode", 1, NULL, 'r' },
{ "ncpus", 1, NULL, 'n' },
{ "cpu-curve-peak", 1, NULL, 'p' },
{ "cpu-curve-period", 1, NULL, 'P' },
{ "utc", 0, NULL, 'u' },
{ "disk-util", 1, NULL, 'd' },
{ "disk-sleep", 1, NULL, 'D' },
{ "disk-block-size", 1, NULL, 'b' },
{ "disk-path", 1, NULL, 'f' },
{ "mem-util", 1, NULL, 'm' },
{ "mem-sleep", 1, NULL, 'M' },
{ 0, 0, 0, 0 }
};
while ((c = getopt_long(argc, argv,
"n:b:c:d:D:f:m:M:p:P:r:qvVhu",
long_options, NULL)) != -1) {
switch (c) {
default:
case 'h': usage(); break;
case 'b':
if (parse_size(optarg, &c_disk_churn_block_size) < 0) {
err("Couldn't parse disk block size '%s'\n", optarg);
return 1;
}
break;
case 'c':
if (parse_int_range(optarg, &c_cpu_util_l, &c_cpu_util_h) < 0) {
err("Couldn't parse CPU utilization setting '%s'\n",
optarg);
return 1;
}
break;
case 'd':
if (parse_large_size(optarg, &c_disk_util) < 0) {
err("Couldn't parse disk utilization filesize '%s'\n",
optarg);
return 1;
}
break;
case 'D':
c_disk_churn_sleep = atol(optarg);
break;
case 'f':
if (!optarg || !*optarg)
break;
if (c_disk_churn_paths_n + 1 > disk_paths_cap) {
disk_paths_cap = (c_disk_churn_paths_n + 1) * 2;
char **tmp;
tmp = (char **)realloc(c_disk_churn_paths,
disk_paths_cap * sizeof(*tmp));
if (tmp == NULL) {
perror("realloc");
_exit(1);
}
c_disk_churn_paths = tmp;
}
c_disk_churn_paths[c_disk_churn_paths_n++] = strdup(optarg);
break;
case 'm':
if (parse_size(optarg, &c_mem_util) < 0) {
err("Couldn't parse memory utilization size '%s'\n", optarg);
return 1;
}
break;
case 'M':
c_mem_stir_sleep = atol(optarg);
break;
case 'n':
ncpus = atoi(optarg);
break;
case 'p':
if (parse_timespan(optarg, &c_cpu_curve_peak) < 0) {
err("Couldn't parse CPU curve peak '%s'; format is"
" INTEGER[SUFFIX], where SUFFIX\n"
"is one of 's' (seconds), 'm' (minutes), 'h' (hours)"
", or 'd' (days); e.g. \"2h\"\n", optarg);
return 1;
}
break;
case 'P':
if (parse_timespan(optarg, &c_cpu_curve_period) < 0) {