186 // create a new array of vtable_indices for default methods
187 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
188 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
189 assert(default_vtable_indices() == NULL, "only create once");
190 set_default_vtable_indices(vtable_indices);
191 return vtable_indices;
192 }
193
194 InstanceKlass::InstanceKlass(int vtable_len,
195 int itable_len,
196 int static_field_size,
197 int nonstatic_oop_map_size,
198 unsigned kind,
199 ReferenceType rt,
200 AccessFlags access_flags,
201 bool is_anonymous) {
202 No_Safepoint_Verifier no_safepoint; // until k becomes parsable
203
204 int iksize = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
205 access_flags.is_interface(), is_anonymous);
206
207 set_vtable_length(vtable_len);
208 set_itable_length(itable_len);
209 set_static_field_size(static_field_size);
210 set_nonstatic_oop_map_size(nonstatic_oop_map_size);
211 set_access_flags(access_flags);
212 _misc_flags = 0; // initialize to zero
213 set_kind(kind);
214 set_is_anonymous(is_anonymous);
215 assert(size() == iksize, "wrong size for object");
216
217 set_array_klasses(NULL);
218 set_methods(NULL);
219 set_method_ordering(NULL);
220 set_default_methods(NULL);
221 set_default_vtable_indices(NULL);
222 set_local_interfaces(NULL);
223 set_transitive_interfaces(NULL);
224 init_implementor();
225 set_fields(NULL, 0);
226 set_constants(NULL);
227 set_class_loader_data(NULL);
228 set_source_file_name_index(0);
229 set_source_debug_extension(NULL, 0);
230 set_array_name(NULL);
231 set_inner_classes(NULL);
232 set_static_oop_field_count(0);
233 set_nonstatic_field_size(0);
234 set_is_marked_dependent(false);
235 set_has_unloaded_dependent(false);
236 set_init_state(InstanceKlass::allocated);
237 set_init_thread(NULL);
238 set_reference_type(rt);
239 set_oop_map_cache(NULL);
240 set_jni_ids(NULL);
241 set_osr_nmethods_head(NULL);
242 set_breakpoints(NULL);
243 init_previous_versions();
244 set_generic_signature_index(0);
245 release_set_methods_jmethod_ids(NULL);
246 set_annotations(NULL);
247 set_jvmti_cached_class_field_map(NULL);
248 set_initial_method_idnum(0);
249 _dependencies = NULL;
250 set_jvmti_cached_class_field_map(NULL);
251 set_cached_class_file(NULL);
252 set_initial_method_idnum(0);
253 set_minor_version(0);
254 set_major_version(0);
255 NOT_PRODUCT(_verify_count = 0;)
256
257 // initialize the non-header words to zero
258 intptr_t* p = (intptr_t*)this;
259 for (int index = InstanceKlass::header_size(); index < iksize; index++) {
260 p[index] = NULL_WORD;
261 }
262
263 // Set temporary value until parseClassFile updates it with the real instance
264 // size.
265 set_layout_helper(Klass::instance_layout_helper(0, true));
266 }
267
268
269 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
1847 size_t idnum = (size_t)method->method_idnum();
1848 jmethodID* jmeths = methods_jmethod_ids_acquire();
1849 size_t length; // length assigned as debugging crumb
1850 jmethodID id = NULL;
1851 if (jmeths != NULL && // If there is a cache
1852 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
1853 id = jmeths[idnum+1]; // Look up the id (may be NULL)
1854 }
1855 return id;
1856 }
1857
1858 int nmethodBucket::decrement() {
1859 return Atomic::add(-1, (volatile int *)&_count);
1860 }
1861
1862 //
1863 // Walk the list of dependent nmethods searching for nmethods which
1864 // are dependent on the changes that were passed in and mark them for
1865 // deoptimization. Returns the number of nmethods found.
1866 //
1867 int nmethodBucket::mark_dependent_nmethods(nmethodBucket* deps, DepChange& changes) {
1868 assert_locked_or_safepoint(CodeCache_lock);
1869 int found = 0;
1870 for (nmethodBucket* b = deps; b != NULL; b = b->next()) {
1871 nmethod* nm = b->get_nmethod();
1872 // since dependencies aren't removed until an nmethod becomes a zombie,
1873 // the dependency list may contain nmethods which aren't alive.
1874 if (b->count() > 0 && nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) {
1875 if (TraceDependencies) {
1876 ResourceMark rm;
1877 tty->print_cr("Marked for deoptimization");
1878 changes.print();
1879 nm->print();
1880 nm->print_dependencies();
1881 }
1882 nm->mark_for_deoptimization();
1883 found++;
1884 }
1885 }
1886 return found;
1887 }
1888
1889 //
1890 // Add an nmethodBucket to the list of dependencies for this nmethod.
1891 // It's possible that an nmethod has multiple dependencies on this klass
1892 // so a count is kept for each bucket to guarantee that creation and
1893 // deletion of dependencies is consistent. Returns new head of the list.
1894 //
1895 nmethodBucket* nmethodBucket::add_dependent_nmethod(nmethodBucket* deps, nmethod* nm) {
1896 assert_locked_or_safepoint(CodeCache_lock);
1897 for (nmethodBucket* b = deps; b != NULL; b = b->next()) {
1898 if (nm == b->get_nmethod()) {
1899 b->increment();
1900 return deps;
1901 }
1902 }
1903 return new nmethodBucket(nm, deps);
1904 }
1905
1906 //
1907 // Decrement count of the nmethod in the dependency list and remove
1908 // the bucket completely when the count goes to 0. This method must
1909 // find a corresponding bucket otherwise there's a bug in the
1910 // recording of dependencies. Returns true if the bucket was deleted,
1911 // or marked ready for reclaimation.
1912 bool nmethodBucket::remove_dependent_nmethod(nmethodBucket** deps, nmethod* nm, bool delete_immediately) {
1913 assert_locked_or_safepoint(CodeCache_lock);
1914
1915 nmethodBucket* first = *deps;
1916 nmethodBucket* last = NULL;
1917
1918 for (nmethodBucket* b = first; b != NULL; b = b->next()) {
1919 if (nm == b->get_nmethod()) {
1920 int val = b->decrement();
1921 guarantee(val >= 0, "Underflow: %d", val);
1922 if (val == 0) {
1923 if (delete_immediately) {
1924 if (last == NULL) {
1925 *deps = b->next();
1926 } else {
1927 last->set_next(b->next());
1928 }
1929 delete b;
1930 }
1931 }
1932 return true;
1933 }
1934 last = b;
1935 }
1936
1937 #ifdef ASSERT
1938 tty->print_raw_cr("### can't find dependent nmethod");
1939 nm->print();
1940 #endif // ASSERT
1941 ShouldNotReachHere();
1942 return false;
1943 }
1944
1945 // Convenience overload, for callers that don't want to delete the nmethodBucket entry.
1946 bool nmethodBucket::remove_dependent_nmethod(nmethodBucket* deps, nmethod* nm) {
1947 nmethodBucket** deps_addr = &deps;
1948 return remove_dependent_nmethod(deps_addr, nm, false /* Don't delete */);
1949 }
1950
1951 //
1952 // Reclaim all unused buckets. Returns new head of the list.
1953 //
1954 nmethodBucket* nmethodBucket::clean_dependent_nmethods(nmethodBucket* deps) {
1955 nmethodBucket* first = deps;
1956 nmethodBucket* last = NULL;
1957 nmethodBucket* b = first;
1958
1959 while (b != NULL) {
1960 assert(b->count() >= 0, "bucket count: %d", b->count());
1961 nmethodBucket* next = b->next();
1962 if (b->count() == 0) {
1963 if (last == NULL) {
1964 first = next;
1965 } else {
1966 last->set_next(next);
1967 }
1968 delete b;
1969 // last stays the same.
1970 } else {
1971 last = b;
1972 }
1973 b = next;
1974 }
1975 return first;
1976 }
1977
1978 #ifndef PRODUCT
1979 void nmethodBucket::print_dependent_nmethods(nmethodBucket* deps, bool verbose) {
1980 int idx = 0;
1981 for (nmethodBucket* b = deps; b != NULL; b = b->next()) {
1982 nmethod* nm = b->get_nmethod();
1983 tty->print("[%d] count=%d { ", idx++, b->count());
1984 if (!verbose) {
1985 nm->print_on(tty, "nmethod");
1986 tty->print_cr(" } ");
1987 } else {
1988 nm->print();
1989 nm->print_dependencies();
1990 tty->print_cr("--- } ");
1991 }
1992 }
1993 }
1994
1995 bool nmethodBucket::is_dependent_nmethod(nmethodBucket* deps, nmethod* nm) {
1996 for (nmethodBucket* b = deps; b != NULL; b = b->next()) {
1997 if (nm == b->get_nmethod()) {
1998 #ifdef ASSERT
1999 int count = b->count();
2000 assert(count >= 0, "count shouldn't be negative: %d", count);
2001 #endif
2002 return true;
2003 }
2004 }
2005 return false;
2006 }
2007 #endif //PRODUCT
2008
2009 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) {
2010 assert_locked_or_safepoint(CodeCache_lock);
2011 return nmethodBucket::mark_dependent_nmethods(_dependencies, changes);
2012 }
2013
2014 void InstanceKlass::clean_dependent_nmethods() {
2015 assert_locked_or_safepoint(CodeCache_lock);
2016
2017 if (has_unloaded_dependent()) {
2018 _dependencies = nmethodBucket::clean_dependent_nmethods(_dependencies);
2019 set_has_unloaded_dependent(false);
2020 }
2021 #ifdef ASSERT
2022 else {
2023 // Verification
2024 for (nmethodBucket* b = _dependencies; b != NULL; b = b->next()) {
2025 assert(b->count() >= 0, "bucket count: %d", b->count());
2026 assert(b->count() != 0, "empty buckets need to be cleaned");
2027 }
2028 }
2029 #endif
2030 }
2031
2032 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2033 assert_locked_or_safepoint(CodeCache_lock);
2034 _dependencies = nmethodBucket::add_dependent_nmethod(_dependencies, nm);
2035 }
2036
2037 void InstanceKlass::remove_dependent_nmethod(nmethod* nm, bool delete_immediately) {
2038 assert_locked_or_safepoint(CodeCache_lock);
2039
2040 if (nmethodBucket::remove_dependent_nmethod(&_dependencies, nm, delete_immediately)) {
2041 set_has_unloaded_dependent(true);
2042 }
2043 }
2044
2045 #ifndef PRODUCT
2046 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2047 nmethodBucket::print_dependent_nmethods(_dependencies, verbose);
2048 }
2049
2050 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2051 return nmethodBucket::is_dependent_nmethod(_dependencies, nm);
2052 }
2053 #endif //PRODUCT
2054
2055 void InstanceKlass::clean_weak_instanceklass_links(BoolObjectClosure* is_alive) {
2056 clean_implementors_list(is_alive);
2057 clean_method_data(is_alive);
2058
2059 clean_dependent_nmethods();
2060 }
2061
2062 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) {
2063 assert(class_loader_data()->is_alive(is_alive), "this klass should be live");
2064 if (is_interface()) {
2065 if (ClassUnloading) {
2066 Klass* impl = implementor();
2067 if (impl != NULL) {
2068 if (!impl->is_loader_alive(is_alive)) {
2069 // remove this guy
2070 Klass** klass = adr_implementor();
2071 assert(klass != NULL, "null klass");
2072 if (klass != NULL) {
2073 *klass = NULL;
2074 }
2075 }
2076 }
2077 }
2078 }
2079 }
2086 }
2087 }
2088 }
2089
2090
2091 static void remove_unshareable_in_class(Klass* k) {
2092 // remove klass's unshareable info
2093 k->remove_unshareable_info();
2094 }
2095
2096 void InstanceKlass::remove_unshareable_info() {
2097 Klass::remove_unshareable_info();
2098 // Unlink the class
2099 if (is_linked()) {
2100 unlink_class();
2101 }
2102 init_implementor();
2103
2104 constants()->remove_unshareable_info();
2105
2106 for (int i = 0; i < methods()->length(); i++) {
2107 Method* m = methods()->at(i);
2108 m->remove_unshareable_info();
2109 }
2110
2111 // do array classes also.
2112 array_klasses_do(remove_unshareable_in_class);
2113 }
2114
2115 static void restore_unshareable_in_class(Klass* k, TRAPS) {
2116 // Array classes have null protection domain.
2117 // --> see ArrayKlass::complete_create_array_klass()
2118 k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
2119 }
2120
2121 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2122 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2123 instanceKlassHandle ik(THREAD, this);
2124
2125 Array<Method*>* methods = ik->methods();
2215 set_jni_ids(NULL);
2216
2217 jmethodID* jmeths = methods_jmethod_ids_acquire();
2218 if (jmeths != (jmethodID*)NULL) {
2219 release_set_methods_jmethod_ids(NULL);
2220 FreeHeap(jmeths);
2221 }
2222
2223 // Deallocate MemberNameTable
2224 {
2225 Mutex* lock_or_null = SafepointSynchronize::is_at_safepoint() ? NULL : MemberNameTable_lock;
2226 MutexLockerEx ml(lock_or_null, Mutex::_no_safepoint_check_flag);
2227 MemberNameTable* mnt = member_names();
2228 if (mnt != NULL) {
2229 delete mnt;
2230 set_member_names(NULL);
2231 }
2232 }
2233
2234 // release dependencies
2235 nmethodBucket* b = _dependencies;
2236 _dependencies = NULL;
2237 while (b != NULL) {
2238 nmethodBucket* next = b->next();
2239 delete b;
2240 b = next;
2241 }
2242
2243 // Deallocate breakpoint records
2244 if (breakpoints() != 0x0) {
2245 methods_do(clear_all_breakpoints);
2246 assert(breakpoints() == 0x0, "should have cleared breakpoints");
2247 }
2248
2249 // deallocate the cached class file
2250 if (_cached_class_file != NULL) {
2251 os::free(_cached_class_file);
2252 _cached_class_file = NULL;
2253 }
2254
2255 // Decrement symbol reference counts associated with the unloaded class.
2256 if (_name != NULL) _name->decrement_refcount();
2257 // unreference array name derived from this class name (arrays of an unloaded
2258 // class can't be referenced anymore).
2259 if (_array_name != NULL) _array_name->decrement_refcount();
2260 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension);
3563 return NULL; // The version of klass is gone, no method is found
3564 }
3565 Method* method = holder->method_with_orig_idnum(idnum);
3566 return method;
3567 }
3568
3569
3570 jint InstanceKlass::get_cached_class_file_len() {
3571 return VM_RedefineClasses::get_cached_class_file_len(_cached_class_file);
3572 }
3573
3574 unsigned char * InstanceKlass::get_cached_class_file_bytes() {
3575 return VM_RedefineClasses::get_cached_class_file_bytes(_cached_class_file);
3576 }
3577
3578
3579 /////////////// Unit tests ///////////////
3580
3581 #ifndef PRODUCT
3582
3583 class TestNmethodBucketContext {
3584 public:
3585 nmethod* _nmethodLast;
3586 nmethod* _nmethodMiddle;
3587 nmethod* _nmethodFirst;
3588
3589 nmethodBucket* _bucketLast;
3590 nmethodBucket* _bucketMiddle;
3591 nmethodBucket* _bucketFirst;
3592
3593 nmethodBucket* _bucketList;
3594
3595 TestNmethodBucketContext() {
3596 CodeCache_lock->lock_without_safepoint_check();
3597
3598 _nmethodLast = reinterpret_cast<nmethod*>(0x8 * 0);
3599 _nmethodMiddle = reinterpret_cast<nmethod*>(0x8 * 1);
3600 _nmethodFirst = reinterpret_cast<nmethod*>(0x8 * 2);
3601
3602 _bucketLast = new nmethodBucket(_nmethodLast, NULL);
3603 _bucketMiddle = new nmethodBucket(_nmethodMiddle, _bucketLast);
3604 _bucketFirst = new nmethodBucket(_nmethodFirst, _bucketMiddle);
3605
3606 _bucketList = _bucketFirst;
3607 }
3608
3609 ~TestNmethodBucketContext() {
3610 delete _bucketLast;
3611 delete _bucketMiddle;
3612 delete _bucketFirst;
3613
3614 CodeCache_lock->unlock();
3615 }
3616 };
3617
3618 class TestNmethodBucket {
3619 public:
3620 static void testRemoveDependentNmethodFirstDeleteImmediately() {
3621 TestNmethodBucketContext c;
3622
3623 nmethodBucket::remove_dependent_nmethod(&c._bucketList, c._nmethodFirst, true /* delete */);
3624
3625 assert(c._bucketList == c._bucketMiddle, "check");
3626 assert(c._bucketList->next() == c._bucketLast, "check");
3627 assert(c._bucketList->next()->next() == NULL, "check");
3628
3629 // Cleanup before context is deleted.
3630 c._bucketFirst = NULL;
3631 }
3632
3633 static void testRemoveDependentNmethodMiddleDeleteImmediately() {
3634 TestNmethodBucketContext c;
3635
3636 nmethodBucket::remove_dependent_nmethod(&c._bucketList, c._nmethodMiddle, true /* delete */);
3637
3638 assert(c._bucketList == c._bucketFirst, "check");
3639 assert(c._bucketList->next() == c._bucketLast, "check");
3640 assert(c._bucketList->next()->next() == NULL, "check");
3641
3642 // Cleanup before context is deleted.
3643 c._bucketMiddle = NULL;
3644 }
3645
3646 static void testRemoveDependentNmethodLastDeleteImmediately() {
3647 TestNmethodBucketContext c;
3648
3649 nmethodBucket::remove_dependent_nmethod(&c._bucketList, c._nmethodLast, true /* delete */);
3650
3651 assert(c._bucketList == c._bucketFirst, "check");
3652 assert(c._bucketList->next() == c._bucketMiddle, "check");
3653 assert(c._bucketList->next()->next() == NULL, "check");
3654
3655 // Cleanup before context is deleted.
3656 c._bucketLast = NULL;
3657 }
3658
3659 static void testRemoveDependentNmethodFirstDeleteDeferred() {
3660 TestNmethodBucketContext c;
3661
3662 nmethodBucket::remove_dependent_nmethod(&c._bucketList, c._nmethodFirst, false /* delete */);
3663
3664 assert(c._bucketList == c._bucketFirst, "check");
3665 assert(c._bucketList->next() == c._bucketMiddle, "check");
3666 assert(c._bucketList->next()->next() == c._bucketLast, "check");
3667 assert(c._bucketList->next()->next()->next() == NULL, "check");
3668
3669 assert(c._bucketFirst->count() == 0, "check");
3670 assert(c._bucketMiddle->count() == 1, "check");
3671 assert(c._bucketLast->count() == 1, "check");
3672 }
3673
3674 static void testRemoveDependentNmethodMiddleDeleteDeferred() {
3675 TestNmethodBucketContext c;
3676
3677 nmethodBucket::remove_dependent_nmethod(&c._bucketList, c._nmethodMiddle, false /* delete */);
3678
3679 assert(c._bucketList == c._bucketFirst, "check");
3680 assert(c._bucketList->next() == c._bucketMiddle, "check");
3681 assert(c._bucketList->next()->next() == c._bucketLast, "check");
3682 assert(c._bucketList->next()->next()->next() == NULL, "check");
3683
3684 assert(c._bucketFirst->count() == 1, "check");
3685 assert(c._bucketMiddle->count() == 0, "check");
3686 assert(c._bucketLast->count() == 1, "check");
3687 }
3688
3689 static void testRemoveDependentNmethodLastDeleteDeferred() {
3690 TestNmethodBucketContext c;
3691
3692 nmethodBucket::remove_dependent_nmethod(&c._bucketList, c._nmethodLast, false /* delete */);
3693
3694 assert(c._bucketList == c._bucketFirst, "check");
3695 assert(c._bucketList->next() == c._bucketMiddle, "check");
3696 assert(c._bucketList->next()->next() == c._bucketLast, "check");
3697 assert(c._bucketList->next()->next()->next() == NULL, "check");
3698
3699 assert(c._bucketFirst->count() == 1, "check");
3700 assert(c._bucketMiddle->count() == 1, "check");
3701 assert(c._bucketLast->count() == 0, "check");
3702 }
3703
3704 static void testRemoveDependentNmethodConvenienceFirst() {
3705 TestNmethodBucketContext c;
3706
3707 nmethodBucket::remove_dependent_nmethod(c._bucketList, c._nmethodFirst);
3708
3709 assert(c._bucketList == c._bucketFirst, "check");
3710 assert(c._bucketList->next() == c._bucketMiddle, "check");
3711 assert(c._bucketList->next()->next() == c._bucketLast, "check");
3712 assert(c._bucketList->next()->next()->next() == NULL, "check");
3713
3714 assert(c._bucketFirst->count() == 0, "check");
3715 assert(c._bucketMiddle->count() == 1, "check");
3716 assert(c._bucketLast->count() == 1, "check");
3717 }
3718
3719 static void testRemoveDependentNmethodConvenienceMiddle() {
3720 TestNmethodBucketContext c;
3721
3722 nmethodBucket::remove_dependent_nmethod(c._bucketList, c._nmethodMiddle);
3723
3724 assert(c._bucketList == c._bucketFirst, "check");
3725 assert(c._bucketList->next() == c._bucketMiddle, "check");
3726 assert(c._bucketList->next()->next() == c._bucketLast, "check");
3727 assert(c._bucketList->next()->next()->next() == NULL, "check");
3728
3729 assert(c._bucketFirst->count() == 1, "check");
3730 assert(c._bucketMiddle->count() == 0, "check");
3731 assert(c._bucketLast->count() == 1, "check");
3732 }
3733
3734 static void testRemoveDependentNmethodConvenienceLast() {
3735 TestNmethodBucketContext c;
3736
3737 nmethodBucket::remove_dependent_nmethod(c._bucketList, c._nmethodLast);
3738
3739 assert(c._bucketList == c._bucketFirst, "check");
3740 assert(c._bucketList->next() == c._bucketMiddle, "check");
3741 assert(c._bucketList->next()->next() == c._bucketLast, "check");
3742 assert(c._bucketList->next()->next()->next() == NULL, "check");
3743
3744 assert(c._bucketFirst->count() == 1, "check");
3745 assert(c._bucketMiddle->count() == 1, "check");
3746 assert(c._bucketLast->count() == 0, "check");
3747 }
3748
3749 static void testRemoveDependentNmethod() {
3750 testRemoveDependentNmethodFirstDeleteImmediately();
3751 testRemoveDependentNmethodMiddleDeleteImmediately();
3752 testRemoveDependentNmethodLastDeleteImmediately();
3753
3754 testRemoveDependentNmethodFirstDeleteDeferred();
3755 testRemoveDependentNmethodMiddleDeleteDeferred();
3756 testRemoveDependentNmethodLastDeleteDeferred();
3757
3758 testRemoveDependentNmethodConvenienceFirst();
3759 testRemoveDependentNmethodConvenienceMiddle();
3760 testRemoveDependentNmethodConvenienceLast();
3761 }
3762
3763 static void test() {
3764 testRemoveDependentNmethod();
3765 }
3766 };
3767
3768 void TestNmethodBucket_test() {
3769 TestNmethodBucket::test();
3770 }
3771
3772 #endif
|
186 // create a new array of vtable_indices for default methods
187 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
188 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
189 assert(default_vtable_indices() == NULL, "only create once");
190 set_default_vtable_indices(vtable_indices);
191 return vtable_indices;
192 }
193
194 InstanceKlass::InstanceKlass(int vtable_len,
195 int itable_len,
196 int static_field_size,
197 int nonstatic_oop_map_size,
198 unsigned kind,
199 ReferenceType rt,
200 AccessFlags access_flags,
201 bool is_anonymous) {
202 No_Safepoint_Verifier no_safepoint; // until k becomes parsable
203
204 int iksize = InstanceKlass::size(vtable_len, itable_len, nonstatic_oop_map_size,
205 access_flags.is_interface(), is_anonymous);
206 set_vtable_length(vtable_len);
207 set_itable_length(itable_len);
208 set_static_field_size(static_field_size);
209 set_nonstatic_oop_map_size(nonstatic_oop_map_size);
210 set_access_flags(access_flags);
211 _misc_flags = 0; // initialize to zero
212 set_kind(kind);
213 set_is_anonymous(is_anonymous);
214 assert(size() == iksize, "wrong size for object");
215
216 set_array_klasses(NULL);
217 set_methods(NULL);
218 set_method_ordering(NULL);
219 set_default_methods(NULL);
220 set_default_vtable_indices(NULL);
221 set_local_interfaces(NULL);
222 set_transitive_interfaces(NULL);
223 init_implementor();
224 set_fields(NULL, 0);
225 set_constants(NULL);
226 set_class_loader_data(NULL);
227 set_source_file_name_index(0);
228 set_source_debug_extension(NULL, 0);
229 set_array_name(NULL);
230 set_inner_classes(NULL);
231 set_static_oop_field_count(0);
232 set_nonstatic_field_size(0);
233 set_is_marked_dependent(false);
234 _dep_context = DependencyContext::EMPTY;
235 set_init_state(InstanceKlass::allocated);
236 set_init_thread(NULL);
237 set_reference_type(rt);
238 set_oop_map_cache(NULL);
239 set_jni_ids(NULL);
240 set_osr_nmethods_head(NULL);
241 set_breakpoints(NULL);
242 init_previous_versions();
243 set_generic_signature_index(0);
244 release_set_methods_jmethod_ids(NULL);
245 set_annotations(NULL);
246 set_jvmti_cached_class_field_map(NULL);
247 set_initial_method_idnum(0);
248 set_jvmti_cached_class_field_map(NULL);
249 set_cached_class_file(NULL);
250 set_initial_method_idnum(0);
251 set_minor_version(0);
252 set_major_version(0);
253 NOT_PRODUCT(_verify_count = 0;)
254
255 // initialize the non-header words to zero
256 intptr_t* p = (intptr_t*)this;
257 for (int index = InstanceKlass::header_size(); index < iksize; index++) {
258 p[index] = NULL_WORD;
259 }
260
261 // Set temporary value until parseClassFile updates it with the real instance
262 // size.
263 set_layout_helper(Klass::instance_layout_helper(0, true));
264 }
265
266
267 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
1845 size_t idnum = (size_t)method->method_idnum();
1846 jmethodID* jmeths = methods_jmethod_ids_acquire();
1847 size_t length; // length assigned as debugging crumb
1848 jmethodID id = NULL;
1849 if (jmeths != NULL && // If there is a cache
1850 (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
1851 id = jmeths[idnum+1]; // Look up the id (may be NULL)
1852 }
1853 return id;
1854 }
1855
1856 int nmethodBucket::decrement() {
1857 return Atomic::add(-1, (volatile int *)&_count);
1858 }
1859
1860 //
1861 // Walk the list of dependent nmethods searching for nmethods which
1862 // are dependent on the changes that were passed in and mark them for
1863 // deoptimization. Returns the number of nmethods found.
1864 //
1865 int DependencyContext::mark_dependent_nmethods(DepChange& changes) {
1866 int found = 0;
1867 for (nmethodBucket* b = dependencies(); b != NULL; b = b->next()) {
1868 nmethod* nm = b->get_nmethod();
1869 // since dependencies aren't removed until an nmethod becomes a zombie,
1870 // the dependency list may contain nmethods which aren't alive.
1871 if (b->count() > 0 && nm->is_alive() && !nm->is_marked_for_deoptimization() && nm->check_dependency_on(changes)) {
1872 if (TraceDependencies) {
1873 ResourceMark rm;
1874 tty->print_cr("Marked for deoptimization");
1875 changes.print();
1876 nm->print();
1877 nm->print_dependencies();
1878 }
1879 nm->mark_for_deoptimization();
1880 found++;
1881 }
1882 }
1883 return found;
1884 }
1885
1886 //
1887 // Add an nmethod to the dependency context.
1888 // It's possible that an nmethod has multiple dependencies on this klass
1889 // so a count is kept for each bucket to guarantee that creation and
1890 // deletion of dependencies is consistent. Returns new head of the list.
1891 //
1892 void DependencyContext::add_dependent_nmethod(nmethod* nm, bool expunge) {
1893 assert_lock_strong(CodeCache_lock);
1894 for (nmethodBucket* b = dependencies(); b != NULL; b = b->next()) {
1895 if (nm == b->get_nmethod()) {
1896 b->increment();
1897 return;
1898 }
1899 }
1900 set_dependencies(new nmethodBucket(nm, dependencies()));
1901
1902 if (expunge) {
1903 // Remove stale entries from the list.
1904 expunge_stale_entries();
1905 }
1906 }
1907
1908 //
1909 // Decrement count of the nmethod in the dependency list and, optionally, remove
1910 // the bucket completely when the count goes to 0. This method must find
1911 // a corresponding bucket otherwise there's a bug in the recording of dependencies.
1912 void DependencyContext::remove_dependent_nmethod(nmethod* nm, bool expunge) {
1913 assert_locked_or_safepoint(CodeCache_lock);
1914 nmethodBucket* first = dependencies();
1915 nmethodBucket* last = NULL;
1916 for (nmethodBucket* b = first; b != NULL; b = b->next()) {
1917 if (nm == b->get_nmethod()) {
1918 int val = b->decrement();
1919 guarantee(val >= 0, "Underflow: %d", val);
1920 if (val == 0) {
1921 if (expunge) {
1922 if (last == NULL) {
1923 set_dependencies(b->next());
1924 } else {
1925 last->set_next(b->next());
1926 }
1927 delete b;
1928 } else {
1929 set_has_stale_entries(true);
1930 }
1931 }
1932 if (expunge) {
1933 // Remove stale entries from the list.
1934 expunge_stale_entries();
1935 }
1936 return;
1937 }
1938 last = b;
1939 }
1940 #ifdef ASSERT
1941 tty->print_raw_cr("### can't find dependent nmethod");
1942 nm->print();
1943 #endif // ASSERT
1944 ShouldNotReachHere();
1945 }
1946
1947 //
1948 // Reclaim all unused buckets.
1949 //
1950 void DependencyContext::expunge_stale_entries() {
1951 assert_locked_or_safepoint(CodeCache_lock);
1952 if (!has_stale_entries()) {
1953 assert(!find_stale_entries(), "inconsistent info");
1954 return;
1955 }
1956 nmethodBucket* first = dependencies();
1957 nmethodBucket* last = NULL;
1958 for (nmethodBucket* b = first; b != NULL;) {
1959 assert(b->count() >= 0, "bucket count: %d", b->count());
1960 nmethodBucket* next = b->next();
1961 if (b->count() == 0) {
1962 if (last == NULL) {
1963 first = next;
1964 } else {
1965 last->set_next(next);
1966 }
1967 delete b;
1968 // last stays the same.
1969 } else {
1970 last = b;
1971 }
1972 b = next;
1973 }
1974 set_dependencies(first);
1975 set_has_stale_entries(false);
1976 }
1977
1978 int DependencyContext::remove_all_dependents() {
1979 //assert_lock_strong(CodeCache_lock); // called from InstanceKlass::release_C_heap_structures()
1980 nmethodBucket* b = dependencies();
1981 set_dependencies(NULL);
1982 int marked = 0;
1983 while (b != NULL) {
1984 nmethod* nm = b->get_nmethod();
1985 if (b->count() > 0 && nm->is_alive() && !nm->is_marked_for_deoptimization()) {
1986 nm->mark_for_deoptimization();
1987 marked++;
1988 }
1989 nmethodBucket* next = b->next();
1990 delete b;
1991 b = next;
1992 }
1993 set_has_stale_entries(false);
1994 return marked;
1995 }
1996
1997 #ifndef PRODUCT
1998 void DependencyContext::print_dependent_nmethods(bool verbose) {
1999 int idx = 0;
2000 for (nmethodBucket* b = dependencies(); b != NULL; b = b->next()) {
2001 nmethod* nm = b->get_nmethod();
2002 tty->print("[%d] count=%d { ", idx++, b->count());
2003 if (!verbose) {
2004 nm->print_on(tty, "nmethod");
2005 tty->print_cr(" } ");
2006 } else {
2007 nm->print();
2008 nm->print_dependencies();
2009 tty->print_cr("--- } ");
2010 }
2011 }
2012 }
2013
2014 bool DependencyContext::is_dependent_nmethod(nmethod* nm) {
2015 for (nmethodBucket* b = dependencies(); b != NULL; b = b->next()) {
2016 if (nm == b->get_nmethod()) {
2017 #ifdef ASSERT
2018 int count = b->count();
2019 assert(count >= 0, "count shouldn't be negative: %d", count);
2020 #endif
2021 return true;
2022 }
2023 }
2024 return false;
2025 }
2026
2027 bool DependencyContext::find_stale_entries() {
2028 for (nmethodBucket* b = dependencies(); b != NULL; b = b->next()) {
2029 if (b->count() == 0) return true;
2030 }
2031 return false;
2032 }
2033
2034 #endif //PRODUCT
2035
2036 int InstanceKlass::mark_dependent_nmethods(DepChange& changes) {
2037 DependencyContext dep_context(&_dep_context);
2038 return dep_context.mark_dependent_nmethods(changes);
2039 }
2040
2041 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2042 DependencyContext dep_context(&_dep_context);
2043 dep_context.add_dependent_nmethod(nm);
2044 }
2045
2046 void InstanceKlass::remove_dependent_nmethod(nmethod* nm, bool delete_immediately) {
2047 DependencyContext dep_context(&_dep_context);
2048 dep_context.remove_dependent_nmethod(nm, delete_immediately);
2049 }
2050
2051 #ifndef PRODUCT
2052 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2053 DependencyContext dep_context(&_dep_context);
2054 dep_context.print_dependent_nmethods(verbose);
2055 }
2056
2057 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2058 DependencyContext dep_context(&_dep_context);
2059 return dep_context.is_dependent_nmethod(nm);
2060 }
2061 #endif //PRODUCT
2062
2063 void InstanceKlass::clean_weak_instanceklass_links(BoolObjectClosure* is_alive) {
2064 clean_implementors_list(is_alive);
2065 clean_method_data(is_alive);
2066
2067 // Since GC iterates InstanceKlasses sequentially, it is safe to remove stale entries here.
2068 DependencyContext dep_context(&_dep_context);
2069 dep_context.expunge_stale_entries();
2070 }
2071
2072 void InstanceKlass::clean_implementors_list(BoolObjectClosure* is_alive) {
2073 assert(class_loader_data()->is_alive(is_alive), "this klass should be live");
2074 if (is_interface()) {
2075 if (ClassUnloading) {
2076 Klass* impl = implementor();
2077 if (impl != NULL) {
2078 if (!impl->is_loader_alive(is_alive)) {
2079 // remove this guy
2080 Klass** klass = adr_implementor();
2081 assert(klass != NULL, "null klass");
2082 if (klass != NULL) {
2083 *klass = NULL;
2084 }
2085 }
2086 }
2087 }
2088 }
2089 }
2096 }
2097 }
2098 }
2099
2100
2101 static void remove_unshareable_in_class(Klass* k) {
2102 // remove klass's unshareable info
2103 k->remove_unshareable_info();
2104 }
2105
2106 void InstanceKlass::remove_unshareable_info() {
2107 Klass::remove_unshareable_info();
2108 // Unlink the class
2109 if (is_linked()) {
2110 unlink_class();
2111 }
2112 init_implementor();
2113
2114 constants()->remove_unshareable_info();
2115
2116 assert(_dep_context == DependencyContext::EMPTY, "dependency context is not shareable");
2117
2118 for (int i = 0; i < methods()->length(); i++) {
2119 Method* m = methods()->at(i);
2120 m->remove_unshareable_info();
2121 }
2122
2123 // do array classes also.
2124 array_klasses_do(remove_unshareable_in_class);
2125 }
2126
2127 static void restore_unshareable_in_class(Klass* k, TRAPS) {
2128 // Array classes have null protection domain.
2129 // --> see ArrayKlass::complete_create_array_klass()
2130 k->restore_unshareable_info(ClassLoaderData::the_null_class_loader_data(), Handle(), CHECK);
2131 }
2132
2133 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2134 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2135 instanceKlassHandle ik(THREAD, this);
2136
2137 Array<Method*>* methods = ik->methods();
2227 set_jni_ids(NULL);
2228
2229 jmethodID* jmeths = methods_jmethod_ids_acquire();
2230 if (jmeths != (jmethodID*)NULL) {
2231 release_set_methods_jmethod_ids(NULL);
2232 FreeHeap(jmeths);
2233 }
2234
2235 // Deallocate MemberNameTable
2236 {
2237 Mutex* lock_or_null = SafepointSynchronize::is_at_safepoint() ? NULL : MemberNameTable_lock;
2238 MutexLockerEx ml(lock_or_null, Mutex::_no_safepoint_check_flag);
2239 MemberNameTable* mnt = member_names();
2240 if (mnt != NULL) {
2241 delete mnt;
2242 set_member_names(NULL);
2243 }
2244 }
2245
2246 // release dependencies
2247 {
2248 DependencyContext ctx(&_dep_context);
2249 int marked = ctx.remove_all_dependents();
2250 assert(marked == 0, "all dependencies should be already invalidated");
2251 }
2252
2253 // Deallocate breakpoint records
2254 if (breakpoints() != 0x0) {
2255 methods_do(clear_all_breakpoints);
2256 assert(breakpoints() == 0x0, "should have cleared breakpoints");
2257 }
2258
2259 // deallocate the cached class file
2260 if (_cached_class_file != NULL) {
2261 os::free(_cached_class_file);
2262 _cached_class_file = NULL;
2263 }
2264
2265 // Decrement symbol reference counts associated with the unloaded class.
2266 if (_name != NULL) _name->decrement_refcount();
2267 // unreference array name derived from this class name (arrays of an unloaded
2268 // class can't be referenced anymore).
2269 if (_array_name != NULL) _array_name->decrement_refcount();
2270 if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension);
3573 return NULL; // The version of klass is gone, no method is found
3574 }
3575 Method* method = holder->method_with_orig_idnum(idnum);
3576 return method;
3577 }
3578
3579
3580 jint InstanceKlass::get_cached_class_file_len() {
3581 return VM_RedefineClasses::get_cached_class_file_len(_cached_class_file);
3582 }
3583
3584 unsigned char * InstanceKlass::get_cached_class_file_bytes() {
3585 return VM_RedefineClasses::get_cached_class_file_bytes(_cached_class_file);
3586 }
3587
3588
3589 /////////////// Unit tests ///////////////
3590
3591 #ifndef PRODUCT
3592
3593 class TestDependencyContext {
3594 public:
3595 nmethod* _nmethods[3];
3596
3597 intptr_t _dependency_context;
3598
3599 TestDependencyContext() : _dependency_context(DependencyContext::EMPTY) {
3600 CodeCache_lock->lock_without_safepoint_check();
3601
3602 DependencyContext depContext(&_dependency_context);
3603
3604 _nmethods[0] = reinterpret_cast<nmethod*>(0x8 * 0);
3605 _nmethods[1] = reinterpret_cast<nmethod*>(0x8 * 1);
3606 _nmethods[2] = reinterpret_cast<nmethod*>(0x8 * 2);
3607
3608 depContext.add_dependent_nmethod(_nmethods[2]);
3609 depContext.add_dependent_nmethod(_nmethods[1]);
3610 depContext.add_dependent_nmethod(_nmethods[0]);
3611 }
3612
3613 ~TestDependencyContext() {
3614 wipe();
3615 CodeCache_lock->unlock();
3616 }
3617
3618 static void testRemoveDependentNmethod(int id, bool delete_immediately) {
3619 TestDependencyContext c;
3620 DependencyContext depContext(&c._dependency_context);
3621 assert(!has_stale_entries(depContext), "check");
3622
3623 nmethod* nm = c._nmethods[id];
3624 depContext.remove_dependent_nmethod(nm, delete_immediately);
3625
3626 if (!delete_immediately) {
3627 assert(has_stale_entries(depContext), "check");
3628 assert(depContext.is_dependent_nmethod(nm), "check");
3629 depContext.expunge_stale_entries();
3630 }
3631
3632 assert(!has_stale_entries(depContext), "check");
3633 assert(!depContext.is_dependent_nmethod(nm), "check");
3634 }
3635
3636 static void testRemoveDependentNmethod() {
3637 testRemoveDependentNmethod(0, false);
3638 testRemoveDependentNmethod(1, false);
3639 testRemoveDependentNmethod(2, false);
3640
3641 testRemoveDependentNmethod(0, true);
3642 testRemoveDependentNmethod(1, true);
3643 testRemoveDependentNmethod(2, true);
3644 }
3645
3646 static void test() {
3647 testRemoveDependentNmethod();
3648 }
3649
3650 static bool has_stale_entries(DependencyContext ctx) {
3651 assert(ctx.has_stale_entries() == ctx.find_stale_entries(), "check");
3652 return ctx.has_stale_entries();
3653 }
3654
3655 void wipe() {
3656 DependencyContext ctx(&_dependency_context);
3657 nmethodBucket* b = ctx.dependencies();
3658 ctx.set_dependencies(NULL);
3659 ctx.set_has_stale_entries(false);
3660 while (b != NULL) {
3661 nmethodBucket* next = b->next();
3662 delete b;
3663 b = next;
3664 }
3665 }
3666 };
3667
3668 void TestDependencyContext_test() {
3669 TestDependencyContext::test();
3670 }
3671
3672 #endif
|