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