src/share/vm/classfile/classLoader.cpp

Thu, 23 Oct 2014 12:02:08 -0700

author
asaha
date
Thu, 23 Oct 2014 12:02:08 -0700
changeset 7476
c2844108a708
parent 7242
f0bedf980c65
child 7322
4cb90023bf2b
permissions
-rw-r--r--

Merge

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
iklam@7089 417 void ClassLoader::trace_class_path(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) {
iklam@7089 423 tty->print("%s", msg);
iklam@7089 424 }
iklam@7089 425 if (name) {
iklam@7089 426 if (strlen(name) < 256) {
iklam@7089 427 tty->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') {
iklam@7089 432 tty->print("%c", name[0]);
iklam@7089 433 name++;
iklam@7089 434 }
iklam@7089 435 }
iklam@7089 436 }
iklam@7089 437 if (msg && msg[0] == '[') {
iklam@7089 438 tty->print_cr("]");
iklam@7089 439 } else {
iklam@7089 440 tty->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 {
iklam@7089 586 trace_class_path("[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@7090 613 void ClassLoader::setup_search_path(const char *class_path) {
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';
duke@435 628 update_class_path_entry_list(path, false);
iklam@7089 629 #if INCLUDE_CDS
iklam@7089 630 if (DumpSharedSpaces) {
iklam@7089 631 check_shared_classpath(path);
iklam@7089 632 }
iklam@7089 633 #endif
iklam@7089 634 while (class_path[end] == os::path_separator()[0]) {
duke@435 635 end++;
duke@435 636 }
duke@435 637 }
duke@435 638 }
duke@435 639
iklam@7090 640 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st,
iklam@7089 641 bool lazy, bool throw_exception, TRAPS) {
duke@435 642 JavaThread* thread = JavaThread::current();
duke@435 643 if (lazy) {
iklam@7089 644 return new LazyClassPathEntry(path, st, throw_exception);
duke@435 645 }
ccheung@5603 646 ClassPathEntry* new_entry = NULL;
ccheung@5603 647 if ((st->st_mode & S_IFREG) == S_IFREG) {
duke@435 648 // Regular file, should be a zip file
duke@435 649 // Canonicalized filename
duke@435 650 char canonical_path[JVM_MAXPATHLEN];
duke@435 651 if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
duke@435 652 // This matches the classic VM
iklam@7089 653 if (throw_exception) {
iklam@7089 654 THROW_MSG_(vmSymbols::java_io_IOException(), "Bad pathname", NULL);
iklam@7089 655 } else {
iklam@7089 656 return NULL;
iklam@7089 657 }
duke@435 658 }
duke@435 659 char* error_msg = NULL;
duke@435 660 jzfile* zip;
duke@435 661 {
duke@435 662 // enable call to C land
duke@435 663 ThreadToNativeFromVM ttn(thread);
duke@435 664 HandleMark hm(thread);
duke@435 665 zip = (*ZipOpen)(canonical_path, &error_msg);
duke@435 666 }
duke@435 667 if (zip != NULL && error_msg == NULL) {
ccheung@5603 668 new_entry = new ClassPathZipEntry(zip, path);
iklam@7089 669 if (TraceClassLoading || TraceClassPaths) {
duke@435 670 tty->print_cr("[Opened %s]", path);
duke@435 671 }
duke@435 672 } else {
duke@435 673 ResourceMark rm(thread);
duke@435 674 char *msg;
duke@435 675 if (error_msg == NULL) {
duke@435 676 msg = NEW_RESOURCE_ARRAY(char, strlen(path) + 128); ;
duke@435 677 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
duke@435 678 } else {
duke@435 679 int len = (int)(strlen(path) + strlen(error_msg) + 128);
duke@435 680 msg = NEW_RESOURCE_ARRAY(char, len); ;
duke@435 681 jio_snprintf(msg, len - 1, "error in opening JAR file <%s> %s", error_msg, path);
duke@435 682 }
iklam@7089 683 if (throw_exception) {
iklam@7089 684 THROW_MSG_(vmSymbols::java_lang_ClassNotFoundException(), msg, NULL);
iklam@7089 685 } else {
iklam@7089 686 return NULL;
iklam@7089 687 }
duke@435 688 }
duke@435 689 } else {
duke@435 690 // Directory
ccheung@5603 691 new_entry = new ClassPathDirEntry(path);
iklam@7089 692 if (TraceClassLoading || TraceClassPaths) {
duke@435 693 tty->print_cr("[Path %s]", path);
duke@435 694 }
duke@435 695 }
ccheung@5603 696 return new_entry;
duke@435 697 }
duke@435 698
duke@435 699
duke@435 700 // Create a class path zip entry for a given path (return NULL if not found
duke@435 701 // or zip/JAR file cannot be opened)
duke@435 702 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path) {
duke@435 703 // check for a regular file
duke@435 704 struct stat st;
duke@435 705 if (os::stat(path, &st) == 0) {
duke@435 706 if ((st.st_mode & S_IFREG) == S_IFREG) {
duke@435 707 char canonical_path[JVM_MAXPATHLEN];
iklam@7090 708 if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) {
duke@435 709 char* error_msg = NULL;
duke@435 710 jzfile* zip;
duke@435 711 {
duke@435 712 // enable call to C land
duke@435 713 JavaThread* thread = JavaThread::current();
duke@435 714 ThreadToNativeFromVM ttn(thread);
duke@435 715 HandleMark hm(thread);
duke@435 716 zip = (*ZipOpen)(canonical_path, &error_msg);
duke@435 717 }
duke@435 718 if (zip != NULL && error_msg == NULL) {
duke@435 719 // create using canonical path
duke@435 720 return new ClassPathZipEntry(zip, canonical_path);
duke@435 721 }
duke@435 722 }
duke@435 723 }
duke@435 724 }
duke@435 725 return NULL;
duke@435 726 }
duke@435 727
duke@435 728 // returns true if entry already on class path
duke@435 729 bool ClassLoader::contains_entry(ClassPathEntry *entry) {
duke@435 730 ClassPathEntry* e = _first_entry;
duke@435 731 while (e != NULL) {
duke@435 732 // assume zip entries have been canonicalized
duke@435 733 if (strcmp(entry->name(), e->name()) == 0) {
duke@435 734 return true;
duke@435 735 }
duke@435 736 e = e->next();
duke@435 737 }
duke@435 738 return false;
duke@435 739 }
duke@435 740
duke@435 741 void ClassLoader::add_to_list(ClassPathEntry *new_entry) {
duke@435 742 if (new_entry != NULL) {
duke@435 743 if (_last_entry == NULL) {
duke@435 744 _first_entry = _last_entry = new_entry;
duke@435 745 } else {
duke@435 746 _last_entry->set_next(new_entry);
duke@435 747 _last_entry = new_entry;
duke@435 748 }
duke@435 749 }
iklam@7089 750 _num_entries ++;
duke@435 751 }
duke@435 752
iklam@7089 753 // Returns true IFF the file/dir exists and the entry was successfully created.
iklam@7090 754 bool ClassLoader::update_class_path_entry_list(const char *path,
iklam@7089 755 bool check_for_duplicates,
iklam@7089 756 bool throw_exception) {
duke@435 757 struct stat st;
ccheung@5603 758 if (os::stat(path, &st) == 0) {
duke@435 759 // File or directory found
duke@435 760 ClassPathEntry* new_entry = NULL;
ccheung@5603 761 Thread* THREAD = Thread::current();
iklam@7089 762 new_entry = create_class_path_entry(path, &st, LazyBootClassLoader, throw_exception, CHECK_(false));
iklam@7089 763 if (new_entry == NULL) {
iklam@7089 764 return false;
iklam@7089 765 }
duke@435 766 // The kernel VM adds dynamically to the end of the classloader path and
duke@435 767 // doesn't reorder the bootclasspath which would break java.lang.Package
duke@435 768 // (see PackageInfo).
duke@435 769 // Add new entry to linked list
duke@435 770 if (!check_for_duplicates || !contains_entry(new_entry)) {
iklam@7089 771 ClassLoaderExt::add_class_path_entry(path, check_for_duplicates, new_entry);
duke@435 772 }
iklam@7089 773 return true;
iklam@7089 774 } else {
iklam@7089 775 #if INCLUDE_CDS
iklam@7089 776 if (DumpSharedSpaces) {
iklam@7089 777 _shared_paths_misc_info->add_nonexist_path(path);
iklam@7089 778 }
iklam@7090 779 #endif
iklam@7089 780 return false;
duke@435 781 }
duke@435 782 }
duke@435 783
duke@435 784 void ClassLoader::print_bootclasspath() {
duke@435 785 ClassPathEntry* e = _first_entry;
duke@435 786 tty->print("[bootclasspath= ");
duke@435 787 while (e != NULL) {
duke@435 788 tty->print("%s ;", e->name());
duke@435 789 e = e->next();
duke@435 790 }
duke@435 791 tty->print_cr("]");
duke@435 792 }
duke@435 793
duke@435 794 void ClassLoader::load_zip_library() {
duke@435 795 assert(ZipOpen == NULL, "should not load zip library twice");
duke@435 796 // First make sure native library is loaded
duke@435 797 os::native_java_library();
duke@435 798 // Load zip library
duke@435 799 char path[JVM_MAXPATHLEN];
duke@435 800 char ebuf[1024];
bpittore@4261 801 void* handle = NULL;
bpittore@4261 802 if (os::dll_build_name(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {
bpittore@4261 803 handle = os::dll_load(path, ebuf, sizeof ebuf);
bpittore@4261 804 }
duke@435 805 if (handle == NULL) {
duke@435 806 vm_exit_during_initialization("Unable to load ZIP library", path);
duke@435 807 }
duke@435 808 // Lookup zip entry points
ikrylov@2322 809 ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, os::dll_lookup(handle, "ZIP_Open"));
ikrylov@2322 810 ZipClose = CAST_TO_FN_PTR(ZipClose_t, os::dll_lookup(handle, "ZIP_Close"));
ikrylov@2322 811 FindEntry = CAST_TO_FN_PTR(FindEntry_t, os::dll_lookup(handle, "ZIP_FindEntry"));
ikrylov@2322 812 ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, os::dll_lookup(handle, "ZIP_ReadEntry"));
ikrylov@2322 813 ReadMappedEntry = CAST_TO_FN_PTR(ReadMappedEntry_t, os::dll_lookup(handle, "ZIP_ReadMappedEntry"));
ikrylov@2322 814 GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, os::dll_lookup(handle, "ZIP_GetNextEntry"));
jiangli@6868 815 Crc32 = CAST_TO_FN_PTR(Crc32_t, os::dll_lookup(handle, "ZIP_CRC32"));
duke@435 816
duke@435 817 // ZIP_Close is not exported on Windows in JDK5.0 so don't abort if ZIP_Close is NULL
jiangli@6868 818 if (ZipOpen == NULL || FindEntry == NULL || ReadEntry == NULL ||
jiangli@6868 819 GetNextEntry == NULL || Crc32 == NULL) {
duke@435 820 vm_exit_during_initialization("Corrupted ZIP library", path);
duke@435 821 }
duke@435 822
duke@435 823 // Lookup canonicalize entry in libjava.dll
duke@435 824 void *javalib_handle = os::native_java_library();
ikrylov@2322 825 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, os::dll_lookup(javalib_handle, "Canonicalize"));
duke@435 826 // This lookup only works on 1.3. Do not check for non-null here
duke@435 827 }
duke@435 828
jiangli@6868 829 int ClassLoader::crc32(int crc, const char* buf, int len) {
jiangli@6868 830 assert(Crc32 != NULL, "ZIP_CRC32 is not found");
jiangli@6868 831 return (*Crc32)(crc, (const jbyte*)buf, len);
jiangli@6868 832 }
jiangli@6868 833
duke@435 834 // PackageInfo data exists in order to support the java.lang.Package
duke@435 835 // class. A Package object provides information about a java package
duke@435 836 // (version, vendor, etc.) which originates in the manifest of the jar
duke@435 837 // file supplying the package. For application classes, the ClassLoader
duke@435 838 // object takes care of this.
duke@435 839
duke@435 840 // For system (boot) classes, the Java code in the Package class needs
duke@435 841 // to be able to identify which source jar file contained the boot
duke@435 842 // class, so that it can extract the manifest from it. This table
duke@435 843 // identifies java packages with jar files in the boot classpath.
duke@435 844
duke@435 845 // Because the boot classpath cannot change, the classpath index is
duke@435 846 // sufficient to identify the source jar file or directory. (Since
duke@435 847 // directories have no manifests, the directory name is not required,
duke@435 848 // but is available.)
duke@435 849
duke@435 850 // When using sharing -- the pathnames of entries in the boot classpath
duke@435 851 // may not be the same at runtime as they were when the archive was
duke@435 852 // created (NFS, Samba, etc.). The actual files and directories named
duke@435 853 // in the classpath must be the same files, in the same order, even
duke@435 854 // though the exact name is not the same.
duke@435 855
zgu@3900 856 class PackageInfo: public BasicHashtableEntry<mtClass> {
duke@435 857 public:
duke@435 858 const char* _pkgname; // Package name
duke@435 859 int _classpath_index; // Index of directory or JAR file loaded from
duke@435 860
duke@435 861 PackageInfo* next() {
zgu@3900 862 return (PackageInfo*)BasicHashtableEntry<mtClass>::next();
duke@435 863 }
duke@435 864
duke@435 865 const char* pkgname() { return _pkgname; }
duke@435 866 void set_pkgname(char* pkgname) { _pkgname = pkgname; }
duke@435 867
duke@435 868 const char* filename() {
duke@435 869 return ClassLoader::classpath_entry(_classpath_index)->name();
duke@435 870 }
duke@435 871
duke@435 872 void set_index(int index) {
duke@435 873 _classpath_index = index;
duke@435 874 }
duke@435 875 };
duke@435 876
duke@435 877
zgu@3900 878 class PackageHashtable : public BasicHashtable<mtClass> {
duke@435 879 private:
duke@435 880 inline unsigned int compute_hash(const char *s, int n) {
duke@435 881 unsigned int val = 0;
duke@435 882 while (--n >= 0) {
duke@435 883 val = *s++ + 31 * val;
duke@435 884 }
duke@435 885 return val;
duke@435 886 }
duke@435 887
duke@435 888 PackageInfo* bucket(int index) {
zgu@3900 889 return (PackageInfo*)BasicHashtable<mtClass>::bucket(index);
duke@435 890 }
duke@435 891
duke@435 892 PackageInfo* get_entry(int index, unsigned int hash,
duke@435 893 const char* pkgname, size_t n) {
duke@435 894 for (PackageInfo* pp = bucket(index); pp != NULL; pp = pp->next()) {
duke@435 895 if (pp->hash() == hash &&
duke@435 896 strncmp(pkgname, pp->pkgname(), n) == 0 &&
duke@435 897 pp->pkgname()[n] == '\0') {
duke@435 898 return pp;
duke@435 899 }
duke@435 900 }
duke@435 901 return NULL;
duke@435 902 }
duke@435 903
duke@435 904 public:
duke@435 905 PackageHashtable(int table_size)
zgu@3900 906 : BasicHashtable<mtClass>(table_size, sizeof(PackageInfo)) {}
duke@435 907
zgu@3900 908 PackageHashtable(int table_size, HashtableBucket<mtClass>* t, int number_of_entries)
zgu@3900 909 : BasicHashtable<mtClass>(table_size, sizeof(PackageInfo), t, number_of_entries) {}
duke@435 910
duke@435 911 PackageInfo* get_entry(const char* pkgname, int n) {
duke@435 912 unsigned int hash = compute_hash(pkgname, n);
duke@435 913 return get_entry(hash_to_index(hash), hash, pkgname, n);
duke@435 914 }
duke@435 915
duke@435 916 PackageInfo* new_entry(char* pkgname, int n) {
duke@435 917 unsigned int hash = compute_hash(pkgname, n);
duke@435 918 PackageInfo* pp;
zgu@3900 919 pp = (PackageInfo*)BasicHashtable<mtClass>::new_entry(hash);
duke@435 920 pp->set_pkgname(pkgname);
duke@435 921 return pp;
duke@435 922 }
duke@435 923
duke@435 924 void add_entry(PackageInfo* pp) {
duke@435 925 int index = hash_to_index(pp->hash());
zgu@3900 926 BasicHashtable<mtClass>::add_entry(index, pp);
duke@435 927 }
duke@435 928
duke@435 929 void copy_pkgnames(const char** packages) {
duke@435 930 int n = 0;
duke@435 931 for (int i = 0; i < table_size(); ++i) {
duke@435 932 for (PackageInfo* pp = bucket(i); pp != NULL; pp = pp->next()) {
duke@435 933 packages[n++] = pp->pkgname();
duke@435 934 }
duke@435 935 }
duke@435 936 assert(n == number_of_entries(), "just checking");
duke@435 937 }
duke@435 938
iklam@7089 939 CDS_ONLY(void copy_table(char** top, char* end, PackageHashtable* table);)
duke@435 940 };
duke@435 941
iklam@7089 942 #if INCLUDE_CDS
duke@435 943 void PackageHashtable::copy_table(char** top, char* end,
duke@435 944 PackageHashtable* table) {
duke@435 945 // Copy (relocate) the table to the shared space.
zgu@3900 946 BasicHashtable<mtClass>::copy_table(top, end);
duke@435 947
duke@435 948 // Calculate the space needed for the package name strings.
duke@435 949 int i;
iklam@7089 950 intptr_t* tableSize = (intptr_t*)(*top);
iklam@7089 951 *top += sizeof(intptr_t); // For table size
iklam@7089 952 char* tableStart = *top;
duke@435 953
duke@435 954 for (i = 0; i < table_size(); ++i) {
duke@435 955 for (PackageInfo* pp = table->bucket(i);
duke@435 956 pp != NULL;
duke@435 957 pp = pp->next()) {
duke@435 958 int n1 = (int)(strlen(pp->pkgname()) + 1);
iklam@7089 959 if (*top + n1 >= end) {
iklam@7089 960 report_out_of_shared_space(SharedMiscData);
iklam@7089 961 }
duke@435 962 pp->set_pkgname((char*)memcpy(*top, pp->pkgname(), n1));
duke@435 963 *top += n1;
duke@435 964 }
duke@435 965 }
duke@435 966 *top = (char*)align_size_up((intptr_t)*top, sizeof(HeapWord));
iklam@7089 967 if (*top >= end) {
iklam@7089 968 report_out_of_shared_space(SharedMiscData);
iklam@7089 969 }
iklam@7089 970
iklam@7089 971 // Write table size
iklam@7089 972 intptr_t len = *top - (char*)tableStart;
iklam@7089 973 *tableSize = len;
duke@435 974 }
duke@435 975
duke@435 976
duke@435 977 void ClassLoader::copy_package_info_buckets(char** top, char* end) {
duke@435 978 _package_hash_table->copy_buckets(top, end);
duke@435 979 }
duke@435 980
duke@435 981 void ClassLoader::copy_package_info_table(char** top, char* end) {
duke@435 982 _package_hash_table->copy_table(top, end, _package_hash_table);
duke@435 983 }
iklam@7089 984 #endif
duke@435 985
duke@435 986 PackageInfo* ClassLoader::lookup_package(const char *pkgname) {
duke@435 987 const char *cp = strrchr(pkgname, '/');
duke@435 988 if (cp != NULL) {
duke@435 989 // Package prefix found
duke@435 990 int n = cp - pkgname + 1;
duke@435 991 return _package_hash_table->get_entry(pkgname, n);
duke@435 992 }
duke@435 993 return NULL;
duke@435 994 }
duke@435 995
duke@435 996
duke@435 997 bool ClassLoader::add_package(const char *pkgname, int classpath_index, TRAPS) {
duke@435 998 assert(pkgname != NULL, "just checking");
duke@435 999 // Bootstrap loader no longer holds system loader lock obj serializing
duke@435 1000 // load_instance_class and thereby add_package
duke@435 1001 {
duke@435 1002 MutexLocker ml(PackageTable_lock, THREAD);
duke@435 1003 // First check for previously loaded entry
duke@435 1004 PackageInfo* pp = lookup_package(pkgname);
duke@435 1005 if (pp != NULL) {
duke@435 1006 // Existing entry found, check source of package
duke@435 1007 pp->set_index(classpath_index);
duke@435 1008 return true;
duke@435 1009 }
duke@435 1010
duke@435 1011 const char *cp = strrchr(pkgname, '/');
duke@435 1012 if (cp != NULL) {
duke@435 1013 // Package prefix found
duke@435 1014 int n = cp - pkgname + 1;
duke@435 1015
zgu@3900 1016 char* new_pkgname = NEW_C_HEAP_ARRAY(char, n + 1, mtClass);
duke@435 1017 if (new_pkgname == NULL) {
duke@435 1018 return false;
duke@435 1019 }
duke@435 1020
duke@435 1021 memcpy(new_pkgname, pkgname, n);
duke@435 1022 new_pkgname[n] = '\0';
duke@435 1023 pp = _package_hash_table->new_entry(new_pkgname, n);
duke@435 1024 pp->set_index(classpath_index);
duke@435 1025
duke@435 1026 // Insert into hash table
duke@435 1027 _package_hash_table->add_entry(pp);
duke@435 1028 }
duke@435 1029 return true;
duke@435 1030 }
duke@435 1031 }
duke@435 1032
duke@435 1033
duke@435 1034 oop ClassLoader::get_system_package(const char* name, TRAPS) {
duke@435 1035 PackageInfo* pp;
duke@435 1036 {
duke@435 1037 MutexLocker ml(PackageTable_lock, THREAD);
duke@435 1038 pp = lookup_package(name);
duke@435 1039 }
duke@435 1040 if (pp == NULL) {
duke@435 1041 return NULL;
duke@435 1042 } else {
duke@435 1043 Handle p = java_lang_String::create_from_str(pp->filename(), THREAD);
duke@435 1044 return p();
duke@435 1045 }
duke@435 1046 }
duke@435 1047
duke@435 1048
duke@435 1049 objArrayOop ClassLoader::get_system_packages(TRAPS) {
duke@435 1050 ResourceMark rm(THREAD);
duke@435 1051 int nof_entries;
duke@435 1052 const char** packages;
duke@435 1053 {
duke@435 1054 MutexLocker ml(PackageTable_lock, THREAD);
duke@435 1055 // Allocate resource char* array containing package names
duke@435 1056 nof_entries = _package_hash_table->number_of_entries();
duke@435 1057 if ((packages = NEW_RESOURCE_ARRAY(const char*, nof_entries)) == NULL) {
duke@435 1058 return NULL;
duke@435 1059 }
duke@435 1060 _package_hash_table->copy_pkgnames(packages);
duke@435 1061 }
duke@435 1062 // Allocate objArray and fill with java.lang.String
never@1577 1063 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(),
duke@435 1064 nof_entries, CHECK_0);
duke@435 1065 objArrayHandle result(THREAD, r);
duke@435 1066 for (int i = 0; i < nof_entries; i++) {
duke@435 1067 Handle str = java_lang_String::create_from_str(packages[i], CHECK_0);
duke@435 1068 result->obj_at_put(i, str());
duke@435 1069 }
duke@435 1070
duke@435 1071 return result();
duke@435 1072 }
duke@435 1073
duke@435 1074
coleenp@2497 1075 instanceKlassHandle ClassLoader::load_classfile(Symbol* h_name, TRAPS) {
duke@435 1076 ResourceMark rm(THREAD);
iklam@7089 1077 const char* class_name = h_name->as_C_string();
iklam@7089 1078 EventMark m("loading class %s", class_name);
duke@435 1079 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion);
duke@435 1080
duke@435 1081 stringStream st;
duke@435 1082 // st.print() uses too much stack space while handling a StackOverflowError
duke@435 1083 // st.print("%s.class", h_name->as_utf8());
duke@435 1084 st.print_raw(h_name->as_utf8());
duke@435 1085 st.print_raw(".class");
iklam@7089 1086 const char* file_name = st.as_string();
iklam@7089 1087 ClassLoaderExt::Context context(class_name, file_name, THREAD);
duke@435 1088
duke@435 1089 // Lookup stream for parsing .class file
duke@435 1090 ClassFileStream* stream = NULL;
duke@435 1091 int classpath_index = 0;
iklam@7089 1092 ClassPathEntry* e = NULL;
iklam@7089 1093 instanceKlassHandle h;
duke@435 1094 {
mchung@1310 1095 PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(),
mchung@1310 1096 ((JavaThread*) THREAD)->get_thread_stat()->perf_timers_addr(),
mchung@1310 1097 PerfClassTraceTime::CLASS_LOAD);
iklam@7089 1098 e = _first_entry;
duke@435 1099 while (e != NULL) {
iklam@7089 1100 stream = e->open_stream(file_name, CHECK_NULL);
iklam@7089 1101 if (!context.check(stream, classpath_index)) {
iklam@7089 1102 return h; // NULL
iklam@7089 1103 }
duke@435 1104 if (stream != NULL) {
duke@435 1105 break;
duke@435 1106 }
duke@435 1107 e = e->next();
duke@435 1108 ++classpath_index;
duke@435 1109 }
duke@435 1110 }
duke@435 1111
duke@435 1112 if (stream != NULL) {
duke@435 1113 // class file found, parse it
duke@435 1114 ClassFileParser parser(stream);
coleenp@4304 1115 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
duke@435 1116 Handle protection_domain;
coleenp@2497 1117 TempNewSymbol parsed_name = NULL;
duke@435 1118 instanceKlassHandle result = parser.parseClassFile(h_name,
coleenp@4304 1119 loader_data,
duke@435 1120 protection_domain,
duke@435 1121 parsed_name,
iklam@7089 1122 context.should_verify(classpath_index),
iklam@7089 1123 THREAD);
iklam@7089 1124 if (HAS_PENDING_EXCEPTION) {
iklam@7089 1125 ResourceMark rm;
iklam@7089 1126 if (DumpSharedSpaces) {
iklam@7089 1127 tty->print_cr("Preload Error: Failed to load %s", class_name);
iklam@7089 1128 }
iklam@7089 1129 return h;
iklam@7089 1130 }
iklam@7089 1131 h = context.record_result(classpath_index, e, result, THREAD);
iklam@7089 1132 } else {
iklam@7089 1133 if (DumpSharedSpaces) {
iklam@7089 1134 tty->print_cr("Preload Error: Cannot find %s", class_name);
duke@435 1135 }
duke@435 1136 }
duke@435 1137
duke@435 1138 return h;
duke@435 1139 }
duke@435 1140
duke@435 1141
zgu@3900 1142 void ClassLoader::create_package_info_table(HashtableBucket<mtClass> *t, int length,
duke@435 1143 int number_of_entries) {
duke@435 1144 assert(_package_hash_table == NULL, "One package info table allowed.");
zgu@3900 1145 assert(length == package_hash_table_size * sizeof(HashtableBucket<mtClass>),
duke@435 1146 "bad shared package info size.");
duke@435 1147 _package_hash_table = new PackageHashtable(package_hash_table_size, t,
duke@435 1148 number_of_entries);
duke@435 1149 }
duke@435 1150
duke@435 1151
duke@435 1152 void ClassLoader::create_package_info_table() {
duke@435 1153 assert(_package_hash_table == NULL, "shouldn't have one yet");
duke@435 1154 _package_hash_table = new PackageHashtable(package_hash_table_size);
duke@435 1155 }
duke@435 1156
duke@435 1157
duke@435 1158 // Initialize the class loader's access to methods in libzip. Parse and
duke@435 1159 // process the boot classpath into a list ClassPathEntry objects. Once
duke@435 1160 // this list has been created, it must not change order (see class PackageInfo)
duke@435 1161 // it can be appended to and is by jvmti and the kernel vm.
duke@435 1162
duke@435 1163 void ClassLoader::initialize() {
duke@435 1164 assert(_package_hash_table == NULL, "should have been initialized by now.");
duke@435 1165 EXCEPTION_MARK;
duke@435 1166
duke@435 1167 if (UsePerfData) {
duke@435 1168 // jvmstat performance counters
duke@435 1169 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
duke@435 1170 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
mchung@1310 1171 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
duke@435 1172 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
mchung@1310 1173 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
duke@435 1174 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
mchung@1310 1175 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
duke@435 1176 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
duke@435 1177 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
mchung@1310 1178 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
mchung@1310 1179
mchung@1310 1180 NEWPERFTICKCOUNTER(_perf_class_parse_time, SUN_CLS, "parseClassTime");
mchung@1310 1181 NEWPERFTICKCOUNTER(_perf_class_parse_selftime, SUN_CLS, "parseClassTime.self");
mchung@1310 1182 NEWPERFTICKCOUNTER(_perf_sys_class_lookup_time, SUN_CLS, "lookupSysClassTime");
mchung@1310 1183 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
mchung@1310 1184 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
mchung@1310 1185 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
mchung@1310 1186 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
mchung@1310 1187 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
mchung@1310 1188 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
mchung@1310 1189 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
mchung@1310 1190 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
mchung@1310 1191 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
mchung@1310 1192 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
mchung@1310 1193
duke@435 1194
duke@435 1195 // The following performance counters are added for measuring the impact
duke@435 1196 // of the bug fix of 6365597. They are mainly focused on finding out
duke@435 1197 // the behavior of system & user-defined classloader lock, whether
duke@435 1198 // ClassLoader.loadClass/findClass is being called synchronized or not.
duke@435 1199 // Also two additional counters are created to see whether 'UnsyncloadClass'
duke@435 1200 // flag is being set or not and how many times load_instance_class call
duke@435 1201 // fails with linkageError etc.
duke@435 1202 NEWPERFEVENTCOUNTER(_sync_systemLoaderLockContentionRate, SUN_CLS,
duke@435 1203 "systemLoaderLockContentionRate");
duke@435 1204 NEWPERFEVENTCOUNTER(_sync_nonSystemLoaderLockContentionRate, SUN_CLS,
duke@435 1205 "nonSystemLoaderLockContentionRate");
duke@435 1206 NEWPERFEVENTCOUNTER(_sync_JVMFindLoadedClassLockFreeCounter, SUN_CLS,
duke@435 1207 "jvmFindLoadedClassNoLockCalls");
duke@435 1208 NEWPERFEVENTCOUNTER(_sync_JVMDefineClassLockFreeCounter, SUN_CLS,
duke@435 1209 "jvmDefineClassNoLockCalls");
duke@435 1210
duke@435 1211 NEWPERFEVENTCOUNTER(_sync_JNIDefineClassLockFreeCounter, SUN_CLS,
duke@435 1212 "jniDefineClassNoLockCalls");
duke@435 1213
duke@435 1214 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS,
duke@435 1215 "unsafeDefineClassCalls");
duke@435 1216
duke@435 1217 NEWPERFEVENTCOUNTER(_isUnsyncloadClass, SUN_CLS, "isUnsyncloadClassSet");
duke@435 1218 NEWPERFEVENTCOUNTER(_load_instance_class_failCounter, SUN_CLS,
duke@435 1219 "loadInstanceClassFailRate");
duke@435 1220
duke@435 1221 // increment the isUnsyncloadClass counter if UnsyncloadClass is set.
duke@435 1222 if (UnsyncloadClass) {
duke@435 1223 _isUnsyncloadClass->inc();
duke@435 1224 }
duke@435 1225 }
duke@435 1226
duke@435 1227 // lookup zip library entry points
duke@435 1228 load_zip_library();
iklam@7089 1229 #if INCLUDE_CDS
duke@435 1230 // initialize search path
iklam@7089 1231 if (DumpSharedSpaces) {
iklam@7089 1232 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info();
iklam@7089 1233 }
iklam@7089 1234 #endif
duke@435 1235 setup_bootstrap_search_path();
duke@435 1236 if (LazyBootClassLoader) {
duke@435 1237 // set up meta index which makes boot classpath initialization lazier
iklam@7089 1238 setup_bootstrap_meta_index();
duke@435 1239 }
duke@435 1240 }
duke@435 1241
iklam@7089 1242 #if INCLUDE_CDS
iklam@7089 1243 void ClassLoader::initialize_shared_path() {
iklam@7089 1244 if (DumpSharedSpaces) {
iklam@7089 1245 ClassLoaderExt::setup_search_paths();
iklam@7089 1246 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
iklam@7089 1247 }
iklam@7089 1248 }
iklam@7089 1249 #endif
duke@435 1250
duke@435 1251 jlong ClassLoader::classloader_time_ms() {
duke@435 1252 return UsePerfData ?
duke@435 1253 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
duke@435 1254 }
duke@435 1255
duke@435 1256 jlong ClassLoader::class_init_count() {
duke@435 1257 return UsePerfData ? _perf_classes_inited->get_value() : -1;
duke@435 1258 }
duke@435 1259
duke@435 1260 jlong ClassLoader::class_init_time_ms() {
duke@435 1261 return UsePerfData ?
duke@435 1262 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
duke@435 1263 }
duke@435 1264
duke@435 1265 jlong ClassLoader::class_verify_time_ms() {
duke@435 1266 return UsePerfData ?
duke@435 1267 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
duke@435 1268 }
duke@435 1269
duke@435 1270 jlong ClassLoader::class_link_count() {
duke@435 1271 return UsePerfData ? _perf_classes_linked->get_value() : -1;
duke@435 1272 }
duke@435 1273
duke@435 1274 jlong ClassLoader::class_link_time_ms() {
duke@435 1275 return UsePerfData ?
duke@435 1276 Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1;
duke@435 1277 }
duke@435 1278
duke@435 1279 int ClassLoader::compute_Object_vtable() {
duke@435 1280 // hardwired for JDK1.2 -- would need to duplicate class file parsing
duke@435 1281 // code to determine actual value from file
duke@435 1282 // Would be value '11' if finals were in vtable
duke@435 1283 int JDK_1_2_Object_vtable_size = 5;
duke@435 1284 return JDK_1_2_Object_vtable_size * vtableEntry::size();
duke@435 1285 }
duke@435 1286
duke@435 1287
duke@435 1288 void classLoader_init() {
duke@435 1289 ClassLoader::initialize();
duke@435 1290 }
duke@435 1291
duke@435 1292
iklam@7090 1293 bool ClassLoader::get_canonical_path(const char* orig, char* out, int len) {
duke@435 1294 assert(orig != NULL && out != NULL && len > 0, "bad arguments");
duke@435 1295 if (CanonicalizeEntry != NULL) {
iklam@7090 1296 JavaThread* THREAD = JavaThread::current();
iklam@7090 1297 JNIEnv* env = THREAD->jni_environment();
iklam@7090 1298 ResourceMark rm(THREAD);
iklam@7090 1299
iklam@7090 1300 // os::native_path writes into orig_copy
iklam@7090 1301 char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(orig)+1);
iklam@7090 1302 strcpy(orig_copy, orig);
iklam@7090 1303 if ((CanonicalizeEntry)(env, os::native_path(orig_copy), out, len) < 0) {
duke@435 1304 return false;
duke@435 1305 }
duke@435 1306 } else {
duke@435 1307 // On JDK 1.2.2 the Canonicalize does not exist, so just do nothing
duke@435 1308 strncpy(out, orig, len);
duke@435 1309 out[len - 1] = '\0';
duke@435 1310 }
duke@435 1311 return true;
duke@435 1312 }
duke@435 1313
duke@435 1314 #ifndef PRODUCT
duke@435 1315
duke@435 1316 void ClassLoader::verify() {
duke@435 1317 _package_hash_table->verify();
duke@435 1318 }
duke@435 1319
duke@435 1320
duke@435 1321 // CompileTheWorld
duke@435 1322 //
duke@435 1323 // Iterates over all class path entries and forces compilation of all methods
duke@435 1324 // in all classes found. Currently, only zip/jar archives are searched.
duke@435 1325 //
duke@435 1326 // The classes are loaded by the Java level bootstrap class loader, and the
duke@435 1327 // initializer is called. If DelayCompilationDuringStartup is true (default),
duke@435 1328 // the interpreter will run the initialization code. Note that forcing
duke@435 1329 // initialization in this way could potentially lead to initialization order
duke@435 1330 // problems, in which case we could just force the initialization bit to be set.
duke@435 1331
duke@435 1332
duke@435 1333 // We need to iterate over the contents of a zip/jar file, so we replicate the
duke@435 1334 // jzcell and jzfile definitions from zip_util.h but rename jzfile to real_jzfile,
duke@435 1335 // since jzfile already has a void* definition.
duke@435 1336 //
duke@435 1337 // Note that this is only used in debug mode.
duke@435 1338 //
duke@435 1339 // HotSpot integration note:
duke@435 1340 // Matches zip_util.h 1.14 99/06/01 from jdk1.3 beta H build
duke@435 1341
duke@435 1342
duke@435 1343 // JDK 1.3 version
duke@435 1344 typedef struct real_jzentry13 { /* Zip file entry */
duke@435 1345 char *name; /* entry name */
duke@435 1346 jint time; /* modification time */
duke@435 1347 jint size; /* size of uncompressed data */
duke@435 1348 jint csize; /* size of compressed data (zero if uncompressed) */
duke@435 1349 jint crc; /* crc of uncompressed data */
duke@435 1350 char *comment; /* optional zip file comment */
duke@435 1351 jbyte *extra; /* optional extra data */
duke@435 1352 jint pos; /* position of LOC header (if negative) or data */
duke@435 1353 } real_jzentry13;
duke@435 1354
duke@435 1355 typedef struct real_jzfile13 { /* Zip file */
duke@435 1356 char *name; /* zip file name */
duke@435 1357 jint refs; /* number of active references */
duke@435 1358 jint fd; /* open file descriptor */
duke@435 1359 void *lock; /* read lock */
duke@435 1360 char *comment; /* zip file comment */
duke@435 1361 char *msg; /* zip error message */
duke@435 1362 void *entries; /* array of hash cells */
duke@435 1363 jint total; /* total number of entries */
duke@435 1364 unsigned short *table; /* Hash chain heads: indexes into entries */
duke@435 1365 jint tablelen; /* number of hash eads */
duke@435 1366 real_jzfile13 *next; /* next zip file in search list */
duke@435 1367 jzentry *cache; /* we cache the most recently freed jzentry */
duke@435 1368 /* Information on metadata names in META-INF directory */
duke@435 1369 char **metanames; /* array of meta names (may have null names) */
duke@435 1370 jint metacount; /* number of slots in metanames array */
duke@435 1371 /* If there are any per-entry comments, they are in the comments array */
duke@435 1372 char **comments;
duke@435 1373 } real_jzfile13;
duke@435 1374
duke@435 1375 // JDK 1.2 version
duke@435 1376 typedef struct real_jzentry12 { /* Zip file entry */
duke@435 1377 char *name; /* entry name */
duke@435 1378 jint time; /* modification time */
duke@435 1379 jint size; /* size of uncompressed data */
duke@435 1380 jint csize; /* size of compressed data (zero if uncompressed) */
duke@435 1381 jint crc; /* crc of uncompressed data */
duke@435 1382 char *comment; /* optional zip file comment */
duke@435 1383 jbyte *extra; /* optional extra data */
duke@435 1384 jint pos; /* position of LOC header (if negative) or data */
duke@435 1385 struct real_jzentry12 *next; /* next entry in hash table */
duke@435 1386 } real_jzentry12;
duke@435 1387
duke@435 1388 typedef struct real_jzfile12 { /* Zip file */
duke@435 1389 char *name; /* zip file name */
duke@435 1390 jint refs; /* number of active references */
duke@435 1391 jint fd; /* open file descriptor */
duke@435 1392 void *lock; /* read lock */
duke@435 1393 char *comment; /* zip file comment */
duke@435 1394 char *msg; /* zip error message */
duke@435 1395 real_jzentry12 *entries; /* array of zip entries */
duke@435 1396 jint total; /* total number of entries */
duke@435 1397 real_jzentry12 **table; /* hash table of entries */
duke@435 1398 jint tablelen; /* number of buckets */
duke@435 1399 jzfile *next; /* next zip file in search list */
duke@435 1400 } real_jzfile12;
duke@435 1401
duke@435 1402
duke@435 1403 void ClassPathDirEntry::compile_the_world(Handle loader, TRAPS) {
duke@435 1404 // For now we only compile all methods in all classes in zip/jar files
duke@435 1405 tty->print_cr("CompileTheWorld : Skipped classes in %s", _dir);
duke@435 1406 tty->cr();
duke@435 1407 }
duke@435 1408
duke@435 1409
duke@435 1410 bool ClassPathDirEntry::is_rt_jar() {
duke@435 1411 return false;
duke@435 1412 }
duke@435 1413
duke@435 1414 void ClassPathZipEntry::compile_the_world(Handle loader, TRAPS) {
duke@435 1415 if (JDK_Version::is_jdk12x_version()) {
duke@435 1416 compile_the_world12(loader, THREAD);
duke@435 1417 } else {
duke@435 1418 compile_the_world13(loader, THREAD);
duke@435 1419 }
duke@435 1420 if (HAS_PENDING_EXCEPTION) {
duke@435 1421 if (PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) {
duke@435 1422 CLEAR_PENDING_EXCEPTION;
duke@435 1423 tty->print_cr("\nCompileTheWorld : Ran out of memory\n");
coleenp@4037 1424 tty->print_cr("Increase class metadata storage if a limit was set");
duke@435 1425 } else {
duke@435 1426 tty->print_cr("\nCompileTheWorld : Unexpected exception occurred\n");
duke@435 1427 }
duke@435 1428 }
duke@435 1429 }
duke@435 1430
duke@435 1431 // Version that works for JDK 1.3.x
duke@435 1432 void ClassPathZipEntry::compile_the_world13(Handle loader, TRAPS) {
duke@435 1433 real_jzfile13* zip = (real_jzfile13*) _zip;
duke@435 1434 tty->print_cr("CompileTheWorld : Compiling all classes in %s", zip->name);
duke@435 1435 tty->cr();
duke@435 1436 // Iterate over all entries in zip file
duke@435 1437 for (int n = 0; ; n++) {
duke@435 1438 real_jzentry13 * ze = (real_jzentry13 *)((*GetNextEntry)(_zip, n));
duke@435 1439 if (ze == NULL) break;
duke@435 1440 ClassLoader::compile_the_world_in(ze->name, loader, CHECK);
duke@435 1441 }
duke@435 1442 }
duke@435 1443
duke@435 1444
duke@435 1445 // Version that works for JDK 1.2.x
duke@435 1446 void ClassPathZipEntry::compile_the_world12(Handle loader, TRAPS) {
duke@435 1447 real_jzfile12* zip = (real_jzfile12*) _zip;
duke@435 1448 tty->print_cr("CompileTheWorld : Compiling all classes in %s", zip->name);
duke@435 1449 tty->cr();
duke@435 1450 // Iterate over all entries in zip file
duke@435 1451 for (int n = 0; ; n++) {
duke@435 1452 real_jzentry12 * ze = (real_jzentry12 *)((*GetNextEntry)(_zip, n));
duke@435 1453 if (ze == NULL) break;
duke@435 1454 ClassLoader::compile_the_world_in(ze->name, loader, CHECK);
duke@435 1455 }
duke@435 1456 }
duke@435 1457
duke@435 1458 bool ClassPathZipEntry::is_rt_jar() {
duke@435 1459 if (JDK_Version::is_jdk12x_version()) {
duke@435 1460 return is_rt_jar12();
duke@435 1461 } else {
duke@435 1462 return is_rt_jar13();
duke@435 1463 }
duke@435 1464 }
duke@435 1465
duke@435 1466 // JDK 1.3 version
duke@435 1467 bool ClassPathZipEntry::is_rt_jar13() {
duke@435 1468 real_jzfile13* zip = (real_jzfile13*) _zip;
duke@435 1469 int len = (int)strlen(zip->name);
duke@435 1470 // Check whether zip name ends in "rt.jar"
duke@435 1471 // This will match other archives named rt.jar as well, but this is
duke@435 1472 // only used for debugging.
duke@435 1473 return (len >= 6) && (strcasecmp(zip->name + len - 6, "rt.jar") == 0);
duke@435 1474 }
duke@435 1475
duke@435 1476 // JDK 1.2 version
duke@435 1477 bool ClassPathZipEntry::is_rt_jar12() {
duke@435 1478 real_jzfile12* zip = (real_jzfile12*) _zip;
duke@435 1479 int len = (int)strlen(zip->name);
duke@435 1480 // Check whether zip name ends in "rt.jar"
duke@435 1481 // This will match other archives named rt.jar as well, but this is
duke@435 1482 // only used for debugging.
duke@435 1483 return (len >= 6) && (strcasecmp(zip->name + len - 6, "rt.jar") == 0);
duke@435 1484 }
duke@435 1485
duke@435 1486 void LazyClassPathEntry::compile_the_world(Handle loader, TRAPS) {
ccheung@5603 1487 ClassPathEntry* cpe = resolve_entry(THREAD);
ccheung@5603 1488 if (cpe != NULL) {
ccheung@5603 1489 cpe->compile_the_world(loader, CHECK);
ccheung@5603 1490 }
duke@435 1491 }
duke@435 1492
duke@435 1493 bool LazyClassPathEntry::is_rt_jar() {
ccheung@5603 1494 Thread* THREAD = Thread::current();
ccheung@5603 1495 ClassPathEntry* cpe = resolve_entry(THREAD);
ccheung@5603 1496 return (cpe != NULL) ? cpe->is_jar_file() : false;
duke@435 1497 }
duke@435 1498
duke@435 1499 void ClassLoader::compile_the_world() {
duke@435 1500 EXCEPTION_MARK;
duke@435 1501 HandleMark hm(THREAD);
duke@435 1502 ResourceMark rm(THREAD);
duke@435 1503 // Make sure we don't run with background compilation
duke@435 1504 BackgroundCompilation = false;
duke@435 1505 // Find bootstrap loader
duke@435 1506 Handle system_class_loader (THREAD, SystemDictionary::java_system_loader());
duke@435 1507 // Iterate over all bootstrap class path entries
duke@435 1508 ClassPathEntry* e = _first_entry;
twisti@4940 1509 jlong start = os::javaTimeMillis();
duke@435 1510 while (e != NULL) {
duke@435 1511 // We stop at rt.jar, unless it is the first bootstrap path entry
duke@435 1512 if (e->is_rt_jar() && e != _first_entry) break;
duke@435 1513 e->compile_the_world(system_class_loader, CATCH);
duke@435 1514 e = e->next();
duke@435 1515 }
twisti@4940 1516 jlong end = os::javaTimeMillis();
drchase@6680 1517 tty->print_cr("CompileTheWorld : Done (%d classes, %d methods, " JLONG_FORMAT " ms)",
twisti@4940 1518 _compile_the_world_class_counter, _compile_the_world_method_counter, (end - start));
duke@435 1519 {
duke@435 1520 // Print statistics as if before normal exit:
duke@435 1521 extern void print_statistics();
duke@435 1522 print_statistics();
duke@435 1523 }
duke@435 1524 vm_exit(0);
duke@435 1525 }
duke@435 1526
twisti@4940 1527 int ClassLoader::_compile_the_world_class_counter = 0;
twisti@4940 1528 int ClassLoader::_compile_the_world_method_counter = 0;
kvn@1623 1529 static int _codecache_sweep_counter = 0;
duke@435 1530
never@2564 1531 // Filter out all exceptions except OOMs
never@2564 1532 static void clear_pending_exception_if_not_oom(TRAPS) {
never@2564 1533 if (HAS_PENDING_EXCEPTION &&
never@2564 1534 !PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) {
never@2564 1535 CLEAR_PENDING_EXCEPTION;
never@2564 1536 }
never@2564 1537 // The CHECK at the caller will propagate the exception out
never@2564 1538 }
never@2564 1539
twisti@5723 1540 /**
twisti@5723 1541 * Returns if the given method should be compiled when doing compile-the-world.
twisti@5723 1542 *
twisti@5723 1543 * TODO: This should be a private method in a CompileTheWorld class.
twisti@5723 1544 */
twisti@5723 1545 static bool can_be_compiled(methodHandle m, int comp_level) {
twisti@5723 1546 assert(CompileTheWorld, "must be");
twisti@5723 1547
twisti@5723 1548 // It's not valid to compile a native wrapper for MethodHandle methods
twisti@5723 1549 // that take a MemberName appendix since the bytecode signature is not
twisti@5723 1550 // correct.
twisti@5723 1551 vmIntrinsics::ID iid = m->intrinsic_id();
twisti@5723 1552 if (MethodHandles::is_signature_polymorphic(iid) && MethodHandles::has_member_arg(iid)) {
twisti@5723 1553 return false;
twisti@5723 1554 }
twisti@5723 1555
twisti@5723 1556 return CompilationPolicy::can_be_compiled(m, comp_level);
twisti@5723 1557 }
twisti@5723 1558
duke@435 1559 void ClassLoader::compile_the_world_in(char* name, Handle loader, TRAPS) {
duke@435 1560 int len = (int)strlen(name);
duke@435 1561 if (len > 6 && strcmp(".class", name + len - 6) == 0) {
duke@435 1562 // We have a .class file
duke@435 1563 char buffer[2048];
duke@435 1564 strncpy(buffer, name, len - 6);
duke@435 1565 buffer[len-6] = 0;
duke@435 1566 // If the file has a period after removing .class, it's not really a
duke@435 1567 // valid class file. The class loader will check everything else.
duke@435 1568 if (strchr(buffer, '.') == NULL) {
twisti@4940 1569 _compile_the_world_class_counter++;
twisti@4940 1570 if (_compile_the_world_class_counter > CompileTheWorldStopAt) return;
never@1158 1571
never@1158 1572 // Construct name without extension
coleenp@2497 1573 TempNewSymbol sym = SymbolTable::new_symbol(buffer, CHECK);
never@1158 1574 // Use loader to load and initialize class
coleenp@4037 1575 Klass* ik = SystemDictionary::resolve_or_null(sym, loader, Handle(), THREAD);
never@1158 1576 instanceKlassHandle k (THREAD, ik);
never@1158 1577 if (k.not_null() && !HAS_PENDING_EXCEPTION) {
never@1158 1578 k->initialize(THREAD);
never@1158 1579 }
never@1158 1580 bool exception_occurred = HAS_PENDING_EXCEPTION;
never@2564 1581 clear_pending_exception_if_not_oom(CHECK);
never@1158 1582 if (CompileTheWorldPreloadClasses && k.not_null()) {
coleenp@4037 1583 ConstantPool::preload_and_initialize_all_classes(k->constants(), THREAD);
never@1158 1584 if (HAS_PENDING_EXCEPTION) {
never@1158 1585 // If something went wrong in preloading we just ignore it
never@2564 1586 clear_pending_exception_if_not_oom(CHECK);
twisti@4940 1587 tty->print_cr("Preloading failed for (%d) %s", _compile_the_world_class_counter, buffer);
duke@435 1588 }
never@1158 1589 }
never@1158 1590
twisti@4940 1591 if (_compile_the_world_class_counter >= CompileTheWorldStartAt) {
never@2605 1592 if (k.is_null() || exception_occurred) {
duke@435 1593 // If something went wrong (e.g. ExceptionInInitializerError) we skip this class
twisti@4940 1594 tty->print_cr("CompileTheWorld (%d) : Skipping %s", _compile_the_world_class_counter, buffer);
duke@435 1595 } else {
twisti@4940 1596 tty->print_cr("CompileTheWorld (%d) : %s", _compile_the_world_class_counter, buffer);
duke@435 1597 // Preload all classes to get around uncommon traps
duke@435 1598 // Iterate over all methods in class
iignatyev@5032 1599 int comp_level = CompilationPolicy::policy()->initial_compile_level();
duke@435 1600 for (int n = 0; n < k->methods()->length(); n++) {
coleenp@4037 1601 methodHandle m (THREAD, k->methods()->at(n));
twisti@5723 1602 if (can_be_compiled(m, comp_level)) {
kvn@1623 1603 if (++_codecache_sweep_counter == CompileTheWorldSafepointInterval) {
kvn@1623 1604 // Give sweeper a chance to keep up with CTW
kvn@1623 1605 VM_ForceSafepoint op;
kvn@1623 1606 VMThread::execute(&op);
kvn@1623 1607 _codecache_sweep_counter = 0;
kvn@1623 1608 }
duke@435 1609 // Force compilation
iignatyev@5032 1610 CompileBroker::compile_method(m, InvocationEntryBci, comp_level,
duke@435 1611 methodHandle(), 0, "CTW", THREAD);
duke@435 1612 if (HAS_PENDING_EXCEPTION) {
never@2564 1613 clear_pending_exception_if_not_oom(CHECK);
twisti@5723 1614 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string());
twisti@4940 1615 } else {
twisti@4940 1616 _compile_the_world_method_counter++;
duke@435 1617 }
iveresov@3035 1618 if (TieredCompilation && TieredStopAtLevel >= CompLevel_full_optimization) {
never@1158 1619 // Clobber the first compile and force second tier compilation
never@1158 1620 nmethod* nm = m->code();
iveresov@7147 1621 if (nm != NULL && !m->is_method_handle_intrinsic()) {
never@1158 1622 // Throw out the code so that the code cache doesn't fill up
never@1158 1623 nm->make_not_entrant();
never@1158 1624 m->clear_code();
never@1158 1625 }
iveresov@2138 1626 CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_full_optimization,
never@1158 1627 methodHandle(), 0, "CTW", THREAD);
never@1158 1628 if (HAS_PENDING_EXCEPTION) {
never@2564 1629 clear_pending_exception_if_not_oom(CHECK);
twisti@5723 1630 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string());
twisti@4940 1631 } else {
twisti@4940 1632 _compile_the_world_method_counter++;
never@1158 1633 }
duke@435 1634 }
twisti@5723 1635 } else {
twisti@5723 1636 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string());
duke@435 1637 }
never@1158 1638
never@1158 1639 nmethod* nm = m->code();
iveresov@7147 1640 if (nm != NULL && !m->is_method_handle_intrinsic()) {
never@1158 1641 // Throw out the code so that the code cache doesn't fill up
never@1158 1642 nm->make_not_entrant();
never@1158 1643 m->clear_code();
duke@435 1644 }
duke@435 1645 }
duke@435 1646 }
duke@435 1647 }
duke@435 1648 }
duke@435 1649 }
duke@435 1650 }
duke@435 1651
duke@435 1652 #endif //PRODUCT
minqi@2459 1653
minqi@2459 1654 // Please keep following two functions at end of this file. With them placed at top or in middle of the file,
minqi@2459 1655 // they could get inlined by agressive compiler, an unknown trick, see bug 6966589.
minqi@2459 1656 void PerfClassTraceTime::initialize() {
minqi@2459 1657 if (!UsePerfData) return;
minqi@2459 1658
minqi@2459 1659 if (_eventp != NULL) {
minqi@2459 1660 // increment the event counter
minqi@2459 1661 _eventp->inc();
minqi@2459 1662 }
minqi@2459 1663
minqi@2459 1664 // stop the current active thread-local timer to measure inclusive time
minqi@2459 1665 _prev_active_event = -1;
minqi@2459 1666 for (int i=0; i < EVENT_TYPE_COUNT; i++) {
minqi@2459 1667 if (_timers[i].is_active()) {
minqi@2459 1668 assert(_prev_active_event == -1, "should have only one active timer");
minqi@2459 1669 _prev_active_event = i;
minqi@2459 1670 _timers[i].stop();
minqi@2459 1671 }
minqi@2459 1672 }
minqi@2459 1673
minqi@2459 1674 if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) {
minqi@2459 1675 // start the inclusive timer if not recursively called
minqi@2459 1676 _t.start();
minqi@2459 1677 }
minqi@2459 1678
minqi@2459 1679 // start thread-local timer of the given event type
minqi@2459 1680 if (!_timers[_event_type].is_active()) {
minqi@2459 1681 _timers[_event_type].start();
minqi@2459 1682 }
minqi@2459 1683 }
minqi@2459 1684
minqi@2459 1685 PerfClassTraceTime::~PerfClassTraceTime() {
minqi@2459 1686 if (!UsePerfData) return;
minqi@2459 1687
minqi@2459 1688 // stop the thread-local timer as the event completes
minqi@2459 1689 // and resume the thread-local timer of the event next on the stack
minqi@2459 1690 _timers[_event_type].stop();
minqi@2459 1691 jlong selftime = _timers[_event_type].ticks();
minqi@2459 1692
minqi@2459 1693 if (_prev_active_event >= 0) {
minqi@2459 1694 _timers[_prev_active_event].start();
minqi@2459 1695 }
minqi@2459 1696
minqi@2459 1697 if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return;
minqi@2459 1698
minqi@2459 1699 // increment the counters only on the leaf call
minqi@2459 1700 _t.stop();
minqi@2459 1701 _timep->inc(_t.ticks());
minqi@2459 1702 if (_selftimep != NULL) {
minqi@2459 1703 _selftimep->inc(selftime);
minqi@2459 1704 }
minqi@2459 1705 // add all class loading related event selftime to the accumulated time counter
minqi@2459 1706 ClassLoader::perf_accumulated_time()->inc(selftime);
minqi@2459 1707
minqi@2459 1708 // reset the timer
minqi@2459 1709 _timers[_event_type].reset();
minqi@2459 1710 }

mercurial