1 /*
2 * Copyright (c) 2001, 2025, 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 */
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;
121
122 // check that the PerfData name doesn't already exist
123 if (PerfDataManager::exists(name_utf)) {
124 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "PerfLong name already exists");
125 }
126
127 switch(variability) {
128 case PerfData::V_Constant:
129 pl = PerfDataManager::create_long_constant(NULL_NS, (char *)name_utf,
130 (PerfData::Units)units, value,
131 CHECK_NULL);
132 break;
133
134 case PerfData::V_Monotonic:
135 pl = PerfDataManager::create_long_counter(NULL_NS, (char *)name_utf,
136 (PerfData::Units)units, value,
137 CHECK_NULL);
138 break;
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) {
182 // only String based ByteArray objects are currently supported
183 DEBUG_ONLY(warning("unexpected units value: %d", variability));
184 THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
185 }
186
187 int value_length;
188 char* name_utf = nullptr;
189 jbyte* value_local = nullptr;
190
191 ResourceMark rm;
192
193 {
194 ThreadToNativeFromVM ttnfv(thread);
195
196 name_utf = jstr_to_utf(env, name, CHECK_NULL);
197
198 value_length = env->GetArrayLength(value);
199
200 value_local = NEW_RESOURCE_ARRAY(jbyte, value_length + 1);
201
202 env->GetByteArrayRegion(value, 0, value_length, value_local);
203 }
204
205 // check that the counter name doesn't already exist
206 if (PerfDataManager::exists((char*)name_utf)) {
207 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "PerfByteArray name already exists");
208 }
209
210 PerfByteArray* pbv = nullptr;
211
212 if (units == PerfData::U_String) {
213
214 if (variability == PerfData::V_Constant) {
215 // create the string constant
216 pbv = PerfDataManager::create_string_constant(NULL_NS, (char*)name_utf,
217 (char*)value_local,
218 CHECK_NULL);
219
220 assert(maxlength == value_length, "string constant length should be == maxlength");
221 maxlength = value_length;
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
289 #undef CBA_ARGS
290 #undef CL_ARGS
291 #undef JLS
292 #undef BB
293 #undef FN_PTR
294 #undef CC
295
296 // This one function is exported, used by NativeLookup.
297 JVM_ENTRY(void, JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass))
298 PerfWrapper("JVM_RegisterPerfMethods");
299 {
300 ThreadToNativeFromVM ttnfv(thread);
301 int ok = env->RegisterNatives(perfclass, perfmethods, sizeof(perfmethods)/sizeof(JNINativeMethod));
302 guarantee(ok == 0, "register perf natives");
303 }
304 JVM_END