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