src/share/vm/classfile/classLoader.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6868
ca6d25be853b
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial