1 /* 2 * Copyright (c) 2012, 2018, 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 "jvm.h" 27 #include "classfile/classLoaderStats.hpp" 28 #include "classfile/javaClasses.hpp" 29 #include "code/codeCache.hpp" 30 #include "compiler/compileBroker.hpp" 31 #include "gc_implementation/g1/g1HeapRegionEventSender.hpp" 32 #include "gc_implementation/shared/gcConfiguration.hpp" 33 #include "gc_implementation/shared/gcTrace.hpp" 34 #include "gc_implementation/shared/objectCountEventSender.hpp" 35 #include "gc_implementation/shared/vmGCOperations.hpp" 36 #include "jfr/jfrEvents.hpp" 37 #include "jfr/periodic/jfrOSInterface.hpp" 38 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp" 39 #include "jfr/periodic/jfrThreadDumpEvent.hpp" 40 #include "jfr/periodic/jfrNetworkUtilization.hpp" 41 #include "jfr/recorder/jfrRecorder.hpp" 42 #include "jfr/support/jfrThreadId.hpp" 43 #include "jfr/utilities/jfrTime.hpp" 44 #include "jfrfiles/jfrPeriodic.hpp" 45 #include "memory/heapInspection.hpp" 46 #include "memory/resourceArea.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "runtime/arguments.hpp" 49 #include "runtime/globals.hpp" 50 #include "runtime/os.hpp" 51 #include "runtime/os_perf.hpp" 52 #include "runtime/thread.inline.hpp" 53 #include "runtime/sweeper.hpp" 54 #include "runtime/vmThread.hpp" 55 #include "services/classLoadingService.hpp" 56 #include "services/management.hpp" 57 #include "services/threadService.hpp" 58 #include "utilities/exceptions.hpp" 59 #include "utilities/globalDefinitions.hpp" 60 61 /** 62 * JfrPeriodic class 63 * Implementation of declarations in 64 * xsl generated traceRequestables.hpp 65 */ 66 #define TRACE_REQUEST_FUNC(id) void JfrPeriodicEventSet::request##id(void) 67 68 TRACE_REQUEST_FUNC(JVMInformation) { 69 ResourceMark rm; 70 EventJVMInformation event; 71 event.set_jvmName(VM_Version::vm_name()); 72 event.set_jvmVersion(VM_Version::internal_vm_info_string()); 73 event.set_javaArguments(Arguments::java_command()); 74 event.set_jvmArguments(Arguments::jvm_args()); 75 event.set_jvmFlags(Arguments::jvm_flags()); 76 event.set_jvmStartTime(Management::vm_init_done_time()); 77 event.set_pid(os::current_process_id()); 78 event.commit(); 79 } 80 81 TRACE_REQUEST_FUNC(OSInformation) { 82 ResourceMark rm; 83 char* os_name = NEW_RESOURCE_ARRAY(char, 2048); 84 JfrOSInterface::os_version(&os_name); 85 EventOSInformation event; 86 event.set_osVersion(os_name); 87 event.commit(); 88 } 89 90 /* 91 * This is left empty on purpose, having ExecutionSample as a requestable 92 * is a way of getting the period. The period is passed to ThreadSampling::update_period. 93 * Implementation in jfrSamples.cpp 94 */ 95 TRACE_REQUEST_FUNC(ExecutionSample) { 96 } 97 TRACE_REQUEST_FUNC(NativeMethodSample) { 98 } 99 100 TRACE_REQUEST_FUNC(ThreadDump) { 101 ResourceMark rm; 102 EventThreadDump event; 103 event.set_result(JfrDcmdEvent::thread_dump()); 104 event.commit(); 105 } 106 107 static int _native_library_callback(const char* name, address base, address top, void *param) { 108 EventNativeLibrary event(UNTIMED); 109 event.set_name(name); 110 event.set_baseAddress((u8)base); 111 event.set_topAddress((u8)top); 112 event.set_endtime(*(JfrTicks*) param); 113 event.commit(); 114 return 0; 115 } 116 117 TRACE_REQUEST_FUNC(NativeLibrary) { 118 JfrTicks ts= JfrTicks::now(); 119 os::get_loaded_modules_info(&_native_library_callback, (void *)&ts); 120 } 121 122 TRACE_REQUEST_FUNC(InitialEnvironmentVariable) { 123 JfrOSInterface::generate_initial_environment_variable_events(); 124 } 125 126 TRACE_REQUEST_FUNC(CPUInformation) { 127 CPUInformation cpu_info; 128 int ret_val = JfrOSInterface::cpu_information(cpu_info); 129 if (ret_val == OS_ERR) { 130 if (LogJFR) tty->print_cr( "Unable to generate requestable event CPUInformation"); 131 return; 132 } 133 if (ret_val == FUNCTIONALITY_NOT_IMPLEMENTED) { 134 return; 135 } 136 if (ret_val == OS_OK) { 137 EventCPUInformation event; 138 event.set_cpu(cpu_info.cpu_name()); 139 event.set_description(cpu_info.cpu_description()); 140 event.set_sockets(cpu_info.number_of_sockets()); 141 event.set_cores(cpu_info.number_of_cores()); 142 event.set_hwThreads(cpu_info.number_of_hardware_threads()); 143 event.commit(); 144 } 145 } 146 147 TRACE_REQUEST_FUNC(CPULoad) { 148 double u = 0; // user time 149 double s = 0; // kernel time 150 double t = 0; // total time 151 int ret_val = JfrOSInterface::cpu_loads_process(&u, &s, &t); 152 if (ret_val == OS_ERR) { 153 if (LogJFR) tty->print_cr( "Unable to generate requestable event CPULoad"); 154 return; 155 } 156 if (ret_val == OS_OK) { 157 EventCPULoad event; 158 event.set_jvmUser((float)u); 159 event.set_jvmSystem((float)s); 160 event.set_machineTotal((float)t); 161 event.commit(); 162 } 163 } 164 165 TRACE_REQUEST_FUNC(ThreadCPULoad) { 166 JfrThreadCPULoadEvent::send_events(); 167 } 168 169 TRACE_REQUEST_FUNC(NetworkUtilization) { 170 JfrNetworkUtilization::send_events(); 171 } 172 173 TRACE_REQUEST_FUNC(CPUTimeStampCounter) { 174 EventCPUTimeStampCounter event; 175 event.set_fastTimeEnabled(JfrTime::is_ft_enabled()); 176 event.set_fastTimeAutoEnabled(JfrTime::is_ft_supported()); 177 event.set_osFrequency(os::elapsed_frequency()); 178 event.set_fastTimeFrequency(JfrTime::frequency()); 179 event.commit(); 180 } 181 182 TRACE_REQUEST_FUNC(SystemProcess) { 183 char pid_buf[16]; 184 SystemProcess* processes = NULL; 185 int num_of_processes = 0; 186 JfrTicks start_time = JfrTicks::now(); 187 int ret_val = JfrOSInterface::system_processes(&processes, &num_of_processes); 188 if (ret_val == OS_ERR) { 189 if (LogJFR) tty->print_cr( "Unable to generate requestable event SystemProcesses"); 190 return; 191 } 192 JfrTicks end_time = JfrTicks::now(); 193 if (ret_val == FUNCTIONALITY_NOT_IMPLEMENTED) { 194 return; 195 } 196 if (ret_val == OS_OK) { 197 // feature is implemented, write real event 198 while (processes != NULL) { 199 SystemProcess* tmp = processes; 200 const char* info = processes->command_line(); 201 if (info == NULL) { 202 info = processes->path(); 203 } 204 if (info == NULL) { 205 info = processes->name(); 206 } 207 if (info == NULL) { 208 info = "?"; 209 } 210 jio_snprintf(pid_buf, sizeof(pid_buf), "%d", processes->pid()); 211 EventSystemProcess event(UNTIMED); 212 event.set_pid(pid_buf); 213 event.set_commandLine(info); 214 event.set_starttime(start_time); 215 event.set_endtime(end_time); 216 event.commit(); 217 processes = processes->next(); 218 delete tmp; 219 } 220 } 221 } 222 223 TRACE_REQUEST_FUNC(ThreadContextSwitchRate) { 224 double rate = 0.0; 225 int ret_val = JfrOSInterface::context_switch_rate(&rate); 226 if (ret_val == OS_ERR) { 227 if (LogJFR) tty->print_cr( "Unable to generate requestable event ThreadContextSwitchRate"); 228 return; 229 } 230 if (ret_val == FUNCTIONALITY_NOT_IMPLEMENTED) { 231 return; 232 } 233 if (ret_val == OS_OK) { 234 EventThreadContextSwitchRate event; 235 event.set_switchRate((float)rate + 0.0f); 236 event.commit(); 237 } 238 } 239 240 #define SEND_FLAGS_OF_TYPE(eventType, flagType) \ 241 do { \ 242 Flag *flag = Flag::flags; \ 243 while (flag->_name != NULL) { \ 244 if (flag->is_ ## flagType()) { \ 245 if (flag->is_unlocked()) { \ 246 Event ## eventType event; \ 247 event.set_name(flag->_name); \ 248 event.set_value(flag->get_ ## flagType()); \ 249 event.set_origin(flag->get_origin()); \ 250 event.commit(); \ 251 } \ 252 } \ 253 ++flag; \ 254 } \ 255 } while (0) 256 257 TRACE_REQUEST_FUNC(IntFlag) { 258 SEND_FLAGS_OF_TYPE(IntFlag, intx); 259 } 260 261 TRACE_REQUEST_FUNC(UnsignedIntFlag) { 262 SEND_FLAGS_OF_TYPE(UnsignedIntFlag, uintx); 263 } 264 265 TRACE_REQUEST_FUNC(LongFlag) { 266 SEND_FLAGS_OF_TYPE(LongFlag, intx); 267 } 268 269 TRACE_REQUEST_FUNC(UnsignedLongFlag) { 270 SEND_FLAGS_OF_TYPE(UnsignedLongFlag, uintx); 271 SEND_FLAGS_OF_TYPE(UnsignedLongFlag, uint64_t); 272 } 273 274 TRACE_REQUEST_FUNC(DoubleFlag) { 275 SEND_FLAGS_OF_TYPE(DoubleFlag, double); 276 } 277 278 TRACE_REQUEST_FUNC(BooleanFlag) { 279 SEND_FLAGS_OF_TYPE(BooleanFlag, bool); 280 } 281 282 TRACE_REQUEST_FUNC(StringFlag) { 283 SEND_FLAGS_OF_TYPE(StringFlag, ccstr); 284 } 285 286 class VM_GC_SendObjectCountEvent : public VM_GC_HeapInspection { 287 public: 288 VM_GC_SendObjectCountEvent() : VM_GC_HeapInspection(NULL, true) {} 289 virtual void doit() { 290 ObjectCountEventSender::enable_requestable_event(); 291 collect(); 292 ObjectCountEventSender::disable_requestable_event(); 293 } 294 }; 295 296 TRACE_REQUEST_FUNC(ObjectCount) { 297 VM_GC_SendObjectCountEvent op; 298 VMThread::execute(&op); 299 } 300 301 class VM_G1SendHeapRegionInfoEvents : public VM_Operation { 302 virtual void doit() { 303 G1HeapRegionEventSender::send_events(); 304 } 305 virtual VMOp_Type type() const { return VMOp_HeapIterateOperation; } 306 }; 307 308 TRACE_REQUEST_FUNC(G1HeapRegionInformation) { 309 if (UseG1GC) { 310 VM_G1SendHeapRegionInfoEvents op; 311 VMThread::execute(&op); 312 } 313 } 314 315 // Java Mission Control (JMC) uses (Java) Long.MIN_VALUE to describe that a 316 // long value is undefined. 317 static jlong jmc_undefined_long = min_jlong; 318 319 TRACE_REQUEST_FUNC(GCConfiguration) { 320 GCConfiguration conf; 321 jlong pause_target = conf.has_pause_target_default_value() ? jmc_undefined_long : conf.pause_target(); 322 EventGCConfiguration event; 323 event.set_youngCollector(conf.young_collector()); 324 event.set_oldCollector(conf.old_collector()); 325 event.set_parallelGCThreads(conf.num_parallel_gc_threads()); 326 event.set_concurrentGCThreads(conf.num_concurrent_gc_threads()); 327 event.set_usesDynamicGCThreads(conf.uses_dynamic_gc_threads()); 328 event.set_isExplicitGCConcurrent(conf.is_explicit_gc_concurrent()); 329 event.set_isExplicitGCDisabled(conf.is_explicit_gc_disabled()); 330 event.set_gcTimeRatio(conf.gc_time_ratio()); 331 event.set_pauseTarget((s8)pause_target); 332 event.commit(); 333 } 334 335 TRACE_REQUEST_FUNC(GCTLABConfiguration) { 336 GCTLABConfiguration conf; 337 EventGCTLABConfiguration event; 338 event.set_usesTLABs(conf.uses_tlabs()); 339 event.set_minTLABSize(conf.min_tlab_size()); 340 event.set_tlabRefillWasteLimit(conf.tlab_refill_waste_limit()); 341 event.commit(); 342 } 343 344 TRACE_REQUEST_FUNC(GCSurvivorConfiguration) { 345 GCSurvivorConfiguration conf; 346 EventGCSurvivorConfiguration event; 347 event.set_maxTenuringThreshold(conf.max_tenuring_threshold()); 348 event.set_initialTenuringThreshold(conf.initial_tenuring_threshold()); 349 event.commit(); 350 } 351 352 TRACE_REQUEST_FUNC(GCHeapConfiguration) { 353 GCHeapConfiguration conf; 354 EventGCHeapConfiguration event; 355 event.set_minSize(conf.min_size()); 356 event.set_maxSize(conf.max_size()); 357 event.set_initialSize(conf.initial_size()); 358 event.set_usesCompressedOops(conf.uses_compressed_oops()); 359 event.set_compressedOopsMode(conf.narrow_oop_mode()); 360 event.set_objectAlignment(conf.object_alignment_in_bytes()); 361 event.set_heapAddressBits(conf.heap_address_size_in_bits()); 362 event.commit(); 363 } 364 365 TRACE_REQUEST_FUNC(YoungGenerationConfiguration) { 366 GCYoungGenerationConfiguration conf; 367 jlong max_size = conf.has_max_size_default_value() ? jmc_undefined_long : conf.max_size(); 368 EventYoungGenerationConfiguration event; 369 event.set_maxSize((u8)max_size); 370 event.set_minSize(conf.min_size()); 371 event.set_newRatio(conf.new_ratio()); 372 event.commit(); 373 } 374 375 TRACE_REQUEST_FUNC(InitialSystemProperty) { 376 SystemProperty* p = Arguments::system_properties(); 377 JfrTicks time_stamp = JfrTicks::now(); 378 while (p != NULL) { 379 if (true/* XXX fix me if you want !p->internal()*/) { 380 EventInitialSystemProperty event(UNTIMED); 381 event.set_key(p->key()); 382 event.set_value(p->value()); 383 event.set_endtime(time_stamp); 384 event.commit(); 385 } 386 p = p->next(); 387 } 388 } 389 390 TRACE_REQUEST_FUNC(ThreadAllocationStatistics) { 391 ResourceMark rm; 392 int initial_size = Threads::number_of_threads(); 393 GrowableArray<jlong> allocated(initial_size); 394 GrowableArray<traceid> thread_ids(initial_size); 395 JfrTicks time_stamp = JfrTicks::now(); 396 { 397 // Collect allocation statistics while holding threads lock 398 MutexLockerEx ml(Threads_lock); 399 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 400 allocated.append(thread->cooked_allocated_bytes()); 401 thread_ids.append(JFR_THREAD_ID(thread)); 402 } 403 } 404 405 // Write allocation statistics to buffer. 406 for(int i = 0; i < thread_ids.length(); i++) { 407 EventThreadAllocationStatistics event(UNTIMED); 408 event.set_allocated(allocated.at(i)); 409 event.set_thread(thread_ids.at(i)); 410 event.set_endtime(time_stamp); 411 event.commit(); 412 } 413 } 414 415 /** 416 * PhysicalMemory event represents: 417 * 418 * @totalSize == The amount of physical memory (hw) installed and reported by the OS, in bytes. 419 * @usedSize == The amount of physical memory currently in use in the system (reserved/committed), in bytes. 420 * 421 * Both fields are systemwide, i.e. represents the entire OS/HW environment. 422 * These fields do not include virtual memory. 423 * 424 * If running inside a guest OS on top of a hypervisor in a virtualized environment, 425 * the total memory reported is the amount of memory configured for the guest OS by the hypervisor. 426 */ 427 TRACE_REQUEST_FUNC(PhysicalMemory) { 428 u8 totalPhysicalMemory = os::physical_memory(); 429 EventPhysicalMemory event; 430 event.set_totalSize(totalPhysicalMemory); 431 event.set_usedSize(totalPhysicalMemory - os::available_memory()); 432 event.commit(); 433 } 434 435 TRACE_REQUEST_FUNC(JavaThreadStatistics) { 436 EventJavaThreadStatistics event; 437 event.set_activeCount(ThreadService::get_live_thread_count()); 438 event.set_daemonCount(ThreadService::get_daemon_thread_count()); 439 event.set_accumulatedCount(ThreadService::get_total_thread_count()); 440 event.set_peakCount(ThreadService::get_peak_thread_count()); 441 event.commit(); 442 } 443 444 TRACE_REQUEST_FUNC(ClassLoadingStatistics) { 445 EventClassLoadingStatistics event; 446 event.set_loadedClassCount(ClassLoadingService::loaded_class_count()); 447 event.set_unloadedClassCount(ClassLoadingService::unloaded_class_count()); 448 event.commit(); 449 } 450 451 class JfrClassLoaderStatsClosure : public ClassLoaderStatsClosure { 452 public: 453 JfrClassLoaderStatsClosure() : ClassLoaderStatsClosure(NULL) {} 454 455 bool do_entry(oop const& key, ClassLoaderStats* const& cls) { 456 const ClassLoaderData* this_cld = cls->_class_loader != NULL ? 457 java_lang_ClassLoader::loader_data(cls->_class_loader) : (ClassLoaderData*)NULL; 458 const ClassLoaderData* parent_cld = cls->_parent != NULL ? 459 java_lang_ClassLoader::loader_data(cls->_parent) : (ClassLoaderData*)NULL; 460 EventClassLoaderStatistics event; 461 event.set_classLoader(this_cld); 462 event.set_parentClassLoader(parent_cld); 463 event.set_classLoaderData((intptr_t)cls->_cld); 464 event.set_classCount(cls->_classes_count); 465 event.set_chunkSize(cls->_chunk_sz); 466 event.set_blockSize(cls->_block_sz); 467 event.set_anonymousClassCount(cls->_anon_classes_count); 468 event.set_anonymousChunkSize(cls->_anon_chunk_sz); 469 event.set_anonymousBlockSize(cls->_anon_block_sz); 470 event.commit(); 471 return true; 472 } 473 474 void createEvents(void) { 475 _stats->iterate(this); 476 } 477 }; 478 479 class JfrClassLoaderStatsVMOperation : public ClassLoaderStatsVMOperation { 480 public: 481 JfrClassLoaderStatsVMOperation() : ClassLoaderStatsVMOperation(NULL) { } 482 483 void doit() { 484 JfrClassLoaderStatsClosure clsc; 485 ClassLoaderDataGraph::cld_do(&clsc); 486 clsc.createEvents(); 487 } 488 }; 489 490 TRACE_REQUEST_FUNC(ClassLoaderStatistics) { 491 JfrClassLoaderStatsVMOperation op; 492 VMThread::execute(&op); 493 } 494 495 TRACE_REQUEST_FUNC(CompilerStatistics) { 496 EventCompilerStatistics event; 497 event.set_compileCount(CompileBroker::get_total_compile_count()); 498 event.set_bailoutCount(CompileBroker::get_total_bailout_count()); 499 event.set_invalidatedCount(CompileBroker::get_total_invalidated_count()); 500 event.set_osrCompileCount(CompileBroker::get_total_osr_compile_count()); 501 event.set_standardCompileCount(CompileBroker::get_total_standard_compile_count()); 502 event.set_osrBytesCompiled(CompileBroker::get_sum_osr_bytes_compiled()); 503 event.set_standardBytesCompiled(CompileBroker::get_sum_standard_bytes_compiled()); 504 event.set_nmetodsSize(CompileBroker::get_sum_nmethod_size()); 505 event.set_nmetodCodeSize(CompileBroker::get_sum_nmethod_code_size()); 506 event.set_peakTimeSpent(CompileBroker::get_peak_compilation_time()); 507 event.set_totalTimeSpent(CompileBroker::get_total_compilation_time()); 508 event.commit(); 509 } 510 511 TRACE_REQUEST_FUNC(CompilerConfiguration) { 512 EventCompilerConfiguration event; 513 event.set_threadCount(CICompilerCount); 514 event.set_tieredCompilation(TieredCompilation); 515 event.commit(); 516 } 517 518 TRACE_REQUEST_FUNC(CodeCacheStatistics) { 519 EventCodeCacheStatistics event; 520 event.set_codeBlobType((u1)0/*bt*/); // XXX 521 event.set_startAddress((u8)CodeCache::low_bound()); 522 event.set_reservedTopAddress((u8)CodeCache::high_bound()); 523 event.set_entryCount(CodeCache::nof_blobs()); 524 event.set_methodCount(CodeCache::nof_nmethods()); 525 event.set_adaptorCount(CodeCache::nof_adapters()); 526 event.set_unallocatedCapacity(CodeCache::unallocated_capacity()); 527 event.set_fullCount(CodeCache::get_codemem_full_count()); 528 event.commit(); 529 } 530 531 TRACE_REQUEST_FUNC(CodeCacheConfiguration) { 532 EventCodeCacheConfiguration event; 533 event.set_initialSize(InitialCodeCacheSize); 534 event.set_reservedSize(ReservedCodeCacheSize); 535 event.set_nonNMethodSize(0/*NonNMethodCodeHeapSize*/); // XXX 536 event.set_profiledSize(0/*ProfiledCodeHeapSize*/); // XXX 537 event.set_nonProfiledSize(0/*NonProfiledCodeHeapSize*/); // XXX 538 event.set_expansionSize(CodeCacheExpansionSize); 539 event.set_minBlockLength(CodeCacheMinBlockLength); 540 event.set_startAddress((u8)CodeCache::low_bound()); 541 event.set_reservedTopAddress((u8)CodeCache::high_bound()); 542 event.commit(); 543 } 544 545 TRACE_REQUEST_FUNC(CodeSweeperStatistics) { 546 EventCodeSweeperStatistics event; 547 event.set_sweepCount(NMethodSweeper::traversal_count()); 548 event.set_methodReclaimedCount(NMethodSweeper::total_nof_methods_reclaimed()); 549 event.set_totalSweepTime(NMethodSweeper::total_time_sweeping()); 550 event.set_peakFractionTime(NMethodSweeper::peak_sweep_fraction_time()); 551 event.set_peakSweepTime(NMethodSweeper::peak_sweep_time()); 552 event.commit(); 553 } 554 555 TRACE_REQUEST_FUNC(CodeSweeperConfiguration) { 556 EventCodeSweeperConfiguration event; 557 event.set_sweeperEnabled(MethodFlushing); 558 event.set_flushingEnabled(UseCodeCacheFlushing); 559 event.commit(); 560 }