forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-helper.c
4686 lines (4172 loc) · 101 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-2021 Canonical, Ltd.
* Copyright (C) 2022-2024 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#include "git-commit-id.h"
#include "core-bitops.h"
#include "core-builtin.h"
#include "core-attribute.h"
#include "core-capabilities.h"
#include "core-cpu-cache.h"
#include "core-hash.h"
#include "core-lock.h"
#include "core-numa.h"
#include "core-pthread.h"
#include "core-pragma.h"
#include "core-sort.h"
#include <sched.h>
#include <pwd.h>
#if defined(HAVE_LINUX_FIEMAP_H)
#include <linux/fiemap.h>
#endif
#if defined(HAVE_SYS_AUXV_H)
#include <sys/auxv.h>
#endif
#if defined(HAVE_SYS_CAPABILITY_H)
#include <sys/capability.h>
#endif
#if defined(HAVE_SYS_LOADAVG_H)
#include <sys/loadavg.h>
#endif
#if defined(HAVE_MACH_MACH_H)
#include <mach/mach.h>
#endif
#if defined(HAVE_MACH_VM_STATISTICS_H)
#include <mach/vm_statistics.h>
#endif
#if (defined(__FreeBSD__) || \
defined(__OpenBSD__)) && \
defined(HAVE_SYS_MOUNT_H)
#include <sys/mount.h>
#endif
#if defined(__FreeBSD__) && \
defined(HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#if defined(HAVE_SYS_PRCTL_H)
#include <sys/prctl.h>
#endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h>
#endif
#if defined(HAVE_SYS_SWAP_H) && \
!defined(__sun__)
#include <sys/swap.h>
#endif
#if defined(HAVE_SYS_SYSCTL_H)
STRESS_PRAGMA_PUSH
STRESS_PRAGMA_WARN_CPP_OFF
#include <sys/sysctl.h>
STRESS_PRAGMA_POP
#endif
#if defined(HAVE_SYS_UTSNAME_H)
#include <sys/utsname.h>
#endif
#if defined(HAVE_LINUX_FS_H)
#include <linux/fs.h>
#endif
#if defined(HAVE_SYS_VFS_H)
#include <sys/vfs.h>
#endif
#if defined(HAVE_LINUX_MAGIC_H)
#include <linux/magic.h>
#endif
#if defined(HAVE_UVM_UVM_EXTERN_H)
#include <uvm/uvm_extern.h>
#endif
/* prctl(2) timer slack support */
#if defined(HAVE_SYS_PRCTL_H) && \
defined(HAVE_PRCTL) && \
defined(PR_SET_TIMERSLACK) && \
defined(PR_GET_TIMERSLACK)
#define HAVE_PRCTL_TIMER_SLACK
#endif
#if defined(NSIG)
#define STRESS_NSIG NSIG
#elif defined(_NSIG)
#define STRESS_NSIG _NSIG
#endif
#if defined(HAVE_COMPILER_TCC) || defined(HAVE_COMPILER_PCC)
int __dso_handle;
#endif
#define MEM_CACHE_SIZE (2 * MB)
#define PAGE_4K_SHIFT (12)
#define PAGE_4K (1 << PAGE_4K_SHIFT)
#define STRESS_ABS_MIN_STACK_SIZE (64 * 1024)
const char ALIGN64 stress_ascii64[64] =
"0123456789ABCDEFGHIJKLMNOPQRSTUV"
"WXYZabcdefghijklmnopqrstuvwxyz@!";
const char ALIGN64 stress_ascii32[32] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ_+@:#!";
static bool stress_stack_check_flag;
typedef struct {
const unsigned long fs_magic;
const char * fs_name;
} stress_fs_name_t;
#if defined(HAVE_LINUX_MAGIC_H) && \
defined(HAVE_SYS_STATFS_H)
static const stress_fs_name_t stress_fs_names[] = {
#if defined(ADFS_SUPER_MAGIC)
{ ADFS_SUPER_MAGIC, "adfs" },
#endif
#if defined(AFFS_SUPER_MAGIC)
{ AFFS_SUPER_MAGIC, "affs" },
#endif
#if defined(AFS_SUPER_MAGIC)
{ AFS_SUPER_MAGIC, "afs" },
#endif
#if defined(AUTOFS_SUPER_MAGIC)
{ AUTOFS_SUPER_MAGIC, "autofs" },
#endif
#if defined(CEPH_SUPER_MAGIC)
{ CEPH_SUPER_MAGIC, "ceph" },
#endif
#if defined(CODA_SUPER_MAGIC)
{ CODA_SUPER_MAGIC, "coda" },
#endif
#if defined(CRAMFS_MAGIC)
{ CRAMFS_MAGIC, "cramfs" },
#endif
#if defined(CRAMFS_MAGIC_WEND)
{ CRAMFS_MAGIC, "cramfs" },
#endif
#if defined(DEBUGFS_MAGIC)
{ DEBUGFS_MAGIC, "debugfs" },
#endif
#if defined(SECURITYFS_MAGIC)
{ SECURITYFS_MAGIC, "securityfs" },
#endif
#if defined(SELINUX_MAGIC)
{ SELINUX_MAGIC, "selinux" },
#endif
#if defined(SMACK_MAGIC)
{ SMACK_MAGIC, "smack" },
#endif
#if defined(RAMFS_MAGIC)
{ RAMFS_MAGIC, "ramfs" },
#endif
#if defined(TMPFS_MAGIC)
{ TMPFS_MAGIC, "tmpfs" },
#endif
#if defined(HUGETLBFS_MAGIC)
{ HUGETLBFS_MAGIC, "hugetlbfs" },
#endif
#if defined(SQUASHFS_MAGIC)
{ SQUASHFS_MAGIC, "squashfs" },
#endif
#if defined(ECRYPTFS_SUPER_MAGIC)
{ ECRYPTFS_SUPER_MAGIC, "ecryptfs" },
#endif
#if defined(EFS_SUPER_MAGIC)
{ EFS_SUPER_MAGIC, "efs" },
#endif
#if defined(EROFS_SUPER_MAGIC_V1)
{ EROFS_SUPER_MAGIC_V1, "erofs" },
#endif
#if defined(EXT2_SUPER_MAGIC)
{ EXT2_SUPER_MAGIC, "ext2" },
#endif
#if defined(EXT3_SUPER_MAGIC)
{ EXT3_SUPER_MAGIC, "ext3" },
#endif
#if defined(XENFS_SUPER_MAGIC)
{ XENFS_SUPER_MAGIC, "xenfs" },
#endif
#if defined(EXT4_SUPER_MAGIC)
{ EXT4_SUPER_MAGIC, "ext4" },
#endif
#if defined(BTRFS_SUPER_MAGIC)
{ BTRFS_SUPER_MAGIC, "btrfs" },
#endif
#if defined(NILFS_SUPER_MAGIC)
{ NILFS_SUPER_MAGIC, "nilfs" },
#endif
#if defined(F2FS_SUPER_MAGIC)
{ F2FS_SUPER_MAGIC, "f2fs" },
#endif
#if defined(HPFS_SUPER_MAGIC)
{ HPFS_SUPER_MAGIC, "hpfs" },
#endif
#if defined(ISOFS_SUPER_MAGIC)
{ ISOFS_SUPER_MAGIC, "isofs" },
#endif
#if defined(JFFS2_SUPER_MAGIC)
{ JFFS2_SUPER_MAGIC, "jffs2" },
#endif
#if defined(XFS_SUPER_MAGIC)
{ XFS_SUPER_MAGIC, "xfs" },
#endif
#if defined(PSTOREFS_MAGIC)
{ PSTOREFS_MAGIC, "pstorefs" },
#endif
#if defined(EFIVARFS_MAGIC)
{ EFIVARFS_MAGIC, "efivars" },
#endif
#if defined(HOSTFS_SUPER_MAGIC)
{ HOSTFS_SUPER_MAGIC, "hostfs" },
#endif
#if defined(OVERLAYFS_SUPER_MAGIC)
{ OVERLAYFS_SUPER_MAGIC, "overlayfs" },
#endif
#if defined(FUSE_SUPER_MAGIC)
{ FUSE_SUPER_MAGIC, "fuse" },
#endif
#if defined(MINIX_SUPER_MAGIC)
{ MINIX_SUPER_MAGIC, "minix" },
#endif
#if defined(MINIX_SUPER_MAGIC2)
{ MINIX_SUPER_MAGIC2, "minix" },
#endif
#if defined(MINIX2_SUPER_MAGIC)
{ MINIX2_SUPER_MAGIC, "minix2" },
#endif
#if defined(MINIX3_SUPER_MAGIC)
{ MINIX3_SUPER_MAGIC, "minix3" },
#endif
#if defined(MSDOS_SUPER_MAGIC)
{ MSDOS_SUPER_MAGIC, "msdos" },
#endif
#if defined(EXFAT_SUPER_MAGIC)
{ EXFAT_SUPER_MAGIC, "exfat" },
#endif
#if defined(NCP_SUPER_MAGIC)
{ NCP_SUPER_MAGIC, "ncp" },
#endif
#if defined(NFS_SUPER_MAGIC)
{ NFS_SUPER_MAGIC, "nfs" },
#endif
#if defined(OCFS2_SUPER_MAGIC)
{ OCFS2_SUPER_MAGIC, "ocfs2" },
#endif
#if defined(OPENPROM_SUPER_MAGIC)
{ OPENPROM_SUPER_MAGIC, "openprom" },
#endif
#if defined(QNX4_SUPER_MAGIC)
{ QNX4_SUPER_MAGIC, "qnx4" },
#endif
#if defined(QNX6_SUPER_MAGIC)
{ QNX6_SUPER_MAGIC, "qnx6" },
#endif
#if defined(AFS_FS_MAGIC)
{ AFS_FS_MAGIC, "afs" },
#endif
#if defined(REISERFS_SUPER_MAGIC)
{ REISERFS_SUPER_MAGIC, "reiserfs" },
#endif
#if defined(SMB_SUPER_MAGIC)
{ SMB_SUPER_MAGIC, "smb" },
#endif
#if defined(CIFS_SUPER_MAGIC)
{ CIFS_SUPER_MAGIC, "cifs" },
#endif
#if defined(SMB2_SUPER_MAGIC)
{ SMB2_SUPER_MAGIC, "smb2" },
#endif
#if defined(CGROUP_SUPER_MAGIC)
{ CGROUP_SUPER_MAGIC, "cgroup" },
#endif
#if defined(CGROUP2_SUPER_MAGIC)
{ CGROUP2_SUPER_MAGIC, "cgroup2" },
#endif
#if defined(V9FS_MAGIC)
{ V9FS_MAGIC, "v9fs" },
#endif
#if defined(RDTGROUP_SUPER_MAGIC)
{ RDTGROUP_SUPER_MAGIC, "rdtgroup" },
#endif
#if defined(DAXFS_MAGIC)
{ DAXFS_MAGIC, "daxfs" },
#endif
#if defined(BINFMTFS_MAGIC)
{ BINFMTFS_MAGIC, "binfmtfs" },
#endif
#if defined(DEVPTS_SUPER_MAGIC)
{ DEVPTS_SUPER_MAGIC, "devpts" },
#endif
#if defined(BINDERFS_SUPER_MAGIC)
{ BINDERFS_SUPER_MAGIC, "binderfs" },
#endif
#if defined(FUTEXFS_SUPER_MAGIC)
{ FUTEXFS_SUPER_MAGIC, "futexfs" },
#endif
#if defined(PIPEFS_MAGIC)
{ PIPEFS_MAGIC, "pipefs" },
#endif
#if defined(PROC_SUPER_MAGIC)
{ PROC_SUPER_MAGIC, "proc" },
#endif
#if defined(SOCKFS_MAGIC)
{ SOCKFS_MAGIC, "sockfs" },
#endif
#if defined(SYSFS_MAGIC)
{ SYSFS_MAGIC, "sysfs" },
#endif
#if defined(NSFS_MAGIC)
{ NSFS_MAGIC, "nsfs" },
#endif
#if defined(BPF_FS_MAGIC)
{ BPF_FS_MAGIC, "bpf_fs" },
#endif
#if defined(AAFS_MAGIC)
{ AAFS_MAGIC, "aafs" },
#endif
#if defined(ZONEFS_MAGIC)
{ ZONEFS_MAGIC, "zonefs" },
#endif
#if defined(UDF_SUPER_MAGIC)
{ UDF_SUPER_MAGIC, "udf" },
#endif
#if defined(UBIFS_SUPER_MAGIC)
{ UBIFS_SUPER_MAGIC, "ubifs" },
#else
{ 0x24051905, "ubifs" },
#endif
{ 0x1badface, "bfs" },
#if defined(HFS_SUPER_MAGIC)
{ HFS_SUPER_MAGIC, "hfs" },
#else
{ 0x4244, "hfs" },
#endif
#if defined(HFSPLUS_SUPER_MAGIC)
{ HFSPLUS_SUPER_MAGIC, "hfsplus" },
#else
{ 0x482b, "hfsplus" },
#endif
#if defined(JFS_SUPER_MAGIC)
{ JFS_SUPER_MAGIC, "jfs" },
#else
{ 0x3153464a, "jfs" },
#endif
#if defined(USBDEVICE_SUPER_MAGIC)
{ USBDEVICE_SUPER_MAGIC, "usbdev" },
#endif
#if defined(MTD_INODE_FS_MAGIC)
{ MTD_INODE_FS_MAGIC, "mtd" },
#endif
#if defined(ANON_INODE_FS_MAGIC)
{ ANON_INODE_FS_MAGIC, "anon" },
#endif
#if defined(BCACHEFS_STATFS_MAGIC)
{ BCACHEFS_STATFS_MAGIC, "bcachefs" },
#else
{ 0xca451a4e, "bacachefs" },
#endif
};
#endif
typedef struct {
const int signum; /* signal number */
const char *name; /* human readable signal name */
} stress_sig_name_t;
#define SIG_NAME(x) { x, #x }
static const stress_sig_name_t sig_names[] = {
#if defined(SIGABRT)
SIG_NAME(SIGABRT),
#endif
#if defined(SIGALRM)
SIG_NAME(SIGALRM),
#endif
#if defined(SIGBUS)
SIG_NAME(SIGBUS),
#endif
#if defined(SIGCHLD)
SIG_NAME(SIGCHLD),
#endif
#if defined(SIGCLD)
SIG_NAME(SIGCLD),
#endif
#if defined(SIGCONT)
SIG_NAME(SIGCONT),
#endif
#if defined(SIGEMT)
SIG_NAME(SIGEMT),
#endif
#if defined(SIGFPE)
SIG_NAME(SIGFPE),
#endif
#if defined(SIGHUP)
SIG_NAME(SIGHUP),
#endif
#if defined(SIGILL)
SIG_NAME(SIGILL),
#endif
#if defined(SIGINFO)
SIG_NAME(SIGINFO),
#endif
#if defined(SIGINT)
SIG_NAME(SIGINT),
#endif
#if defined(SIGIO)
SIG_NAME(SIGIO),
#endif
#if defined(SIGIOT)
SIG_NAME(SIGIOT),
#endif
#if defined(SIGKILL)
SIG_NAME(SIGKILL),
#endif
#if defined(SIGLOST)
SIG_NAME(SIGLOST),
#endif
#if defined(SIGPIPE)
SIG_NAME(SIGPIPE),
#endif
#if defined(SIGPOLL)
SIG_NAME(SIGPOLL),
#endif
#if defined(SIGPROF)
SIG_NAME(SIGPROF),
#endif
#if defined(SIGPWR)
SIG_NAME(SIGPWR),
#endif
#if defined(SIGQUIT)
SIG_NAME(SIGQUIT),
#endif
#if defined(SIGSEGV)
SIG_NAME(SIGSEGV),
#endif
#if defined(SIGSTKFLT)
SIG_NAME(SIGSTKFLT),
#endif
#if defined(SIGSTOP)
SIG_NAME(SIGSTOP),
#endif
#if defined(SIGTSTP)
SIG_NAME(SIGTSTP),
#endif
#if defined(SIGSYS)
SIG_NAME(SIGSYS),
#endif
#if defined(SIGTERM)
SIG_NAME(SIGTERM),
#endif
#if defined(SIGTRAP)
SIG_NAME(SIGTRAP),
#endif
#if defined(SIGTTIN)
SIG_NAME(SIGTTIN),
#endif
#if defined(SIGTTOU)
SIG_NAME(SIGTTOU),
#endif
#if defined(SIGUNUSED)
SIG_NAME(SIGUNUSED),
#endif
#if defined(SIGURG)
SIG_NAME(SIGURG),
#endif
#if defined(SIGUSR1)
SIG_NAME(SIGUSR1),
#endif
#if defined(SIGUSR2)
SIG_NAME(SIGUSR2),
#endif
#if defined(SIGVTALRM)
SIG_NAME(SIGVTALRM),
#endif
#if defined(SIGXCPU)
SIG_NAME(SIGXCPU),
#endif
#if defined(SIGXFSZ)
SIG_NAME(SIGXFSZ),
#endif
#if defined(SIGWINCH)
SIG_NAME(SIGWINCH),
#endif
};
static char *stress_temp_path;
/*
* stress_temp_path_free()
* free and NULLify temporary file path
*/
void stress_temp_path_free(void)
{
if (stress_temp_path)
free(stress_temp_path);
stress_temp_path = NULL;
}
/*
* stress_set_temp_path()
* set temporary file path, default
* is . - current dir
*/
int stress_set_temp_path(const char *path)
{
stress_temp_path_free();
stress_temp_path = stress_const_optdup(path);
if (!stress_temp_path) {
(void)fprintf(stderr, "aborting: cannot allocate memory for '%s'\n", path);
return -1;
}
return 0;
}
/*
* stress_get_temp_path()
* get temporary file path, return "." if null
*/
const char *stress_get_temp_path(void)
{
if (!stress_temp_path)
return ".";
return stress_temp_path;
}
/*
* stress_check_temp_path()
* check if temp path is accessible
*/
int stress_check_temp_path(void)
{
const char *path = stress_get_temp_path();
if (access(path, R_OK | W_OK) < 0) {
(void)fprintf(stderr, "aborting: temp-path '%s' must be readable "
"and writeable\n", path);
return -1;
}
return 0;
}
/*
* stress_mk_filename()
* generate a full file name from a path and filename
*/
size_t stress_mk_filename(
char *fullname,
const size_t fullname_len,
const char *pathname,
const char *filename)
{
/*
* This may not be efficient, but it works. Do not
* be tempted to optimize this, it is not used frequently
* and is not a CPU bottleneck.
*/
(void)shim_strscpy(fullname, pathname, fullname_len);
(void)shim_strlcat(fullname, "/", fullname_len);
return shim_strlcat(fullname, filename, fullname_len);
}
/*
* stress_get_page_size()
* get page_size
*/
size_t stress_get_page_size(void)
{
static size_t page_size = 0;
/* Use cached size */
if (page_size > 0)
return page_size;
#if defined(_SC_PAGESIZE)
{
/* Use modern sysconf */
const long sz = sysconf(_SC_PAGESIZE);
if (sz > 0) {
page_size = (size_t)sz;
return page_size;
}
}
#else
UNEXPECTED
#endif
#if defined(HAVE_GETPAGESIZE)
{
/* Use deprecated getpagesize */
const 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;
UNEXPECTED
#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;
UNEXPECTED
#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 int32_t ticks_per_second = 0;
if (ticks_per_second > 0)
return ticks_per_second;
ticks_per_second = (int32_t)sysconf(_SC_CLK_TCK);
return ticks_per_second;
#else
UNEXPECTED
return -1;
#endif
}
/*
* stress_get_meminfo()
* wrapper for linux sysinfo
*/
static int stress_get_meminfo(
size_t *freemem,
size_t *totalmem,
size_t *freeswap,
size_t *totalswap)
{
#if defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO)
struct sysinfo info;
(void)shim_memset(&info, 0, sizeof(info));
if (sysinfo(&info) == 0) {
*freemem = info.freeram * info.mem_unit;
*totalmem = info.totalram * info.mem_unit;
*freeswap = info.freeswap * info.mem_unit;
*totalswap = info.totalswap * info.mem_unit;
return 0;
}
#endif
#if defined(__FreeBSD__)
{
const size_t page_size = (size_t)stress_bsd_getsysctl_uint("vm.stats.vm.v_page_size");
#if 0
/*
* Enable total swap only when we can determine free swap
*/
const size_t max_size_t = (size_t)-1;
const uint64_t vm_swap_total = stress_bsd_getsysctl_uint64("vm.swap_total");
*totalswap = (vm_swap_total >= max_size_t) ? max_size_t : (size_t)vm_swap_total;
#endif
*freemem = page_size * stress_bsd_getsysctl_uint32("vm.stats.vm.v_free_count");
*totalmem = page_size *
(stress_bsd_getsysctl_uint32("vm.stats.vm.v_active_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_inactive_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_laundry_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_wire_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_free_count"));
*freeswap = 0;
*totalswap = 0;
return 0;
}
#endif
#if defined(__NetBSD__) && \
defined(HAVE_UVM_UVM_EXTERN_H)
{
struct uvmexp_sysctl u;
if (stress_bsd_getsysctl("vm.uvmexp2", &u, sizeof(u)) == 0) {
*freemem = (size_t)u.free * u.pagesize;
*totalmem = (size_t)u.npages * u.pagesize;
*totalswap = (size_t)u.swpages * u.pagesize;
*freeswap = *totalswap - (size_t)u.swpginuse * u.pagesize;
return 0;
}
}
#endif
#if defined(__APPLE__) && \
defined(HAVE_MACH_MACH_H) && \
defined(HAVE_MACH_VM_STATISTICS_H)
{
vm_statistics64_data_t vm_stat;
mach_port_t host = mach_host_self();
natural_t count = HOST_VM_INFO64_COUNT;
size_t page_size = stress_get_page_size();
int ret;
/* zero vm_stat, keep cppcheck silent */
(void)shim_memset(&vm_stat, 0, sizeof(vm_stat));
ret = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stat, &count);
if (ret >= 0) {
*freemem = page_size * vm_stat.free_count;
*totalmem = page_size * (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count +
vm_stat.zero_fill_count);
return 0;
}
}
#endif
*freemem = 0;
*totalmem = 0;
*freeswap = 0;
*totalswap = 0;
return -1;
}
/*
* 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,
size_t *totalswap)
{
#if defined(__linux__)
char buf[64];
#endif
(void)stress_get_meminfo(freemem, totalmem, freeswap, totalswap);
#if defined(__linux__)
if (stress_system_read("/proc/sys/kernel/shmall", buf, sizeof(buf)) > 0) {
if (sscanf(buf, "%zu", shmall) == 1)
return;
}
#endif
*shmall = 0;
}
/*
* stress_get_gpu_freq_mhz()
* get GPU frequency in MHz, set to 0.0 if not readable
*/
void stress_get_gpu_freq_mhz(double *gpu_freq)
{
#if defined(__linux__)
char buf[64];
if (stress_system_read("/sys/class/drm/card0/gt_cur_freq_mhz", buf, sizeof(buf)) > 0) {
if (sscanf(buf, "%lf", gpu_freq) == 1)
return;
} else if (stress_system_read("/sys/class/drm/card0/gt_cur_freq_mhz", buf, sizeof(buf)) > 0) {
if (sscanf(buf, "%lf", gpu_freq) == 1)
return;
}
#endif
*gpu_freq = 0.0;
}
#if !defined(PR_SET_MEMORY_MERGE)
#define PR_SET_MEMORY_MERGE (67)
#endif
/*
* stress_ksm_memory_merge()
* set kernel samepage merging flag (linux only)
*/
void stress_ksm_memory_merge(const int flag)
{
#if defined(__linux__) && \
defined(PR_SET_MEMORY_MERGE) && \
defined(HAVE_SYS_PRCTL_H)
if ((flag >= 0) && (flag <= 1)) {
static int prev_flag = -1;
if (flag != prev_flag) {
VOID_RET(int, prctl(PR_SET_MEMORY_MERGE, flag));
prev_flag = flag;
}
(void)stress_system_write("/sys/kernel/mm/ksm/run", "1\n", 2);
}
#else
(void)flag;
#endif
}
/*
* stress_low_memory()
* return true if running low on memory
*/
bool stress_low_memory(const size_t requested)
{
static size_t prev_freemem = 0;
static size_t prev_freeswap = 0;
size_t freemem, totalmem, freeswap, totalswap;
static double threshold = -1.0;
bool low_memory = false;
if (stress_get_meminfo(&freemem, &totalmem, &freeswap, &totalswap) == 0) {
/*
* Threshold not set, then get
*/
if (threshold < 0.0) {
size_t bytes = 0;
if (stress_get_setting("oom-avoid-bytes", &bytes)) {
threshold = 100.0 * (double)bytes / (double)freemem;
} else {
/* Not specified, then default to 2.5% */
threshold = 2.5;
}
}
/*
* Stats from previous call valid, then check for memory
* changes
*/
if ((prev_freemem + prev_freeswap) > 0) {
ssize_t delta;
delta = (ssize_t)prev_freemem - (ssize_t)freemem;
delta = (delta * 2) + requested;
/* memory shrinking quickly? */
if (delta > (ssize_t)freemem) {
low_memory = true;
goto update;
}
/* swap shrinking quickly? */
delta = (ssize_t)prev_freeswap - (ssize_t)freeswap;
if (delta > 0) {
low_memory = true;
goto update;
}
}
/* Not enough for allocation and slop? */
if (freemem < ((4 * MB) + requested)) {
low_memory = true;
goto update;
}
/* Less than 3% left? */
if (((double)freemem * 100.0 / (double)(totalmem - requested)) < threshold) {
low_memory = true;
goto update;
}
/* Any swap enabled with free memory we are too low? */
if ((totalswap > 0) && (freeswap + freemem < (requested + (2 * MB)))) {
low_memory = true;
goto update;
}
update:
prev_freemem = freemem;
prev_freeswap = freeswap;
/* low memory? automatically enable ksm memory merging */
if (low_memory)
stress_ksm_memory_merge(1);
}
return low_memory;
}
#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_page_size();
const uint64_t max_pages = ~0ULL / page_size;
phys_pages = (uint64_t)sysconf(STRESS_SC_PAGES);
/* Avoid overflow */
if (phys_pages > max_pages)
phys_pages = max_pages;
return phys_pages * page_size;
#else
UNEXPECTED
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;
const char *path = stress_get_temp_path();
if (!path)
return 0;
(void)shim_memset(&buf, 0, sizeof(buf));
rc = statvfs(path, &buf);
if (rc < 0)
return 0;
max_blocks = (~(fsblkcnt_t)0) / buf.f_bsize;
blocks = buf.f_bavail;
if (blocks > max_blocks)