< prev index next >

src/hotspot/share/prims/perf.cpp

Print this page

 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/vmSymbols.hpp"
 27 #include "jni.h"
 28 #include "jvm.h"
 29 #include "memory/allocation.inline.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "runtime/interfaceSupport.inline.hpp"
 33 #include "runtime/perfData.inline.hpp"
 34 #include "runtime/perfMemory.hpp"
 35 
 36 /*
 37  *      Implementation of class jdk.internal.perf.Perf
 38  */
 39 
 40 
 41 #define PERF_ENTRY(result_type, header) \
 42   JVM_ENTRY(result_type, header)
 43 
 44 #define PERF_END JVM_END
 45 
 46 #define PerfWrapper(arg) /* Unimplemented at this time */
 47 
 48 static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
 49 
 50   char* utfstr = nullptr;
 51 
 52   if (str == nullptr) {
 53     THROW_NULL(vmSymbols::java_lang_NullPointerException());
 54     //throw_new(env,"NullPointerException");
 55   }
 56 
 57   int len = env->GetStringUTFLength(str);
 58   int unicode_len = env->GetStringLength(str);
 59 
 60   utfstr = NEW_RESOURCE_ARRAY(char, len + 1);
 61 
 62   env->GetStringUTFRegion(str, 0, unicode_len, utfstr);
 63 
 64   return utfstr;
 65 }
 66 
 67 PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, int vmid))
 68 
 69   PerfWrapper("Perf_Attach");
 70 
 71   char* address = nullptr;
 72   size_t capacity = 0;
 73 
 74   // attach to the PerfData memory region for the specified VM
 75   PerfMemory::attach(vmid, &address, &capacity, CHECK_NULL);
 76 
 77   {
 78     ThreadToNativeFromVM ttnfv(thread);
 79     return env->NewDirectByteBuffer(address, (jlong)capacity);
 80   }
 81 
 82 PERF_END
 83 
 84 PERF_ENTRY(void, Perf_Detach(JNIEnv *env, jobject unused, jobject buffer))
 85 
 86   PerfWrapper("Perf_Detach");
 87 
 88   if (!UsePerfData) {
 89     // With -XX:-UsePerfData, detach is just a NOP
 90     return;
 91   }
 92 
 93   void* address = nullptr;
 94   jlong capacity = 0;
 95 
 96   // get buffer address and capacity
 97   {
 98    ThreadToNativeFromVM ttnfv(thread);
 99    address = env->GetDirectBufferAddress(buffer);
100    capacity = env->GetDirectBufferCapacity(buffer);
101   }
102 
103   PerfMemory::detach((char*)address, capacity);
104 
105 PERF_END
106 
107 PERF_ENTRY(jobject, Perf_CreateLong(JNIEnv *env, jobject perf, jstring name,
108            int variability, int units, jlong value))
109 
110   PerfWrapper("Perf_CreateLong");
111 
112   char* name_utf = nullptr;
113 
114   if (units <= 0 || units > PerfData::U_Last) {
115     debug_only(warning("unexpected units argument, units = %d", units));
116     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
117   }
118 
119   ResourceMark rm;
120 
121   {
122     ThreadToNativeFromVM ttnfv(thread);
123 
124     name_utf = jstr_to_utf(env, name, CHECK_NULL);
125   }
126 
127   PerfLong* pl = nullptr;

