131 PerfCounter* ClassLoader::_perf_define_appclass_time = NULL;
132 PerfCounter* ClassLoader::_perf_define_appclass_selftime = NULL;
133 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = NULL;
134 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;
135 PerfCounter* ClassLoader::_sync_systemLoaderLockContentionRate = NULL;
136 PerfCounter* ClassLoader::_sync_nonSystemLoaderLockContentionRate = NULL;
137 PerfCounter* ClassLoader::_sync_JVMFindLoadedClassLockFreeCounter = NULL;
138 PerfCounter* ClassLoader::_sync_JVMDefineClassLockFreeCounter = NULL;
139 PerfCounter* ClassLoader::_sync_JNIDefineClassLockFreeCounter = NULL;
140 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;
141 PerfCounter* ClassLoader::_load_instance_class_failCounter = NULL;
142
143 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL;
144 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = NULL;
145 ClassPathEntry* ClassLoader::_jrt_entry = NULL;
146 ClassPathEntry* ClassLoader::_first_append_entry = NULL;
147 ClassPathEntry* ClassLoader::_last_append_entry = NULL;
148 #if INCLUDE_CDS
149 ClassPathEntry* ClassLoader::_app_classpath_entries = NULL;
150 ClassPathEntry* ClassLoader::_last_app_classpath_entry = NULL;
151 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
152 #endif
153
154 // helper routines
155 bool string_starts_with(const char* str, const char* str_to_find) {
156 size_t str_len = strlen(str);
157 size_t str_to_find_len = strlen(str_to_find);
158 if (str_to_find_len > str_len) {
159 return false;
160 }
161 return (strncmp(str, str_to_find, str_to_find_len) == 0);
162 }
163
164 static const char* get_jimage_version_string() {
165 static char version_string[10] = "";
166 if (version_string[0] == '\0') {
167 jio_snprintf(version_string, sizeof(version_string), "%d.%d",
168 Abstract_VM_Version::vm_major_version(), Abstract_VM_Version::vm_minor_version());
169 }
170 return (const char*)version_string;
704 for (int start = 0; start < len; start = end) {
705 while (class_path[end] && class_path[end] != os::path_separator()[0]) {
706 end++;
707 }
708 EXCEPTION_MARK;
709 ResourceMark rm(THREAD);
710 char* path = NEW_RESOURCE_ARRAY(char, end - start + 1);
711 strncpy(path, &class_path[start], end - start);
712 path[end - start] = '\0';
713
714 check_shared_classpath(path);
715
716 update_class_path_entry_list(path, false, false);
717
718 while (class_path[end] == os::path_separator()[0]) {
719 end++;
720 }
721 }
722 }
723
724 #endif
725
726 // Construct the array of module/path pairs as specified to --patch-module
727 // for the boot loader to search ahead of the jimage, if the class being
728 // loaded is defined to a module that has been specified to --patch-module.
729 void ClassLoader::setup_patch_mod_entries() {
730 Thread* THREAD = Thread::current();
731 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
732 int num_of_entries = patch_mod_args->length();
733
734
735 // Set up the boot loader's _patch_mod_entries list
736 _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true);
737
738 for (int i = 0; i < num_of_entries; i++) {
739 const char* module_name = (patch_mod_args->at(i))->module_name();
740 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK);
741 assert(module_sym != NULL, "Failed to obtain Symbol for module name");
742 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
743
744 char* class_path = (patch_mod_args->at(i))->path_string();
1495 loader_data,
1496 protection_domain,
1497 NULL, // host_klass
1498 NULL, // cp_patches
1499 THREAD);
1500 if (HAS_PENDING_EXCEPTION) {
1501 if (DumpSharedSpaces) {
1502 tty->print_cr("Preload Error: Failed to load %s", class_name);
1503 }
1504 return NULL;
1505 }
1506
1507 if (!add_package(file_name, classpath_index, THREAD)) {
1508 return NULL;
1509 }
1510
1511 return result;
1512 }
1513
1514 #if INCLUDE_CDS
1515 static char* skip_uri_protocol(char* source) {
1516 if (strncmp(source, "file:", 5) == 0) {
1517 // file: protocol path could start with file:/ or file:///
1518 // locate the char after all the forward slashes
1519 int offset = 5;
1520 while (*(source + offset) == '/') {
1521 offset++;
1522 }
1523 source += offset;
1524 // for non-windows platforms, move back one char as the path begins with a '/'
1525 #ifndef _WINDOWS
1526 source -= 1;
1527 #endif
1528 } else if (strncmp(source, "jrt:/", 5) == 0) {
1529 source += 5;
1530 }
1531 return source;
1532 }
1533
1534 // Record the shared classpath index and loader type for classes loaded
1535 // by the builtin loaders at dump time.
1536 void ClassLoader::record_result(InstanceKlass* ik, const ClassFileStream* stream) {
1537 assert(DumpSharedSpaces, "sanity");
1538 assert(stream != NULL, "sanity");
1539
1540 if (ik->is_anonymous()) {
1541 // We do not archive anonymous classes.
1542 return;
1543 }
1544
1545 char* src = (char*)stream->source();
1546 if (src == NULL) {
1547 if (ik->class_loader() == NULL) {
1548 // JFR classes
1549 ik->set_shared_classpath_index(0);
1550 ik->set_class_loader_type(ClassLoader::BOOT_LOADER);
1551 }
1552 return;
1553 }
1554
1555 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1556
1557 ModuleEntry* module = ik->module();
1558 int classpath_index = -1;
1559 ResourceMark rm;
1560 char* canonical_path = NEW_RESOURCE_ARRAY(char, JVM_MAXPATHLEN);
1561
1562 // save the path from the file: protocol or the module name from the jrt: protocol
1563 // if no protocol prefix is found, path is the same as stream->source()
1564 char* path = skip_uri_protocol(src);
1565 for (int i = 0; i < FileMapInfo::get_number_of_share_classpaths(); i++) {
1566 SharedClassPathEntry* ent = FileMapInfo::shared_classpath(i);
1567 if (get_canonical_path(ent->name(), canonical_path, JVM_MAXPATHLEN)) {
1568 // If the path (from the class stream srouce) is the same as the shared
1569 // class path, then we have a match. For classes from module image loaded by the
1570 // PlatformClassLoader, the stream->source() is not the name of the module image.
1571 // Need to look for 'jrt:' explicitly.
1572 if (strcmp(canonical_path, os::native_path((char*)path)) == 0 ||
1573 (i == 0 && string_starts_with(src, "jrt:"))) {
1574 classpath_index = i;
1575 break;
1576 }
1577 }
1578 }
1579 if (classpath_index < 0) {
1580 // Shared classpath entry table only contains boot class path and -cp path.
1581 // No path entry found for this class. Must be a shared class loaded by the
1582 // user defined classloader.
1583 assert(ik->shared_classpath_index() < 0, "Sanity");
1584 return;
1585 }
1586
1587 const char* const class_name = ik->name()->as_C_string();
1588 const char* const file_name = file_name_for_class_name(class_name,
1589 ik->name()->utf8_length());
1590 assert(file_name != NULL, "invariant");
1591 Thread* THREAD = Thread::current();
1592 ClassLoaderExt::Context context(class_name, file_name, CATCH);
1593 context.record_result(ik->name(), classpath_index, ik, THREAD);
1594 }
1595 #endif // INCLUDE_CDS
1596
1597 // Initialize the class loader's access to methods in libzip. Parse and
1598 // process the boot classpath into a list ClassPathEntry objects. Once
1599 // this list has been created, it must not change order (see class PackageInfo)
1600 // it can be appended to and is by jvmti and the kernel vm.
1601
1602 void ClassLoader::initialize() {
1603 EXCEPTION_MARK;
1604
1605 if (UsePerfData) {
1606 // jvmstat performance counters
1607 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1608 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1609 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1610 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1611 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1654 }
1655
1656 // lookup zip library entry points
1657 load_zip_library();
1658 // lookup jimage library entry points
1659 load_jimage_library();
1660 #if INCLUDE_CDS
1661 // initialize search path
1662 if (DumpSharedSpaces) {
1663 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info();
1664 }
1665 #endif
1666 setup_bootstrap_search_path();
1667 }
1668
1669 #if INCLUDE_CDS
1670 void ClassLoader::initialize_shared_path() {
1671 if (DumpSharedSpaces) {
1672 ClassLoaderExt::setup_search_paths();
1673 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
1674 }
1675 }
1676 #endif
1677
1678 jlong ClassLoader::classloader_time_ms() {
1679 return UsePerfData ?
1680 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1681 }
1682
1683 jlong ClassLoader::class_init_count() {
1684 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1685 }
1686
1687 jlong ClassLoader::class_init_time_ms() {
1688 return UsePerfData ?
1689 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1690 }
1691
1692 jlong ClassLoader::class_verify_time_ms() {
1693 return UsePerfData ?
|
131 PerfCounter* ClassLoader::_perf_define_appclass_time = NULL;
132 PerfCounter* ClassLoader::_perf_define_appclass_selftime = NULL;
133 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = NULL;
134 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;
135 PerfCounter* ClassLoader::_sync_systemLoaderLockContentionRate = NULL;
136 PerfCounter* ClassLoader::_sync_nonSystemLoaderLockContentionRate = NULL;
137 PerfCounter* ClassLoader::_sync_JVMFindLoadedClassLockFreeCounter = NULL;
138 PerfCounter* ClassLoader::_sync_JVMDefineClassLockFreeCounter = NULL;
139 PerfCounter* ClassLoader::_sync_JNIDefineClassLockFreeCounter = NULL;
140 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;
141 PerfCounter* ClassLoader::_load_instance_class_failCounter = NULL;
142
143 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL;
144 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = NULL;
145 ClassPathEntry* ClassLoader::_jrt_entry = NULL;
146 ClassPathEntry* ClassLoader::_first_append_entry = NULL;
147 ClassPathEntry* ClassLoader::_last_append_entry = NULL;
148 #if INCLUDE_CDS
149 ClassPathEntry* ClassLoader::_app_classpath_entries = NULL;
150 ClassPathEntry* ClassLoader::_last_app_classpath_entry = NULL;
151 ClassPathEntry* ClassLoader::_module_path_entries = NULL;
152 ClassPathEntry* ClassLoader::_last_module_path_entry = NULL;
153 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL;
154 #endif
155
156 // helper routines
157 bool string_starts_with(const char* str, const char* str_to_find) {
158 size_t str_len = strlen(str);
159 size_t str_to_find_len = strlen(str_to_find);
160 if (str_to_find_len > str_len) {
161 return false;
162 }
163 return (strncmp(str, str_to_find, str_to_find_len) == 0);
164 }
165
166 static const char* get_jimage_version_string() {
167 static char version_string[10] = "";
168 if (version_string[0] == '\0') {
169 jio_snprintf(version_string, sizeof(version_string), "%d.%d",
170 Abstract_VM_Version::vm_major_version(), Abstract_VM_Version::vm_minor_version());
171 }
172 return (const char*)version_string;
706 for (int start = 0; start < len; start = end) {
707 while (class_path[end] && class_path[end] != os::path_separator()[0]) {
708 end++;
709 }
710 EXCEPTION_MARK;
711 ResourceMark rm(THREAD);
712 char* path = NEW_RESOURCE_ARRAY(char, end - start + 1);
713 strncpy(path, &class_path[start], end - start);
714 path[end - start] = '\0';
715
716 check_shared_classpath(path);
717
718 update_class_path_entry_list(path, false, false);
719
720 while (class_path[end] == os::path_separator()[0]) {
721 end++;
722 }
723 }
724 }
725
726 void ClassLoader::add_to_module_path_entries(const char* path,
727 ClassPathEntry* entry) {
728 assert(entry != NULL, "ClassPathEntry should not be NULL");
729 assert(DumpSharedSpaces, "dump time only");
730
731 // The entry does not exist, add to the list
732 if (_module_path_entries == NULL) {
733 assert(_last_module_path_entry == NULL, "Sanity");
734 _module_path_entries = _last_module_path_entry = entry;
735 } else {
736 _last_module_path_entry->set_next(entry);
737 _last_module_path_entry = entry;
738 }
739 }
740
741 // Add a module path to the _module_path_entries list.
742 void ClassLoader::update_module_path_entry_list(const char *path,
743 bool throw_exception) {
744 assert(DumpSharedSpaces, "dump time only");
745 struct stat st;
746 int ret = os::stat(path, &st);
747 assert(ret == 0, "module path must exist");
748 // File or directory found
749 ClassPathEntry* new_entry = NULL;
750 Thread* THREAD = Thread::current();
751 new_entry = create_class_path_entry(path, &st, throw_exception,
752 false /*is_boot_append */, CHECK);
753 if (new_entry == NULL) {
754 return;
755 }
756
757 add_to_module_path_entries(path, new_entry);
758 return;
759 }
760
761 void ClassLoader::setup_module_search_path(const char* path) {
762 check_shared_classpath(path);
763 update_module_path_entry_list(path);
764 }
765 #endif // INCLUDE_CDS
766
767 // Construct the array of module/path pairs as specified to --patch-module
768 // for the boot loader to search ahead of the jimage, if the class being
769 // loaded is defined to a module that has been specified to --patch-module.
770 void ClassLoader::setup_patch_mod_entries() {
771 Thread* THREAD = Thread::current();
772 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
773 int num_of_entries = patch_mod_args->length();
774
775
776 // Set up the boot loader's _patch_mod_entries list
777 _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true);
778
779 for (int i = 0; i < num_of_entries; i++) {
780 const char* module_name = (patch_mod_args->at(i))->module_name();
781 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK);
782 assert(module_sym != NULL, "Failed to obtain Symbol for module name");
783 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
784
785 char* class_path = (patch_mod_args->at(i))->path_string();
1536 loader_data,
1537 protection_domain,
1538 NULL, // host_klass
1539 NULL, // cp_patches
1540 THREAD);
1541 if (HAS_PENDING_EXCEPTION) {
1542 if (DumpSharedSpaces) {
1543 tty->print_cr("Preload Error: Failed to load %s", class_name);
1544 }
1545 return NULL;
1546 }
1547
1548 if (!add_package(file_name, classpath_index, THREAD)) {
1549 return NULL;
1550 }
1551
1552 return result;
1553 }
1554
1555 #if INCLUDE_CDS
1556 char* ClassLoader::skip_uri_protocol(char* source) {
1557 if (strncmp(source, "file:", 5) == 0) {
1558 // file: protocol path could start with file:/ or file:///
1559 // locate the char after all the forward slashes
1560 int offset = 5;
1561 while (*(source + offset) == '/') {
1562 offset++;
1563 }
1564 source += offset;
1565 // for non-windows platforms, move back one char as the path begins with a '/'
1566 #ifndef _WINDOWS
1567 source -= 1;
1568 #endif
1569 } else if (strncmp(source, "jrt:/", 5) == 0) {
1570 source += 5;
1571 }
1572 return source;
1573 }
1574
1575 // Record the shared classpath index and loader type for classes loaded
1576 // by the builtin loaders at dump time.
1577 void ClassLoader::record_result(InstanceKlass* ik, const ClassFileStream* stream) {
1578 assert(DumpSharedSpaces, "sanity");
1579 assert(stream != NULL, "sanity");
1580
1581 if (ik->is_anonymous()) {
1582 // We do not archive anonymous classes.
1583 return;
1584 }
1585
1586 oop loader = ik->class_loader();
1587 char* src = (char*)stream->source();
1588 if (src == NULL) {
1589 if (loader == NULL) {
1590 // JFR classes
1591 ik->set_shared_classpath_index(0);
1592 ik->set_class_loader_type(ClassLoader::BOOT_LOADER);
1593 }
1594 return;
1595 }
1596
1597 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1598
1599 ResourceMark rm;
1600 Thread* THREAD = Thread::current();
1601 int classpath_index = -1;
1602 PackageEntry* pkg_entry = ik->package();
1603
1604 if (FileMapInfo::get_number_of_shared_paths() > 0) {
1605 char* canonical_path = NEW_RESOURCE_ARRAY(char, JVM_MAXPATHLEN);
1606
1607 // save the path from the file: protocol or the module name from the jrt: protocol
1608 // if no protocol prefix is found, path is the same as stream->source()
1609 char* path = skip_uri_protocol(src);
1610 for (int i = 0; i < FileMapInfo::get_number_of_shared_paths(); i++) {
1611 SharedClassPathEntry* ent = FileMapInfo::shared_path(i);
1612 if (get_canonical_path(ent->name(), canonical_path, JVM_MAXPATHLEN)) {
1613 // If the path (from the class stream source) is the same as the shared
1614 // class or module path, then we have a match.
1615 if (strcmp(canonical_path, os::native_path((char*)path)) == 0) {
1616 // NULL pkg_entry and pkg_entry in an unnamed module implies the class
1617 // is from the -cp or -Xbootclasspath/a.
1618 if ((pkg_entry == NULL) || (pkg_entry->in_unnamed_module())) {
1619 // Ensure the index is within the -cp range before assigning
1620 // to the classpath_index.
1621 if (SystemDictionary::is_system_class_loader(loader) &&
1622 (i >= ClassLoaderExt::app_class_paths_start_index()) &&
1623 (i < ClassLoaderExt::app_module_paths_start_index())) {
1624 classpath_index = i;
1625 break;
1626 } else {
1627 if ((i >= 1) &&
1628 (i < ClassLoaderExt::app_class_paths_start_index())) {
1629 // The class must be from -Xbootclasspath/a
1630 assert(loader == NULL, "sanity");
1631 classpath_index = i;
1632 break;
1633 }
1634 }
1635 } else {
1636 // A class from a named module from the --module-path. Ensure the index is
1637 // within the --module-path range before assigning to the classpath_index.
1638 if ((pkg_entry != NULL) && !(pkg_entry->in_unnamed_module()) && (i > 0)) {
1639 if (i >= ClassLoaderExt::app_module_paths_start_index() &&
1640 i < FileMapInfo::get_number_of_shared_paths()) {
1641 classpath_index = i;
1642 break;
1643 }
1644 }
1645 }
1646 }
1647 // for index 0 and the stream->source() is the modules image or has the jrt: protocol.
1648 // The class must be from the runtime modules image.
1649 if (i == 0 && (is_modules_image(src) || string_starts_with(src, "jrt:"))) {
1650 classpath_index = i;
1651 break;
1652 }
1653 }
1654 }
1655
1656 // No path entry found for this class. Must be a shared class loaded by the
1657 // user defined classloader.
1658 if (classpath_index < 0) {
1659 assert(ik->shared_classpath_index() < 0, "Sanity");
1660 return;
1661 }
1662 } else {
1663 // The shared path table is set up after module system initialization.
1664 // The path table contains no entry before that. Any classes loaded prior
1665 // to the setup of the shared path table must be from the modules image.
1666 assert(is_modules_image(src), "stream must be from modules image");
1667 assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");
1668 classpath_index = 0;
1669 }
1670
1671 const char* const class_name = ik->name()->as_C_string();
1672 const char* const file_name = file_name_for_class_name(class_name,
1673 ik->name()->utf8_length());
1674 assert(file_name != NULL, "invariant");
1675
1676 ClassLoaderExt::Context context(class_name, file_name, CATCH);
1677 context.record_result(ik->name(), classpath_index, ik, THREAD);
1678 }
1679 #endif // INCLUDE_CDS
1680
1681 // Initialize the class loader's access to methods in libzip. Parse and
1682 // process the boot classpath into a list ClassPathEntry objects. Once
1683 // this list has been created, it must not change order (see class PackageInfo)
1684 // it can be appended to and is by jvmti and the kernel vm.
1685
1686 void ClassLoader::initialize() {
1687 EXCEPTION_MARK;
1688
1689 if (UsePerfData) {
1690 // jvmstat performance counters
1691 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1692 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1693 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1694 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1695 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1738 }
1739
1740 // lookup zip library entry points
1741 load_zip_library();
1742 // lookup jimage library entry points
1743 load_jimage_library();
1744 #if INCLUDE_CDS
1745 // initialize search path
1746 if (DumpSharedSpaces) {
1747 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info();
1748 }
1749 #endif
1750 setup_bootstrap_search_path();
1751 }
1752
1753 #if INCLUDE_CDS
1754 void ClassLoader::initialize_shared_path() {
1755 if (DumpSharedSpaces) {
1756 ClassLoaderExt::setup_search_paths();
1757 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check()
1758 }
1759 }
1760
1761 void ClassLoader::initialize_module_path() {
1762 if (DumpSharedSpaces) {
1763 ClassLoaderExt::setup_module_paths();
1764 FileMapInfo::allocate_shared_path_table();
1765 }
1766 }
1767 #endif
1768
1769 jlong ClassLoader::classloader_time_ms() {
1770 return UsePerfData ?
1771 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1772 }
1773
1774 jlong ClassLoader::class_init_count() {
1775 return UsePerfData ? _perf_classes_inited->get_value() : -1;
1776 }
1777
1778 jlong ClassLoader::class_init_time_ms() {
1779 return UsePerfData ?
1780 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1781 }
1782
1783 jlong ClassLoader::class_verify_time_ms() {
1784 return UsePerfData ?
|