/
omrsysinfo.c
4795 lines (4233 loc) · 154 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, 2019 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>
#include <sys/types.h>
#include <sys/sysctl.h>
#endif /* defined(LINUX) && !defined(OMRZTPF) */
#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>
#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"
#include "omrsysinfo_helpers.h"
#endif /* defined(J9ZOS390) */
#if defined(LINUXPPC)
#include "auxv.h"
#include <strings.h>
#endif /* defined(LINUXPPC) */
#if defined(AIXPPC)
#include <fcntl.h>
#include <sys/procfs.h>
#include <sys/systemcfg.h>
#endif /* defined(AIXPPC) */
/* 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))
/* For the omrsysinfo_env_iterator */
extern char **environ;
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);
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);
#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;
#else
return "unknown";
#endif
}
intptr_t
omrsysinfo_get_env(struct OMRPortLibrary *portLibrary, const char *envVar, char *infoString, uintptr_t bufSize)
{
char *value = (char *)getenv(envVar);
uintptr_t len = 0;
if (NULL == value) {
return -1;
} else if ((len = strlen(value)) >= bufSize) {
return len + 1;
} else {
strcpy(infoString, value);
return 0;
}
}
const char *
omrsysinfo_get_OS_type(struct OMRPortLibrary *portLibrary)
{
#if defined(J9OS_I5)
/* JCL on IBM i expects to see "OS/400" */
return "OS/400";
#elif defined(OSX)
return "Mac OS X";
#else
if (NULL == PPG_si_osType) {
int rc = 0;
struct utsname sysinfo;
#ifdef J9ZOS390
rc = __osname(&sysinfo);
#else
rc = uname(&sysinfo);
#endif
if (rc >= 0) {
int len = strlen(sysinfo.sysname);
char *buffer = portLibrary->mem_allocate_memory(portLibrary, len + 1, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == buffer) {
return NULL;
}
/* copy and NUL-terminate */
memcpy(buffer, sysinfo.sysname, len);
buffer[len] = '\0';
PPG_si_osType = buffer;
}
}
return PPG_si_osType;
#endif
}
#if defined(OSX)
#define KERN_OSPRODUCTVERSION "kern.osproductversion"
#define PLIST_FILE "/System/Library/CoreServices/SystemVersion.plist"
#define PRODUCT_VERSION_ELEMENT "<key>ProductVersion</key>"
#define STRING_OPEN_TAG "<string>"
#define STRING_CLOSE_TAG "</string>"
#endif
#if defined(J9OS_I5)
/* 'V', 'R', 'M', '0', & null */
#define RELEASE_OVERHEAD 5
#define FORMAT_STRING "V%sR%sM0"
#else
/* "." and terminating null character */
#define RELEASE_OVERHEAD 2
#define FORMAT_STRING "%s.%s"
#endif /* defined(J9OS_I5) */
const char *
omrsysinfo_get_OS_version(struct OMRPortLibrary *portLibrary)
{
if (NULL == PPG_si_osVersion) {
int rc = -1;
#if defined(OSX)
char *versionString = NULL;
char *allocatedFileBuffer = NULL;
#else
struct utsname sysinfo;
#endif
#ifdef J9ZOS390
rc = __osname(&sysinfo);
#elif defined(OSX)
/* uname() on MacOS returns the version of the Darwin kernel, not the operating system product name.
* For compatibility with the reference implementation,
* we need the operating system product name.
* On newer versions of MacOS, the OS name is provided by sysctlbyname().
* On older versions of MacOS, the OS name is provided by a property list file.
* The relevant portion is:
* <key>ProductVersion</key>
* <string>10.13.6</string>
*/
size_t resultSize = 0;
rc = sysctlbyname(KERN_OSPRODUCTVERSION, NULL, &resultSize, NULL, 0);
if (rc < 0) { /* older version of MacOS. Fall back to parsing the plist. */
intptr_t plistFile = portLibrary->file_open(portLibrary, PLIST_FILE, EsOpenRead, 0444);
if (plistFile >= 0) {
char myBuffer[512]; /* the file size is typically ~480 bytes */
char *buffer = myBuffer;
size_t len = (size_t) portLibrary->file_flength(portLibrary, plistFile);
rc = 0;
if (len >= sizeof(myBuffer)) { /* leave space for a terminating null */
allocatedFileBuffer = portLibrary->mem_allocate_memory(portLibrary, len + 1, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
buffer = allocatedFileBuffer;
}
if (NULL != buffer) {
char *readPtr = buffer;
size_t bytesRemaining = len;
while (bytesRemaining > 0) {
intptr_t result = portLibrary->file_read(portLibrary, plistFile, readPtr, bytesRemaining);
if (result > 0) {
bytesRemaining -= result;
readPtr += result;
} else {
rc = -1; /* error or unexpected EOF */
break;
}
}
if (0 == rc) {
buffer[len] = '\0';
readPtr = strstr(buffer, PRODUCT_VERSION_ELEMENT);
if (NULL != readPtr) {
readPtr = strstr(readPtr + sizeof(PRODUCT_VERSION_ELEMENT) - 1, STRING_OPEN_TAG);
}
if (NULL != readPtr) {
versionString = readPtr + sizeof(STRING_OPEN_TAG) - 1;
readPtr = strstr(versionString, STRING_CLOSE_TAG);
}
if (NULL != readPtr) {
*readPtr = '\0';
resultSize = readPtr - versionString;
} else {
rc = -1; /* parse error */
}
}
}
portLibrary->file_close(portLibrary, plistFile);
if (rc < 0) {
/* free the buffer if it was allocated */
portLibrary->mem_free_memory(portLibrary, allocatedFileBuffer);
}
}
}
#else
rc = uname(&sysinfo);
#endif /* defined(OSX) */
if (rc >= 0) {
int len;
char *buffer = NULL;
#if defined(RS6000) || defined(J9ZOS390)
len = strlen(sysinfo.version) + strlen(sysinfo.release) + RELEASE_OVERHEAD;
buffer = portLibrary->mem_allocate_memory(portLibrary, len, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL != buffer) {
portLibrary->str_printf(portLibrary, buffer, len, FORMAT_STRING, sysinfo.version, sysinfo.release);
}
#elif defined(OSX)
len = resultSize + 1;
buffer = portLibrary->mem_allocate_memory(portLibrary, len, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL != buffer) {
if (NULL != versionString) {
strncpy(buffer, versionString, len);
} else {
rc = sysctlbyname(KERN_OSPRODUCTVERSION, buffer, &resultSize, NULL, 0);
}
buffer[resultSize] = '\0';
}
/* allocatedFileBuffer is null if it was not allocated */
portLibrary->mem_free_memory(portLibrary, allocatedFileBuffer);
#else
len = strlen(sysinfo.release) + 1;
buffer = portLibrary->mem_allocate_memory(portLibrary, len, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL != buffer) {
strncpy(buffer, sysinfo.release, len);
buffer[len - 1] = '\0';
}
#endif /* RS6000 */
PPG_si_osVersion = buffer;
}
}
return PPG_si_osVersion;
}
uintptr_t
omrsysinfo_get_pid(struct OMRPortLibrary *portLibrary)
{
return getpid();
}
uintptr_t
omrsysinfo_get_ppid(struct OMRPortLibrary *portLibrary)
{
return getppid();
}
uintptr_t
omrsysinfo_get_euid(struct OMRPortLibrary *portLibrary)
{
uintptr_t uid = geteuid();
Trc_PRT_sysinfo_get_egid(uid);
return uid;
}
uintptr_t
omrsysinfo_get_egid(struct OMRPortLibrary *portLibrary)
{
uintptr_t gid = getegid();
Trc_PRT_sysinfo_get_egid(gid);
return gid;
}
intptr_t
omrsysinfo_get_groups(struct OMRPortLibrary *portLibrary, uint32_t **gidList, uint32_t categoryCode)
{
intptr_t size = 0;
gid_t *list = NULL;
intptr_t myerror;
Trc_PRT_sysinfo_get_groups_Entry();
*gidList = NULL;
/* First get the number of supplementary group IDs */
size = getgroups(0, NULL);
if (-1 == size) {
/* Above call getgroups(0, ...) is not expected to fail. In case it does, return appropriate error code */
myerror = errno;
Trc_PRT_sysinfo_get_groups_Error_GetGroupsSize(myerror);
setPortableError(portLibrary, getgroupsErrorMsgPrefix, OMRPORT_ERROR_SYSINFO_GETGROUPSSIZE_ERROR, myerror);
} else {
list = portLibrary->mem_allocate_memory(portLibrary, size * sizeof(uint32_t), OMR_GET_CALLSITE(), categoryCode);
if (NULL == list) {
Trc_PRT_sysinfo_get_groups_Error_ListAllocateFailed(size);
size = -1;
} else {
size = getgroups(size, list);
if (-1 == size) {
myerror = errno;
portLibrary->mem_free_memory(portLibrary, list);
Trc_PRT_sysinfo_get_groups_Error_GetGroups(myerror);
setPortableError(portLibrary, getgroupsErrorMsgPrefix, OMRPORT_ERROR_SYSINFO_GETGROUPS_ERROR, myerror);
} else {
*gidList = (uint32_t *)list;
}
}
}
Trc_PRT_sysinfo_get_groups_Exit(size, *gidList);
return size;
}
/**
* @internal Helper routine that determines the full path of the current executable
* that launched the JVM instance.
*/
static intptr_t
find_executable_name(struct OMRPortLibrary *portLibrary, char **result)
{
#if defined(LINUX)
intptr_t retval = readSymbolicLink(portLibrary, "/proc/self/exe", result);
if (NULL != *result) {
/* The code in this block is a work around for a linux bug. If /proc/self/exe is read after a
* call to fstat on a NFSv4 share, then the string ' (deleted)' will be appended to
* the file name.
*
* The work around to this problem for now is to prune off the ' (deleted)' string.
*
* Until this fix numerous 'pltest.exe' failures were occurring on 'vmfarm'. I expect over time
* once kernels are patch this code can be removed.
*/
int exeNameLen = strlen(*result);
const char *deletedStr = " (deleted)";
int deletedStrLen = strlen(deletedStr);
if (exeNameLen > deletedStrLen) {
if (strncmp(&((*result)[exeNameLen - deletedStrLen]), deletedStr, deletedStrLen) == 0) {
(*result)[exeNameLen - deletedStrLen] = '\0';
}
}
}
return retval;
#elif defined(OSX)
intptr_t rc = -1;
uint32_t size = 0;
/* First call is just to get required size. */;
_NSGetExecutablePath(*result, &size);
*result = portLibrary->mem_allocate_memory(portLibrary, size, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (0 == _NSGetExecutablePath(*result, &size)) {
rc = 0;
}
return rc;
#else /* defined(OSX) */
intptr_t retval = -1;
intptr_t length;
char *p;
char *currentName = NULL;
char *currentPath = NULL;
char *originalWorkingDirectory = NULL;
#ifdef J9ZOS390
char *e2aName = NULL;
int token = 0;
W_PSPROC buf;
pid_t mypid = getpid();
memset(&buf, 0x00, sizeof(buf));
buf.ps_pathptr = portLibrary->mem_allocate_memory(portLibrary, buf.ps_pathlen = PS_PATHBLEN, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (buf.ps_pathptr == NULL) {
retval = -1;
goto cleanup;
}
while ((token = w_getpsent(token, &buf, sizeof(buf))) > 0) {
if (buf.ps_pid == mypid) {
#if defined(J9ZOS390) && !defined(OMR_EBCDIC)
e2aName = e2a_func(buf.ps_pathptr, strlen(buf.ps_pathptr) + 1);
#else /* defined(J9ZOS390) && !defined(OMR_EBCDIC) */
e2aName = buf.ps_pathptr;
#endif /* defined(J9ZOS390) && !defined(OMR_EBCDIC) */
break;
}
}
portLibrary->mem_free_memory(portLibrary, buf.ps_pathptr);
/* Return val of w_getpsent == -1 indicates error, == 0 indicates no more processes */
if (token <= 0) {
retval = -1;
goto cleanup;
}
currentPath = (portLibrary->mem_allocate_memory)(portLibrary, strlen(e2aName) + 1, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (currentPath) {
strcpy(currentPath, e2aName);
}
#if defined(J9ZOS390) && !defined(OMR_EBCDIC)
free(e2aName);
#endif /* defined(J9ZOS390) && !defined(OMR_EBCDIC) */
#else
char *execName = NULL;
#if defined(AIXPPC)
/* On AIX, "/proc" provides a way of determining argument vector (not just using
* argv[0] from within main()).
*/
struct psinfo psinfoData = {0};
char buffer[PATH_MAX]; /* Do not initialize; large array. */
char ***pArgv = NULL;
int32_t fd = -1;
uintptr_t readBytes = 0L;
uintptr_t remainingBytes = sizeof(psinfoData);
uintptr_t readOffset = 0L;
uintptr_t strLen = 0L;
int32_t portableError = 0;
portLibrary->str_printf(portLibrary, buffer, PATH_MAX, "/proc/%ld/psinfo", getpid());
fd = portLibrary->file_open(portLibrary, buffer, EsOpenRead, 0);
if (-1 == fd) {
portableError = portLibrary->error_last_error_number(portLibrary);
Trc_PRT_find_executable_name_failedOpeningProcFS(portableError);
portLibrary->error_set_last_error_with_message(portLibrary,
portableError,
"Failed to open /proc fs");
retval = -1;
goto cleanup;
}
/* Read the process info; loop in order to ensure the complete structure is read in. */
do {
readBytes = portLibrary->file_read(portLibrary,
fd,
((char *) &psinfoData) + readOffset,
remainingBytes);
/* Advance readOffset by bytes read and adjust this out from remainingBytes (to be read). */
remainingBytes -= readBytes;
readOffset += readBytes;
/* Check for error and EOF conditions. Should this arise before reading ample bytes, file read
* has supposedly failed.
*/
if (-1 == readBytes) {
/* Read failed because of some reason. Check the portable error code. */
portableError = portLibrary->error_last_error_number(portLibrary);
Trc_PRT_find_executable_name_invalidRead(portableError);
portLibrary->error_set_last_error_with_message(portLibrary, portableError, "Input/output error");
retval = -1;
portLibrary->file_close(portLibrary, fd);
goto cleanup;
} else if ((0 == readBytes) && (0 != remainingBytes)) {
/* EOF reached but haven't read required number bytes (file simply too short)! No error number
* is expected to be set here, so use OMRPORT_ERROR_FILE_IO to indicate I/O error.
*/
Trc_PRT_find_executable_name_invalidRead(OMRPORT_ERROR_FILE_IO);
portLibrary->error_set_last_error_with_message(portLibrary,
OMRPORT_ERROR_FILE_IO,
"Input/output error: /proc file truncated");
retval = -1;
portLibrary->file_close(portLibrary, fd);
goto cleanup;
}
} while (0 != remainingBytes); /* Read until there are no bytes remaining to fill the structure. */
portLibrary->file_close(portLibrary, fd);
pArgv = (char ***)psinfoData.pr_argv;
execName = pArgv[0][0];
#endif
strLen = strlen(execName) + 1;
currentPath = (portLibrary->mem_allocate_memory)(portLibrary, strLen, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL != currentPath) {
strncpy(currentPath, execName, strLen - 1);
currentPath[strLen - 1] = '\0';
}
#endif
if (NULL == currentPath) {
retval = -1;
goto cleanup;
}
retval = cwdname(portLibrary, &originalWorkingDirectory);
if (retval) {
retval = -1;
goto cleanup;
}
gotPathName:
/* split path into directory part and filename part. */
p = strrchr(currentPath, '/');
if (p) {
*p++ = '\0';
currentName = (portLibrary->mem_allocate_memory)(portLibrary, strlen(p) + 1, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (!currentName) {
retval = -1;
goto cleanup;
}
strcpy(currentName, p);
} else {
currentName = currentPath;
currentPath = NULL;
retval = searchSystemPath(portLibrary, currentName, ¤tPath);
if (retval) {
retval = -1;
goto cleanup;
}
}
/* go there */
if (currentPath) {
if (currentPath[0]) {
if (0 != chdir(currentPath)) {
retval = -1;
goto cleanup;
}
}
(portLibrary->mem_free_memory)(portLibrary, currentPath);
currentPath = NULL;
}
if (isSymbolicLink(portLibrary, currentName)) {
/* try to follow the link. */
retval = readSymbolicLink(portLibrary, currentName, ¤tPath);
#ifdef DEBUG
(portLibrary->tty_printf)(portLibrary, "read cp=%s\n", currentPath);
#endif
if (retval) {
retval = -1;
goto cleanup;