/
os_solaris.cpp
5234 lines (4497 loc) · 175 KB
/
os_solaris.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) 1997, 2020, Oracle and/or its affiliates. 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.
*
*/
// no precompiled headers
#include "jvm.h"
#include "classfile/classLoader.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/icBuffer.hpp"
#include "code/vtableStubs.hpp"
#include "compiler/compileBroker.hpp"
#include "compiler/disassembler.hpp"
#include "interpreter/interpreter.hpp"
#include "logging/log.hpp"
#include "logging/logStream.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/filemap.hpp"
#include "memory/universe.hpp"
#include "oops/oop.inline.hpp"
#include "os_share_solaris.hpp"
#include "os_solaris.inline.hpp"
#include "prims/jniFastGetField.hpp"
#include "prims/jvm_misc.hpp"
#include "runtime/arguments.hpp"
#include "runtime/atomic.hpp"
#include "runtime/extendedPC.hpp"
#include "runtime/globals.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/java.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/objectMonitor.hpp"
#include "runtime/orderAccess.hpp"
#include "runtime/osThread.hpp"
#include "runtime/perfMemory.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/statSampler.hpp"
#include "runtime/stubRoutines.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadCritical.hpp"
#include "runtime/timer.hpp"
#include "runtime/vm_version.hpp"
#include "semaphore_posix.hpp"
#include "services/attachListener.hpp"
#include "services/memTracker.hpp"
#include "services/runtimeService.hpp"
#include "utilities/align.hpp"
#include "utilities/decoder.hpp"
#include "utilities/defaultStream.hpp"
#include "utilities/events.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/macros.hpp"
#include "utilities/vmError.hpp"
// put OS-includes here
# include <dlfcn.h>
# include <errno.h>
# include <exception>
# include <link.h>
# include <poll.h>
# include <pthread.h>
# include <setjmp.h>
# include <signal.h>
# include <stdio.h>
# include <alloca.h>
# include <sys/filio.h>
# include <sys/ipc.h>
# include <sys/lwp.h>
# include <sys/machelf.h> // for elf Sym structure used by dladdr1
# include <sys/mman.h>
# include <sys/processor.h>
# include <sys/procset.h>
# include <sys/pset.h>
# include <sys/resource.h>
# include <sys/shm.h>
# include <sys/socket.h>
# include <sys/stat.h>
# include <sys/systeminfo.h>
# include <sys/time.h>
# include <sys/times.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <sys/utsname.h>
# include <thread.h>
# include <unistd.h>
# include <sys/priocntl.h>
# include <sys/rtpriocntl.h>
# include <sys/tspriocntl.h>
# include <sys/iapriocntl.h>
# include <sys/fxpriocntl.h>
# include <sys/loadavg.h>
# include <string.h>
# include <stdio.h>
# define _STRUCTURED_PROC 1 // this gets us the new structured proc interfaces of 5.6 & later
# include <sys/procfs.h> // see comment in <sys/procfs.h>
#define MAX_PATH (2 * K)
// for timer info max values which include all bits
#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
// Here are some liblgrp types from sys/lgrp_user.h to be able to
// compile on older systems without this header file.
#ifndef MADV_ACCESS_LWP
#define MADV_ACCESS_LWP 7 /* next LWP to access heavily */
#endif
#ifndef MADV_ACCESS_MANY
#define MADV_ACCESS_MANY 8 /* many processes to access heavily */
#endif
#ifndef LGRP_RSRC_CPU
#define LGRP_RSRC_CPU 0 /* CPU resources */
#endif
#ifndef LGRP_RSRC_MEM
#define LGRP_RSRC_MEM 1 /* memory resources */
#endif
// Values for ThreadPriorityPolicy == 1
int prio_policy1[CriticalPriority+1] = {
-99999, 0, 16, 32, 48, 64,
80, 96, 112, 124, 127, 127 };
// System parameters used internally
static clock_t clock_tics_per_sec = 100;
// Track if we have called enable_extended_FILE_stdio (on Solaris 10u4+)
static bool enabled_extended_FILE_stdio = false;
// For diagnostics to print a message once. see run_periodic_checks
static bool check_addr0_done = false;
static sigset_t check_signal_done;
static bool check_signals = true;
address os::Solaris::handler_start; // start pc of thr_sighndlrinfo
address os::Solaris::handler_end; // end pc of thr_sighndlrinfo
address os::Solaris::_main_stack_base = NULL; // 4352906 workaround
os::Solaris::pthread_setname_np_func_t os::Solaris::_pthread_setname_np = NULL;
// "default" initializers for missing libc APIs
extern "C" {
static int lwp_mutex_init(mutex_t *mx, int scope, void *arg) { memset(mx, 0, sizeof(mutex_t)); return 0; }
static int lwp_mutex_destroy(mutex_t *mx) { return 0; }
static int lwp_cond_init(cond_t *cv, int scope, void *arg){ memset(cv, 0, sizeof(cond_t)); return 0; }
static int lwp_cond_destroy(cond_t *cv) { return 0; }
}
// "default" initializers for pthread-based synchronization
extern "C" {
static int pthread_mutex_default_init(mutex_t *mx, int scope, void *arg) { memset(mx, 0, sizeof(mutex_t)); return 0; }
static int pthread_cond_default_init(cond_t *cv, int scope, void *arg){ memset(cv, 0, sizeof(cond_t)); return 0; }
}
static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
static inline size_t adjust_stack_size(address base, size_t size) {
if ((ssize_t)size < 0) {
// 4759953: Compensate for ridiculous stack size.
size = max_intx;
}
if (size > (size_t)base) {
// 4812466: Make sure size doesn't allow the stack to wrap the address space.
size = (size_t)base;
}
return size;
}
static inline stack_t get_stack_info() {
stack_t st;
int retval = thr_stksegment(&st);
st.ss_size = adjust_stack_size((address)st.ss_sp, st.ss_size);
assert(retval == 0, "incorrect return value from thr_stksegment");
assert((address)&st < (address)st.ss_sp, "Invalid stack base returned");
assert((address)&st > (address)st.ss_sp-st.ss_size, "Invalid stack size returned");
return st;
}
static void _handle_uncaught_cxx_exception() {
VMError::report_and_die("An uncaught C++ exception");
}
bool os::is_primordial_thread(void) {
int r = thr_main();
guarantee(r == 0 || r == 1, "CR6501650 or CR6493689");
return r == 1;
}
address os::current_stack_base() {
bool _is_primordial_thread = is_primordial_thread();
// Workaround 4352906, avoid calls to thr_stksegment by
// thr_main after the first one (it looks like we trash
// some data, causing the value for ss_sp to be incorrect).
if (!_is_primordial_thread || os::Solaris::_main_stack_base == NULL) {
stack_t st = get_stack_info();
if (_is_primordial_thread) {
// cache initial value of stack base
os::Solaris::_main_stack_base = (address)st.ss_sp;
}
return (address)st.ss_sp;
} else {
guarantee(os::Solaris::_main_stack_base != NULL, "Attempt to use null cached stack base");
return os::Solaris::_main_stack_base;
}
}
size_t os::current_stack_size() {
size_t size;
if (!is_primordial_thread()) {
size = get_stack_info().ss_size;
} else {
struct rlimit limits;
getrlimit(RLIMIT_STACK, &limits);
size = adjust_stack_size(os::Solaris::_main_stack_base, (size_t)limits.rlim_cur);
}
// base may not be page aligned
address base = current_stack_base();
address bottom = align_up(base - size, os::vm_page_size());;
return (size_t)(base - bottom);
}
struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {
return localtime_r(clock, res);
}
void os::Solaris::try_enable_extended_io() {
typedef int (*enable_extended_FILE_stdio_t)(int, int);
if (!UseExtendedFileIO) {
return;
}
enable_extended_FILE_stdio_t enabler =
(enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT,
"enable_extended_FILE_stdio");
if (enabler) {
enabler(-1, -1);
}
}
jint os::Solaris::_os_thread_limit = 0;
volatile jint os::Solaris::_os_thread_count = 0;
julong os::available_memory() {
return Solaris::available_memory();
}
julong os::Solaris::available_memory() {
return (julong)sysconf(_SC_AVPHYS_PAGES) * os::vm_page_size();
}
julong os::Solaris::_physical_memory = 0;
julong os::physical_memory() {
return Solaris::physical_memory();
}
static hrtime_t first_hrtime = 0;
static const hrtime_t hrtime_hz = 1000*1000*1000;
static volatile hrtime_t max_hrtime = 0;
void os::Solaris::initialize_system_info() {
set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
_physical_memory = (julong)sysconf(_SC_PHYS_PAGES) *
(julong)sysconf(_SC_PAGESIZE);
}
uint os::processor_id() {
const processorid_t id = ::getcpuid();
assert(id >= 0 && id < _processor_count, "Invalid processor id");
return (uint)id;
}
int os::active_processor_count() {
// User has overridden the number of active processors
if (ActiveProcessorCount > 0) {
log_trace(os)("active_processor_count: "
"active processor count set by user : %d",
ActiveProcessorCount);
return ActiveProcessorCount;
}
int online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
pid_t pid = getpid();
psetid_t pset = PS_NONE;
// Are we running in a processor set or is there any processor set around?
if (pset_bind(PS_QUERY, P_PID, pid, &pset) == 0) {
uint_t pset_cpus;
// Query the number of cpus available to us.
if (pset_info(pset, NULL, &pset_cpus, NULL) == 0) {
assert(pset_cpus > 0 && pset_cpus <= online_cpus, "sanity check");
return pset_cpus;
}
}
// Otherwise return number of online cpus
return online_cpus;
}
void os::set_native_thread_name(const char *name) {
if (Solaris::_pthread_setname_np != NULL) {
// Only the first 31 bytes of 'name' are processed by pthread_setname_np
// but we explicitly copy into a size-limited buffer to avoid any
// possible overflow.
char buf[32];
snprintf(buf, sizeof(buf), "%s", name);
buf[sizeof(buf) - 1] = '\0';
Solaris::_pthread_setname_np(pthread_self(), buf);
}
}
bool os::bind_to_processor(uint processor_id) {
// We assume that a processorid_t can be stored in a uint.
assert(sizeof(uint) == sizeof(processorid_t),
"can't convert uint to processorid_t");
int bind_result =
processor_bind(P_LWPID, // bind LWP.
P_MYID, // bind current LWP.
(processorid_t) processor_id, // id.
NULL); // don't return old binding.
return (bind_result == 0);
}
// 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;
}
void os::init_system_properties_values() {
// The next steps are taken in the product version:
//
// Obtain the JAVA_HOME value from the location of libjvm.so.
// This library should be located at:
// <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
//
// If "/jre/lib/" appears at the right place in the path, then we
// assume libjvm.so is installed in a JDK and we use this path.
//
// Otherwise exit with message: "Could not create the Java virtual machine."
//
// The following extra steps are taken in the debugging version:
//
// If "/jre/lib/" does NOT appear at the right place in the path
// instead of exit check for $JAVA_HOME environment variable.
//
// If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
// then we append a fake suffix "hotspot/libjvm.so" to this path so
// it looks like libjvm.so is installed there
// <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
//
// Otherwise exit.
//
// Important note: if the location of libjvm.so changes this
// code needs to be changed accordingly.
// Base path of extensions installed on the system.
#define SYS_EXT_DIR "/usr/jdk/packages"
#define EXTENSIONS_DIR "/lib/ext"
// Buffer that fits several sprintfs.
// Note that the space for the colon and the trailing null are provided
// by the nulls included by the sizeof operator.
const size_t bufsize =
MAX3((size_t)MAXPATHLEN, // For dll_dir & friends.
sizeof(SYS_EXT_DIR) + sizeof("/lib/"), // invariant ld_library_path
(size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + 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.
*(strrchr(buf, '/')) = '\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.
{
// Use dlinfo() to determine the correct java.library.path.
//
// If we're launched by the Java launcher, and the user
// does not set java.library.path explicitly on the commandline,
// the Java launcher sets LD_LIBRARY_PATH for us and unsets
// LD_LIBRARY_PATH_32 and LD_LIBRARY_PATH_64. In this case
// dlinfo returns LD_LIBRARY_PATH + crle settings (including
// /usr/lib), which is exactly what we want.
//
// If the user does set java.library.path, it completely
// overwrites this setting, and always has.
//
// If we're not launched by the Java launcher, we may
// get here with any/all of the LD_LIBRARY_PATH[_32|64]
// settings. Again, dlinfo does exactly what we want.
Dl_serinfo info_sz, *info = &info_sz;
Dl_serpath *path;
char *library_path;
char *common_path = buf;
// Determine search path count and required buffer size.
if (dlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info) == -1) {
FREE_C_HEAP_ARRAY(char, buf);
vm_exit_during_initialization("dlinfo SERINFOSIZE request", dlerror());
}
// Allocate new buffer and initialize.
info = (Dl_serinfo*)NEW_C_HEAP_ARRAY(char, info_sz.dls_size, mtInternal);
info->dls_size = info_sz.dls_size;
info->dls_cnt = info_sz.dls_cnt;
// Obtain search path information.
if (dlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info) == -1) {
FREE_C_HEAP_ARRAY(char, buf);
FREE_C_HEAP_ARRAY(char, info);
vm_exit_during_initialization("dlinfo SERINFO request", dlerror());
}
path = &info->dls_serpath[0];
// Note: Due to a legacy implementation, most of the library path
// is set in the launcher. This was to accomodate linking restrictions
// on legacy Solaris implementations (which are no longer supported).
// Eventually, all the library path setting will be done here.
//
// However, to prevent the proliferation of improperly built native
// libraries, the new path component /usr/jdk/packages is added here.
// Construct the invariant part of ld_library_path.
sprintf(common_path, SYS_EXT_DIR "/lib");
// Struct size is more than sufficient for the path components obtained
// through the dlinfo() call, so only add additional space for the path
// components explicitly added here.
size_t library_path_size = info->dls_size + strlen(common_path);
library_path = NEW_C_HEAP_ARRAY(char, library_path_size, mtInternal);
library_path[0] = '\0';
// Construct the desired Java library path from the linker's library
// search path.
//
// For compatibility, it is optimal that we insert the additional path
// components specific to the Java VM after those components specified
// in LD_LIBRARY_PATH (if any) but before those added by the ld.so
// infrastructure.
if (info->dls_cnt == 0) { // Not sure this can happen, but allow for it.
strcpy(library_path, common_path);
} else {
int inserted = 0;
int i;
for (i = 0; i < info->dls_cnt; i++, path++) {
uint_t flags = path->dls_flags & LA_SER_MASK;
if (((flags & LA_SER_LIBPATH) == 0) && !inserted) {
strcat(library_path, common_path);
strcat(library_path, os::path_separator());
inserted = 1;
}
strcat(library_path, path->dls_name);
strcat(library_path, os::path_separator());
}
// Eliminate trailing path separator.
library_path[strlen(library_path)-1] = '\0';
}
// happens before argument parsing - can't use a trace flag
// tty->print_raw("init_system_properties_values: native lib path: ");
// tty->print_raw_cr(library_path);
// Callee copies into its own buffer.
Arguments::set_library_path(library_path);
FREE_C_HEAP_ARRAY(char, library_path);
FREE_C_HEAP_ARRAY(char, info);
}
// Extensions directories.
sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
Arguments::set_ext_dirs(buf);
FREE_C_HEAP_ARRAY(char, buf);
#undef SYS_EXT_DIR
#undef EXTENSIONS_DIR
}
void os::breakpoint() {
BREAKPOINT;
}
extern "C" void breakpoint() {
// use debugger to set breakpoint here
}
static thread_t main_thread;
// Thread start routine for all newly created threads
extern "C" void* thread_native_entry(void* thread_addr) {
Thread* thread = (Thread*)thread_addr;
thread->record_stack_base_and_size();
// 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);
int prio;
thread->initialize_thread_current();
OSThread* osthr = thread->osthread();
osthr->set_lwp_id(_lwp_self()); // Store lwp in case we are bound
log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ").",
os::current_thread_id());
if (UseNUMA) {
int lgrp_id = os::numa_get_group_id();
if (lgrp_id != -1) {
thread->set_lgrp_id(lgrp_id);
}
}
// Our priority was set when we were created, and stored in the
// osthread, but couldn't be passed through to our LWP until now.
// So read back the priority and set it again.
if (osthr->thread_id() != -1) {
if (UseThreadPriorities) {
int prio = osthr->native_priority();
if (ThreadPriorityVerbose) {
tty->print_cr("Starting Thread " INTPTR_FORMAT ", LWP is "
INTPTR_FORMAT ", setting priority: %d\n",
osthr->thread_id(), osthr->lwp_id(), prio);
}
os::set_native_priority(thread, prio);
}
} else if (ThreadPriorityVerbose) {
warning("Can't set priority in _start routine, thread id hasn't been set\n");
}
assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
// initialize signal mask for this thread
os::Solaris::hotspot_sigmask(thread);
os::Solaris::init_thread_fpu_state();
std::set_terminate(_handle_uncaught_cxx_exception);
thread->call_run();
// Note: at this point the thread object may already have deleted itself.
// Do not dereference it from here on out.
// One less thread is executing
// When the VMThread gets here, the main thread may have already exited
// which frees the CodeHeap containing the Atomic::dec code
if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
Atomic::dec(&os::Solaris::_os_thread_count);
}
log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ").", os::current_thread_id());
if (UseDetachedThreads) {
thr_exit(NULL);
ShouldNotReachHere();
}
return NULL;
}
static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
// Allocate the OSThread object
OSThread* osthread = new OSThread(NULL, NULL);
if (osthread == NULL) return NULL;
// Store info on the Solaris thread into the OSThread
osthread->set_thread_id(thread_id);
osthread->set_lwp_id(_lwp_self());
if (UseNUMA) {
int lgrp_id = os::numa_get_group_id();
if (lgrp_id != -1) {
thread->set_lgrp_id(lgrp_id);
}
}
if (ThreadPriorityVerbose) {
tty->print_cr("In create_os_thread, Thread " INTPTR_FORMAT ", LWP is " INTPTR_FORMAT "\n",
osthread->thread_id(), osthread->lwp_id());
}
// Initial thread state is INITIALIZED, not SUSPENDED
osthread->set_state(INITIALIZED);
return osthread;
}
void os::Solaris::hotspot_sigmask(Thread* thread) {
//Save caller's signal mask
sigset_t sigmask;
pthread_sigmask(SIG_SETMASK, NULL, &sigmask);
OSThread *osthread = thread->osthread();
osthread->set_caller_sigmask(sigmask);
pthread_sigmask(SIG_UNBLOCK, os::Solaris::unblocked_signals(), NULL);
if (!ReduceSignalUsage) {
if (thread->is_VM_thread()) {
// Only the VM thread handles BREAK_SIGNAL ...
pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL);
} else {
// ... all other threads block BREAK_SIGNAL
assert(!sigismember(vm_signals(), SIGINT), "SIGINT should not be blocked");
pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);
}
}
}
bool os::create_attached_thread(JavaThread* thread) {
#ifdef ASSERT
thread->verify_not_published();
#endif
OSThread* osthread = create_os_thread(thread, thr_self());
if (osthread == NULL) {
return false;
}
// Initial thread state is RUNNABLE
osthread->set_state(RUNNABLE);
thread->set_osthread(osthread);
// initialize signal mask for this thread
// and save the caller's signal mask
os::Solaris::hotspot_sigmask(thread);
log_info(os, thread)("Thread attached (tid: " UINTX_FORMAT ").",
os::current_thread_id());
return true;
}
bool os::create_main_thread(JavaThread* thread) {
#ifdef ASSERT
thread->verify_not_published();
#endif
if (_starting_thread == NULL) {
_starting_thread = create_os_thread(thread, main_thread);
if (_starting_thread == NULL) {
return false;
}
}
// The primodial thread is runnable from the start
_starting_thread->set_state(RUNNABLE);
thread->set_osthread(_starting_thread);
// initialize signal mask for this thread
// and save the caller's signal mask
os::Solaris::hotspot_sigmask(thread);
return true;
}
// Helper function to trace thread attributes, similar to os::Posix::describe_pthread_attr()
static char* describe_thr_create_attributes(char* buf, size_t buflen,
size_t stacksize, long flags) {
stringStream ss(buf, buflen);
ss.print("stacksize: " SIZE_FORMAT "k, ", stacksize / 1024);
ss.print("flags: ");
#define PRINT_FLAG(f) if (flags & f) ss.print( #f " ");
#define ALL(X) \
X(THR_SUSPENDED) \
X(THR_DETACHED) \
X(THR_BOUND) \
X(THR_NEW_LWP) \
X(THR_DAEMON)
ALL(PRINT_FLAG)
#undef ALL
#undef PRINT_FLAG
return buf;
}
// return default stack size for thr_type
size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
// default stack size when not specified by caller is 1M (2M for LP64)
size_t s = (BytesPerWord >> 2) * K * K;
return s;
}
bool os::create_thread(Thread* thread, ThreadType thr_type,
size_t req_stack_size) {
// Allocate the OSThread object
OSThread* osthread = new OSThread(NULL, NULL);
if (osthread == NULL) {
return false;
}
if (ThreadPriorityVerbose) {
char *thrtyp;
switch (thr_type) {
case vm_thread:
thrtyp = (char *)"vm";
break;
case cgc_thread:
thrtyp = (char *)"cgc";
break;
case pgc_thread:
thrtyp = (char *)"pgc";
break;
case java_thread:
thrtyp = (char *)"java";
break;
case compiler_thread:
thrtyp = (char *)"compiler";
break;
case watcher_thread:
thrtyp = (char *)"watcher";
break;
default:
thrtyp = (char *)"unknown";
break;
}
tty->print_cr("In create_thread, creating a %s thread\n", thrtyp);
}
// 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);
// Initial state is ALLOCATED but not INITIALIZED
osthread->set_state(ALLOCATED);
if (os::Solaris::_os_thread_count > os::Solaris::_os_thread_limit) {
// We got lots of threads. Check if we still have some address space left.
// Need to be at least 5Mb of unreserved address space. We do check by
// trying to reserve some.
const size_t VirtualMemoryBangSize = 20*K*K;
char* mem = os::reserve_memory(VirtualMemoryBangSize);
if (mem == NULL) {
delete osthread;
return false;
} else {
// Release the memory again
os::release_memory(mem, VirtualMemoryBangSize);
}
}
// Setup osthread because the child thread may need it.
thread->set_osthread(osthread);
// Create the Solaris thread
thread_t tid = 0;
long flags = (UseDetachedThreads ? THR_DETACHED : 0) | THR_SUSPENDED;
int status;
// Mark that we don't have an lwp or thread id yet.
// In case we attempt to set the priority before the thread starts.
osthread->set_lwp_id(-1);
osthread->set_thread_id(-1);
status = thr_create(NULL, stack_size, thread_native_entry, thread, flags, &tid);
char buf[64];
if (status == 0) {
log_info(os, thread)("Thread started (tid: " UINTX_FORMAT ", attributes: %s). ",
(uintx) tid, describe_thr_create_attributes(buf, sizeof(buf), stack_size, flags));
} else {
log_warning(os, thread)("Failed to start thread - thr_create failed (%s) for attributes: %s.",
os::errno_name(status), describe_thr_create_attributes(buf, sizeof(buf), stack_size, flags));
// 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);
}
if (status != 0) {
thread->set_osthread(NULL);
// Need to clean up stuff we've allocated so far
delete osthread;
return false;
}
Atomic::inc(&os::Solaris::_os_thread_count);
// Store info on the Solaris thread into the OSThread
osthread->set_thread_id(tid);
// Remember that we created this thread so we can set priority on it
osthread->set_vm_created();
// Most thread types will set an explicit priority before starting the thread,
// but for those that don't we need a valid value to read back in thread_native_entry.
osthread->set_native_priority(NormPriority);
// Initial thread state is INITIALIZED, not SUSPENDED
osthread->set_state(INITIALIZED);
// The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
return true;
}
debug_only(static bool signal_sets_initialized = false);
static sigset_t unblocked_sigs, vm_sigs;
void os::Solaris::signal_sets_init() {
// Should also have an assertion stating we are still single-threaded.
assert(!signal_sets_initialized, "Already initialized");
// Fill in signals that are necessarily unblocked for all threads in
// the VM. Currently, we unblock the following signals:
// SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden
// by -Xrs (=ReduceSignalUsage));
// BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all
// other threads. The "ReduceSignalUsage" boolean tells us not to alter
// the dispositions or masks wrt these signals.
// Programs embedding the VM that want to use the above signals for their
// own purposes must, at this time, use the "-Xrs" option to prevent
// interference with shutdown hooks and BREAK_SIGNAL thread dumping.
// (See bug 4345157, and other related bugs).
// In reality, though, unblocking these signals is really a nop, since
// these signals are not blocked by default.
sigemptyset(&unblocked_sigs);
sigaddset(&unblocked_sigs, SIGILL);
sigaddset(&unblocked_sigs, SIGSEGV);
sigaddset(&unblocked_sigs, SIGBUS);
sigaddset(&unblocked_sigs, SIGFPE);
sigaddset(&unblocked_sigs, ASYNC_SIGNAL);
if (!ReduceSignalUsage) {
if (!os::Posix::is_sig_ignored(SHUTDOWN1_SIGNAL)) {
sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);
}
if (!os::Posix::is_sig_ignored(SHUTDOWN2_SIGNAL)) {
sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);
}
if (!os::Posix::is_sig_ignored(SHUTDOWN3_SIGNAL)) {
sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);
}
}
// Fill in signals that are blocked by all but the VM thread.
sigemptyset(&vm_sigs);
if (!ReduceSignalUsage) {
sigaddset(&vm_sigs, BREAK_SIGNAL);
}
debug_only(signal_sets_initialized = true);
// For diagnostics only used in run_periodic_checks
sigemptyset(&check_signal_done);
}
// These are signals that are unblocked while a thread is running Java.
// (For some reason, they get blocked by default.)
sigset_t* os::Solaris::unblocked_signals() {
assert(signal_sets_initialized, "Not initialized");
return &unblocked_sigs;
}
// These are the signals that are blocked while a (non-VM) thread is
// running Java. Only the VM thread handles these signals.
sigset_t* os::Solaris::vm_signals() {
assert(signal_sets_initialized, "Not initialized");
return &vm_sigs;
}
// CR 7190089: on Solaris, primordial thread's stack needs adjusting.
// Without the adjustment, stack size is incorrect if stack is set to unlimited (ulimit -s unlimited).
void os::Solaris::correct_stack_boundaries_for_primordial_thread(Thread* thr) {
assert(is_primordial_thread(), "Call only for primordial thread");
JavaThread* jt = (JavaThread *)thr;
assert(jt != NULL, "Sanity check");
size_t stack_size;
address base = jt->stack_base();
if (Arguments::created_by_java_launcher()) {
// Use 2MB to allow for Solaris 7 64 bit mode.
stack_size = JavaThread::stack_size_at_create() == 0
? 2048*K : JavaThread::stack_size_at_create();
// There are rare cases when we may have already used more than
// the basic stack size allotment before this method is invoked.
// Attempt to allow for a normally sized java_stack.
size_t current_stack_offset = (size_t)(base - (address)&stack_size);
stack_size += ReservedSpace::page_align_size_down(current_stack_offset);
} else {
// 6269555: If we were not created by a Java launcher, i.e. if we are
// running embedded in a native application, treat the primordial thread
// as much like a native attached thread as possible. This means using
// the current stack size from thr_stksegment(), unless it is too large
// to reliably setup guard pages. A reasonable max size is 8MB.
size_t current_size = os::current_stack_size();
// This should never happen, but just in case....
if (current_size == 0) current_size = 2 * K * K;
stack_size = current_size > (8 * K * K) ? (8 * K * K) : current_size;
}
address bottom = align_up(base - stack_size, os::vm_page_size());;
stack_size = (size_t)(base - bottom);
assert(stack_size > 0, "Stack size calculation problem");
if (stack_size > jt->stack_size()) {
#ifndef PRODUCT
struct rlimit limits;
getrlimit(RLIMIT_STACK, &limits);
size_t size = adjust_stack_size(base, (size_t)limits.rlim_cur);
assert(size >= jt->stack_size(), "Stack size problem in main thread");
#endif
tty->print_cr("Stack size of %d Kb exceeds current limit of %d Kb.\n"
"(Stack sizes are rounded up to a multiple of the system page size.)\n"
"See limit(1) to increase the stack size limit.",
stack_size / K, jt->stack_size() / K);
vm_exit(1);
}
assert(jt->stack_size() >= stack_size,
"Attempt to map more stack than was allocated");
jt->set_stack_size(stack_size);
}
// Free Solaris resources related to the OSThread
void os::free_thread(OSThread* osthread) {
assert(osthread != NULL, "os::free_thread but 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;
}
void os::pd_start_thread(Thread* thread) {
int status = thr_continue(thread->osthread()->thread_id());
assert_status(status == 0, status, "thr_continue failed");
}
intx os::current_thread_id() {