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