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 #include "classfile/vmSymbols.hpp"
26 #include "jni.h"
27 #include "jvm.h"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "runtime/interfaceSupport.inline.hpp"
32 #include "runtime/perfData.inline.hpp"
33 #include "runtime/perfMemory.hpp"
34
35 /*
36 * Implementation of class jdk.internal.perf.Perf
37 */
38
39
40 #define PERF_ENTRY(result_type, header) \
41 JVM_ENTRY(result_type, header)
42
43 #define PERF_END JVM_END
44
45 #define PerfWrapper(arg) /* Unimplemented at this time */
46
47 static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
48
49 char* utfstr = nullptr;
50
51 if (str == nullptr) {
52 THROW_NULL(vmSymbols::java_lang_NullPointerException());
53 //throw_new(env,"NullPointerException");
54 }
55
56 int len = env->GetStringUTFLength(str);
57 int unicode_len = env->GetStringLength(str);
58
59 utfstr = NEW_RESOURCE_ARRAY(char, len + 1);
60
61 env->GetStringUTFRegion(str, 0, unicode_len, utfstr);
62
63 return utfstr;
64 }
65
66 PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, int vmid))
67
68 PerfWrapper("Perf_Attach");
69
70 char* address = nullptr;
71 size_t capacity = 0;
72
73 // attach to the PerfData memory region for the specified VM
74 PerfMemory::attach(vmid, &address, &capacity, CHECK_NULL);
75
76 {
77 ThreadToNativeFromVM ttnfv(thread);
78 return env->NewDirectByteBuffer(address, (jlong)capacity);
79 }
80
81 PERF_END
82
83 PERF_ENTRY(void, Perf_Detach(JNIEnv *env, jobject unused, jobject buffer))
84
85 PerfWrapper("Perf_Detach");
86
87 if (!UsePerfData) {
88 // With -XX:-UsePerfData, detach is just a NOP
89 return;
90 }
91
92 void* address = nullptr;
93 jlong capacity = 0;
94
95 // get buffer address and capacity
96 {
97 ThreadToNativeFromVM ttnfv(thread);
98 address = env->GetDirectBufferAddress(buffer);
99 capacity = env->GetDirectBufferCapacity(buffer);
100 }
101
102 PerfMemory::detach((char*)address, capacity);
103
104 PERF_END
105
106 PERF_ENTRY(jobject, Perf_CreateLong(JNIEnv *env, jobject perf, jstring name,
107 int variability, int units, jlong value))
108
109 PerfWrapper("Perf_CreateLong");
110
111 char* name_utf = nullptr;
112
113 if (units <= 0 || units > PerfData::U_Last) {
114 debug_only(warning("unexpected units argument, units = %d", units));
115 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
116 }
117
118 ResourceMark rm;
119
120 {
121 ThreadToNativeFromVM ttnfv(thread);
122
123 name_utf = jstr_to_utf(env, name, CHECK_NULL);
124 }
125
126 PerfLong* pl = nullptr;
145
146 case PerfData::V_Variable:
147 pl = PerfDataManager::create_long_variable(NULL_NS, (char *)name_utf,
148 (PerfData::Units)units, value,
149 CHECK_NULL);
150 break;
151
152 default: /* Illegal Argument */
153 debug_only(warning("unexpected variability value: %d", variability));
154 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
155 break;
156 }
157
158 long* lp = (long*)pl->get_address();
159
160 {
161 ThreadToNativeFromVM ttnfv(thread);
162 return env->NewDirectByteBuffer(lp, sizeof(jlong));
163 }
164
165 PERF_END
166
167 PERF_ENTRY(jobject, Perf_CreateByteArray(JNIEnv *env, jobject perf,
168 jstring name, jint variability,
169 jint units, jbyteArray value,
170 jint maxlength))
171
172 PerfWrapper("Perf_CreateByteArray");
173
174 // check for valid byte array objects
175 if (name == nullptr || value == nullptr) {
176 THROW_NULL(vmSymbols::java_lang_NullPointerException());
177 }
178
179 // check for valid variability classification
180 if (variability != PerfData::V_Constant &&
181 variability != PerfData::V_Variable) {
182 debug_only(warning("unexpected variability value: %d", variability));
183 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
184 }
185
186 // check for valid units
187 if (units != PerfData::U_String) {
228 }
229 else {
230
231 // create the string variable
232 pbv = PerfDataManager::create_string_variable(NULL_NS, (char*)name_utf,
233 maxlength,
234 (char*)value_local,
235 CHECK_NULL);
236
237 assert(maxlength >= value_length,"string variable length should be <= maxlength");
238 }
239 }
240
241 char* cp = (char*)pbv->get_address();
242
243 {
244 ThreadToNativeFromVM ttnfv(thread);
245 return env->NewDirectByteBuffer(cp, maxlength+1);
246 }
247
248 PERF_END
249
250 PERF_ENTRY(jlong, Perf_HighResCounter(JNIEnv *env, jobject perf))
251
252 PerfWrapper("Perf_HighResCounter");
253
254 // this should be a method in java.lang.System. This value could
255 // be acquired through access to a PerfData performance counter, but
256 // doing so would require that the PerfData monitoring overhead be
257 // incurred by all Java applications, which is unacceptable.
258
259 return os::elapsed_counter();
260
261 PERF_END
262
263 PERF_ENTRY(jlong, Perf_HighResFrequency(JNIEnv *env, jobject perf))
264
265 PerfWrapper("Perf_HighResFrequency");
266
267 // this should be a method in java.lang.System. This value could
268 // be acquired through access to a PerfData performance counter, but
269 // doing so would require that the PerfData monitoring overhead be
270 // incurred by all Java applications, which is unacceptable.
271
272 return os::elapsed_frequency();
273
274 PERF_END
275
276 /// JVM_RegisterPerfMethods
277
278 #define CC (char*) /*cast a literal from (const char*)*/
279 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
280 #define BB "Ljava/nio/ByteBuffer;"
281 #define JLS "Ljava/lang/String;"
282 #define CL_ARGS CC "(" JLS "IIJ)" BB
283 #define CBA_ARGS CC "(" JLS "II[BI)" BB
284
285 static JNINativeMethod perfmethods[] = {
286
287 {CC "attach0", CC "(I)" BB, FN_PTR(Perf_Attach)},
288 {CC "detach", CC "(" BB ")V", FN_PTR(Perf_Detach)},
289 {CC "createLong", CL_ARGS, FN_PTR(Perf_CreateLong)},
290 {CC "createByteArray", CBA_ARGS, FN_PTR(Perf_CreateByteArray)},
291 {CC "highResCounter", CC "()J", FN_PTR(Perf_HighResCounter)},
292 {CC "highResFrequency", CC "()J", FN_PTR(Perf_HighResFrequency)}
293 };
294
|
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 #include "classfile/vmSymbols.hpp"
26 #include "jni.h"
27 #include "jvm.h"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "runtime/interfaceSupport.inline.hpp"
32 #include "runtime/perfData.inline.hpp"
33 #include "runtime/perfMemory.hpp"
34
35 /*
36 * Implementation of class jdk.internal.perf.Perf
37 */
38
39 #define PerfWrapper(arg) /* Unimplemented at this time */
40
41 static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
42
43 char* utfstr = nullptr;
44
45 if (str == nullptr) {
46 THROW_NULL(vmSymbols::java_lang_NullPointerException());
47 //throw_new(env,"NullPointerException");
48 }
49
50 int len = env->GetStringUTFLength(str);
51 int unicode_len = env->GetStringLength(str);
52
53 utfstr = NEW_RESOURCE_ARRAY(char, len + 1);
54
55 env->GetStringUTFRegion(str, 0, unicode_len, utfstr);
56
57 return utfstr;
58 }
59
60 JVM_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, int vmid))
61
62 PerfWrapper("Perf_Attach");
63
64 char* address = nullptr;
65 size_t capacity = 0;
66
67 // attach to the PerfData memory region for the specified VM
68 PerfMemory::attach(vmid, &address, &capacity, CHECK_NULL);
69
70 {
71 ThreadToNativeFromVM ttnfv(thread);
72 return env->NewDirectByteBuffer(address, (jlong)capacity);
73 }
74
75 JVM_END
76
77 JVM_ENTRY(void, Perf_Detach(JNIEnv *env, jobject unused, jobject buffer))
78
79 PerfWrapper("Perf_Detach");
80
81 if (!UsePerfData) {
82 // With -XX:-UsePerfData, detach is just a NOP
83 return;
84 }
85
86 void* address = nullptr;
87 jlong capacity = 0;
88
89 // get buffer address and capacity
90 {
91 ThreadToNativeFromVM ttnfv(thread);
92 address = env->GetDirectBufferAddress(buffer);
93 capacity = env->GetDirectBufferCapacity(buffer);
94 }
95
96 PerfMemory::detach((char*)address, capacity);
97
98 JVM_END
99
100 JVM_ENTRY(jobject, Perf_CreateLong(JNIEnv *env, jobject perf, jstring name,
101 int variability, int units, jlong value))
102
103 PerfWrapper("Perf_CreateLong");
104
105 char* name_utf = nullptr;
106
107 if (units <= 0 || units > PerfData::U_Last) {
108 debug_only(warning("unexpected units argument, units = %d", units));
109 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
110 }
111
112 ResourceMark rm;
113
114 {
115 ThreadToNativeFromVM ttnfv(thread);
116
117 name_utf = jstr_to_utf(env, name, CHECK_NULL);
118 }
119
120 PerfLong* pl = nullptr;
139
140 case PerfData::V_Variable:
141 pl = PerfDataManager::create_long_variable(NULL_NS, (char *)name_utf,
142 (PerfData::Units)units, value,
143 CHECK_NULL);
144 break;
145
146 default: /* Illegal Argument */
147 debug_only(warning("unexpected variability value: %d", variability));
148 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
149 break;
150 }
151
152 long* lp = (long*)pl->get_address();
153
154 {
155 ThreadToNativeFromVM ttnfv(thread);
156 return env->NewDirectByteBuffer(lp, sizeof(jlong));
157 }
158
159 JVM_END
160
161 JVM_ENTRY(jobject, Perf_CreateByteArray(JNIEnv *env, jobject perf,
162 jstring name, jint variability,
163 jint units, jbyteArray value,
164 jint maxlength))
165
166 PerfWrapper("Perf_CreateByteArray");
167
168 // check for valid byte array objects
169 if (name == nullptr || value == nullptr) {
170 THROW_NULL(vmSymbols::java_lang_NullPointerException());
171 }
172
173 // check for valid variability classification
174 if (variability != PerfData::V_Constant &&
175 variability != PerfData::V_Variable) {
176 debug_only(warning("unexpected variability value: %d", variability));
177 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
178 }
179
180 // check for valid units
181 if (units != PerfData::U_String) {
222 }
223 else {
224
225 // create the string variable
226 pbv = PerfDataManager::create_string_variable(NULL_NS, (char*)name_utf,
227 maxlength,
228 (char*)value_local,
229 CHECK_NULL);
230
231 assert(maxlength >= value_length,"string variable length should be <= maxlength");
232 }
233 }
234
235 char* cp = (char*)pbv->get_address();
236
237 {
238 ThreadToNativeFromVM ttnfv(thread);
239 return env->NewDirectByteBuffer(cp, maxlength+1);
240 }
241
242 JVM_END
243
244 JVM_ENTRY(jlong, Perf_HighResCounter(JNIEnv *env, jobject perf))
245
246 PerfWrapper("Perf_HighResCounter");
247
248 // this should be a method in java.lang.System. This value could
249 // be acquired through access to a PerfData performance counter, but
250 // doing so would require that the PerfData monitoring overhead be
251 // incurred by all Java applications, which is unacceptable.
252
253 return os::elapsed_counter();
254
255 JVM_END
256
257 JVM_ENTRY(jlong, Perf_HighResFrequency(JNIEnv *env, jobject perf))
258
259 PerfWrapper("Perf_HighResFrequency");
260
261 // this should be a method in java.lang.System. This value could
262 // be acquired through access to a PerfData performance counter, but
263 // doing so would require that the PerfData monitoring overhead be
264 // incurred by all Java applications, which is unacceptable.
265
266 return os::elapsed_frequency();
267
268 JVM_END
269
270 /// JVM_RegisterPerfMethods
271
272 #define CC (char*) /*cast a literal from (const char*)*/
273 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
274 #define BB "Ljava/nio/ByteBuffer;"
275 #define JLS "Ljava/lang/String;"
276 #define CL_ARGS CC "(" JLS "IIJ)" BB
277 #define CBA_ARGS CC "(" JLS "II[BI)" BB
278
279 static JNINativeMethod perfmethods[] = {
280
281 {CC "attach0", CC "(I)" BB, FN_PTR(Perf_Attach)},
282 {CC "detach", CC "(" BB ")V", FN_PTR(Perf_Detach)},
283 {CC "createLong", CL_ARGS, FN_PTR(Perf_CreateLong)},
284 {CC "createByteArray", CBA_ARGS, FN_PTR(Perf_CreateByteArray)},
285 {CC "highResCounter", CC "()J", FN_PTR(Perf_HighResCounter)},
286 {CC "highResFrequency", CC "()J", FN_PTR(Perf_HighResFrequency)}
287 };
288
|