perf.cpp 9.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35
#include "precompiled.hpp"
#include "classfile/vmSymbols.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "oops/oop.inline.hpp"
#include "prims/jni.h"
#include "prims/jvm.h"
#include "runtime/interfaceSupport.hpp"
#include "runtime/perfData.hpp"
#include "runtime/perfMemory.hpp"

D
duke 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/*
 *      Implementation of class sun.misc.Perf
 */


#define PERF_ENTRY(result_type, header) \
  JVM_ENTRY(result_type, header)

#define PERF_END JVM_END

#define PerfWrapper(arg) /* Unimplemented at this time */

static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {

  char* utfstr = NULL;

  if (str == NULL) {
    THROW_0(vmSymbols::java_lang_NullPointerException());
    //throw_new(env,"NullPointerException");
  }

  int len = env->GetStringUTFLength(str);
  int unicode_len = env->GetStringLength(str);

  utfstr = NEW_RESOURCE_ARRAY(char, len + 1);

  env->GetStringUTFRegion(str, 0, unicode_len, utfstr);

  return utfstr;
}

PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, jstring user, int vmid, int mode))

  PerfWrapper("Perf_Attach");

  char* address = 0;
  size_t capacity = 0;
  const char* user_utf = NULL;

  ResourceMark rm;

  {
    ThreadToNativeFromVM ttnfv(thread);

    user_utf = user == NULL ? NULL : jstr_to_utf(env, user, CHECK_NULL);
  }

  if (mode != PerfMemory::PERF_MODE_RO &&
      mode != PerfMemory::PERF_MODE_RW) {
    THROW_0(vmSymbols::java_lang_IllegalArgumentException());
  }

  // attach to the PerfData memory region for the specified VM
  PerfMemory::attach(user_utf, vmid, (PerfMemory::PerfMemoryMode) mode,
                     &address, &capacity, CHECK_NULL);

  {
    ThreadToNativeFromVM ttnfv(thread);
    return env->NewDirectByteBuffer(address, (jlong)capacity);
  }

PERF_END

PERF_ENTRY(void, Perf_Detach(JNIEnv *env, jobject unused, jobject buffer))

  PerfWrapper("Perf_Detach");

  void* address = 0;
  jlong capacity = 0;

  // get buffer address and capacity
  {
   ThreadToNativeFromVM ttnfv(thread);
   address = env->GetDirectBufferAddress(buffer);
   capacity = env->GetDirectBufferCapacity(buffer);
  }

  PerfMemory::detach((char*)address, capacity, CHECK);

PERF_END

PERF_ENTRY(jobject, Perf_CreateLong(JNIEnv *env, jobject perf, jstring name,
           int variability, int units, jlong value))

  PerfWrapper("Perf_CreateLong");

  char* name_utf = NULL;

  if (units <= 0 || units > PerfData::U_Last) {
    debug_only(warning("unexpected units argument, units = %d", units));
    THROW_0(vmSymbols::java_lang_IllegalArgumentException());
  }

  ResourceMark rm;

  {
    ThreadToNativeFromVM ttnfv(thread);

    name_utf = jstr_to_utf(env, name, CHECK_NULL);
  }

  PerfLong* pl = NULL;

  // check that the PerfData name doesn't already exist
  if (PerfDataManager::exists(name_utf)) {
    THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "PerfLong name already exists");
  }

  switch(variability) {
145
  case PerfData::V_Constant:
D
duke 已提交
146 147 148 149 150
    pl = PerfDataManager::create_long_constant(NULL_NS, (char *)name_utf,
                                               (PerfData::Units)units, value,
                                               CHECK_NULL);
    break;

151 152
  case PerfData::V_Monotonic:
    pl = PerfDataManager::create_long_counter(NULL_NS, (char *)name_utf,
D
duke 已提交
153 154 155 156
                                               (PerfData::Units)units, value,
                                               CHECK_NULL);
    break;

157 158
  case PerfData::V_Variable:
    pl = PerfDataManager::create_long_variable(NULL_NS, (char *)name_utf,
D
duke 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
                                              (PerfData::Units)units, value,
                                              CHECK_NULL);
    break;

  default: /* Illegal Argument */
    debug_only(warning("unexpected variability value: %d", variability));
    THROW_0(vmSymbols::java_lang_IllegalArgumentException());
    break;
  }

  long* lp = (long*)pl->get_address();

  {
    ThreadToNativeFromVM ttnfv(thread);
    return env->NewDirectByteBuffer(lp, sizeof(jlong));
  }

PERF_END

