2
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/vmSymbols.hpp"
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"
37
* Implementation of class jdk.internal.perf.Perf
41
#define PERF_ENTRY(result_type, header) \
42
JVM_ENTRY(result_type, header)
44
#define PERF_END JVM_END
46
#define PerfWrapper(arg) /* Unimplemented at this time */
48
static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
50
char* utfstr = nullptr;
53
THROW_0(vmSymbols::java_lang_NullPointerException());
54
//throw_new(env,"NullPointerException");
57
int len = env->GetStringUTFLength(str);
58
int unicode_len = env->GetStringLength(str);
60
utfstr = NEW_RESOURCE_ARRAY(char, len + 1);
62
env->GetStringUTFRegion(str, 0, unicode_len, utfstr);
67
PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, int vmid))
69
PerfWrapper("Perf_Attach");
74
// attach to the PerfData memory region for the specified VM
75
PerfMemory::attach(vmid, &address, &capacity, CHECK_NULL);
78
ThreadToNativeFromVM ttnfv(thread);
79
return env->NewDirectByteBuffer(address, (jlong)capacity);
84
PERF_ENTRY(void, Perf_Detach(JNIEnv *env, jobject unused, jobject buffer))
86
PerfWrapper("Perf_Detach");
89
// With -XX:-UsePerfData, detach is just a NOP
96
// get buffer address and capacity
98
ThreadToNativeFromVM ttnfv(thread);
99
address = env->GetDirectBufferAddress(buffer);
100
capacity = env->GetDirectBufferCapacity(buffer);
103
PerfMemory::detach((char*)address, capacity);
107
PERF_ENTRY(jobject, Perf_CreateLong(JNIEnv *env, jobject perf, jstring name,
108
int variability, int units, jlong value))
110
PerfWrapper("Perf_CreateLong");
112
char* name_utf = nullptr;
114
if (units <= 0 || units > PerfData::U_Last) {
115
debug_only(warning("unexpected units argument, units = %d", units));
116
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
122
ThreadToNativeFromVM ttnfv(thread);
124
name_utf = jstr_to_utf(env, name, CHECK_NULL);
127
PerfLong* pl = nullptr;
129
// check that the PerfData name doesn't already exist
130
if (PerfDataManager::exists(name_utf)) {
131
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "PerfLong name already exists");
134
switch(variability) {
135
case PerfData::V_Constant:
136
pl = PerfDataManager::create_long_constant(NULL_NS, (char *)name_utf,
137
(PerfData::Units)units, value,
141
case PerfData::V_Monotonic:
142
pl = PerfDataManager::create_long_counter(NULL_NS, (char *)name_utf,
143
(PerfData::Units)units, value,
147
case PerfData::V_Variable:
148
pl = PerfDataManager::create_long_variable(NULL_NS, (char *)name_utf,
149
(PerfData::Units)units, value,
153
default: /* Illegal Argument */
154
debug_only(warning("unexpected variability value: %d", variability));
155
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
159
long* lp = (long*)pl->get_address();
162
ThreadToNativeFromVM ttnfv(thread);
163
return env->NewDirectByteBuffer(lp, sizeof(jlong));
168
PERF_ENTRY(jobject, Perf_CreateByteArray(JNIEnv *env, jobject perf,
169
jstring name, jint variability,
170
jint units, jbyteArray value,
173
PerfWrapper("Perf_CreateByteArray");
175
// check for valid byte array objects
176
if (name == nullptr || value == nullptr) {
177
THROW_0(vmSymbols::java_lang_NullPointerException());
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_0(vmSymbols::java_lang_IllegalArgumentException());
187
// check for valid units
188
if (units != PerfData::U_String) {
189
// only String based ByteArray objects are currently supported
190
debug_only(warning("unexpected units value: %d", variability));
191
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
195
char* name_utf = nullptr;
196
jbyte* value_local = nullptr;
201
ThreadToNativeFromVM ttnfv(thread);
203
name_utf = jstr_to_utf(env, name, CHECK_NULL);
205
value_length = env->GetArrayLength(value);
207
value_local = NEW_RESOURCE_ARRAY(jbyte, value_length + 1);
209
env->GetByteArrayRegion(value, 0, value_length, value_local);
212
// check that the counter name doesn't already exist
213
if (PerfDataManager::exists((char*)name_utf)) {
214
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "PerfByteArray name already exists");
217
PerfByteArray* pbv = nullptr;
219
if (units == PerfData::U_String) {
221
if (variability == PerfData::V_Constant) {
222
// create the string constant
223
pbv = PerfDataManager::create_string_constant(NULL_NS, (char*)name_utf,
227
assert(maxlength == value_length, "string constant length should be == maxlength");
228
maxlength = value_length;
232
// create the string variable
233
pbv = PerfDataManager::create_string_variable(NULL_NS, (char*)name_utf,
238
assert(maxlength >= value_length,"string variable length should be <= maxlength");
242
char* cp = (char*)pbv->get_address();
245
ThreadToNativeFromVM ttnfv(thread);
246
return env->NewDirectByteBuffer(cp, maxlength+1);
251
PERF_ENTRY(jlong, Perf_HighResCounter(JNIEnv *env, jobject perf))
253
PerfWrapper("Perf_HighResCounter");
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.
260
return os::elapsed_counter();
264
PERF_ENTRY(jlong, Perf_HighResFrequency(JNIEnv *env, jobject perf))
266
PerfWrapper("Perf_HighResFrequency");
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.
273
return os::elapsed_frequency();
277
/// JVM_RegisterPerfMethods
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
286
static JNINativeMethod perfmethods[] = {
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)}
303
// This one function is exported, used by NativeLookup.
304
JVM_ENTRY(void, JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass))
305
PerfWrapper("JVM_RegisterPerfMethods");
307
ThreadToNativeFromVM ttnfv(thread);
308
int ok = env->RegisterNatives(perfclass, perfmethods, sizeof(perfmethods)/sizeof(JNINativeMethod));
309
guarantee(ok == 0, "register perf natives");