146 
147   case PerfData::V_Variable:
148     pl = PerfDataManager::create_long_variable(NULL_NS, (char *)name_utf,
149                                               (PerfData::Units)units, value,
150                                               CHECK_NULL);
151     break;
152 
153   default: /* Illegal Argument */
154     debug_only(warning("unexpected variability value: %d", variability));
155     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
156     break;
157   }
158 
159   long* lp = (long*)pl->get_address();
160 
161   {
162     ThreadToNativeFromVM ttnfv(thread);
163     return env->NewDirectByteBuffer(lp, sizeof(jlong));
164   }
165 
166 PERF_END
167 
168 PERF_ENTRY(jobject, Perf_CreateByteArray(JNIEnv *env, jobject perf,
169                                          jstring name, jint variability,
170                                          jint units, jbyteArray value,
171                                          jint maxlength))
172 
173   PerfWrapper("Perf_CreateByteArray");
174 
175   // check for valid byte array objects
176   if (name == nullptr || value == nullptr) {
177     THROW_NULL(vmSymbols::java_lang_NullPointerException());
178   }
179 
180   // check for valid variability classification
181   if (variability != PerfData::V_Constant &&
182       variability != PerfData::V_Variable) {
183     debug_only(warning("unexpected variability value: %d", variability));
184     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
185   }
186 
187   // check for valid units
188   if (units != PerfData::U_String) {

229     }
230     else {
231 
232       // create the string variable
233       pbv = PerfDataManager::create_string_variable(NULL_NS, (char*)name_utf,
234                                                     maxlength,
235                                                     (char*)value_local,
236                                                     CHECK_NULL);
237 
238      assert(maxlength >= value_length,"string variable length should be <= maxlength");
239     }
240   }
241 
242   char* cp = (char*)pbv->get_address();
243 
244   {
245     ThreadToNativeFromVM ttnfv(thread);
246     return env->NewDirectByteBuffer(cp, maxlength+1);
247   }
248 
249 PERF_END
250 
251 PERF_ENTRY(jlong, Perf_HighResCounter(JNIEnv *env, jobject perf))
252 
253   PerfWrapper("Perf_HighResCounter");
254 
255   // this should be a method in java.lang.System. This value could
256   // be acquired through access to a PerfData performance counter, but
257   // doing so would require that the PerfData monitoring overhead be
258   // incurred by all Java applications, which is unacceptable.
259 
260   return os::elapsed_counter();
261 
262 PERF_END
263 
264 PERF_ENTRY(jlong, Perf_HighResFrequency(JNIEnv *env, jobject perf))
265 
266   PerfWrapper("Perf_HighResFrequency");
267 
268   // this should be a method in java.lang.System. This value could
269   // be acquired through access to a PerfData performance counter, but
270   // doing so would require that the PerfData monitoring overhead be
271   // incurred by all Java applications, which is unacceptable.
272 
273   return os::elapsed_frequency();
274 
275 PERF_END
276 
277 /// JVM_RegisterPerfMethods
278 
279 #define CC (char*)  /*cast a literal from (const char*)*/
280 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
281 #define BB "Ljava/nio/ByteBuffer;"
282 #define JLS "Ljava/lang/String;"
283 #define CL_ARGS     CC "(" JLS "IIJ)" BB
284 #define CBA_ARGS    CC "(" JLS "II[BI)" BB
285 
286 static JNINativeMethod perfmethods[] = {
287 
288   {CC "attach0",             CC "(I)" BB,         FN_PTR(Perf_Attach)},
289   {CC "detach",              CC "(" BB ")V",      FN_PTR(Perf_Detach)},
290   {CC "createLong",          CL_ARGS,             FN_PTR(Perf_CreateLong)},
291   {CC "createByteArray",     CBA_ARGS,            FN_PTR(Perf_CreateByteArray)},
292   {CC "highResCounter",      CC "()J",            FN_PTR(Perf_HighResCounter)},
293   {CC "highResFrequency",    CC "()J",            FN_PTR(Perf_HighResFrequency)}
294 };
295 

 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/vmSymbols.hpp"
 27 #include "jni.h"
 28 #include "jvm.h"
 29 #include "memory/allocation.inline.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "runtime/interfaceSupport.inline.hpp"
 33 #include "runtime/perfData.inline.hpp"
 34 #include "runtime/perfMemory.hpp"
 35 
 36 /*
 37  *      Implementation of class jdk.internal.perf.Perf
 38  */
 39 






 40 #define PerfWrapper(arg) /* Unimplemented at this time */
 41 
 42 static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
 43 
 44   char* utfstr = nullptr;
 45 
 46   if (str == nullptr) {
 47     THROW_NULL(vmSymbols::java_lang_NullPointerException());
 48     //throw_new(env,"NullPointerException");
 49   }
 50 
 51   int len = env->GetStringUTFLength(str);
 52   int unicode_len = env->GetStringLength(str);
 53 
 54   utfstr = NEW_RESOURCE_ARRAY(char, len + 1);
 55 
 56   env->GetStringUTFRegion(str, 0, unicode_len, utfstr);
 57 
 58   return utfstr;
 59 }
 60 
 61 JVM_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, int vmid))
 62 
 63   PerfWrapper("Perf_Attach");
 64 
 65   char* address = nullptr;
 66   size_t capacity = 0;
 67 
 68   // attach to the PerfData memory region for the specified VM
 69   PerfMemory::attach(vmid, &address, &capacity, CHECK_NULL);
 70 
 71   {
 72     ThreadToNativeFromVM ttnfv(thread);
 73     return env->NewDirectByteBuffer(address, (jlong)capacity);
 74   }
 75 
 76 JVM_END
 77 
 78 JVM_ENTRY(void, Perf_Detach(JNIEnv *env, jobject unused, jobject buffer))
 79 
 80   PerfWrapper("Perf_Detach");
 81 
 82   if (!UsePerfData) {
 83     // With -XX:-UsePerfData, detach is just a NOP
 84     return;
 85   }
 86 
 87   void* address = nullptr;
 88   jlong capacity = 0;
 89 
 90   // get buffer address and capacity
 91   {
 92    ThreadToNativeFromVM ttnfv(thread);
 93    address = env->GetDirectBufferAddress(buffer);
 94    capacity = env->GetDirectBufferCapacity(buffer);
 95   }
 96 
 97   PerfMemory::detach((char*)address, capacity);
 98 
 99 JVM_END
