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