Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/classLoader.hpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_CLASSFILE_CLASSLOADER_HPP25#define SHARE_VM_CLASSFILE_CLASSLOADER_HPP2627#include "classfile/classFileParser.hpp"28#include "runtime/perfData.hpp"2930// The VM class loader.31#include <sys/stat.h>323334// Meta-index (optional, to be able to skip opening boot classpath jar files)35class MetaIndex: public CHeapObj<mtClass> {36private:37char** _meta_package_names;38int _num_meta_package_names;39public:40MetaIndex(char** meta_package_names, int num_meta_package_names);41~MetaIndex();42bool may_contain(const char* class_name);43};444546// Class path entry (directory or zip file)4748class ClassPathEntry: public CHeapObj<mtClass> {49private:50ClassPathEntry* _next;51public:52// Next entry in class path53ClassPathEntry* next() { return _next; }54void set_next(ClassPathEntry* next) {55// may have unlocked readers, so write atomically.56OrderAccess::release_store_ptr(&_next, next);57}58virtual bool is_jar_file() = 0;59virtual const char* name() = 0;60virtual bool is_lazy();61// Constructor62ClassPathEntry();63// Attempt to locate file_name through this class path entry.64// Returns a class file parsing stream if successfull.65virtual ClassFileStream* open_stream(const char* name, TRAPS) = 0;66// Debugging67NOT_PRODUCT(virtual void compile_the_world(Handle loader, TRAPS) = 0;)68NOT_PRODUCT(virtual bool is_rt_jar() = 0;)69};707172class ClassPathDirEntry: public ClassPathEntry {73private:74const char* _dir; // Name of directory75public:76bool is_jar_file() { return false; }77const char* name() { return _dir; }78ClassPathDirEntry(const char* dir);79ClassFileStream* open_stream(const char* name, TRAPS);80// Debugging81NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)82NOT_PRODUCT(bool is_rt_jar();)83};848586// Type definitions for zip file and zip file entry87typedef void* jzfile;88typedef struct {89char *name; /* entry name */90jlong time; /* modification time */91jlong size; /* size of uncompressed data */92jlong csize; /* size of compressed data (zero if uncompressed) */93jint crc; /* crc of uncompressed data */94char *comment; /* optional zip file comment */95jbyte *extra; /* optional extra data */96jlong pos; /* position of LOC header (if negative) or data */97} jzentry;9899100class ClassPathZipEntry: public ClassPathEntry {101private:102jzfile* _zip; // The zip archive103const char* _zip_name; // Name of zip archive104public:105bool is_jar_file() { return true; }106const char* name() { return _zip_name; }107ClassPathZipEntry(jzfile* zip, const char* zip_name);108~ClassPathZipEntry();109u1* open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS);110ClassFileStream* open_stream(const char* name, TRAPS);111void contents_do(void f(const char* name, void* context), void* context);112// Debugging113NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)114NOT_PRODUCT(void compile_the_world12(Handle loader, TRAPS);) // JDK 1.2 version115NOT_PRODUCT(void compile_the_world13(Handle loader, TRAPS);) // JDK 1.3 version116NOT_PRODUCT(bool is_rt_jar();)117NOT_PRODUCT(bool is_rt_jar12();)118NOT_PRODUCT(bool is_rt_jar13();)119};120121122// For lazier loading of boot class path entries123class LazyClassPathEntry: public ClassPathEntry {124private:125const char* _path; // dir or file126struct stat _st;127MetaIndex* _meta_index;128bool _has_error;129bool _throw_exception;130volatile ClassPathEntry* _resolved_entry;131public:132ClassPathEntry* resolve_entry(TRAPS);133bool is_jar_file();134const char* name() { return _path; }135LazyClassPathEntry(const char* path, const struct stat* st, bool throw_exception);136u1* open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS);137ClassFileStream* open_stream(const char* name, TRAPS);138void set_meta_index(MetaIndex* meta_index) { _meta_index = meta_index; }139virtual bool is_lazy();140// Debugging141NOT_PRODUCT(void compile_the_world(Handle loader, TRAPS);)142NOT_PRODUCT(bool is_rt_jar();)143};144145class PackageHashtable;146class PackageInfo;147class SharedPathsMiscInfo;148template <MEMFLAGS F> class HashtableBucket;149150class ClassLoader: AllStatic {151public:152enum SomeConstants {153package_hash_table_size = 31 // Number of buckets154};155protected:156friend class LazyClassPathEntry;157158// Performance counters159static PerfCounter* _perf_accumulated_time;160static PerfCounter* _perf_classes_inited;161static PerfCounter* _perf_class_init_time;162static PerfCounter* _perf_class_init_selftime;163static PerfCounter* _perf_classes_verified;164static PerfCounter* _perf_class_verify_time;165static PerfCounter* _perf_class_verify_selftime;166static PerfCounter* _perf_classes_linked;167static PerfCounter* _perf_class_link_time;168static PerfCounter* _perf_class_link_selftime;169static PerfCounter* _perf_class_parse_time;170static PerfCounter* _perf_class_parse_selftime;171static PerfCounter* _perf_sys_class_lookup_time;172static PerfCounter* _perf_shared_classload_time;173static PerfCounter* _perf_sys_classload_time;174static PerfCounter* _perf_app_classload_time;175static PerfCounter* _perf_app_classload_selftime;176static PerfCounter* _perf_app_classload_count;177static PerfCounter* _perf_define_appclasses;178static PerfCounter* _perf_define_appclass_time;179static PerfCounter* _perf_define_appclass_selftime;180static PerfCounter* _perf_app_classfile_bytes_read;181static PerfCounter* _perf_sys_classfile_bytes_read;182183static PerfCounter* _sync_systemLoaderLockContentionRate;184static PerfCounter* _sync_nonSystemLoaderLockContentionRate;185static PerfCounter* _sync_JVMFindLoadedClassLockFreeCounter;186static PerfCounter* _sync_JVMDefineClassLockFreeCounter;187static PerfCounter* _sync_JNIDefineClassLockFreeCounter;188189static PerfCounter* _unsafe_defineClassCallCounter;190static PerfCounter* _isUnsyncloadClass;191static PerfCounter* _load_instance_class_failCounter;192193// First entry in linked list of ClassPathEntry instances194static ClassPathEntry* _first_entry;195// Last entry in linked list of ClassPathEntry instances196static ClassPathEntry* _last_entry;197static int _num_entries;198199// Hash table used to keep track of loaded packages200static PackageHashtable* _package_hash_table;201static const char* _shared_archive;202203// Info used by CDS204CDS_ONLY(static SharedPathsMiscInfo * _shared_paths_misc_info;)205206// Hash function207static unsigned int hash(const char *s, int n);208// Returns the package file name corresponding to the specified package209// or class name, or null if not found.210static PackageInfo* lookup_package(const char *pkgname);211// Adds a new package entry for the specified class or package name and212// corresponding directory or jar file name.213static bool add_package(const char *pkgname, int classpath_index, TRAPS);214215// Initialization216static void setup_bootstrap_meta_index();217static void setup_meta_index(const char* meta_index_path, const char* meta_index_dir,218int start_index);219static void setup_bootstrap_search_path();220static void setup_search_path(const char *class_path, bool canonicalize=false);221222static void load_zip_library();223static ClassPathEntry* create_class_path_entry(const char *path, const struct stat* st,224bool lazy, bool throw_exception, TRAPS);225226// Canonicalizes path names, so strcmp will work properly. This is mainly227// to avoid confusing the zip library228static bool get_canonical_path(const char* orig, char* out, int len);229public:230static int crc32(int crc, const char* buf, int len);231static bool update_class_path_entry_list(const char *path,232bool check_for_duplicates,233bool throw_exception=true);234static void print_bootclasspath();235236// Timing237static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; }238static PerfCounter* perf_classes_inited() { return _perf_classes_inited; }239static PerfCounter* perf_class_init_time() { return _perf_class_init_time; }240static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; }241static PerfCounter* perf_classes_verified() { return _perf_classes_verified; }242static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; }243static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; }244static PerfCounter* perf_classes_linked() { return _perf_classes_linked; }245static PerfCounter* perf_class_link_time() { return _perf_class_link_time; }246static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; }247static PerfCounter* perf_class_parse_time() { return _perf_class_parse_time; }248static PerfCounter* perf_class_parse_selftime() { return _perf_class_parse_selftime; }249static PerfCounter* perf_sys_class_lookup_time() { return _perf_sys_class_lookup_time; }250static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; }251static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; }252static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; }253static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; }254static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; }255static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; }256static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; }257static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }258static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }259static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }260261// Record how often system loader lock object is contended262static PerfCounter* sync_systemLoaderLockContentionRate() {263return _sync_systemLoaderLockContentionRate;264}265266// Record how often non system loader lock object is contended267static PerfCounter* sync_nonSystemLoaderLockContentionRate() {268return _sync_nonSystemLoaderLockContentionRate;269}270271// Record how many calls to JVM_FindLoadedClass w/o holding a lock272static PerfCounter* sync_JVMFindLoadedClassLockFreeCounter() {273return _sync_JVMFindLoadedClassLockFreeCounter;274}275276// Record how many calls to JVM_DefineClass w/o holding a lock277static PerfCounter* sync_JVMDefineClassLockFreeCounter() {278return _sync_JVMDefineClassLockFreeCounter;279}280281// Record how many calls to jni_DefineClass w/o holding a lock282static PerfCounter* sync_JNIDefineClassLockFreeCounter() {283return _sync_JNIDefineClassLockFreeCounter;284}285286// Record how many calls to Unsafe_DefineClass287static PerfCounter* unsafe_defineClassCallCounter() {288return _unsafe_defineClassCallCounter;289}290291// Record how many times SystemDictionary::load_instance_class call292// fails with linkageError when Unsyncloadclass flag is set.293static PerfCounter* load_instance_class_failCounter() {294return _load_instance_class_failCounter;295}296297// Load individual .class file298static instanceKlassHandle load_classfile(Symbol* h_name, TRAPS);299300// If the specified package has been loaded by the system, then returns301// the name of the directory or ZIP file that the package was loaded from.302// Returns null if the package was not loaded.303// Note: The specified name can either be the name of a class or package.304// If a package name is specified, then it must be "/"-separator and also305// end with a trailing "/".306static oop get_system_package(const char* name, TRAPS);307308// Returns an array of Java strings representing all of the currently309// loaded system packages.310// Note: The package names returned are "/"-separated and end with a311// trailing "/".312static objArrayOop get_system_packages(TRAPS);313314// Initialization315static void initialize();316CDS_ONLY(static void initialize_shared_path();)317static void create_package_info_table();318static void create_package_info_table(HashtableBucket<mtClass> *t, int length,319int number_of_entries);320static int compute_Object_vtable();321322static ClassPathEntry* classpath_entry(int n) {323ClassPathEntry* e = ClassLoader::_first_entry;324while (--n >= 0) {325assert(e != NULL, "Not that many classpath entries.");326e = e->next();327}328return e;329}330331static int num_classpath_entries() {332return _num_entries;333}334335#if INCLUDE_CDS336// Sharing dump and restore337static void copy_package_info_buckets(char** top, char* end);338static void copy_package_info_table(char** top, char* end);339340static void check_shared_classpath(const char *path);341static void finalize_shared_paths_misc_info();342static int get_shared_paths_misc_info_size();343static void* get_shared_paths_misc_info();344static bool check_shared_paths_misc_info(void* info, int size);345static void exit_with_path_failure(const char* error, const char* message);346#endif347348static void trace_class_path(outputStream* out, const char* msg, const char* name = NULL);349350// VM monitoring and management support351static jlong classloader_time_ms();352static jlong class_method_total_size();353static jlong class_init_count();354static jlong class_init_time_ms();355static jlong class_verify_time_ms();356static jlong class_link_count();357static jlong class_link_time_ms();358359// indicates if class path already contains a entry (exact match by name)360static bool contains_entry(ClassPathEntry* entry);361362// adds a class path list363static void add_to_list(ClassPathEntry* new_entry);364365// creates a class path zip entry (returns NULL if JAR file cannot be opened)366static ClassPathZipEntry* create_class_path_zip_entry(const char *apath);367368// obtain package name from a fully qualified class name369// *bad_class_name is set to true if there's a problem with parsing class_name, to370// distinguish from a class_name with no package name, as both cases have a NULL return value371static const char* package_from_name(const char* const class_name, bool* bad_class_name = NULL);372373// Debugging374static void verify() PRODUCT_RETURN;375376// Force compilation of all methods in all classes in bootstrap class path (stress test)377#ifndef PRODUCT378protected:379static int _compile_the_world_class_counter;380static int _compile_the_world_method_counter;381public:382static void compile_the_world();383static void compile_the_world_in(char* name, Handle loader, TRAPS);384static int compile_the_world_counter() { return _compile_the_world_class_counter; }385#endif //PRODUCT386};387388// PerfClassTraceTime is used to measure time for class loading related events.389// This class tracks cumulative time and exclusive time for specific event types.390// During the execution of one event, other event types (e.g. class loading and391// resolution) as well as recursive calls of the same event type could happen.392// Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)393// (i.e. only one event type) are active at a time even multiple PerfClassTraceTime394// instances have been created as multiple events are happening.395class PerfClassTraceTime {396public:397enum {398CLASS_LOAD = 0,399PARSE_CLASS = 1,400CLASS_LINK = 2,401CLASS_VERIFY = 3,402CLASS_CLINIT = 4,403DEFINE_CLASS = 5,404EVENT_TYPE_COUNT = 6405};406protected:407// _t tracks time from initialization to destruction of this timer instance408// including time for all other event types, and recursive calls of this type.409// When a timer is called recursively, the elapsedTimer _t would not be used.410elapsedTimer _t;411PerfLongCounter* _timep;412PerfLongCounter* _selftimep;413PerfLongCounter* _eventp;414// pointer to thread-local recursion counter and timer array415// The thread_local timers track cumulative time for specific event types416// exclusive of time for other event types, but including recursive calls417// of the same type.418int* _recursion_counters;419elapsedTimer* _timers;420int _event_type;421int _prev_active_event;422423public:424425inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */426PerfLongCounter* selftimep, /* counter incremented with exclusive time */427PerfLongCounter* eventp, /* event counter */428int* recursion_counters, /* thread-local recursion counter array */429elapsedTimer* timers, /* thread-local timer array */430int type /* event type */ ) :431_timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {432initialize();433}434435inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */436elapsedTimer* timers, /* thread-local timer array */437int type /* event type */ ) :438_timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) {439initialize();440}441442inline void suspend() { _t.stop(); _timers[_event_type].stop(); }443inline void resume() { _t.start(); _timers[_event_type].start(); }444445~PerfClassTraceTime();446void initialize();447};448449#endif // SHARE_VM_CLASSFILE_CLASSLOADER_HPP450451452