forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-helper.c
1510 lines (1322 loc) · 31.9 KB
/
core-helper.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) 2014-2019 Canonical, Ltd.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if !defined(PR_SET_DISABLE)
#define SUID_DUMP_DISABLE (0) /* No setuid dumping */
#endif
#if !defined(SUID_DUMP_USER)
#define SUID_DUMP_USER (1) /* Dump as user of process */
#endif
#if defined(HAVE_PRCTL_TIMER_SLACK)
static unsigned long timer_slack = 0;
#endif
#if defined(NSIG)
#define STRESS_NSIG NSIG
#elif defined(_NSIG)
#define STRESS_NSIG _NSIG
#endif
#if defined(__TINYC__)
int __dso_handle;
#endif
static const char *stress_temp_path = ".";
/*
* stress_get_pagesize()
* get pagesize
*/
size_t stress_get_pagesize(void)
{
static size_t page_size = 0;
/* Use cached size */
if (page_size > 0)
return page_size;
#if defined(_SC_PAGESIZE)
{
/* Use modern sysconf */
long sz = sysconf(_SC_PAGESIZE);
if (sz > 0) {
page_size = (size_t)sz;
return page_size;
}
}
#endif
#if defined(HAVE_GETPAGESIZE)
{
/* Use deprecated getpagesize */
long sz = getpagesize();
if (sz > 0) {
page_size = (size_t)sz;
return page_size;
}
}
#endif
/* Guess */
page_size = PAGE_4K;
return page_size;
}
/*
* stress_get_processors_online()
* get number of processors that are online
*/
int32_t stress_get_processors_online(void)
{
static int32_t processors_online = 0;
if (processors_online > 0)
return processors_online;
#if defined(_SC_NPROCESSORS_ONLN)
processors_online = (int32_t)sysconf(_SC_NPROCESSORS_ONLN);
if (processors_online < 0)
processors_online = 1;
#else
processors_online = 1;
#endif
return processors_online;
}
/*
* stress_get_processors_configured()
* get number of processors that are configured
*/
int32_t stress_get_processors_configured(void)
{
static int32_t processors_configured = 0;
if (processors_configured > 0)
return processors_configured;
#if defined(_SC_NPROCESSORS_CONF)
processors_configured = (int32_t)sysconf(_SC_NPROCESSORS_CONF);
if (processors_configured < 0)
processors_configured = stress_get_processors_online();
#else
processors_configured = 1;
#endif
return processors_configured;
}
/*
* stress_get_ticks_per_second()
* get number of ticks perf second
*/
int32_t stress_get_ticks_per_second(void)
{
#if defined(_SC_CLK_TCK)
static uint32_t ticks_per_second = 0;
if (ticks_per_second > 0)
return ticks_per_second;
ticks_per_second = sysconf(_SC_CLK_TCK);
return ticks_per_second;
#else
return -1;
#endif
}
/*
* stress_get_memlimits()
* get SHMALL and memory in system
* these are set to zero on failure
*/
void stress_get_memlimits(
size_t *shmall,
size_t *freemem,
size_t *totalmem,
size_t *freeswap)
{
#if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
struct sysinfo info;
FILE *fp;
#endif
*shmall = 0;
*freemem = 0;
*totalmem = 0;
*freeswap = 0;
#if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
if (sysinfo(&info) == 0) {
*freemem = info.freeram * info.mem_unit;
*totalmem = info.totalram * info.mem_unit;
*freeswap = info.freeswap * info.mem_unit;
}
fp = fopen("/proc/sys/kernel/shmall", "r");
if (!fp)
return;
if (fscanf(fp, "%zu", shmall) != 1) {
(void)fclose(fp);
return;
}
(void)fclose(fp);
#endif
}
#if defined(_SC_AVPHYS_PAGES)
#define STRESS_SC_PAGES _SC_AVPHYS_PAGES
#elif defined(_SC_PHYS_PAGES)
#define STRESS_SC_PAGES _SC_PHYS_PAGES
#endif
/*
* stress_get_phys_mem_size()
* get size of physical memory still available, 0 if failed
*/
uint64_t stress_get_phys_mem_size(void)
{
#if defined(STRESS_SC_PAGES)
uint64_t phys_pages = 0;
const size_t page_size = stress_get_pagesize();
const uint64_t max_pages = ~0ULL / page_size;
phys_pages = sysconf(STRESS_SC_PAGES);
/* Avoid overflow */
if (phys_pages > max_pages)
phys_pages = max_pages;
return phys_pages * page_size;
#else
return 0ULL;
#endif
}
/*
* stress_get_filesystem_size()
* get size of free space still available on the
* file system where stress temporary path is located,
* return 0 if failed
*/
uint64_t stress_get_filesystem_size(void)
{
#if defined(HAVE_SYS_STATVFS_H)
int rc;
struct statvfs buf;
fsblkcnt_t blocks, max_blocks;
if (!stress_temp_path)
return 0;
rc = statvfs(stress_temp_path, &buf);
if (rc < 0)
return 0;
max_blocks = (~(fsblkcnt_t)0) / buf.f_bsize;
blocks = buf.f_bavail;
if (blocks > max_blocks)
blocks = max_blocks;
return (uint64_t)buf.f_bsize * blocks;
#else
return 0ULL;
#endif
}
/*
* stress_get_filesystem_available_inodes()
* get number of free available inodes on the current stress
* temporary path, return 0 if failed
*/
uint64_t stress_get_filesystem_available_inodes(void)
{
#if defined(HAVE_SYS_STATVFS_H)
int rc;
struct statvfs buf;
if (!stress_temp_path)
return 0;
rc = statvfs(stress_temp_path, &buf);
if (rc < 0)
return 0;
return (uint64_t)buf.f_favail;
#else
return 0ULL;
#endif
}
/*
* stress_set_nonblock()
* try to make fd non-blocking
*/
int stress_set_nonblock(const int fd)
{
int flags;
#if defined(O_NONBLOCK)
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
flags = 0;
return fcntl(fd, F_SETFL, O_NONBLOCK | flags);
#else
flags = 1;
return ioctl(fd, FIOBIO, &flags);
#endif
}
/*
* stress_get_load_avg()
* get load average
*/
int stress_get_load_avg(
double *min1,
double *min5,
double *min15)
{
#if NEED_GLIBC(2, 2, 0) && !defined(__UCLIBC__)
int rc;
double loadavg[3];
rc = getloadavg(loadavg, 3);
if (rc < 0)
goto fail;
*min1 = loadavg[0];
*min5 = loadavg[1];
*min15 = loadavg[2];
return 0;
fail:
#elif defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
struct sysinfo info;
const double scale = 1.0 / (double)(1 << SI_LOAD_SHIFT);
if (sysinfo(&info) < 0)
goto fail;
*min1 = info.loads[0] * scale;
*min5 = info.loads[1] * scale;
*min15 = info.loads[2] * scale;
return 0;
fail:
#endif
*min1 = *min5 = *min15 = 0.0;
return -1;
}
/*
* stress_parent_died_alarm()
* send child SIGALRM if the parent died
*/
void stress_parent_died_alarm(void)
{
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_PDEATHSIG)
(void)prctl(PR_SET_PDEATHSIG, SIGALRM);
#endif
}
/*
* stress_process_dumpable()
* set dumpable flag, e.g. produce a core dump or not,
* don't print an error if these fail, it's not that
* critical
*/
int stress_process_dumpable(const bool dumpable)
{
int fd, rc = 0;
#if defined(RLIMIT_CORE)
{
struct rlimit lim;
int ret;
ret = getrlimit(RLIMIT_CORE, &lim);
if (ret == 0) {
lim.rlim_cur = 0;
(void)setrlimit(RLIMIT_CORE, &lim);
}
lim.rlim_cur = 0;
lim.rlim_max = 0;
(void)setrlimit(RLIMIT_CORE, &lim);
}
#endif
/*
* changing PR_SET_DUMPABLE also affects the
* oom adjust capability, so for now, we disable
* this as I'd rather have a oom'able process when
* memory gets contrained. Don't enable this
* unless one checks that processes able oomable!
*/
#if 0 && defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_DUMPABLE)
(void)prctl(PR_SET_DUMPABLE,
dumpable ? SUID_DUMP_USER : SUID_DUMP_DISABLE);
#endif
if ((fd = open("/proc/self/coredump_filter", O_WRONLY)) >= 0) {
char const *str =
dumpable ? "0x33" : "0x00";
if (write(fd, str, strlen(str)) < 0)
rc = -1;
(void)close(fd);
}
return rc;
}
/*
* stress_set_timer_slackigned_longns()
* set timer slack in nanoseconds
*/
int stress_set_timer_slack_ns(const char *opt)
{
#if defined(HAVE_PRCTL_TIMER_SLACK)
timer_slack = get_uint32(opt);
#else
(void)opt;
#endif
return 0;
}
/*
* stress_set_timer_slack()
* set timer slack
*/
void stress_set_timer_slack(void)
{
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(HAVE_PRCTL_TIMER_SLACK)
(void)prctl(PR_SET_TIMERSLACK, timer_slack);
#endif
}
/*
* stress_set_proc_name()
* Set process name, we don't care if it fails
*/
void stress_set_proc_name(const char *name)
{
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_NAME)
if (!(g_opt_flags & OPT_FLAGS_KEEP_NAME))
(void)prctl(PR_SET_NAME, name);
#else
(void)name; /* No-op */
#endif
}
/*
* stress_munge_underscore()
* turn '_' to '-' in strings
*/
char *stress_munge_underscore(const char *str)
{
static char munged[128];
char *dst;
const char *src;
const size_t str_len = strlen(str);
const ssize_t len = STRESS_MINIMUM(str_len, sizeof(munged) - 1);
for (src = str, dst = munged; *src && (dst - munged) < len; src++)
*dst++ = (*src == '_' ? '-' : *src);
*dst = '\0';
return munged;
}
/*
* __stress_get_stack_direction()
* helper to determine direction of stack
*/
static ssize_t __stress_get_stack_direction(const uint8_t *val1)
{
const uint8_t val2 = 0;
const ssize_t diff = &val2 - (const uint8_t *)val1;
return (diff > 0) - (diff < 0);
}
/*
* stress_get_stack_direction()
* determine which way the stack goes, up / down
* just pass in any var on the stack before calling
* return:
* 1 - stack goes down (conventional)
* 0 - error
* -1 - stack goes up (unconventional)
*/
ssize_t stress_get_stack_direction(void)
{
uint8_t val1 = 0;
uint8_t waste[64];
waste[(sizeof waste) - 1] = 0;
return __stress_get_stack_direction(&val1);
}
/*
* stress_uint64_zero()
* return uint64 zero in way that force less smart
* static analysers to realise we are doing this
* to force a division by zero. I'd like to have
* a better solution than this ghastly way.
*/
uint64_t stress_uint64_zero(void)
{
return g_shared->zero;
}
/*
* stress_set_temp_path()
* set temporary file path, default
* is . - current dir
*/
int stress_set_temp_path(const char *path)
{
stress_temp_path = path;
if (access(path, R_OK | W_OK) < 0) {
(void)fprintf(stderr, "temp-path '%s' must be readable "
"and writeable\n", path);
return -1;
}
return 0;
}
/*
* stress_temp_filename()
* construct a temp filename
*/
int stress_temp_filename(
char *path,
const size_t len,
const char *name,
const pid_t pid,
const uint32_t instance,
const uint64_t magic)
{
return snprintf(path, len, "%s/tmp-%s-%d-%"
PRIu32 "/%s-%d-%"
PRIu32 "-%" PRIu64,
stress_temp_path,
name, (int)pid, instance,
name, (int)pid, instance, magic);
}
/*
* stress_temp_filename_args()
* construct a temp filename using info from args
*/
int stress_temp_filename_args(
const args_t *args,
char *path,
const size_t len,
const uint64_t magic)
{
return stress_temp_filename(path, len, args->name,
args->pid, args->instance, magic);
}
/*
* stress_temp_dir()
* create a temporary directory name
*/
int stress_temp_dir(
char *path,
const size_t len,
const char *name,
const pid_t pid,
const uint32_t instance)
{
return snprintf(path, len, "%s/tmp-%s-%d-%" PRIu32,
stress_temp_path, name, (int)pid, instance);
}
/*
* stress_temp_dir_args()
* create a temporary directory name using info from args
*/
int stress_temp_dir_args(
const args_t *args,
char *path,
const size_t len)
{
return stress_temp_dir(path, len,
args->name, args->pid, args->instance);
}
/*
* stress_temp_dir_mk()
* create a temporary directory
*/
int stress_temp_dir_mk(
const char *name,
const pid_t pid,
const uint32_t instance)
{
int ret;
char tmp[PATH_MAX];
stress_temp_dir(tmp, sizeof(tmp), name, pid, instance);
ret = mkdir(tmp, S_IRWXU);
if (ret < 0) {
ret = -errno;
pr_fail("%s: mkdir '%s' failed, errno=%d (%s)\n",
name, tmp, errno, strerror(errno));
}
return ret;
}
/*
* stress_temp_dir_mk_args()
* create a temporary director using info from args
*/
int stress_temp_dir_mk_args(const args_t *args)
{
return stress_temp_dir_mk(args->name, args->pid, args->instance);
}
/*
* stress_temp_dir_rm()
* remove a temporary directory
*/
int stress_temp_dir_rm(
const char *name,
const pid_t pid,
const uint32_t instance)
{
int ret;
char tmp[PATH_MAX + 1];
stress_temp_dir(tmp, sizeof(tmp), name, pid, instance);
ret = rmdir(tmp);
if (ret < 0) {
ret = -errno;
pr_fail("%s: rmdir '%s' failed, errno=%d (%s)\n",
name, tmp, errno, strerror(errno));
}
return ret;
}
/*
* stress_temp_dir_rm_args()
* remove a temporary directory using info from args
*/
int stress_temp_dir_rm_args(const args_t *args)
{
return stress_temp_dir_rm(args->name, args->pid, args->instance);
}
/*
* stress_cwd_readwriteable()
* check if cwd is read/writeable
*/
void stress_cwd_readwriteable(void)
{
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) == NULL) {
pr_dbg("cwd: Cannot determine current working directory\n");
return;
}
if (access(path, R_OK | W_OK)) {
pr_inf("Working directory %s is not read/writeable, "
"some I/O tests may fail\n", path);
return;
}
}
/*
* stress_strsignal()
* signum to human readable string
*/
const char *stress_strsignal(const int signum)
{
static char buffer[128];
#if defined(STRESS_NSIG)
const char *str = NULL;
if ((signum >= 0) && (signum < STRESS_NSIG))
str = strsignal(signum);
if (str)
(void)snprintf(buffer, sizeof(buffer), "signal %d (%s)",
signum, str);
else
(void)snprintf(buffer, sizeof(buffer), "signal %d", signum);
#else
(void)snprintf(buffer, sizeof(buffer), "signal %d", signum);
#endif
return buffer;
}
/*
* stress_strnrnd()
* fill string with random chars
*/
void stress_strnrnd(char *str, const size_t len)
{
const char *end = str + len;
while (str < end - 1)
*str++ = (mwc8() % 26) + 'a';
*str = '\0';
}
/*
* pr_yaml_runinfo()
* log info about the system we are running stress-ng on
*/
void pr_yaml_runinfo(FILE *yaml)
{
#if defined(HAVE_UNAME) && defined(HAVE_SYS_UTSNAME_H)
struct utsname uts;
#endif
#if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
struct sysinfo info;
#endif
time_t t;
struct tm *tm = NULL;
char hostname[128];
const char *user = shim_getlogin();
pr_yaml(yaml, "system-info:\n");
if (time(&t) != ((time_t)-1))
tm = localtime(&t);
pr_yaml(yaml, " stress-ng-version: " VERSION "\n");
pr_yaml(yaml, " run-by: %s\n", user ? user : "unknown");
if (tm) {
pr_yaml(yaml, " date-yyyy-mm-dd: %4.4d:%2.2d:%2.2d\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
pr_yaml(yaml, " time-hh-mm-ss: %2.2d:%2.2d:%2.2d\n",
tm->tm_hour, tm->tm_min, tm->tm_sec);
pr_yaml(yaml, " epoch-secs: %ld\n", (long)t);
}
if (!gethostname(hostname, sizeof(hostname)))
pr_yaml(yaml, " hostname: %s\n", hostname);
#if defined(HAVE_UNAME) && defined(HAVE_SYS_UTSNAME_H)
if (uname(&uts) == 0) {
pr_yaml(yaml, " sysname: %s\n", uts.sysname);
pr_yaml(yaml, " nodename: %s\n", uts.nodename);
pr_yaml(yaml, " release: %s\n", uts.release);
pr_yaml(yaml, " version: %s\n", uts.version);
pr_yaml(yaml, " machine: %s\n", uts.machine);
}
#endif
#if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
if (sysinfo(&info) == 0) {
pr_yaml(yaml, " uptime: %ld\n", info.uptime);
pr_yaml(yaml, " totalram: %lu\n", info.totalram);
pr_yaml(yaml, " freeram: %lu\n", info.freeram);
pr_yaml(yaml, " sharedram: %lu\n", info.sharedram);
pr_yaml(yaml, " bufferram: %lu\n", info.bufferram);
pr_yaml(yaml, " totalswap: %lu\n", info.totalswap);
pr_yaml(yaml, " freeswap: %lu\n", info.freeswap);
}
#endif
pr_yaml(yaml, " pagesize: %zd\n", stress_get_pagesize());
pr_yaml(yaml, " cpus: %" PRId32 "\n", stress_get_processors_configured());
pr_yaml(yaml, " cpus-online: %" PRId32 "\n", stress_get_processors_online());
pr_yaml(yaml, " ticks-per-second: %" PRId32 "\n", stress_get_ticks_per_second());
pr_yaml(yaml, "\n");
}
/*
* stress_cache_alloc()
* allocate shared cache buffer
*/
int stress_cache_alloc(const char *name)
{
#if defined(__linux__)
cpus_t *cpu_caches;
cpu_cache_t *cache = NULL;
uint16_t max_cache_level = 0;
#endif
#if !defined(__linux__)
g_shared->mem_cache_size = MEM_CACHE_SIZE;
#else
cpu_caches = get_all_cpu_cache_details();
if (!cpu_caches) {
if (warn_once(WARN_ONCE_CACHE_DEFAULT))
pr_inf("%s: using defaults, can't determine cache details from sysfs\n", name);
g_shared->mem_cache_size = MEM_CACHE_SIZE;
goto init_done;
}
max_cache_level = get_max_cache_level(cpu_caches);
if (g_shared->mem_cache_level > max_cache_level) {
if (warn_once(WARN_ONCE_CACHE_REDUCED))
pr_dbg("%s: reducing cache level from L%d (too high) "
"to L%d\n", name,
g_shared->mem_cache_level, max_cache_level);
g_shared->mem_cache_level = max_cache_level;
}
cache = get_cpu_cache(cpu_caches, g_shared->mem_cache_level);
if (!cache) {
if (warn_once(WARN_ONCE_CACHE_NONE))
pr_inf("%s: using built-in defaults as no suitable "
"cache found\n", name);
g_shared->mem_cache_size = MEM_CACHE_SIZE;
goto init_done;
}
if (g_shared->mem_cache_ways > 0) {
uint64_t way_size;
if (g_shared->mem_cache_ways > cache->ways) {
if (warn_once(WARN_ONCE_CACHE_WAY))
pr_inf("%s: cache way value too high - "
"defaulting to %d (the maximum)\n",
name, cache->ways);
g_shared->mem_cache_ways = cache->ways;
}
way_size = cache->size / cache->ways;
/* only fill the specified number of cache ways */
g_shared->mem_cache_size = way_size * g_shared->mem_cache_ways;
} else {
/* fill the entire cache */
g_shared->mem_cache_size = cache->size;
}
if (!g_shared->mem_cache_size) {
if (warn_once(WARN_ONCE_CACHE_DEFAULT))
pr_inf("%s: using built-in defaults as "
"unable to determine cache size\n", name);
g_shared->mem_cache_size = MEM_CACHE_SIZE;
}
init_done:
free_cpu_caches(cpu_caches);
#endif
g_shared->mem_cache = calloc(g_shared->mem_cache_size, 1);
if (!g_shared->mem_cache) {
pr_err("%s: failed to allocate shared cache buffer\n",
name);
return -1;
}
if (warn_once(WARN_ONCE_CACHE_SIZE))
pr_dbg("%s: default cache size: %" PRIu64 "K\n",
name, g_shared->mem_cache_size / 1024);
return 0;
}
/*
* stress_cache_free()
* free shared cache buffer
*/
void stress_cache_free(void)
{
free(g_shared->mem_cache);
}
/*
* system_write()
* write a buffer to a /sys or /proc entry
*/
int system_write(
const char *path,
const char *buf,
const size_t buf_len)
{
int fd;
ssize_t ret;
fd = open(path, O_WRONLY);
if (fd < 0)
return -errno;
ret = write(fd, buf, buf_len);
if (ret < (ssize_t)buf_len)
ret = -errno;
(void)close(fd);
return ret;
}
/*
* system_read()
* read a buffer from a /sys or /proc entry
*/
int system_read(
const char *path,
char *buf,
const size_t buf_len)
{
int fd;
ssize_t ret;
(void)memset(buf, 0, buf_len);
fd = open(path, O_RDONLY);
if (fd < 0)
return -errno;
ret = read(fd, buf, buf_len);
if (ret < 0)
ret = -errno;
(void)close(fd);
return ret;
}
/*
* stress_is_prime64()
* return true if 64 bit value n is prime
* http://en.wikipedia.org/wiki/Primality_test
*/
static inline bool stress_is_prime64(const uint64_t n)
{
register uint64_t i, max;
if (n <= 3)
return n >= 2;
if ((n % 2 == 0) || (n % 3 == 0))
return false;
max = sqrt(n) + 1;
for (i = 5; i < max; i+= 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
/*
* stress_get_prime64()
* find a prime that is not a multiple of n,
* used for file name striding
*/
uint64_t stress_get_prime64(const uint64_t n)
{
static uint p = 1009;
if (n != p)
return p;
/* Search for next prime.. */
for (;;) {
p += 2;
if ((n % p) && stress_is_prime64(p))
return p;
}
}
/*
* stress_get_file_limit()
* get max number of files that the current
* process can open;
*/
size_t stress_get_file_limit(void)
{
struct rlimit rlim;
size_t i, opened = 0, max = 65536; /* initial guess */
if (!getrlimit(RLIMIT_NOFILE, &rlim))
max = (size_t)rlim.rlim_cur;
/* Determine max number of free file descriptors we have */
for (i = 0; i < max; i++) {
if (fcntl((int)i, F_GETFL) > -1)
opened++;
}
return max - opened;
}
/*
* stress_sigaltstack()
* attempt to set up an alternative signal stack
* stack - must be at least 4K
* size - size of stack (- STACK_ALIGNMENT)
*/
int stress_sigaltstack(const void *stack, const size_t size)
{
#if defined(HAVE_SIGALTSTACK)
stack_t ss;
if (size < (size_t)MINSIGSTKSZ) {
pr_err("sigaltstack stack size %zu must be more than %zuK\n",
size, (size_t)MINSIGSTKSZ / 1024);
return -1;
}
ss.ss_sp = stress_align_address(stack, STACK_ALIGNMENT);
ss.ss_size = size;
ss.ss_flags = 0;
if (sigaltstack(&ss, NULL) < 0) {
pr_fail("sigaltstack failed: errno=%d (%s)\n",
errno, strerror(errno));
return -1;
}
#else
(void)stack;
(void)size;
#endif
return 0;
}
/*
* stress_sighandler()
* set signal handler in generic way
*/
int stress_sighandler(
const char *name,
const int signum,