duke@435: /* trims@1907: * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: // The VM class loader. duke@435: #include duke@435: duke@435: duke@435: // Meta-index (optional, to be able to skip opening boot classpath jar files) duke@435: class MetaIndex: public CHeapObj { duke@435: private: duke@435: char** _meta_package_names; duke@435: int _num_meta_package_names; duke@435: public: duke@435: MetaIndex(char** meta_package_names, int num_meta_package_names); duke@435: ~MetaIndex(); duke@435: bool may_contain(const char* class_name); duke@435: }; duke@435: duke@435: duke@435: // Class path entry (directory or zip file) duke@435: duke@435: class ClassPathEntry: public CHeapObj { duke@435: private: duke@435: ClassPathEntry* _next; duke@435: public: duke@435: // Next entry in class path duke@435: ClassPathEntry* next() { return _next; } duke@435: void set_next(ClassPathEntry* next) { duke@435: // may have unlocked readers, so write atomically. duke@435: OrderAccess::release_store_ptr(&_next, next); duke@435: } duke@435: virtual bool is_jar_file() = 0; duke@435: virtual const char* name() = 0; duke@435: virtual bool is_lazy(); duke@435: // Constructor duke@435: ClassPathEntry(); duke@435: // Attempt to locate file_name through this class path entry. duke@435: // Returns a class file parsing stream if successfull. duke@435: virtual ClassFileStream* open_stream(const char* name) = 0; duke@435: // Debugging duke@435: NOT_PRODUCT(virtual void compile_the_world(Handle loader, TRAPS) = 0;) duke@435: NOT_PRODUCT(virtual bool is_rt_jar() = 0;) duke@435: }; duke@435: duke@435: duke@435: class ClassPathDirEntry: public ClassPathEntry { duke@435: private: duke@435: char* _dir; // Name of directory duke@435: public: duke@435: bool is_jar_file() { return false; } duke@435: const char* name() { return _dir; } duke@435: ClassPathDirEntry(char* dir); duke@435: ClassFileStream* open_stream(const char* name); duke@435: // Debugging duke@435: NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);) duke@435: NOT_PRODUCT(bool is_rt_jar();) duke@435: }; duke@435: duke@435: duke@435: // Type definitions for zip file and zip file entry duke@435: typedef void* jzfile; duke@435: typedef struct { duke@435: char *name; /* entry name */ duke@435: jlong time; /* modification time */ duke@435: jlong size; /* size of uncompressed data */ duke@435: jlong csize; /* size of compressed data (zero if uncompressed) */ duke@435: jint crc; /* crc of uncompressed data */ duke@435: char *comment; /* optional zip file comment */ duke@435: jbyte *extra; /* optional extra data */ duke@435: jlong pos; /* position of LOC header (if negative) or data */ duke@435: } jzentry; duke@435: duke@435: duke@435: class ClassPathZipEntry: public ClassPathEntry { duke@435: private: duke@435: jzfile* _zip; // The zip archive duke@435: char* _zip_name; // Name of zip archive duke@435: public: duke@435: bool is_jar_file() { return true; } duke@435: const char* name() { return _zip_name; } duke@435: ClassPathZipEntry(jzfile* zip, const char* zip_name); duke@435: ~ClassPathZipEntry(); duke@435: ClassFileStream* open_stream(const char* name); duke@435: void contents_do(void f(const char* name, void* context), void* context); duke@435: // Debugging duke@435: NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);) duke@435: NOT_PRODUCT(void compile_the_world12(Handle loader, TRAPS);) // JDK 1.2 version duke@435: NOT_PRODUCT(void compile_the_world13(Handle loader, TRAPS);) // JDK 1.3 version duke@435: NOT_PRODUCT(bool is_rt_jar();) duke@435: NOT_PRODUCT(bool is_rt_jar12();) duke@435: NOT_PRODUCT(bool is_rt_jar13();) duke@435: }; duke@435: duke@435: duke@435: // For lazier loading of boot class path entries duke@435: class LazyClassPathEntry: public ClassPathEntry { duke@435: private: duke@435: char* _path; // dir or file duke@435: struct stat _st; duke@435: MetaIndex* _meta_index; duke@435: volatile ClassPathEntry* _resolved_entry; duke@435: ClassPathEntry* resolve_entry(); duke@435: public: duke@435: bool is_jar_file(); duke@435: const char* name() { return _path; } duke@435: LazyClassPathEntry(char* path, struct stat st); duke@435: ClassFileStream* open_stream(const char* name); duke@435: void set_meta_index(MetaIndex* meta_index) { _meta_index = meta_index; } duke@435: virtual bool is_lazy(); duke@435: // Debugging duke@435: NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);) duke@435: NOT_PRODUCT(bool is_rt_jar();) duke@435: }; duke@435: duke@435: class PackageHashtable; duke@435: class PackageInfo; duke@435: class HashtableBucket; duke@435: duke@435: class ClassLoader: AllStatic { duke@435: public: duke@435: enum SomeConstants { duke@435: package_hash_table_size = 31 // Number of buckets duke@435: }; duke@435: private: duke@435: friend class LazyClassPathEntry; duke@435: duke@435: // Performance counters duke@435: static PerfCounter* _perf_accumulated_time; duke@435: static PerfCounter* _perf_classes_inited; duke@435: static PerfCounter* _perf_class_init_time; mchung@1310: static PerfCounter* _perf_class_init_selftime; mchung@1310: static PerfCounter* _perf_classes_verified; duke@435: static PerfCounter* _perf_class_verify_time; mchung@1310: static PerfCounter* _perf_class_verify_selftime; duke@435: static PerfCounter* _perf_classes_linked; duke@435: static PerfCounter* _perf_class_link_time; mchung@1310: static PerfCounter* _perf_class_link_selftime; mchung@1310: static PerfCounter* _perf_class_parse_time; mchung@1310: static PerfCounter* _perf_class_parse_selftime; mchung@1310: static PerfCounter* _perf_sys_class_lookup_time; mchung@1310: static PerfCounter* _perf_shared_classload_time; mchung@1310: static PerfCounter* _perf_sys_classload_time; mchung@1310: static PerfCounter* _perf_app_classload_time; mchung@1310: static PerfCounter* _perf_app_classload_selftime; mchung@1310: static PerfCounter* _perf_app_classload_count; mchung@1310: static PerfCounter* _perf_define_appclasses; mchung@1310: static PerfCounter* _perf_define_appclass_time; mchung@1310: static PerfCounter* _perf_define_appclass_selftime; mchung@1310: static PerfCounter* _perf_app_classfile_bytes_read; mchung@1310: static PerfCounter* _perf_sys_classfile_bytes_read; duke@435: duke@435: static PerfCounter* _sync_systemLoaderLockContentionRate; duke@435: static PerfCounter* _sync_nonSystemLoaderLockContentionRate; duke@435: static PerfCounter* _sync_JVMFindLoadedClassLockFreeCounter; duke@435: static PerfCounter* _sync_JVMDefineClassLockFreeCounter; duke@435: static PerfCounter* _sync_JNIDefineClassLockFreeCounter; duke@435: duke@435: static PerfCounter* _unsafe_defineClassCallCounter; duke@435: static PerfCounter* _isUnsyncloadClass; duke@435: static PerfCounter* _load_instance_class_failCounter; duke@435: duke@435: // First entry in linked list of ClassPathEntry instances duke@435: static ClassPathEntry* _first_entry; duke@435: // Last entry in linked list of ClassPathEntry instances duke@435: static ClassPathEntry* _last_entry; duke@435: // Hash table used to keep track of loaded packages duke@435: static PackageHashtable* _package_hash_table; duke@435: static const char* _shared_archive; duke@435: duke@435: // Hash function duke@435: static unsigned int hash(const char *s, int n); duke@435: // Returns the package file name corresponding to the specified package duke@435: // or class name, or null if not found. duke@435: static PackageInfo* lookup_package(const char *pkgname); duke@435: // Adds a new package entry for the specified class or package name and duke@435: // corresponding directory or jar file name. duke@435: static bool add_package(const char *pkgname, int classpath_index, TRAPS); duke@435: duke@435: // Initialization duke@435: static void setup_meta_index(); duke@435: static void setup_bootstrap_search_path(); duke@435: static void load_zip_library(); duke@435: static void create_class_path_entry(char *path, struct stat st, ClassPathEntry **new_entry, bool lazy); duke@435: duke@435: // Canonicalizes path names, so strcmp will work properly. This is mainly duke@435: // to avoid confusing the zip library duke@435: static bool get_canonical_path(char* orig, char* out, int len); duke@435: public: duke@435: // Used by the kernel jvm. duke@435: static void update_class_path_entry_list(const char *path, duke@435: bool check_for_duplicates); duke@435: static void print_bootclasspath(); duke@435: duke@435: // Timing mchung@1310: static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; } mchung@1310: static PerfCounter* perf_classes_inited() { return _perf_classes_inited; } mchung@1310: static PerfCounter* perf_class_init_time() { return _perf_class_init_time; } mchung@1310: static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; } mchung@1310: static PerfCounter* perf_classes_verified() { return _perf_classes_verified; } mchung@1310: static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; } mchung@1310: static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; } mchung@1310: static PerfCounter* perf_classes_linked() { return _perf_classes_linked; } mchung@1310: static PerfCounter* perf_class_link_time() { return _perf_class_link_time; } mchung@1310: static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; } mchung@1310: static PerfCounter* perf_class_parse_time() { return _perf_class_parse_time; } mchung@1310: static PerfCounter* perf_class_parse_selftime() { return _perf_class_parse_selftime; } mchung@1310: static PerfCounter* perf_sys_class_lookup_time() { return _perf_sys_class_lookup_time; } mchung@1310: static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; } mchung@1310: static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; } mchung@1310: static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; } mchung@1310: static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; } mchung@1310: static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; } mchung@1310: static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; } mchung@1310: static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; } mchung@1310: static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; } mchung@1310: static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; } mchung@1310: static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; } duke@435: duke@435: // Record how often system loader lock object is contended duke@435: static PerfCounter* sync_systemLoaderLockContentionRate() { duke@435: return _sync_systemLoaderLockContentionRate; duke@435: } duke@435: duke@435: // Record how often non system loader lock object is contended duke@435: static PerfCounter* sync_nonSystemLoaderLockContentionRate() { duke@435: return _sync_nonSystemLoaderLockContentionRate; duke@435: } duke@435: duke@435: // Record how many calls to JVM_FindLoadedClass w/o holding a lock duke@435: static PerfCounter* sync_JVMFindLoadedClassLockFreeCounter() { duke@435: return _sync_JVMFindLoadedClassLockFreeCounter; duke@435: } duke@435: duke@435: // Record how many calls to JVM_DefineClass w/o holding a lock duke@435: static PerfCounter* sync_JVMDefineClassLockFreeCounter() { duke@435: return _sync_JVMDefineClassLockFreeCounter; duke@435: } duke@435: duke@435: // Record how many calls to jni_DefineClass w/o holding a lock duke@435: static PerfCounter* sync_JNIDefineClassLockFreeCounter() { duke@435: return _sync_JNIDefineClassLockFreeCounter; duke@435: } duke@435: duke@435: // Record how many calls to Unsafe_DefineClass duke@435: static PerfCounter* unsafe_defineClassCallCounter() { duke@435: return _unsafe_defineClassCallCounter; duke@435: } duke@435: duke@435: // Record how many times SystemDictionary::load_instance_class call duke@435: // fails with linkageError when Unsyncloadclass flag is set. duke@435: static PerfCounter* load_instance_class_failCounter() { duke@435: return _load_instance_class_failCounter; duke@435: } duke@435: duke@435: // Load individual .class file duke@435: static instanceKlassHandle load_classfile(symbolHandle h_name, TRAPS); duke@435: duke@435: // If the specified package has been loaded by the system, then returns duke@435: // the name of the directory or ZIP file that the package was loaded from. duke@435: // Returns null if the package was not loaded. duke@435: // Note: The specified name can either be the name of a class or package. duke@435: // If a package name is specified, then it must be "/"-separator and also duke@435: // end with a trailing "/". duke@435: static oop get_system_package(const char* name, TRAPS); duke@435: duke@435: // Returns an array of Java strings representing all of the currently duke@435: // loaded system packages. duke@435: // Note: The package names returned are "/"-separated and end with a duke@435: // trailing "/". duke@435: static objArrayOop get_system_packages(TRAPS); duke@435: duke@435: // Initialization duke@435: static void initialize(); duke@435: static void create_package_info_table(); duke@435: static void create_package_info_table(HashtableBucket *t, int length, duke@435: int number_of_entries); duke@435: static int compute_Object_vtable(); duke@435: duke@435: static ClassPathEntry* classpath_entry(int n) { duke@435: ClassPathEntry* e = ClassLoader::_first_entry; duke@435: while (--n >= 0) { duke@435: assert(e != NULL, "Not that many classpath entries."); duke@435: e = e->next(); duke@435: } duke@435: return e; duke@435: } duke@435: duke@435: // Sharing dump and restore duke@435: static void copy_package_info_buckets(char** top, char* end); duke@435: static void copy_package_info_table(char** top, char* end); duke@435: duke@435: // VM monitoring and management support duke@435: static jlong classloader_time_ms(); duke@435: static jlong class_method_total_size(); duke@435: static jlong class_init_count(); duke@435: static jlong class_init_time_ms(); duke@435: static jlong class_verify_time_ms(); duke@435: static jlong class_link_count(); duke@435: static jlong class_link_time_ms(); duke@435: duke@435: // indicates if class path already contains a entry (exact match by name) duke@435: static bool contains_entry(ClassPathEntry* entry); duke@435: duke@435: // adds a class path list duke@435: static void add_to_list(ClassPathEntry* new_entry); duke@435: duke@435: // creates a class path zip entry (returns NULL if JAR file cannot be opened) duke@435: static ClassPathZipEntry* create_class_path_zip_entry(const char *apath); duke@435: duke@435: // Debugging duke@435: static void verify() PRODUCT_RETURN; duke@435: duke@435: // Force compilation of all methods in all classes in bootstrap class path (stress test) duke@435: #ifndef PRODUCT duke@435: private: duke@435: static int _compile_the_world_counter; duke@435: public: duke@435: static void compile_the_world(); duke@435: static void compile_the_world_in(char* name, Handle loader, TRAPS); duke@435: static int compile_the_world_counter() { return _compile_the_world_counter; } duke@435: #endif //PRODUCT duke@435: }; mchung@1310: mchung@1310: // PerfClassTraceTime is used to measure time for class loading related events. mchung@1310: // This class tracks cumulative time and exclusive time for specific event types. mchung@1310: // During the execution of one event, other event types (e.g. class loading and mchung@1310: // resolution) as well as recursive calls of the same event type could happen. mchung@1310: // Only one elapsed timer (cumulative) and one thread-local self timer (exclusive) mchung@1310: // (i.e. only one event type) are active at a time even multiple PerfClassTraceTime mchung@1310: // instances have been created as multiple events are happening. mchung@1310: class PerfClassTraceTime { mchung@1310: public: mchung@1310: enum { mchung@1310: CLASS_LOAD = 0, mchung@1310: PARSE_CLASS = 1, mchung@1310: CLASS_LINK = 2, mchung@1310: CLASS_VERIFY = 3, mchung@1310: CLASS_CLINIT = 4, mchung@1310: DEFINE_CLASS = 5, mchung@1310: EVENT_TYPE_COUNT = 6 mchung@1310: }; mchung@1310: protected: mchung@1310: // _t tracks time from initialization to destruction of this timer instance mchung@1310: // including time for all other event types, and recursive calls of this type. mchung@1310: // When a timer is called recursively, the elapsedTimer _t would not be used. mchung@1310: elapsedTimer _t; mchung@1310: PerfLongCounter* _timep; mchung@1310: PerfLongCounter* _selftimep; mchung@1310: PerfLongCounter* _eventp; mchung@1310: // pointer to thread-local recursion counter and timer array mchung@1310: // The thread_local timers track cumulative time for specific event types mchung@1310: // exclusive of time for other event types, but including recursive calls mchung@1310: // of the same type. mchung@1310: int* _recursion_counters; mchung@1310: elapsedTimer* _timers; mchung@1310: int _event_type; mchung@1310: int _prev_active_event; mchung@1310: mchung@1310: public: mchung@1310: mchung@1310: inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */ mchung@1310: PerfLongCounter* selftimep, /* counter incremented with exclusive time */ mchung@1310: PerfLongCounter* eventp, /* event counter */ mchung@1310: int* recursion_counters, /* thread-local recursion counter array */ mchung@1310: elapsedTimer* timers, /* thread-local timer array */ mchung@1310: int type /* event type */ ) : mchung@1310: _timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) { mchung@1310: initialize(); mchung@1310: } mchung@1310: mchung@1310: inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */ mchung@1310: elapsedTimer* timers, /* thread-local timer array */ mchung@1310: int type /* event type */ ) : mchung@1310: _timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) { mchung@1310: initialize(); mchung@1310: } mchung@1310: mchung@1310: void initialize() { mchung@1310: if (!UsePerfData) return; mchung@1310: mchung@1310: if (_eventp != NULL) { mchung@1310: // increment the event counter mchung@1310: _eventp->inc(); mchung@1310: } mchung@1310: mchung@1310: // stop the current active thread-local timer to measure inclusive time mchung@1310: _prev_active_event = -1; mchung@1310: for (int i=0; i < EVENT_TYPE_COUNT; i++) { mchung@1310: if (_timers[i].is_active()) { mchung@1310: assert(_prev_active_event == -1, "should have only one active timer"); mchung@1310: _prev_active_event = i; mchung@1310: _timers[i].stop(); mchung@1310: } mchung@1310: } mchung@1310: mchung@1310: if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) { mchung@1310: // start the inclusive timer if not recursively called mchung@1310: _t.start(); mchung@1310: } mchung@1310: mchung@1310: // start thread-local timer of the given event type mchung@1310: if (!_timers[_event_type].is_active()) { mchung@1310: _timers[_event_type].start(); mchung@1310: } mchung@1310: } mchung@1310: mchung@1310: inline void suspend() { _t.stop(); _timers[_event_type].stop(); } mchung@1310: inline void resume() { _t.start(); _timers[_event_type].start(); } mchung@1310: mchung@1310: ~PerfClassTraceTime() { mchung@1310: if (!UsePerfData) return; mchung@1310: mchung@1310: // stop the thread-local timer as the event completes mchung@1310: // and resume the thread-local timer of the event next on the stack mchung@1310: _timers[_event_type].stop(); mchung@1310: jlong selftime = _timers[_event_type].ticks(); mchung@1310: mchung@1310: if (_prev_active_event >= 0) { mchung@1310: _timers[_prev_active_event].start(); mchung@1310: } mchung@1310: mchung@1310: if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return; mchung@1310: mchung@1310: // increment the counters only on the leaf call mchung@1310: _t.stop(); mchung@1310: _timep->inc(_t.ticks()); mchung@1310: if (_selftimep != NULL) { mchung@1310: _selftimep->inc(selftime); mchung@1310: } mchung@1310: // add all class loading related event selftime to the accumulated time counter mchung@1310: ClassLoader::perf_accumulated_time()->inc(selftime); mchung@1310: mchung@1310: // reset the timer mchung@1310: _timers[_event_type].reset(); mchung@1310: } mchung@1310: }; mchung@1310: