annotate src/share/vm/classfile/classLoader.hpp @ 2031:75efcee5ac47

6966589: hs16-b08 causes java.lang.StackOverflowError Reviewed-by: mchung, dholmes, chrisphi
author minqi
date Thu, 07 Oct 2010 13:49:40 -0700
parents f95d63e2154a
children 3582bf76420e
rev   line source
duke@0 1 /*
stefank@1885 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1885 25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADER_HPP
stefank@1885 26 #define SHARE_VM_CLASSFILE_CLASSLOADER_HPP
stefank@1885 27
stefank@1885 28 #include "classfile/classFileParser.hpp"
stefank@1885 29 #include "runtime/perfData.hpp"
stefank@1885 30
duke@0 31 // The VM class loader.
duke@0 32 #include <sys/stat.h>
duke@0 33
duke@0 34
duke@0 35 // Meta-index (optional, to be able to skip opening boot classpath jar files)
duke@0 36 class MetaIndex: public CHeapObj {
duke@0 37 private:
duke@0 38 char** _meta_package_names;
duke@0 39 int _num_meta_package_names;
duke@0 40 public:
duke@0 41 MetaIndex(char** meta_package_names, int num_meta_package_names);
duke@0 42 ~MetaIndex();
duke@0 43 bool may_contain(const char* class_name);
duke@0 44 };
duke@0 45
duke@0 46
duke@0 47 // Class path entry (directory or zip file)
duke@0 48
duke@0 49 class ClassPathEntry: public CHeapObj {
duke@0 50 private:
duke@0 51 ClassPathEntry* _next;
duke@0 52 public:
duke@0 53 // Next entry in class path
duke@0 54 ClassPathEntry* next() { return _next; }
duke@0 55 void set_next(ClassPathEntry* next) {
duke@0 56 // may have unlocked readers, so write atomically.
duke@0 57 OrderAccess::release_store_ptr(&_next, next);
duke@0 58 }
duke@0 59 virtual bool is_jar_file() = 0;
duke@0 60 virtual const char* name() = 0;
duke@0 61 virtual bool is_lazy();
duke@0 62 // Constructor
duke@0 63 ClassPathEntry();
duke@0 64 // Attempt to locate file_name through this class path entry.
duke@0 65 // Returns a class file parsing stream if successfull.
duke@0 66 virtual ClassFileStream* open_stream(const char* name) = 0;
duke@0 67 // Debugging
duke@0 68 NOT_PRODUCT(virtual void compile_the_world(Handle loader, TRAPS) = 0;)
duke@0 69 NOT_PRODUCT(virtual bool is_rt_jar() = 0;)
duke@0 70 };
duke@0 71
duke@0 72
duke@0 73 class ClassPathDirEntry: public ClassPathEntry {
duke@0 74 private:
duke@0 75 char* _dir; // Name of directory
duke@0 76 public:
duke@0 77 bool is_jar_file() { return false; }
duke@0 78 const char* name() { return _dir; }
duke@0 79 ClassPathDirEntry(char* dir);
duke@0 80 ClassFileStream* open_stream(const char* name);
duke@0 81 // Debugging
duke@0 82 NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
duke@0 83 NOT_PRODUCT(bool is_rt_jar();)
duke@0 84 };
duke@0 85
duke@0 86
duke@0 87 // Type definitions for zip file and zip file entry
duke@0 88 typedef void* jzfile;
duke@0 89 typedef struct {
duke@0 90 char *name; /* entry name */
duke@0 91 jlong time; /* modification time */
duke@0 92 jlong size; /* size of uncompressed data */
duke@0 93 jlong csize; /* size of compressed data (zero if uncompressed) */
duke@0 94 jint crc; /* crc of uncompressed data */
duke@0 95 char *comment; /* optional zip file comment */
duke@0 96 jbyte *extra; /* optional extra data */
duke@0 97 jlong pos; /* position of LOC header (if negative) or data */
duke@0 98 } jzentry;
duke@0 99
duke@0 100
duke@0 101 class ClassPathZipEntry: public ClassPathEntry {
duke@0 102 private:
duke@0 103 jzfile* _zip; // The zip archive
duke@0 104 char* _zip_name; // Name of zip archive
duke@0 105 public:
duke@0 106 bool is_jar_file() { return true; }
duke@0 107 const char* name() { return _zip_name; }
duke@0 108 ClassPathZipEntry(jzfile* zip, const char* zip_name);
duke@0 109 ~ClassPathZipEntry();
duke@0 110 ClassFileStream* open_stream(const char* name);
duke@0 111 void contents_do(void f(const char* name, void* context), void* context);
duke@0 112 // Debugging
duke@0 113 NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
duke@0 114 NOT_PRODUCT(void compile_the_world12(Handle loader, TRAPS);) // JDK 1.2 version
duke@0 115 NOT_PRODUCT(void compile_the_world13(Handle loader, TRAPS);) // JDK 1.3 version
duke@0 116 NOT_PRODUCT(bool is_rt_jar();)
duke@0 117 NOT_PRODUCT(bool is_rt_jar12();)
duke@0 118 NOT_PRODUCT(bool is_rt_jar13();)
duke@0 119 };
duke@0 120
duke@0 121
duke@0 122 // For lazier loading of boot class path entries
duke@0 123 class LazyClassPathEntry: public ClassPathEntry {
duke@0 124 private:
duke@0 125 char* _path; // dir or file
duke@0 126 struct stat _st;
duke@0 127 MetaIndex* _meta_index;
duke@0 128 volatile ClassPathEntry* _resolved_entry;
duke@0 129 ClassPathEntry* resolve_entry();
duke@0 130 public:
duke@0 131 bool is_jar_file();
duke@0 132 const char* name() { return _path; }
duke@0 133 LazyClassPathEntry(char* path, struct stat st);
duke@0 134 ClassFileStream* open_stream(const char* name);
duke@0 135 void set_meta_index(MetaIndex* meta_index) { _meta_index = meta_index; }
duke@0 136 virtual bool is_lazy();
duke@0 137 // Debugging
duke@0 138 NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)
duke@0 139 NOT_PRODUCT(bool is_rt_jar();)
duke@0 140 };
duke@0 141
duke@0 142 class PackageHashtable;
duke@0 143 class PackageInfo;
duke@0 144 class HashtableBucket;
duke@0 145
duke@0 146 class ClassLoader: AllStatic {
duke@0 147 public:
duke@0 148 enum SomeConstants {
duke@0 149 package_hash_table_size = 31 // Number of buckets
duke@0 150 };
duke@0 151 private:
duke@0 152 friend class LazyClassPathEntry;
duke@0 153
duke@0 154 // Performance counters
duke@0 155 static PerfCounter* _perf_accumulated_time;
duke@0 156 static PerfCounter* _perf_classes_inited;
duke@0 157 static PerfCounter* _perf_class_init_time;
mchung@875 158 static PerfCounter* _perf_class_init_selftime;
mchung@875 159 static PerfCounter* _perf_classes_verified;
duke@0 160 static PerfCounter* _perf_class_verify_time;
mchung@875 161 static PerfCounter* _perf_class_verify_selftime;
duke@0 162 static PerfCounter* _perf_classes_linked;
duke@0 163 static PerfCounter* _perf_class_link_time;
mchung@875 164 static PerfCounter* _perf_class_link_selftime;
mchung@875 165 static PerfCounter* _perf_class_parse_time;
mchung@875 166 static PerfCounter* _perf_class_parse_selftime;
mchung@875 167 static PerfCounter* _perf_sys_class_lookup_time;
mchung@875 168 static PerfCounter* _perf_shared_classload_time;
mchung@875 169 static PerfCounter* _perf_sys_classload_time;
mchung@875 170 static PerfCounter* _perf_app_classload_time;
mchung@875 171 static PerfCounter* _perf_app_classload_selftime;
mchung@875 172 static PerfCounter* _perf_app_classload_count;
mchung@875 173 static PerfCounter* _perf_define_appclasses;
mchung@875 174 static PerfCounter* _perf_define_appclass_time;
mchung@875 175 static PerfCounter* _perf_define_appclass_selftime;
mchung@875 176 static PerfCounter* _perf_app_classfile_bytes_read;
mchung@875 177 static PerfCounter* _perf_sys_classfile_bytes_read;
duke@0 178
duke@0 179 static PerfCounter* _sync_systemLoaderLockContentionRate;
duke@0 180 static PerfCounter* _sync_nonSystemLoaderLockContentionRate;
duke@0 181 static PerfCounter* _sync_JVMFindLoadedClassLockFreeCounter;
duke@0 182 static PerfCounter* _sync_JVMDefineClassLockFreeCounter;
duke@0 183 static PerfCounter* _sync_JNIDefineClassLockFreeCounter;
duke@0 184
duke@0 185 static PerfCounter* _unsafe_defineClassCallCounter;
duke@0 186 static PerfCounter* _isUnsyncloadClass;
duke@0 187 static PerfCounter* _load_instance_class_failCounter;
duke@0 188
duke@0 189 // First entry in linked list of ClassPathEntry instances
duke@0 190 static ClassPathEntry* _first_entry;
duke@0 191 // Last entry in linked list of ClassPathEntry instances
duke@0 192 static ClassPathEntry* _last_entry;
duke@0 193 // Hash table used to keep track of loaded packages
duke@0 194 static PackageHashtable* _package_hash_table;
duke@0 195 static const char* _shared_archive;
duke@0 196
duke@0 197 // Hash function
duke@0 198 static unsigned int hash(const char *s, int n);
duke@0 199 // Returns the package file name corresponding to the specified package
duke@0 200 // or class name, or null if not found.
duke@0 201 static PackageInfo* lookup_package(const char *pkgname);
duke@0 202 // Adds a new package entry for the specified class or package name and
duke@0 203 // corresponding directory or jar file name.
duke@0 204 static bool add_package(const char *pkgname, int classpath_index, TRAPS);
duke@0 205
duke@0 206 // Initialization
duke@0 207 static void setup_meta_index();
duke@0 208 static void setup_bootstrap_search_path();
duke@0 209 static void load_zip_library();
duke@0 210 static void create_class_path_entry(char *path, struct stat st, ClassPathEntry **new_entry, bool lazy);
duke@0 211
duke@0 212 // Canonicalizes path names, so strcmp will work properly. This is mainly
duke@0 213 // to avoid confusing the zip library
duke@0 214 static bool get_canonical_path(char* orig, char* out, int len);
duke@0 215 public:
duke@0 216 // Used by the kernel jvm.
duke@0 217 static void update_class_path_entry_list(const char *path,
duke@0 218 bool check_for_duplicates);
duke@0 219 static void print_bootclasspath();
duke@0 220
duke@0 221 // Timing
mchung@875 222 static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; }
mchung@875 223 static PerfCounter* perf_classes_inited() { return _perf_classes_inited; }
mchung@875 224 static PerfCounter* perf_class_init_time() { return _perf_class_init_time; }
mchung@875 225 static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; }
mchung@875 226 static PerfCounter* perf_classes_verified() { return _perf_classes_verified; }
mchung@875 227 static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; }
mchung@875 228 static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; }
mchung@875 229 static PerfCounter* perf_classes_linked() { return _perf_classes_linked; }
mchung@875 230 static PerfCounter* perf_class_link_time() { return _perf_class_link_time; }
mchung@875 231 static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; }
mchung@875 232 static PerfCounter* perf_class_parse_time() { return _perf_class_parse_time; }
mchung@875 233 static PerfCounter* perf_class_parse_selftime() { return _perf_class_parse_selftime; }
mchung@875 234 static PerfCounter* perf_sys_class_lookup_time() { return _perf_sys_class_lookup_time; }
mchung@875 235 static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; }
mchung@875 236 static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; }
mchung@875 237 static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; }
mchung@875 238 static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; }
mchung@875 239 static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; }
mchung@875 240 static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; }
mchung@875 241 static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; }
mchung@875 242 static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }
mchung@875 243 static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }
mchung@875 244 static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }
duke@0 245
duke@0 246 // Record how often system loader lock object is contended
duke@0 247 static PerfCounter* sync_systemLoaderLockContentionRate() {
duke@0 248 return _sync_systemLoaderLockContentionRate;
duke@0 249 }
duke@0 250
duke@0 251 // Record how often non system loader lock object is contended
duke@0 252 static PerfCounter* sync_nonSystemLoaderLockContentionRate() {
duke@0 253 return _sync_nonSystemLoaderLockContentionRate;
duke@0 254 }
duke@0 255
duke@0 256 // Record how many calls to JVM_FindLoadedClass w/o holding a lock
duke@0 257 static PerfCounter* sync_JVMFindLoadedClassLockFreeCounter() {
duke@0 258 return _sync_JVMFindLoadedClassLockFreeCounter;
duke@0 259 }
duke@0 260
duke@0 261 // Record how many calls to JVM_DefineClass w/o holding a lock
duke@0 262 static PerfCounter* sync_JVMDefineClassLockFreeCounter() {
duke@0 263 return _sync_JVMDefineClassLockFreeCounter;
duke@0 264 }
duke@0 265
duke@0 266 // Record how many calls to jni_DefineClass w/o holding a lock
duke@0 267 static PerfCounter* sync_JNIDefineClassLockFreeCounter() {
duke@0 268 return _sync_JNIDefineClassLockFreeCounter;
duke@0 269 }
duke@0 270
duke@0 271 // Record how many calls to Unsafe_DefineClass
duke@0 272 static PerfCounter* unsafe_defineClassCallCounter() {
duke@0 273 return _unsafe_defineClassCallCounter;
duke@0 274 }
duke@0 275
duke@0 276 // Record how many times SystemDictionary::load_instance_class call
duke@0 277 // fails with linkageError when Unsyncloadclass flag is set.
duke@0 278 static PerfCounter* load_instance_class_failCounter() {
duke@0 279 return _load_instance_class_failCounter;
duke@0 280 }
duke@0 281
duke@0 282 // Load individual .class file
duke@0 283 static instanceKlassHandle load_classfile(symbolHandle h_name, TRAPS);
duke@0 284
duke@0 285 // If the specified package has been loaded by the system, then returns
duke@0 286 // the name of the directory or ZIP file that the package was loaded from.
duke@0 287 // Returns null if the package was not loaded.
duke@0 288 // Note: The specified name can either be the name of a class or package.
duke@0 289 // If a package name is specified, then it must be "/"-separator and also
duke@0 290 // end with a trailing "/".
duke@0 291 static oop get_system_package(const char* name, TRAPS);
duke@0 292
duke@0 293 // Returns an array of Java strings representing all of the currently
duke@0 294 // loaded system packages.
duke@0 295 // Note: The package names returned are "/"-separated and end with a
duke@0 296 // trailing "/".
duke@0 297 static objArrayOop get_system_packages(TRAPS);
duke@0 298
duke@0 299 // Initialization
duke@0 300 static void initialize();
duke@0 301 static void create_package_info_table();
duke@0 302 static void create_package_info_table(HashtableBucket *t, int length,
duke@0 303 int number_of_entries);
duke@0 304 static int compute_Object_vtable();
duke@0 305
duke@0 306 static ClassPathEntry* classpath_entry(int n) {
duke@0 307 ClassPathEntry* e = ClassLoader::_first_entry;
duke@0 308 while (--n >= 0) {
duke@0 309 assert(e != NULL, "Not that many classpath entries.");
duke@0 310 e = e->next();
duke@0 311 }
duke@0 312 return e;
duke@0 313 }
duke@0 314
duke@0 315 // Sharing dump and restore
duke@0 316 static void copy_package_info_buckets(char** top, char* end);
duke@0 317 static void copy_package_info_table(char** top, char* end);
duke@0 318
duke@0 319 // VM monitoring and management support
duke@0 320 static jlong classloader_time_ms();
duke@0 321 static jlong class_method_total_size();
duke@0 322 static jlong class_init_count();
duke@0 323 static jlong class_init_time_ms();
duke@0 324 static jlong class_verify_time_ms();
duke@0 325 static jlong class_link_count();
duke@0 326 static jlong class_link_time_ms();
duke@0 327
duke@0 328 // indicates if class path already contains a entry (exact match by name)
duke@0 329 static bool contains_entry(ClassPathEntry* entry);
duke@0 330
duke@0 331 // adds a class path list
duke@0 332 static void add_to_list(ClassPathEntry* new_entry);
duke@0 333
duke@0 334 // creates a class path zip entry (returns NULL if JAR file cannot be opened)
duke@0 335 static ClassPathZipEntry* create_class_path_zip_entry(const char *apath);
duke@0 336
duke@0 337 // Debugging
duke@0 338 static void verify() PRODUCT_RETURN;
duke@0 339
duke@0 340 // Force compilation of all methods in all classes in bootstrap class path (stress test)
duke@0 341 #ifndef PRODUCT
duke@0 342 private:
duke@0 343 static int _compile_the_world_counter;
duke@0 344 public:
duke@0 345 static void compile_the_world();
duke@0 346 static void compile_the_world_in(char* name, Handle loader, TRAPS);
duke@0 347 static int compile_the_world_counter() { return _compile_the_world_counter; }
duke@0 348 #endif //PRODUCT
duke@0 349 };
mchung@875 350
mchung@875 351 // PerfClassTraceTime is used to measure time for class loading related events.
mchung@875 352 // This class tracks cumulative time and exclusive time for specific event types.
mchung@875 353 // During the execution of one event, other event types (e.g. class loading and
mchung@875 354 // resolution) as well as recursive calls of the same event type could happen.
mchung@875 355 // Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)
mchung@875 356 // (i.e. only one event type) are active at a time even multiple PerfClassTraceTime
mchung@875 357 // instances have been created as multiple events are happening.
mchung@875 358 class PerfClassTraceTime {
minqi@2031 359 public:
minqi@2031 360 enum {
minqi@2031 361 CLASS_LOAD = 0,
minqi@2031 362 PARSE_CLASS = 1,
minqi@2031 363 CLASS_LINK = 2,
minqi@2031 364 CLASS_VERIFY = 3,
minqi@2031 365 CLASS_CLINIT = 4,
minqi@2031 366 DEFINE_CLASS = 5,
minqi@2031 367 EVENT_TYPE_COUNT = 6
minqi@2031 368 };
minqi@2031 369 protected:
minqi@2031 370 // _t tracks time from initialization to destruction of this timer instance
minqi@2031 371 // including time for all other event types, and recursive calls of this type.
minqi@2031 372 // When a timer is called recursively, the elapsedTimer _t would not be used.
minqi@2031 373 elapsedTimer _t;
minqi@2031 374 PerfLongCounter* _timep;
minqi@2031 375 PerfLongCounter* _selftimep;
minqi@2031 376 PerfLongCounter* _eventp;
minqi@2031 377 // pointer to thread-local recursion counter and timer array
minqi@2031 378 // The thread_local timers track cumulative time for specific event types
minqi@2031 379 // exclusive of time for other event types, but including recursive calls
minqi@2031 380 // of the same type.
minqi@2031 381 int* _recursion_counters;
minqi@2031 382 elapsedTimer* _timers;
minqi@2031 383 int _event_type;
minqi@2031 384 int _prev_active_event;
mchung@875 385
minqi@2031 386 public:
mchung@875 387
minqi@2031 388 inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */
minqi@2031 389 PerfLongCounter* selftimep, /* counter incremented with exclusive time */
minqi@2031 390 PerfLongCounter* eventp, /* event counter */
minqi@2031 391 int* recursion_counters, /* thread-local recursion counter array */
minqi@2031 392 elapsedTimer* timers, /* thread-local timer array */
minqi@2031 393 int type /* event type */ ) :
minqi@2031 394 _timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {
minqi@2031 395 initialize();
minqi@2031 396 }
mchung@875 397
minqi@2031 398 inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */
minqi@2031 399 elapsedTimer* timers, /* thread-local timer array */
minqi@2031 400 int type /* event type */ ) :
minqi@2031 401 _timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) {
minqi@2031 402 initialize();
minqi@2031 403 }
mchung@875 404
minqi@2031 405 inline void suspend() { _t.stop(); _timers[_event_type].stop(); }
minqi@2031 406 inline void resume() { _t.start(); _timers[_event_type].start(); }
mchung@875 407
minqi@2031 408 ~PerfClassTraceTime();
minqi@2031 409 void initialize();
mchung@875 410 };
mchung@875 411
stefank@1885 412 #endif // SHARE_VM_CLASSFILE_CLASSLOADER_HPP