GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — async-profiler/src/flightRecorder.cpp at jonoffcpu-dev · jonoffcpu/async-profiler Expand file tree/ Copy pathflightRecorder.cppMore file actions 1718 lines (1472 loc) · 57.9 KB / Copy pathflightRecorder.cppFile metadata and controls1718 lines (1472 loc) · 57.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* * Copyright The async-profiler authors * SPDX-License-Identifier: Apache-2.0 */ #include <assert.h> #include <map> #include <string> #include <arpa/inet.h> #include <errno.h> #include <fcntl.h> #include <stdint.h> #include <signal.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/utsname.h> #include <unistd.h> #include "flightRecorder.h" #include "incbin.h" #include "javaApi.h" #include "jfrMetadata.h" #include "lookup.h" #include "os.h" #include "processSampler.h" #include "profiler.h" #include "rateLimit.h" #include "spinLock.h" #include "symbols.h" #include "threadFilter.h" #include "threadLocalData.h" #include "tsc.h" #include "userEvents.h" #include "vmStructs.h" INCLUDE_HELPER_CLASS(JFR_SYNC_NAME, JFR_SYNC_CLASS, "one/profiler/JfrSync") static void JNICALL JfrSync_stopProfiler(JNIEnv* env, jclass cls) { Profiler::instance()->stop(false, "master-stop"); } const int SMALL_BUFFER_SIZE = 1024; const int SMALL_BUFFER_LIMIT = SMALL_BUFFER_SIZE - 128; const int RECORDING_BUFFER_SIZE = 65536; const int RECORDING_BUFFER_LIMIT = RECORDING_BUFFER_SIZE - 4096; const int MAX_STRING_LENGTH = 8191; const u64 MAX_JLONG = 0x7fffffffffffffffULL; const u64 MIN_JLONG = 0x8000000000000000ULL; enum GCWhen { BEFORE_GC, AFTER_GC }; static constexpr unsigned char JFR_TYPE_FOR_CATEGORY[EC_CATEGORIES] = { T_EXECUTION_SAMPLE, // EC_CPU T_ALLOC_IN_NEW_TLAB, // EC_ALLOC T_MONITOR_ENTER, // EC_LOCK T_WALL_CLOCK_SAMPLE, // EC_WALL T_MALLOC, // EC_NATIVEMEM T_NATIVE_LOCK, // EC_NATIVELOCK T_METHOD_TRACE, // EC_TRACE T_SPAN, // EC_SPAN }; static SpinLock _rec_lock(1); static jclass _jfr_sync_class = NULL; static jmethodID _start_method; static jmethodID _stop_method; static jmethodID _box_method; static bool _jfr_starting = false; static const char* const SETTING_CSTACK[] = {NULL, "no", "fp", "dwarf", "vm"}; struct CpuTime { u64 real; u64 user; u64 system; }; struct CpuTimes { CpuTime proc; CpuTime total; }; class Buffer { private: int _offset; char _data[0]; protected: Buffer() : _offset(0) { } public: const char* data() const { return _data; } int offset() const { return _offset; } int skip(int delta) { int offset = _offset; _offset = offset + delta; return offset; } void reset() { _offset = 0; } void put(const char* v, u32 len) { memcpy(_data + _offset, v, len); _offset += (int)len; } void put8(char v) { _data[_offset++] = v; } void put16(short v) { *(short*)(_data + _offset) = htons(v); _offset += 2; } void put32(int v) { *(int*)(_data + _offset) = htonl(v); _offset += 4; } void put64(u64 v) { *(u64*)(_data + _offset) = OS::hton64(v); _offset += 8; } void putFloat(float v) { union { float f; int i; } u; u.f = v; put32(u.i); } void putVar32(u32 v) { while (v > 0x7f) { _data[_offset++] = (char)v | 0x80; v >>= 7; } _data[_offset++] = (char)v; } void putVar64(u64 v) { int iter = 0; while (v > 0x1fffff) { _data[_offset++] = (char)v | 0x80; v >>= 7; _data[_offset++] = (char)v | 0x80; v >>= 7; _data[_offset++] = (char)v | 0x80; v >>= 7; if (++iter == 3) return; } while (v > 0x7f) { _data[_offset++] = (char)v | 0x80; v >>= 7; } _data[_offset++] = (char)v; } void putUtf8(const char* v) { if (v == NULL) { put8(0); } else { size_t len = strlen(v); putUtf8(v, len < MAX_STRING_LENGTH ? len : MAX_STRING_LENGTH); } } void putUtf8(const char* v, u32 len) { put8(3); putVar32(len); put(v, len); } void putByteString(const char* v, u32 len) { put8(5); // STRING_ENCODING_LATIN1_BYTE_ARRAY putVar32(len); put(v, len); } void putByteString(const char* v) { if (v == NULL) { put8(0); } else { size_t len = strlen(v); putByteString(v, len < MAX_STRING_LENGTH ? len : MAX_STRING_LENGTH); } } void put8(int offset, char v) { _data[offset] = v; } void putVar32(int offset, u32 v) { _data[offset] = v | 0x80; _data[offset + 1] = (v >> 7) | 0x80; _data[offset + 2] = (v >> 14) | 0x80; _data[offset + 3] = (v >> 21) | 0x80; _data[offset + 4] = (v >> 28); } }; class SmallBuffer : public Buffer { private: char _buf[SMALL_BUFFER_SIZE - sizeof(Buffer)]; public: SmallBuffer() : Buffer() { } }; class RecordingBuffer : public Buffer { private: char _buf[RECORDING_BUFFER_SIZE - sizeof(Buffer)]; public: RecordingBuffer() : Buffer() { } }; class Recording { private: static char* _agent_properties; static char* _jvm_args; static char* _jvm_flags; static char* _java_command; RecordingBuffer _buf[CONCURRENCY_LEVEL]; int _fd; int _memfd; char* _master_recording_file; off_t _chunk_start; ThreadFilter _thread_set; MethodMap _method_map; Dictionary _string_pool; u64 _start_time; u64 _start_ticks; u64 _stop_time; u64 _stop_ticks; u64 _base_id; u64 _bytes_written; u64 _chunk_size; u64 _chunk_time; int _available_processors; int _recorded_lib_count; bool _in_memory; bool _cpu_monitor_enabled; bool _heap_monitor_enabled; u32 _last_gc_id; CpuTimes _last_times; SmallBuffer _monitor_buf; RecordingBuffer _proc_buf; ProcessSampler _process_sampler; bool _signal_capture; char* _signal_session_id; u32 _signal_capture_epoch; int _signal_number; const char* _signal_delivery; bool _io_error; bool _finished; static float ratio(float value) { return value < 0 ? 0 : value > 1 ? 1 : value; } void setIoError() { __atomic_store_n(&_io_error, true, __ATOMIC_RELAXED); } bool hasIoError() const { return __atomic_load_n(&_io_error, __ATOMIC_RELAXED); } public: Recording(int fd, const char* master_recording_file, Arguments& args) : _fd(fd) { _io_error = false; _finished = false; _master_recording_file = master_recording_file == NULL ? NULL : strdup(master_recording_file); _chunk_start = lseek(_fd, 0, SEEK_END); if (_chunk_start < 0) setIoError(); _start_time = OS::micros(); _start_ticks = TSC::ticks(); _base_id = 0; _bytes_written = 0; _memfd = -1; _in_memory = false; _signal_capture = args._signal_cookie; _signal_session_id = _signal_capture ? strdup(args._signal_id) : NULL; _signal_capture_epoch = (u32)args._signal_epoch; _signal_number = _signal_capture ? args._cookie_signal : 0; _signal_delivery = args._signal_cookie_delivery == SIGNAL_COOKIE_COALESCING ? "coalescing" : "queued"; _chunk_size = args._chunk_size <= 0 ? MAX_JLONG : (args._chunk_size < 262144 ? 262144 : args._chunk_size); _chunk_time = args._chunk_time <= 0 ? MAX_JLONG : (args._chunk_time < 5 ? 5 : args._chunk_time) * 1000000ULL; _available_processors = OS::getCpuCount(); writeHeader(_buf); writeMetadata(_buf); writeRecordingInfo(_buf); writeSignalCaptureContext(_buf); writeSettings(_buf, args); if (!args.hasOption(NO_SYSTEM_INFO)) { writeOsCpuInfo(_buf); writeJvmInfo(_buf); } if (!args.hasOption(NO_SYSTEM_PROPS)) { writeSystemProperties(_buf); } if (!args.hasOption(NO_NATIVE_LIBS)) { _recorded_lib_count = 0; writeNativeLibraries(_buf); } else { _recorded_lib_count = -1; } flush(_buf); if (args.hasOption(IN_MEMORY) && (_memfd = OS::createMemoryFile("async-profiler-recording")) >= 0) { _in_memory = true; } _cpu_monitor_enabled = !args.hasOption(NO_CPU_LOAD); if (_cpu_monitor_enabled) { _last_times.proc.real = OS::getProcessCpuTime(&_last_times.proc.user, &_last_times.proc.system); _last_times.total.real = OS::getTotalCpuTime(&_last_times.total.user, &_last_times.total.system); } _heap_monitor_enabled = !args.hasOption(NO_HEAP_SUMMARY) && VM::_totalMemory != NULL && VM::_freeMemory != NULL; _last_gc_id = 0; if (args._proc > 0) { _process_sampler.enable(args._proc * 1000000); } } ~Recording() { finish(); free(_signal_session_id); } bool finish() { if (_finished) return !hasIoError(); _finished = true; off_t chunk_end = finishChunk(); if (_memfd >= 0) { if (close(_memfd) != 0) setIoError(); _memfd = -1; } if (_master_recording_file != NULL) { if (chunk_end < 0 || !appendRecording(_master_recording_file, chunk_end)) setIoError(); free(_master_recording_file); _master_recording_file = NULL; } else if (_fd >= 0 && fsync(_fd) != 0) { setIoError(); } if (_fd >= 0 && close(_fd) != 0) setIoError(); _fd = -1; return !hasIoError(); } bool good() const { return !hasIoError(); } off_t finishChunk() { flush(&_monitor_buf); flush(&_proc_buf); writeNativeLibraries(_buf); for (int i = 0; i < CONCURRENCY_LEVEL; i++) { flush(&_buf[i]); } _stop_time = OS::micros(); _stop_ticks = TSC::ticks(); if (_memfd >= 0) { off_t memory_size = lseek(_memfd, 0, SEEK_CUR); if (memory_size < 0 || !OS::copyFile(_memfd, _fd, 0, memory_size)) setIoError(); _in_memory = false; } off_t cpool_offset = lseek(_fd, 0, SEEK_CUR); if (cpool_offset < 0) setIoError(); writeCpool(_buf); flush(_buf); off_t chunk_end = lseek(_fd, 0, SEEK_CUR); if (chunk_end < 0) setIoError(); // Patch cpool size field _buf->putVar32(0, chunk_end - cpool_offset); if (!pwriteAll(_buf->data(), 5, cpool_offset)) setIoError(); // Workaround for JDK-8191415: compute actual TSC frequency, in case JFR is wrong u64 tsc_frequency; if (TSC::enabled()) { tsc_frequency = (u64)(double(_stop_ticks - _start_ticks) / double(_stop_time - _start_time) * 1000000); } else { // TSC is not enabled, so frequency can be used to get the constant clock tick frequency tsc_frequency = TSC::frequency(); } // Patch chunk header _buf->put64(chunk_end - _chunk_start); _buf->put64(cpool_offset - _chunk_start); _buf->put64(68); _buf->put64(_start_time * 1000); _buf->put64((_stop_time - _start_time) * 1000); _buf->put64(_start_ticks); _buf->put64(tsc_frequency); if (!pwriteAll(_buf->data(), 56, _chunk_start + 8)) setIoError(); OS::freePageCache(_fd, _chunk_start); _buf->reset(); return chunk_end; } void switchChunk() { _chunk_start = finishChunk(); _start_time = _stop_time; _start_ticks = _stop_ticks; _base_id += 0x1000000; _bytes_written = 0; writeHeader(_buf); writeMetadata(_buf); writeRecordingInfo(_buf); writeSignalCaptureContext(_buf); flush(_buf); if (_memfd >= 0) { while (ftruncate(_memfd, 0) < 0 && errno == EINTR); // restart if interrupted _in_memory = true; } } bool needSwitchChunk(u64 wall_time) { return loadAcquire(_bytes_written) >= _chunk_size || wall_time - _start_time >= _chunk_time; } size_t usedMemory() { return _method_map.usedMemory() + _thread_set.usedMemory() + (_memfd >= 0 ? lseek(_memfd, 0, SEEK_CUR) : 0); } void cpuMonitorCycle() { if (!_cpu_monitor_enabled) return; CpuTimes times; times.proc.real = OS::getProcessCpuTime(×.proc.user, ×.proc.system); times.total.real = OS::getTotalCpuTime(×.total.user, ×.total.system); float proc_user = 0, proc_system = 0, machine_total = 0; if (times.proc.real != (u64)-1 && times.proc.real > _last_times.proc.real) { float delta = (times.proc.real - _last_times.proc.real) * _available_processors; proc_user = ratio((times.proc.user - _last_times.proc.user) / delta); proc_system = ratio((times.proc.system - _last_times.proc.system) / delta); } if (times.total.real != (u64)-1 && times.total.real > _last_times.total.real) { float delta = times.total.real - _last_times.total.real; machine_total = ratio(((times.total.user + times.total.system) - (_last_times.total.user + _last_times.total.system)) / delta); if (machine_total < proc_user + proc_system) { machine_total = ratio(proc_user + proc_system); } } recordCpuLoad(&_monitor_buf, proc_user, proc_system, machine_total); flushIfNeeded(&_monitor_buf, SMALL_BUFFER_LIMIT); _last_times = times; } void heapMonitorCycle(u32 gc_id) { if (!_heap_monitor_enabled || gc_id == _last_gc_id) return; recordHeapSummary(&_monitor_buf, gc_id, AFTER_GC, VM::_totalMemory(), VM::_freeMemory()); flushIfNeeded(&_monitor_buf, SMALL_BUFFER_LIMIT); _last_gc_id = gc_id; } void processMonitorCycle(const u64 wall_time) { if (!_process_sampler.shouldSample(wall_time)) return; const u64 deadline_ns = OS::nanotime() + MAX_TIME_NS; const int process_count = _process_sampler.sample(wall_time); for (int pid_index = 0; pid_index < process_count; pid_index++) { const u64 current_time = OS::nanotime(); if (current_time > deadline_ns) { Log::debug("Sampled %d of %d processes due to time limit", pid_index, process_count); break; } ProcessInfo info; if (_process_sampler.getProcessInfo(pid_index, current_time, info)) { flushIfNeeded(&_proc_buf, RECORDING_BUFFER_LIMIT - MAX_PROCESS_SAMPLE_JFR_EVENT_LENGTH); recordProcessSample(&_proc_buf, &info); } } } bool hasMasterRecording() const { return _master_recording_file != NULL; } bool appendRecording(const char* target_file, size_t size) { int append_fd = open(target_file, O_WRONLY); if (append_fd >= 0) { bool result = lseek(append_fd, 0, SEEK_END) >= 0 && OS::copyFile(_fd, append_fd, 0, size) && fsync(append_fd) == 0; if (close(append_fd) != 0) result = false; return result; } else { Log::warn("Failed to open JFR recording at %s: %s", target_file, strerror(errno)); return false; } } Buffer* buffer(int lock_index) { return &_buf[lock_index]; } bool parseAgentProperties() { JNIEnv* env = VM::jni(); jclass vm_support = env->FindClass("jdk/internal/vm/VMSupport"); if (vm_support == NULL) { env->ExceptionClear(); vm_support = env->FindClass("sun/misc/VMSupport"); } if (vm_support != NULL) { jmethodID get_agent_props = env->GetStaticMethodID(vm_support, "getAgentProperties", "()Ljava/util/Properties;"); jmethodID to_string = env->GetMethodID(env->FindClass("java/lang/Object"), "toString", "()Ljava/lang/String;"); if (get_agent_props != NULL && to_string != NULL) { jobject props = env->CallStaticObjectMethod(vm_support, get_agent_props); if (props != NULL && !env->ExceptionCheck()) { jstring str = (jstring)env->CallObjectMethod(props, to_string); if (str != NULL && !env->ExceptionCheck()) { _agent_properties = (char*)env->GetStringUTFChars(str, NULL); } } } } env->ExceptionClear(); if (_agent_properties == NULL) { return false; } char* p = _agent_properties + 1; p[strlen(p) - 1] = 0; while (*p) { if (strncmp(p, "sun.jvm.args=", 13) == 0) { _jvm_args = p + 13; } else if (strncmp(p, "sun.jvm.flags=", 14) == 0) { _jvm_flags = p + 14; } else if (strncmp(p, "sun.java.command=", 17) == 0) { _java_command = p + 17; } if ((p = strstr(p, ", ")) == NULL) { break; } *p = 0; p += 2; } return true; } static const char* getFeaturesString(char* str, size_t size, StackWalkFeatures f) { int chars = snprintf(str, size, "%s%s%s%s%s%s%s", f.stats ? ",stats" : "", f.jnienv ? ",jnienv" : "", f.agct ? ",agct" : "", f.mixed ? ",mixed" : "", f.vtable_target ? ",vtable" : "", f.comp_task ? ",comptask" : "", f.pc_addr ? ",pcaddr" : ""); return chars > 0 ? str + 1 : ""; } bool pwriteAll(const char* data, size_t size, off_t offset) { while (size > 0) { ssize_t result = pwrite(_fd, data, size, offset); if (result < 0 && errno == EINTR) continue; if (result <= 0) return false; data += result; size -= (size_t)result; offset += result; } return true; } void flush(Buffer* buf) { int fd = _in_memory ? _memfd : _fd; const char* data = buf->data(); size_t remaining = buf->offset(); size_t written = 0; while (remaining > 0) { ssize_t result = write(fd, data + written, remaining); if (result < 0 && errno == EINTR) continue; if (result <= 0) { setIoError(); break; } written += (size_t)result; remaining -= (size_t)result; } if (written > 0) { atomicInc(_bytes_written, (u64)written); } buf->reset(); } void flushIfNeeded(Buffer* buf, int limit = RECORDING_BUFFER_LIMIT) { if (buf->offset() >= limit) { flush(buf); } } void writeHeader(Buffer* buf) { buf->put("FLR\0", 4); // magic buf->put16(2); // major buf->put16(0); // minor buf->put64(1024 * 1024 * 1024); // chunk size (initially large, for JMC to skip incomplete chunk) buf->put64(0); // cpool offset buf->put64(0); // meta offset buf->put64(_start_time * 1000); // start time, ns buf->put64(0); // duration, ns buf->put64(_start_ticks); // start ticks // A frequency here may be inaccurate when using TSC clock. // It will be overwritten with a correct value in finishChunk later. buf->put64(TSC::frequency()); // ticks per sec buf->put32(1); // features } void writeMetadata(Buffer* buf) { int metadata_start = buf->skip(5); // size will be patched later buf->putVar32(T_METADATA); buf->putVar64(_start_ticks); buf->putVar32(0); buf->putVar32(0x7fffffff); // must not clash with JFR metadata ID, or 'jfr print' will break const Index& strings = JfrMetadata::strings(); buf->putVar32(strings.size()); strings.forEachOrdered([&] (size_t idx, const std::string& s) { buf->putUtf8(s.c_str()); }); writeElement(buf, JfrMetadata::root()); buf->putVar32(metadata_start, buf->offset() - metadata_start); } void writeElement(Buffer* buf, const Element* e) { buf->putVar32(e->_name); buf->putVar32(e->_attributes.size()); for (int i = 0; i < e->_attributes.size(); i++) { buf->putVar32(e->_attributes[i]._key); buf->putVar32(e->_attributes[i]._value); } buf->putVar32(e->_children.size()); for (int i = 0; i < e->_children.size(); i++) { writeElement(buf, e->_children[i]); } } void writeRecordingInfo(Buffer* buf) { int start = buf->skip(1); buf->put8(T_ACTIVE_RECORDING); buf->putVar64(_start_ticks); buf->putVar32(1); buf->putUtf8("async-profiler " PROFILER_VERSION); buf->putUtf8("async-profiler.jfr"); buf->putVar64(MAX_JLONG); buf->putVar32(0); buf->putVar64(_start_time / 1000); buf->putVar64(MAX_JLONG); buf->put8(start, buf->offset() - start); } void writeSignalCaptureContext(Buffer* buf) { if (!_signal_capture) return; int start = buf->skip(5); buf->put8(T_SIGNAL_CAPTURE); buf->putVar64(_start_ticks); buf->putVar32(1); buf->putUtf8(_signal_session_id); buf->putVar64(_signal_capture_epoch); buf->putVar32(_signal_number); buf->putUtf8(_signal_delivery); buf->putVar64(OS::processId()); // Informational AP process timestamp. It is not a kernel task-generation proof. buf->putVar64(OS::processStartTime()); buf->putVar32(start, buf->offset() - start); } void writeSettings(Buffer* buf, Arguments& args) { assert(args._cstack < sizeof(SETTING_CSTACK) / sizeof(char*)); writeStringSetting(buf, T_ACTIVE_RECORDING, "version", PROFILER_VERSION); writeStringSetting(buf, T_ACTIVE_RECORDING, "engine", Profiler::instance()->_engine->type()); writeStringSetting(buf, T_ACTIVE_RECORDING, "cstack", SETTING_CSTACK[args._cstack]); writeStringSetting(buf, T_ACTIVE_RECORDING, "clock", TSC::enabled() ? "tsc" : "monotonic"); writeStringSetting(buf, T_ACTIVE_RECORDING, "event", args._event); writeStringSetting(buf, T_ACTIVE_RECORDING, "filter", args._filter); writeStringSetting(buf, T_ACTIVE_RECORDING, "begin", args._begin); writeStringSetting(buf, T_ACTIVE_RECORDING, "end", args._end); writeListSetting(buf, T_ACTIVE_RECORDING, "include", args._include); writeListSetting(buf, T_ACTIVE_RECORDING, "exclude", args._exclude); writeIntSetting(buf, T_ACTIVE_RECORDING, "jstackdepth", args._jstackdepth); writeIntSetting(buf, T_ACTIVE_RECORDING, "jfropts", args._jfr_options); writeIntSetting(buf, T_ACTIVE_RECORDING, "chunksize", args._chunk_size); writeIntSetting(buf, T_ACTIVE_RECORDING, "chunktime", args._chunk_time); writeIntSetting(buf, T_ACTIVE_RECORDING, "memlimit", args._mem_limit); for (int i = 0; i < EC_CATEGORIES; i++) { if (args._rate_limit[i] >= 0) { writeIntSetting(buf, JFR_TYPE_FOR_CATEGORY[i], "ratelimit", args._rate_limit[i]); } } char str[256]; writeStringSetting(buf, T_ACTIVE_RECORDING, "features", getFeaturesString(str, sizeof(str), args._features)); bool execution_samples = (args.eventMask() & (1 << EC_CPU)) != 0; writeBoolSetting(buf, T_EXECUTION_SAMPLE, "enabled", execution_samples); writeBoolSetting(buf, T_SIGNAL_SAMPLE, "enabled", args._signal_cookie); if (execution_samples) { writeIntSetting(buf, T_EXECUTION_SAMPLE, "interval", args._interval); writeBoolSetting(buf, T_EXECUTION_SAMPLE, "alluser", args._alluser); } if (args._wall >= 0) { writeIntSetting(buf, T_EXECUTION_SAMPLE, "wall", args._wall); writeBoolSetting(buf, T_EXECUTION_SAMPLE, "nobatch", args._nobatch); } if (args._nativemem >= 0) { writeIntSetting(buf, T_MALLOC, "nativemem", args._nativemem); } if (args._nativelock >= 0) { writeIntSetting(buf, T_NATIVE_LOCK, "nativelock", args._nativelock); } writeBoolSetting(buf, T_ALLOC_IN_NEW_TLAB, "enabled", args._alloc >= 0); writeBoolSetting(buf, T_ALLOC_OUTSIDE_TLAB, "enabled", args._alloc >= 0); if (args._alloc >= 0) { writeIntSetting(buf, T_ALLOC_IN_NEW_TLAB, "alloc", args._alloc); writeBoolSetting(buf, T_ALLOC_IN_NEW_TLAB, "live", args._live); } writeBoolSetting(buf, T_MONITOR_ENTER, "enabled", args._lock >= 0); writeBoolSetting(buf, T_THREAD_PARK, "enabled", args._lock >= 0); if (args._lock >= 0) { writeIntSetting(buf, T_MONITOR_ENTER, "lock", args._lock); } writeBoolSetting(buf, T_METHOD_TRACE, "enabled", !args._trace.empty()); writeListSetting(buf, T_METHOD_TRACE, "trace", args._trace); writeBoolSetting(buf, T_PROCESS_SAMPLE, "enabled", args._proc > 0); if (args._proc > 0) { writeIntSetting(buf, T_PROCESS_SAMPLE, "proc", args._proc); } writeBoolSetting(buf, T_ACTIVE_RECORDING, "debugSymbols", VM::loaded() && VMStructs::libjvm()->hasDebugSymbols()); writeBoolSetting(buf, T_ACTIVE_RECORDING, "kernelSymbols", Symbols::haveKernelSymbols()); } void writeStringSetting(Buffer* buf, int category, const char* key, const char* value) { flushIfNeeded(buf, RECORDING_BUFFER_LIMIT - MAX_STRING_LENGTH); int start = buf->skip(5); buf->put8(T_ACTIVE_SETTING); buf->putVar64(_start_ticks); buf->putVar32(category); buf->putUtf8(key); buf->putUtf8(value); buf->putVar32(start, buf->offset() - start); } void writeBoolSetting(Buffer* buf, int category, const char* key, bool value) { writeStringSetting(buf, category, key, value ? "true" : "false"); } void writeIntSetting(Buffer* buf, int category, const char* key, long long value) { char str[32]; snprintf(str, sizeof(str), "%lld", value); writeStringSetting(buf, category, key, str); } void writeListSetting(Buffer* buf, int category, const char* key, const std::vector<const char*>& list) { for (const char* s : list) { writeStringSetting(buf, category, key, s); } } void writeOsCpuInfo(Buffer* buf) { struct utsname u; if (uname(&u) != 0) { return; } char str[512]; snprintf(str, sizeof(str) - 1, "uname: %s %s %s %s", u.sysname, u.release, u.version, u.machine); str[sizeof(str) - 1] = 0; int start = buf->skip(5); buf->put8(T_OS_INFORMATION); buf->putVar64(_start_ticks); buf->putUtf8(str); buf->putVar32(start, buf->offset() - start); start = buf->skip(5); buf->put8(T_CPU_INFORMATION); buf->putVar64(_start_ticks); buf->putUtf8(u.machine); buf->putUtf8(OS::getCpuDescription(str, sizeof(str) - 1) ? str : ""); buf->putVar32(1); buf->putVar32(_available_processors); buf->putVar32(_available_processors); buf->putVar32(start, buf->offset() - start); } void writeJvmInfo(Buffer* buf) { if (_agent_properties == NULL && !(VM::loaded() && parseAgentProperties())) { return; } char* jvm_name = NULL; char* jvm_version = NULL; jvmtiEnv* jvmti = VM::jvmti(); jvmti->GetSystemProperty("java.vm.name", &jvm_name); jvmti->GetSystemProperty("java.vm.version", &jvm_version); flushIfNeeded(buf, RECORDING_BUFFER_LIMIT - 5 * MAX_STRING_LENGTH); int start = buf->skip(5); buf->put8(T_JVM_INFORMATION); buf->putVar64(_start_ticks); buf->putUtf8(jvm_name); buf->putUtf8(jvm_version); buf->putUtf8(_jvm_args); buf->putUtf8(_jvm_flags); buf->putUtf8(_java_command); buf->putVar64(OS::processStartTime()); buf->putVar32(OS::processId()); buf->putVar32(start, buf->offset() - start); jvmti->Deallocate((unsigned char*)jvm_version); jvmti->Deallocate((unsigned char*)jvm_name); } void writeSystemProperties(Buffer* buf) { jvmtiEnv* jvmti = VM::jvmti(); jint count; char** keys; if (!VM::loaded() || jvmti->GetSystemProperties(&count, &keys) != 0) { return; } for (int i = 0; i < count; i++) { char* key = keys[i]; char* value = NULL; if (jvmti->GetSystemProperty(key, &value) == 0) { flushIfNeeded(buf, RECORDING_BUFFER_LIMIT - 2 * MAX_STRING_LENGTH); int start = buf->skip(5); buf->put8(T_INITIAL_SYSTEM_PROPERTY); buf->putVar64(_start_ticks); buf->putUtf8(key); buf->putUtf8(value); buf->putVar32(start, buf->offset() - start); jvmti->Deallocate((unsigned char*)value); } jvmti->Deallocate((unsigned char*)key); } jvmti->Deallocate((unsigned char*)keys); } void writeNativeLibraries(Buffer* buf) { if (_recorded_lib_count < 0) return; Profiler* profiler = Profiler::instance(); CodeCacheArray& native_libs = profiler->_native_libs; int native_lib_count = native_libs.count(); for (int i = _recorded_lib_count; i < native_lib_count; i++) { flushIfNeeded(buf, RECORDING_BUFFER_LIMIT - MAX_STRING_LENGTH); int start = buf->skip(5); buf->put8(T_NATIVE_LIBRARY); buf->putVar64(_start_ticks); buf->putUtf8(native_libs[i]->name()); buf->putVar64((uintptr_t) native_libs[i]->minAddress()); buf->putVar64((uintptr_t) native_libs[i]->maxAddress()); buf->putVar32(start, buf->offset() - start); } _recorded_lib_count = native_lib_count; } void writeCpool(Buffer* buf) { buf->skip(5); // size will be patched later buf->putVar32(T_CPOOL); buf->putVar64(_start_ticks); buf->putVar32(0); buf->putVar32(0); buf->putVar32(1); buf->putVar32(12); Index packages(1); Index symbols(1); Lookup lookup(&_method_map, Profiler::instance()->classMap(), &packages, &symbols, OUTPUT_JFR); writeFrameTypes(buf); writeThreadStates(buf); writeGCWhen(buf); writeThreads(buf); writeStackTraces(buf, &lookup); writeMethods(buf, &lookup); writeClasses(buf, &lookup); writePackages(buf, &lookup); writeSymbols(buf, &lookup); writeStrings(buf); writeUserEventTypes(buf); // Write log levels last. The order does not affect the JFR's validity, // but log levels have an easily-visible format that makes it easy // to see if a JFR file has been accidentally truncated. writeLogLevels(buf); } void writePoolHeader(Buffer* buf, JfrType type, u32 size) { if (size > 0) { buf->putVar32(type); buf->putVar32(size); } else { // JDK's built-in JFR reader does not support empty pools. // Write a dummy String pool of 1 element instead. buf->putVar32(T_STRING); buf->putVar32(1); buf->putVar32(0); // key buf->put8(0); // null string } } void writeStringMap(Buffer* buf, JfrType type, std::map<u32, const char*>& strings) { writePoolHeader(buf, type, strings.size()); for (std::map<u32, const char*>::const_iterator it = strings.begin(); it != strings.end(); ++it) { flushIfNeeded(buf, RECORDING_BUFFER_LIMIT - MAX_STRING_LENGTH); buf->putVar32(it->first); buf->putUtf8(it->second); } } void writeFrameTypes(Buffer* buf) { buf->putVar32(T_FRAME_TYPE); buf->putVar32(7); buf->putVar32(FRAME_INTERPRETED); buf->putUtf8("Interpreted"); buf->putVar32(FRAME_JIT_COMPILED); buf->putUtf8("JIT compiled"); buf->putVar32(FRAME_INLINED); buf->putUtf8("Inlined"); buf->putVar32(FRAME_NATIVE); buf->putUtf8("Native"); buf->putVar32(FRAME_CPP); buf->putUtf8("C++"); buf->putVar32(FRAME_KERNEL); buf->putUtf8("Kernel"); buf->putVar32(FRAME_C1_COMPILED); buf->putUtf8("C1 compiled"); } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |