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