-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathomrsysinfo.c
6341 lines (5582 loc) · 202 KB
/
omrsysinfo.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) 2015, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
/**
* @file
* @ingroup Port
* @brief System information
*/
#if 0
#define ENV_DEBUG
#endif
#if defined(LINUX) && !defined(OMRZTPF)
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#elif defined(OSX)
#define _XOPEN_SOURCE
#include <libproc.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>
#include <mach-o/dyld.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif /* defined(LINUX) && !defined(OMRZTPF) */
#if defined(OSX) || defined(OMR_OS_BSD)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif /* defined(OSX) || defined(OMR_OS_BSD) */
#if defined(OSX)
#include <crt_externs.h>
#endif /* defined(OSX) */
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include "omrport.h"
#if defined(J9OS_I5)
#include "Xj9I5OSInterface.H"
#endif
#if !defined(J9ZOS390)
#include <sys/param.h>
#endif /* !defined(J9ZOS390) */
#include <sys/time.h>
#include <sys/resource.h>
#include <nl_types.h>
#include <langinfo.h>
#if defined(J9ZOS390)
#include "omrsimap.h"
#endif /* defined(J9ZOS390) */
#if defined(LINUXPPC) || (defined(S390) && defined(LINUX) && !defined(J9ZTPF)) || (defined(AARCH64) && defined(LINUX))
#include "auxv.h"
#include <strings.h>
#endif /* defined(LINUXPPC) || (defined(S390) && defined(LINUX) && !defined(J9ZTPF)) || (defined(AARCH64) && defined(LINUX)) */
#if (defined(S390))
#include "omrportpriv.h"
#include "omrportpg.h"
#endif /* defined(S390) */
#if defined(AIXPPC)
#include <fcntl.h>
#include <sys/procfs.h>
#include <sys/systemcfg.h>
#endif /* defined(AIXPPC) */
#include "omrsysinfo_helpers.h"
/* Start copy from omrfiletext.c */
/* __STDC_ISO_10646__ indicates that the platform wchar_t encoding is Unicode */
/* but older versions of libc fail to set the flag, even though they are Unicode */
#if defined(__STDC_ISO_10646__) || defined(LINUX) ||defined(OSX)
#define J9VM_USE_MBTOWC
#else /* defined(__STDC_ISO_10646__) || defined(LINUX) || defined(OSX) */
#include "omriconvhelpers.h"
#endif /* defined(__STDC_ISO_10646__) || defined(LINUX) || defined(OSX) */
/* a2e overrides nl_langinfo to return ASCII strings. We need the native EBCDIC string */
#if defined(J9ZOS390) && defined(nl_langinfo)
#undef nl_langinfo
#endif
#if defined(J9ZOS390)
#include "omrgetuserid.h"
#endif
/* End copy from omrfiletext.c */
#ifdef AIXPPC
#if defined(J9OS_I5)
/* The PASE compiler does not support libperfstat.h */
#else
#include <libperfstat.h>
#endif
#include <sys/proc.h>
/* omrsysinfo_get_number_CPUs_by_type */
#include <sys/thread.h>
#include <sys/rset.h>
#endif
#ifdef RS6000
#include <sys/systemcfg.h>
#include <sys/sysconfig.h>
#include <assert.h>
#if defined(OMR_ENV_DATA64)
#define LIBC_NAME "/usr/lib/libc.a(shr_64.o)"
#else
#define LIBC_NAME "/usr/lib/libc.a(shr.o)"
#endif
#endif
#if defined(LINUX) && !defined(OMRZTPF)
#include <linux/magic.h>
#include <sys/sysinfo.h>
#include <sys/vfs.h>
#include <sched.h>
#elif defined(OSX)
#include <sys/sysctl.h>
#endif /* defined(LINUX) && !defined(OMRZTPF) */
#include <unistd.h>
#if defined(LINUX)
#include "omrcgroup.h"
#endif /* defined(LINUX) */
#include "omrportpriv.h"
#include "omrportpg.h"
#include "omrportptb.h"
#include "ut_omrport.h"
#if defined(OMRZTPF)
#include <locale.h>
#include <tpf/c_cinfc.h>
#include <tpf/c_dctist.h>
#include <tpf/tpfapi.h>
#include <tpf/sysapi.h>
#endif /* defined(OMRZTPF) */
#if defined(J9ZOS390)
#include <sys/ps.h>
#include <sys/types.h>
#include "atoe.h"
#if !defined(PATH_MAX)
/* This is a somewhat arbitrarily selected fixed buffer size. */
#define PATH_MAX 1024
#endif
#pragma linkage (GETNCPUS,OS)
#pragma map (Get_Number_Of_CPUs,"GETNCPUS")
uintptr_t Get_Number_Of_CPUs();
#endif
#define JIFFIES 100
#define USECS_PER_SEC 1000000
#define TICKS_TO_USEC ((uint64_t)(USECS_PER_SEC/JIFFIES))
static uintptr_t copyEnvToBuffer(struct OMRPortLibrary *portLibrary, void *args);
static uintptr_t copyEnvToBufferSignalHandler(struct OMRPortLibrary *portLib, uint32_t gpType, void *gpInfo, void *unUsed);
static void setPortableError(OMRPortLibrary *portLibrary, const char *funcName, int32_t portlibErrno, int systemErrno);
#if (defined(LINUXPPC) || defined(AIXPPC))
static OMRProcessorArchitecture omrsysinfo_map_ppc_processor(const char *processorName);
const char* omrsysinfo_get_ppc_processor_feature_name(uint32_t feature);
#endif /* (defined(LINUXPPC) || defined(AIXPPC)) */
#if (defined(AIXPPC) || defined(S390) || defined(J9ZOS390))
static void omrsysinfo_set_feature(OMRProcessorDesc *desc, uint32_t feature);
#endif /* defined(AIXPPC) || defined(S390) || defined(J9ZOS390) */
#if defined(LINUXPPC)
static intptr_t omrsysinfo_get_linux_ppc_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc);
#if !defined(AT_HWCAP2)
#define AT_HWCAP2 26 /* needed until glibc 2.17 */
#endif /* !defined(AT_HWCAP2) */
#endif /* defined(LINUXPPC) */
#if defined(AIXPPC)
static intptr_t omrsysinfo_get_aix_ppc_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc);
#endif /* defined(AIXPPC) */
#if !defined(__power_8)
#define POWER_8 0x10000 /* Power 8 class CPU */
#define __power_8() (_system_configuration.implementation == POWER_8)
#if !defined(J9OS_I5_V6R1)
#define PPI8_1 0x4B
#define PPI8_2 0x4D
#define __phy_proc_imp_8() (_system_configuration.phys_implementation == PPI8_1 || _system_configuration.phys_implementation == PPI8_2)
#endif /* !defined(J9OS_I5_V6R1) */
#endif /* !defined(__power_8) */
#if !defined(__power_9)
#define POWER_9 0x20000 /* Power 9 class CPU */
#define __power_9() (_system_configuration.implementation == POWER_9)
#endif /* !defined(__power_9) */
#if !defined(__power_10)
#define POWER_10 0x40000 /* Power 10 class CPU */
#define __power_10() (_system_configuration.implementation == POWER_10)
#endif /* !defined(__power_10) */
#if defined(J9OS_I5_V6R1) /* vmx_version id only available since TL4 */
#define __power_vsx() (_system_configuration.vmx_version > 1)
#endif
#if !defined(J9OS_I5_V7R2) && !defined(J9OS_I5_V6R1)
/* both i 7.1 and i 7.2 do not support this function */
#if !defined(SC_TM_VER)
#define SC_TM_VER 59
#endif /* !defined(SC_TM_VER) */
#if !defined(__power_tm)
#define __power_tm() ((long)getsystemcfg(SC_TM_VER) > 0) /* taken from AIX 7.1 sys/systemcfg.h */
#endif /* !defined(__power_tm) */
#endif /* !defined(J9OS_I5_V7R2) && !defined(J9OS_I5_V6R1) */
#if (defined(S390) || defined(J9ZOS390) || defined(J9ZTPF))
static BOOLEAN omrsysinfo_test_stfle(struct OMRPortLibrary *portLibrary, uint64_t stfleBit);
static intptr_t omrsysinfo_get_s390_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc);
const char * omrsysinfo_get_s390_processor_feature_name(uint32_t feature);
#endif /* defined(S390) || defined(J9ZOS390) || defined(J9ZTPF) */
#if defined(RISCV)
static intptr_t omrsysinfo_get_riscv_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc);
#endif
#if defined(AARCH64) && defined(LINUX)
static const char *omrsysinfo_get_aarch64_processor_feature_name(uint32_t feature);
static intptr_t omrsysinfo_get_linux_aarch64_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc);
#endif /* defined(AARCH64) && defined(LINUX) */
static const char getgroupsErrorMsgPrefix[] = "getgroups : ";
typedef struct EnvListItem {
struct EnvListItem *next;
char *nameAndValue;
} EnvListItem;
typedef struct CopyEnvToBufferArgs {
uintptr_t bufferSizeBytes;
void *buffer;
uintptr_t numElements;
} CopyEnvToBufferArgs;
/* For the omrsysinfo_limit_iterator */
struct {
int resource;
char *resourceName;
} limitMap[] = {
#if defined(RLIMIT_AS)
{RLIMIT_AS, "RLIMIT_AS"},
#endif
#if defined(RLIMIT_CORE)
{RLIMIT_CORE, "RLIMIT_CORE"},
#endif
#if defined(RLIMIT_CPU)
{RLIMIT_CPU, "RLIMIT_CPU"},
#endif
#if defined(RLIMIT_DATA)
{RLIMIT_DATA, "RLIMIT_DATA"},
#endif
#if defined(RLIMIT_FSIZE)
{RLIMIT_FSIZE, "RLIMIT_FSIZE"},
#endif
#if defined(RLIMIT_LOCKS)
{RLIMIT_LOCKS, "RLIMIT_LOCKS"},
#endif
#if defined(RLIMIT_MEMLOCK)
{RLIMIT_MEMLOCK, "RLIMIT_MEMLOCK"},
#endif
#if defined(RLIMIT_NOFILE)
{RLIMIT_NOFILE, "RLIMIT_NOFILE"},
#endif
#if defined(RLIMIT_THREADS)
{RLIMIT_THREADS, "RLIMIT_THREADS"},
#endif
#if defined(RLIMIT_NPROC)
{RLIMIT_NPROC, "RLIMIT_NPROC"},
#endif
#if defined(RLIMIT_RSS)
{RLIMIT_RSS, "RLIMIT_RSS"},
#endif
#if defined(RLIMIT_STACK)
{RLIMIT_STACK, "RLIMIT_STACK"},
#endif
#if defined(RLIMIT_MSGQUEUE)
{RLIMIT_MSGQUEUE, "RLIMIT_MSGQUEUE"}, /* since Linux 2.6.8 */
#endif
#if defined(RLIMIT_NICE)
{RLIMIT_NICE, "RLIMIT_NICE"}, /* since Linux 2.6.12*/
#endif
#if defined(RLIMIT_RTPRIO)
{RLIMIT_RTPRIO, "RLIMIT_RTPRIO"}, /* since Linux 2.6.12 */
#endif
#if defined(RLIMIT_SIGPENDING)
{RLIMIT_SIGPENDING, "RLIMIT_SIGPENDING"}, /* since linux 2.6.8 */
#endif
#if defined(RLIMIT_MEMLIMIT)
{RLIMIT_MEMLIMIT, "RLIMIT_MEMLIMIT"}, /* likely z/OS only */
#endif
};
#if defined(LINUX)
#define OMR_CGROUP_V1_MOUNT_POINT "/sys/fs/cgroup"
#define ROOT_CGROUP "/"
#define SYSTEMD_INIT_CGROUP "/init.scope"
#define OMR_PROC_PID_ONE_CGROUP_FILE "/proc/1/cgroup"
#define MAX_DEFAULT_VALUE_CHECK (LLONG_MAX - (1024 * 1024 * 1024)) /* subtracting the MAX page size (1GB) from LLONG_MAX to check against a value */
#define CGROUP_METRIC_FILE_CONTENT_MAX_LIMIT 1024
/* An entry in /proc/<pid>/cgroup is of following form:
* <hierarchy ID>:<subsystem>[,<subsystem>]*:<cgroup name>
*
* An example:
* 7:cpuacct,cpu:/mycgroup
*/
#define PROC_PID_CGROUP_ENTRY_FORMAT "%d:%[^:]:%s"
#define PROC_PID_CGROUP_SYSTEMD_ENTRY_FORMAT "%d::%s"
#define SINGLE_CGROUP_METRIC 1
/* Currently 12 subsystems or resource controllers are defined.
*/
typedef enum OMRCgroupSubsystem {
INVALID_SUBSYSTEM = -1,
BLKIO,
CPU,
CPUACCT,
CPUSET,
DEVICES,
FREEZER,
HUGETLB,
MEMORY,
NET_CLS,
NET_PRIO,
PERF_EVENT,
PIDS,
NUM_SUBSYSTEMS
} OMRCgroupSubsystem;
const char * const subsystemNames[NUM_SUBSYSTEMS] = {
"blkio",
"cpu",
"cpuacct",
"cpuset",
"devices",
"freezer",
"hugetlb",
"memory",
"net_cls",
"net_prio",
"perf_event",
"pids",
};
struct {
const char *name;
uint64_t flag;
} supportedSubsystems[] = {
{ "cpu", OMR_CGROUP_SUBSYSTEM_CPU },
{ "memory", OMR_CGROUP_SUBSYSTEM_MEMORY },
{ "cpuset", OMR_CGROUP_SUBSYSTEM_CPUSET}
};
typedef struct OMRCgroupMetricInfoElement {
char *metricTag;
char *metricKeyInFile;
char *metricUnit;
BOOLEAN isValueToBeChecked;
} OMRCgroupMetricInfoElement;
static struct OMRCgroupMetricInfoElement oomControlMetricElementList[] = {
{ "OOM Killer Disabled", "oom_kill_disable", NULL, FALSE },
{ "Under OOM", "under_oom", NULL, FALSE }
};
static struct OMRCgroupMetricInfoElement cpuStatMetricElementList[] = {
{ "Period intervals elapsed count", "nr_periods", NULL, FALSE },
{ "Throttled count", "nr_throttled", NULL, FALSE },
{ "Total throttle time", "throttled_time", "nanoseconds", FALSE }
};
typedef struct OMRCgroupSubsystemMetricMap {
char *metricFileName;
OMRCgroupMetricInfoElement *metricInfoElementList;
int32_t metricElementsCount;
} OMRCgroupSubsystemMetricMap;
static struct OMRCgroupSubsystemMetricMap omrCgroupMemoryMetricMap[] = {
{ "memory.limit_in_bytes", &(OMRCgroupMetricInfoElement){ "Memory Limit", NULL, "bytes", TRUE }, SINGLE_CGROUP_METRIC },
{ "memory.memsw.limit_in_bytes", &(OMRCgroupMetricInfoElement){ "Memory + Swap Limit", NULL, "bytes", TRUE }, SINGLE_CGROUP_METRIC },
{ "memory.usage_in_bytes", &(OMRCgroupMetricInfoElement){ "Memory Usage", NULL, "bytes", FALSE }, SINGLE_CGROUP_METRIC },
{ "memory.memsw.usage_in_bytes", &(OMRCgroupMetricInfoElement){ "Memory + Swap Usage", NULL, "bytes", FALSE }, SINGLE_CGROUP_METRIC },
{ "memory.max_usage_in_bytes", &(OMRCgroupMetricInfoElement){ "Memory Max Usage", NULL, "bytes", FALSE }, SINGLE_CGROUP_METRIC },
{ "memory.memsw.max_usage_in_bytes", &(OMRCgroupMetricInfoElement){ "Memory + Swap Max Usage", NULL, "bytes", FALSE }, SINGLE_CGROUP_METRIC },
{ "memory.failcnt", &(OMRCgroupMetricInfoElement){ "Memory limit exceeded count", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC },
{ "memory.memsw.failcnt", &(OMRCgroupMetricInfoElement){ "Memory + Swap limit exceeded count", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC },
{ "memory.oom_control", &oomControlMetricElementList[0], sizeof(oomControlMetricElementList)/sizeof(oomControlMetricElementList[0]) }
};
static struct OMRCgroupSubsystemMetricMap omrCgroupCpuMetricMap[] = {
{ "cpu.cfs_period_us", &(OMRCgroupMetricInfoElement){ "CPU Period", NULL, "microseconds", FALSE }, SINGLE_CGROUP_METRIC },
{ "cpu.cfs_quota_us", &(OMRCgroupMetricInfoElement){ "CPU Quota", NULL, "microseconds", TRUE }, SINGLE_CGROUP_METRIC },
{ "cpu.shares", &(OMRCgroupMetricInfoElement){ "CPU Shares", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC },
{ "cpu.stat", &cpuStatMetricElementList[0], sizeof(cpuStatMetricElementList)/sizeof(cpuStatMetricElementList[0]) }
};
static struct OMRCgroupSubsystemMetricMap omrCgroupCpusetMetricMap[] = {
{ "cpuset.cpu_exclusive", &(OMRCgroupMetricInfoElement){ "CPU exclusive", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC },
{ "cpuset.mem_exclusive", &(OMRCgroupMetricInfoElement){ "Mem exclusive", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC },
{ "cpuset.cpus", &(OMRCgroupMetricInfoElement){ "CPUs", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC },
{ "cpuset.mems", &(OMRCgroupMetricInfoElement){ "Mems", NULL, NULL, FALSE }, SINGLE_CGROUP_METRIC }
};
static uint32_t attachedPortLibraries;
static omrthread_monitor_t cgroupEntryListMonitor;
#endif /* defined(LINUX) */
static intptr_t cwdname(struct OMRPortLibrary *portLibrary, char **result);
static uint32_t getLimitSharedMemory(struct OMRPortLibrary *portLibrary, uint64_t *limit);
static uint32_t getLimitFileDescriptors(struct OMRPortLibrary *portLibrary, uint64_t *result, BOOLEAN hardLimitRequested);
static uint32_t setLimitFileDescriptors(struct OMRPortLibrary *portLibrary, uint64_t limit, BOOLEAN hardLimitRequested);
#if defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390)
static intptr_t readSymbolicLink(struct OMRPortLibrary *portLibrary, char *linkFilename, char **result);
#endif /* defined(LINUX) || defined(AIXPPC) || defined(J9ZOS390) */
#if defined(AIXPPC) || defined(J9ZOS390)
static BOOLEAN isSymbolicLink(struct OMRPortLibrary *portLibrary, char *filename);
static intptr_t searchSystemPath(struct OMRPortLibrary *portLibrary, char *filename, char **result);
#endif /* defined(AIXPPC) || defined(J9ZOS390) */
#if defined(J9ZOS390)
static void setOSFeature(struct OMROSDesc *desc, uint32_t feature);
static intptr_t getZOSDescription(struct OMRPortLibrary *portLibrary, struct OMROSDesc *desc);
#endif /* defined(J9ZOS390) */
#if !defined(RS6000) && !defined(J9ZOS390) && !defined(OSX) && !defined(OMRZTPF)
static uint64_t getPhysicalMemory(struct OMRPortLibrary *portLibrary);
#elif defined(OMRZTPF) /* !defined(RS6000) && !defined(J9ZOS390) && !defined(OSX) && !defined(OMRZTPF) */
static uint64_t getPhysicalMemory();
#endif /* !defined(RS6000) && !defined(J9ZOS390) && !defined(OSX) && !defined(OMRZTPF) */
#if defined(OMRZTPF)
uintptr_t get_IPL_IstreamCount();
uintptr_t get_Dispatch_IstreamCount();
#endif /* defined(OMRZTPF) */
#if defined(LINUX) && !defined(OMRZTPF)
static BOOLEAN isCgroupV1Available(struct OMRPortLibrary *portLibrary);
static void freeCgroupEntries(struct OMRPortLibrary *portLibrary, OMRCgroupEntry *cgEntryList);
static char * getCgroupNameForSubsystem(struct OMRPortLibrary *portLibrary, OMRCgroupEntry *cgEntryList, const char *subsystem);
static int32_t addCgroupEntry(struct OMRPortLibrary *portLibrary, OMRCgroupEntry **cgEntryList, int32_t hierId, const char *subsystem, const char *cgroupName, uint64_t flag);
static int32_t readCgroupFile(struct OMRPortLibrary *portLibrary, int pid, BOOLEAN inContainer, OMRCgroupEntry **cgroupEntryList, uint64_t *availableSubsystems);
static OMRCgroupSubsystem getCgroupSubsystemFromFlag(uint64_t subsystemFlag);
static int32_t getAbsolutePathOfCgroupSubsystemFile(struct OMRPortLibrary *portLibrary, uint64_t subsystemFlag, const char *fileName, char *fullPath, intptr_t *bufferLength);
static int32_t getHandleOfCgroupSubsystemFile(struct OMRPortLibrary *portLibrary, uint64_t subsystemFlag, const char *fileName, FILE **subsystemFile);
static int32_t readCgroupMetricFromFile(struct OMRPortLibrary *portLibrary, uint64_t subsystemFlag, const char *fileName, const char *metricKeyInFile, char **fileContent, char *value);
static int32_t readCgroupSubsystemFile(struct OMRPortLibrary *portLibrary, uint64_t subsystemFlag, const char *fileName, int32_t numItemsToRead, const char *format, ...);
static int32_t isRunningInContainer(struct OMRPortLibrary *portLibrary, BOOLEAN *inContainer);
static int32_t getCgroupMemoryLimit(struct OMRPortLibrary *portLibrary, uint64_t *limit);
#endif /* defined(LINUX) */
#if defined(LINUX)
static int32_t retrieveLinuxMemoryStatsFromProcFS(struct OMRPortLibrary *portLibrary, struct J9MemoryInfo *memInfo);
static int32_t retrieveLinuxCgroupMemoryStats(struct OMRPortLibrary *portLibrary, struct OMRCgroupMemoryInfo *cgroupMemInfo);
static int32_t retrieveLinuxMemoryStats(struct OMRPortLibrary *portLibrary, struct J9MemoryInfo *memInfo);
#elif defined(OSX)
static int32_t retrieveOSXMemoryStats(struct OMRPortLibrary *portLibrary, struct J9MemoryInfo *memInfo);
#elif defined(AIXPPC)
static int32_t retrieveAIXMemoryStats(struct OMRPortLibrary *portLibrary, struct J9MemoryInfo *memInfo);
#endif
/**
* @internal
* Determines the proper portable error code to return given a native error code
*
* @param[in] errorCode The error code reported by the OS
*
* @return the (negative) portable error code
*/
static int32_t
findError(int32_t errorCode)
{
switch (errorCode) {
case EACCES:
/* FALLTHROUGH */
case EPERM:
return OMRPORT_ERROR_FILE_NOPERMISSION;
case EFAULT:
return OMRPORT_ERROR_FILE_EFAULT;
case EINVAL:
return OMRPORT_ERROR_FILE_INVAL;
case EBADF:
return OMRPORT_ERROR_FILE_BADF;
case ENOENT:
return OMRPORT_ERROR_FILE_NOENT;
case ENOTDIR:
return OMRPORT_ERROR_FILE_NOTDIR;
case ENOMEM:
return OMRPORT_ERROR_FILE_INSUFFICIENT_BUFFER;
case EMFILE:
/* FALLTHROUGH */
case ENFILE:
return OMRPORT_ERROR_FILE_TOO_MANY_OPEN_FILES;
default:
return OMRPORT_ERROR_FILE_OPFAILED;
}
}
void
omrsysinfo_set_number_user_specified_CPUs(struct OMRPortLibrary *portLibrary, uintptr_t number)
{
Trc_PRT_sysinfo_set_number_user_specified_CPUs_Entered();
portLibrary->portGlobals->userSpecifiedCPUs = number;
Trc_PRT_sysinfo_set_number_user_specified_CPUs_Exit(number);
}
intptr_t
omrsysinfo_process_exists(struct OMRPortLibrary *portLibrary, uintptr_t pid)
{
intptr_t rc = 0;
int result = 0;
result = kill(pid, 0);
/*
* If sig is 0, then no signal is sent, but error checking is still performed.
* By using signal == 0, kill just checks for the existence and accessibility of the process.
*/
if (0 == result) {
rc = 1; /* The process with pid exists and this process has sufficient permissions to send it a signal. */
} else if (-1 == result) { /* note that kill() returns only 0 or -1 */
if (errno == ESRCH) {
/* The kill() failed because was there are no processes or process groups corresponding to pid. */
rc = 0;
} else if (errno == EPERM) {
/* The target process exists but this process does not have permission to send it a signal. */
rc = 1;
} else {
rc = -1;
}
}
return rc;
}
const char *
omrsysinfo_get_CPU_architecture(struct OMRPortLibrary *portLibrary)
{
#if defined(RS6000) || defined(LINUXPPC)
#ifdef PPC64
#ifdef OMR_ENV_LITTLE_ENDIAN
return OMRPORT_ARCH_PPC64LE;
#else /* OMR_ENV_LITTLE_ENDIAN */
return OMRPORT_ARCH_PPC64;
#endif /* OMR_ENV_LITTLE_ENDIAN */
#else
return OMRPORT_ARCH_PPC;
#endif /* PPC64*/
#elif defined(J9X86)
return OMRPORT_ARCH_X86;
#elif defined(S390) || defined(J9ZOS390)
#if defined(S39064) || defined(J9ZOS39064)
return OMRPORT_ARCH_S390X;
#else
return OMRPORT_ARCH_S390;
#endif /* S39064 || J9ZOS39064 */
#elif defined(J9HAMMER)
return OMRPORT_ARCH_HAMMER;
#elif defined(J9ARM)
return OMRPORT_ARCH_ARM;
#elif defined(J9AARCH64)
return OMRPORT_ARCH_AARCH64;
#elif defined(RISCV)
return OMRPORT_ARCH_RISCV;
#else
return "unknown";
#endif
}
intptr_t
omrsysinfo_get_processor_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc)
{
intptr_t rc = -1;
Trc_PRT_sysinfo_get_processor_description_Entered(desc);
if (NULL != desc) {
memset(desc, 0, sizeof(OMRProcessorDesc));
#if (defined(J9X86) || defined(J9HAMMER))
rc = omrsysinfo_get_x86_description(portLibrary, desc);
#elif defined(LINUXPPC)
rc = omrsysinfo_get_linux_ppc_description(portLibrary, desc);
#elif defined(AIXPPC)
rc = omrsysinfo_get_aix_ppc_description(portLibrary, desc);
#elif (defined(S390) || defined(J9ZOS390))
rc = omrsysinfo_get_s390_description(portLibrary, desc);
#elif defined(RISCV)
rc = omrsysinfo_get_riscv_description(portLibrary, desc);
#elif (defined(AARCH64) && defined(LINUX))
rc = omrsysinfo_get_linux_aarch64_description(portLibrary, desc);
#endif
}
Trc_PRT_sysinfo_get_processor_description_Exit(rc);
return rc;
}
BOOLEAN
omrsysinfo_processor_has_feature(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc, uint32_t feature)
{
BOOLEAN rc = FALSE;
Trc_PRT_sysinfo_processor_has_feature_Entered(desc, feature);
#if defined(J9OS_I5)
#if defined(J9OS_I5_V5R4)
if ((OMR_FEATURE_PPC_HAS_VSX == feature) || (OMR_FEATURE_PPC_HAS_ALTIVEC == feature) || (OMR_FEATURE_PPC_HTM == feature)) {
Trc_PRT_sysinfo_processor_has_feature_Exit((UDATA)rc);
return rc;
}
#elif defined(J9OS_I5_V6R1) || defined(J9OS_I5_V7R2)
if (OMR_FEATURE_PPC_HTM == feature) {
Trc_PRT_sysinfo_processor_has_feature_Exit((UDATA)rc);
return rc;
}
#endif
#endif
if ((NULL != desc) && (feature < (OMRPORT_SYSINFO_FEATURES_SIZE * 32))) {
uint32_t featureIndex = feature / 32;
uint32_t featureShift = feature % 32;
rc = OMR_ARE_ALL_BITS_SET(desc->features[featureIndex], 1u << featureShift);
}
Trc_PRT_sysinfo_processor_has_feature_Exit((uintptr_t)rc);
return rc;
}
intptr_t
omrsysinfo_processor_set_feature(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc, uint32_t feature, BOOLEAN enable)
{
intptr_t rc = -1;
Trc_PRT_sysinfo_processor_set_feature_Entered(desc, feature, enable);
if ((NULL != desc) && (feature < (OMRPORT_SYSINFO_FEATURES_SIZE * 32))) {
uint32_t featureIndex = feature / 32;
uint32_t featureShift = feature % 32;
if (enable) {
desc->features[featureIndex] |= (1u << featureShift);
}
else {
desc->features[featureIndex] &= ~(1u << featureShift);
}
rc = 0;
}
Trc_PRT_sysinfo_processor_set_feature_Exit(rc);
return rc;
}
const char*
omrsysinfo_get_processor_feature_name(struct OMRPortLibrary *portLibrary, uint32_t feature)
{
const char* rc = "null";
Trc_PRT_sysinfo_get_processor_feature_name_Entered(feature);
#if (defined(J9X86) || defined(J9HAMMER))
rc = omrsysinfo_get_x86_processor_feature_name(feature);
#elif (defined(S390) || defined(J9ZOS390) || defined(J9ZTPF))
rc = omrsysinfo_get_s390_processor_feature_name(feature);
#elif (defined(AIXPPC) || defined(LINUXPPC))
rc = omrsysinfo_get_ppc_processor_feature_name(feature);
#elif (defined(AARCH64) && defined(LINUX))
rc = omrsysinfo_get_aarch64_processor_feature_name(feature);
#endif
Trc_PRT_sysinfo_get_processor_feature_name_Exit(rc);
return rc;
}
/**
* Generate the corresponding string literals for the provided OMRProcessorDesc. The buffer will be zero
* initialized and overwritten with the processor feature output string.
*
* @param[in] portLibrary The port library.
* @param[in] desc The struct that contains the list of processor features to be converted to string.
* @param[out] buffer The processor feature output string.
* @param[in] length The size of the buffer in number of bytes.
*
* @return 0 on success, -1 if output string size exceeds input length.
*/
intptr_t
omrsysinfo_get_processor_feature_string(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc, char * buffer, const size_t length)
{
BOOLEAN start = TRUE;
size_t i = 0;
size_t j = 0;
size_t numberOfBits = 0;
size_t bufferLength = 0;
memset(buffer, 0, length);
for (i = 0; i < OMRPORT_SYSINFO_FEATURES_SIZE; i++) {
numberOfBits = CHAR_BIT * sizeof(desc->features[i]);
for (j = 0; j < numberOfBits; j++) {
if (desc->features[i] & (1 << j)) {
uint32_t feature = (uint32_t)(i * numberOfBits + j);
const char * featureName = omrsysinfo_get_processor_feature_name(portLibrary, feature);
size_t featureLength = strlen(featureName);
if (start == FALSE) {
strncat(buffer, " ", length - bufferLength - 1);
bufferLength += 1;
} else {
start = FALSE;
}
if (length - bufferLength - 1 < featureLength) {
return -1;
}
strncat(buffer, featureName, length - bufferLength - 1);
bufferLength += featureLength;
}
}
}
return 0;
}
#if (defined(AIXPPC) || defined(S390) || defined(J9ZOS390))
/**
* @internal
* Helper to set appropriate feature field in a OMRProcessorDesc struct.
*
* @param[in] desc pointer to the struct that contains the CPU type and features.
* @param[in] feature to set
*
*/
static void
omrsysinfo_set_feature(OMRProcessorDesc *desc, uint32_t feature)
{
if ((NULL != desc) && (feature < (OMRPORT_SYSINFO_FEATURES_SIZE * 32))) {
uint32_t featureIndex = feature / 32;
uint32_t featureShift = feature % 32;
desc->features[featureIndex] = (desc->features[featureIndex] | (1u << (featureShift)));
}
}
#endif /* defined(AIXPPC) || defined(S390) || defined(J9ZOS390) */
#if (defined(LINUXPPC) || defined(AIXPPC))
const char *
omrsysinfo_get_ppc_processor_feature_name(uint32_t feature)
{
switch (feature) {
case OMR_FEATURE_PPC_32:
return "mode32";
case OMR_FEATURE_PPC_64:
return "mode64";
case OMR_FEATURE_PPC_601_INSTR:
return "601";
case OMR_FEATURE_PPC_HAS_ALTIVEC:
return "vec";
case OMR_FEATURE_PPC_HAS_FPU:
return "fpu";
case OMR_FEATURE_PPC_HAS_MMU:
return "mmu";
case OMR_FEATURE_PPC_HAS_4xxMAC:
return "4xxmac";
case OMR_FEATURE_PPC_UNIFIED_CACHE:
return "uc";
case OMR_FEATURE_PPC_HAS_SPE:
return "spe";
case OMR_FEATURE_PPC_HAS_EFP_SINGLE:
return "efp_single";
case OMR_FEATURE_PPC_HAS_EFP_DOUBLE:
return "efp_double";
case OMR_FEATURE_PPC_POWER4:
return "p4";
case OMR_FEATURE_PPC_POWER5:
return "p5";
case OMR_FEATURE_PPC_POWER5_PLUS:
return "p5+";
case OMR_FEATURE_PPC_CELL_BE:
return "cell_be";
case OMR_FEATURE_PPC_BOOKE:
return "booke";
case OMR_FEATURE_PPC_SMT:
return "smt";
case OMR_FEATURE_PPC_ICACHE_SNOOP:
return "icache_snoop";
case OMR_FEATURE_PPC_ARCH_2_05:
return "arch_2_05";
case OMR_FEATURE_PPC_PA6T:
return "pa6t";
case OMR_FEATURE_PPC_HAS_DFP:
return "dfp";
case OMR_FEATURE_PPC_POWER6_EXT:
return "p6_ext";
case OMR_FEATURE_PPC_ARCH_2_06:
return "arch_2_06";
case OMR_FEATURE_PPC_HAS_VSX:
return "vsx";
case OMR_FEATURE_PPC_PSERIES_PERFMON_COMPAT:
return "perfmon_compact";
case OMR_FEATURE_PPC_TRUE_LE:
return "true_le";
case OMR_FEATURE_PPC_LE:
return "le";
case OMR_FEATURE_PPC_ARCH_2_07:
return "arch_2_07";
case OMR_FEATURE_PPC_HTM:
return "htm";
case OMR_FEATURE_PPC_DSCR:
return "dscr";
case OMR_FEATURE_PPC_EBB:
return "ebb";
case OMR_FEATURE_PPC_ISEL:
return "isel";
case OMR_FEATURE_PPC_TAR:
return "tar";
default:
return "null";
}
return "null";
}
#endif /* defined(LINUXPPC) || defined(AIXPPC) */
#if defined(LINUXPPC)
/**
* @internal
* Populates OMRProcessorDesc *desc on Linux PPC
*
* @param[in] desc pointer to the struct that will contain the CPU type and features.
*
* @return 0 on success, -1 on failure
*/
static intptr_t
omrsysinfo_get_linux_ppc_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc)
{
char* platform = NULL;
char* base_platform = NULL;
/* initialize auxv prior to querying the auxv */
if (prefetch_auxv() != 0) {
goto _error;
}
/* Linux PPC processor */
platform = (char *) query_auxv(AT_PLATFORM);
if ((NULL == platform) || (((char *) -1) == platform)) {
goto _error;
}
desc->processor = omrsysinfo_map_ppc_processor(platform);
/* Linux PPC physical processor */
base_platform = (char *) query_auxv(AT_BASE_PLATFORM);
if ((NULL == base_platform) || (((char *) -1) == base_platform)) {
/* AT_PLATFORM is known from call above. Default BASE to unknown */
desc->physicalProcessor = OMR_PROCESSOR_PPC_UNKNOWN;
} else {
desc->physicalProcessor = omrsysinfo_map_ppc_processor(base_platform);
}
/* Linux PPC features:
* Can't error check these calls as both 0 & -1 are valid
* bit fields that could be returned by this query.
*/
desc->features[0] = query_auxv(AT_HWCAP);
desc->features[1] = query_auxv(AT_HWCAP2);
return 0;
_error:
desc->processor = OMR_PROCESSOR_PPC_UNKNOWN;
desc->physicalProcessor = OMR_PROCESSOR_PPC_UNKNOWN;
desc->features[0] = 0;
desc->features[1] = 0;
return -1;
}
/**
* @internal
* Maps a PPC processor string to the OMRProcessorArchitecture enum.
*
* @param[in] processorName
*
* @return An OMRProcessorArchitecture OMR_PROCESSOR_PPC_* found otherwise OMR_PROCESSOR_PPC_UNKNOWN.
*/
static OMRProcessorArchitecture
omrsysinfo_map_ppc_processor(const char *processorName)
{
OMRProcessorArchitecture rc = OMR_PROCESSOR_PPC_UNKNOWN;
if (0 == strncasecmp(processorName, "ppc403", 6)) {
rc = OMR_PROCESSOR_PPC_PWR403;
} else if (0 == strncasecmp(processorName, "ppc405", 6)) {
rc = OMR_PROCESSOR_PPC_PWR405;
} else if (0 == strncasecmp(processorName, "ppc440gp", 8)) {
rc = OMR_PROCESSOR_PPC_PWR440;
} else if (0 == strncasecmp(processorName, "ppc601", 6)) {
rc = OMR_PROCESSOR_PPC_PWR601;
} else if (0 == strncasecmp(processorName, "ppc603", 6)) {
rc = OMR_PROCESSOR_PPC_PWR603;
} else if (0 == strncasecmp(processorName, "ppc604", 6)) {
rc = OMR_PROCESSOR_PPC_PWR604;
} else if (0 == strncasecmp(processorName, "ppc7400", 7)) {
rc = OMR_PROCESSOR_PPC_PWR603;
} else if (0 == strncasecmp(processorName, "ppc750", 6)) {
rc = OMR_PROCESSOR_PPC_7XX;
} else if (0 == strncasecmp(processorName, "rs64", 4)) {
rc = OMR_PROCESSOR_PPC_PULSAR;
} else if (0 == strncasecmp(processorName, "ppc970", 6)) {
rc = OMR_PROCESSOR_PPC_GP;
} else if (0 == strncasecmp(processorName, "power3", 6)) {
rc = OMR_PROCESSOR_PPC_PWR630;
} else if (0 == strncasecmp(processorName, "power4", 6)) {
rc = OMR_PROCESSOR_PPC_GP;
} else if (0 == strncasecmp(processorName, "power5", 6)) {
rc = OMR_PROCESSOR_PPC_GR;
} else if (0 == strncasecmp(processorName, "power6", 6)) {
rc = OMR_PROCESSOR_PPC_P6;
} else if (0 == strncasecmp(processorName, "power7", 6)) {
rc = OMR_PROCESSOR_PPC_P7;
} else if (0 == strncasecmp(processorName, "power8", 6)) {
rc = OMR_PROCESSOR_PPC_P8;
} else if (0 == strncasecmp(processorName, "power9", 6)) {
rc = OMR_PROCESSOR_PPC_P9;
} else if (0 == strncasecmp(processorName, "power10", 7)) {
rc = OMR_PROCESSOR_PPC_P10;
}
return rc;
}
#endif /* defined(LINUXPPC) */
#if defined(AIXPPC)
/**
* @internal
* Populates OMRProcessorDesc *desc on AIX
*
* @param[in] desc pointer to the struct that will contain the CPU type and features.
*
* @return 0 on success, -1 on failure
*/
static intptr_t
omrsysinfo_get_aix_ppc_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc)
{
/* AIX processor */
if (__power_rs1() || __power_rsc()) {
desc->processor = OMR_PROCESSOR_PPC_RIOS1;
} else if (__power_rs2()) {
desc->processor = OMR_PROCESSOR_PPC_RIOS2;
} else if (__power_601()) {