src/share/vm/prims/whitebox.cpp

Tue, 02 Apr 2013 11:28:33 +0200

author
mgerdin
date
Tue, 02 Apr 2013 11:28:33 +0200
changeset 4850
ede380e13960
parent 4592
12e01444ca2d
child 4885
3b890cd4da64
child 4908
b84fd7d73702
permissions
-rw-r--r--

8009763: Add WB test for String.intern()
Summary: Add convenience method in StringTable, add WhiteBox method and simple sanity test
Reviewed-by: mgerdin, zgu
Contributed-by: leonid.mesnik@oracle.com

     1 /*
     2  * Copyright (c) 2012, 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  */
    25 #include "precompiled.hpp"
    27 #include "memory/universe.hpp"
    28 #include "oops/oop.inline.hpp"
    30 #include "classfile/symbolTable.hpp"
    31 #include "classfile/classLoaderData.hpp"
    33 #include "prims/whitebox.hpp"
    34 #include "prims/wbtestmethods/parserTests.hpp"
    36 #include "runtime/interfaceSupport.hpp"
    37 #include "runtime/os.hpp"
    38 #include "utilities/debug.hpp"
    39 #include "utilities/macros.hpp"
    41 #if INCLUDE_ALL_GCS
    42 #include "gc_implementation/g1/concurrentMark.hpp"
    43 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
    44 #include "gc_implementation/g1/heapRegionRemSet.hpp"
    45 #endif // INCLUDE_ALL_GCS
    47 #ifdef INCLUDE_NMT
    48 #include "services/memTracker.hpp"
    49 #endif // INCLUDE_NMT
    51 #include "compiler/compileBroker.hpp"
    53 bool WhiteBox::_used = false;
    55 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
    56   return (jlong)(void*)JNIHandles::resolve(obj);
    57 WB_END
    59 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
    60   return heapOopSize;
    61 WB_END
    64 class WBIsKlassAliveClosure : public KlassClosure {
    65     Symbol* _name;
    66     bool _found;
    67 public:
    68     WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {}
    70     void do_klass(Klass* k) {
    71       if (_found) return;
    72       Symbol* ksym = k->name();
    73       if (ksym->fast_compare(_name) == 0) {
    74         _found = true;
    75       }
    76     }
    78     bool found() const {
    79         return _found;
    80     }
    81 };
    83 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name))
    84   Handle h_name = JNIHandles::resolve(name);
    85   if (h_name.is_null()) return false;
    86   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false);
    87   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
    89   WBIsKlassAliveClosure closure(sym);
    90   ClassLoaderDataGraph::classes_do(&closure);
    92   return closure.found();
    93 WB_END
    95 #if INCLUDE_ALL_GCS
    96 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
    97   G1CollectedHeap* g1 = G1CollectedHeap::heap();
    98   oop result = JNIHandles::resolve(obj);
    99   const HeapRegion* hr = g1->heap_region_containing(result);
   100   return hr->isHumongous();
   101 WB_END
   103 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
   104   G1CollectedHeap* g1 = G1CollectedHeap::heap();
   105   size_t nr = g1->free_regions();
   106   return (jlong)nr;
   107 WB_END
   109 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
   110   G1CollectedHeap* g1 = G1CollectedHeap::heap();
   111   ConcurrentMark* cm = g1->concurrent_mark();
   112   return cm->concurrent_marking_in_progress();
   113 WB_END
   115 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
   116   return (jint)HeapRegion::GrainBytes;
   117 WB_END
   118 #endif // INCLUDE_ALL_GCS
   120 #ifdef INCLUDE_NMT
   121 // Keep track of the 3 allocations in NMTAllocTest so we can free them later
   122 // on and verify that they're not visible anymore
   123 static void* nmtMtTest1 = NULL, *nmtMtTest2 = NULL, *nmtMtTest3 = NULL;
   125 // Alloc memory using the test memory type so that we can use that to see if
   126 // NMT picks it up correctly
   127 WB_ENTRY(jboolean, WB_NMTAllocTest(JNIEnv* env))
   128   void *mem;
   130   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
   131     return false;
   132   }
   134   // Allocate 2 * 128k + 256k + 1024k and free the 1024k one to make sure we track
   135   // everything correctly. Total should be 512k held alive.
   136   nmtMtTest1 = os::malloc(128 * 1024, mtTest);
   137   mem = os::malloc(1024 * 1024, mtTest);
   138   nmtMtTest2 = os::malloc(256 * 1024, mtTest);
   139   os::free(mem, mtTest);
   140   nmtMtTest3 = os::malloc(128 * 1024, mtTest);
   142   return true;
   143 WB_END
   145 // Free the memory allocated by NMTAllocTest
   146 WB_ENTRY(jboolean, WB_NMTFreeTestMemory(JNIEnv* env))
   148   if (nmtMtTest1 == NULL || nmtMtTest2 == NULL || nmtMtTest3 == NULL) {
   149     return false;
   150   }
   152   os::free(nmtMtTest1, mtTest);
   153   nmtMtTest1 = NULL;
   154   os::free(nmtMtTest2, mtTest);
   155   nmtMtTest2 = NULL;
   156   os::free(nmtMtTest3, mtTest);
   157   nmtMtTest3 = NULL;
   159   return true;
   160 WB_END
   162 // Block until the current generation of NMT data to be merged, used to reliably test the NMT feature
   163 WB_ENTRY(jboolean, WB_NMTWaitForDataMerge(JNIEnv* env))
   165   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
   166     return false;
   167   }
   169   return MemTracker::wbtest_wait_for_data_merge();
   170 WB_END
   172 #endif // INCLUDE_NMT
   174 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
   175   assert(method != NULL, "method should not be null");
   176   ThreadToNativeFromVM ttn(thread);
   177   return env->FromReflectedMethod(method);
   178 }
   180 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
   181   MutexLockerEx mu(Compile_lock);
   182   CodeCache::mark_all_nmethods_for_deoptimization();
   183   VM_Deoptimize op;
   184   VMThread::execute(&op);
   185 WB_END
   187 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method))
   188   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   189   MutexLockerEx mu(Compile_lock);
   190   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   191   int result = 0;
   192   nmethod* code = mh->code();
   193   if (code != NULL) {
   194     code->mark_for_deoptimization();
   195     ++result;
   196   }
   197   result += CodeCache::mark_for_deoptimization(mh());
   198   if (result > 0) {
   199     VM_Deoptimize op;
   200     VMThread::execute(&op);
   201   }
   202   return result;
   203 WB_END
   205 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method))
   206   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   207   MutexLockerEx mu(Compile_lock);
   208   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   209   nmethod* code = mh->code();
   210   if (code == NULL) {
   211     return JNI_FALSE;
   212   }
   213   return (code->is_alive() && !code->is_marked_for_deoptimization());
   214 WB_END
   216 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method))
   217   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   218   MutexLockerEx mu(Compile_lock);
   219   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   220   return !mh->is_not_compilable();
   221 WB_END
   223 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
   224   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   225   MutexLockerEx mu(Compile_lock);
   226   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   227   return mh->queued_for_compilation();
   228 WB_END
   230 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method))
   231   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   232   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   233   nmethod* code = mh->code();
   234   return (code != NULL ? code->comp_level() : CompLevel_none);
   235 WB_END
   238 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method))
   239   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   240   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   241   mh->set_not_compilable();
   242 WB_END
   244 WB_ENTRY(jboolean, WB_SetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
   245   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
   246   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
   247   bool result = mh->dont_inline();
   248   mh->set_dont_inline(value == JNI_TRUE);
   249   return result;
   250 WB_END
   252 WB_ENTRY(jint, WB_GetCompileQueuesSize(JNIEnv* env, jobject o))
   253   return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
   254          CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
   255 WB_END
   257 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
   258   ResourceMark rm(THREAD);
   259   int len;
   260   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len);
   261   oop found_string = StringTable::the_table()->lookup(name, len);
   262   if (found_string == NULL) {
   263         return false;
   264   }
   265   return true;
   266 WB_END
   269 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
   270   Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true);
   271   Universe::heap()->collect(GCCause::_last_ditch_collection);
   272 WB_END
   275 //Some convenience methods to deal with objects from java
   276 int WhiteBox::offset_for_field(const char* field_name, oop object,
   277     Symbol* signature_symbol) {
   278   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
   279   Thread* THREAD = Thread::current();
   281   //Get the class of our object
   282   Klass* arg_klass = object->klass();
   283   //Turn it into an instance-klass
   284   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
   286   //Create symbols to look for in the class
   287   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
   288       THREAD);
   290   //To be filled in with an offset of the field we're looking for
   291   fieldDescriptor fd;
   293   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
   294   if (res == NULL) {
   295     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
   296         name_symbol->as_C_string());
   297     fatal("Invalid layout of preloaded class");
   298   }
   300   //fetch the field at the offset we've found
   301   int dest_offset = fd.offset();
   303   return dest_offset;
   304 }
   307 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
   308   int offset = offset_for_field(field_name, object,
   309       vmSymbols::string_signature());
   310   oop string = object->obj_field(offset);
   311   if (string == NULL) {
   312     return NULL;
   313   }
   314   const char* ret = java_lang_String::as_utf8_string(string);
   315   return ret;
   316 }
   318 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
   319   int offset =
   320       offset_for_field(field_name, object, vmSymbols::bool_signature());
   321   bool ret = (object->bool_field(offset) == JNI_TRUE);
   322   return ret;
   323 }
   326 #define CC (char*)
   328 static JNINativeMethod methods[] = {
   329   {CC"getObjectAddress",   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
   330   {CC"getHeapOopSize",     CC"()I",                   (void*)&WB_GetHeapOopSize    },
   331   {CC"isClassAlive0",      CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
   332   {CC"parseCommandLine",
   333       CC"(Ljava/lang/String;[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
   334       (void*) &WB_ParseCommandLine
   335   },
   336 #if INCLUDE_ALL_GCS
   337   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
   338   {CC"g1IsHumongous",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
   339   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
   340   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
   341 #endif // INCLUDE_ALL_GCS
   342 #ifdef INCLUDE_NMT
   343   {CC"NMTAllocTest",       CC"()Z",                   (void*)&WB_NMTAllocTest      },
   344   {CC"NMTFreeTestMemory",  CC"()Z",                   (void*)&WB_NMTFreeTestMemory },
   345   {CC"NMTWaitForDataMerge",CC"()Z",                   (void*)&WB_NMTWaitForDataMerge},
   346 #endif // INCLUDE_NMT
   347   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
   348   {CC"deoptimizeMethod",   CC"(Ljava/lang/reflect/Method;)I",
   349                                                       (void*)&WB_DeoptimizeMethod  },
   350   {CC"isMethodCompiled",   CC"(Ljava/lang/reflect/Method;)Z",
   351                                                       (void*)&WB_IsMethodCompiled  },
   352   {CC"isMethodCompilable", CC"(Ljava/lang/reflect/Method;)Z",
   353                                                       (void*)&WB_IsMethodCompilable},
   354   {CC"isMethodQueuedForCompilation",
   355       CC"(Ljava/lang/reflect/Method;)Z",              (void*)&WB_IsMethodQueuedForCompilation},
   356   {CC"makeMethodNotCompilable",
   357       CC"(Ljava/lang/reflect/Method;)V",              (void*)&WB_MakeMethodNotCompilable},
   358   {CC"setDontInlineMethod",
   359       CC"(Ljava/lang/reflect/Method;Z)Z",             (void*)&WB_SetDontInlineMethod},
   360   {CC"getMethodCompilationLevel",
   361       CC"(Ljava/lang/reflect/Method;)I",              (void*)&WB_GetMethodCompilationLevel},
   362   {CC"getCompileQueuesSize",
   363       CC"()I",                                        (void*)&WB_GetCompileQueuesSize},
   364   {CC"isInStringTable",   CC"(Ljava/lang/String;)Z",  (void*)&WB_IsInStringTable  },
   365   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
   366 };
   368 #undef CC
   370 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
   371   {
   372     if (WhiteBoxAPI) {
   373       // Make sure that wbclass is loaded by the null classloader
   374       instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass());
   375       Handle loader(ikh->class_loader());
   376       if (loader.is_null()) {
   377         ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
   378         jint result = env->RegisterNatives(wbclass, methods, sizeof(methods)/sizeof(methods[0]));
   379         if (result == 0) {
   380           WhiteBox::set_used();
   381         }
   382       }
   383     }
   384   }
   385 JVM_END

mercurial