100 
101 JVM_ENTRY(jobject, Perf_CreateLong(JNIEnv *env, jobject perf, jstring name,
102            int variability, int units, jlong value))
103 
104   PerfWrapper("Perf_CreateLong");
105 
106   char* name_utf = nullptr;
107 
108   if (units <= 0 || units > PerfData::U_Last) {
109     debug_only(warning("unexpected units argument, units = %d", units));
110     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
111   }
112 
113   ResourceMark rm;
114 
115   {
116     ThreadToNativeFromVM ttnfv(thread);
117 
118     name_utf = jstr_to_utf(env, name, CHECK_NULL);
119   }
120 
121   PerfLong* pl = nullptr;

140 
141   case PerfData::V_Variable:
142     pl = PerfDataManager::create_long_variable(NULL_NS, (char *)name_utf,
143                                               (PerfData::Units)units, value,
144                                               CHECK_NULL);
145     break;
146 
147   default: /* Illegal Argument */
148     debug_only(warning("unexpected variability value: %d", variability));
149     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
150     break;
151   }
152 
153   long* lp = (long*)pl->get_address();
154 
155   {
156     ThreadToNativeFromVM ttnfv(thread);
157     return env->NewDirectByteBuffer(lp, sizeof(jlong));
158   }
159 
160 JVM_END
161 
162 JVM_ENTRY(jobject, Perf_CreateByteArray(JNIEnv *env, jobject perf,
163                                          jstring name, jint variability,
164                                          jint units, jbyteArray value,
165                                          jint maxlength))
166 
167   PerfWrapper("Perf_CreateByteArray");
168 
169   // check for valid byte array objects
170   if (name == nullptr || value == nullptr) {
171     THROW_NULL(vmSymbols::java_lang_NullPointerException());
172   }
173 
174   // check for valid variability classification
175   if (variability != PerfData::V_Constant &&
176       variability != PerfData::V_Variable) {
177     debug_only(warning("unexpected variability value: %d", variability));
178     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
179   }
180 
181   // check for valid units
182   if (units != PerfData::U_String) {

223     }
224     else {
225 
226       // create the string variable
227       pbv = PerfDataManager::create_string_variable(NULL_NS, (char*)name_utf,
228                                                     maxlength,
229                                                     (char*)value_local,
230                                                     CHECK_NULL);
231 
232      assert(maxlength >= value_length,"string variable length should be <= maxlength");
233     }
234   }
235 
236   char* cp = (char*)pbv->get_address();
237 
238   {
239     ThreadToNativeFromVM ttnfv(thread);
240     return env->NewDirectByteBuffer(cp, maxlength+1);
241   }
242 
243 JVM_END
244 
245 JVM_ENTRY(jlong, Perf_HighResCounter(JNIEnv *env, jobject perf))
246 
247   PerfWrapper("Perf_HighResCounter");
248 
249   // this should be a method in java.lang.System. This value could
250   // be acquired through access to a PerfData performance counter, but
251   // doing so would require that the PerfData monitoring overhead be
252   // incurred by all Java applications, which is unacceptable.
253 
254   return os::elapsed_counter();
255 
256 JVM_END
257 
258 JVM_ENTRY(jlong, Perf_HighResFrequency(JNIEnv *env, jobject perf))
259 
260   PerfWrapper("Perf_HighResFrequency");
261 
262   // this should be a method in java.lang.System. This value could
263   // be acquired through access to a PerfData performance counter, but
264   // doing so would require that the PerfData monitoring overhead be
265   // incurred by all Java applications, which is unacceptable.
266 
267   return os::elapsed_frequency();
268 
269 JVM_END
270 
271 /// JVM_RegisterPerfMethods
272 
273 #define CC (char*)  /*cast a literal from (const char*)*/
274 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
275 #define BB "Ljava/nio/ByteBuffer;"
276 #define JLS "Ljava/lang/String;"
277 #define CL_ARGS     CC "(" JLS "IIJ)" BB
278 #define CBA_ARGS    CC "(" JLS "II[BI)" BB
279 
280 static JNINativeMethod perfmethods[] = {
281 
282   {CC "attach0",             CC "(I)" BB,         FN_PTR(Perf_Attach)},
283   {CC "detach",              CC "(" BB ")V",      FN_PTR(Perf_Detach)},
284   {CC "createLong",          CL_ARGS,             FN_PTR(Perf_CreateLong)},
285   {CC "createByteArray",     CBA_ARGS,            FN_PTR(Perf_CreateByteArray)},
286   {CC "highResCounter",      CC "()J",            FN_PTR(Perf_HighResCounter)},
287   {CC "highResFrequency",    CC "()J",            FN_PTR(Perf_HighResFrequency)}
288 };
289 
< prev index next >