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 "jvm.h"
27 #include "logging/log.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/arguments.hpp"
31 #include "runtime/handles.inline.hpp"
32 #include "runtime/java.hpp"
33 #include "runtime/javaCalls.hpp"
34 #include "runtime/mutex.hpp"
35 #include "runtime/mutexLocker.hpp"
36 #include "runtime/os.hpp"
37 #include "runtime/perfData.inline.hpp"
38 #include "utilities/exceptions.hpp"
39 #include "utilities/globalCounter.inline.hpp"
40 #include "utilities/globalDefinitions.hpp"
41
42 PerfDataList* PerfDataManager::_all = nullptr;
43 PerfDataList* PerfDataManager::_constants = nullptr;
44 volatile bool PerfDataManager::_has_PerfData = 0;
45
46 /*
47 * The jvmstat global and subsystem jvmstat counter name spaces. The top
48 * level name spaces imply the interface stability level of the counter,
49 * which generally follows the Java package, class, and property naming
50 * conventions. The CounterNS enumeration values should be used to index
51 * into this array.
52 */
53 const char* PerfDataManager::_name_spaces[] = {
54 // top level name spaces
55 "java", // stable and supported name space
56 "com.sun", // unstable but supported name space
57 "sun", // unstable and unsupported name space
58 // subsystem name spaces
59 "java.gc", // Garbage Collection name spaces
60 "com.sun.gc",
61 "sun.gc",
62 "java.ci", // Compiler name spaces
63 "com.sun.ci",
64 "sun.ci",
65 "java.cls", // Class Loader name spaces
66 "com.sun.cls",
67 "sun.cls",
68 "java.rt", // Runtime name spaces
69 "com.sun.rt",
70 "sun.rt",
71 "java.os", // Operating System name spaces
72 "com.sun.os",
73 "sun.os",
74 "java.threads", // Threads System name spaces
75 "com.sun.threads",
76 "sun.threads",
77 "java.threads.cpu_time", //Thread CPU time name spaces
78 "com.sun.threads.cpu_time",
79 "sun.threads.cpu_time",
80 "java.property", // Java Property name spaces
81 "com.sun.property",
82 "sun.property",
83 "",
84 };
85
86 PerfData::PerfData(CounterNS ns, const char* name, Units u, Variability v)
87 : _name(nullptr), _v(v), _u(u), _on_c_heap(false), _valuep(nullptr) {
88
89 const char* prefix = PerfDataManager::ns_to_string(ns);
90
91 const size_t _name_size = strlen(name) + strlen(prefix) + 2;
92 _name = NEW_C_HEAP_ARRAY(char, _name_size, mtInternal);
93 assert(strlen(name) != 0, "invalid name");
94
95 if (ns == NULL_NS) {
96 // No prefix is added to counters with the NULL_NS namespace.
97 strcpy(_name, name);
98 // set the F_Supported flag based on the counter name prefix.
99 if (PerfDataManager::is_stable_supported(_name) ||
100 PerfDataManager::is_unstable_supported(_name)) {
101 _flags = F_Supported;
102 }
103 else {
104 _flags = F_None;
105 }
106 }
107 else {
108 os::snprintf_checked(_name, _name_size, "%s.%s", prefix, name);
109 // set the F_Supported flag based on the given namespace.
110 if (PerfDataManager::is_stable_supported(ns) ||
111 PerfDataManager::is_unstable_supported(ns)) {
112 _flags = F_Supported;
113 }
114 else {
115 _flags = F_None;
116 }
117 }
118 }
119
120 PerfData::~PerfData() {
121 FREE_C_HEAP_ARRAY(char, _name);
122 if (is_on_c_heap()) {
123 FREE_C_HEAP_ARRAY(PerfDataEntry, _pdep);
124 }
125 }
126
127 void PerfData::create_entry(BasicType dtype, size_t dsize, size_t vlen) {
128
129 size_t dlen = vlen==0 ? 1 : vlen;
130
131 size_t namelen = strlen(name()) + 1; // include null terminator
132 size_t size = sizeof(PerfDataEntry) + namelen;
133 size_t pad_length = ((size % dsize) == 0) ? 0 : dsize - (size % dsize);
134 size += pad_length;
135 size_t data_start = size;
136 size += (dsize * dlen);
137
138 // align size to assure allocation in units of 8 bytes
139 int align = sizeof(jlong) - 1;
140 size = ((size + align) & ~align);
141 char* psmp = PerfMemory::alloc(size);
142
143 if (psmp == nullptr) {
144 // out of PerfMemory memory resources. allocate on the C heap
145 // to avoid vm termination.
146 psmp = NEW_C_HEAP_ARRAY(char, size, mtInternal);
147 _on_c_heap = true;
148 }
149
150 // compute the addresses for the name and data
151 char* cname = psmp + sizeof(PerfDataEntry);
152
153 // data is in the last dsize*dlen bytes of the entry
154 void* valuep = (void*) (psmp + data_start);
155
156 assert(is_on_c_heap() || PerfMemory::contains(cname), "just checking");
157 assert(is_on_c_heap() || PerfMemory::contains((char*)valuep), "just checking");
158
159 // copy the name, including null terminator, into PerfData memory
160 strcpy(cname, name());
161
162
163 // set the header values in PerfData memory
164 PerfDataEntry* pdep = (PerfDataEntry*)psmp;
165 pdep->entry_length = (jint)size;
166 pdep->name_offset = (jint) ((uintptr_t) cname - (uintptr_t) psmp);
167 pdep->vector_length = (jint)vlen;
168 pdep->data_type = (jbyte) type2char(dtype);
169 pdep->data_units = units();
170 pdep->data_variability = variability();
171 pdep->flags = (jbyte)flags();
172 pdep->data_offset = (jint) data_start;
173
174 log_debug(perf, datacreation)("name = %s, dtype = %d, variability = %d,"
175 " units = %d, dsize = %zu, vlen = %zu,"
176 " pad_length = %zu, size = %zu, on_c_heap = %s,"
177 " address = " INTPTR_FORMAT ","
178 " data address = " INTPTR_FORMAT,
179 cname, dtype, variability(),
180 units(), dsize, vlen,
181 pad_length, size, is_on_c_heap() ? "TRUE":"FALSE",
182 p2i(psmp), p2i(valuep));
183
184 // record the start of the entry and the location of the data field.
185 _pdep = pdep;
186 _valuep = valuep;
187
188 // mark the PerfData memory region as having been updated.
189 PerfMemory::mark_updated();
190 }
191
192 bool PerfData::name_equals(const char* name) const {
193 return strcmp(name, this->name()) == 0;
194 }
195
196 PerfLong::PerfLong(CounterNS ns, const char* namep, Units u, Variability v)
197 : PerfData(ns, namep, u, v) {
198
199 create_entry(T_LONG, sizeof(jlong));
200 }
201
202 PerfByteArray::PerfByteArray(CounterNS ns, const char* namep, Units u,
203 Variability v, jint length)
204 : PerfData(ns, namep, u, v), _length(length) {
205
206 create_entry(T_BYTE, sizeof(jbyte), (size_t)_length);
207 }
208
209 void PerfString::set_string(const char* s2) {
210
211 // copy n bytes of the string, assuring the null string is
212 // copied if s2 == nullptr.
213 strncpy((char *)_valuep, s2 == nullptr ? "" : s2, _length);
214
215 // assure the string is null terminated when strlen(s2) >= _length
216 ((char*)_valuep)[_length-1] = '\0';
217 }
218
219 PerfStringConstant::PerfStringConstant(CounterNS ns, const char* namep,
220 const char* initial_value)
221 : PerfString(ns, namep, V_Constant,
222 initial_value == nullptr ? 1 :
223 MIN2((jint)(strlen((char*)initial_value)+1),
224 (jint)(PerfMaxStringConstLength+1)),
225 initial_value) {
226
227 if (PrintMiscellaneous && Verbose) {
228 if (is_valid() && initial_value != nullptr &&
229 ((jint)strlen(initial_value) > (jint)PerfMaxStringConstLength)) {
230
231 warning("Truncating PerfStringConstant: name = %s,"
232 " length = " INT32_FORMAT ","
233 " PerfMaxStringConstLength = " INT32_FORMAT "\n",
234 namep,
235 (jint)strlen(initial_value),
236 (jint)PerfMaxStringConstLength);
237 }
238 }
239 }
240
241
242 void PerfDataManager::destroy() {
243
244 if (_all == nullptr)
245 // destroy already called, or initialization never happened
246 return;
247
248 // About to delete the counters than might still be accessed by other threads.
249 // The shutdown is performed in two stages: a) clear the flag to notify future
250 // counter users that we are at shutdown; b) sync up with current users, waiting
251 // for them to finish with counters.
252 //
253 AtomicAccess::store(&_has_PerfData, false);
254 GlobalCounter::write_synchronize();
255
256 log_debug(perf, datacreation)("Total = %d, Constants = %d",
257 _all->length(),
258 _constants == nullptr ? 0 : _constants->length());
259
260 for (int index = 0; index < _all->length(); index++) {
261 PerfData* p = _all->at(index);
262 delete p;
263 }
264
265 delete(_all);
266 delete(_constants);
267
268 _all = nullptr;
269 _constants = nullptr;
270 }
271
272 void PerfDataManager::add_item(PerfData* p) {
273
274 MutexLocker ml(PerfDataManager_lock);
275
276 // Default sizes determined using -Xlog:perf+datacreation=debug
277 if (_all == nullptr) {
278 _all = new PerfDataList(191);
279 AtomicAccess::release_store(&_has_PerfData, true);
280 }
281
282 assert(!_all->contains(p->name()), "duplicate name added: %s", p->name());
283
284 // add to the list of all perf data items
285 _all->append(p);
286
287 if (p->variability() == PerfData::V_Constant) {
288 if (_constants == nullptr) {
289 _constants = new PerfDataList(51);
290 }
291 _constants->append(p);
292 return;
293 }
294 }
295
296 char* PerfDataManager::counter_name(const char* ns, const char* name) {
297 assert(ns != nullptr, "ns string required");
298 assert(name != nullptr, "name string required");
299
300 size_t len = strlen(ns) + strlen(name) + 2;
301 char* result = NEW_RESOURCE_ARRAY(char, len);
302 os::snprintf_checked(result, len, "%s.%s", ns, name);
303 return result;
304 }
305
306 char* PerfDataManager::name_space(const char* ns, const char* sub,
307 int instance) {
308 char intbuf[40];
309 jio_snprintf(intbuf, 40, UINT32_FORMAT, instance);
310 return name_space(ns, name_space(sub, intbuf));
311 }
312
313 char *PerfDataManager::name_space(const char* ns, int instance) {
314 char intbuf[40];
315 jio_snprintf(intbuf, 40, UINT32_FORMAT, instance);
316 return name_space(ns, intbuf);
317 }
318
319 PerfStringConstant* PerfDataManager::create_string_constant(CounterNS ns,
320 const char* name,
321 const char* s,
322 TRAPS) {
323
324 PerfStringConstant* p = new PerfStringConstant(ns, name, s);
325
326 if (!p->is_valid()) {
327 // allocation of native resources failed.
328 delete p;
329 THROW_NULL(vmSymbols::java_lang_OutOfMemoryError());
330 }
331
332 add_item(p);
333
334 return p;
335 }
336
337 PerfLongConstant* PerfDataManager::create_long_constant(CounterNS ns,
338 const char* name,
339 PerfData::Units u,
340 jlong val, TRAPS) {
341
342 PerfLongConstant* p = new PerfLongConstant(ns, name, u, val);
343
344 if (!p->is_valid()) {
345 // allocation of native resources failed.
346 delete p;
347 THROW_NULL(vmSymbols::java_lang_OutOfMemoryError());
348 }
349
350 add_item(p);
351
352 return p;
353 }
354
355 PerfStringVariable* PerfDataManager::create_string_variable(CounterNS ns,
356 const char* name,
357 int max_length,
358 const char* s,
359 TRAPS) {
360
361 if (max_length == 0 && s != nullptr) max_length = (int)strlen(s);
362
363 assert(max_length != 0, "PerfStringVariable with length 0");
364
365 PerfStringVariable* p = new PerfStringVariable(ns, name, max_length, s);
366
367 if (!p->is_valid()) {
368 // allocation of native resources failed.
369 delete p;
370 THROW_NULL(vmSymbols::java_lang_OutOfMemoryError());
371 }
372
373 add_item(p);
374
375 return p;
376 }
377
378 PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns,
379 const char* name,
380 PerfData::Units u,
381 jlong ival, TRAPS) {
382
383 PerfLongVariable* p = new PerfLongVariable(ns, name, u, ival);
384
385 if (!p->is_valid()) {
386 // allocation of native resources failed.
387 delete p;
388 THROW_NULL(vmSymbols::java_lang_OutOfMemoryError());
389 }
390
391 add_item(p);
392
393 return p;
394 }
395
396 PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns,
397 const char* name,
398 PerfData::Units u,
399 jlong ival, TRAPS) {
400
401 PerfLongCounter* p = new PerfLongCounter(ns, name, u, ival);
402
403 if (!p->is_valid()) {
404 // allocation of native resources failed.
405 delete p;
406 THROW_NULL(vmSymbols::java_lang_OutOfMemoryError());
407 }
408
409 add_item(p);
410
411 return p;
412 }
413
414 /*
415 * Call into java.lang.System.getProperty to check that the value of the
416 * specified property matches
417 */
418 void PerfDataManager::assert_system_property(const char* name, const char* value, TRAPS) {
419 #ifdef ASSERT
420 ResourceMark rm(THREAD);
421
422 // setup the arguments to getProperty
423 Handle key_str = java_lang_String::create_from_str(name, CHECK);
424
425 // return value
426 JavaValue result(T_OBJECT);
427
428 // public static String getProperty(String key, String def);
429 JavaCalls::call_static(&result, vmClasses::System_klass(),
430 vmSymbols::getProperty_name(),
431 vmSymbols::string_string_signature(), key_str, CHECK);
432
433 oop value_oop = result.get_oop();
434 assert(value_oop != nullptr, "property must have a value");
435
436 // convert Java String to utf8 string
437 char *system_value = java_lang_String::as_utf8_string(value_oop);
438
439 assert(strcmp(value, system_value) == 0, "property value mustn't differ from System.getProperty. Our value is: %s, System.getProperty is: %s",
440 value, system_value);
441 #endif // ASSERT
442 }
443
444 /*
445 * Adds a constant counter of the given property. Asserts the value does not
446 * differ from the value retrievable from System.getProperty(name)
447 */
448 void PerfDataManager::add_property_constant(CounterNS name_space, const char* name, const char* value, TRAPS) {
449 // the property must exist
450 assert(value != nullptr, "property name should be have a value: %s", name);
451 assert_system_property(name, value, CHECK);
452
453 // create the property counter
454 PerfDataManager::create_string_constant(name_space, name, value, CHECK);
455 }
456
457 /*
458 * Adds a string constant of the given property. Retrieves the value via
459 * Arguments::get_property() and asserts the value for the does not differ from
460 * the value retrievable from System.getProperty()
461 */
462 void PerfDataManager::add_property_constant(CounterNS name_space, const char* name, TRAPS) {
463 add_property_constant(name_space, name, Arguments::get_property(name), CHECK);
464 }
465
466 /*
467 * Adds a string constant of the given property. Retrieves the value via
468 * Arguments::get_property() and asserts the value for the does not differ from
469 * the value retrievable from System.getProperty()
470 */
471 void PerfDataManager::add_optional_property_constant(CounterNS name_space, const char* name, TRAPS) {
472 const char* value = Arguments::get_property(name);
473
474 if (value != nullptr) {
475 add_property_constant(name_space, name, value, CHECK);
476 }
477 }
478
479 void PerfDataManager::create_system_property_instrumentation(TRAPS) {
480
481 // Non-writeable, constant properties
482 add_property_constant(JAVA_PROPERTY, "java.vm.specification.name", "Java Virtual Machine Specification", CHECK);
483 add_property_constant(JAVA_PROPERTY, "java.version", JDK_Version::java_version(), CHECK);
484 add_property_constant(JAVA_PROPERTY, "java.vm.version", VM_Version::vm_release(), CHECK);
485 add_property_constant(JAVA_PROPERTY, "java.vm.name", VM_Version::vm_name(), CHECK);
486 add_property_constant(JAVA_PROPERTY, "java.vm.vendor", VM_Version::vm_vendor(), CHECK);
487 add_property_constant(JAVA_PROPERTY, "jdk.debug", VM_Version::jdk_debug_level(), CHECK);
488
489 // Get remaining property constants via Arguments::get_property,
490 // which does a linear search over the internal system properties list.
491
492 // SUN_PROPERTY properties
493 add_property_constant(SUN_PROPERTY, "sun.boot.library.path", CHECK);
494
495 // JAVA_PROPERTY properties
496 add_property_constant(JAVA_PROPERTY, "java.vm.specification.version", CHECK);
497 add_property_constant(JAVA_PROPERTY, "java.vm.specification.vendor", CHECK);
498 add_property_constant(JAVA_PROPERTY, "java.vm.info", CHECK);
499 add_property_constant(JAVA_PROPERTY, "java.library.path", CHECK);
500 add_property_constant(JAVA_PROPERTY, "java.class.path", CHECK);
501 add_property_constant(JAVA_PROPERTY, "java.home", CHECK);
502
503 add_optional_property_constant(JAVA_PROPERTY, "jdk.module.path", CHECK);
504 add_optional_property_constant(JAVA_PROPERTY, "jdk.module.upgrade.path", CHECK);
505 add_optional_property_constant(JAVA_PROPERTY, "jdk.module.main", CHECK);
506 }
507
508 void PerfDataManager::create_misc_perfdata() {
509
510 ResourceMark rm;
511 EXCEPTION_MARK;
512
513 // numeric constants
514
515 // frequency of the native high resolution timer
516 create_constant(SUN_OS, "hrt.frequency", PerfData::U_Hertz,
517 os::elapsed_frequency(), CHECK);
518
519 // string constants
520
521 // create string instrumentation for various Java properties.
522 create_system_property_instrumentation(CHECK);
523
524 // HotSpot flags (from .hotspotrc) and args (from command line)
525 //
526 create_string_constant(JAVA_RT, "vmFlags", Arguments::jvm_flags(), CHECK);
527 create_string_constant(JAVA_RT, "vmArgs", Arguments::jvm_args(), CHECK);
528
529 // java class name/jar file and arguments to main class
530 // note: name is coordinated with launcher and Arguments.cpp
531 create_string_constant(SUN_RT, "javaCommand", Arguments::java_command(), CHECK);
532
533 // the Java VM Internal version string
534 create_string_constant(SUN_RT, "internalVersion",
535 VM_Version::internal_vm_info_string(), CHECK);
536 }
537
538 PerfDataList::PerfDataList(int length) {
539
540 _set = new (mtInternal) PerfDataArray(length, mtInternal);
541 }
542
543 PerfDataList::PerfDataList(PerfDataList* p) {
544
545 _set = new (mtInternal) PerfDataArray(p->length(), mtInternal);
546
547 _set->appendAll(p->get_impl());
548 }
549
550 PerfDataList::~PerfDataList() {
551
552 delete _set;
553
554 }
555
556 PerfData* PerfDataList::find_by_name(const char* name) {
557
558 int i = _set->find_if([&](PerfData* pd) { return pd->name_equals(name); });
559
560 if (i >= 0 && i <= _set->length())
561 return _set->at(i);
562 else
563 return nullptr;
564 }
565
566 PerfDataList* PerfDataList::clone() {
567
568 PerfDataList* copy = new PerfDataList(this);
569
570 assert(copy != nullptr, "just checking");
571
572 return copy;
573 }
574