src/share/vm/classfile/classLoader.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 8717
77d9c9da7188
child 9926
d20a5f399218
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/classFileParser.hpp"
stefank@2314 27 #include "classfile/classFileStream.hpp"
stefank@2314 28 #include "classfile/classLoader.hpp"
iklam@7089 29 #include "classfile/classLoaderExt.hpp"
coleenp@4304 30 #include "classfile/classLoaderData.inline.hpp"
stefank@2314 31 #include "classfile/javaClasses.hpp"
iklam@7089 32 #if INCLUDE_CDS
iklam@7089 33 #include "classfile/sharedPathsMiscInfo.hpp"
iklam@7089 34 #include "classfile/sharedClassUtil.hpp"
iklam@7089 35 #endif
stefank@2314 36 #include "classfile/systemDictionary.hpp"
stefank@2314 37 #include "classfile/vmSymbols.hpp"
stefank@2314 38 #include "compiler/compileBroker.hpp"
stefank@2314 39 #include "gc_interface/collectedHeap.inline.hpp"
stefank@2314 40 #include "interpreter/bytecodeStream.hpp"
stefank@2314 41 #include "interpreter/oopMapCache.hpp"
stefank@2314 42 #include "memory/allocation.inline.hpp"
iklam@7089 43 #include "memory/filemap.hpp"
stefank@2314 44 #include "memory/generation.hpp"
stefank@2314 45 #include "memory/oopFactory.hpp"
stefank@2314 46 #include "memory/universe.inline.hpp"
stefank@2314 47 #include "oops/instanceKlass.hpp"
stefank@2314 48 #include "oops/instanceRefKlass.hpp"
stefank@2314 49 #include "oops/oop.inline.hpp"
coleenp@2497 50 #include "oops/symbol.hpp"
stefank@2314 51 #include "prims/jvm_misc.hpp"
stefank@2314 52 #include "runtime/arguments.hpp"
stefank@2314 53 #include "runtime/compilationPolicy.hpp"
stefank@2314 54 #include "runtime/fprofiler.hpp"
stefank@2314 55 #include "runtime/handles.hpp"
stefank@2314 56 #include "runtime/handles.inline.hpp"
stefank@2314 57 #include "runtime/init.hpp"
stefank@2314 58 #include "runtime/interfaceSupport.hpp"
stefank@2314 59 #include "runtime/java.hpp"
stefank@2314 60 #include "runtime/javaCalls.hpp"
stefank@2314 61 #include "runtime/threadCritical.hpp"
stefank@2314 62 #include "runtime/timer.hpp"
stefank@2314 63 #include "services/management.hpp"
stefank@2314 64 #include "services/threadService.hpp"
stefank@2314 65 #include "utilities/events.hpp"
stefank@2314 66 #include "utilities/hashtable.hpp"
stefank@2314 67 #include "utilities/hashtable.inline.hpp"
stefank@2314 68 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 69 # include "os_linux.inline.hpp"
stefank@2314 70 #endif
stefank@2314 71 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 72 # include "os_solaris.inline.hpp"
stefank@2314 73 #endif
stefank@2314 74 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 75 # include "os_windows.inline.hpp"
stefank@2314 76 #endif
goetz@6461 77 #ifdef TARGET_OS_FAMILY_aix
goetz@6461 78 # include "os_aix.inline.hpp"
goetz@6461 79 #endif
never@3156 80 #ifdef TARGET_OS_FAMILY_bsd
never@3156 81 # include "os_bsd.inline.hpp"
never@3156 82 #endif
duke@435 83
duke@435 84
duke@435 85 // Entry points in zip.dll for loading zip/jar file entries
duke@435 86
duke@435 87 typedef void * * (JNICALL *ZipOpen_t)(const char *name, char **pmsg);
duke@435 88 typedef void (JNICALL *ZipClose_t)(jzfile *zip);
duke@435 89 typedef jzentry* (JNICALL *FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen);
duke@435 90 typedef jboolean (JNICALL *ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf);
duke@435 91 typedef jboolean (JNICALL *ReadMappedEntry_t)(jzfile *zip, jzentry *entry, unsigned char **buf, char *namebuf);
duke@435 92 typedef jzentry* (JNICALL *GetNextEntry_t)(jzfile *zip, jint n);
jiangli@6868 93 typedef jint (JNICALL *Crc32_t)(jint crc, const jbyte *buf, jint len);
duke@435 94
duke@435 95 static ZipOpen_t ZipOpen = NULL;
duke@435 96 static ZipClose_t ZipClose = NULL;
duke@435 97 static FindEntry_t FindEntry = NULL;
duke@435 98 static ReadEntry_t ReadEntry = NULL;
duke@435 99 static ReadMappedEntry_t ReadMappedEntry = NULL;
duke@435 100 static GetNextEntry_t GetNextEntry = NULL;
duke@435 101 static canonicalize_fn_t CanonicalizeEntry = NULL;
jiangli@6868 102 static Crc32_t Crc32 = NULL;
duke@435 103
duke@435 104 // Globals
duke@435 105
duke@435 106 PerfCounter* ClassLoader::_perf_accumulated_time = NULL;
duke@435 107 PerfCounter* ClassLoader::_perf_classes_inited = NULL;
duke@435 108 PerfCounter* ClassLoader::_perf_class_init_time = NULL;
mchung@1310 109 PerfCounter* ClassLoader::_perf_class_init_selftime = NULL;
mchung@1310 110 PerfCounter* ClassLoader::_perf_classes_verified = NULL;
duke@435 111 PerfCounter* ClassLoader::_perf_class_verify_time = NULL;
mchung@1310 112 PerfCounter* ClassLoader::_perf_class_verify_selftime = NULL;
duke@435 113 PerfCounter* ClassLoader::_perf_classes_linked = NULL;
duke@435 114 PerfCounter* ClassLoader::_perf_class_link_time = NULL;
mchung@1310 115 PerfCounter* ClassLoader::_perf_class_link_selftime = NULL;
mchung@1310 116 PerfCounter* ClassLoader::_perf_class_parse_time = NULL;
mchung@1310 117 PerfCounter* ClassLoader::_perf_class_parse_selftime = NULL;
mchung@1310 118 PerfCounter* ClassLoader::_perf_sys_class_lookup_time = NULL;
mchung@1310 119 PerfCounter* ClassLoader::_perf_shared_classload_time = NULL;
mchung@1310 120 PerfCounter* ClassLoader::_perf_sys_classload_time = NULL;
mchung@1310 121 PerfCounter* ClassLoader::_perf_app_classload_time = NULL;
mchung@1310 122 PerfCounter* ClassLoader::_perf_app_classload_selftime = NULL;
mchung@1310 123 PerfCounter* ClassLoader::_perf_app_classload_count = NULL;
mchung@1310 124 PerfCounter* ClassLoader::_perf_define_appclasses = NULL;
mchung@1310 125 PerfCounter* ClassLoader::_perf_define_appclass_time = NULL;
mchung@1310 126 PerfCounter* ClassLoader::_perf_define_appclass_selftime = NULL;
mchung@1310 127 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = NULL;
mchung@1310 128 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;
duke@435 129 PerfCounter* ClassLoader::_sync_systemLoaderLockContentionRate = NULL;
duke@435 130 PerfCounter* ClassLoader::_sync_nonSystemLoaderLockContentionRate = NULL;
duke@435 131 PerfCounter* ClassLoader::_sync_JVMFindLoadedClassLockFreeCounter = NULL;
duke@435 132 PerfCounter* ClassLoader::_sync_JVMDefineClassLockFreeCounter = NULL;
duke@435 133 PerfCounter* ClassLoader::_sync_JNIDefineClassLockFreeCounter = NULL;
duke@435 134 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;
duke@435 135 PerfCounter* ClassLoader::_isUnsyncloadClass = NULL;
duke@435 136 PerfCounter* ClassLoader::_load_instance_class_failCounter = NULL;
duke@435 137
duke@435 138 ClassPathEntry* ClassLoader::_first_entry = NULL;
duke@435 139 ClassPathEntry* ClassLoader::_last_entry = NULL;
iklam@7089 140 int ClassLoader::_num_entries = 0;
duke@435 141 PackageHashtable* ClassLoader::_package_hash_table = NULL;
duke@435 142
iklam@7089 143 #if INCLUDE_CDS
iklam@7089 144 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
iklam@7089 145 #endif
duke@435 146 // helper routines
duke@435 147 bool string_starts_with(const char* str, const char* str_to_find) {
duke@435 148 size_t str_len = strlen(str);
duke@435 149 size_t str_to_find_len = strlen(str_to_find);
duke@435 150 if (str_to_find_len > str_len) {
duke@435 151 return false;
duke@435 152 }
duke@435 153 return (strncmp(str, str_to_find, str_to_find_len) == 0);
duke@435 154 }
duke@435 155
duke@435 156 bool string_ends_with(const char* str, const char* str_to_find) {
duke@435 157 size_t str_len = strlen(str);
duke@435 158 size_t str_to_find_len = strlen(str_to_find);
duke@435 159 if (str_to_find_len > str_len) {
duke@435 160 return false;
duke@435 161 }
duke@435 162 return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
duke@435 163 }
duke@435 164
duke@435 165
duke@435 166 MetaIndex::MetaIndex(char** meta_package_names, int num_meta_package_names) {
duke@435 167 if (num_meta_package_names == 0) {
duke@435 168 _meta_package_names = NULL;
duke@435 169 _num_meta_package_names = 0;
duke@435 170 } else {
zgu@3900 171 _meta_package_names = NEW_C_HEAP_ARRAY(char*, num_meta_package_names, mtClass);
duke@435 172 _num_meta_package_names = num_meta_package_names;
duke@435 173 memcpy(_meta_package_names, meta_package_names, num_meta_package_names * sizeof(char*));
duke@435 174 }
duke@435 175 }
duke@435 176
duke@435 177
duke@435 178 MetaIndex::~MetaIndex() {
zgu@3900 179 FREE_C_HEAP_ARRAY(char*, _meta_package_names, mtClass);
duke@435 180 }
duke@435 181
duke@435 182
duke@435 183 bool MetaIndex::may_contain(const char* class_name) {
duke@435 184 if ( _num_meta_package_names == 0) {
duke@435 185 return false;
duke@435 186 }
duke@435 187 size_t class_name_len = strlen(class_name);
duke@435 188 for (int i = 0; i < _num_meta_package_names; i++) {
duke@435 189 char* pkg = _meta_package_names[i];
duke@435 190 size_t pkg_len = strlen(pkg);
duke@435 191 size_t min_len = MIN2(class_name_len, pkg_len);
duke@435 192 if (!strncmp(class_name, pkg, min_len)) {
duke@435 193 return true;
duke@435 194 }
duke@435 195 }
duke@435 196 return false;
duke@435 197 }
duke@435 198
duke@435 199
duke@435 200 ClassPathEntry::ClassPathEntry() {
duke@435 201 set_next(NULL);
duke@435 202 }
duke@435 203
duke@435 204
duke@435 205 bool ClassPathEntry::is_lazy() {
duke@435 206 return false;
duke@435 207 }
duke@435 208
iklam@7090 209 ClassPathDirEntry::ClassPathDirEntry(const char* dir) : ClassPathEntry() {
iklam@7090 210 char* copy = NEW_C_HEAP_ARRAY(char, strlen(dir)+1, mtClass);
iklam@7090 211 strcpy(copy, dir);
iklam@7090 212 _dir = copy;
duke@435 213 }
duke@435 214
duke@435 215
ccheung@5603 216 ClassFileStream* ClassPathDirEntry::open_stream(const char* name, TRAPS) {
duke@435 217 // construct full path name
duke@435 218 char path[JVM_MAXPATHLEN];
duke@435 219 if (jio_snprintf(path, sizeof(path), "%s%s%s", _dir, os::file_separator(), name) == -1) {
duke@435 220 return NULL;
duke@435 221 }
duke@435 222 // check if file exists
duke@435 223 struct stat st;
duke@435 224 if (os::stat(path, &st) == 0) {
iklam@7089 225 #if INCLUDE_CDS
iklam@7089 226 if (DumpSharedSpaces) {
iklam@7089 227 // We have already check in ClassLoader::check_shared_classpath() that the directory is empty, so
iklam@7089 228 // we should never find a file underneath it -- unless user has added a new file while we are running
iklam@7089 229 // the dump, in which case let's quit!
iklam@7089 230 ShouldNotReachHere();
iklam@7089 231 }
iklam@7089 232 #endif
duke@435 233 // found file, open it
ikrylov@2322 234 int file_handle = os::open(path, 0, 0);
duke@435 235 if (file_handle != -1) {
duke@435 236 // read contents into resource array
duke@435 237 u1* buffer = NEW_RESOURCE_ARRAY(u1, st.st_size);
duke@435 238 size_t num_read = os::read(file_handle, (char*) buffer, st.st_size);
duke@435 239 // close file
ikrylov@2322 240 os::close(file_handle);
duke@435 241 // construct ClassFileStream
duke@435 242 if (num_read == (size_t)st.st_size) {
mchung@1310 243 if (UsePerfData) {
mchung@1310 244 ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read);
mchung@1310 245 }
duke@435 246 return new ClassFileStream(buffer, st.st_size, _dir); // Resource allocated
duke@435 247 }
duke@435 248 }
duke@435 249 }
duke@435 250 return NULL;
duke@435 251 }
duke@435 252
duke@435 253
duke@435 254 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name) : ClassPathEntry() {
duke@435 255 _zip = zip;
iklam@7090 256 char *copy = NEW_C_HEAP_ARRAY(char, strlen(zip_name)+1, mtClass);
iklam@7090 257 strcpy(copy, zip_name);
iklam@7090 258 _zip_name = copy;
duke@435 259 }
duke@435 260
duke@435 261 ClassPathZipEntry::~ClassPathZipEntry() {
duke@435 262 if (ZipClose != NULL) {
duke@435 263 (*ZipClose)(_zip);
duke@435 264 }
zgu@3900 265 FREE_C_HEAP_ARRAY(char, _zip_name, mtClass);
duke@435 266 }
duke@435 267
iklam@7089 268 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) {
iklam@7089 269 // enable call to C land
duke@435 270 JavaThread* thread = JavaThread::current();
duke@435 271 ThreadToNativeFromVM ttn(thread);
duke@435 272 // check whether zip archive contains name
iklam@7089 273 jint name_len;
iklam@7089 274 jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len);
duke@435 275 if (entry == NULL) return NULL;
duke@435 276 u1* buffer;
duke@435 277 char name_buf[128];
duke@435 278 char* filename;
duke@435 279 if (name_len < 128) {
duke@435 280 filename = name_buf;
duke@435 281 } else {
duke@435 282 filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
duke@435 283 }
duke@435 284
iklam@7089 285 // file found, get pointer to the entry in mmapped jar file.
duke@435 286 if (ReadMappedEntry == NULL ||
duke@435 287 !(*ReadMappedEntry)(_zip, entry, &buffer, filename)) {
iklam@7089 288 // mmapped access not available, perhaps due to compression,
duke@435 289 // read contents into resource array
iklam@7089 290 int size = (*filesize) + ((nul_terminate) ? 1 : 0);
iklam@7089 291 buffer = NEW_RESOURCE_ARRAY(u1, size);
duke@435 292 if (!(*ReadEntry)(_zip, entry, buffer, filename)) return NULL;
duke@435 293 }
iklam@7089 294
iklam@7089 295 // return result
iklam@7089 296 if (nul_terminate) {
iklam@7089 297 buffer[*filesize] = 0;
iklam@7089 298 }
iklam@7089 299 return buffer;
iklam@7089 300 }
iklam@7089 301
iklam@7089 302 ClassFileStream* ClassPathZipEntry::open_stream(const char* name, TRAPS) {
iklam@7089 303 jint filesize;
iklam@7089 304 u1* buffer = open_entry(name, &filesize, false, CHECK_NULL);
iklam@7089 305 if (buffer == NULL) {
iklam@7089 306 return NULL;
iklam@7089 307 }
mchung@1310 308 if (UsePerfData) {
mchung@1310 309 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
mchung@1310 310 }
iklam@7089 311 return new ClassFileStream(buffer, filesize, _zip_name); // Resource allocated
duke@435 312 }
duke@435 313
duke@435 314 // invoke function for each entry in the zip file
duke@435 315 void ClassPathZipEntry::contents_do(void f(const char* name, void* context), void* context) {
duke@435 316 JavaThread* thread = JavaThread::current();
duke@435 317 HandleMark handle_mark(thread);
duke@435 318 ThreadToNativeFromVM ttn(thread);
duke@435 319 for (int n = 0; ; n++) {
duke@435 320 jzentry * ze = ((*GetNextEntry)(_zip, n));
duke@435 321 if (ze == NULL) break;
duke@435 322 (*f)(ze->name, context);
duke@435 323 }
duke@435 324 }
duke@435 325
iklam@7090 326 LazyClassPathEntry::LazyClassPathEntry(const char* path, const struct stat* st, bool throw_exception) : ClassPathEntry() {
duke@435 327 _path = strdup(path);
ccheung@5603 328 _st = *st;
duke@435 329 _meta_index = NULL;
duke@435 330 _resolved_entry = NULL;
ccheung@5603 331 _has_error = false;
iklam@7089 332 _throw_exception = throw_exception;
duke@435 333 }
duke@435 334
duke@435 335 bool LazyClassPathEntry::is_jar_file() {
duke@435 336 return ((_st.st_mode & S_IFREG) == S_IFREG);
duke@435 337 }
duke@435 338
ccheung@5603 339 ClassPathEntry* LazyClassPathEntry::resolve_entry(TRAPS) {
duke@435 340 if (_resolved_entry != NULL) {
duke@435 341 return (ClassPathEntry*) _resolved_entry;
duke@435 342 }
duke@435 343 ClassPathEntry* new_entry = NULL;
iklam@7089 344 new_entry = ClassLoader::create_class_path_entry(_path, &_st, false, _throw_exception, CHECK_NULL);
iklam@7089 345 if (!_throw_exception && new_entry == NULL) {
iklam@7089 346 assert(!HAS_PENDING_EXCEPTION, "must be");
iklam@7089 347 return NULL;
iklam@7089 348 }
duke@435 349 {
duke@435 350 ThreadCritical tc;
duke@435 351 if (_resolved_entry == NULL) {
duke@435 352 _resolved_entry = new_entry;
duke@435 353 return new_entry;
duke@435 354 }
duke@435 355 }
duke@435 356 assert(_resolved_entry != NULL, "bug in MT-safe resolution logic");
duke@435 357 delete new_entry;
duke@435 358 return (ClassPathEntry*) _resolved_entry;
duke@435 359 }
duke@435 360
ccheung@5603 361 ClassFileStream* LazyClassPathEntry::open_stream(const char* name, TRAPS) {
duke@435 362 if (_meta_index != NULL &&
duke@435 363 !_meta_index->may_contain(name)) {
duke@435 364 return NULL;
duke@435 365 }
ccheung@5603 366 if (_has_error) {
ccheung@5603 367 return NULL;
ccheung@5603 368 }
ccheung@5603 369 ClassPathEntry* cpe = resolve_entry(THREAD);
ccheung@5603 370 if (cpe == NULL) {
ccheung@5603 371 _has_error = true;
ccheung@5603 372 return NULL;
ccheung@5603 373 } else {
ccheung@5603 374 return cpe->open_stream(name, THREAD);
ccheung@5603 375 }
duke@435 376 }
duke@435 377
duke@435 378 bool LazyClassPathEntry::is_lazy() {
duke@435 379 return true;
duke@435 380 }
duke@435 381
iklam@7089 382 u1* LazyClassPathEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) {
iklam@7089 383 if (_has_error) {
iklam@7089 384 return NULL;
iklam@7089 385 }
iklam@7089 386 ClassPathEntry* cpe = resolve_entry(THREAD);
iklam@7089 387 if (cpe == NULL) {
iklam@7089 388 _has_error = true;
iklam@7089 389 return NULL;
iklam@7089 390 } else if (cpe->is_jar_file()) {
iklam@7089 391 return ((ClassPathZipEntry*)cpe)->open_entry(name, filesize, nul_terminate,THREAD);
iklam@7089 392 } else {
iklam@7089 393 ShouldNotReachHere();
iklam@7089 394 *filesize = 0;
iklam@7089 395 return NULL;
iklam@7089 396 }
iklam@7089 397 }
iklam@7089 398
duke@435 399 static void print_meta_index(LazyClassPathEntry* entry,
duke@435 400 GrowableArray<char*>& meta_packages) {
duke@435 401 tty->print("[Meta index for %s=", entry->name());
duke@435 402 for (int i = 0; i < meta_packages.length(); i++) {
duke@435 403 if (i > 0) tty->print(" ");
drchase@6680 404 tty->print("%s", meta_packages.at(i));
duke@435 405 }
duke@435 406 tty->print_cr("]");
duke@435 407 }
duke@435 408
iklam@7089 409 #if INCLUDE_CDS
iklam@7089 410 void ClassLoader::exit_with_path_failure(const char* error, const char* message) {
iklam@7089 411 assert(DumpSharedSpaces, "only called at dump time");
iklam@7089 412 tty->print_cr("Hint: enable -XX:+TraceClassPaths to diagnose the failure");
iklam@7089 413 vm_exit_during_initialization(error, message);
iklam@7089 414 }
iklam@7089 415 #endif
duke@435 416
ccheung@8184 417 void ClassLoader::trace_class_path(outputStream* out, const char* msg, const char* name) {
iklam@7089 418 if (!TraceClassPaths) {
iklam@7089 419 return;
iklam@7089 420 }
iklam@7089 421
iklam@7089 422 if (msg) {
ccheung@8184 423 out->print("%s", msg);
iklam@7089 424 }
iklam@7089 425 if (name) {
iklam@7089 426 if (strlen(name) < 256) {
ccheung@8184 427 out->print("%s", name);
iklam@7089 428 } else {
iklam@7089 429 // For very long paths, we need to print each character separately,
iklam@7089 430 // as print_cr() has a length limit
iklam@7089 431 while (name[0] != '\0') {
ccheung@8184 432 out->print("%c", name[0]);
iklam@7089 433 name++;
iklam@7089 434 }
iklam@7089 435 }
iklam@7089 436 }
iklam@7089 437 if (msg && msg[0] == '[') {
ccheung@8184 438 out->print_cr("]");
iklam@7089 439 } else {
ccheung@8184 440 out->cr();
iklam@7089 441 }
iklam@7089 442 }
iklam@7089 443
iklam@7089 444 void ClassLoader::setup_bootstrap_meta_index() {
duke@435 445 // Set up meta index which allows us to open boot jars lazily if
duke@435 446 // class data sharing is enabled
iklam@7089 447 const char* meta_index_path = Arguments::get_meta_index_path();
iklam@7089 448 const char* meta_index_dir = Arguments::get_meta_index_dir();
iklam@7089 449 setup_meta_index(meta_index_path, meta_index_dir, 0);
iklam@7089 450 }
iklam@7089 451
iklam@7089 452 void ClassLoader::setup_meta_index(const char* meta_index_path, const char* meta_index_dir, int start_index) {
duke@435 453 const char* known_version = "% VERSION 2";
duke@435 454 FILE* file = fopen(meta_index_path, "r");
duke@435 455 int line_no = 0;
iklam@7089 456 #if INCLUDE_CDS
iklam@7089 457 if (DumpSharedSpaces) {
iklam@7089 458 if (file != NULL) {
iklam@7089 459 _shared_paths_misc_info->add_required_file(meta_index_path);
iklam@7089 460 } else {
iklam@7089 461 _shared_paths_misc_info->add_nonexist_path(meta_index_path);
iklam@7089 462 }
iklam@7089 463 }
iklam@7089 464 #endif
duke@435 465 if (file != NULL) {
duke@435 466 ResourceMark rm;
duke@435 467 LazyClassPathEntry* cur_entry = NULL;
duke@435 468 GrowableArray<char*> boot_class_path_packages(10);
duke@435 469 char package_name[256];
duke@435 470 bool skipCurrentJar = false;
duke@435 471 while (fgets(package_name, sizeof(package_name), file) != NULL) {
duke@435 472 ++line_no;
duke@435 473 // Remove trailing newline
duke@435 474 package_name[strlen(package_name) - 1] = '\0';
duke@435 475 switch(package_name[0]) {
duke@435 476 case '%':
duke@435 477 {
duke@435 478 if ((line_no == 1) && (strcmp(package_name, known_version) != 0)) {
duke@435 479 if (TraceClassLoading && Verbose) {
duke@435 480 tty->print("[Unsupported meta index version]");
duke@435 481 }
duke@435 482 fclose(file);
duke@435 483 return;
duke@435 484 }
duke@435 485 }
duke@435 486
duke@435 487 // These directives indicate jar files which contain only
duke@435 488 // classes, only non-classfile resources, or a combination of
duke@435 489 // the two. See src/share/classes/sun/misc/MetaIndex.java and
duke@435 490 // make/tools/MetaIndex/BuildMetaIndex.java in the J2SE
duke@435 491 // workspace.
duke@435 492 case '#':
duke@435 493 case '!':
duke@435 494 case '@':
duke@435 495 {
duke@435 496 // Hand off current packages to current lazy entry (if any)
duke@435 497 if ((cur_entry != NULL) &&
duke@435 498 (boot_class_path_packages.length() > 0)) {
iklam@7089 499 if ((TraceClassLoading || TraceClassPaths) && Verbose) {
duke@435 500 print_meta_index(cur_entry, boot_class_path_packages);
duke@435 501 }
duke@435 502 MetaIndex* index = new MetaIndex(boot_class_path_packages.adr_at(0),
duke@435 503 boot_class_path_packages.length());
duke@435 504 cur_entry->set_meta_index(index);
duke@435 505 }
duke@435 506 cur_entry = NULL;
duke@435 507 boot_class_path_packages.clear();
duke@435 508
duke@435 509 // Find lazy entry corresponding to this jar file
iklam@7089 510 int count = 0;
iklam@7089 511 for (ClassPathEntry* entry = _first_entry; entry != NULL; entry = entry->next(), count++) {
iklam@7089 512 if (count >= start_index &&
iklam@7089 513 entry->is_lazy() &&
duke@435 514 string_starts_with(entry->name(), meta_index_dir) &&
duke@435 515 string_ends_with(entry->name(), &package_name[2])) {
duke@435 516 cur_entry = (LazyClassPathEntry*) entry;
duke@435 517 break;
duke@435 518 }
duke@435 519 }
duke@435 520
duke@435 521 // If the first character is '@', it indicates the following jar
duke@435 522 // file is a resource only jar file in which case, we should skip
duke@435 523 // reading the subsequent entries since the resource loading is
duke@435 524 // totally handled by J2SE side.
duke@435 525 if (package_name[0] == '@') {
duke@435 526 if (cur_entry != NULL) {
duke@435 527 cur_entry->set_meta_index(new MetaIndex(NULL, 0));
duke@435 528 }
duke@435 529 cur_entry = NULL;
duke@435 530 skipCurrentJar = true;
duke@435 531 } else {
duke@435 532 skipCurrentJar = false;
duke@435 533 }
duke@435 534
duke@435 535 break;
duke@435 536 }
duke@435 537
duke@435 538 default:
duke@435 539 {
duke@435 540 if (!skipCurrentJar && cur_entry != NULL) {
duke@435 541 char* new_name = strdup(package_name);
duke@435 542 boot_class_path_packages.append(new_name);
duke@435 543 }
duke@435 544 }
duke@435 545 }
duke@435 546 }
duke@435 547 // Hand off current packages to current lazy entry (if any)
duke@435 548 if ((cur_entry != NULL) &&
duke@435 549 (boot_class_path_packages.length() > 0)) {
iklam@7089 550 if ((TraceClassLoading || TraceClassPaths) && Verbose) {
duke@435 551 print_meta_index(cur_entry, boot_class_path_packages);
duke@435 552 }
duke@435 553 MetaIndex* index = new MetaIndex(boot_class_path_packages.adr_at(0),
duke@435 554 boot_class_path_packages.length());
duke@435 555 cur_entry->set_meta_index(index);
duke@435 556 }
duke@435 557 fclose(file);
duke@435 558 }
duke@435 559 }
duke@435 560
iklam@7089 561 #if INCLUDE_CDS
iklam@7089 562 void ClassLoader::check_shared_classpath(const char *path) {
iklam@7089 563 if (strcmp(path, "") == 0) {
iklam@7089 564 exit_with_path_failure("Cannot have empty path in archived classpaths", NULL);
iklam@7089 565 }
iklam@7089 566
iklam@7089 567 struct stat st;
iklam@7089 568 if (os::stat(path, &st) == 0) {
iklam@7089 569 if ((st.st_mode & S_IFREG) != S_IFREG) { // is directory
iklam@7089 570 if (!os::dir_is_empty(path)) {
iklam@7089 571 tty->print_cr("Error: non-empty directory '%s'", path);
iklam@7089 572 exit_with_path_failure("CDS allows only empty directories in archived classpaths", NULL);
iklam@7089 573 }
iklam@7089 574 }
iklam@7089 575 }
iklam@7089 576 }
iklam@7089 577 #endif
iklam@7089 578
duke@435 579 void ClassLoader::setup_bootstrap_search_path() {
duke@435 580 assert(_first_entry == NULL, "should not setup bootstrap class search path twice");
iklam@7090 581 const char* sys_class_path = Arguments::get_sysclasspath();
iklam@7090 582 if (PrintSharedArchiveAndExit) {
iklam@7090 583 // Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily
iklam@7090 584 // the same as the bootcp of the shared archive.
iklam@7090 585 } else {
ccheung@8184 586 trace_class_path(tty, "[Bootstrap loader class path=", sys_class_path);
duke@435 587 }
iklam@7089 588 #if INCLUDE_CDS
iklam@7089 589 if (DumpSharedSpaces) {
iklam@7090 590 _shared_paths_misc_info->add_boot_classpath(sys_class_path);
iklam@7089 591 }
iklam@7089 592 #endif
iklam@7089 593 setup_search_path(sys_class_path);
iklam@7089 594 }
duke@435 595
iklam@7089 596 #if INCLUDE_CDS
iklam@7089 597 int ClassLoader::get_shared_paths_misc_info_size() {
iklam@7089 598 return _shared_paths_misc_info->get_used_bytes();
iklam@7089 599 }
iklam@7089 600
iklam@7089 601 void* ClassLoader::get_shared_paths_misc_info() {
iklam@7089 602 return _shared_paths_misc_info->buffer();
iklam@7089 603 }
iklam@7089 604
iklam@7089 605 bool ClassLoader::check_shared_paths_misc_info(void *buf, int size) {
iklam@7089 606 SharedPathsMiscInfo* checker = SharedClassUtil::allocate_shared_paths_misc_info((char*)buf, size);
iklam@7089 607 bool result = checker->check();
iklam@7089 608 delete checker;
iklam@7089 609 return result;
iklam@7089 610 }
iklam@7089 611 #endif
iklam@7089 612
iklam@7322 613 void ClassLoader::setup_search_path(const char *class_path, bool canonicalize) {
iklam@7089 614 int offset = 0;
iklam@7089 615 int len = (int)strlen(class_path);
duke@435 616 int end = 0;
duke@435 617
duke@435 618 // Iterate over class path entries
duke@435 619 for (int start = 0; start < len; start = end) {
iklam@7089 620 while (class_path[end] && class_path[end] != os::path_separator()[0]) {
duke@435 621 end++;
duke@435 622 }
iklam@7089 623 EXCEPTION_MARK;
iklam@7089 624 ResourceMark rm(THREAD);
iklam@7089 625 char* path = NEW_RESOURCE_ARRAY(char, end - start + 1);
iklam@7089 626 strncpy(path, &class_path[start], end - start);
iklam@7089 627 path[end - start] = '\0';
iklam@7322 628 if (canonicalize) {
iklam@7322 629 char* canonical_path = NEW_RESOURCE_ARRAY(char, JVM_MAXPATHLEN + 1);
iklam@7322 630 if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
iklam@7322 631 path = canonical_path;
iklam@7322 632 }
iklam@7322 633 }
iklam@7322 634 update_class_path_entry_list(path, /*check_for_duplicates=*/canonicalize);
iklam@7089 635 #if INCLUDE_CDS
iklam@7089 636 if (DumpSharedSpaces) {
iklam@7089 637 check_shared_classpath(path);
iklam@7089 638 }
iklam@7089 639 #endif
iklam@7089 640 while (class_path[end] == os::path_separator()[0]) {
duke@435 641 end++;
duke@435 642 }
duke@435 643 }
duke@435 644 }
duke@435 645
iklam@7090 646 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st,
iklam@7089 647 bool lazy, bool throw_exception, TRAPS) {
duke@435 648 JavaThread* thread = JavaThread::current();
duke@435 649 if (lazy) {
iklam@7089 650 return new LazyClassPathEntry(path, st, throw_exception);
duke@435 651 }
ccheung@5603 652 ClassPathEntry* new_entry = NULL;
ccheung@5603 653 if ((st->st_mode & S_IFREG) == S_IFREG) {
duke@435 654 // Regular file, should be a zip file
duke@435 655 // Canonicalized filename
duke@435 656 char canonical_path[JVM_MAXPATHLEN];
duke@435 657 if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
duke@435 658 // This matches the classic VM
iklam@7089 659 if (throw_exception) {
iklam@7089 660 THROW_MSG_(vmSymbols::java_io_IOException(), "Bad pathname", NULL);
iklam@7089 661 } else {
iklam@7089 662 return NULL;
iklam@7089 663 }
duke@435 664 }
duke@435 665 char* error_msg = NULL;
duke@435 666 jzfile* zip;
duke@435 667 {
duke@435 668 // enable call to C land
duke@435 669 ThreadToNativeFromVM ttn(thread);
duke@435 670 HandleMark hm(thread);
duke@435 671 zip = (*ZipOpen)(canonical_path, &error_msg);
duke@435 672 }
duke@435 673 if (zip != NULL && error_msg == NULL) {
ccheung@5603 674 new_entry = new ClassPathZipEntry(zip, path);
iklam@7089 675 if (TraceClassLoading || TraceClassPaths) {
duke@435 676 tty->print_cr("[Opened %s]", path);
duke@435 677 }
duke@435 678 } else {
duke@435 679 ResourceMark rm(thread);
duke@435 680 char *msg;
duke@435 681 if (error_msg == NULL) {
duke@435 682 msg = NEW_RESOURCE_ARRAY(char, strlen(path) + 128); ;
duke@435 683 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
duke@435 684 } else {
duke@435 685 int len = (int)(strlen(path) + strlen(error_msg) + 128);
duke@435 686 msg = NEW_RESOURCE_ARRAY(char, len); ;
duke@435 687 jio_snprintf(msg, len - 1, "error in opening JAR file <%s> %s", error_msg, path);
duke@435 688 }
iklam@7089 689 if (throw_exception) {
iklam@7089 690 THROW_MSG_(vmSymbols::java_lang_ClassNotFoundException(), msg, NULL);
iklam@7089 691 } else {
iklam@7089 692 return NULL;
iklam@7089 693 }
duke@435 694 }
duke@435 695 } else {
duke@435 696 // Directory
ccheung@5603 697 new_entry = new ClassPathDirEntry(path);
iklam@7089 698 if (TraceClassLoading || TraceClassPaths) {
duke@435 699 tty->print_cr("[Path %s]", path);
duke@435 700 }
duke@435 701 }
ccheung@5603 702 return new_entry;
duke@435 703 }
duke@435 704
duke@435 705
duke@435 706 // Create a class path zip entry for a given path (return NULL if not found
duke@435 707 // or zip/JAR file cannot be opened)
duke@435 708 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path) {
duke@435 709 // check for a regular file
duke@435 710 struct stat st;
duke@435 711 if (os::stat(path, &st) == 0) {
duke@435 712 if ((st.st_mode & S_IFREG) == S_IFREG) {
duke@435 713 char canonical_path[JVM_MAXPATHLEN];
iklam@7090 714 if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
duke@435 715 char* error_msg = NULL;
duke@435 716 jzfile* zip;
duke@435 717 {
duke@435 718 // enable call to C land
duke@435 719 JavaThread* thread = JavaThread::current();
duke@435 720 ThreadToNativeFromVM ttn(thread);
duke@435 721 HandleMark hm(thread);
duke@435 722 zip = (*ZipOpen)(canonical_path, &error_msg);
duke@435 723 }
duke@435 724 if (zip != NULL && error_msg == NULL) {
duke@435 725 // create using canonical path
duke@435 726 return new ClassPathZipEntry(zip, canonical_path);
duke@435 727 }
duke@435 728 }
duke@435 729 }
duke@435 730 }
duke@435 731 return NULL;
duke@435 732 }
duke@435 733
duke@435 734 // returns true if entry already on class path
duke@435 735 bool ClassLoader::contains_entry(ClassPathEntry *entry) {
duke@435 736 ClassPathEntry* e = _first_entry;
duke@435 737 while (e != NULL) {
duke@435 738 // assume zip entries have been canonicalized
duke@435 739 if (strcmp(entry->name(), e->name()) == 0) {
duke@435 740 return true;
duke@435 741 }
duke@435 742 e = e->next();
duke@435 743 }
duke@435 744 return false;
duke@435 745 }
duke@435 746
duke@435 747 void ClassLoader::add_to_list(ClassPathEntry *new_entry) {
duke@435 748 if (new_entry != NULL) {
duke@435 749 if (_last_entry == NULL) {
duke@435 750 _first_entry = _last_entry = new_entry;
duke@435 751 } else {
duke@435 752 _last_entry->set_next(new_entry);
duke@435 753 _last_entry = new_entry;
duke@435 754 }
duke@435 755 }
iklam@7089 756 _num_entries ++;
duke@435 757 }
duke@435 758
iklam@7089 759 // Returns true IFF the file/dir exists and the entry was successfully created.
iklam@7090 760 bool ClassLoader::update_class_path_entry_list(const char *path,
iklam@7089 761 bool check_for_duplicates,
iklam@7089 762 bool throw_exception) {
duke@435 763 struct stat st;
ccheung@5603 764 if (os::stat(path, &st) == 0) {
duke@435 765 // File or directory found
duke@435 766 ClassPathEntry* new_entry = NULL;
ccheung@5603 767 Thread* THREAD = Thread::current();
iklam@7089 768 new_entry = create_class_path_entry(path, &st, LazyBootClassLoader, throw_exception, CHECK_(false));
iklam@7089 769 if (new_entry == NULL) {
iklam@7089 770 return false;
iklam@7089 771 }
duke@435 772 // The kernel VM adds dynamically to the end of the classloader path and
duke@435 773 // doesn't reorder the bootclasspath which would break java.lang.Package
duke@435 774 // (see PackageInfo).
duke@435 775 // Add new entry to linked list
duke@435 776 if (!check_for_duplicates || !contains_entry(new_entry)) {
iklam@7089 777 ClassLoaderExt::add_class_path_entry(path, check_for_duplicates, new_entry);
duke@435 778 }
iklam@7089 779 return true;
iklam@7089 780 } else {
iklam@7089 781 #if INCLUDE_CDS
iklam@7089 782 if (DumpSharedSpaces) {
iklam@7089 783 _shared_paths_misc_info->add_nonexist_path(path);
iklam@7089 784 }
iklam@7090 785 #endif
iklam@7089 786 return false;
duke@435 787 }
duke@435 788 }
duke@435 789
duke@435 790 void ClassLoader::print_bootclasspath() {
duke@435 791 ClassPathEntry* e = _first_entry;
duke@435 792 tty->print("[bootclasspath= ");
duke@435 793 while (e != NULL) {
duke@435 794 tty->print("%s ;", e->name());
duke@435 795 e = e->next();
duke@435 796 }
duke@435 797 tty->print_cr("]");
duke@435 798 }
duke@435 799
duke@435 800 void ClassLoader::load_zip_library() {
duke@435 801 assert(ZipOpen == NULL, "should not load zip library twice");
duke@435 802 // First make sure native library is loaded
duke@435 803 os::native_java_library();
duke@435 804 // Load zip library
duke@435 805 char path[JVM_MAXPATHLEN];
duke@435 806 char ebuf[1024];
bpittore@4261 807 void* handle = NULL;
bpittore@4261 808 if (os::dll_build_name(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
bpittore@4261 809 handle = os::dll_load(path, ebuf, sizeof ebuf);
bpittore@4261 810 }
duke@435 811 if (handle == NULL) {
duke@435 812 vm_exit_during_initialization("Unable to load ZIP library", path);
duke@435 813 }
duke@435 814 // Lookup zip entry points
ikrylov@2322 815 ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, os::dll_lookup(handle, "ZIP_Open"));
ikrylov@2322 816 ZipClose = CAST_TO_FN_PTR(ZipClose_t, os::dll_lookup(handle, "ZIP_Close"));
ikrylov@2322 817 FindEntry = CAST_TO_FN_PTR(FindEntry_t, os::dll_lookup(handle, "ZIP_FindEntry"));
ikrylov@2322 818 ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, os::dll_lookup(handle, "ZIP_ReadEntry"));
ikrylov@2322 819 ReadMappedEntry = CAST_TO_FN_PTR(ReadMappedEntry_t, os::dll_lookup(handle, "ZIP_ReadMappedEntry"));
ikrylov@2322 820 GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, os::dll_lookup(handle, "ZIP_GetNextEntry"));
jiangli@6868 821 Crc32 = CAST_TO_FN_PTR(Crc32_t, os::dll_lookup(handle, "ZIP_CRC32"));
duke@435 822
duke@435 823 // ZIP_Close is not exported on Windows in JDK5.0 so don't abort if ZIP_Close is NULL
jiangli@6868 824 if (ZipOpen == NULL || FindEntry == NULL || ReadEntry == NULL ||
jiangli@6868 825 GetNextEntry == NULL || Crc32 == NULL) {
duke@435 826 vm_exit_during_initialization("Corrupted ZIP library", path);
duke@435 827 }
duke@435 828
duke@435 829 // Lookup canonicalize entry in libjava.dll
duke@435 830 void *javalib_handle = os::native_java_library();
ikrylov@2322 831 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, os::dll_lookup(javalib_handle, "Canonicalize"));
duke@435 832 // This lookup only works on 1.3. Do not check for non-null here
duke@435 833 }
duke@435 834
jiangli@6868 835 int ClassLoader::crc32(int crc, const char* buf, int len) {
jiangli@6868 836 assert(Crc32 != NULL, "ZIP_CRC32 is not found");
jiangli@6868 837 return (*Crc32)(crc, (const jbyte*)buf, len);
jiangli@6868 838 }
jiangli@6868 839
duke@435 840 // PackageInfo data exists in order to support the java.lang.Package
duke@435 841 // class. A Package object provides information about a java package
duke@435 842 // (version, vendor, etc.) which originates in the manifest of the jar
duke@435 843 // file supplying the package. For application classes, the ClassLoader
duke@435 844 // object takes care of this.
duke@435 845
duke@435 846 // For system (boot) classes, the Java code in the Package class needs
duke@435 847 // to be able to identify which source jar file contained the boot
duke@435 848 // class, so that it can extract the manifest from it. This table
duke@435 849 // identifies java packages with jar files in the boot classpath.
duke@435 850
duke@435 851 // Because the boot classpath cannot change, the classpath index is
duke@435 852 // sufficient to identify the source jar file or directory. (Since
duke@435 853 // directories have no manifests, the directory name is not required,
duke@435 854 // but is available.)
duke@435 855
duke@435 856 // When using sharing -- the pathnames of entries in the boot classpath
duke@435 857 // may not be the same at runtime as they were when the archive was
duke@435 858 // created (NFS, Samba, etc.). The actual files and directories named
duke@435 859 // in the classpath must be the same files, in the same order, even
duke@435 860 // though the exact name is not the same.
duke@435 861
zgu@3900 862 class PackageInfo: public BasicHashtableEntry<mtClass> {
duke@435 863 public:
duke@435 864 const char* _pkgname; // Package name
duke@435 865 int _classpath_index; // Index of directory or JAR file loaded from
duke@435 866
duke@435 867 PackageInfo* next() {
zgu@3900 868 return (PackageInfo*)BasicHashtableEntry<mtClass>::next();
duke@435 869 }
duke@435 870
duke@435 871 const char* pkgname() { return _pkgname; }
duke@435 872 void set_pkgname(char* pkgname) { _pkgname = pkgname; }
duke@435 873
duke@435 874 const char* filename() {
duke@435 875 return ClassLoader::classpath_entry(_classpath_index)->name();
duke@435 876 }
duke@435 877
duke@435 878 void set_index(int index) {
duke@435 879 _classpath_index = index;
duke@435 880 }
duke@435 881 };
duke@435 882
duke@435 883
zgu@3900 884 class PackageHashtable : public BasicHashtable<mtClass> {
duke@435 885 private:
duke@435 886 inline unsigned int compute_hash(const char *s, int n) {
duke@435 887 unsigned int val = 0;
duke@435 888 while (--n >= 0) {
duke@435 889 val = *s++ + 31 * val;
duke@435 890 }
duke@435 891 return val;
duke@435 892 }
duke@435 893
duke@435 894 PackageInfo* bucket(int index) {
zgu@3900 895 return (PackageInfo*)BasicHashtable<mtClass>::bucket(index);
duke@435 896 }
duke@435 897
duke@435 898 PackageInfo* get_entry(int index, unsigned int hash,
duke@435 899 const char* pkgname, size_t n) {
duke@435 900 for (PackageInfo* pp = bucket(index); pp != NULL; pp = pp->next()) {
duke@435 901 if (pp->hash() == hash &&
duke@435 902 strncmp(pkgname, pp->pkgname(), n) == 0 &&
duke@435 903 pp->pkgname()[n] == '\0') {
duke@435 904 return pp;
duke@435 905 }
duke@435 906 }
duke@435 907 return NULL;
duke@435 908 }
duke@435 909
duke@435 910 public:
duke@435 911 PackageHashtable(int table_size)
zgu@3900 912 : BasicHashtable<mtClass>(table_size, sizeof(PackageInfo)) {}
duke@435 913
zgu@3900 914 PackageHashtable(int table_size, HashtableBucket<mtClass>* t, int number_of_entries)
zgu@3900 915 : BasicHashtable<mtClass>(table_size, sizeof(PackageInfo), t, number_of_entries) {}
duke@435 916
duke@435 917 PackageInfo* get_entry(const char* pkgname, int n) {
duke@435 918 unsigned int hash = compute_hash(pkgname, n);
duke@435 919 return get_entry(hash_to_index(hash), hash, pkgname, n);
duke@435 920 }
duke@435 921
duke@435 922 PackageInfo* new_entry(char* pkgname, int n) {
duke@435 923 unsigned int hash = compute_hash(pkgname, n);
duke@435 924 PackageInfo* pp;
zgu@3900 925 pp = (PackageInfo*)BasicHashtable<mtClass>::new_entry(hash);
duke@435 926 pp->set_pkgname(pkgname);
duke@435 927 return pp;
duke@435 928 }
duke@435 929
duke@435 930 void add_entry(PackageInfo* pp) {
duke@435 931 int index = hash_to_index(pp->hash());
zgu@3900 932 BasicHashtable<mtClass>::add_entry(index, pp);
duke@435 933 }
duke@435 934
duke@435 935 void copy_pkgnames(const char** packages) {
duke@435 936 int n = 0;
duke@435 937 for (int i = 0; i < table_size(); ++i) {
duke@435 938 for (PackageInfo* pp = bucket(i); pp != NULL; pp = pp->next()) {
duke@435 939 packages[n++] = pp->pkgname();
duke@435 940 }
duke@435 941 }
duke@435 942 assert(n == number_of_entries(), "just checking");
duke@435 943 }
duke@435 944
iklam@7089 945 CDS_ONLY(void copy_table(char** top, char* end, PackageHashtable* table);)
duke@435 946 };
duke@435 947
iklam@7089 948 #if INCLUDE_CDS
duke@435 949 void PackageHashtable::copy_table(char** top, char* end,
duke@435 950 PackageHashtable* table) {
duke@435 951 // Copy (relocate) the table to the shared space.
zgu@3900 952 BasicHashtable<mtClass>::copy_table(top, end);
duke@435 953
duke@435 954 // Calculate the space needed for the package name strings.
duke@435 955 int i;
iklam@7089 956 intptr_t* tableSize = (intptr_t*)(*top);
iklam@7089 957 *top += sizeof(intptr_t); // For table size
iklam@7089 958 char* tableStart = *top;
duke@435 959
duke@435 960 for (i = 0; i < table_size(); ++i) {
duke@435 961 for (PackageInfo* pp = table->bucket(i);
duke@435 962 pp != NULL;
duke@435 963 pp = pp->next()) {
duke@435 964 int n1 = (int)(strlen(pp->pkgname()) + 1);
iklam@7089 965 if (*top + n1 >= end) {
iklam@7089 966 report_out_of_shared_space(SharedMiscData);
iklam@7089 967 }
duke@435 968 pp->set_pkgname((char*)memcpy(*top, pp->pkgname(), n1));
duke@435 969 *top += n1;
duke@435 970 }
duke@435 971 }
duke@435 972 *top = (char*)align_size_up((intptr_t)*top, sizeof(HeapWord));
iklam@7089 973 if (*top >= end) {
iklam@7089 974 report_out_of_shared_space(SharedMiscData);
iklam@7089 975 }
iklam@7089 976
iklam@7089 977 // Write table size
iklam@7089 978 intptr_t len = *top - (char*)tableStart;
iklam@7089 979 *tableSize = len;
duke@435 980 }
duke@435 981
duke@435 982
duke@435 983 void ClassLoader::copy_package_info_buckets(char** top, char* end) {
duke@435 984 _package_hash_table->copy_buckets(top, end);
duke@435 985 }
duke@435 986
duke@435 987 void ClassLoader::copy_package_info_table(char** top, char* end) {
duke@435 988 _package_hash_table->copy_table(top, end, _package_hash_table);
duke@435 989 }
iklam@7089 990 #endif
duke@435 991
duke@435 992 PackageInfo* ClassLoader::lookup_package(const char *pkgname) {
duke@435 993 const char *cp = strrchr(pkgname, '/');
duke@435 994 if (cp != NULL) {
duke@435 995 // Package prefix found
duke@435 996 int n = cp - pkgname + 1;
duke@435 997 return _package_hash_table->get_entry(pkgname, n);
duke@435 998 }
duke@435 999 return NULL;
duke@435 1000 }
duke@435 1001
duke@435 1002
duke@435 1003 bool ClassLoader::add_package(const char *pkgname, int classpath_index, TRAPS) {
duke@435 1004 assert(pkgname != NULL, "just checking");
duke@435 1005 // Bootstrap loader no longer holds system loader lock obj serializing
duke@435 1006 // load_instance_class and thereby add_package
duke@435 1007 {
duke@435 1008 MutexLocker ml(PackageTable_lock, THREAD);
duke@435 1009 // First check for previously loaded entry
duke@435 1010 PackageInfo* pp = lookup_package(pkgname);
duke@435 1011 if (pp != NULL) {
duke@435 1012 // Existing entry found, check source of package
duke@435 1013 pp->set_index(classpath_index);
duke@435 1014 return true;
duke@435 1015 }
duke@435 1016
duke@435 1017 const char *cp = strrchr(pkgname, '/');
duke@435 1018 if (cp != NULL) {
duke@435 1019 // Package prefix found
duke@435 1020 int n = cp - pkgname + 1;
duke@435 1021
zgu@3900 1022 char* new_pkgname = NEW_C_HEAP_ARRAY(char, n + 1, mtClass);
duke@435 1023 if (new_pkgname == NULL) {
duke@435 1024 return false;
duke@435 1025 }
duke@435 1026
duke@435 1027 memcpy(new_pkgname, pkgname, n);
duke@435 1028 new_pkgname[n] = '\0';
duke@435 1029 pp = _package_hash_table->new_entry(new_pkgname, n);
duke@435 1030 pp->set_index(classpath_index);
duke@435 1031
duke@435 1032 // Insert into hash table
duke@435 1033 _package_hash_table->add_entry(pp);
duke@435 1034 }
duke@435 1035 return true;
duke@435 1036 }
duke@435 1037 }
duke@435 1038
duke@435 1039
duke@435 1040 oop ClassLoader::get_system_package(const char* name, TRAPS) {
duke@435 1041 PackageInfo* pp;
duke@435 1042 {
duke@435 1043 MutexLocker ml(PackageTable_lock, THREAD);
duke@435 1044 pp = lookup_package(name);
duke@435 1045 }
duke@435 1046 if (pp == NULL) {
duke@435 1047 return NULL;
duke@435 1048 } else {
duke@435 1049 Handle p = java_lang_String::create_from_str(pp->filename(), THREAD);
duke@435 1050 return p();
duke@435 1051 }
duke@435 1052 }
duke@435 1053
duke@435 1054
duke@435 1055 objArrayOop ClassLoader::get_system_packages(TRAPS) {
duke@435 1056 ResourceMark rm(THREAD);
duke@435 1057 int nof_entries;
duke@435 1058 const char** packages;
duke@435 1059 {
duke@435 1060 MutexLocker ml(PackageTable_lock, THREAD);
duke@435 1061 // Allocate resource char* array containing package names
duke@435 1062 nof_entries = _package_hash_table->number_of_entries();
duke@435 1063 if ((packages = NEW_RESOURCE_ARRAY(const char*, nof_entries)) == NULL) {
duke@435 1064 return NULL;
duke@435 1065 }
duke@435 1066 _package_hash_table->copy_pkgnames(packages);
duke@435 1067 }
duke@435 1068 // Allocate objArray and fill with java.lang.String
never@1577 1069 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(),
duke@435 1070 nof_entries, CHECK_0);
duke@435 1071 objArrayHandle result(THREAD, r);
duke@435 1072 for (int i = 0; i < nof_entries; i++) {
duke@435 1073 Handle str = java_lang_String::create_from_str(packages[i], CHECK_0);
duke@435 1074 result->obj_at_put(i, str());
duke@435 1075 }
duke@435 1076
duke@435 1077 return result();
duke@435 1078 }
duke@435 1079
duke@435 1080
coleenp@2497 1081 instanceKlassHandle ClassLoader::load_classfile(Symbol* h_name, TRAPS) {
duke@435 1082 ResourceMark rm(THREAD);
iklam@7089 1083 const char* class_name = h_name->as_C_string();
iklam@7089 1084 EventMark m("loading class %s", class_name);
duke@435 1085 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion);
duke@435 1086
duke@435 1087 stringStream st;
duke@435 1088 // st.print() uses too much stack space while handling a StackOverflowError
duke@435 1089 // st.print("%s.class", h_name->as_utf8());
duke@435 1090 st.print_raw(h_name->as_utf8());
duke@435 1091 st.print_raw(".class");
iklam@7089 1092 const char* file_name = st.as_string();
iklam@7089 1093 ClassLoaderExt::Context context(class_name, file_name, THREAD);
duke@435 1094
duke@435 1095 // Lookup stream for parsing .class file
duke@435 1096 ClassFileStream* stream = NULL;
duke@435 1097 int classpath_index = 0;
iklam@7089 1098 ClassPathEntry* e = NULL;
iklam@7089 1099 instanceKlassHandle h;
duke@435 1100 {
mchung@1310 1101 PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(),
mchung@1310 1102 ((JavaThread*) THREAD)->get_thread_stat()->perf_timers_addr(),
mchung@1310 1103 PerfClassTraceTime::CLASS_LOAD);
iklam@7089 1104 e = _first_entry;
duke@435 1105 while (e != NULL) {
iklam@7089 1106 stream = e->open_stream(file_name, CHECK_NULL);
iklam@7089 1107 if (!context.check(stream, classpath_index)) {
iklam@7089 1108 return h; // NULL
iklam@7089 1109 }
duke@435 1110 if (stream != NULL) {
duke@435 1111 break;
duke@435 1112 }
duke@435 1113 e = e->next();
duke@435 1114 ++classpath_index;
duke@435 1115 }
duke@435 1116 }
duke@435 1117
duke@435 1118 if (stream != NULL) {
duke@435 1119 // class file found, parse it
duke@435 1120 ClassFileParser parser(stream);
coleenp@4304 1121 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
duke@435 1122 Handle protection_domain;
coleenp@2497 1123 TempNewSymbol parsed_name = NULL;
duke@435 1124 instanceKlassHandle result = parser.parseClassFile(h_name,
coleenp@4304 1125 loader_data,
duke@435 1126 protection_domain,
duke@435 1127 parsed_name,
iklam@7089 1128 context.should_verify(classpath_index),
iklam@7089 1129 THREAD);
iklam@7089 1130 if (HAS_PENDING_EXCEPTION) {
iklam@7089 1131 ResourceMark rm;
iklam@7089 1132 if (DumpSharedSpaces) {
iklam@7089 1133 tty->print_cr("Preload Error: Failed to load %s", class_name);
iklam@7089 1134 }
iklam@7089 1135 return h;
iklam@7089 1136 }
apetushkov@9858 1137
apetushkov@9858 1138 #if INCLUDE_JFR
apetushkov@9858 1139 {
apetushkov@9858 1140 InstanceKlass* ik = result();
apetushkov@9858 1141 ON_KLASS_CREATION(ik, parser, THREAD);
apetushkov@9858 1142 result = instanceKlassHandle(ik);
apetushkov@9858 1143 }
apetushkov@9858 1144 #endif
apetushkov@9858 1145
iklam@7089 1146 h = context.record_result(classpath_index, e, result, THREAD);
iklam@7089 1147 } else {
iklam@7089 1148 if (DumpSharedSpaces) {
jiangli@7363 1149 tty->print_cr("Preload Warning: Cannot find %s", class_name);
duke@435 1150 }
duke@435 1151 }
duke@435 1152
duke@435 1153 return h;
duke@435 1154 }
duke@435 1155
duke@435 1156
zgu@3900 1157 void ClassLoader::create_package_info_table(HashtableBucket<mtClass> *t, int length,
duke@435 1158 int number_of_entries) {
duke@435 1159 assert(_package_hash_table == NULL, "One package info table allowed.");
zgu@3900 1160 assert(length == package_hash_table_size * sizeof(HashtableBucket<mtClass>),
duke@435 1161 "bad shared package info size.");
duke@435 1162 _package_hash_table = new PackageHashtable(package_hash_table_size, t,
duke@435 1163 number_of_entries);
duke@435 1164 }
duke@435 1165
duke@435 1166
duke@435 1167 void ClassLoader::create_package_info_table() {
duke@435 1168 assert(_package_hash_table == NULL, "shouldn't have one yet");
duke@435 1169 _package_hash_table = new PackageHashtable(package_hash_table_size);
duke@435 1170 }
duke@435 1171
duke@435 1172
duke@435 1173 // Initialize the class loader's access to methods in libzip. Parse and
duke@435 1174 // process the boot classpath into a list ClassPathEntry objects. Once
duke@435 1175 // this list has been created, it must not change order (see class PackageInfo)
duke@435 1176 // it can be appended to and is by jvmti and the kernel vm.
duke@435 1177
duke@435 1178 void ClassLoader::initialize() {
duke@435 1179 assert(_package_hash_table == NULL, "should have been initialized by now.");
duke@435 1180 EXCEPTION_MARK;
duke@435 1181
duke@435 1182 if (UsePerfData) {
duke@435 1183 // jvmstat performance counters
duke@435 1184 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
duke@435 1185 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
mchung@1310 1186 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
duke@435 1187 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
mchung@1310 1188 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
duke@435 1189 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
mchung@1310 1190 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
duke@435 1191 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
duke@435 1192 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
mchung@1310 1193 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
mchung@1310 1194
mchung@1310 1195 NEWPERFTICKCOUNTER(_perf_class_parse_time, SUN_CLS, "parseClassTime");
mchung@1310 1196 NEWPERFTICKCOUNTER(_perf_class_parse_selftime, SUN_CLS, "parseClassTime.self");
mchung@1310 1197 NEWPERFTICKCOUNTER(_perf_sys_class_lookup_time, SUN_CLS, "lookupSysClassTime");
mchung@1310 1198 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
mchung@1310 1199 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
mchung@1310 1200 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
mchung@1310 1201 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
mchung@1310 1202 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
mchung@1310 1203 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
mchung@1310 1204 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
mchung@1310 1205 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
mchung@1310 1206 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
mchung@1310 1207 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
mchung@1310 1208
duke@435 1209
duke@435 1210 // The following performance counters are added for measuring the impact
duke@435 1211 // of the bug fix of 6365597. They are mainly focused on finding out
duke@435 1212 // the behavior of system & user-defined classloader lock, whether
duke@435 1213 // ClassLoader.loadClass/findClass is being called synchronized or not.
duke@435 1214 // Also two additional counters are created to see whether 'UnsyncloadClass'
duke@435 1215 // flag is being set or not and how many times load_instance_class call
duke@435 1216 // fails with linkageError etc.
duke@435 1217 NEWPERFEVENTCOUNTER(_sync_systemLoaderLockContentionRate, SUN_CLS,
duke@435 1218 "systemLoaderLockContentionRate");
duke@435 1219 NEWPERFEVENTCOUNTER(_sync_nonSystemLoaderLockContentionRate, SUN_CLS,
duke@435 1220 "nonSystemLoaderLockContentionRate");
duke@435 1221 NEWPERFEVENTCOUNTER(_sync_JVMFindLoadedClassLockFreeCounter, SUN_CLS,
duke@435 1222 "jvmFindLoadedClassNoLockCalls");
duke@435 1223 NEWPERFEVENTCOUNTER(_sync_JVMDefineClassLockFreeCounter, SUN_CLS,
duke@435 1224 "jvmDefineClassNoLockCalls");
duke@435 1225
duke@435 1226 NEWPERFEVENTCOUNTER(_sync_JNIDefineClassLockFreeCounter, SUN_CLS,
duke@435 1227 "jniDefineClassNoLockCalls");
duke@435 1228
duke@435 1229 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS,
duke@435 1230 "unsafeDefineClassCalls");
duke@435 1231
duke@435 1232 NEWPERFEVENTCOUNTER(_isUnsyncloadClass, SUN_CLS, "isUnsyncloadClassSet");
duke@435 1233 NEWPERFEVENTCOUNTER(_load_instance_class_failCounter, SUN_CLS,
duke@435 1234 "loadInstanceClassFailRate");
duke@435 1235
duke@435 1236 // increment the isUnsyncloadClass counter if UnsyncloadClass is set.
duke@435 1237 if (UnsyncloadClass) {
duke@435 1238 _isUnsyncloadClass->inc();
duke@435 1239 }
duke@435 1240 }
duke@435 1241
duke@435 1242 // lookup zip library entry points
duke@435 1243 load_zip_library();
iklam@7089 1244 #if INCLUDE_CDS
duke@435 1245 // initialize search path
iklam@7089 1246 if (DumpSharedSpaces) {
iklam@7089 1247 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info();
iklam@7089 1248 }
iklam@7089 1249 #endif
duke@435 1250 setup_bootstrap_search_path();
duke@435 1251 if (LazyBootClassLoader) {
duke@435 1252 // set up meta index which makes boot classpath initialization lazier
iklam@7089 1253 setup_bootstrap_meta_index();
duke@435 1254 }
duke@435 1255 }
duke@435 1256
iklam@7089 1257 #if INCLUDE_CDS
iklam@7089 1258 void ClassLoader::initialize_shared_path() {
iklam@7089 1259 if (DumpSharedSpaces) {
iklam@7089 1260 ClassLoaderExt::setup_search_paths();
iklam@7089 1261 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
iklam@7089 1262 }
iklam@7089 1263 }
iklam@7089 1264 #endif
duke@435 1265
duke@435 1266 jlong ClassLoader::classloader_time_ms() {
duke@435 1267 return UsePerfData ?
duke@435 1268 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
duke@435 1269 }
duke@435 1270
duke@435 1271 jlong ClassLoader::class_init_count() {
duke@435 1272 return UsePerfData ? _perf_classes_inited->get_value() : -1;
duke@435 1273 }
duke@435 1274
duke@435 1275 jlong ClassLoader::class_init_time_ms() {
duke@435 1276 return UsePerfData ?
duke@435 1277 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
duke@435 1278 }
duke@435 1279
duke@435 1280 jlong ClassLoader::class_verify_time_ms() {
duke@435 1281 return UsePerfData ?
duke@435 1282 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
duke@435 1283 }
duke@435 1284
duke@435 1285 jlong ClassLoader::class_link_count() {
duke@435 1286 return UsePerfData ? _perf_classes_linked->get_value() : -1;
duke@435 1287 }
duke@435 1288
duke@435 1289 jlong ClassLoader::class_link_time_ms() {
duke@435 1290 return UsePerfData ?
duke@435 1291 Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1;
duke@435 1292 }
duke@435 1293
duke@435 1294 int ClassLoader::compute_Object_vtable() {
duke@435 1295 // hardwired for JDK1.2 -- would need to duplicate class file parsing
duke@435 1296 // code to determine actual value from file
duke@435 1297 // Would be value '11' if finals were in vtable
duke@435 1298 int JDK_1_2_Object_vtable_size = 5;
duke@435 1299 return JDK_1_2_Object_vtable_size * vtableEntry::size();
duke@435 1300 }
duke@435 1301
duke@435 1302
duke@435 1303 void classLoader_init() {
duke@435 1304 ClassLoader::initialize();
duke@435 1305 }
duke@435 1306
duke@435 1307
iklam@7090 1308 bool ClassLoader::get_canonical_path(const char* orig, char* out, int len) {
duke@435 1309 assert(orig != NULL && out != NULL && len > 0, "bad arguments");
duke@435 1310 if (CanonicalizeEntry != NULL) {
iklam@7090 1311 JavaThread* THREAD = JavaThread::current();
iklam@7090 1312 JNIEnv* env = THREAD->jni_environment();
iklam@7090 1313 ResourceMark rm(THREAD);
iklam@7090 1314
iklam@7090 1315 // os::native_path writes into orig_copy
iklam@7090 1316 char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(orig)+1);
iklam@7090 1317 strcpy(orig_copy, orig);
iklam@7090 1318 if ((CanonicalizeEntry)(env, os::native_path(orig_copy), out, len) < 0) {
duke@435 1319 return false;
duke@435 1320 }
duke@435 1321 } else {
duke@435 1322 // On JDK 1.2.2 the Canonicalize does not exist, so just do nothing
duke@435 1323 strncpy(out, orig, len);
duke@435 1324 out[len - 1] = '\0';
duke@435 1325 }
duke@435 1326 return true;
duke@435 1327 }
duke@435 1328
duke@435 1329 #ifndef PRODUCT
duke@435 1330
duke@435 1331 void ClassLoader::verify() {
duke@435 1332 _package_hash_table->verify();
duke@435 1333 }
duke@435 1334
duke@435 1335
duke@435 1336 // CompileTheWorld
duke@435 1337 //
duke@435 1338 // Iterates over all class path entries and forces compilation of all methods
duke@435 1339 // in all classes found. Currently, only zip/jar archives are searched.
duke@435 1340 //
duke@435 1341 // The classes are loaded by the Java level bootstrap class loader, and the
duke@435 1342 // initializer is called. If DelayCompilationDuringStartup is true (default),
duke@435 1343 // the interpreter will run the initialization code. Note that forcing
duke@435 1344 // initialization in this way could potentially lead to initialization order
duke@435 1345 // problems, in which case we could just force the initialization bit to be set.
duke@435 1346
duke@435 1347
duke@435 1348 // We need to iterate over the contents of a zip/jar file, so we replicate the
duke@435 1349 // jzcell and jzfile definitions from zip_util.h but rename jzfile to real_jzfile,
duke@435 1350 // since jzfile already has a void* definition.
duke@435 1351 //
duke@435 1352 // Note that this is only used in debug mode.
duke@435 1353 //
duke@435 1354 // HotSpot integration note:
duke@435 1355 // Matches zip_util.h 1.14 99/06/01 from jdk1.3 beta H build
duke@435 1356
duke@435 1357
duke@435 1358 // JDK 1.3 version
duke@435 1359 typedef struct real_jzentry13 { /* Zip file entry */
duke@435 1360 char *name; /* entry name */
duke@435 1361 jint time; /* modification time */
duke@435 1362 jint size; /* size of uncompressed data */
duke@435 1363 jint csize; /* size of compressed data (zero if uncompressed) */
duke@435 1364 jint crc; /* crc of uncompressed data */
duke@435 1365 char *comment; /* optional zip file comment */
duke@435 1366 jbyte *extra; /* optional extra data */
duke@435 1367 jint pos; /* position of LOC header (if negative) or data */
duke@435 1368 } real_jzentry13;
duke@435 1369
duke@435 1370 typedef struct real_jzfile13 { /* Zip file */
duke@435 1371 char *name; /* zip file name */
duke@435 1372 jint refs; /* number of active references */
duke@435 1373 jint fd; /* open file descriptor */
duke@435 1374 void *lock; /* read lock */
duke@435 1375 char *comment; /* zip file comment */
duke@435 1376 char *msg; /* zip error message */
duke@435 1377 void *entries; /* array of hash cells */
duke@435 1378 jint total; /* total number of entries */
duke@435 1379 unsigned short *table; /* Hash chain heads: indexes into entries */
duke@435 1380 jint tablelen; /* number of hash eads */
duke@435 1381 real_jzfile13 *next; /* next zip file in search list */
duke@435 1382 jzentry *cache; /* we cache the most recently freed jzentry */
duke@435 1383 /* Information on metadata names in META-INF directory */
duke@435 1384 char **metanames; /* array of meta names (may have null names) */
duke@435 1385 jint metacount; /* number of slots in metanames array */
duke@435 1386 /* If there are any per-entry comments, they are in the comments array */
duke@435 1387 char **comments;
duke@435 1388 } real_jzfile13;
duke@435 1389
duke@435 1390 // JDK 1.2 version
duke@435 1391 typedef struct real_jzentry12 { /* Zip file entry */
duke@435 1392 char *name; /* entry name */
duke@435 1393 jint time; /* modification time */
duke@435 1394 jint size; /* size of uncompressed data */
duke@435 1395 jint csize; /* size of compressed data (zero if uncompressed) */
duke@435 1396 jint crc; /* crc of uncompressed data */
duke@435 1397 char *comment; /* optional zip file comment */
duke@435 1398 jbyte *extra; /* optional extra data */
duke@435 1399 jint pos; /* position of LOC header (if negative) or data */
duke@435 1400 struct real_jzentry12 *next; /* next entry in hash table */
duke@435 1401 } real_jzentry12;
duke@435 1402
duke@435 1403 typedef struct real_jzfile12 { /* Zip file */
duke@435 1404 char *name; /* zip file name */
duke@435 1405 jint refs; /* number of active references */
duke@435 1406 jint fd; /* open file descriptor */
duke@435 1407 void *lock; /* read lock */
duke@435 1408 char *comment; /* zip file comment */
duke@435 1409 char *msg; /* zip error message */
duke@435 1410 real_jzentry12 *entries; /* array of zip entries */
duke@435 1411 jint total; /* total number of entries */
duke@435 1412 real_jzentry12 **table; /* hash table of entries */
duke@435 1413 jint tablelen; /* number of buckets */
duke@435 1414 jzfile *next; /* next zip file in search list */
duke@435 1415 } real_jzfile12;
duke@435 1416
duke@435 1417
duke@435 1418 void ClassPathDirEntry::compile_the_world(Handle loader, TRAPS) {
duke@435 1419 // For now we only compile all methods in all classes in zip/jar files
duke@435 1420 tty->print_cr("CompileTheWorld : Skipped classes in %s", _dir);
duke@435 1421 tty->cr();
duke@435 1422 }
duke@435 1423
duke@435 1424
duke@435 1425 bool ClassPathDirEntry::is_rt_jar() {
duke@435 1426 return false;
duke@435 1427 }
duke@435 1428
duke@435 1429 void ClassPathZipEntry::compile_the_world(Handle loader, TRAPS) {
duke@435 1430 if (JDK_Version::is_jdk12x_version()) {
duke@435 1431 compile_the_world12(loader, THREAD);
duke@435 1432 } else {
duke@435 1433 compile_the_world13(loader, THREAD);
duke@435 1434 }
duke@435 1435 if (HAS_PENDING_EXCEPTION) {
duke@435 1436 if (PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) {
duke@435 1437 CLEAR_PENDING_EXCEPTION;
duke@435 1438 tty->print_cr("\nCompileTheWorld : Ran out of memory\n");
coleenp@4037 1439 tty->print_cr("Increase class metadata storage if a limit was set");
duke@435 1440 } else {
duke@435 1441 tty->print_cr("\nCompileTheWorld : Unexpected exception occurred\n");
duke@435 1442 }
duke@435 1443 }
duke@435 1444 }
duke@435 1445
duke@435 1446 // Version that works for JDK 1.3.x
duke@435 1447 void ClassPathZipEntry::compile_the_world13(Handle loader, TRAPS) {
duke@435 1448 real_jzfile13* zip = (real_jzfile13*) _zip;
duke@435 1449 tty->print_cr("CompileTheWorld : Compiling all classes in %s", zip->name);
duke@435 1450 tty->cr();
duke@435 1451 // Iterate over all entries in zip file
duke@435 1452 for (int n = 0; ; n++) {
duke@435 1453 real_jzentry13 * ze = (real_jzentry13 *)((*GetNextEntry)(_zip, n));
duke@435 1454 if (ze == NULL) break;
duke@435 1455 ClassLoader::compile_the_world_in(ze->name, loader, CHECK);
duke@435 1456 }
duke@435 1457 }
duke@435 1458
duke@435 1459
duke@435 1460 // Version that works for JDK 1.2.x
duke@435 1461 void ClassPathZipEntry::compile_the_world12(Handle loader, TRAPS) {
duke@435 1462 real_jzfile12* zip = (real_jzfile12*) _zip;
duke@435 1463 tty->print_cr("CompileTheWorld : Compiling all classes in %s", zip->name);
duke@435 1464 tty->cr();
duke@435 1465 // Iterate over all entries in zip file
duke@435 1466 for (int n = 0; ; n++) {
duke@435 1467 real_jzentry12 * ze = (real_jzentry12 *)((*GetNextEntry)(_zip, n));
duke@435 1468 if (ze == NULL) break;
duke@435 1469 ClassLoader::compile_the_world_in(ze->name, loader, CHECK);
duke@435 1470 }
duke@435 1471 }
duke@435 1472
duke@435 1473 bool ClassPathZipEntry::is_rt_jar() {
duke@435 1474 if (JDK_Version::is_jdk12x_version()) {
duke@435 1475 return is_rt_jar12();
duke@435 1476 } else {
duke@435 1477 return is_rt_jar13();
duke@435 1478 }
duke@435 1479 }
duke@435 1480
duke@435 1481 // JDK 1.3 version
duke@435 1482 bool ClassPathZipEntry::is_rt_jar13() {
duke@435 1483 real_jzfile13* zip = (real_jzfile13*) _zip;
duke@435 1484 int len = (int)strlen(zip->name);
duke@435 1485 // Check whether zip name ends in "rt.jar"
duke@435 1486 // This will match other archives named rt.jar as well, but this is
duke@435 1487 // only used for debugging.
duke@435 1488 return (len >= 6) && (strcasecmp(zip->name + len - 6, "rt.jar") == 0);
duke@435 1489 }
duke@435 1490
duke@435 1491 // JDK 1.2 version
duke@435 1492 bool ClassPathZipEntry::is_rt_jar12() {
duke@435 1493 real_jzfile12* zip = (real_jzfile12*) _zip;
duke@435 1494 int len = (int)strlen(zip->name);
duke@435 1495 // Check whether zip name ends in "rt.jar"
duke@435 1496 // This will match other archives named rt.jar as well, but this is
duke@435 1497 // only used for debugging.
duke@435 1498 return (len >= 6) && (strcasecmp(zip->name + len - 6, "rt.jar") == 0);
duke@435 1499 }
duke@435 1500
duke@435 1501 void LazyClassPathEntry::compile_the_world(Handle loader, TRAPS) {
ccheung@5603 1502 ClassPathEntry* cpe = resolve_entry(THREAD);
ccheung@5603 1503 if (cpe != NULL) {
ccheung@5603 1504 cpe->compile_the_world(loader, CHECK);
ccheung@5603 1505 }
duke@435 1506 }
duke@435 1507
duke@435 1508 bool LazyClassPathEntry::is_rt_jar() {
ccheung@5603 1509 Thread* THREAD = Thread::current();
ccheung@5603 1510 ClassPathEntry* cpe = resolve_entry(THREAD);
ccheung@5603 1511 return (cpe != NULL) ? cpe->is_jar_file() : false;
duke@435 1512 }
duke@435 1513
duke@435 1514 void ClassLoader::compile_the_world() {
duke@435 1515 EXCEPTION_MARK;
duke@435 1516 HandleMark hm(THREAD);
duke@435 1517 ResourceMark rm(THREAD);
duke@435 1518 // Make sure we don't run with background compilation
duke@435 1519 BackgroundCompilation = false;
duke@435 1520 // Find bootstrap loader
duke@435 1521 Handle system_class_loader (THREAD, SystemDictionary::java_system_loader());
duke@435 1522 // Iterate over all bootstrap class path entries
duke@435 1523 ClassPathEntry* e = _first_entry;
twisti@4940 1524 jlong start = os::javaTimeMillis();
duke@435 1525 while (e != NULL) {
duke@435 1526 // We stop at rt.jar, unless it is the first bootstrap path entry
duke@435 1527 if (e->is_rt_jar() && e != _first_entry) break;
duke@435 1528 e->compile_the_world(system_class_loader, CATCH);
duke@435 1529 e = e->next();
duke@435 1530 }
twisti@4940 1531 jlong end = os::javaTimeMillis();
drchase@6680 1532 tty->print_cr("CompileTheWorld : Done (%d classes, %d methods, " JLONG_FORMAT " ms)",
twisti@4940 1533 _compile_the_world_class_counter, _compile_the_world_method_counter, (end - start));
duke@435 1534 {
duke@435 1535 // Print statistics as if before normal exit:
duke@435 1536 extern void print_statistics();
duke@435 1537 print_statistics();
duke@435 1538 }
duke@435 1539 vm_exit(0);
duke@435 1540 }
duke@435 1541
twisti@4940 1542 int ClassLoader::_compile_the_world_class_counter = 0;
twisti@4940 1543 int ClassLoader::_compile_the_world_method_counter = 0;
kvn@1623 1544 static int _codecache_sweep_counter = 0;
duke@435 1545
never@2564 1546 // Filter out all exceptions except OOMs
never@2564 1547 static void clear_pending_exception_if_not_oom(TRAPS) {
never@2564 1548 if (HAS_PENDING_EXCEPTION &&
never@2564 1549 !PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) {
never@2564 1550 CLEAR_PENDING_EXCEPTION;
never@2564 1551 }
never@2564 1552 // The CHECK at the caller will propagate the exception out
never@2564 1553 }
never@2564 1554
twisti@5723 1555 /**
twisti@5723 1556 * Returns if the given method should be compiled when doing compile-the-world.
twisti@5723 1557 *
twisti@5723 1558 * TODO: This should be a private method in a CompileTheWorld class.
twisti@5723 1559 */
twisti@5723 1560 static bool can_be_compiled(methodHandle m, int comp_level) {
twisti@5723 1561 assert(CompileTheWorld, "must be");
twisti@5723 1562
twisti@5723 1563 // It's not valid to compile a native wrapper for MethodHandle methods
twisti@5723 1564 // that take a MemberName appendix since the bytecode signature is not
twisti@5723 1565 // correct.
twisti@5723 1566 vmIntrinsics::ID iid = m->intrinsic_id();
twisti@5723 1567 if (MethodHandles::is_signature_polymorphic(iid) && MethodHandles::has_member_arg(iid)) {
twisti@5723 1568 return false;
twisti@5723 1569 }
twisti@5723 1570
twisti@5723 1571 return CompilationPolicy::can_be_compiled(m, comp_level);
twisti@5723 1572 }
twisti@5723 1573
duke@435 1574 void ClassLoader::compile_the_world_in(char* name, Handle loader, TRAPS) {
duke@435 1575 int len = (int)strlen(name);
duke@435 1576 if (len > 6 && strcmp(".class", name + len - 6) == 0) {
duke@435 1577 // We have a .class file
duke@435 1578 char buffer[2048];
duke@435 1579 strncpy(buffer, name, len - 6);
duke@435 1580 buffer[len-6] = 0;
duke@435 1581 // If the file has a period after removing .class, it's not really a
duke@435 1582 // valid class file. The class loader will check everything else.
duke@435 1583 if (strchr(buffer, '.') == NULL) {
twisti@4940 1584 _compile_the_world_class_counter++;
twisti@4940 1585 if (_compile_the_world_class_counter > CompileTheWorldStopAt) return;
never@1158 1586
never@1158 1587 // Construct name without extension
coleenp@2497 1588 TempNewSymbol sym = SymbolTable::new_symbol(buffer, CHECK);
never@1158 1589 // Use loader to load and initialize class
coleenp@4037 1590 Klass* ik = SystemDictionary::resolve_or_null(sym, loader, Handle(), THREAD);
never@1158 1591 instanceKlassHandle k (THREAD, ik);
never@1158 1592 if (k.not_null() && !HAS_PENDING_EXCEPTION) {
never@1158 1593 k->initialize(THREAD);
never@1158 1594 }
never@1158 1595 bool exception_occurred = HAS_PENDING_EXCEPTION;
never@2564 1596 clear_pending_exception_if_not_oom(CHECK);
never@1158 1597 if (CompileTheWorldPreloadClasses && k.not_null()) {
coleenp@4037 1598 ConstantPool::preload_and_initialize_all_classes(k->constants(), THREAD);
never@1158 1599 if (HAS_PENDING_EXCEPTION) {
never@1158 1600 // If something went wrong in preloading we just ignore it
never@2564 1601 clear_pending_exception_if_not_oom(CHECK);
twisti@4940 1602 tty->print_cr("Preloading failed for (%d) %s", _compile_the_world_class_counter, buffer);
duke@435 1603 }
never@1158 1604 }
never@1158 1605
twisti@4940 1606 if (_compile_the_world_class_counter >= CompileTheWorldStartAt) {
never@2605 1607 if (k.is_null() || exception_occurred) {
duke@435 1608 // If something went wrong (e.g. ExceptionInInitializerError) we skip this class
twisti@4940 1609 tty->print_cr("CompileTheWorld (%d) : Skipping %s", _compile_the_world_class_counter, buffer);
duke@435 1610 } else {
twisti@4940 1611 tty->print_cr("CompileTheWorld (%d) : %s", _compile_the_world_class_counter, buffer);
duke@435 1612 // Preload all classes to get around uncommon traps
duke@435 1613 // Iterate over all methods in class
iignatyev@5032 1614 int comp_level = CompilationPolicy::policy()->initial_compile_level();
duke@435 1615 for (int n = 0; n < k->methods()->length(); n++) {
coleenp@4037 1616 methodHandle m (THREAD, k->methods()->at(n));
twisti@5723 1617 if (can_be_compiled(m, comp_level)) {
kvn@1623 1618 if (++_codecache_sweep_counter == CompileTheWorldSafepointInterval) {
kvn@1623 1619 // Give sweeper a chance to keep up with CTW
kvn@1623 1620 VM_ForceSafepoint op;
kvn@1623 1621 VMThread::execute(&op);
kvn@1623 1622 _codecache_sweep_counter = 0;
kvn@1623 1623 }
duke@435 1624 // Force compilation
iignatyev@5032 1625 CompileBroker::compile_method(m, InvocationEntryBci, comp_level,
duke@435 1626 methodHandle(), 0, "CTW", THREAD);
duke@435 1627 if (HAS_PENDING_EXCEPTION) {
never@2564 1628 clear_pending_exception_if_not_oom(CHECK);
twisti@5723 1629 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string());
twisti@4940 1630 } else {
twisti@4940 1631 _compile_the_world_method_counter++;
duke@435 1632 }
iveresov@3035 1633 if (TieredCompilation && TieredStopAtLevel >= CompLevel_full_optimization) {
never@1158 1634 // Clobber the first compile and force second tier compilation
never@1158 1635 nmethod* nm = m->code();
iveresov@7147 1636 if (nm != NULL && !m->is_method_handle_intrinsic()) {
never@1158 1637 // Throw out the code so that the code cache doesn't fill up
never@1158 1638 nm->make_not_entrant();
never@1158 1639 }
iveresov@2138 1640 CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_full_optimization,
never@1158 1641 methodHandle(), 0, "CTW", THREAD);
never@1158 1642 if (HAS_PENDING_EXCEPTION) {
never@2564 1643 clear_pending_exception_if_not_oom(CHECK);
twisti@5723 1644 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string());
twisti@4940 1645 } else {
twisti@4940 1646 _compile_the_world_method_counter++;
never@1158 1647 }
duke@435 1648 }
twisti@5723 1649 } else {
twisti@5723 1650 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string());
duke@435 1651 }
never@1158 1652
never@1158 1653 nmethod* nm = m->code();
iveresov@7147 1654 if (nm != NULL && !m->is_method_handle_intrinsic()) {
never@1158 1655 // Throw out the code so that the code cache doesn't fill up
never@1158 1656 nm->make_not_entrant();
duke@435 1657 }
duke@435 1658 }
duke@435 1659 }
duke@435 1660 }
duke@435 1661 }
duke@435 1662 }
duke@435 1663 }
duke@435 1664
duke@435 1665 #endif //PRODUCT
minqi@2459 1666
minqi@2459 1667 // Please keep following two functions at end of this file. With them placed at top or in middle of the file,
minqi@2459 1668 // they could get inlined by agressive compiler, an unknown trick, see bug 6966589.
minqi@2459 1669 void PerfClassTraceTime::initialize() {
minqi@2459 1670 if (!UsePerfData) return;
minqi@2459 1671
minqi@2459 1672 if (_eventp != NULL) {
minqi@2459 1673 // increment the event counter
minqi@2459 1674 _eventp->inc();
minqi@2459 1675 }
minqi@2459 1676
minqi@2459 1677 // stop the current active thread-local timer to measure inclusive time
minqi@2459 1678 _prev_active_event = -1;
minqi@2459 1679 for (int i=0; i < EVENT_TYPE_COUNT; i++) {
minqi@2459 1680 if (_timers[i].is_active()) {
minqi@2459 1681 assert(_prev_active_event == -1, "should have only one active timer");
minqi@2459 1682 _prev_active_event = i;
minqi@2459 1683 _timers[i].stop();
minqi@2459 1684 }
minqi@2459 1685 }
minqi@2459 1686
minqi@2459 1687 if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) {
minqi@2459 1688 // start the inclusive timer if not recursively called
minqi@2459 1689 _t.start();
minqi@2459 1690 }
minqi@2459 1691
minqi@2459 1692 // start thread-local timer of the given event type
minqi@2459 1693 if (!_timers[_event_type].is_active()) {
minqi@2459 1694 _timers[_event_type].start();
minqi@2459 1695 }
minqi@2459 1696 }
minqi@2459 1697
minqi@2459 1698 PerfClassTraceTime::~PerfClassTraceTime() {
minqi@2459 1699 if (!UsePerfData) return;
minqi@2459 1700
minqi@2459 1701 // stop the thread-local timer as the event completes
minqi@2459 1702 // and resume the thread-local timer of the event next on the stack
minqi@2459 1703 _timers[_event_type].stop();
minqi@2459 1704 jlong selftime = _timers[_event_type].ticks();
minqi@2459 1705
minqi@2459 1706 if (_prev_active_event >= 0) {
minqi@2459 1707 _timers[_prev_active_event].start();
minqi@2459 1708 }
minqi@2459 1709
minqi@2459 1710 if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return;
minqi@2459 1711
minqi@2459 1712 // increment the counters only on the leaf call
minqi@2459 1713 _t.stop();
minqi@2459 1714 _timep->inc(_t.ticks());
minqi@2459 1715 if (_selftimep != NULL) {
minqi@2459 1716 _selftimep->inc(selftime);
minqi@2459 1717 }
minqi@2459 1718 // add all class loading related event selftime to the accumulated time counter
minqi@2459 1719 ClassLoader::perf_accumulated_time()->inc(selftime);
minqi@2459 1720
minqi@2459 1721 // reset the timer
minqi@2459 1722 _timers[_event_type].reset();
minqi@2459 1723 }

mercurial