PERF_ENTRY(jobject, Perf_CreateByteArray(JNIEnv *env, jobject perf,
                                         jstring name, jint variability,
                                         jint units, jbyteArray value,
                                         jint maxlength))

  PerfWrapper("Perf_CreateByteArray");

  // check for valid byte array objects
  if (name == NULL || value == NULL) {
    THROW_0(vmSymbols::java_lang_NullPointerException());
  }

  // check for valid variability classification
  if (variability != PerfData::V_Constant &&
      variability != PerfData::V_Variable) {
    debug_only(warning("unexpected variability value: %d", variability));
    THROW_0(vmSymbols::java_lang_IllegalArgumentException());
  }

  // check for valid units
  if (units != PerfData::U_String) {
    // only String based ByteArray objects are currently supported
    debug_only(warning("unexpected units value: %d", variability));
    THROW_0(vmSymbols::java_lang_IllegalArgumentException());
  }

  int value_length;
  char* name_utf = NULL;
  jbyte* value_local = NULL;

  ResourceMark rm;

  {
    ThreadToNativeFromVM ttnfv(thread);

    name_utf = jstr_to_utf(env, name, CHECK_NULL);

    value_length = env->GetArrayLength(value);

    value_local = NEW_RESOURCE_ARRAY(jbyte, value_length + 1);

    env->GetByteArrayRegion(value, 0, value_length, value_local);
  }

  // check that the counter name doesn't already exist
  if (PerfDataManager::exists((char*)name_utf)) {
    THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "PerfByteArray name already exists");
  }

  PerfByteArray* pbv = NULL;

  if (units == PerfData::U_String) {

    if (variability == PerfData::V_Constant) {
      // create the string constant
      pbv = PerfDataManager::create_string_constant(NULL_NS, (char*)name_utf,
                                                    (char*)value_local,
                                                    CHECK_NULL);

      assert(maxlength == value_length, "string constant length should be == maxlength");
      maxlength = value_length;
    }
    else {

      // create the string variable
      pbv = PerfDataManager::create_string_variable(NULL_NS, (char*)name_utf,
                                                    maxlength,
                                                    (char*)value_local,
                                                    CHECK_NULL);

     assert(maxlength >= value_length,"string variable length should be <= maxlength");
    }
  }

  char* cp = (char*)pbv->get_address();

  {
    ThreadToNativeFromVM ttnfv(thread);
    return env->NewDirectByteBuffer(cp, maxlength+1);
  }

PERF_END

PERF_ENTRY(jlong, Perf_HighResCounter(JNIEnv *env, jobject perf))

  PerfWrapper("Perf_HighResCounter");

  // this should be a method in java.lang.System. This value could
  // be acquired through access to a PerfData performance counter, but
  // doing so would require that the PerfData monitoring overhead be
  // incurred by all Java applications, which is unacceptable.

  return os::elapsed_counter();

PERF_END

PERF_ENTRY(jlong, Perf_HighResFrequency(JNIEnv *env, jobject perf))

  PerfWrapper("Perf_HighResFrequency");

  // this should be a method in java.lang.System. This value could
  // be acquired through access to a PerfData performance counter, but
  // doing so would require that the PerfData monitoring overhead be
  // incurred by all Java applications, which is unacceptable.

  return os::elapsed_frequency();

PERF_END

/// JVM_RegisterPerfMethods

#define CC (char*)  /*cast a literal from (const char*)*/
#define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
#define BB "Ljava/nio/ByteBuffer;"
#define JLS "Ljava/lang/String;"
#define CL_ARGS     CC"("JLS"IIJ)"BB
#define CBA_ARGS    CC"("JLS"II[BI)"BB

static JNINativeMethod perfmethods[] = {

  {CC"attach",              CC"("JLS"II)"BB,  FN_PTR(Perf_Attach)},
  {CC"detach",              CC"("BB")V",      FN_PTR(Perf_Detach)},
  {CC"createLong",          CL_ARGS,          FN_PTR(Perf_CreateLong)},
  {CC"createByteArray",     CBA_ARGS,         FN_PTR(Perf_CreateByteArray)},
  {CC"highResCounter",      CC"()J",          FN_PTR(Perf_HighResCounter)},
  {CC"highResFrequency",    CC"()J",          FN_PTR(Perf_HighResFrequency)}
};

#undef CBA_ARGS
#undef CL_ARGS
#undef JLS
#undef BB
#undef FN_PTR
#undef CC

// This one function is exported, used by NativeLookup.
JVM_ENTRY(void, JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass))
  PerfWrapper("JVM_RegisterPerfMethods");
  {
    ThreadToNativeFromVM ttnfv(thread);
    int ok = env->RegisterNatives(perfclass, perfmethods, sizeof(perfmethods)/sizeof(JNINativeMethod));
    guarantee(ok == 0, "register perf natives");
  }
JVM_END