/
os_aix.cpp
3175 lines (2677 loc) · 104 KB
/
os_aix.cpp
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) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
// According to the AIX OS doc #pragma alloca must be used
// with C++ compiler before referencing the function alloca()
#pragma alloca
// no precompiled headers
#include "jvm.h"
#include "classfile/vmSymbols.hpp"
#include "code/icBuffer.hpp"
#include "code/vtableStubs.hpp"
#include "compiler/compileBroker.hpp"
#include "interpreter/interpreter.hpp"
#include "jvmtifiles/jvmti.h"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "libo4.hpp"
#include "libperfstat_aix.hpp"
#include "libodm_aix.hpp"
#include "loadlib_aix.hpp"
#include "memory/allocation.inline.hpp"
#include "misc_aix.hpp"
#include "oops/oop.inline.hpp"
#include "os_aix.inline.hpp"
#include "os_share_aix.hpp"
#include "porting_aix.hpp"
#include "prims/jniFastGetField.hpp"
#include "prims/jvm_misc.hpp"
#include "runtime/arguments.hpp"
#include "runtime/atomic.hpp"
#include "runtime/globals.hpp"
#include "runtime/globals_extension.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/objectMonitor.hpp"
#include "runtime/os.hpp"
#include "runtime/osThread.hpp"
#include "runtime/perfMemory.hpp"
#include "runtime/safefetch.inline.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/statSampler.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadCritical.hpp"
#include "runtime/timer.hpp"
#include "runtime/vm_version.hpp"
#include "services/attachListener.hpp"
#include "services/runtimeService.hpp"
#include "signals_posix.hpp"
#include "utilities/align.hpp"
#include "utilities/decoder.hpp"
#include "utilities/defaultStream.hpp"
#include "utilities/events.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/vmError.hpp"
// put OS-includes here (sorted alphabetically)
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <procinfo.h>
#include <pthread.h>
#include <pwd.h>
#include <semaphore.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/systemcfg.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/vminfo.h>
// Missing prototypes for various system APIs.
extern "C"
int mread_real_time(timebasestruct_t *t, size_t size_of_timebasestruct_t);
#if !defined(_AIXVERSION_610)
extern "C" int getthrds64(pid_t, struct thrdentry64*, int, tid64_t*, int);
extern "C" int getprocs64(procentry64*, int, fdsinfo*, int, pid_t*, int);
extern "C" int getargs(procsinfo*, int, char*, int);
#endif
#define MAX_PATH (2 * K)
// for timer info max values which include all bits
#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
// for multipage initialization error analysis (in 'g_multipage_error')
#define ERROR_MP_OS_TOO_OLD 100
#define ERROR_MP_EXTSHM_ACTIVE 101
#define ERROR_MP_VMGETINFO_FAILED 102
#define ERROR_MP_VMGETINFO_CLAIMS_NO_SUPPORT_FOR_64K 103
// excerpts from systemcfg.h that might be missing on older os levels
#ifndef PV_7
#define PV_7 0x200000 /* Power PC 7 */
#endif
#ifndef PV_7_Compat
#define PV_7_Compat 0x208000 /* Power PC 7 */
#endif
#ifndef PV_8
#define PV_8 0x300000 /* Power PC 8 */
#endif
#ifndef PV_8_Compat
#define PV_8_Compat 0x308000 /* Power PC 8 */
#endif
#ifndef PV_9
#define PV_9 0x400000 /* Power PC 9 */
#endif
#ifndef PV_9_Compat
#define PV_9_Compat 0x408000 /* Power PC 9 */
#endif
static address resolve_function_descriptor_to_code_pointer(address p);
static void vmembk_print_on(outputStream* os);
////////////////////////////////////////////////////////////////////////////////
// global variables (for a description see os_aix.hpp)
julong os::Aix::_physical_memory = 0;
pthread_t os::Aix::_main_thread = ((pthread_t)0);
int os::Aix::_page_size = -1;
// -1 = uninitialized, 0 if AIX, 1 if OS/400 pase
int os::Aix::_on_pase = -1;
// 0 = uninitialized, otherwise 32 bit number:
// 0xVVRRTTSS
// VV - major version
// RR - minor version
// TT - tech level, if known, 0 otherwise
// SS - service pack, if known, 0 otherwise
uint32_t os::Aix::_os_version = 0;
// -1 = uninitialized, 0 - no, 1 - yes
int os::Aix::_xpg_sus_mode = -1;
// -1 = uninitialized, 0 - no, 1 - yes
int os::Aix::_extshm = -1;
////////////////////////////////////////////////////////////////////////////////
// local variables
static volatile jlong max_real_time = 0;
static jlong initial_time_count = 0;
static int clock_tics_per_sec = 100;
// Process break recorded at startup.
static address g_brk_at_startup = NULL;
// This describes the state of multipage support of the underlying
// OS. Note that this is of no interest to the outsize world and
// therefore should not be defined in AIX class.
//
// AIX supports four different page sizes - 4K, 64K, 16MB, 16GB. The
// latter two (16M "large" resp. 16G "huge" pages) require special
// setup and are normally not available.
//
// AIX supports multiple page sizes per process, for:
// - Stack (of the primordial thread, so not relevant for us)
// - Data - data, bss, heap, for us also pthread stacks
// - Text - text code
// - shared memory
//
// Default page sizes can be set via linker options (-bdatapsize, -bstacksize, ...)
// and via environment variable LDR_CNTRL (DATAPSIZE, STACKPSIZE, ...).
//
// For shared memory, page size can be set dynamically via
// shmctl(). Different shared memory regions can have different page
// sizes.
//
// More information can be found at AIBM info center:
// http://publib.boulder.ibm.com/infocenter/aix/v6r1/index.jsp?topic=/com.ibm.aix.prftungd/doc/prftungd/multiple_page_size_app_support.htm
//
static struct {
size_t pagesize; // sysconf _SC_PAGESIZE (4K)
size_t datapsize; // default data page size (LDR_CNTRL DATAPSIZE)
size_t shmpsize; // default shared memory page size (LDR_CNTRL SHMPSIZE)
size_t pthr_stack_pagesize; // stack page size of pthread threads
size_t textpsize; // default text page size (LDR_CNTRL STACKPSIZE)
bool can_use_64K_pages; // True if we can alloc 64K pages dynamically with Sys V shm.
bool can_use_16M_pages; // True if we can alloc 16M pages dynamically with Sys V shm.
int error; // Error describing if something went wrong at multipage init.
} g_multipage_support = {
(size_t) -1,
(size_t) -1,
(size_t) -1,
(size_t) -1,
(size_t) -1,
false, false,
0
};
// We must not accidentally allocate memory close to the BRK - even if
// that would work - because then we prevent the BRK segment from
// growing which may result in a malloc OOM even though there is
// enough memory. The problem only arises if we shmat() or mmap() at
// a specific wish address, e.g. to place the heap in a
// compressed-oops-friendly way.
static bool is_close_to_brk(address a) {
assert0(g_brk_at_startup != NULL);
if (a >= g_brk_at_startup &&
a < (g_brk_at_startup + MaxExpectedDataSegmentSize)) {
return true;
}
return false;
}
julong os::available_memory() {
return Aix::available_memory();
}
julong os::Aix::available_memory() {
// Avoid expensive API call here, as returned value will always be null.
if (os::Aix::on_pase()) {
return 0x0LL;
}
os::Aix::meminfo_t mi;
if (os::Aix::get_meminfo(&mi)) {
return mi.real_free;
} else {
return ULONG_MAX;
}
}
julong os::physical_memory() {
return Aix::physical_memory();
}
// Return true if user is running as root.
bool os::have_special_privileges() {
static bool init = false;
static bool privileges = false;
if (!init) {
privileges = (getuid() != geteuid()) || (getgid() != getegid());
init = true;
}
return privileges;
}
// Helper function, emulates disclaim64 using multiple 32bit disclaims
// because we cannot use disclaim64() on AS/400 and old AIX releases.
static bool my_disclaim64(char* addr, size_t size) {
if (size == 0) {
return true;
}
// Maximum size 32bit disclaim() accepts. (Theoretically 4GB, but I just do not trust that.)
const unsigned int maxDisclaimSize = 0x40000000;
const unsigned int numFullDisclaimsNeeded = (size / maxDisclaimSize);
const unsigned int lastDisclaimSize = (size % maxDisclaimSize);
char* p = addr;
for (int i = 0; i < numFullDisclaimsNeeded; i ++) {
if (::disclaim(p, maxDisclaimSize, DISCLAIM_ZEROMEM) != 0) {
trcVerbose("Cannot disclaim %p - %p (errno %d)\n", p, p + maxDisclaimSize, errno);
return false;
}
p += maxDisclaimSize;
}
if (lastDisclaimSize > 0) {
if (::disclaim(p, lastDisclaimSize, DISCLAIM_ZEROMEM) != 0) {
trcVerbose("Cannot disclaim %p - %p (errno %d)\n", p, p + lastDisclaimSize, errno);
return false;
}
}
return true;
}
// Cpu architecture string
#if defined(PPC32)
static char cpu_arch[] = "ppc";
#elif defined(PPC64)
static char cpu_arch[] = "ppc64";
#else
#error Add appropriate cpu_arch setting
#endif
// Wrap the function "vmgetinfo" which is not available on older OS releases.
static int checked_vmgetinfo(void *out, int command, int arg) {
if (os::Aix::on_pase() && os::Aix::os_version_short() < 0x0601) {
guarantee(false, "cannot call vmgetinfo on AS/400 older than V6R1");
}
return ::vmgetinfo(out, command, arg);
}
// Given an address, returns the size of the page backing that address.
size_t os::Aix::query_pagesize(void* addr) {
if (os::Aix::on_pase() && os::Aix::os_version_short() < 0x0601) {
// AS/400 older than V6R1: no vmgetinfo here, default to 4K
return 4*K;
}
vm_page_info pi;
pi.addr = (uint64_t)addr;
if (checked_vmgetinfo(&pi, VM_PAGE_INFO, sizeof(pi)) == 0) {
return pi.pagesize;
} else {
assert(false, "vmgetinfo failed to retrieve page size");
return 4*K;
}
}
void os::Aix::initialize_system_info() {
// Get the number of online(logical) cpus instead of configured.
os::_processor_count = sysconf(_SC_NPROCESSORS_ONLN);
assert(_processor_count > 0, "_processor_count must be > 0");
// Retrieve total physical storage.
os::Aix::meminfo_t mi;
if (!os::Aix::get_meminfo(&mi)) {
assert(false, "os::Aix::get_meminfo failed.");
}
_physical_memory = (julong) mi.real_total;
}
// Helper function for tracing page sizes.
static const char* describe_pagesize(size_t pagesize) {
switch (pagesize) {
case 4*K : return "4K";
case 64*K: return "64K";
case 16*M: return "16M";
case 16*G: return "16G";
default:
assert(false, "surprise");
return "??";
}
}
// Probe OS for multipage support.
// Will fill the global g_multipage_support structure.
// Must be called before calling os::large_page_init().
static void query_multipage_support() {
guarantee(g_multipage_support.pagesize == -1,
"do not call twice");
g_multipage_support.pagesize = ::sysconf(_SC_PAGESIZE);
// This really would surprise me.
assert(g_multipage_support.pagesize == 4*K, "surprise!");
// Query default data page size (default page size for C-Heap, pthread stacks and .bss).
// Default data page size is defined either by linker options (-bdatapsize)
// or by environment variable LDR_CNTRL (suboption DATAPSIZE). If none is given,
// default should be 4K.
{
void* p = ::malloc(16*M);
g_multipage_support.datapsize = os::Aix::query_pagesize(p);
::free(p);
}
// Query default shm page size (LDR_CNTRL SHMPSIZE).
// Note that this is pure curiosity. We do not rely on default page size but set
// our own page size after allocated.
{
const int shmid = ::shmget(IPC_PRIVATE, 1, IPC_CREAT | S_IRUSR | S_IWUSR);
guarantee(shmid != -1, "shmget failed");
void* p = ::shmat(shmid, NULL, 0);
::shmctl(shmid, IPC_RMID, NULL);
guarantee(p != (void*) -1, "shmat failed");
g_multipage_support.shmpsize = os::Aix::query_pagesize(p);
::shmdt(p);
}
// Before querying the stack page size, make sure we are not running as primordial
// thread (because primordial thread's stack may have different page size than
// pthread thread stacks). Running a VM on the primordial thread won't work for a
// number of reasons so we may just as well guarantee it here.
guarantee0(!os::is_primordial_thread());
// Query pthread stack page size. Should be the same as data page size because
// pthread stacks are allocated from C-Heap.
{
int dummy = 0;
g_multipage_support.pthr_stack_pagesize = os::Aix::query_pagesize(&dummy);
}
// Query default text page size (LDR_CNTRL TEXTPSIZE).
{
address any_function =
resolve_function_descriptor_to_code_pointer((address)describe_pagesize);
g_multipage_support.textpsize = os::Aix::query_pagesize(any_function);
}
// Now probe for support of 64K pages and 16M pages.
// Before OS/400 V6R1, there is no support for pages other than 4K.
if (os::Aix::on_pase_V5R4_or_older()) {
trcVerbose("OS/400 < V6R1 - no large page support.");
g_multipage_support.error = ERROR_MP_OS_TOO_OLD;
goto query_multipage_support_end;
}
// Now check which page sizes the OS claims it supports, and of those, which actually can be used.
{
const int MAX_PAGE_SIZES = 4;
psize_t sizes[MAX_PAGE_SIZES];
const int num_psizes = checked_vmgetinfo(sizes, VMINFO_GETPSIZES, MAX_PAGE_SIZES);
if (num_psizes == -1) {
trcVerbose("vmgetinfo(VMINFO_GETPSIZES) failed (errno: %d)", errno);
trcVerbose("disabling multipage support.");
g_multipage_support.error = ERROR_MP_VMGETINFO_FAILED;
goto query_multipage_support_end;
}
guarantee(num_psizes > 0, "vmgetinfo(.., VMINFO_GETPSIZES, ...) failed.");
assert(num_psizes <= MAX_PAGE_SIZES, "Surprise! more than 4 page sizes?");
trcVerbose("vmgetinfo(.., VMINFO_GETPSIZES, ...) returns %d supported page sizes: ", num_psizes);
for (int i = 0; i < num_psizes; i ++) {
trcVerbose(" %s ", describe_pagesize(sizes[i]));
}
// Can we use 64K, 16M pages?
for (int i = 0; i < num_psizes; i ++) {
const size_t pagesize = sizes[i];
if (pagesize != 64*K && pagesize != 16*M) {
continue;
}
bool can_use = false;
trcVerbose("Probing support for %s pages...", describe_pagesize(pagesize));
const int shmid = ::shmget(IPC_PRIVATE, pagesize,
IPC_CREAT | S_IRUSR | S_IWUSR);
guarantee0(shmid != -1); // Should always work.
// Try to set pagesize.
struct shmid_ds shm_buf = { 0 };
shm_buf.shm_pagesize = pagesize;
if (::shmctl(shmid, SHM_PAGESIZE, &shm_buf) != 0) {
const int en = errno;
::shmctl(shmid, IPC_RMID, NULL); // As early as possible!
trcVerbose("shmctl(SHM_PAGESIZE) failed with errno=%d", errno);
} else {
// Attach and double check pageisze.
void* p = ::shmat(shmid, NULL, 0);
::shmctl(shmid, IPC_RMID, NULL); // As early as possible!
guarantee0(p != (void*) -1); // Should always work.
const size_t real_pagesize = os::Aix::query_pagesize(p);
if (real_pagesize != pagesize) {
trcVerbose("real page size (" SIZE_FORMAT_HEX ") differs.", real_pagesize);
} else {
can_use = true;
}
::shmdt(p);
}
trcVerbose("Can use: %s", (can_use ? "yes" : "no"));
if (pagesize == 64*K) {
g_multipage_support.can_use_64K_pages = can_use;
} else if (pagesize == 16*M) {
g_multipage_support.can_use_16M_pages = can_use;
}
}
} // end: check which pages can be used for shared memory
query_multipage_support_end:
trcVerbose("base page size (sysconf _SC_PAGESIZE): %s",
describe_pagesize(g_multipage_support.pagesize));
trcVerbose("Data page size (C-Heap, bss, etc): %s",
describe_pagesize(g_multipage_support.datapsize));
trcVerbose("Text page size: %s",
describe_pagesize(g_multipage_support.textpsize));
trcVerbose("Thread stack page size (pthread): %s",
describe_pagesize(g_multipage_support.pthr_stack_pagesize));
trcVerbose("Default shared memory page size: %s",
describe_pagesize(g_multipage_support.shmpsize));
trcVerbose("Can use 64K pages dynamically with shared memory: %s",
(g_multipage_support.can_use_64K_pages ? "yes" :"no"));
trcVerbose("Can use 16M pages dynamically with shared memory: %s",
(g_multipage_support.can_use_16M_pages ? "yes" :"no"));
trcVerbose("Multipage error details: %d",
g_multipage_support.error);
// sanity checks
assert0(g_multipage_support.pagesize == 4*K);
assert0(g_multipage_support.datapsize == 4*K || g_multipage_support.datapsize == 64*K);
assert0(g_multipage_support.textpsize == 4*K || g_multipage_support.textpsize == 64*K);
assert0(g_multipage_support.pthr_stack_pagesize == g_multipage_support.datapsize);
assert0(g_multipage_support.shmpsize == 4*K || g_multipage_support.shmpsize == 64*K);
}
void os::init_system_properties_values() {
#ifndef OVERRIDE_LIBPATH
#define DEFAULT_LIBPATH "/lib:/usr/lib"
#else
#define DEFAULT_LIBPATH OVERRIDE_LIBPATH
#endif
#define EXTENSIONS_DIR "/lib/ext"
// Buffer that fits several sprintfs.
// Note that the space for the trailing null is provided
// by the nulls included by the sizeof operator.
const size_t bufsize =
MAX2((size_t)MAXPATHLEN, // For dll_dir & friends.
(size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR)); // extensions dir
char *buf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
// sysclasspath, java_home, dll_dir
{
char *pslash;
os::jvm_path(buf, bufsize);
// Found the full path to libjvm.so.
// Now cut the path to <java_home>/jre if we can.
pslash = strrchr(buf, '/');
if (pslash != NULL) {
*pslash = '\0'; // Get rid of /libjvm.so.
}
pslash = strrchr(buf, '/');
if (pslash != NULL) {
*pslash = '\0'; // Get rid of /{client|server|hotspot}.
}
Arguments::set_dll_dir(buf);
if (pslash != NULL) {
pslash = strrchr(buf, '/');
if (pslash != NULL) {
*pslash = '\0'; // Get rid of /lib.
}
}
Arguments::set_java_home(buf);
if (!set_boot_path('/', ':')) {
vm_exit_during_initialization("Failed setting boot class path.", NULL);
}
}
// Where to look for native libraries.
// On Aix we get the user setting of LIBPATH.
// Eventually, all the library path setting will be done here.
// Get the user setting of LIBPATH.
const char *v = ::getenv("LIBPATH");
const char *v_colon = ":";
if (v == NULL) { v = ""; v_colon = ""; }
// Concatenate user and invariant part of ld_library_path.
// That's +1 for the colon and +1 for the trailing '\0'.
char *ld_library_path = NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + sizeof(DEFAULT_LIBPATH) + 1, mtInternal);
sprintf(ld_library_path, "%s%s" DEFAULT_LIBPATH, v, v_colon);
Arguments::set_library_path(ld_library_path);
FREE_C_HEAP_ARRAY(char, ld_library_path);
// Extensions directories.
sprintf(buf, "%s" EXTENSIONS_DIR, Arguments::get_java_home());
Arguments::set_ext_dirs(buf);
FREE_C_HEAP_ARRAY(char, buf);
#undef DEFAULT_LIBPATH
#undef EXTENSIONS_DIR
}
////////////////////////////////////////////////////////////////////////////////
// breakpoint support
void os::breakpoint() {
BREAKPOINT;
}
extern "C" void breakpoint() {
// use debugger to set breakpoint here
}
// retrieve memory information.
// Returns false if something went wrong;
// content of pmi undefined in this case.
bool os::Aix::get_meminfo(meminfo_t* pmi) {
assert(pmi, "get_meminfo: invalid parameter");
memset(pmi, 0, sizeof(meminfo_t));
if (os::Aix::on_pase()) {
// On PASE, use the libo4 porting library.
unsigned long long virt_total = 0;
unsigned long long real_total = 0;
unsigned long long real_free = 0;
unsigned long long pgsp_total = 0;
unsigned long long pgsp_free = 0;
if (libo4::get_memory_info(&virt_total, &real_total, &real_free, &pgsp_total, &pgsp_free)) {
pmi->virt_total = virt_total;
pmi->real_total = real_total;
pmi->real_free = real_free;
pmi->pgsp_total = pgsp_total;
pmi->pgsp_free = pgsp_free;
return true;
}
return false;
} else {
// On AIX, I use the (dynamically loaded) perfstat library to retrieve memory statistics
// See:
// http://publib.boulder.ibm.com/infocenter/systems/index.jsp
// ?topic=/com.ibm.aix.basetechref/doc/basetrf1/perfstat_memtot.htm
// http://publib.boulder.ibm.com/infocenter/systems/index.jsp
// ?topic=/com.ibm.aix.files/doc/aixfiles/libperfstat.h.htm
perfstat_memory_total_t psmt;
memset (&psmt, '\0', sizeof(psmt));
const int rc = libperfstat::perfstat_memory_total(NULL, &psmt, sizeof(psmt), 1);
if (rc == -1) {
trcVerbose("perfstat_memory_total() failed (errno=%d)", errno);
assert(0, "perfstat_memory_total() failed");
return false;
}
assert(rc == 1, "perfstat_memory_total() - weird return code");
// excerpt from
// http://publib.boulder.ibm.com/infocenter/systems/index.jsp
// ?topic=/com.ibm.aix.files/doc/aixfiles/libperfstat.h.htm
// The fields of perfstat_memory_total_t:
// u_longlong_t virt_total Total virtual memory (in 4 KB pages).
// u_longlong_t real_total Total real memory (in 4 KB pages).
// u_longlong_t real_free Free real memory (in 4 KB pages).
// u_longlong_t pgsp_total Total paging space (in 4 KB pages).
// u_longlong_t pgsp_free Free paging space (in 4 KB pages).
pmi->virt_total = psmt.virt_total * 4096;
pmi->real_total = psmt.real_total * 4096;
pmi->real_free = psmt.real_free * 4096;
pmi->pgsp_total = psmt.pgsp_total * 4096;
pmi->pgsp_free = psmt.pgsp_free * 4096;
return true;
}
} // end os::Aix::get_meminfo
//////////////////////////////////////////////////////////////////////////////
// create new thread
// Thread start routine for all newly created threads
static void *thread_native_entry(Thread *thread) {
thread->record_stack_base_and_size();
const pthread_t pthread_id = ::pthread_self();
const tid_t kernel_thread_id = ::thread_self();
LogTarget(Info, os, thread) lt;
if (lt.is_enabled()) {
address low_address = thread->stack_end();
address high_address = thread->stack_base();
lt.print("Thread is alive (tid: " UINTX_FORMAT ", kernel thread id: " UINTX_FORMAT
", stack [" PTR_FORMAT " - " PTR_FORMAT " (" SIZE_FORMAT "k using %uk pages)).",
os::current_thread_id(), (uintx) kernel_thread_id, low_address, high_address,
(high_address - low_address) / K, os::Aix::query_pagesize(low_address) / K);
}
// Normally, pthread stacks on AIX live in the data segment (are allocated with malloc()
// by the pthread library). In rare cases, this may not be the case, e.g. when third-party
// tools hook pthread_create(). In this case, we may run into problems establishing
// guard pages on those stacks, because the stacks may reside in memory which is not
// protectable (shmated).
if (thread->stack_base() > ::sbrk(0)) {
log_warning(os, thread)("Thread stack not in data segment.");
}
// Try to randomize the cache line index of hot stack frames.
// This helps when threads of the same stack traces evict each other's
// cache lines. The threads can be either from the same JVM instance, or
// from different JVM instances. The benefit is especially true for
// processors with hyperthreading technology.
static int counter = 0;
int pid = os::current_process_id();
alloca(((pid ^ counter++) & 7) * 128);
thread->initialize_thread_current();
OSThread* osthread = thread->osthread();
// Thread_id is pthread id.
osthread->set_thread_id(pthread_id);
// .. but keep kernel thread id too for diagnostics
osthread->set_kernel_thread_id(kernel_thread_id);
// Initialize signal mask for this thread.
PosixSignals::hotspot_sigmask(thread);
// Initialize floating point control register.
os::Aix::init_thread_fpu_state();
assert(osthread->get_state() == RUNNABLE, "invalid os thread state");
// Call one more level start routine.
thread->call_run();
// Note: at this point the thread object may already have deleted itself.
// Prevent dereferencing it from here on out.
thread = NULL;
log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", kernel thread id: " UINTX_FORMAT ").",
os::current_thread_id(), (uintx) kernel_thread_id);
return 0;
}
bool os::create_thread(Thread* thread, ThreadType thr_type,
size_t req_stack_size) {
assert(thread->osthread() == NULL, "caller responsible");
// Allocate the OSThread object.
OSThread* osthread = new OSThread(NULL, NULL);
if (osthread == NULL) {
return false;
}
// Set the correct thread state.
osthread->set_thread_type(thr_type);
// Initial state is ALLOCATED but not INITIALIZED
osthread->set_state(ALLOCATED);
thread->set_osthread(osthread);
// Init thread attributes.
pthread_attr_t attr;
pthread_attr_init(&attr);
guarantee(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0, "???");
// Make sure we run in 1:1 kernel-user-thread mode.
if (os::Aix::on_aix()) {
guarantee(pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) == 0, "???");
guarantee(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) == 0, "???");
}
// Start in suspended state, and in os::thread_start, wake the thread up.
guarantee(pthread_attr_setsuspendstate_np(&attr, PTHREAD_CREATE_SUSPENDED_NP) == 0, "???");
// Calculate stack size if it's not specified by caller.
size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
// JDK-8187028: It was observed that on some configurations (4K backed thread stacks)
// the real thread stack size may be smaller than the requested stack size, by as much as 64K.
// This very much looks like a pthread lib error. As a workaround, increase the stack size
// by 64K for small thread stacks (arbitrarily choosen to be < 4MB)
if (stack_size < 4096 * K) {
stack_size += 64 * K;
}
// On Aix, pthread_attr_setstacksize fails with huge values and leaves the
// thread size in attr unchanged. If this is the minimal stack size as set
// by pthread_attr_init this leads to crashes after thread creation. E.g. the
// guard pages might not fit on the tiny stack created.
int ret = pthread_attr_setstacksize(&attr, stack_size);
if (ret != 0) {
log_warning(os, thread)("The %sthread stack size specified is invalid: " SIZE_FORMAT "k",
(thr_type == compiler_thread) ? "compiler " : ((thr_type == java_thread) ? "" : "VM "),
stack_size / K);
thread->set_osthread(NULL);
delete osthread;
return false;
}
// Save some cycles and a page by disabling OS guard pages where we have our own
// VM guard pages (in java threads). For other threads, keep system default guard
// pages in place.
if (thr_type == java_thread || thr_type == compiler_thread) {
ret = pthread_attr_setguardsize(&attr, 0);
}
pthread_t tid = 0;
if (ret == 0) {
ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
}
if (ret == 0) {
char buf[64];
log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
(uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
} else {
char buf[64];
log_warning(os, thread)("Failed to start thread - pthread_create failed (%d=%s) for attributes: %s.",
ret, os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
// Log some OS information which might explain why creating the thread failed.
log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
LogStream st(Log(os, thread)::info());
os::Posix::print_rlimit_info(&st);
os::print_memory_info(&st);
}
pthread_attr_destroy(&attr);
if (ret != 0) {
// Need to clean up stuff we've allocated so far.
thread->set_osthread(NULL);
delete osthread;
return false;
}
// OSThread::thread_id is the pthread id.
osthread->set_thread_id(tid);
return true;
}
/////////////////////////////////////////////////////////////////////////////
// attach existing thread
// bootstrap the main thread
bool os::create_main_thread(JavaThread* thread) {
assert(os::Aix::_main_thread == pthread_self(), "should be called inside main thread");
return create_attached_thread(thread);
}
bool os::create_attached_thread(JavaThread* thread) {
#ifdef ASSERT
thread->verify_not_published();
#endif
// Allocate the OSThread object
OSThread* osthread = new OSThread(NULL, NULL);
if (osthread == NULL) {
return false;
}
const pthread_t pthread_id = ::pthread_self();
const tid_t kernel_thread_id = ::thread_self();
// OSThread::thread_id is the pthread id.
osthread->set_thread_id(pthread_id);
// .. but keep kernel thread id too for diagnostics
osthread->set_kernel_thread_id(kernel_thread_id);
// initialize floating point control register
os::Aix::init_thread_fpu_state();
// Initial thread state is RUNNABLE
osthread->set_state(RUNNABLE);
thread->set_osthread(osthread);
if (UseNUMA) {
int lgrp_id = os::numa_get_group_id();
if (lgrp_id != -1) {
thread->set_lgrp_id(lgrp_id);
}
}
// initialize signal mask for this thread
// and save the caller's signal mask
PosixSignals::hotspot_sigmask(thread);
log_info(os, thread)("Thread attached (tid: " UINTX_FORMAT ", kernel thread id: " UINTX_FORMAT ").",
os::current_thread_id(), (uintx) kernel_thread_id);
return true;
}
void os::pd_start_thread(Thread* thread) {
int status = pthread_continue_np(thread->osthread()->pthread_id());
assert(status == 0, "thr_continue failed");
}
// Free OS resources related to the OSThread
void os::free_thread(OSThread* osthread) {
assert(osthread != NULL, "osthread not set");
// We are told to free resources of the argument thread,
// but we can only really operate on the current thread.
assert(Thread::current()->osthread() == osthread,
"os::free_thread but not current thread");
// Restore caller's signal mask
sigset_t sigmask = osthread->caller_sigmask();
pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
delete osthread;
}
////////////////////////////////////////////////////////////////////////////////
// time support
// Time since start-up in seconds to a fine granularity.
double os::elapsedTime() {
return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution
}
jlong os::elapsed_counter() {
return javaTimeNanos() - initial_time_count;
}
jlong os::elapsed_frequency() {
return NANOSECS_PER_SEC; // nanosecond resolution
}
bool os::supports_vtime() { return true; }
double os::elapsedVTime() {
struct rusage usage;
int retval = getrusage(RUSAGE_THREAD, &usage);
if (retval == 0) {
return usage.ru_utime.tv_sec + usage.ru_stime.tv_sec + (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / (1000.0 * 1000);
} else {
// better than nothing, but not much
return elapsedTime();
}
}
// We use mread_real_time here.
// On AIX: If the CPU has a time register, the result will be RTC_POWER and
// it has to be converted to real time. AIX documentations suggests to do
// this unconditionally, so we do it.
//
// See: https://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.basetrf2/read_real_time.htm
//
// On PASE: mread_real_time will always return RTC_POWER_PC data, so no
// conversion is necessary. However, mread_real_time will not return
// monotonic results but merely matches read_real_time. So we need a tweak
// to ensure monotonic results.
//
// For PASE no public documentation exists, just word by IBM
jlong os::javaTimeNanos() {
timebasestruct_t time;
int rc = mread_real_time(&time, TIMEBASE_SZ);
if (os::Aix::on_pase()) {
assert(rc == RTC_POWER, "expected time format RTC_POWER from mread_real_time in PASE");
jlong now = jlong(time.tb_high) * NANOSECS_PER_SEC + jlong(time.tb_low);
jlong prev = max_real_time;
if (now <= prev) {
return prev; // same or retrograde time;
}
jlong obsv = Atomic::cmpxchg(&max_real_time, prev, now);
assert(obsv >= prev, "invariant"); // Monotonicity
// If the CAS succeeded then we're done and return "now".
// If the CAS failed and the observed value "obsv" is >= now then
// we should return "obsv". If the CAS failed and now > obsv > prv then
// some other thread raced this thread and installed a new value, in which case
// we could either (a) retry the entire operation, (b) retry trying to install now
// or (c) just return obsv. We use (c). No loop is required although in some cases
// we might discard a higher "now" value in deference to a slightly lower but freshly
// installed obsv value. That's entirely benign -- it admits no new orderings compared
// to (a) or (b) -- and greatly reduces coherence traffic.
// We might also condition (c) on the magnitude of the delta between obsv and now.
// Avoiding excessive CAS operations to hot RW locations is critical.
// See https://blogs.oracle.com/dave/entry/cas_and_cache_trivia_invalidate
return (prev == obsv) ? now : obsv;
} else {
if (rc != RTC_POWER) {
rc = time_base_to_time(&time, TIMEBASE_SZ);
assert(rc != -1, "error calling time_base_to_time()");
}
return jlong(time.tb_high) * NANOSECS_PER_SEC + jlong(time.tb_low);