mirror of
https://github.com/async-profiler/async-profiler.git
synced 2026-04-28 10:53:49 +00:00
Compare commits
113 Commits
v4.2.1
...
7bd911a007
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bd911a007 | ||
|
|
2df2733d1d | ||
|
|
4d5441f2cd | ||
|
|
cc9e91bd8f | ||
|
|
e899de6a9c | ||
|
|
fbc3942095 | ||
|
|
6afb9572c1 | ||
|
|
f763e195ee | ||
|
|
f1b87ead07 | ||
|
|
4dda6c40af | ||
|
|
264b8ab5da | ||
|
|
c383a35ff4 | ||
|
|
82ae80a660 | ||
|
|
7e92b5cdac | ||
|
|
fe69e4fab2 | ||
|
|
d94581c24c | ||
|
|
f3c31942fb | ||
|
|
a246ced814 | ||
|
|
8d653dd5e0 | ||
|
|
cc0eab1789 | ||
|
|
842b612e08 | ||
|
|
ff4336d136 | ||
|
|
e1dd4c05f6 | ||
|
|
174dc31d88 | ||
|
|
dbd9fc7520 | ||
|
|
dc69cf4b80 | ||
|
|
abc8b7f493 | ||
|
|
4ea8e5bbb6 | ||
|
|
71ad47a46e | ||
|
|
0023021ddf | ||
|
|
444d0e6353 | ||
|
|
7e2ed0e77e | ||
|
|
68244fbf6f | ||
|
|
31042f13bc | ||
|
|
a3c6d92d39 | ||
|
|
036c87e50d | ||
|
|
15b1161f57 | ||
|
|
b02434bd9d | ||
|
|
9c293283f2 | ||
|
|
3aba5ee521 | ||
|
|
078935591f | ||
|
|
dc88d3f756 | ||
|
|
a071e8a2f8 | ||
|
|
6e6acc1769 | ||
|
|
709a777393 | ||
|
|
b9d6843ae5 | ||
|
|
3722d05ba0 | ||
|
|
872be63220 | ||
|
|
a89d7ddeba | ||
|
|
f789c4f748 | ||
|
|
d43d328b58 | ||
|
|
037c09906d | ||
|
|
f352361814 | ||
|
|
19b22efeff | ||
|
|
16fdebf78c | ||
|
|
0b73f655a8 | ||
|
|
cf4739a61b | ||
|
|
757bf8edd3 | ||
|
|
d0d16240d4 | ||
|
|
fc9b5c85cf | ||
|
|
436d5b5066 | ||
|
|
4663784b98 | ||
|
|
d2172a6382 | ||
|
|
93b6ae376d | ||
|
|
ee4ac6e888 | ||
|
|
865e8b91f8 | ||
|
|
2a4f329cba | ||
|
|
9c425ca74f | ||
|
|
d871819848 | ||
|
|
bf84fadb3c | ||
|
|
fde780e275 | ||
|
|
6e04336375 | ||
|
|
a77d091e08 | ||
|
|
999f0c7ae3 | ||
|
|
cdaf6e76ba | ||
|
|
3a493bedc4 | ||
|
|
7b24ad89b6 | ||
|
|
5bf0e311c2 | ||
|
|
8772214f7e | ||
|
|
3bb1e72d09 | ||
|
|
ea0b34b578 | ||
|
|
e92eb45812 | ||
|
|
d304fd5d75 | ||
|
|
61a676f87f | ||
|
|
b855e0c2c4 | ||
|
|
763616aa17 | ||
|
|
a25e5194bf | ||
|
|
ff24f1220c | ||
|
|
124eca439e | ||
|
|
4bcfe9ee7b | ||
|
|
0b7ee6d830 | ||
|
|
01325ea87c | ||
|
|
78172b7cb0 | ||
|
|
fd269e6450 | ||
|
|
61d48a6b43 | ||
|
|
c6c2fc1497 | ||
|
|
585054661f | ||
|
|
0cb40bee11 | ||
|
|
8c851ddad2 | ||
|
|
8fa4fd0b78 | ||
|
|
9611d55567 | ||
|
|
eb4d126a2d | ||
|
|
85ae06b177 | ||
|
|
872631f82b | ||
|
|
7482988021 | ||
|
|
e647076de5 | ||
|
|
c478490ce9 | ||
|
|
6e10742be1 | ||
|
|
49e56704f9 | ||
|
|
79d9058b18 | ||
|
|
9674d20873 | ||
|
|
538f3a2e48 | ||
|
|
e35113a647 |
@@ -3,24 +3,82 @@
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<style>
|
||||
body {margin: 0; padding: 10px 10px 22px 10px; background-color: #ffffff}
|
||||
:root {--bg: #ffffff; --fg: #000000; --hl-bg: #ffffe0; --hl-border: #ffc000; --link: #0366d6; --legend-bg: #ffffe0; --legend-border: #666666}
|
||||
:root.dark {--bg: #1e1e1e; --fg: #cccccc; --hl-bg: #3a3a00; --hl-border: #8a7000; --link: #58a6ff; --legend-bg: #333333; --legend-border: #888888}
|
||||
body {margin: 0; padding: 10px 10px 22px 10px; background-color: var(--bg); color: var(--fg)}
|
||||
h1 {margin: 5px 0 0 0; font-size: 18px; font-weight: normal; text-align: center}
|
||||
header {margin: -24px 0 5px 0; line-height: 24px}
|
||||
button {font: 12px sans-serif; cursor: pointer}
|
||||
p {position: fixed; bottom: 0; margin: 0; padding: 2px 3px 2px 3px; outline: 1px solid #ffc000; display: none; overflow: hidden; white-space: nowrap; background-color: #ffffe0}
|
||||
a {color: #0366d6}
|
||||
#hl {position: absolute; display: none; overflow: hidden; white-space: nowrap; pointer-events: none; background-color: #ffffe0; outline: 1px solid #ffc000; height: 15px}
|
||||
header {margin: -22px 0 6px 0}
|
||||
button {border: none; background: none; width: 24px; height: 24px; cursor: pointer; margin: 0; padding: 2px 0 0 0; text-align: center}
|
||||
button:hover {background-color: var(--hl-bg); outline: 1px solid var(--hl-border); border-radius: 4px}
|
||||
dl {margin: 0 4px 8px 4px}
|
||||
dt {margin: 1px; padding: 2px 0; font-weight: bold}
|
||||
dd {margin: 1px; padding: 2px 4px}
|
||||
dl.frames {float: left; width: 160px}
|
||||
dl.frames > dd {color: #000000}
|
||||
dl.hotkeys {clear: left; border-top: 1px solid var(--legend-border)}
|
||||
dl.hotkeys > dt {float: left; clear: left; width: 158px; margin-right: 4px; text-align: right}
|
||||
dl.hotkeys > dd {float: left}
|
||||
p {position: fixed; bottom: 0; margin: 0; padding: 2px 3px 2px 3px; outline: 1px solid var(--hl-border); display: none; overflow: hidden; white-space: nowrap; background-color: var(--hl-bg); color: var(--fg)}
|
||||
a {color: var(--link)}
|
||||
#legend {padding: 4px; border-radius: 4px; background: var(--legend-bg); border: 1px solid var(--legend-border); display: none}
|
||||
#hl {position: absolute; display: none; overflow: hidden; white-space: nowrap; pointer-events: none; background-color: var(--hl-bg); outline: 1px solid var(--hl-border); height: 15px}
|
||||
#hl span {padding: 0 3px 0 3px}
|
||||
#status {left: 0}
|
||||
#match {right: 0}
|
||||
#reset {cursor: pointer}
|
||||
#canvas {width: 100%; height: 576px}
|
||||
</style>
|
||||
<script>
|
||||
{
|
||||
let theme;
|
||||
try { theme = localStorage.getItem('flame-theme'); } catch (ignored) {}
|
||||
if (theme ? theme === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
document.documentElement.classList.add('dark');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body style='font: 12px Verdana, sans-serif'>
|
||||
<h1>CPU profile</h1>
|
||||
<header style='text-align: left'><button id='inverted' title='Invert'>🔻</button> <button id='search' title='Search'>🔍</button></header>
|
||||
<header style='text-align: right'>Produced by <a href='https://github.com/async-profiler/async-profiler'>async-profiler</a></header>
|
||||
<header style='float: left'>
|
||||
<button id='inverted' title='Invert (I)'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 392 392'><path d='M196,36 L316,156 L76,156 Z' fill='#004d80'/><path d='M196,356 L76,236 L316,236 Z' fill='#004d80'/><path d='M196,54 L298,156 L94,156 Z' fill='#ff8d40'/><path d='M196,338 L94,236 L298,236 Z' fill='#40b2ff'/><rect x='94' y='188' width='204' height='16' fill='#004d80'/></svg></button>
|
||||
<button id='search' title='Search (Ctrl+F)'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='-39.3 -39.3 471.1 471.1'><circle cx='147.7' cy='147.8' r='125.9' fill='#fff'/><path fill='#40b2ff' d='M370.7 348.7c0 1.4-1.6 6.3-7.2 12.3-6.2 6.7-12.5 9.8-14.7 9.8h-.1c-19.5-1.6-62-43.2-109.6-106.8 9.2-7.2 17.5-15.5 24.6-24.6 63.6 47.6 105.2 90.2 106.8 109.6z'/><path fill='#ff8d40' d='M208.7 86.9l-14.5 14.5c-17.1 17.1-46.5 5-46.5-19.3V61.6c-49 0-88.4 40.8-86.1 90.2 2 43.9 38.1 80 82 82 49.5 2.3 90.2-37.2 90.2-86.1 0-23.7-9.6-45.2-25.1-60.8z'/><path fill='#004d80' d='M276.1 221c12.3-21.5 19.5-46.5 19.5-73.2C295.6 66.3 229.2.1 147.7.1S0 66.3 0 147.9s66.3 147.7 147.7 147.7c26.6 0 51.5-7.1 73.2-19.5 39.8 53.3 91.9 113.5 126.1 116.4 12.3.5 22.9-6.7 32.8-16.7 5.2-5.6 13.8-16.9 12.8-28.8-2.9-34.1-63.1-86.2-116.4-126.1zM147.7 273.8c-69.5 0-125.9-56.5-125.9-125.9S78.3 21.9 147.7 21.9 273.6 78.4 273.6 147.8s-56.4 126-125.9 126zm215.9 87.2c-6.2 6.7-12.4 9.8-14.7 9.8h-.1c-19.5-1.6-62-43.2-109.6-106.8 9.2-7.2 17.5-15.5 24.6-24.6 63.6 47.6 105.2 90.2 106.8 109.6 0 1.4-1.6 6.3-7.2 12.4z'/></svg></button>
|
||||
<button id='darkmode' title='Toggle dark mode (D)'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><path d='M10 4a6 6 0 0 1 0 12z' fill='#ff8d40'/><path d='M10 4a6 6 0 0 0 0 12z' fill='#ffffff'/><circle cx='10' cy='10' r='8' fill='none' stroke='#004d80'/></svg></button>
|
||||
<button id='info'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><circle cx='10' cy='10' r='8' stroke='#004d80' fill='none'/><path d='M10 5.5c-1.25 0-2.25 1-2.25 2.25H9a1.25 1.25 0 0 1 2.5 0c0 .65-.55 1-1 1.2-.7.35-1.25.85-1.25 1.8V11h1.5v-.25c0-.37.29-.65.68-.83.73-.34 1.32-.87 1.32-2.17 0-1.25-1.5-2.25-2.75-2.25' fill='#ff8d40' stroke='#ff8d40' stroke-width='.6' stroke-linecap='round' stroke-linejoin='round'/><circle cx='10' cy='13.5' r='1.2' fill='#ff8d40'/></svg></button>
|
||||
</header>
|
||||
<header style='float: right'>Produced by <a href='https://github.com/async-profiler/async-profiler'>async-profiler</a></header>
|
||||
<div id='legend' style='position: absolute'>
|
||||
<dl class='frames'>
|
||||
<dt>Frame types</dt>
|
||||
<dd style='background-color: #e17d00'>Kernel</dd>
|
||||
<dd style='background-color: #e15a5a'>Native</dd>
|
||||
<dd style='background-color: #c8c83c'>C++ (VM)</dd>
|
||||
<dd style='background-color: #50e150'>Java compiled</dd>
|
||||
<dd style='background-color: #cce880'>Java compiled by C1</dd>
|
||||
<dd style='background-color: #50cccc'>Inlined</dd>
|
||||
<dd style='background-color: #b2e1b2'>Interpreted</dd>
|
||||
</dl>
|
||||
<dl class='frames'>
|
||||
<dt>Allocation profile</dt>
|
||||
<dd style='background-color: #50cccc'>Allocated class</dd>
|
||||
<dd style='background-color: #e17d00'>Allocation outside TLAB</dd>
|
||||
<dt>Lock profile</dt>
|
||||
<dd style='background-color: #50cccc'>Lock class</dd>
|
||||
<dt> </dt>
|
||||
<dt>Search</dt>
|
||||
<dd style='background-color: #ee00ee'>Matches regexp</dd>
|
||||
</dl>
|
||||
<dl class='hotkeys'>
|
||||
<dt>Click frame</dt><dd>Zoom into frame</dd>
|
||||
<dt>Ctrl/Alt+Click</dt><dd>Remove stack</dd>
|
||||
<dt>0</dt><dd>Reset zoom</dd>
|
||||
<dt>I</dt><dd>Invert graph</dd>
|
||||
<dt>Ctrl+F</dt><dd>Search</dd>
|
||||
<dt>N</dt><dd>Next match</dd>
|
||||
<dt>Shift+N</dt><dd>Previous match</dd>
|
||||
<dt>Esc</dt><dd>Cancel search</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<canvas id='canvas'></canvas>
|
||||
<div id='hl'><span></span></div>
|
||||
<p id='status'></p>
|
||||
@@ -30,9 +88,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
'use strict';
|
||||
let root, px, pattern;
|
||||
let level0 = 0, left0 = 0, width0 = 0;
|
||||
let level0 = 0, left0 = 0, width0 = 0, d = 0;
|
||||
let nav = [], navIndex, matchval;
|
||||
let inverted = false;
|
||||
const U = undefined;
|
||||
const maxdiff = -1;
|
||||
const levels = Array(36);
|
||||
for (let h = 0; h < levels.length; h++) {
|
||||
levels[h] = [];
|
||||
@@ -66,10 +126,18 @@
|
||||
return '#' + (p[0] + ((p[1] * v) << 16 | (p[2] * v) << 8 | (p[3] * v))).toString(16);
|
||||
}
|
||||
|
||||
function getDiffColor(diff) {
|
||||
if (diff === U) return '#ffdd33';
|
||||
if (diff === 0) return '#e0e0e0';
|
||||
const v = Math.round(128 * (maxdiff - Math.abs(diff)) / maxdiff) + 96;
|
||||
return diff > 0 ? 'rgb(255,' + v + ',' + v + ')' : 'rgb(' + v + ',' + v + ',255)';
|
||||
}
|
||||
|
||||
function f(key, level, left, width, inln, c1, int) {
|
||||
levels[level0 = level].push({level, left: left0 += left, width: width0 = width || width0,
|
||||
color: getColor(palette[key & 7]), title: cpool[key >>> 3],
|
||||
details: (int ? ', int=' + int : '') + (c1 ? ', c1=' + c1 : '') + (inln ? ', inln=' + inln : '')
|
||||
color: maxdiff >= 0 ? getDiffColor(d) : getColor(palette[key & 7]),
|
||||
title: cpool[key >>> 3],
|
||||
details: (d ? (d > 0 ? ', +' : ', ') + d : '') + (int ? ', int=' + int : '') + (c1 ? ', c1=' + c1 : '') + (inln ? ', inln=' + inln : '')
|
||||
});
|
||||
}
|
||||
|
||||
@@ -141,8 +209,10 @@
|
||||
}
|
||||
|
||||
function render(newRoot, nav) {
|
||||
const bg = getComputedStyle(document.documentElement).getPropertyValue('--bg');
|
||||
|
||||
if (root) {
|
||||
c.fillStyle = '#ffffff';
|
||||
c.fillStyle = bg;
|
||||
c.fillRect(0, 0, canvasWidth, canvasHeight);
|
||||
}
|
||||
|
||||
@@ -184,7 +254,7 @@
|
||||
}
|
||||
|
||||
if (f.level < root.level) {
|
||||
c.fillStyle = 'rgba(255, 255, 255, 0.5)';
|
||||
c.fillStyle = bg + '80';
|
||||
c.fillRect((f.left - x0) * px, y, f.width * px, 15);
|
||||
}
|
||||
}
|
||||
@@ -221,7 +291,7 @@
|
||||
canvas.title = f.title + '\n(' + samples(f.width) + f.details + ', ' + pct(f.width, levels[0][0].width) + '%)';
|
||||
canvas.style.cursor = 'pointer';
|
||||
canvas.onclick = function() {
|
||||
if (event.altKey && h >= root.level) {
|
||||
if ((event.altKey || event.ctrlKey) && h >= root.level && h > 0) {
|
||||
removeStack(f.left, f.width);
|
||||
root.width > f.width ? render(root) : render();
|
||||
} else if (f !== root) {
|
||||
@@ -262,10 +332,30 @@
|
||||
search(false);
|
||||
}
|
||||
|
||||
document.getElementById('darkmode').onclick = function() {
|
||||
const theme = document.documentElement.classList.toggle('dark') ? 'dark' : 'light';
|
||||
try { localStorage.setItem('flame-theme', theme); } catch (ignored) {}
|
||||
render(root);
|
||||
}
|
||||
|
||||
const btnInfo = document.getElementById('info');
|
||||
const legend = document.getElementById('legend');
|
||||
|
||||
btnInfo.onmouseover = function() {
|
||||
legend.style.left = (btnInfo.offsetLeft + 24) + 'px';
|
||||
legend.style.top = (btnInfo.offsetTop + 24) + 'px';
|
||||
legend.style.display = 'block';
|
||||
}
|
||||
|
||||
btnInfo.onmouseout = function() {
|
||||
legend.style.display = 'none';
|
||||
}
|
||||
|
||||
window.onkeydown = function(event) {
|
||||
if ((event.ctrlKey || event.metaKey) && event.key === 'f') {
|
||||
event.preventDefault();
|
||||
search(true);
|
||||
return false;
|
||||
} else if (event.key === 'Escape') {
|
||||
search(false);
|
||||
} else if ((event.key === 'n' || event.key === 'N') && nav.length > 0) {
|
||||
@@ -274,6 +364,19 @@
|
||||
document.getElementById('matchval').textContent = matchval + ' (' + (navIndex + 1) + ' of ' + nav.length + ')';
|
||||
window.scroll(0, inverted ? root.level * 16 : canvasHeight - (root.level + 1) * 16);
|
||||
canvas.onmousemove();
|
||||
return false;
|
||||
} else if (event.key === 'i') {
|
||||
canvas.onmouseout();
|
||||
document.getElementById('inverted').onclick();
|
||||
return false;
|
||||
} else if (event.key === 'd') {
|
||||
document.getElementById('darkmode').onclick();
|
||||
return false;
|
||||
} else if (event.key === '0') {
|
||||
canvas.onmouseout();
|
||||
root = levels[0][0];
|
||||
search(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 76 KiB |
BIN
.assets/images/flamegraph_diff.png
Normal file
BIN
.assets/images/flamegraph_diff.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
1
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
1
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
@@ -2,7 +2,6 @@
|
||||
name: "🐛 Bug Report"
|
||||
description: Report a bug
|
||||
title: "(short issue description)"
|
||||
labels: [bug]
|
||||
assignees: []
|
||||
body:
|
||||
- type: textarea
|
||||
|
||||
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@@ -17,6 +17,9 @@ env:
|
||||
build_java_distribution: corretto
|
||||
build_java_version: 11
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ${{ inputs.runner }}
|
||||
|
||||
3
.github/workflows/code-check.yml
vendored
3
.github/workflows/code-check.yml
vendored
@@ -7,6 +7,9 @@ concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
cpp-lint:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
5
.github/workflows/integ.yml
vendored
5
.github/workflows/integ.yml
vendored
@@ -37,6 +37,9 @@ on:
|
||||
required: false
|
||||
default: 0
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
integration-test:
|
||||
runs-on: ${{ inputs.runner }}
|
||||
@@ -105,7 +108,7 @@ jobs:
|
||||
make build/test.jar
|
||||
cp -r ${{ steps.extract_artifact.outputs.release_directory }}/bin build
|
||||
cp -r ${{ steps.extract_artifact.outputs.release_directory }}/lib build
|
||||
make test-java RETRY_COUNT=${{ inputs.retry-count }} -j
|
||||
make test-java TEST_THREADS=2 RETRY_COUNT=${{ inputs.retry-count }} -j
|
||||
- name: Upload integration test logs
|
||||
uses: actions/upload-artifact@v4
|
||||
if: always()
|
||||
|
||||
34
.github/workflows/linters.yml
vendored
34
.github/workflows/linters.yml
vendored
@@ -8,6 +8,9 @@ concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
license-header:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -23,3 +26,34 @@ jobs:
|
||||
run: |
|
||||
npm install -g prettier@3.4.2
|
||||
make check-md
|
||||
eof-newline:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: EOF newline check
|
||||
env:
|
||||
offenders_path: /tmp/eof_newline_offenders.txt
|
||||
run: |
|
||||
find . -path './.git' -prune -o -exec file --mime-type {} + | grep 'text/' | awk -F: '{print $1}' | while read -r file; do
|
||||
# Read last byte and verify it's a newline
|
||||
if [ -s "$file" ] && [ "$(tail -c1 "$file" | wc -l)" -eq 0 ]; then
|
||||
echo "$file" >> "$offenders_path"
|
||||
fi
|
||||
done
|
||||
if [ -s "$offenders_path" ]; then
|
||||
cat "$offenders_path"
|
||||
exit 1
|
||||
fi
|
||||
trailing-spaces:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Trailing spaces check
|
||||
env:
|
||||
offenders_path: /tmp/trailing_space_offenders.txt
|
||||
run: |
|
||||
grep -rIlE --exclude-dir=.git '[[:blank:]]+$' . > "$offenders_path" || true
|
||||
if [ -s "$offenders_path" ]; then
|
||||
cat "$offenders_path"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
14
.github/workflows/test-and-publish-nightly.yml
vendored
14
.github/workflows/test-and-publish-nightly.yml
vendored
@@ -3,11 +3,15 @@ name: CI
|
||||
on: # We are very liberal in terms of triggering builds. This should be revisited if we start seeing a lot of queueing
|
||||
- push
|
||||
- pull_request
|
||||
- workflow_dispatch
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
build-jars:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -49,7 +53,7 @@ jobs:
|
||||
|
||||
integ-linux-x64:
|
||||
name: integ / linux-x64
|
||||
needs: build-linux-x64
|
||||
needs: [build-linux-x64, build-jars]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -92,7 +96,7 @@ jobs:
|
||||
|
||||
integ-linux-arm64:
|
||||
name: integ / linux-arm64
|
||||
needs: build-linux-arm64
|
||||
needs: [build-linux-arm64, build-jars]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -112,7 +116,7 @@ jobs:
|
||||
|
||||
integ-macos:
|
||||
name: integ / macos
|
||||
needs: build-macos
|
||||
needs: [build-macos, build-jars]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -139,9 +143,11 @@ jobs:
|
||||
|
||||
publish-only-on-push:
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
permissions:
|
||||
contents: write
|
||||
name: publish (nightly)
|
||||
runs-on: ubuntu-latest
|
||||
needs: [build-jars, integ-linux-x64, integ-linux-arm64, integ-macos]
|
||||
needs: [integ-linux-x64, integ-linux-arm64, integ-macos]
|
||||
steps:
|
||||
- name: Download async-profiler binaries and jars
|
||||
uses: actions/download-artifact@v4
|
||||
|
||||
@@ -14,6 +14,8 @@ header:
|
||||
paths-ignore:
|
||||
- 'src/jattach'
|
||||
- 'src/res'
|
||||
- '**/MANIFEST.MF'
|
||||
- 'test/**/*.collapsed'
|
||||
license:
|
||||
content: |
|
||||
Copyright The async-profiler authors
|
||||
|
||||
94
CHANGELOG.md
94
CHANGELOG.md
@@ -1,6 +1,88 @@
|
||||
# Changelog
|
||||
|
||||
## [4.2] - Early Access
|
||||
## [4.4]
|
||||
|
||||
### Features
|
||||
|
||||
- #1553: Differential Flame Graphs
|
||||
|
||||
### Improvements
|
||||
|
||||
- #1705: `memlimit` option to limit size of the call trace storage
|
||||
- #1706: Extend syntax of `-j` option to truncate deep stacks
|
||||
- #1720: FlameGraph: Dark mode toggle
|
||||
- #1672: FlameGraph: Use Ctrl+Click in addition to Alt+Click to remove stacks
|
||||
- #1684: Unwind ARM64 generated stubs on JDK 26+
|
||||
- #1676: Make `dwarf` stack walking mode an alias for `vm`
|
||||
- #1671: An option to select TLAB based AllocTracer engine with JDK 11+
|
||||
- #1670: Move converter Main class to the one.convert package
|
||||
- #1660: Provide non-aggregated samples in OTLP converter
|
||||
- #1701, #1682: Speed-up stack walking
|
||||
|
||||
### Breaking changes
|
||||
|
||||
- #1673: Permanently remove `check` command
|
||||
- #1675: Remove unsafe AsyncGetCallTrace recovery tricks along with `safemode` option
|
||||
- #1677: Remove `cstack=lbr` option
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- #1716: Wall-clock Heatmap does not count samples correctly
|
||||
- #1715: Fix Zing crash when profiling cpu+wall together
|
||||
- #1708: Another fix for correct vDSO unwinding on ARM64
|
||||
- #1707: Workaround for JFR shutdown race
|
||||
- #1699: Allow negative keys in JFR constant pool
|
||||
- #1697: Ensure remaining buffer is sufficient for event data in JfrReader
|
||||
- #1654: Prefer perf-events engine when record-cpu or target-cpu are selected
|
||||
- #1585: Scale perf counters in case of multiplexing
|
||||
- #1528: Add a hard-coded limit on the maximum number of jmethodIDs
|
||||
- Do not walk past virtual thread continuation barriers
|
||||
|
||||
## [4.3] - 2026-01-20
|
||||
|
||||
### Features
|
||||
|
||||
- #1547: Native lock profiling
|
||||
- #1566: Filter cpu/wall profiles by latency
|
||||
- #1568: Expose async-profiler metrics in Prometheus format
|
||||
- #1628: async-profiler.jar as Java agent; remote control via JMX
|
||||
|
||||
### Improvements
|
||||
|
||||
- #1140: FlameGraph improvements: legend, hot keys, new toolbar icons
|
||||
- #1530: Timezone switcher between Local and UTC time in Heatmaps
|
||||
- #1582: Support `--include`/`--exclude` options for JFR to Heatmap/OTLP/pprof conversion
|
||||
- #1624: Compatibility with OTLP v1.9.0
|
||||
- #1629: Harden crash protection in StackWalker
|
||||
|
||||
### Breaking changes
|
||||
|
||||
- #1277: New `timeSpan` field in WallClockSample events
|
||||
- #1518: Deprecate `check` command
|
||||
- #1590: Support compilation on modern JDKs. Drop JDK 7 support
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- #1599: Workaround for the kernel PERF_EVENT_IOC_REFRESH bug
|
||||
- #1596: Do not block any signals during execution of a custom crash handler
|
||||
- #1584: JfrReader loops on corrupted recordings
|
||||
- #1555: Parse FlameGraph title from HTML input
|
||||
- #1621: `loop` and `timeout` options do not work together
|
||||
- #1641: Unwind vDSO correctly on Linux-ARM64
|
||||
- #1648: Fix stop sequence in Profiler::start
|
||||
- #1575: Fix CodeCache memory leak in lock profiling while looping
|
||||
- #1558: Fix record-cpu bug when kernel stacks are not available
|
||||
- #1651: Do not record CPU frame for non-perf samples
|
||||
- #1614, #1615, #1617, #1623: Fix races related to VM termination
|
||||
|
||||
## [4.2.1] - 2025-11-22
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- #1599: Workaround for the kernel PERF_EVENT_IOC_REFRESH bug
|
||||
- #1596: Do not block any signals during execution of a custom crash handler
|
||||
|
||||
## [4.2] - 2025-10-20
|
||||
|
||||
### Features
|
||||
|
||||
@@ -31,7 +113,7 @@
|
||||
|
||||
- #1515: Fix UnsatisfiedLinkError when tmpdir is set to a relative path
|
||||
- #1500: Detect if `calloc` calls `malloc` for nativemem profiling
|
||||
- #1427: Re-implement SafeAccess crash protection
|
||||
- #1427: Re-implement SafeAccess crash protection
|
||||
- #1417: Two wall-clock profilers interfere with each other
|
||||
|
||||
### Project Infrastructure
|
||||
@@ -421,7 +503,7 @@
|
||||
### Features
|
||||
- Converters between different output formats:
|
||||
- JFR -> nflx (FlameScope)
|
||||
- Collapsed stacks -> HTML 5 Flame Graph
|
||||
- Collapsed stacks -> HTML 5 Flame Graph
|
||||
|
||||
### Improvements
|
||||
- `profiler.sh` no longer requires bash (contributed by @cfstras)
|
||||
@@ -505,7 +587,7 @@
|
||||
### Features
|
||||
- Interactive Call tree and Backtrace tree in HTML format (contributed by @rpulle)
|
||||
- Experimental support for Java Flight Recorder (JFR) compatible output
|
||||
|
||||
|
||||
### Improvements
|
||||
- Added units: `ms`, `us`, `s` and multipliers: `K`, `M`, `G` for interval argument
|
||||
- API and command-line option `-v` for profiler version
|
||||
@@ -519,7 +601,7 @@
|
||||
|
||||
### Features
|
||||
- Profiling of native functions, e.g. malloc
|
||||
|
||||
|
||||
### Improvements
|
||||
- JDK 9, 10, 11 support for heap profiling with accurate stack traces
|
||||
- `root` can now profile Java processes of any user
|
||||
@@ -531,7 +613,7 @@
|
||||
- Produce SVG files out of the box; flamegraph.pl is no longer needed
|
||||
- Profile ReentrantLock contention
|
||||
- Java API
|
||||
|
||||
|
||||
### Improvements
|
||||
- Allocation and Lock profiler now works on JDK 7, too
|
||||
- Faster dumping of results
|
||||
|
||||
44
Makefile
44
Makefile
@@ -1,4 +1,4 @@
|
||||
PROFILER_VERSION ?= 4.2
|
||||
PROFILER_VERSION ?= 4.4
|
||||
|
||||
ifeq ($(COMMIT_TAG),true)
|
||||
PROFILER_VERSION := $(PROFILER_VERSION)-$(shell git rev-parse --short=8 HEAD)
|
||||
@@ -52,6 +52,7 @@ JAR=$(JAVA_HOME)/bin/jar
|
||||
JAVA=$(JAVA_HOME)/bin/java
|
||||
JAVA_TARGET=8
|
||||
JAVAC_OPTIONS=--release $(JAVA_TARGET) -Xlint:-options
|
||||
TEST_JAVA ?= $(JAVA_HOME)/bin/java
|
||||
|
||||
TEST_LIB_DIR=build/test/lib
|
||||
TEST_BIN_DIR=build/test/bin
|
||||
@@ -61,7 +62,8 @@ LOG_DIR=build/test/logs
|
||||
LOG_LEVEL=
|
||||
SKIP=
|
||||
RETRY_COUNT=0
|
||||
TEST_FLAGS=-DlogDir=$(LOG_DIR) -DlogLevel=$(LOG_LEVEL) -Dskip='$(subst $(COMMA), ,$(SKIP))' -DretryCount=$(RETRY_COUNT)
|
||||
TEST_THREADS ?= 8
|
||||
TEST_FLAGS=-DlogDir=$(LOG_DIR) -DlogLevel=$(LOG_LEVEL) -Dskip='$(subst $(COMMA), ,$(SKIP))' -DretryCount=$(RETRY_COUNT) -DthreadCount=$(TEST_THREADS)
|
||||
|
||||
# always sort SOURCES so zInit is last.
|
||||
SOURCES := $(sort $(wildcard src/*.cpp))
|
||||
@@ -69,12 +71,15 @@ HEADERS := $(wildcard src/*.h)
|
||||
RESOURCES := $(wildcard src/res/*)
|
||||
JAVA_HELPER_CLASSES := $(wildcard src/helper/one/profiler/*.class)
|
||||
API_SOURCES := $(wildcard src/api/one/profiler/*.java)
|
||||
JAR_MANIFEST := src/api/one/profiler/MANIFEST.MF
|
||||
CONVERTER_SOURCES := $(shell find src/converter -name '*.java')
|
||||
TEST_SOURCES := $(shell find test -name '*.java' ! -path 'test/stubs/*')
|
||||
TESTS ?=
|
||||
CPP_TEST_SOURCES := test/native/testRunner.cpp $(shell find test/native -name '*Test.cpp')
|
||||
CPP_TEST_HEADER := test/native/testRunner.hpp
|
||||
CPP_TEST_INCLUDES := -Isrc -Itest/native
|
||||
TEST_LIB_SOURCES := $(wildcard test/native/libs/*)
|
||||
TEST_BIN_SOURCES := $(shell find test/test -name "*.c*")
|
||||
|
||||
ifeq ($(JAVA_HOME),)
|
||||
JAVA_HOME:=$(shell java -cp . JavaHome)
|
||||
@@ -95,7 +100,8 @@ ifeq ($(OS),Darwin)
|
||||
MERGE=false
|
||||
endif
|
||||
else
|
||||
CXXFLAGS += -U_FORTIFY_SOURCE -Wl,-z,defs -Wl,--exclude-libs,ALL -static-libstdc++ -static-libgcc -fdata-sections -ffunction-sections -Wl,--gc-sections -ggdb -Wunused-variable
|
||||
CXXFLAGS += -U_FORTIFY_SOURCE -Wl,-z,defs -Wl,--exclude-libs,ALL -static-libstdc++ -static-libgcc
|
||||
CXXFLAGS += -fdata-sections -ffunction-sections -Wl,--gc-sections -ggdb -Wunused-variable -Wno-psabi
|
||||
ifeq ($(MERGE),true)
|
||||
CXXFLAGS += -fwhole-program
|
||||
endif
|
||||
@@ -132,8 +138,7 @@ ifneq (,$(STATIC_BINARY))
|
||||
CFLAGS += -static -fdata-sections -ffunction-sections -Wl,--gc-sections
|
||||
endif
|
||||
|
||||
|
||||
.PHONY: all jar release build-test test clean coverage clean-coverage build-test-java build-test-cpp build-test-libs build-test-bins test-cpp test-java check-md format-md
|
||||
.PHONY: all jar release build-test test clean coverage clean-coverage build-test-java build-test-cpp test-cpp test-java check-md format-md
|
||||
|
||||
all: build/bin build/lib build/$(LIB_PROFILER) build/$(ASPROF) jar build/$(JFRCONV) build/$(ASPROF_HEADER)
|
||||
|
||||
@@ -194,20 +199,20 @@ build/$(ASPROF_HEADER): src/asprof.h
|
||||
mkdir -p build/include
|
||||
cp -f $< build/include
|
||||
|
||||
build/$(API_JAR): $(API_SOURCES)
|
||||
build/$(API_JAR): $(API_SOURCES) $(JAR_MANIFEST)
|
||||
mkdir -p build/api
|
||||
$(JAVAC) $(JAVAC_OPTIONS) -d build/api $(API_SOURCES)
|
||||
$(JAR) cf $@ -C build/api .
|
||||
$(JAR) cfm $@ $(JAR_MANIFEST) -C build/api .
|
||||
$(RM) -r build/api
|
||||
|
||||
build/$(CONVERTER_JAR): $(CONVERTER_SOURCES) $(RESOURCES)
|
||||
mkdir -p build/converter
|
||||
$(JAVAC) $(JAVAC_OPTIONS) -d build/converter $(CONVERTER_SOURCES)
|
||||
$(JAR) cfe $@ Main -C build/converter . -C src/res .
|
||||
$(JAR) cfe $@ one.convert.Main -C build/converter . -C src/res .
|
||||
$(RM) -r build/converter
|
||||
|
||||
%.class: %.java
|
||||
$(JAVAC) -source 7 -target 7 -Xlint:-options -g:none $^
|
||||
$(JAVAC) -source $(JAVA_TARGET) -target $(JAVA_TARGET) -Xlint:-options -g:none $^
|
||||
|
||||
build/test/cpptests: $(CPP_TEST_SOURCES) $(CPP_TEST_HEADER) $(SOURCES) $(HEADERS) $(RESOURCES) $(JAVA_HELPER_CLASSES)
|
||||
mkdir -p build/test
|
||||
@@ -218,19 +223,20 @@ else
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(DEFS) $(INCLUDES) $(CPP_TEST_INCLUDES) -fPIC -o $@ $(SOURCES) $(CPP_TEST_SOURCES) $(LIBS)
|
||||
endif
|
||||
|
||||
build-test-java: all build/$(TEST_JAR) build-test-libs build-test-bins
|
||||
build-test-java: all build/$(TEST_JAR) build/test/build-test-libs build/test/build-test-bins
|
||||
|
||||
build-test-cpp: build/test/cpptests build-test-libs
|
||||
build-test-cpp: build/test/cpptests build/test/build-test-libs
|
||||
|
||||
build-test: build-test-cpp build-test-java
|
||||
|
||||
build-test-libs:
|
||||
build/test/build-test-libs: $(TEST_LIB_SOURCES)
|
||||
@mkdir -p $(TEST_LIB_DIR)
|
||||
$(CC) -shared -fPIC -o $(TEST_LIB_DIR)/libreladyn.$(SOEXT) test/native/libs/reladyn.c
|
||||
$(CC) -shared -fPIC -o $(TEST_LIB_DIR)/libcallsmalloc.$(SOEXT) test/native/libs/callsmalloc.c
|
||||
$(CC) -shared -fPIC $(INCLUDES) -Isrc -o $(TEST_LIB_DIR)/libjnimalloc.$(SOEXT) test/native/libs/jnimalloc.c
|
||||
$(CC) -shared -fPIC -o $(TEST_LIB_DIR)/libmalloc.$(SOEXT) test/native/libs/malloc.c
|
||||
$(CC) -fno-optimize-sibling-calls -shared -fPIC $(INCLUDES) -Isrc -o $(TEST_LIB_DIR)/libjninativestacks.$(SOEXT) test/native/libs/jninativestacks.c
|
||||
$(CC) -shared -fPIC $(INCLUDES) -Isrc -o $(TEST_LIB_DIR)/libjninativelocks.$(SOEXT) test/native/libs/jninativelocks.c -lpthread
|
||||
|
||||
ifeq ($(OS_TAG),linux)
|
||||
$(CC) -c -shared -fPIC -o $(TEST_LIB_DIR)/vaddrdif.o test/native/libs/vaddrdif.c
|
||||
@@ -242,15 +248,18 @@ ifeq ($(OS_TAG),linux)
|
||||
$(AS) -o $(TEST_LIB_DIR)/twiceatzero.o test/native/libs/twiceatzero.s
|
||||
$(LD) -shared -o $(TEST_LIB_DIR)/libtwiceatzero.$(SOEXT) $(TEST_LIB_DIR)/twiceatzero.o --section-start=.seg1=0x4000 -z max-page-size=0x1000
|
||||
endif
|
||||
@touch $@
|
||||
|
||||
build-test-bins:
|
||||
build/test/build-test-bins: $(TEST_BIN_SOURCES)
|
||||
@mkdir -p $(TEST_BIN_DIR)
|
||||
$(CC) -o $(TEST_BIN_DIR)/malloc_plt_dyn test/test/nativemem/malloc_plt_dyn.c
|
||||
$(CC) -o $(TEST_BIN_DIR)/native_api -Isrc test/test/c/native_api.c -ldl
|
||||
$(CC) -o $(TEST_BIN_DIR)/native_lock_contention test/test/nativelock/native_lock_contention.c -lpthread
|
||||
$(CC) -o $(TEST_BIN_DIR)/profile_with_dlopen -Isrc test/test/nativemem/profile_with_dlopen.c -ldl
|
||||
$(CC) -o $(TEST_BIN_DIR)/preload_malloc -Isrc test/test/nativemem/preload_malloc.c -ldl
|
||||
$(CC) -o $(TEST_BIN_DIR)/nativemem_known_lib_crash -Isrc test/test/nativemem/nativemem_known_lib_crash.c -ldl
|
||||
$(CXX) -o $(TEST_BIN_DIR)/non_java_app -std=c++11 $(INCLUDES) $(CPP_TEST_INCLUDES) test/test/nonjava/non_java_app.cpp $(LIBS)
|
||||
@touch $@
|
||||
|
||||
test-cpp: build-test-cpp
|
||||
echo "Running cpp tests..."
|
||||
@@ -258,7 +267,7 @@ test-cpp: build-test-cpp
|
||||
|
||||
test-java: build-test-java
|
||||
echo "Running tests against $(LIB_PROFILER)"
|
||||
$(JAVA) "-Djava.library.path=$(TEST_LIB_DIR)" $(TEST_FLAGS) -ea -cp "build/$(TEST_JAR):build/jar/*:build/lib/*:$(TEST_DEPS_DIR)/*:$(TEST_GEN_DIR)/*" one.profiler.test.Runner $(subst $(COMMA), ,$(TESTS))
|
||||
$(TEST_JAVA) $(TEST_FLAGS) -ea -cp "build/$(TEST_JAR):build/jar/*:$(TEST_DEPS_DIR)/*:$(TEST_GEN_DIR)/*" one.profiler.test.Runner $(subst $(COMMA), ,$(TESTS))
|
||||
|
||||
coverage: override FAT_BINARY=false
|
||||
coverage: clean-coverage
|
||||
@@ -267,7 +276,12 @@ coverage: clean-coverage
|
||||
cd build/test/ && gcovr -r ../.. --html-details --gcov-executable "$(GCOV)" -o coverage/index.html
|
||||
rm -rf -- -.gc*
|
||||
|
||||
test: test-cpp test-java
|
||||
# unit tests shouldn't run if the user selects an integration test target
|
||||
ifeq ($(TESTS),)
|
||||
TEST_CPP := test-cpp
|
||||
endif
|
||||
|
||||
test: $(TEST_CPP) test-java
|
||||
|
||||
$(TEST_DEPS_DIR):
|
||||
mkdir -p $@
|
||||
|
||||
10
README.md
10
README.md
@@ -23,12 +23,12 @@ to learn about more features.
|
||||
|
||||
# Download
|
||||
|
||||
### Stable release: [4.1](https://github.com/async-profiler/async-profiler/releases/tag/v4.1)
|
||||
### Stable release: [4.3](https://github.com/async-profiler/async-profiler/releases/tag/v4.3)
|
||||
|
||||
- Linux x64: [async-profiler-4.1-linux-x64.tar.gz](https://github.com/async-profiler/async-profiler/releases/download/v4.1/async-profiler-4.1-linux-x64.tar.gz)
|
||||
- Linux arm64: [async-profiler-4.1-linux-arm64.tar.gz](https://github.com/async-profiler/async-profiler/releases/download/v4.1/async-profiler-4.1-linux-arm64.tar.gz)
|
||||
- macOS arm64/x64: [async-profiler-4.1-macos.zip](https://github.com/async-profiler/async-profiler/releases/download/v4.1/async-profiler-4.1-macos.zip)
|
||||
- Profile converters: [jfr-converter.jar](https://github.com/async-profiler/async-profiler/releases/download/v4.1/jfr-converter.jar)
|
||||
- Linux x64: [async-profiler-4.3-linux-x64.tar.gz](https://github.com/async-profiler/async-profiler/releases/download/v4.3/async-profiler-4.3-linux-x64.tar.gz)
|
||||
- Linux arm64: [async-profiler-4.3-linux-arm64.tar.gz](https://github.com/async-profiler/async-profiler/releases/download/v4.3/async-profiler-4.3-linux-arm64.tar.gz)
|
||||
- macOS arm64/x64: [async-profiler-4.3-macos.zip](https://github.com/async-profiler/async-profiler/releases/download/v4.3/async-profiler-4.3-macos.zip)
|
||||
- Profile converters: [jfr-converter.jar](https://github.com/async-profiler/async-profiler/releases/download/v4.3/jfr-converter.jar)
|
||||
|
||||
### Nightly builds
|
||||
|
||||
|
||||
@@ -2,15 +2,17 @@
|
||||
|
||||
async-profiler provides `jfrconv` utility to convert between different profile output formats.
|
||||
`jfrconv` can be found at the same location as the `asprof` binary. Converter is also available
|
||||
as a standalone Java application: [`jfr-converter.jar`](https://github.com/async-profiler/async-profiler/releases/download/v4.1/jfr-converter.jar).
|
||||
as a standalone Java application: [`jfr-converter.jar`](https://github.com/async-profiler/async-profiler/releases/latest/download/jfr-converter.jar).
|
||||
|
||||
## Supported conversions
|
||||
|
||||
| Source | html | collapsed | pprof | pb.gz | heatmap | otlp |
|
||||
| --------- | ---- | --------- | ----- | ----- | ------- | ---- |
|
||||
| jfr | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| html | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| collapsed | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
The tool can convert several source formats into various outputs. The conversion capabilities are summarized below:
|
||||
|
||||
| Source format | to html | to collapsed | to pprof | to pb.gz | to heatmap | to otlp |
|
||||
| ------------- | ------- | ------------ | -------- | -------- | ---------- | ------- |
|
||||
| jfr | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| html | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
| collapsed | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -43,6 +45,8 @@ Conversion options:
|
||||
|
||||
# otlp: OpenTelemetry profile format.
|
||||
|
||||
Differential Flame Graph:
|
||||
--diff <base-profile> <new-profile>
|
||||
|
||||
JFR options:
|
||||
--cpu Generate only CPU profile during conversion
|
||||
@@ -53,7 +57,9 @@ JFR options:
|
||||
--nativemem Generate native memory allocation profile
|
||||
--leak Only include memory leaks in nativemem
|
||||
--tail RATIO Ignore tail allocations for leak profiling (10% by default)
|
||||
--lock Generate only Lock contention profile during conversion
|
||||
--lock Generate only lock contention profile during conversion
|
||||
--nativelock Generate only native (pthread) lock contention profile
|
||||
--trace Convert only MethodTrace events
|
||||
-t --threads Split stack traces by threads
|
||||
-s --state LIST Filter thread states: runnable, sleeping, default. State name is case insensitive
|
||||
and can be abbreviated, e.g. -s r
|
||||
@@ -73,6 +79,7 @@ JFR options:
|
||||
# an absolute time in hh:mm:ss or yyyy-MM-dd'T'hh:mm:ss format;
|
||||
# a relative time from the beginning of recording;
|
||||
# a relative time from the end of recording (a negative number).
|
||||
--latency MS Retain only samples within MethodTraces of at least MS milliseconds
|
||||
|
||||
Flame Graph options:
|
||||
--title STRING Convert to Flame Graph with provided title
|
||||
@@ -117,7 +124,7 @@ jfrconv --cpu foo.jfr
|
||||
|
||||
for HTML output as HTML is the default format for conversion from JFR.
|
||||
|
||||
#### Flame Graph options
|
||||
### Flame Graph options
|
||||
|
||||
To add a custom title to the generated Flame Graph, use `--title`, which has the default value `Flame Graph`:
|
||||
|
||||
@@ -125,9 +132,37 @@ To add a custom title to the generated Flame Graph, use `--title`, which has the
|
||||
jfrconv --cpu foo.jfr foo.html -r --title "Custom Title"
|
||||
```
|
||||
|
||||
### Other formats
|
||||
### Differential Flame Graph
|
||||
|
||||
`jfrconv` supports converting a JFR file to `collapsed`, `pprof`, `pb.gz` and `heatmap` formats as well.
|
||||
To find performance regressions, it may be useful to compare current profile
|
||||
to a previous one that serves as a baseline. Differential Flame Graph
|
||||
visualizes such a comparsion with a special color scheme:
|
||||
|
||||
- Red color denotes frames with more samples comparing to the baseline (i.e. regression);
|
||||
- Blue is for frames with less samples;
|
||||
- Yellow are new frames that were absent in the baseline.
|
||||
|
||||
The more intense the color, the larger the delta.
|
||||
For each different frame, the delta value is displayed in a tooltip.
|
||||
|
||||

|
||||
|
||||
Differential Flame Graph takes the shape of the current profile:
|
||||
all frames have exactly the same size as in the normal Flame Graph.
|
||||
This means, frames that exist only in the base profile will not be visible.
|
||||
To see such frames, create another differential Flame Graph,
|
||||
swapping the base and the current input file.
|
||||
|
||||
To create differential Flame Graph, run `jfrconv --diff` with two input files:
|
||||
basline profile and new profile. Both files can be in JFR, HTML, or collapsed format.
|
||||
Other converter options work as usual.
|
||||
|
||||
```
|
||||
jfrconv --cpu --diff baseline.jfr new.jfr diff.html
|
||||
```
|
||||
|
||||
Output file name is optional. If omitted, `jfrconv` takes the name
|
||||
of the second input file, replacing its extension with `.diff.html`.
|
||||
|
||||
## Standalone converter examples
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ process requires setting two kernel parameters. You can set them using sysctl as
|
||||
# sysctl kernel.kptr_restrict=0
|
||||
```
|
||||
|
||||
For better profiling accuracy, it is [recommended](Troubleshooting.md#known-limitations)
|
||||
to start the JVM with `-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints` flags,
|
||||
unless async-profiler is loaded at JVM startup.
|
||||
|
||||
## Find a process to profile
|
||||
|
||||
Common ways to find the target process include using
|
||||
|
||||
@@ -9,10 +9,17 @@ it is possible to attach async-profiler as an agent on the command line. For exa
|
||||
$ java -agentpath:/path/to/libasyncProfiler.so=start,event=cpu,file=profile.html ...
|
||||
```
|
||||
|
||||
On macOS, the library name is `libasyncProfiler.dylib` instead of `libasyncProfiler.so`.
|
||||
|
||||
Agent library is configured through the JVMTI argument interface.
|
||||
The format of the arguments string is described
|
||||
[in the source code](https://github.com/async-profiler/async-profiler/blob/v4.1/src/arguments.cpp#L45).
|
||||
`asprof` actually converts command line arguments to that format.
|
||||
The argument string is a comma-separated list of [profiler options](ProfilerOptions.md):
|
||||
|
||||
```
|
||||
option[=value],option[=value]...
|
||||
```
|
||||
|
||||
`asprof` internally converts command line arguments to the above format and attaches
|
||||
`libasyncProfiler.so` agent to a running process.
|
||||
|
||||
Another important use of attaching async-profiler as an agent is for continuous profiling.
|
||||
|
||||
|
||||
@@ -18,61 +18,64 @@ The below options are `action`s for async-profiler and common for both `asprof`
|
||||
| `resume` | Start or resume earlier profiling session that has been stopped. All the collected data remains valid. The profiling options are not preserved between sessions, and should be specified again. |
|
||||
| `stop` | Stop profiling and print the report. |
|
||||
| `dump` | Dump collected data without stopping profiling session. |
|
||||
| `check` | Check if the specified profiling event is available. |
|
||||
| `status` | Print profiling status: whether profiler is active and for how long. |
|
||||
| `meminfo` | Print used memory statistics. |
|
||||
| `metrics` | Print profiler metrics in Prometheus format. |
|
||||
| `list` | Show the list of profiling events available for the target process specified with PID. |
|
||||
|
||||
## Options applicable to any output format
|
||||
## General options
|
||||
|
||||
| asprof | Launch as agent | Description |
|
||||
| --------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `-o fmt` | `fmt` | Specifies what information to dump when profiling ends. For various dump option details, please refer to [Dump Option Appendix](#dump-option). |
|
||||
| `-d N` | N/A | asprof-only option designed for interactive use. It is a shortcut for running 3 actions: start, sleep for N seconds, stop. If no `start`, `resume`, `stop` or `status` option is given, the profiler will run for the specified period of time and then automatically stop.<br>Example: `asprof -d 30 <pid>` |
|
||||
| `--timeout N` | `timeout=N` | The profiling duration, in seconds. The profiler will run for the specified period of time and then automatically stop.<br>Example: `java -agentpath:/path/to/libasyncProfiler.so=start,event=cpu,timeout=30,file=profile.html <application>` |
|
||||
| `-e --event EVENT` | `event=EVENT` | The profiling event: `cpu`, `alloc`, `nativemem`, `lock`, `cache-misses` etc. Use `list` to see the complete list of available events.<br>Please refer to [Profiling Modes](ProfilingModes.md) for additional information. |
|
||||
| `-i --interval N` | `interval=N` | Interval has different meaning depending on the event. For CPU profiling, it's CPU time in nanoseconds. In wall clock mode, it's wall clock time. For Java method profiling or native function profiling, it's number of calls. For PMU profiling, it's number of events. Time intervals may be followed by `s` for seconds, `ms` for milliseconds, `us` for microseconds or `ns` for nanoseconds.<br>Example: `asprof -e cpu -i 5ms 8983` |
|
||||
| `--alloc N` | `alloc=N` | Allocation profiling interval in bytes or in other units, if N is followed by `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). |
|
||||
| `--live` | `live` | Retain allocation samples with live objects only (object that have not been collected by the end of profiling session). Useful for finding Java heap memory leaks. |
|
||||
| `--nativemem N` | `nativemem=N` | Native memory allocation profiling. N, if specified is the interval in bytes or in other units, if N is followed by `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). Default N is 0. |
|
||||
| `--nofree` | `nofree` | Will not record free calls in native memory allocation profiling. This is relevant when tracking memory leaks is not important and there are lots of free calls. |
|
||||
| `--trace METHOD[:DURATION]` | `trace=METHOD[:DURATION]` | Method to be traced with optional latency threshold. Example: `--trace my.pkg.Class.Method:50ms`. Latency threshold defaults to 0 (all calls are profiled). Can be used multiple times. |
|
||||
|
||||
| `--lock DURATION` | `lock=DURATION` | In lock profiling mode, sample contended locks when total lock duration overflows the threshold. |
|
||||
| `--wall INTERVAL` | `wall=INTERVAL` | Wall clock profiling interval. Use this option instead of `-e wall` to enable wall clock profiling with another event, typically `cpu`.<br>Example: `asprof -e cpu --wall 100ms -f combined.jfr 8983`. |
|
||||
| `--proc INTERVAL` | `proc=INTERVAL` | Collect statistics about other processes in the system. Default sampling interval is 30s. |
|
||||
| `-j N` | `jstackdepth=N` | Sets the maximum stack depth. The default is 2048.<br>Example: `asprof -j 30 8983` |
|
||||
| `-I PATTERN` | `include=PATTERN` | Filter stack traces by the given pattern(s). `-I` defines the name pattern that _must_ be present in the stack traces. `-I` can be specified multiple times. A pattern may begin or end with a star `*` that denotes any (possibly empty) sequence of characters.<br>Example: `asprof -I 'Primes.*' -I 'java/*' 8983` |
|
||||
| `-X PATTERN` | `exclude=PATTERN` | Filter stack traces by the given pattern(s). `-X` defines the name pattern that _must not_ occur in any of stack traces in the output. `-X` can be specified multiple times. A pattern may begin or end with a star `*` that denotes any (possibly empty) sequence of characters.<br>Example: `asprof -X '*Unsafe.park*' 8983` |
|
||||
| `-L level` | `loglevel=level` | Log level: `debug`, `info`, `warn`, `error` or `none`. |
|
||||
| `-F features` | `features=LIST` | Comma separated (or `+` separated when launching as an agent) list of stack walking features. Supported features are:<ul><li>`stats` - log stack walking performance stats.</li><li>`vtable` - display targets of megamorphic virtual calls as an extra frame on top of `vtable stub` or `itable stub`.</li><li>`comptask` - display current compilation task (a Java method being compiled) in a JIT compiler stack trace.</li><li>`pcaddr` - display instruction addresses .</li></ul>More details [here](AdvancedStacktraceFeatures.md). |
|
||||
| `-f FILENAME` | `file` | The file name to dump the profile information to.<br>`%p` in the file name is expanded to the PID of the target JVM;<br>`%t` - to the timestamp;<br>`%n{MAX}` - to the sequence number;<br>`%{ENV}` - to the value of the given environment variable.<br>Example: `asprof -o collapsed -f /tmp/traces-%t.txt 8983` |
|
||||
| `--loop TIME` | `loop=TIME` | Run profiler in a loop (continuous profiling). The argument is either a clock time (`hh:mm:ss`) or a loop duration in `s`econds, `m`inutes, `h`ours, or `d`ays. Make sure the filename includes a timestamp pattern, or the output will be overwritten on each iteration.<br>Example: `asprof --loop 1h -f /var/log/profile-%t.jfr 8983` |
|
||||
| `--all-user` | `alluser` | Include only user-mode events. This option is helpful when kernel profiling is restricted by `perf_event_paranoid` settings. |
|
||||
| `--sched` | `sched` | Group threads by Linux-specific scheduling policy: BATCH/IDLE/OTHER. |
|
||||
| `--cstack MODE` | `cstack=MODE` | How to walk native frames (C stack). Possible modes are `fp` (Frame Pointer), `dwarf` (DWARF unwind info), `lbr` (Last Branch Record, available on Haswell since Linux 4.1), `vm`, `vmx` (HotSpot VM Structs) and `no` (do not collect C stack).<br><br>By default, C stack is shown in cpu, ctimer, wall-clock and perf-events profiles. Java-level events like `alloc` and `lock` collect only Java stack. |
|
||||
| `--signal NUM` | `signal=NUM` | Use alternative signal for cpu or wall clock profiling. To change both signals, specify two numbers separated by a slash: `--signal SIGCPU/SIGWALL`. |
|
||||
| `--clock SOURCE` | `clock=SOURCE` | Clock source for JFR timestamps: `tsc` (default) or `monotonic` (equivalent for `CLOCK_MONOTONIC`). |
|
||||
| `--begin function` | `begin=FUNCTION` | Automatically start profiling when the specified native function is executed. |
|
||||
| `--end function` | `end=FUNCTION` | Automatically stop profiling when the specified native function is executed. |
|
||||
| `--ttsp` | `ttsp` | Time-to-safepoint profiling. An alias for `--begin SafepointSynchronize::begin --end RuntimeService::record_safepoint_synchronized`.<br>It is not a separate event type, but rather a constraint. Whatever event type you choose (e.g. `cpu` or `wall`), the profiler will work as usual, except that only events between the safepoint request and the start of the VM operation will be recorded. |
|
||||
| `--nostop` | `nostop` | Record profiling window between `--begin` and `--end`, but do not stop profiling outside window. |
|
||||
| `--libpath PATH` | `libpath=PATH` | Full path to `libasyncProfiler.so` (useful when profiling a container from the host). |
|
||||
| `--filter FILTER` | `filter=FILTER` | In the wall-clock profiling mode, profile only threads with the specified ids.<br>Example: `asprof -e wall -d 30 --filter 120-127,132,134 Computey` |
|
||||
| `--fdtransfer` | `fdtransfer` | Run a background process that provides access to perf_events to an unprivileged process. `--fdtransfer` is useful for profiling a process in a container (which lacks access to perf_events) from the host.<br>See [Profiling Java in a container](ProfilingInContainer.md). |
|
||||
| `--target-cpu` | `target-cpu` | In perf_events profiling mode, instruct the profiler to only sample threads running on the specified CPU, defaults to -1.<br>Example: `asprof --target-cpu 3`. |
|
||||
| `--record-cpu` | `record-cpu` | In perf_events profiling mode, instruct the profiler to capture which CPU a sample was taken on. |
|
||||
| `-v --version` | `version` | Prints the version of profiler library. If PID is specified, gets the version of the library loaded into the given process. |
|
||||
| asprof | Launch as agent | Description |
|
||||
| -------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `-o fmt` | `fmt` | Specifies what information to dump when profiling ends. For various dump option details, please refer to [Dump Option Appendix](#dump-option). |
|
||||
| `-f FILENAME` | `file=FILENAME` | The file name to dump the profile information to.<br>`%p` in the file name is expanded to the PID of the target JVM;<br>`%t` - to the timestamp;<br>`%n{MAX}` - to the sequence number;<br>`%{ENV}` - to the value of the given environment variable.<br>Example: `asprof -o collapsed -f /tmp/traces-%t.txt 8983` |
|
||||
| `-d N` | N/A | asprof-only option designed for interactive use. It is a shortcut for running 3 actions: start, sleep for N seconds, stop. If no `start`, `resume`, `stop` or `status` option is given, the profiler will run for the specified period of time and then automatically stop.<br>Example: `asprof -d 30 <pid>` |
|
||||
| `--timeout N` | `timeout=N` | The profiling duration, in seconds. The profiler will run for the specified period of time and then automatically stop.<br>Example: `java -agentpath:/path/to/libasyncProfiler.so=start,event=cpu,timeout=30,file=profile.html <application>` |
|
||||
| `--loop TIME` | `loop=TIME` | Run profiler in a loop (continuous profiling). The argument is either a clock time (`hh:mm:ss`) or a loop duration in `s`econds, `m`inutes, `h`ours, or `d`ays. Make sure the filename includes a timestamp pattern, or the output will be overwritten on each iteration.<br>Example: `asprof --loop 1h -f /var/log/profile-%t.jfr 8983` |
|
||||
| `-e --event EVENT` | `event=EVENT` | The profiling event: `cpu`, `alloc`, `nativemem`, `lock`, `cache-misses` etc. Use `list` to see the complete list of available events.<br>Please refer to [Profiling Modes](ProfilingModes.md) for additional information. |
|
||||
| `-i --interval N` | `interval=N` | Interval has different meaning depending on the event. For CPU profiling, it's CPU time in nanoseconds. In wall clock mode, it's wall clock time. For Java method profiling or native function profiling, it's number of calls. For PMU profiling, it's number of events. Time intervals may be followed by `s` for seconds, `ms` for milliseconds, `us` for microseconds or `ns` for nanoseconds.<br>Example: `asprof -e cpu -i 5ms 8983` |
|
||||
| `--alloc N` | `alloc=N` | Allocation profiling interval in bytes or in other units, if N is followed by `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). |
|
||||
| `--tlab` | `tlab` | Use TLAB events for allocation profiling |
|
||||
| `--live` | `live` | Retain allocation samples with live objects only (object that have not been collected by the end of profiling session). Useful for finding Java heap memory leaks. |
|
||||
| `--nativemem N` | `nativemem=N` | Native memory allocation profiling. N, if specified is the interval in bytes or in other units, if N is followed by `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). Default N is 0. |
|
||||
| `--nofree` | `nofree` | Will not record free calls in native memory allocation profiling. This is relevant when tracking memory leaks is not important and there are lots of free calls. |
|
||||
| `--trace METHOD[:T]` | `trace=METHOD[:T]` | Java method to be traced, optionally followed by a latency threshold.<br>Example: `--trace my.pkg.Class.Method:50ms`.<br>Latency threshold defaults to 0 (all calls are profiled). Can be used multiple times. |
|
||||
| `--lock TIME` | `lock=TIME` | In lock profiling mode, sample contended locks whenever total lock wait time overflows the specified threshold. |
|
||||
| `--nativelock TIME` | `nativelock=TIME ` | In native lock profiling mode, sample contended pthread locks (mutex/rwlock) whenever total lock wait time overflows the specified threshold. |
|
||||
| `--wall INTERVAL` | `wall=INTERVAL` | Wall clock profiling interval. Use this option instead of `-e wall` to enable wall clock profiling with another event, typically `cpu`.<br>Example: `asprof -e cpu --wall 100ms -f combined.jfr 8983`. |
|
||||
| `--nobatch` | `nobatch` | Disable wall clock profiling optimization. Async-profiler will emit one `jdk.ExecutionSample` event for each wall clock sample instead of batching them in a custom `profiler.WallClockSample` event. |
|
||||
| `-j N` | `jstackdepth=N` | Sets the maximum stack depth. The default is 2048.<br>Example: `asprof -j 30 8983`<br>The argument may include two numbers separated by `/` (e.g. `200/40`). In this case, stack traces deeper than 200 frames will be truncated to the top 40 frames. This can be useful to prevent a deep recursion from bloating the profile. |
|
||||
| `-F features` | `features=LIST` | Comma separated (or `+` separated when launching as an agent) list of stack walking features. Supported features are:<ul><li>`stats` - log stack walking performance stats.</li><li>`vtable` - display targets of megamorphic virtual calls as an extra frame on top of `vtable stub` or `itable stub`.</li><li>`comptask` - display current compilation task (a Java method being compiled) in a JIT compiler stack trace.</li><li>`pcaddr` - display instruction addresses .</li></ul>More details [here](AdvancedStacktraceFeatures.md). |
|
||||
| `-L level` | `loglevel=level` | Log level: `debug`, `info`, `warn`, `error` or `none`. |
|
||||
| N/A | `log=FILENAME` | Dedicated file for log messages. Used internally by asprof. |
|
||||
| N/A | `quiet` | Do not log "Profiling started/stopped" message. Used internally by asprof. |
|
||||
| N/A | `server=ADDRESS` | Start insecure HTTP server with the given IP address/port to control the profiler. This option can be specified as `-agentpath` argument only. Be careful not to expose async-profiler server in a public network. |
|
||||
| `--all-user` | `alluser` | Include only user-mode events. This option is helpful when kernel profiling is restricted by `perf_event_paranoid` settings. |
|
||||
| `--sched` | `sched` | Group threads by Linux-specific scheduling policy: BATCH/IDLE/OTHER. |
|
||||
| `--cstack MODE` | `cstack=MODE` | How to walk native frames (C stack). Possible modes are `fp` (Frame Pointer), `dwarf` (DWARF unwind info), `vm`, `vmx` (HotSpot VM Structs) and `no` (do not collect C stack).<br><br>By default, C stack is shown in cpu, ctimer, wall-clock and perf-events profiles. Java-level events like `alloc` and `lock` collect only Java stack. |
|
||||
| `--signal NUM` | `signal=NUM` | Use alternative signal for cpu or wall clock profiling. To change both signals, specify two numbers separated by a slash: `--signal SIGCPU/SIGWALL`. |
|
||||
| `--clock SOURCE` | `clock=SOURCE` | Clock source for JFR timestamps: `tsc` (default) or `monotonic` (equivalent for `CLOCK_MONOTONIC`). |
|
||||
| `--begin function` | `begin=FUNCTION` | Automatically start profiling when the specified native function is executed. |
|
||||
| `--end function` | `end=FUNCTION` | Automatically stop profiling when the specified native function is executed. |
|
||||
| `--ttsp` | `ttsp` | Time-to-safepoint profiling. An alias for `--begin SafepointSynchronize::begin --end RuntimeService::record_safepoint_synchronized`.<br>It is not a separate event type, but rather a constraint. Whatever event type you choose (e.g. `cpu` or `wall`), the profiler will work as usual, except that only events between the safepoint request and the start of the VM operation will be recorded. |
|
||||
| `--nostop` | `nostop` | Record profiling window between `--begin` and `--end`, but do not stop profiling outside window. |
|
||||
| `--memlimit SIZE` | `memlimit=SIZE` | Limit memory used by the call trace storage. Once the limit is exceeded, no new stack traces will be recorded. The lowest possible limit is 10 MB; the default is unlimited.<br>Example: `asprof -e cpu --memlimit 128m` |
|
||||
| `--libpath PATH` | N/A | Full path to `libasyncProfiler.so` (useful when profiling a container from the host). |
|
||||
| `--filter FILTER` | `filter=FILTER` | In the wall-clock profiling mode, profile only threads with the specified ids.<br>Example: `asprof -e wall -d 30 --filter 120-127,132,134 Computey` |
|
||||
| `--fdtransfer` | `fdtransfer` | Run a background process that provides access to perf_events to an unprivileged process. `--fdtransfer` is useful for profiling a process in a container (which lacks access to perf_events) from the host.<br>See [Profiling Java in a container](ProfilingInContainer.md). |
|
||||
| `--target-cpu` | `target-cpu` | In perf_events profiling mode, instruct the profiler to only sample threads running on the specified CPU, defaults to -1.<br>Example: `asprof --target-cpu 3`. |
|
||||
| `--record-cpu` | `record-cpu` | In perf_events profiling mode, instruct the profiler to capture which CPU a sample was taken on. |
|
||||
| `-v --version` | `version` | Prints the version of profiler library. If PID is specified, gets the version of the library loaded into the given process. |
|
||||
|
||||
## Options applicable to JFR output only
|
||||
|
||||
| asprof | Launch as agent | Description |
|
||||
| ------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `--chunksize N` | `chunksize=N` | Approximate size for a single JFR chunk. A new chunk will be started whenever specified size is reached. The default `chunksize` is 100MB.<br>Example: `asprof -f profile.jfr --chunksize 100m 8983` |
|
||||
| `--chunktime N` | `chunktime=N` | Approximate time limit for a single JFR chunk. A new chunk will be started whenever specified time limit is reached. The default `chunktime` is 1 hour.<br>Example: `asprof -f profile.jfr --chunktime 1h 8983` |
|
||||
| `--jfropts OPTIONS` | `jfropts=OPTIONS` | Comma separated list of JFR recording options. Currently, the only available option is `mem` supported on Linux 3.17+. `mem` enables accumulating events in memory instead of flushing synchronously to a file. |
|
||||
| `--jfrsync CONFIG` | `jfrsync[=CONFIG]` | Start Java Flight Recording with the given configuration synchronously with the profiler. The output .jfr file will include all regular JFR events, except that execution samples will be obtained from async-profiler. This option implies `-o jfr`.<br>`CONFIG` is a predefined JFR profile or a JFR configuration file (.jfc) or a list of JFR events started with `+`.<br><br>Example: `asprof -e cpu --jfrsync profile -f combined.jfr 8983` |
|
||||
| `--all` | `all` | Shorthand for enabling `cpu`, `wall`, `alloc`, `live`, `nativemem` and `lock` profiling simultaneously. This can be combined with `--alloc 2m --lock 10ms` etc. to pass custom interval/threshold. It is also possible to combine it with `-e` argument to change the type of event being collected (default is `cpu`). This is not recommended for production, especially for continuous profiling. |
|
||||
| asprof | Launch as agent | Description |
|
||||
| ------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `--chunksize N` | `chunksize=N` | Approximate size for a single JFR chunk. A new chunk will be started whenever specified size is reached. The default `chunksize` is 100MB.<br>Example: `asprof -f profile.jfr --chunksize 100m 8983` |
|
||||
| `--chunktime N` | `chunktime=N` | Approximate time limit for a single JFR chunk. A new chunk will be started whenever specified time limit is reached. The default `chunktime` is 1 hour.<br>Example: `asprof -f profile.jfr --chunktime 1h 8983` |
|
||||
| `--jfropts OPTIONS` | `jfropts=OPTIONS` | Comma separated list of JFR recording options. Currently, the only available option is `mem` supported on Linux 3.17+. `mem` enables accumulating events in memory instead of flushing synchronously to a file. |
|
||||
| `--jfrsync CONFIG` | `jfrsync[=CONFIG]` | Start Java Flight Recording with the given configuration synchronously with the profiler. The output .jfr file will include all regular JFR events, except that execution samples will be obtained from async-profiler. This option implies `-o jfr`.<br>`CONFIG` is a predefined JFR profile or a JFR configuration file (.jfc) or a list of JFR events started with `+`.<br>Example: `asprof -e cpu --jfrsync profile -f combined.jfr 8983` |
|
||||
| `--proc INTERVAL` | `proc=INTERVAL` | Collect statistics about other processes in the system. Default sampling interval is 30s. |
|
||||
| `--all` | `all` | Shorthand for enabling `cpu`, `wall`, `alloc`, `live`, `lock`, `nativelock`, `nativemem`, and `proc` profiling simultaneously. This can be combined with `--alloc 2m --lock 10ms` etc. to pass custom interval/threshold. It is also possible to combine it with `-e` argument to change the type of event being collected (default is `cpu`). This is not recommended for production, especially for continuous profiling. |
|
||||
|
||||
## Options applicable to FlameGraph and Tree view outputs only
|
||||
|
||||
@@ -89,15 +92,20 @@ By default, async-profiler merges stack traces starting from the outermost (e.g.
|
||||
|
||||
## Options applicable to any output format except JFR
|
||||
|
||||
| asprof | Launch as agent | Description |
|
||||
| -------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `-t --threads` | `threads` | Profile threads separately. Each stack trace will end with a frame that denotes a single thread.<br>Example: `asprof -t 8983` |
|
||||
| `-s --simple` | `simple` | Print simple class names instead of fully qualified names. |
|
||||
| `-n --norm` | `norm` | Normalize names of hidden classes / lambdas. |
|
||||
| `-g --sig` | `sig` | Print method signatures. |
|
||||
| `-l --lib` | `lib` | Prepend library names to symbols, e.g. ``libjvm.so`JVM_DefineClassWithSource``. |
|
||||
| `--total` | `total` | Count the total value of the collected metric instead of the number of samples, e.g. total allocation size. |
|
||||
| `-a --ann` | `ann` | Annotate JIT compiled methods with `_[j]`, inlined methods with `_[i]`, interpreted methods with `_[0]` and C1 compiled methods with `_[1]`. FlameGraph and Tree view will color frames depending on their type regardless of this option. |
|
||||
| asprof | Launch as agent | Description |
|
||||
| -------------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `-t --threads` | `threads` | Profile threads separately. Each stack trace will end with a frame that denotes a single thread.<br>Example: `asprof -t 8983` |
|
||||
| `-s --simple` | `simple` | Print simple class names instead of fully qualified names. |
|
||||
| `-n --norm` | `norm` | Normalize names of hidden classes / lambdas. |
|
||||
| `-g --sig` | `sig` | Print method signatures. |
|
||||
| `-a --ann` | `ann` | Annotate JIT compiled methods with `_[j]`, inlined methods with `_[i]`, interpreted methods with `_[0]` and C1 compiled methods with `_[1]`. FlameGraph and Tree view will color frames depending on their type regardless of this option. |
|
||||
| `-l --lib` | `lib` | Prepend library names to symbols, e.g. ``libjvm.so`JVM_DefineClassWithSource``. |
|
||||
| `--dot` | `dot` | Dotted class names, e.g. `java.lang.String` instead of `java/lang/String`. |
|
||||
| `--samples` | `samples` | Count the number of samples. This is the default aggregation option. |
|
||||
| `--total` | `total` | Count the total value of the collected metric instead of the number of samples, e.g. total allocation size. |
|
||||
| `-I PATTERN` | `include=PATTERN` | Filter stack traces by the given pattern(s). `-I` defines the name pattern that _must_ be present in the stack traces. `-I` can be specified multiple times. A pattern may begin or end with a star `*` that denotes any (possibly empty) sequence of characters.<br>Example: `asprof -I 'Primes.*' -I 'java/*' 8983` |
|
||||
| `-X PATTERN` | `exclude=PATTERN` | Filter stack traces by the given pattern(s). `-X` defines the name pattern that _must not_ occur in any of stack traces in the output. `-X` can be specified multiple times. A pattern may begin or end with a star `*` that denotes any (possibly empty) sequence of characters.<br>Example: `asprof -X '*Unsafe.park*' 8983` |
|
||||
| N/A | `mcache[=AGE]` | Maximum age of the method name cache. Default is `0` (do not cache method names between profiling sessions). |
|
||||
|
||||
## Appendix
|
||||
|
||||
|
||||
@@ -172,6 +172,30 @@ enter this lock/monitor.
|
||||
|
||||
Example: `asprof -e lock -t -i 5ms -f result.html 8983`
|
||||
|
||||
## Native lock profiling
|
||||
|
||||
`--nativelock` option tells async-profiler to measure pthread lock contention in the profiled application.
|
||||
Native lock profiling can help developers understand pthread lock acquisition patterns, lock contention (when threads
|
||||
have to wait to acquire native locks), time spent waiting for pthread mutexes and read-write locks, and which code paths
|
||||
are blocked due to native synchronization primitives.
|
||||
|
||||
Native lock profiling works by intercepting calls to:
|
||||
|
||||
- [`pthread_mutex_lock`](https://man7.org/linux/man-pages/man3/pthread_mutex_lock.3p.html)
|
||||
- [`pthread_rwlock_rdlock`](https://man7.org/linux/man-pages/man3/pthread_rwlock_rdlock.3p.html)
|
||||
- [`pthread_rwlock_wrlock`](https://man7.org/linux/man-pages/man3/pthread_rwlock_wrlock.3p.html)
|
||||
|
||||
In this mode, the top frame shows the native function that experienced contention (e.g., pthread_mutex_lock_hook),
|
||||
and the counter represents the number of nanoseconds threads spent waiting to acquire the lock.
|
||||
|
||||
Key differences from Java lock profiling:
|
||||
|
||||
- Profiles native pthread locks instead of Java monitors.
|
||||
- Works with C/C++ applications and native libraries used by Java applications.
|
||||
- Captures contention in native code paths that Java lock profiling cannot see.
|
||||
|
||||
Example: `asprof --nativelock 5ms -t -f result.html 8983`
|
||||
|
||||
## Java method profiling
|
||||
|
||||
`-e ClassName.methodName` option instruments the given Java method
|
||||
@@ -295,6 +319,7 @@ asprof --loop 1h -f /var/log/profile-%t.jfr 8983
|
||||
| `-e page-faults` | Software page faults |
|
||||
| `-e context-switches` | Context switches |
|
||||
| `-e cycles` | Total CPU cycles |
|
||||
| `-e ref-cycles` | CPU reference cycles, not affected by CPU frequency scaling |
|
||||
| `-e instructions` | Retired CPU instructions |
|
||||
| `-e cache-references` | Cache accesses (usually Last Level Cache, but may depend on the architecture) |
|
||||
| `-e cache-misses` | Cache accesses requiring fetching data from a higher-level cache or main memory |
|
||||
|
||||
@@ -23,18 +23,6 @@ due to being signal safe in async-profiler.
|
||||
|
||||
The feature can be enabled with the option `--cstack dwarf` (or its agent equivalent `cstack=dwarf`).
|
||||
|
||||
## LBR
|
||||
|
||||
Modern Intel CPUs can profile branch instructions, including `call`s and `ret`s, and store their source and destination
|
||||
addresses (Last Branch Records) in hardware registers. Starting from Haswell, CPU can match these addresses to form a
|
||||
branch stack. This branch stack will be effectively a call chain automatically collected by the hardware.
|
||||
|
||||
LBR stacks are not always complete or accurate, but they still appear much more helpful comparing to FP-based stack
|
||||
walking, when a native library is compiled with omitted frame pointers. It works only with hardware events like
|
||||
`-e cycles` (`instructions`, `cache-misses` etc.) and the maximum call chain depth is 32 (hardware limit).
|
||||
|
||||
The feature can be enabled with the option `--cstack lbr` (or its agent equivalent `cstack=lbr`).
|
||||
|
||||
## VM Structs
|
||||
|
||||
async-profiler can leverage JVM internal structures to replicate the logic of Java stack walking
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>tools.profiler</groupId>
|
||||
<artifactId>jfr-converter</artifactId>
|
||||
<version>4.2</version>
|
||||
<version>4.4</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>async-profiler</name>
|
||||
@@ -57,7 +57,7 @@
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>Main</mainClass>
|
||||
<mainClass>one.convert.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
|
||||
7
pom.xml
7
pom.xml
@@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>tools.profiler</groupId>
|
||||
<artifactId>async-profiler</artifactId>
|
||||
<version>4.2</version>
|
||||
<version>4.4</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>async-profiler</name>
|
||||
@@ -98,6 +98,11 @@
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestFile>src/api/one/profiler/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
|
||||
@@ -18,6 +18,33 @@ u64 AllocTracer::_interval;
|
||||
volatile u64 AllocTracer::_allocated_bytes;
|
||||
|
||||
|
||||
Error AllocTracer::initialize() {
|
||||
if (_in_new_tlab.entry() == 0 || _outside_tlab.entry() == 0) {
|
||||
CodeCache* libjvm = VMStructs::libjvm();
|
||||
const void* ne;
|
||||
const void* oe;
|
||||
|
||||
if ((ne = libjvm->findSymbolByPrefix("_ZN11AllocTracer27send_allocation_in_new_tlab")) != NULL &&
|
||||
(oe = libjvm->findSymbolByPrefix("_ZN11AllocTracer28send_allocation_outside_tlab")) != NULL) {
|
||||
_trap_kind = 1; // JDK 10+
|
||||
} else if ((ne = libjvm->findSymbolByPrefix("_ZN11AllocTracer33send_allocation_in_new_tlab_eventE11KlassHandleP8HeapWord")) != NULL &&
|
||||
(oe = libjvm->findSymbolByPrefix("_ZN11AllocTracer34send_allocation_outside_tlab_eventE11KlassHandleP8HeapWord")) != NULL) {
|
||||
_trap_kind = 1; // JDK 8u262+
|
||||
} else if ((ne = libjvm->findSymbolByPrefix("_ZN11AllocTracer33send_allocation_in_new_tlab_event")) != NULL &&
|
||||
(oe = libjvm->findSymbolByPrefix("_ZN11AllocTracer34send_allocation_outside_tlab_event")) != NULL) {
|
||||
_trap_kind = 2; // JDK 7-9
|
||||
} else {
|
||||
return Error("No AllocTracer symbols found. Are JDK debug symbols installed?");
|
||||
}
|
||||
|
||||
_in_new_tlab.assign(ne);
|
||||
_outside_tlab.assign(oe);
|
||||
_in_new_tlab.pair(_outside_tlab);
|
||||
}
|
||||
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
// Called whenever our breakpoint trap is hit
|
||||
void AllocTracer::trapHandler(int signo, siginfo_t* siginfo, void* ucontext) {
|
||||
StackFrame frame(ucontext);
|
||||
@@ -69,46 +96,15 @@ void AllocTracer::recordAllocation(void* ucontext, EventType event_type, uintptr
|
||||
Profiler::instance()->recordSample(ucontext, total_size, event_type, &event);
|
||||
}
|
||||
|
||||
Error AllocTracer::check(Arguments& args) {
|
||||
Error AllocTracer::start(Arguments& args) {
|
||||
if (args._live && !args._all) {
|
||||
// This engine is only going to be selected in Profiler::selectAllocEngine
|
||||
// when can_generate_sampled_object_alloc_events is not available, i.e. JDK<11.
|
||||
return Error("'live' option is supported on OpenJDK 11+");
|
||||
}
|
||||
|
||||
if (_in_new_tlab.entry() != 0 && _outside_tlab.entry() != 0) {
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
CodeCache* libjvm = VMStructs::libjvm();
|
||||
const void* ne;
|
||||
const void* oe;
|
||||
|
||||
if ((ne = libjvm->findSymbolByPrefix("_ZN11AllocTracer27send_allocation_in_new_tlab")) != NULL &&
|
||||
(oe = libjvm->findSymbolByPrefix("_ZN11AllocTracer28send_allocation_outside_tlab")) != NULL) {
|
||||
_trap_kind = 1; // JDK 10+
|
||||
} else if ((ne = libjvm->findSymbolByPrefix("_ZN11AllocTracer33send_allocation_in_new_tlab_eventE11KlassHandleP8HeapWord")) != NULL &&
|
||||
(oe = libjvm->findSymbolByPrefix("_ZN11AllocTracer34send_allocation_outside_tlab_eventE11KlassHandleP8HeapWord")) != NULL) {
|
||||
_trap_kind = 1; // JDK 8u262+
|
||||
} else if ((ne = libjvm->findSymbolByPrefix("_ZN11AllocTracer33send_allocation_in_new_tlab_event")) != NULL &&
|
||||
(oe = libjvm->findSymbolByPrefix("_ZN11AllocTracer34send_allocation_outside_tlab_event")) != NULL) {
|
||||
_trap_kind = 2; // JDK 7-9
|
||||
} else {
|
||||
return Error("No AllocTracer symbols found. Are JDK debug symbols installed?");
|
||||
}
|
||||
|
||||
_in_new_tlab.assign(ne);
|
||||
_outside_tlab.assign(oe);
|
||||
_in_new_tlab.pair(_outside_tlab);
|
||||
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error AllocTracer::start(Arguments& args) {
|
||||
Error error = check(args);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
Error error = initialize();
|
||||
if (error) return error;
|
||||
|
||||
_interval = args._alloc > 0 ? args._alloc : 0;
|
||||
_allocated_bytes = 0;
|
||||
|
||||
@@ -22,6 +22,7 @@ class AllocTracer : public Engine {
|
||||
static u64 _interval;
|
||||
static volatile u64 _allocated_bytes;
|
||||
|
||||
static Error initialize();
|
||||
static void recordAllocation(void* ucontext, EventType event_type, uintptr_t rklass,
|
||||
uintptr_t total_size, uintptr_t instance_size);
|
||||
|
||||
@@ -38,7 +39,6 @@ class AllocTracer : public Engine {
|
||||
return "bytes";
|
||||
}
|
||||
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
|
||||
|
||||
26
src/api/one/profiler/Agent.java
Normal file
26
src/api/one/profiler/Agent.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package one.profiler;
|
||||
|
||||
import javax.management.ObjectName;
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class Agent {
|
||||
|
||||
public static void premain(String args) throws Exception {
|
||||
agentmain(args);
|
||||
}
|
||||
|
||||
public static void agentmain(String args) throws Exception {
|
||||
AsyncProfiler profiler = AsyncProfiler.getInstance();
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(
|
||||
profiler,
|
||||
new ObjectName(AsyncProfilerMXBean.OBJECT_NAME));
|
||||
if (args != null && !args.isEmpty()) {
|
||||
profiler.execute(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,16 +39,22 @@ public class AsyncProfiler implements AsyncProfilerMXBean {
|
||||
// No need to load library, if it has been preloaded with -agentpath
|
||||
profiler.getVersion();
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
File file = extractEmbeddedLib();
|
||||
if (file != null) {
|
||||
try {
|
||||
System.load(file.getAbsolutePath());
|
||||
} finally {
|
||||
file.delete();
|
||||
}
|
||||
String libraryPath = System.getProperty("one.profiler.libraryPath");
|
||||
if (libraryPath != null && !libraryPath.isEmpty()) {
|
||||
System.load(new File(libraryPath).getAbsolutePath());
|
||||
} else {
|
||||
System.loadLibrary("asyncProfiler");
|
||||
File file = extractEmbeddedLib();
|
||||
if (file != null) {
|
||||
try {
|
||||
System.load(file.getAbsolutePath());
|
||||
} finally {
|
||||
file.delete();
|
||||
}
|
||||
} else {
|
||||
System.loadLibrary("asyncProfiler");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +177,7 @@ public class AsyncProfiler implements AsyncProfilerMXBean {
|
||||
|
||||
/**
|
||||
* Execute an agent-compatible profiling command -
|
||||
* the comma-separated list of arguments described in arguments.cpp
|
||||
* the comma-separated list of arguments defined in arguments.cpp
|
||||
*
|
||||
* @param command Profiling command
|
||||
* @return The command result
|
||||
|
||||
@@ -17,6 +17,8 @@ package one.profiler;
|
||||
* }</pre>
|
||||
*/
|
||||
public interface AsyncProfilerMXBean {
|
||||
String OBJECT_NAME = "one.profiler:type=AsyncProfiler";
|
||||
|
||||
void start(String event, long interval) throws IllegalStateException;
|
||||
void resume(String event, long interval) throws IllegalStateException;
|
||||
void stop() throws IllegalStateException;
|
||||
|
||||
2
src/api/one/profiler/MANIFEST.MF
Normal file
2
src/api/one/profiler/MANIFEST.MF
Normal file
@@ -0,0 +1,2 @@
|
||||
Agent-Class: one.profiler.Agent
|
||||
Premain-Class: one.profiler.Agent
|
||||
29
src/arch.h
29
src/arch.h
@@ -27,27 +27,26 @@ typedef unsigned short u16;
|
||||
typedef unsigned int u32;
|
||||
typedef unsigned long long u64;
|
||||
|
||||
static inline u64 atomicInc(volatile u64& var, u64 increment = 1) {
|
||||
return __sync_fetch_and_add(&var, increment);
|
||||
template<typename T>
|
||||
static inline T atomicInc(T& var, T increment = 1) {
|
||||
return __atomic_fetch_add(&var, increment, __ATOMIC_ACQ_REL);
|
||||
}
|
||||
|
||||
static inline int atomicInc(volatile u32& var, int increment = 1) {
|
||||
return __sync_fetch_and_add(&var, increment);
|
||||
template<typename T>
|
||||
static inline T atomicDec(T& var, T decrement = 1) {
|
||||
return __atomic_fetch_sub(&var, decrement, __ATOMIC_ACQ_REL);
|
||||
}
|
||||
|
||||
static inline int atomicInc(volatile int& var, int increment = 1) {
|
||||
return __sync_fetch_and_add(&var, increment);
|
||||
}
|
||||
|
||||
static inline u64 loadAcquire(u64& var) {
|
||||
template<typename T>
|
||||
static inline T loadAcquire(T& var) {
|
||||
return __atomic_load_n(&var, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
|
||||
static inline void storeRelease(u64& var, u64 value) {
|
||||
return __atomic_store_n(&var, value, __ATOMIC_RELEASE);
|
||||
template<typename T, typename U>
|
||||
static inline void storeRelease(T& var, U value) {
|
||||
__atomic_store_n(&var, static_cast<T>(value), __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
|
||||
#if defined(__x86_64__) || defined(__i386__)
|
||||
|
||||
typedef unsigned char instruction_t;
|
||||
@@ -56,7 +55,6 @@ const int BREAKPOINT_OFFSET = 0;
|
||||
|
||||
const int SYSCALL_SIZE = 2;
|
||||
const int FRAME_PC_SLOT = 1;
|
||||
const int PROBE_SP_LIMIT = 4;
|
||||
const int PLT_HEADER_SIZE = 16;
|
||||
const int PLT_ENTRY_SIZE = 16;
|
||||
const int PERF_REG_PC = 8; // PERF_REG_X86_IP
|
||||
@@ -78,7 +76,6 @@ const int BREAKPOINT_OFFSET = 0;
|
||||
|
||||
const int SYSCALL_SIZE = sizeof(instruction_t);
|
||||
const int FRAME_PC_SLOT = 1;
|
||||
const int PROBE_SP_LIMIT = 0;
|
||||
const int PLT_HEADER_SIZE = 20;
|
||||
const int PLT_ENTRY_SIZE = 12;
|
||||
const int PERF_REG_PC = 15; // PERF_REG_ARM_PC
|
||||
@@ -99,7 +96,6 @@ const int BREAKPOINT_OFFSET = 0;
|
||||
|
||||
const int SYSCALL_SIZE = sizeof(instruction_t);
|
||||
const int FRAME_PC_SLOT = 1;
|
||||
const int PROBE_SP_LIMIT = 0;
|
||||
const int PLT_HEADER_SIZE = 32;
|
||||
const int PLT_ENTRY_SIZE = 16;
|
||||
const int PERF_REG_PC = 32; // PERF_REG_ARM64_PC
|
||||
@@ -122,7 +118,6 @@ const int BREAKPOINT_OFFSET = 8;
|
||||
|
||||
const int SYSCALL_SIZE = sizeof(instruction_t);
|
||||
const int FRAME_PC_SLOT = 2;
|
||||
const int PROBE_SP_LIMIT = 0;
|
||||
const int PLT_HEADER_SIZE = 24;
|
||||
const int PLT_ENTRY_SIZE = 24;
|
||||
const int PERF_REG_PC = 32; // PERF_REG_POWERPC_NIP
|
||||
@@ -147,7 +142,6 @@ const int BREAKPOINT_OFFSET = 0;
|
||||
|
||||
const int SYSCALL_SIZE = sizeof(instruction_t);
|
||||
const int FRAME_PC_SLOT = 1; // return address is at -1 from FP
|
||||
const int PROBE_SP_LIMIT = 0;
|
||||
const int PLT_HEADER_SIZE = 24; // Best guess from examining readelf
|
||||
const int PLT_ENTRY_SIZE = 24; // ...same...
|
||||
const int PERF_REG_PC = 0; // PERF_REG_RISCV_PC
|
||||
@@ -168,7 +162,6 @@ const int BREAKPOINT_OFFSET = 0;
|
||||
|
||||
const int SYSCALL_SIZE = sizeof(instruction_t);
|
||||
const int FRAME_PC_SLOT = 1;
|
||||
const int PROBE_SP_LIMIT = 0;
|
||||
const int PLT_HEADER_SIZE = 32;
|
||||
const int PLT_ENTRY_SIZE = 16;
|
||||
const int PERF_REG_PC = 0; // PERF_REG_LOONGARCH_PC
|
||||
|
||||
@@ -37,84 +37,7 @@ const size_t EXTRA_BUF_SIZE = 512;
|
||||
|
||||
|
||||
// Parses agent arguments.
|
||||
// The format of the string is:
|
||||
// arg[,arg...]
|
||||
// where arg is one of the following options:
|
||||
// start - start profiling
|
||||
// resume - start or resume profiling without resetting collected data
|
||||
// stop - stop profiling
|
||||
// dump - dump collected data without stopping profiling session
|
||||
// check - check if the specified profiling event is available
|
||||
// status - print profiling status (inactive / running for X seconds)
|
||||
// meminfo - print profiler memory stats
|
||||
// list - show the list of available profiling events
|
||||
// version - display the agent version
|
||||
// event=EVENT - which event to trace (cpu, wall, cache-misses, etc.)
|
||||
// alloc[=BYTES] - profile allocations with BYTES interval
|
||||
// live - build allocation profile from live objects only
|
||||
// nativemem[=BYTES] - profile native allocations with BYTES interval
|
||||
// nofree - do not collect free calls in native allocation profiling
|
||||
// trace=METHOD[:DURATION] - method to be traced with optional latency threshold
|
||||
// lock[=DURATION] - profile contended locks overflowing the DURATION ns bucket (default: 10us)
|
||||
// wall[=NS] - run wall clock profiling together with CPU profiling
|
||||
// nobatch - legacy wall clock sampling without batch events
|
||||
// proc[=S] - collect process stats (default: 30s)
|
||||
// collapsed - dump collapsed stacks (the format used by FlameGraph script)
|
||||
// flamegraph - produce Flame Graph in HTML format
|
||||
// tree - produce call tree in HTML format
|
||||
// jfr - dump events in Java Flight Recorder format
|
||||
// jfropts=OPTIONS - JFR recording options: numeric bitmask or 'mem'
|
||||
// jfrsync[=CONFIG] - start Java Flight Recording with the given config along with the profiler
|
||||
// traces[=N] - dump top N call traces
|
||||
// flat[=N] - dump top N methods (aka flat profile)
|
||||
// otlp - dump in OpenTelemetry format
|
||||
// samples - count the number of samples (default)
|
||||
// total - count the total value (time, bytes, etc.) instead of samples
|
||||
// chunksize=N - approximate size of JFR chunk in bytes (default: 100 MB)
|
||||
// chunktime=N - duration of JFR chunk in seconds (default: 1 hour)
|
||||
// timeout=TIME - automatically stop profiler at TIME (absolute or relative)
|
||||
// loop=TIME - run profiler in a loop (continuous profiling)
|
||||
// interval=N - sampling interval in ns (default: 10'000'000, i.e. 10 ms)
|
||||
// jstackdepth=N - maximum Java stack depth (default: 2048)
|
||||
// signal=N - use alternative signal for cpu or wall clock profiling
|
||||
// features=LIST - advanced stack trace features (mixed, vtable, comptask, pcaddr)"
|
||||
// safemode=BITS - disable stack recovery techniques (default: 0, i.e. everything enabled)
|
||||
// file=FILENAME - output file name for dumping
|
||||
// log=FILENAME - log warnings and errors to the given dedicated stream
|
||||
// loglevel=LEVEL - logging level: TRACE, DEBUG, INFO, WARN, ERROR, or NONE
|
||||
// quiet - do not log "Profiling started/stopped" message
|
||||
// server=ADDRESS - start insecure HTTP server at ADDRESS/PORT
|
||||
// filter=FILTER - thread filter
|
||||
// threads - profile different threads separately
|
||||
// sched - group threads by scheduling policy
|
||||
// cstack=MODE - how to collect C stack frames in addition to Java stack
|
||||
// MODE is 'fp', 'dwarf', 'lbr', 'vm' or 'no'
|
||||
// clock=SOURCE - clock source for JFR timestamps: 'tsc' or 'monotonic'
|
||||
// alluser - include only user-mode events
|
||||
// fdtransfer - use fdtransfer to pass fds to the profiler
|
||||
// target-cpu=CPU - sample threads on a specific CPU (perf_events only, default: -1)
|
||||
// record-cpu - record which cpu a sample was taken on
|
||||
// simple - simple class names instead of FQN
|
||||
// dot - dotted class names
|
||||
// norm - normalize names of hidden classes / lambdas
|
||||
// sig - print method signatures
|
||||
// ann - annotate Java methods
|
||||
// lib - prepend library names
|
||||
// mcache - max age of jmethodID cache (default: 0 = disabled)
|
||||
// include=PATTERN - include stack traces containing PATTERN
|
||||
// exclude=PATTERN - exclude stack traces containing PATTERN
|
||||
// begin=FUNCTION - begin profiling when FUNCTION is executed
|
||||
// end=FUNCTION - end profiling when FUNCTION is executed
|
||||
// nostop - do not stop profiling outside --begin/--end window
|
||||
// ttsp - only time-to-safepoint profiling
|
||||
// title=TITLE - FlameGraph title
|
||||
// minwidth=PCT - FlameGraph minimum frame width in percent
|
||||
// reverse - generate stack-reversed FlameGraph / Call tree (defaults to icicle graph)
|
||||
// inverted - toggles the layout for reversed stacktraces from icicle to flamegraph
|
||||
// and for default stacktraces from flamegraph to icicle
|
||||
//
|
||||
// It is possible to specify multiple dump options at the same time
|
||||
|
||||
// The format of the string is: arg[,arg...]
|
||||
Error Arguments::parse(const char* args) {
|
||||
if (args == NULL) {
|
||||
return Error::OK;
|
||||
@@ -148,14 +71,11 @@ Error Arguments::parse(const char* args) {
|
||||
CASE("dump")
|
||||
_action = ACTION_DUMP;
|
||||
|
||||
CASE("check")
|
||||
_action = ACTION_CHECK;
|
||||
|
||||
CASE("status")
|
||||
_action = ACTION_STATUS;
|
||||
|
||||
CASE("meminfo")
|
||||
_action = ACTION_MEMINFO;
|
||||
CASE("metrics")
|
||||
_action = ACTION_METRICS;
|
||||
|
||||
CASE("list")
|
||||
_action = ACTION_LIST;
|
||||
@@ -228,6 +148,8 @@ Error Arguments::parse(const char* args) {
|
||||
if (_nativemem < 0) _nativemem = 0;
|
||||
} else if (strcmp(value, EVENT_LOCK) == 0) {
|
||||
if (_lock < 0) _lock = DEFAULT_LOCK_INTERVAL;
|
||||
} else if (strcmp(value, EVENT_NATIVELOCK) == 0) {
|
||||
if (_nativelock < 0) _nativelock = DEFAULT_LOCK_INTERVAL;
|
||||
} else if (_event != NULL && !_all) {
|
||||
msg = "Duplicate event argument";
|
||||
} else {
|
||||
@@ -240,14 +162,19 @@ Error Arguments::parse(const char* args) {
|
||||
}
|
||||
|
||||
CASE("loop")
|
||||
_loop = true;
|
||||
if (value == NULL || (_timeout = parseTimeout(value)) == -1) {
|
||||
if (value == NULL || (_loop = parseTimeout(value)) == -1) {
|
||||
msg = "Invalid loop duration";
|
||||
}
|
||||
|
||||
CASE("memlimit")
|
||||
_mem_limit = value == NULL ? 0 : parseUnits(value, BYTES);
|
||||
|
||||
CASE("alloc")
|
||||
_alloc = value == NULL ? 0 : parseUnits(value, BYTES);
|
||||
|
||||
CASE("tlab")
|
||||
_tlab = true;
|
||||
|
||||
CASE("nativemem")
|
||||
_nativemem = value == NULL ? 0 : parseUnits(value, BYTES);
|
||||
|
||||
@@ -260,6 +187,9 @@ Error Arguments::parse(const char* args) {
|
||||
CASE("lock")
|
||||
_lock = value == NULL ? DEFAULT_LOCK_INTERVAL : parseUnits(value, NANOS);
|
||||
|
||||
CASE("nativelock")
|
||||
_nativelock = value == NULL ? DEFAULT_LOCK_INTERVAL : parseUnits(value, NANOS);
|
||||
|
||||
CASE("wall")
|
||||
_wall = value == NULL ? 0 : parseUnits(value, NANOS);
|
||||
|
||||
@@ -285,6 +215,9 @@ Error Arguments::parse(const char* args) {
|
||||
if (_lock < 0) {
|
||||
_lock = DEFAULT_LOCK_INTERVAL;
|
||||
}
|
||||
if (_nativelock < 0) {
|
||||
_nativelock = DEFAULT_LOCK_INTERVAL;
|
||||
}
|
||||
if (_nativemem < 0) {
|
||||
_nativemem = DEFAULT_ALLOC_INTERVAL;
|
||||
}
|
||||
@@ -305,6 +238,9 @@ Error Arguments::parse(const char* args) {
|
||||
CASE("jstackdepth")
|
||||
if (value == NULL || (_jstackdepth = atoi(value)) <= 0) {
|
||||
msg = "jstackdepth must be > 0";
|
||||
} else {
|
||||
char* slash = strchr(value, '/');
|
||||
_truncated_stack_depth = slash != NULL ? atoi(slash + 1) : _jstackdepth;
|
||||
}
|
||||
|
||||
CASE("signal")
|
||||
@@ -319,24 +255,13 @@ Error Arguments::parse(const char* args) {
|
||||
if (value != NULL) {
|
||||
if (strstr(value, "stats")) _features.stats = 1;
|
||||
if (strstr(value, "jnienv")) _features.jnienv = 1;
|
||||
if (strstr(value, "probesp")) _features.probe_sp = 1;
|
||||
if (strstr(value, "agct")) _features.agct = 1;
|
||||
if (strstr(value, "mixed")) _features.mixed = 1;
|
||||
if (strstr(value, "vtable")) _features.vtable_target = 1;
|
||||
if (strstr(value, "comptask")) _features.comp_task = 1;
|
||||
if (strstr(value, "pcaddr")) _features.pc_addr = 1;
|
||||
}
|
||||
|
||||
CASE("safemode") {
|
||||
// Left for compatibility purpose; will be eventually migrated to 'features'
|
||||
int bits = value == NULL ? INT_MAX : (int)strtol(value, NULL, 0);
|
||||
_features.unknown_java = (bits & 1) ? 0 : 1;
|
||||
_features.unwind_stub = (bits & 2) ? 0 : 1;
|
||||
_features.unwind_comp = (bits & 4) ? 0 : 1;
|
||||
_features.unwind_native = (bits & 8) ? 0 : 1;
|
||||
_features.java_anchor = (bits & 16) ? 0 : 1;
|
||||
_features.gc_traces = (bits & 32) ? 0 : 1;
|
||||
}
|
||||
|
||||
CASE("file")
|
||||
if (value == NULL || value[0] == 0) {
|
||||
msg = "file must not be empty";
|
||||
@@ -402,8 +327,6 @@ Error Arguments::parse(const char* args) {
|
||||
_cstack = CSTACK_FP;
|
||||
} else if (strcmp(value, "dwarf") == 0) {
|
||||
_cstack = CSTACK_DWARF;
|
||||
} else if (strcmp(value, "lbr") == 0) {
|
||||
_cstack = CSTACK_LBR;
|
||||
} else if (strcmp(value, "vm") == 0) {
|
||||
_cstack = CSTACK_VM;
|
||||
} else if (strcmp(value, "vmx") == 0) {
|
||||
@@ -490,7 +413,7 @@ Error Arguments::parse(const char* args) {
|
||||
return Error(msg);
|
||||
}
|
||||
|
||||
if (_event == NULL && _alloc < 0 && _lock < 0 && _wall < 0 && _nativemem < 0 && _trace.empty()) {
|
||||
if (_event == NULL && _alloc < 0 && _lock < 0 && _wall < 0 && _nativemem < 0 && _nativelock < 0 && _trace.empty()) {
|
||||
_event = EVENT_CPU;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ const char* const EVENT_CPU = "cpu";
|
||||
const char* const EVENT_ALLOC = "alloc";
|
||||
const char* const EVENT_NATIVEMEM = "nativemem";
|
||||
const char* const EVENT_LOCK = "lock";
|
||||
const char* const EVENT_NATIVELOCK = "nativelock";
|
||||
const char* const EVENT_WALL = "wall";
|
||||
const char* const EVENT_CTIMER = "ctimer";
|
||||
const char* const EVENT_ITIMER = "itimer";
|
||||
@@ -32,9 +33,8 @@ enum SHORT_ENUM Action {
|
||||
ACTION_RESUME,
|
||||
ACTION_STOP,
|
||||
ACTION_DUMP,
|
||||
ACTION_CHECK,
|
||||
ACTION_STATUS,
|
||||
ACTION_MEMINFO,
|
||||
ACTION_METRICS,
|
||||
ACTION_LIST,
|
||||
ACTION_VERSION
|
||||
};
|
||||
@@ -60,7 +60,6 @@ enum SHORT_ENUM CStack {
|
||||
CSTACK_NO, // do not collect native frames
|
||||
CSTACK_FP, // walk stack using Frame Pointer links
|
||||
CSTACK_DWARF, // use DWARF unwinding info from .eh_frame section
|
||||
CSTACK_LBR, // Last Branch Record hardware capability
|
||||
CSTACK_VM // unwind using HotSpot VMStructs
|
||||
};
|
||||
|
||||
@@ -100,30 +99,20 @@ enum EventMask {
|
||||
EM_LOCK = 4,
|
||||
EM_WALL = 8,
|
||||
EM_NATIVEMEM = 16,
|
||||
EM_METHOD_TRACE = 32
|
||||
EM_NATIVELOCK = 32,
|
||||
EM_METHOD_TRACE = 64
|
||||
};
|
||||
constexpr int EVENT_MASK_SIZE = 6;
|
||||
constexpr int EVENT_MASK_SIZE = 7;
|
||||
|
||||
struct StackWalkFeatures {
|
||||
// Deprecated stack recovery techniques used to workaround AsyncGetCallTrace flaws
|
||||
unsigned short unknown_java : 1;
|
||||
unsigned short unwind_stub : 1;
|
||||
unsigned short unwind_comp : 1;
|
||||
unsigned short unwind_native : 1;
|
||||
unsigned short java_anchor : 1;
|
||||
unsigned short gc_traces : 1;
|
||||
|
||||
// Common features
|
||||
unsigned short stats : 1; // collect stack walking duration statistics
|
||||
|
||||
// Additional HotSpot-specific features
|
||||
unsigned short jnienv : 1; // verify JNIEnv* obtained using VMStructs
|
||||
unsigned short probe_sp : 1; // when AsyncGetCallTrace fails, adjust SP and retry
|
||||
unsigned short agct : 1; // force usage of AsyncGetCallTrace instead of VMStructs
|
||||
unsigned short mixed : 1; // mixed stack traces with Java and native frames interleaved
|
||||
unsigned short vtable_target : 1; // show receiver classes of vtable/itable stubs
|
||||
unsigned short comp_task : 1; // display current compilation task for JIT threads
|
||||
unsigned short pc_addr : 1; // record exact PC address for each sample
|
||||
unsigned short _padding : 3; // pad structure to 16 bits
|
||||
unsigned short _padding : 9; // pad structure to 16 bits
|
||||
};
|
||||
|
||||
|
||||
@@ -174,14 +163,18 @@ class Arguments {
|
||||
const char* _event;
|
||||
std::vector<const char*> _trace;
|
||||
int _timeout;
|
||||
int _loop;
|
||||
size_t _mem_limit;
|
||||
long _interval;
|
||||
long _alloc;
|
||||
long _nativemem;
|
||||
long _lock;
|
||||
long _nativelock;
|
||||
long _wall;
|
||||
long _proc;
|
||||
bool _all;
|
||||
int _jstackdepth;
|
||||
int _truncated_stack_depth;
|
||||
int _signal;
|
||||
const char* _file;
|
||||
const char* _log;
|
||||
@@ -192,12 +185,12 @@ class Arguments {
|
||||
std::vector<const char*> _include;
|
||||
std::vector<const char*> _exclude;
|
||||
unsigned char _mcache;
|
||||
bool _loop;
|
||||
bool _preloaded;
|
||||
bool _quiet;
|
||||
bool _threads;
|
||||
bool _sched;
|
||||
bool _record_cpu;
|
||||
bool _tlab;
|
||||
bool _live;
|
||||
bool _nofree;
|
||||
bool _nobatch;
|
||||
@@ -234,14 +227,18 @@ class Arguments {
|
||||
_event(NULL),
|
||||
_trace(),
|
||||
_timeout(0),
|
||||
_loop(0),
|
||||
_mem_limit(0),
|
||||
_interval(0),
|
||||
_alloc(-1),
|
||||
_nativemem(-1),
|
||||
_lock(-1),
|
||||
_nativelock(-1),
|
||||
_wall(-1),
|
||||
_proc(-1),
|
||||
_all(false),
|
||||
_jstackdepth(DEFAULT_JSTACKDEPTH),
|
||||
_truncated_stack_depth(DEFAULT_JSTACKDEPTH),
|
||||
_signal(0),
|
||||
_file(NULL),
|
||||
_log(NULL),
|
||||
@@ -252,12 +249,12 @@ class Arguments {
|
||||
_include(),
|
||||
_exclude(),
|
||||
_mcache(0),
|
||||
_loop(false),
|
||||
_preloaded(false),
|
||||
_quiet(false),
|
||||
_threads(false),
|
||||
_sched(false),
|
||||
_record_cpu(false),
|
||||
_tlab(false),
|
||||
_live(false),
|
||||
_nofree(false),
|
||||
_nobatch(false),
|
||||
@@ -267,7 +264,7 @@ class Arguments {
|
||||
_fdtransfer_path(NULL),
|
||||
_target_cpu(-1),
|
||||
_style(0),
|
||||
_features{1, 1, 1, 1, 1, 1},
|
||||
_features{},
|
||||
_cstack(CSTACK_DEFAULT),
|
||||
_clock(CLK_DEFAULT),
|
||||
_output(OUTPUT_NONE),
|
||||
@@ -298,7 +295,7 @@ class Arguments {
|
||||
|
||||
bool hasOutputFile() const {
|
||||
return _file != NULL &&
|
||||
(_action == ACTION_STOP || _action == ACTION_DUMP ? _output != OUTPUT_JFR : _action >= ACTION_CHECK);
|
||||
(_action == ACTION_STOP || _action == ACTION_DUMP ? _output != OUTPUT_JFR : _action >= ACTION_STATUS);
|
||||
}
|
||||
|
||||
bool hasOption(JfrOption option) const {
|
||||
@@ -306,12 +303,13 @@ class Arguments {
|
||||
}
|
||||
|
||||
int eventMask() const {
|
||||
return (_event != NULL ? EM_CPU : 0) |
|
||||
(_alloc >= 0 ? EM_ALLOC : 0) |
|
||||
(_lock >= 0 ? EM_LOCK : 0) |
|
||||
(_wall >= 0 ? EM_WALL : 0) |
|
||||
(_nativemem >= 0 ? EM_NATIVEMEM : 0) |
|
||||
(!_trace.empty() ? EM_METHOD_TRACE : 0);
|
||||
return (_event != NULL ? EM_CPU : 0) |
|
||||
(_alloc >= 0 ? EM_ALLOC : 0) |
|
||||
(_lock >= 0 ? EM_LOCK : 0) |
|
||||
(_wall >= 0 ? EM_WALL : 0) |
|
||||
(_nativemem >= 0 ? EM_NATIVEMEM : 0) |
|
||||
(_nativelock >= 0 ? EM_NATIVELOCK : 0) |
|
||||
(!_trace.empty() ? EM_METHOD_TRACE : 0);
|
||||
}
|
||||
|
||||
static long parseUnits(const char* str, const Multiplier* multipliers);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "callTraceStorage.h"
|
||||
#include "os.h"
|
||||
@@ -12,6 +13,7 @@
|
||||
static const u32 INITIAL_CAPACITY = 65536;
|
||||
static const u32 CALL_TRACE_CHUNK = 8 * 1024 * 1024;
|
||||
static const u32 OVERFLOW_TRACE_ID = 0x7fffffff;
|
||||
static const size_t MEM_LIMIT_EXTRA = 0x10000; // reserve up to 64 KB for LongHashTable headers
|
||||
|
||||
|
||||
class LongHashTable {
|
||||
@@ -83,6 +85,8 @@ CallTrace CallTraceStorage::_overflow_trace = {1, {BCI_ERROR, LP64_ONLY(0 COMMA)
|
||||
|
||||
CallTraceStorage::CallTraceStorage() : _allocator(CALL_TRACE_CHUNK) {
|
||||
_current_table = LongHashTable::allocate(NULL, INITIAL_CAPACITY);
|
||||
_used_memory = _current_table->usedMemory();
|
||||
_mem_limit = SIZE_MAX;
|
||||
_overflow = 0;
|
||||
}
|
||||
|
||||
@@ -92,12 +96,14 @@ CallTraceStorage::~CallTraceStorage() {
|
||||
}
|
||||
}
|
||||
|
||||
void CallTraceStorage::clear() {
|
||||
void CallTraceStorage::clear(size_t mem_limit) {
|
||||
while (_current_table->prev() != NULL) {
|
||||
_current_table = _current_table->destroy();
|
||||
}
|
||||
_current_table->clear();
|
||||
_used_memory = _current_table->usedMemory();
|
||||
_allocator.clear();
|
||||
_mem_limit = mem_limit ? mem_limit | MEM_LIMIT_EXTRA : SIZE_MAX;
|
||||
_overflow = 0;
|
||||
}
|
||||
|
||||
@@ -108,11 +114,7 @@ u32 CallTraceStorage::capacity() {
|
||||
}
|
||||
|
||||
size_t CallTraceStorage::usedMemory() {
|
||||
size_t bytes = _allocator.usedMemory();
|
||||
for (LongHashTable* table = _current_table; table != NULL; table = table->prev()) {
|
||||
bytes += table->usedMemory();
|
||||
}
|
||||
return bytes;
|
||||
return _used_memory + _allocator.usedMemory();
|
||||
}
|
||||
|
||||
void CallTraceStorage::collectTraces(std::map<u32, CallTrace*>& map) {
|
||||
@@ -241,15 +243,23 @@ u32 CallTraceStorage::put(int num_frames, ASGCT_CallFrame* frames, u64 counter)
|
||||
|
||||
while (keys[slot] != hash) {
|
||||
if (keys[slot] == 0) {
|
||||
if (usedMemory() > _mem_limit) {
|
||||
// Stop adding new stack traces once memory limit is exceeded
|
||||
atomicInc(_overflow);
|
||||
return OVERFLOW_TRACE_ID;
|
||||
}
|
||||
|
||||
if (!__sync_bool_compare_and_swap(&keys[slot], 0, hash)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Increment the table size, and if the load factor exceeds 0.75, reserve a new table
|
||||
// Increment the table size, and if the load factor exceeds 0.75, reserve a new table.
|
||||
// This condition can be hit only once per table, so the below allocation is race-free.
|
||||
if (table->incSize() == capacity * 3 / 4) {
|
||||
LongHashTable* new_table = LongHashTable::allocate(table, capacity * 2);
|
||||
if (new_table != NULL) {
|
||||
__sync_bool_compare_and_swap(&_current_table, table, new_table);
|
||||
atomicInc(_used_memory, new_table->usedMemory());
|
||||
storeRelease(_current_table, new_table);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,11 @@ struct CallTraceSample {
|
||||
u64 counter;
|
||||
|
||||
CallTrace* acquireTrace() {
|
||||
return __atomic_load_n(&trace, __ATOMIC_ACQUIRE);
|
||||
return loadAcquire(trace);
|
||||
}
|
||||
|
||||
void setTrace(CallTrace* value) {
|
||||
return __atomic_store_n(&trace, value, __ATOMIC_RELEASE);
|
||||
storeRelease(trace, value);
|
||||
}
|
||||
|
||||
CallTraceSample& operator+=(const CallTraceSample& s) {
|
||||
@@ -47,6 +47,8 @@ class CallTraceStorage {
|
||||
|
||||
LinearAllocator _allocator;
|
||||
LongHashTable* _current_table;
|
||||
size_t _used_memory;
|
||||
size_t _mem_limit;
|
||||
u64 _overflow;
|
||||
|
||||
u64 calcHash(int num_frames, ASGCT_CallFrame* frames);
|
||||
@@ -57,9 +59,10 @@ class CallTraceStorage {
|
||||
CallTraceStorage();
|
||||
~CallTraceStorage();
|
||||
|
||||
void clear();
|
||||
void clear(size_t mem_limit);
|
||||
u32 capacity();
|
||||
size_t usedMemory();
|
||||
u64 overflow() { return _overflow; }
|
||||
|
||||
void collectTraces(std::map<u32, CallTrace*>& map);
|
||||
void collectSamples(std::vector<CallTraceSample*>& samples);
|
||||
|
||||
@@ -219,6 +219,12 @@ void CodeCache::addImport(void** entry, const char* name) {
|
||||
saveImport(im_pthread_create, entry);
|
||||
} else if (strcmp(name, "pthread_exit") == 0) {
|
||||
saveImport(im_pthread_exit, entry);
|
||||
} else if (strcmp(name, "pthread_mutex_lock") == 0) {
|
||||
saveImport(im_pthread_mutex_lock, entry);
|
||||
} else if (strcmp(name, "pthread_rwlock_rdlock") == 0) {
|
||||
saveImport(im_pthread_rwlock_rdlock, entry);
|
||||
} else if (strcmp(name, "pthread_rwlock_wrlock") == 0) {
|
||||
saveImport(im_pthread_rwlock_wrlock, entry);
|
||||
} else if (strcmp(name, "pthread_setspecific") == 0) {
|
||||
saveImport(im_pthread_setspecific, entry);
|
||||
} else if (strcmp(name, "poll") == 0) {
|
||||
@@ -317,5 +323,5 @@ size_t CodeCache::usedMemory() {
|
||||
for (int i = 0; i < _count; i++) {
|
||||
bytes += NativeFunc::usedMemory(_blobs[i]._name);
|
||||
}
|
||||
return bytes;
|
||||
return bytes + sizeof(CodeCache);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#define _CODECACHE_H
|
||||
|
||||
#include <jvmti.h>
|
||||
#include "arch.h"
|
||||
|
||||
|
||||
#define NO_MIN_ADDRESS ((const void*)-1)
|
||||
@@ -20,6 +21,9 @@ enum ImportId {
|
||||
im_dlopen,
|
||||
im_pthread_create,
|
||||
im_pthread_exit,
|
||||
im_pthread_mutex_lock,
|
||||
im_pthread_rwlock_rdlock,
|
||||
im_pthread_rwlock_wrlock,
|
||||
im_pthread_setspecific,
|
||||
im_poll,
|
||||
im_malloc,
|
||||
@@ -216,6 +220,7 @@ class CodeCacheArray {
|
||||
private:
|
||||
CodeCache* _libs[MAX_NATIVE_LIBS];
|
||||
int _count;
|
||||
size_t _used_memory;
|
||||
|
||||
public:
|
||||
CodeCacheArray() : _count(0) {
|
||||
@@ -226,13 +231,18 @@ class CodeCacheArray {
|
||||
}
|
||||
|
||||
int count() {
|
||||
return __atomic_load_n(&_count, __ATOMIC_ACQUIRE);
|
||||
return loadAcquire(_count);
|
||||
}
|
||||
|
||||
size_t usedMemory() {
|
||||
return _used_memory;
|
||||
}
|
||||
|
||||
void add(CodeCache* lib) {
|
||||
int index = __atomic_load_n(&_count, __ATOMIC_ACQUIRE);
|
||||
int index = loadAcquire(_count);
|
||||
_libs[index] = lib;
|
||||
__atomic_store_n(&_count, index + 1, __ATOMIC_RELEASE);
|
||||
_used_memory += lib->usedMemory();
|
||||
storeRelease(_count, index + 1);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Arguments {
|
||||
public String title = "Flame Graph";
|
||||
public String title;
|
||||
public String highlight;
|
||||
public String output;
|
||||
public String state;
|
||||
@@ -24,11 +24,13 @@ public class Arguments {
|
||||
public boolean help;
|
||||
public boolean reverse;
|
||||
public boolean inverted;
|
||||
public boolean diff;
|
||||
public boolean cpu;
|
||||
public boolean cpuTime;
|
||||
public boolean wall;
|
||||
public boolean alloc;
|
||||
public boolean nativemem;
|
||||
public boolean nativelock;
|
||||
public boolean leak;
|
||||
public boolean live;
|
||||
public boolean lock;
|
||||
@@ -43,6 +45,7 @@ public class Arguments {
|
||||
public boolean dot;
|
||||
public long from;
|
||||
public long to;
|
||||
public long latency = -1;
|
||||
public final List<String> files = new ArrayList<>();
|
||||
|
||||
public Arguments(String... args) {
|
||||
|
||||
39
src/converter/one/convert/BidirectionalIndex.java
Normal file
39
src/converter/one/convert/BidirectionalIndex.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package one.convert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BidirectionalIndex<T> extends Index<T> {
|
||||
private final ArrayList<T> reverseIndex;
|
||||
|
||||
public BidirectionalIndex(Class<T> cls, T empty) {
|
||||
this(cls, empty, 256);
|
||||
}
|
||||
|
||||
public BidirectionalIndex(Class<T> cls, T empty, int initialCapacity) {
|
||||
super(cls, empty, initialCapacity);
|
||||
this.reverseIndex = new ArrayList<>(initialCapacity);
|
||||
this.reverseIndex.add(empty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int index(T key) {
|
||||
assert super.size() == reverseIndex.size();
|
||||
int idx = super.index(key);
|
||||
if (idx < reverseIndex.size()) {
|
||||
// Key already exists
|
||||
return idx;
|
||||
}
|
||||
assert idx == reverseIndex.size();
|
||||
reverseIndex.add(key);
|
||||
return idx;
|
||||
}
|
||||
|
||||
public T getKey(int idx) {
|
||||
return reverseIndex.get(idx);
|
||||
}
|
||||
}
|
||||
@@ -20,18 +20,24 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
private static final String[] FRAME_SUFFIX = {"_[0]", "_[j]", "_[i]", "", "", "_[k]", "_[1]"};
|
||||
private static final byte HAS_SUFFIX = (byte) 0x80;
|
||||
private static final int FLUSH_THRESHOLD = 15000;
|
||||
private static final long NEW_FRAME_DIFF = Long.MIN_VALUE;
|
||||
private static final Pattern TID_FRAME_PATTERN = Pattern.compile("\\[(.* )?tid=\\d+]");
|
||||
|
||||
private final Arguments args;
|
||||
private final Index<String> cpool = new Index<>(String.class, "");
|
||||
private final Frame root = new Frame(0, TYPE_NATIVE);
|
||||
private final StringBuilder outbuf = new StringBuilder(FLUSH_THRESHOLD + 1000);
|
||||
|
||||
private String title = "Flame Graph";
|
||||
private int[] order;
|
||||
private int[] cpoolMap;
|
||||
private int depth;
|
||||
private int lastLevel;
|
||||
private long lastX;
|
||||
private long lastTotal;
|
||||
private long lastDiff;
|
||||
private long mintotal;
|
||||
private long maxdiff = -1;
|
||||
|
||||
public FlameGraph(Arguments args) {
|
||||
this.args = args;
|
||||
@@ -71,7 +77,11 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
boolean needRebuild = args.reverse || args.include != null || args.exclude != null;
|
||||
|
||||
try (BufferedReader br = new BufferedReader(in)) {
|
||||
while (!br.readLine().startsWith("const cpool")) ;
|
||||
for (String line; !(line = br.readLine()).startsWith("const cpool"); ) {
|
||||
if (line.startsWith("<h1")) {
|
||||
title = line.substring(line.indexOf('>') + 1, line.lastIndexOf("</h1>"));
|
||||
}
|
||||
}
|
||||
br.readLine();
|
||||
|
||||
String s = "";
|
||||
@@ -84,6 +94,8 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
while (!br.readLine().isEmpty()) ;
|
||||
|
||||
for (String line; !(line = br.readLine()).isEmpty(); ) {
|
||||
if (line.startsWith("d=")) continue; // artifact of a differential flame graph
|
||||
|
||||
StringTokenizer st = new StringTokenizer(line.substring(2, line.length() - 1), ",");
|
||||
int nameAndType = Integer.parseInt(st.nextToken());
|
||||
|
||||
@@ -103,12 +115,10 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
|
||||
int titleIndex = nameAndType >>> 3;
|
||||
byte type = (byte) (nameAndType & 7);
|
||||
if (st.hasMoreTokens() && (type <= TYPE_INLINED || type >= TYPE_C1_COMPILED)) {
|
||||
type = TYPE_JIT_COMPILED;
|
||||
}
|
||||
byte normalizedType = type <= TYPE_INLINED || type >= TYPE_C1_COMPILED ? TYPE_JIT_COMPILED : type;
|
||||
|
||||
Frame f = level > 0 || needRebuild ? new Frame(titleIndex, type) : root;
|
||||
f.self = f.total = total;
|
||||
Frame f = level > 0 || needRebuild ? new Frame(titleIndex, normalizedType) : root;
|
||||
fillFrameCounters(f, type, total);
|
||||
if (st.hasMoreTokens()) f.inlined = Long.parseLong(st.nextToken());
|
||||
if (st.hasMoreTokens()) f.c1 = Long.parseLong(st.nextToken());
|
||||
if (st.hasMoreTokens()) f.interpreted = Long.parseLong(st.nextToken());
|
||||
@@ -171,6 +181,32 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
depth = Math.max(depth, stack.size);
|
||||
}
|
||||
|
||||
public void diff(FlameGraph base) {
|
||||
// Build a map that translates this cpool keys to the base flamegraph's cpool keys
|
||||
cpoolMap = Arrays.stream(cpool.keys()).mapToInt(title -> base.cpool.getOrDefault(title, -1)).toArray();
|
||||
diff(base.root, root);
|
||||
}
|
||||
|
||||
private void diff(Frame base, Frame current) {
|
||||
current.diff = base == null ? NEW_FRAME_DIFF : current.self - base.self;
|
||||
maxdiff = Math.max(maxdiff, Math.abs(current.diff));
|
||||
|
||||
for (Frame child : current.values()) {
|
||||
Frame baseChild = base == null ? null : base.get(translateKey(child.key));
|
||||
diff(baseChild, child);
|
||||
}
|
||||
}
|
||||
|
||||
private int translateKey(int key) {
|
||||
return cpoolMap[key & TITLE_MASK] | (key & ~TITLE_MASK);
|
||||
}
|
||||
|
||||
public void dump(OutputStream out) throws IOException {
|
||||
try (PrintStream ps = new PrintStream(out, false, "UTF-8")) {
|
||||
dump(ps);
|
||||
}
|
||||
}
|
||||
|
||||
public void dump(PrintStream out) {
|
||||
mintotal = (long) (root.total * args.minwidth / 100);
|
||||
|
||||
@@ -186,13 +222,16 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
out.print(Math.min(depth * 16, 32767));
|
||||
|
||||
tail = printTill(out, tail, "/*title:*/");
|
||||
out.print(args.title);
|
||||
out.print(args.title != null ? args.title : title);
|
||||
|
||||
// inverted toggles the layout for reversed stacktraces from icicle to flamegraph
|
||||
// and for default stacktraces from flamegraphs to icicle.
|
||||
tail = printTill(out, tail, "/*inverted:*/false");
|
||||
out.print(args.reverse ^ args.inverted);
|
||||
|
||||
tail = printTill(out, tail, "/*maxdiff:*/-1");
|
||||
out.print(maxdiff);
|
||||
|
||||
tail = printTill(out, tail, "/*depth:*/0");
|
||||
out.print(depth);
|
||||
|
||||
@@ -227,6 +266,15 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
}
|
||||
|
||||
private void printFrame(PrintStream out, Frame frame, int level, long x) {
|
||||
StringBuilder sb = outbuf;
|
||||
if (frame.diff != lastDiff) {
|
||||
if (frame.diff == NEW_FRAME_DIFF) {
|
||||
sb.append("d=U\n");
|
||||
} else {
|
||||
sb.append("d=").append(frame.diff).append('\n');
|
||||
}
|
||||
}
|
||||
|
||||
int nameAndType = order[frame.getTitleIndex()] << 3 | frame.getType();
|
||||
boolean hasExtraTypes = (frame.inlined | frame.c1 | frame.interpreted) != 0 &&
|
||||
frame.inlined < frame.total && frame.interpreted < frame.total;
|
||||
@@ -238,7 +286,7 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
func = 'n';
|
||||
}
|
||||
|
||||
StringBuilder sb = outbuf.append(func).append('(').append(nameAndType);
|
||||
sb.append(func).append('(').append(nameAndType);
|
||||
if (func == 'f') {
|
||||
sb.append(',').append(level).append(',').append(x - lastX);
|
||||
}
|
||||
@@ -258,6 +306,7 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
lastLevel = level;
|
||||
lastX = x;
|
||||
lastTotal = frame.total;
|
||||
lastDiff = frame.diff;
|
||||
|
||||
Frame[] children = frame.values().toArray(EMPTY_FRAME_ARRAY);
|
||||
Arrays.sort(children, this);
|
||||
@@ -279,6 +328,9 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
sb.append(strings[frame.getTitleIndex()]).append(FRAME_SUFFIX[frame.getType()]);
|
||||
if (frame.self > 0) {
|
||||
int tmpLength = sb.length();
|
||||
if (maxdiff >= 0) {
|
||||
sb.append(' ').append(frame.diff == NEW_FRAME_DIFF ? 0 : frame.self - frame.diff);
|
||||
}
|
||||
out.print(sb.append(' ').append(frame.self).append('\n'));
|
||||
sb.setLength(tmpLength);
|
||||
}
|
||||
@@ -316,6 +368,21 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
return include != null;
|
||||
}
|
||||
|
||||
private static void fillFrameCounters(Frame frame, byte type, long ticks) {
|
||||
frame.self = frame.total = ticks;
|
||||
switch (type) {
|
||||
case TYPE_INTERPRETED:
|
||||
frame.interpreted = ticks;
|
||||
break;
|
||||
case TYPE_INLINED:
|
||||
frame.inlined = ticks;
|
||||
break;
|
||||
case TYPE_C1_COMPILED:
|
||||
frame.c1 = ticks;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Frame addChild(Frame frame, String title, byte type, long ticks) {
|
||||
frame.total += ticks;
|
||||
|
||||
@@ -390,7 +457,7 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
return order[f1.getTitleIndex()] - order[f2.getTitleIndex()];
|
||||
}
|
||||
|
||||
public static void convert(String input, String output, Arguments args) throws IOException {
|
||||
public static FlameGraph parse(String input, Arguments args) throws IOException {
|
||||
FlameGraph fg = new FlameGraph(args);
|
||||
try (InputStreamReader in = new InputStreamReader(new FileInputStream(input), StandardCharsets.UTF_8)) {
|
||||
if (input.endsWith(".html")) {
|
||||
@@ -399,6 +466,11 @@ public class FlameGraph implements Comparator<Frame> {
|
||||
fg.parseCollapsed(in);
|
||||
}
|
||||
}
|
||||
return fg;
|
||||
}
|
||||
|
||||
public static void convert(String input, String output, Arguments args) throws IOException {
|
||||
FlameGraph fg = parse(input, args);
|
||||
try (PrintStream out = new PrintStream(output, "UTF-8")) {
|
||||
fg.dump(out);
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@ public class Frame extends HashMap<Integer, Frame> {
|
||||
public static final byte TYPE_KERNEL = 5;
|
||||
public static final byte TYPE_C1_COMPILED = 6;
|
||||
|
||||
private static final int TYPE_SHIFT = 28;
|
||||
static final int TYPE_SHIFT = 28;
|
||||
static final int TITLE_MASK = (1 << TYPE_SHIFT) - 1;
|
||||
|
||||
final int key;
|
||||
long total;
|
||||
long self;
|
||||
long diff;
|
||||
long inlined, c1, interpreted;
|
||||
|
||||
private Frame(int key) {
|
||||
@@ -36,7 +38,7 @@ public class Frame extends HashMap<Integer, Frame> {
|
||||
}
|
||||
|
||||
int getTitleIndex() {
|
||||
return key & ((1 << TYPE_SHIFT) - 1);
|
||||
return key & TITLE_MASK;
|
||||
}
|
||||
|
||||
byte getType() {
|
||||
|
||||
@@ -5,16 +5,14 @@
|
||||
|
||||
package one.convert;
|
||||
|
||||
import one.jfr.ClassRef;
|
||||
import one.jfr.Dictionary;
|
||||
import one.jfr.JfrReader;
|
||||
import one.jfr.MethodRef;
|
||||
import one.jfr.*;
|
||||
import one.jfr.event.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.BitSet;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static one.convert.Frame.*;
|
||||
|
||||
@@ -33,14 +31,15 @@ public abstract class JfrConverter extends Classifier {
|
||||
}
|
||||
|
||||
public void convert() throws IOException {
|
||||
jfr.stopAtNewChunk = true;
|
||||
TimeIntervals timeIntervals = readLatencyTimeIntervals();
|
||||
|
||||
jfr.stopAtNewChunk = true;
|
||||
while (jfr.hasMoreChunks()) {
|
||||
// Reset method dictionary, since new chunk may have different IDs
|
||||
methodNames = new Dictionary<>();
|
||||
|
||||
collector.beforeChunk();
|
||||
collectEvents();
|
||||
collectEvents(timeIntervals);
|
||||
collector.afterChunk();
|
||||
|
||||
convertChunk();
|
||||
@@ -51,12 +50,39 @@ public abstract class JfrConverter extends Classifier {
|
||||
}
|
||||
}
|
||||
|
||||
protected final TimeIntervals readLatencyTimeIntervals() throws IOException {
|
||||
if (args.latency < 0) return null;
|
||||
|
||||
TimeIntervals.Builder intervalsBuilder = new TimeIntervals.Builder();
|
||||
boolean foundMethodTrace = false; // We'll throw an exception if none is found
|
||||
|
||||
jfr.stopAtNewChunk = true;
|
||||
while (jfr.hasMoreChunks()) {
|
||||
long minLatencyTicks = args.latency * jfr.ticksPerSec / 1000;
|
||||
MethodTrace event;
|
||||
while ((event = jfr.readEvent(MethodTrace.class)) != null) {
|
||||
foundMethodTrace = true;
|
||||
if (event.duration >= minLatencyTicks) {
|
||||
intervalsBuilder.add(jfr.eventTimeToNanos(event.time), jfr.eventTimeToNanos(event.time + event.duration));
|
||||
}
|
||||
}
|
||||
}
|
||||
jfr.rewind();
|
||||
|
||||
if (!foundMethodTrace) {
|
||||
throw new RuntimeException("No jdk.MethodTrace events found");
|
||||
}
|
||||
return intervalsBuilder.build();
|
||||
}
|
||||
|
||||
protected EventCollector createCollector(Arguments args) {
|
||||
return new EventAggregator(args.threads, args.grain);
|
||||
}
|
||||
|
||||
protected void collectEvents() throws IOException {
|
||||
protected void collectEvents(TimeIntervals timeIntervals) throws IOException {
|
||||
// args.nativemem ? MallocEvent.class should always be first for the leak detection feature
|
||||
Class<? extends Event> eventClass = args.nativemem ? MallocEvent.class
|
||||
: args.nativelock ? NativeLockEvent.class
|
||||
: args.live ? LiveObject.class
|
||||
: args.alloc ? AllocationSample.class
|
||||
: args.lock ? ContendedLock.class
|
||||
@@ -84,7 +110,9 @@ public abstract class JfrConverter extends Classifier {
|
||||
for (Event event; (event = jfr.readEvent(eventClass)) != null; ) {
|
||||
if (event.time >= startTicks && event.time <= endTicks) {
|
||||
if (threadStates == null || threadStates.get(((ExecutionSample) event).threadState)) {
|
||||
collector.collect(event);
|
||||
if (timeIntervals == null || timeIntervals.contains(jfr.eventTimeToNanos(event.time))) {
|
||||
collector.collect(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,6 +122,49 @@ public abstract class JfrConverter extends Classifier {
|
||||
// To be overridden in subclasses
|
||||
}
|
||||
|
||||
protected boolean excludeStack(int stackId, int threadId, long classId) {
|
||||
Pattern include = args.include;
|
||||
Pattern exclude = args.exclude;
|
||||
if (include == null && exclude == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (args.threads) {
|
||||
String threadName = getThreadName(threadId);
|
||||
if (exclude != null && exclude.matcher(threadName).matches()) {
|
||||
return true;
|
||||
}
|
||||
if (include != null && include.matcher(threadName).matches()) {
|
||||
if (exclude == null) return false;
|
||||
include = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (classId != 0) {
|
||||
String className = getClassName(classId);
|
||||
if (exclude != null && exclude.matcher(className).matches()) {
|
||||
return true;
|
||||
}
|
||||
if (include != null && include.matcher(className).matches()) {
|
||||
if (exclude == null) return false;
|
||||
include = null;
|
||||
}
|
||||
}
|
||||
|
||||
StackTrace stackTrace = jfr.stackTraces.get(stackId);
|
||||
for (int i = 0; i < stackTrace.methods.length; i++) {
|
||||
String name = getMethodName(stackTrace.methods[i], stackTrace.types[i]);
|
||||
if (exclude != null && exclude.matcher(name).matches()) {
|
||||
return true;
|
||||
}
|
||||
if (include != null && include.matcher(name).matches()) {
|
||||
if (exclude == null) return false;
|
||||
include = null;
|
||||
}
|
||||
}
|
||||
return include != null;
|
||||
}
|
||||
|
||||
protected int toThreadState(String name) {
|
||||
Map<Integer, String> threadStates = jfr.enums.get("jdk.types.ThreadState");
|
||||
if (threadStates != null) {
|
||||
@@ -281,11 +352,11 @@ public abstract class JfrConverter extends Classifier {
|
||||
}
|
||||
|
||||
public double counterFactor() {
|
||||
return args.lock ? 1e9 / jfr.ticksPerSec : 1.0;
|
||||
return (args.lock || args.nativelock) ? jfr.nanosPerTick : 1.0;
|
||||
}
|
||||
|
||||
// Select sum(samples) or sum(value) depending on the --total option.
|
||||
// For lock events, convert lock duration from ticks to nanoseconds.
|
||||
// For lock and nativelock events, convert lock duration from ticks to nanoseconds.
|
||||
protected abstract class AggregatedEventVisitor implements EventCollector.Visitor {
|
||||
private final double factor = !args.total ? 0.0 : counterFactor();
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import one.jfr.StackTrace;
|
||||
import one.jfr.event.AllocationSample;
|
||||
import one.jfr.event.Event;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
@@ -73,19 +72,21 @@ public class JfrToFlame extends JfrConverter {
|
||||
}
|
||||
|
||||
public void dump(OutputStream out) throws IOException {
|
||||
try (PrintStream ps = new PrintStream(out, false, "UTF-8")) {
|
||||
fg.dump(ps);
|
||||
fg.dump(out);
|
||||
}
|
||||
|
||||
public static FlameGraph parse(String input, Arguments args) throws IOException {
|
||||
try (JfrReader jfr = new JfrReader(input)) {
|
||||
JfrToFlame converter = new JfrToFlame(jfr, args);
|
||||
converter.convert();
|
||||
return converter.fg;
|
||||
}
|
||||
}
|
||||
|
||||
public static void convert(String input, String output, Arguments args) throws IOException {
|
||||
JfrToFlame converter;
|
||||
try (JfrReader jfr = new JfrReader(input)) {
|
||||
converter = new JfrToFlame(jfr, args);
|
||||
converter.convert();
|
||||
}
|
||||
try (FileOutputStream out = new FileOutputStream(output)) {
|
||||
converter.dump(out);
|
||||
FlameGraph fg = parse(input, args);
|
||||
try (PrintStream out = new PrintStream(output, "UTF-8")) {
|
||||
fg.dump(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,17 @@ public class JfrToHeatmap extends JfrConverter {
|
||||
@Override
|
||||
protected EventCollector createCollector(Arguments args) {
|
||||
return new EventCollector() {
|
||||
long wallInterval;
|
||||
|
||||
private long getWallInterval() {
|
||||
if (wallInterval == 0) {
|
||||
String wall = jfr.settings.get("wall");
|
||||
long interval = Long.parseLong(wall != null ? wall : jfr.settings.get("interval"));
|
||||
wallInterval = interval != 0 ? interval : 50_000_000;
|
||||
}
|
||||
return wallInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collect(Event event) {
|
||||
int classId = 0;
|
||||
@@ -42,10 +53,14 @@ public class JfrToHeatmap extends JfrConverter {
|
||||
type = TYPE_KERNEL;
|
||||
}
|
||||
|
||||
long msFromStart = (event.time - jfr.chunkStartTicks) * 1_000 / jfr.ticksPerSec;
|
||||
long timeMs = jfr.chunkStartNanos / 1_000_000 + msFromStart;
|
||||
|
||||
heatmap.addEvent(event.stackTraceId, event.tid, classId, type, timeMs);
|
||||
long timeNs = jfr.eventTimeToNanos(event.time);
|
||||
long samples = event.samples();
|
||||
while (true) {
|
||||
heatmap.addEvent(event.stackTraceId, event.tid, classId, type, timeNs / 1_000_000);
|
||||
if (--samples <= 0) break;
|
||||
// Only wall clock events can have samples > 1
|
||||
timeNs += getWallInterval();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,6 +77,7 @@ public class JfrToHeatmap extends JfrConverter {
|
||||
@Override
|
||||
public void afterChunk() {
|
||||
jfr.stackTraces.clear();
|
||||
wallInterval = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,10 +7,10 @@ package one.convert;
|
||||
|
||||
import static one.convert.OtlpConstants.*;
|
||||
|
||||
import one.jfr.Dictionary;
|
||||
import one.jfr.JfrReader;
|
||||
import one.jfr.StackTrace;
|
||||
import one.jfr.event.Event;
|
||||
import one.jfr.event.EventCollector;
|
||||
import one.jfr.event.*;
|
||||
import one.proto.Proto;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
@@ -18,7 +18,9 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.*;
|
||||
|
||||
/** Converts .jfr output to OpenTelemetry protocol. */
|
||||
/**
|
||||
* Converts .jfr output to OpenTelemetry protocol.
|
||||
*/
|
||||
public class JfrToOtlp extends JfrConverter {
|
||||
// Size in bytes to be allocated in the buffer to hold the varint containing the length of the message
|
||||
private static final int MSG_LARGE = 5;
|
||||
@@ -28,6 +30,13 @@ public class JfrToOtlp extends JfrConverter {
|
||||
private final Index<String> functionPool = new Index<>(String.class, "");
|
||||
private final Index<Line> linePool = new Index<>(Line.class, Line.EMPTY);
|
||||
private final Index<KeyValue> attributesPool = new Index<>(KeyValue.class, KeyValue.EMPTY);
|
||||
private final Index<IntArray> stacksPool = new Index<>(IntArray.class, IntArray.EMPTY);
|
||||
private final int threadNameIndex = stringPool.index(OTLP_THREAD_NAME);
|
||||
|
||||
private final Dictionary<AggregatedEvent> aggregatedEvents = new Dictionary<>();
|
||||
// Chunk-private cache to remember mappings from stacktrace ID to OTLP stack index
|
||||
private final Map<Integer, Integer> stacksIndexCache = new HashMap<>();
|
||||
private double chunkCounterFactor;
|
||||
|
||||
private final Proto proto = new Proto(1024);
|
||||
|
||||
@@ -39,6 +48,50 @@ public class JfrToOtlp extends JfrConverter {
|
||||
out.write(proto.buffer(), 0, proto.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected EventCollector createCollector(Arguments args) {
|
||||
return new EventCollector() {
|
||||
public void beforeChunk() {
|
||||
chunkCounterFactor = counterFactor();
|
||||
aggregatedEvents.clear();
|
||||
stacksIndexCache.clear();
|
||||
}
|
||||
|
||||
public void collect(Event e) {
|
||||
if (excludeStack(e.stackTraceId, e.tid, 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
long key = ((long) e.tid) << 32 | e.stackTraceId;
|
||||
AggregatedEvent ec = aggregatedEvents.get(key);
|
||||
if (ec == null) {
|
||||
ec = new AggregatedEvent();
|
||||
aggregatedEvents.put(key, ec);
|
||||
}
|
||||
|
||||
long recordedValue = !args.total ? e.samples() : chunkCounterFactor == 1.0 ? e.value() : (long) (e.value() * chunkCounterFactor);
|
||||
ec.recordEvent(getUnixTimestampNanos(e.time), recordedValue);
|
||||
}
|
||||
|
||||
private long getUnixTimestampNanos(long jfrTimestamp) {
|
||||
long nanosFromStart = (long) ((jfrTimestamp - jfr.chunkStartTicks) * jfr.nanosPerTick);
|
||||
return jfr.chunkStartNanos + nanosFromStart;
|
||||
}
|
||||
|
||||
public void afterChunk() {}
|
||||
|
||||
public boolean finish() {
|
||||
aggregatedEvents.clear();
|
||||
stacksIndexCache.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
public void forEach(Visitor visitor) {
|
||||
throw new UnsupportedOperationException("Not supported");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convert() throws IOException {
|
||||
long rpMark = proto.startField(PROFILES_DATA_resource_profiles, MSG_LARGE);
|
||||
@@ -54,36 +107,71 @@ public class JfrToOtlp extends JfrConverter {
|
||||
protected void convertChunk() {
|
||||
long pMark = proto.startField(SCOPE_PROFILES_profiles, MSG_LARGE);
|
||||
|
||||
writeSampleTypes();
|
||||
writeTimingInformation();
|
||||
long sttMark = proto.startField(PROFILE_sample_type, MSG_SMALL);
|
||||
proto.field(VALUE_TYPE_type_strindex, stringPool.index(getValueType()));
|
||||
proto.field(VALUE_TYPE_unit_strindex,
|
||||
stringPool.index(args.total ? getTotalUnits() : getSampleUnits()));
|
||||
proto.commitField(sttMark);
|
||||
|
||||
List<Integer> locationIndices = new ArrayList<>();
|
||||
collector.forEach(new OtlpEventToSampleVisitor(locationIndices));
|
||||
proto.fieldFixed64(PROFILE_time_unix_nano, jfr.chunkStartNanos);
|
||||
proto.field(PROFILE_duration_nanos, jfr.chunkDurationNanos());
|
||||
|
||||
long liMark = proto.startField(PROFILE_location_indices, MSG_LARGE);
|
||||
locationIndices.forEach(proto::writeInt);
|
||||
proto.commitField(liMark);
|
||||
aggregatedEvents.forEach((key, value) -> {
|
||||
int stackTraceId = (int) key;
|
||||
int tid = (int) (key >> 32);
|
||||
writeSample(stackTraceId, tid, value);
|
||||
});
|
||||
|
||||
proto.commitField(pMark);
|
||||
}
|
||||
|
||||
private void writeSampleTypes() {
|
||||
long stsMark = proto.startField(PROFILE_sample_type, MSG_SMALL);
|
||||
proto.field(VALUE_TYPE_type_strindex, stringPool.index(getValueType()));
|
||||
proto.field(VALUE_TYPE_unit_strindex, stringPool.index(getSampleUnits()));
|
||||
proto.field(VALUE_TYPE_aggregation_temporality, AGGREGATION_TEMPORARALITY_cumulative);
|
||||
proto.commitField(stsMark);
|
||||
|
||||
long sttMark = proto.startField(PROFILE_sample_type, MSG_SMALL);
|
||||
proto.field(VALUE_TYPE_type_strindex, stringPool.index(getValueType()));
|
||||
proto.field(VALUE_TYPE_unit_strindex, stringPool.index(getTotalUnits()));
|
||||
proto.field(VALUE_TYPE_aggregation_temporality, AGGREGATION_TEMPORARALITY_cumulative);
|
||||
proto.commitField(sttMark);
|
||||
private IntArray makeStack(int stackTraceId) {
|
||||
StackTrace st = jfr.stackTraces.get(stackTraceId);
|
||||
int[] stack = new int[st.methods.length];
|
||||
for (int i = 0; i < st.methods.length; ++i) {
|
||||
stack[i] = linePool.index(makeLine(st, i));
|
||||
}
|
||||
return new IntArray(stack);
|
||||
}
|
||||
|
||||
private void writeTimingInformation() {
|
||||
proto.field(PROFILE_time_nanos, jfr.chunkStartNanos);
|
||||
proto.field(PROFILE_duration_nanos, jfr.chunkDurationNanos());
|
||||
private Line makeLine(StackTrace stackTrace, int i) {
|
||||
String methodName = getMethodName(stackTrace.methods[i], stackTrace.types[i]);
|
||||
int lineNumber = stackTrace.locations[i] >>> 16;
|
||||
int functionIdx = functionPool.index(methodName);
|
||||
return new Line(functionIdx, lineNumber);
|
||||
}
|
||||
|
||||
private void writeSample(int stackTraceId, int tid, AggregatedEvent ae) {
|
||||
// 24 is the sum of:
|
||||
// 4 tags: 1 byte
|
||||
// 5 * 2: max size of thread name and stack idx
|
||||
// 5 * 2: max size of timestamps/values arrays
|
||||
int maxLengthBytes = varintSize(24 + ae.eventsCount * (8 /* fixed64 */ + 10 /* max varint */));
|
||||
long sMark = proto.startField(PROFILE_samples, maxLengthBytes);
|
||||
|
||||
proto.field(SAMPLE_stack_index, stacksIndexCache.computeIfAbsent(stackTraceId, key -> stacksPool.index(makeStack(key))));
|
||||
|
||||
String threadName = getThreadName(tid);
|
||||
KeyValue threadNameKv = new KeyValue(threadNameIndex, threadName);
|
||||
proto.field(SAMPLE_attribute_indices, attributesPool.index(threadNameKv));
|
||||
|
||||
long tMark = proto.startField(SAMPLE_timestamps_unix_nano, varintSize(8 * ae.eventsCount));
|
||||
for (int i = 0; i < ae.eventsCount; ++i) {
|
||||
proto.writeFixed64(ae.timestamps[i]);
|
||||
}
|
||||
proto.commitField(tMark);
|
||||
|
||||
long vMark = proto.startField(SAMPLE_values, varintSize(10 * ae.eventsCount));
|
||||
for (int i = 0; i < ae.eventsCount; ++i) {
|
||||
proto.writeLong(ae.values[i]);
|
||||
}
|
||||
proto.commitField(vMark);
|
||||
|
||||
proto.commitField(sMark);
|
||||
}
|
||||
|
||||
private static int varintSize(long value) {
|
||||
return (640 - Long.numberOfLeadingZeros(value | 1) * 9) / 64;
|
||||
}
|
||||
|
||||
private void writeProfileDictionary() {
|
||||
@@ -93,37 +181,43 @@ public class JfrToOtlp extends JfrConverter {
|
||||
long mMark = proto.startField(PROFILES_DICTIONARY_mapping_table, MSG_SMALL);
|
||||
proto.commitField(mMark);
|
||||
|
||||
// Write function table
|
||||
for (String name : functionPool.keys()) {
|
||||
long fMark = proto.startField(PROFILES_DICTIONARY_function_table, MSG_SMALL);
|
||||
proto.field(FUNCTION_name_strindex, stringPool.index(name));
|
||||
proto.commitField(fMark);
|
||||
}
|
||||
|
||||
// Write location table
|
||||
for (Line line : linePool.keys()) {
|
||||
long locMark = proto.startField(PROFILES_DICTIONARY_location_table, MSG_SMALL);
|
||||
proto.field(LOCATION_mapping_index, 0);
|
||||
|
||||
long lineMark = proto.startField(LOCATION_line, MSG_SMALL);
|
||||
proto.field(LINE_function_index, line.functionIdx);
|
||||
proto.field(LINE_line, line.lineNumber);
|
||||
proto.field(LINE_lines, line.lineNumber);
|
||||
proto.commitField(lineMark);
|
||||
|
||||
proto.commitField(locMark);
|
||||
}
|
||||
|
||||
// Write string table
|
||||
for (IntArray stack : stacksPool.keys()) {
|
||||
long stackMark = proto.startField(PROFILES_DICTIONARY_stack_table, MSG_LARGE);
|
||||
long locationIndicesMark = proto.startField(STACK_location_indices, MSG_LARGE);
|
||||
for (int locationIdx : stack.array) {
|
||||
proto.writeInt(locationIdx);
|
||||
}
|
||||
proto.commitField(locationIndicesMark);
|
||||
proto.commitField(stackMark);
|
||||
}
|
||||
|
||||
for (String s : stringPool.keys()) {
|
||||
proto.field(PROFILES_DICTIONARY_string_table, s);
|
||||
}
|
||||
|
||||
// Write attributes table
|
||||
for (KeyValue kv : attributesPool.keys()) {
|
||||
long aMark = proto.startField(PROFILES_DICTIONARY_attribute_table, MSG_LARGE);
|
||||
proto.field(KEY_VALUE_key, kv.key);
|
||||
proto.field(KEY_VALUE_AND_UNIT_key_strindex, kv.keyStrindex);
|
||||
|
||||
long vMark = proto.startField(KEY_VALUE_value, MSG_LARGE);
|
||||
long vMark = proto.startField(KEY_VALUE_AND_UNIT_value, MSG_LARGE);
|
||||
proto.field(ANY_VALUE_string_value, kv.value);
|
||||
proto.commitField(vMark);
|
||||
|
||||
@@ -144,83 +238,13 @@ public class JfrToOtlp extends JfrConverter {
|
||||
}
|
||||
}
|
||||
|
||||
private final class OtlpEventToSampleVisitor implements EventCollector.Visitor {
|
||||
private final List<Integer> locationIndices;
|
||||
private final double factor = counterFactor();
|
||||
private final double ticksPerNanosecond = jfr.ticksPerSec / 1_000_000_000.0;
|
||||
|
||||
// JFR constant pool stacktrace ID to Range
|
||||
private final Map<Integer, Range> idToRange = new HashMap<>();
|
||||
|
||||
// Next index to be used for a location into Profile.location_indices
|
||||
private int nextLocationIdx = 0;
|
||||
|
||||
public OtlpEventToSampleVisitor(List<Integer> locationIndices) {
|
||||
this.locationIndices = locationIndices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Event event, long samples, long value) {
|
||||
long nanosFromStart = (long) ((event.time - jfr.chunkStartTicks) / ticksPerNanosecond);
|
||||
long timeNanos = jfr.chunkStartNanos + nanosFromStart;
|
||||
|
||||
Range range = idToRange.computeIfAbsent(event.stackTraceId, this::computeLocationRange);
|
||||
|
||||
long sMark = proto.startField(PROFILE_sample, MSG_SMALL);
|
||||
proto.field(SAMPLE_locations_start_index, range.start);
|
||||
proto.field(SAMPLE_locations_length, range.length);
|
||||
proto.field(SAMPLE_timestamps_unix_nano, timeNanos);
|
||||
|
||||
KeyValue threadName = new KeyValue("thread.name", getThreadName(event.tid));
|
||||
proto.field(SAMPLE_attribute_indices, attributesPool.index(threadName));
|
||||
|
||||
long svMark = proto.startField(SAMPLE_value, MSG_SMALL);
|
||||
proto.writeLong(samples);
|
||||
proto.writeLong(factor == 1.0 ? value : (long) (value * factor));
|
||||
proto.commitField(svMark);
|
||||
|
||||
proto.commitField(sMark);
|
||||
}
|
||||
|
||||
// Range of values in Profile.location_indices
|
||||
private Range computeLocationRange(int stackTraceId) {
|
||||
StackTrace st = jfr.stackTraces.get(stackTraceId);
|
||||
if (st == null) {
|
||||
return new Range(0, 0);
|
||||
}
|
||||
for (int i = 0; i < st.methods.length; ++i) {
|
||||
locationIndices.add(linePool.index(makeLine(st, i)));
|
||||
}
|
||||
Range range = new Range(nextLocationIdx, st.methods.length);
|
||||
nextLocationIdx += st.methods.length;
|
||||
return range;
|
||||
}
|
||||
|
||||
private Line makeLine(StackTrace stackTrace, int i) {
|
||||
String methodName = getMethodName(stackTrace.methods[i], stackTrace.types[i]);
|
||||
int lineNumber = stackTrace.locations[i] >>> 16;
|
||||
int functionIdx = functionPool.index(methodName);
|
||||
return new Line(functionIdx, lineNumber);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Range {
|
||||
final int start;
|
||||
final int length;
|
||||
|
||||
public Range(int start, int length) {
|
||||
this.start = start;
|
||||
this.length = length;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Line {
|
||||
static final Line EMPTY = new Line(0, 0);
|
||||
|
||||
final int functionIdx;
|
||||
final int lineNumber;
|
||||
|
||||
private Line(int functionIdx, int lineNumber) {
|
||||
Line(int functionIdx, int lineNumber) {
|
||||
this.functionIdx = functionIdx;
|
||||
this.lineNumber = lineNumber;
|
||||
}
|
||||
@@ -242,14 +266,14 @@ public class JfrToOtlp extends JfrConverter {
|
||||
}
|
||||
|
||||
private static final class KeyValue {
|
||||
static final KeyValue EMPTY = new KeyValue("", "");
|
||||
static final KeyValue EMPTY = new KeyValue(0, "");
|
||||
|
||||
final String key;
|
||||
final int keyStrindex;
|
||||
// Only string value is fine for now
|
||||
final String value;
|
||||
|
||||
private KeyValue(String key, String value) {
|
||||
this.key = key;
|
||||
KeyValue(int keyStrindex, String value) {
|
||||
this.keyStrindex = keyStrindex;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@@ -258,14 +282,53 @@ public class JfrToOtlp extends JfrConverter {
|
||||
if (!(o instanceof KeyValue)) return false;
|
||||
|
||||
KeyValue other = (KeyValue) o;
|
||||
return key.equals(other.key) && value.equals(other.value);
|
||||
return keyStrindex == other.keyStrindex && value.equals(other.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result = 31 * result + key.hashCode();
|
||||
result = 31 * result + keyStrindex;
|
||||
return 31 * result + value.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class IntArray {
|
||||
static final IntArray EMPTY = new IntArray(new int[0]);
|
||||
|
||||
final int[] array;
|
||||
final int hash;
|
||||
|
||||
IntArray(int[] array) {
|
||||
this.array = array;
|
||||
this.hash = Arrays.hashCode(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof IntArray && Arrays.equals(array, ((IntArray) o).array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AggregatedEvent {
|
||||
long[] timestamps = new long[1];
|
||||
long[] values = new long[1];
|
||||
int eventsCount = 0;
|
||||
|
||||
public void recordEvent(long timestamp, long value) {
|
||||
if (eventsCount == timestamps.length) {
|
||||
int newSize = timestamps.length * 2;
|
||||
timestamps = Arrays.copyOf(timestamps, newSize);
|
||||
values = Arrays.copyOf(values, newSize);
|
||||
}
|
||||
timestamps[eventsCount] = timestamp;
|
||||
values[eventsCount] = value;
|
||||
++eventsCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,9 @@ public class JfrToPprof extends JfrConverter {
|
||||
|
||||
@Override
|
||||
public void visit(Event event, long value) {
|
||||
if (excludeStack(event.stackTraceId, event.tid, event.classId())) {
|
||||
return;
|
||||
}
|
||||
profile.field(2, sample(s, event, value));
|
||||
s.reset();
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import one.convert.*;
|
||||
package one.convert;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
@@ -18,7 +19,7 @@ public class Main {
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.files.size() == 1) {
|
||||
if (args.files.size() == (args.diff ? 2 : 1)) {
|
||||
args.files.add(".");
|
||||
}
|
||||
|
||||
@@ -35,6 +36,34 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
if (args.diff) {
|
||||
if (fileCount != 2) {
|
||||
throw new IllegalArgumentException("--diff option requires two input files");
|
||||
}
|
||||
if (!"html".equals(args.output) && !"collapsed".equals(args.output)) {
|
||||
throw new IllegalArgumentException("--diff option requires html or collapsed output format");
|
||||
}
|
||||
|
||||
args.norm = true; // don't let random IDs in class names spoil comparison
|
||||
|
||||
String input1 = args.files.get(0);
|
||||
String input2 = args.files.get(1);
|
||||
String output = isDirectory ? new File(lastFile, replaceExt(input2, "diff." + args.output)).getPath() : lastFile;
|
||||
|
||||
System.out.print("Converting " + getFileName(input2) + " vs " + getFileName(input1) + " -> " + getFileName(output) + " ");
|
||||
System.out.flush();
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
FlameGraph base = parseFlameGraph(input1, args);
|
||||
FlameGraph current = parseFlameGraph(input2, args);
|
||||
current.diff(base);
|
||||
current.dump(new FileOutputStream(output));
|
||||
long endTime = System.nanoTime();
|
||||
|
||||
System.out.print("# " + (endTime - startTime) / 1000000 / 1000.0 + " s\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < fileCount; i++) {
|
||||
String input = args.files.get(i);
|
||||
String output = isDirectory ? new File(lastFile, replaceExt(input, args.output)).getPath() : lastFile;
|
||||
@@ -68,6 +97,14 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
public static FlameGraph parseFlameGraph(String input, Arguments args) throws IOException {
|
||||
if (isJfr(input)) {
|
||||
return JfrToFlame.parse(input, args);
|
||||
} else {
|
||||
return FlameGraph.parse(input, args);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getFileName(String fileName) {
|
||||
return fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1);
|
||||
}
|
||||
@@ -96,6 +133,9 @@ public class Main {
|
||||
"\n" +
|
||||
"Conversion options:\n" +
|
||||
" -o --output FORMAT Output format: html, collapsed, pprof, pb.gz, heatmap, otlp\n" +
|
||||
" -I --include REGEX Include only stacks with the specified frames\n" +
|
||||
" -X --exclude REGEX Exclude stacks with the specified frames\n" +
|
||||
" --diff Create differential Flame Graph from two input files\n" +
|
||||
"\n" +
|
||||
"JFR options:\n" +
|
||||
" --cpu CPU profile (ExecutionSample)\n" +
|
||||
@@ -107,6 +147,8 @@ public class Main {
|
||||
" --leak Only include memory leaks in nativemem\n" +
|
||||
" --tail RATIO Ignore tail allocations for leak profiling (10% by default)\n" +
|
||||
" --lock Lock contention profile\n" +
|
||||
" --nativelock Native (pthread) lock contention profile\n" +
|
||||
" --trace Method traces / latency profile\n" +
|
||||
" -t --threads Split stack traces by threads\n" +
|
||||
" -s --state LIST Filter thread states: runnable, sleeping\n" +
|
||||
" --classify Classify samples into predefined categories\n" +
|
||||
@@ -118,6 +160,7 @@ public class Main {
|
||||
" --dot Dotted class names\n" +
|
||||
" --from TIME Start time in ms (absolute or relative)\n" +
|
||||
" --to TIME End time in ms (absolute or relative)\n" +
|
||||
" --latency MS Retain only samples within MethodTraces of at least MS milliseconds\n" +
|
||||
"\n" +
|
||||
"Flame Graph options:\n" +
|
||||
" --title STRING Flame Graph title\n" +
|
||||
@@ -127,8 +170,6 @@ public class Main {
|
||||
" -r --reverse Reverse stack traces (defaults to icicle graph)\n" +
|
||||
" -i --inverted Toggles the layout for reversed stacktraces from icicle to flamegraph\n" +
|
||||
" and for default stacktraces from flamegraph to icicle\n" +
|
||||
" -I --include REGEX Include only stacks with the specified frames\n" +
|
||||
" -X --exclude REGEX Exclude stacks with the specified frames\n" +
|
||||
" --highlight REGEX Highlight frames matching the given pattern\n");
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,15 @@ package one.convert;
|
||||
|
||||
final class OtlpConstants {
|
||||
|
||||
static final String OTLP_THREAD_NAME = "thread.name";
|
||||
|
||||
static final int
|
||||
PROFILES_DICTIONARY_mapping_table = 1,
|
||||
PROFILES_DICTIONARY_location_table = 2,
|
||||
PROFILES_DICTIONARY_function_table = 3,
|
||||
PROFILES_DICTIONARY_string_table = 5,
|
||||
PROFILES_DICTIONARY_attribute_table = 6;
|
||||
PROFILES_DICTIONARY_attribute_table = 6,
|
||||
PROFILES_DICTIONARY_stack_table = 7;
|
||||
|
||||
static final int
|
||||
PROFILES_DATA_resource_profiles = 1,
|
||||
@@ -24,10 +27,9 @@ final class OtlpConstants {
|
||||
|
||||
static final int
|
||||
PROFILE_sample_type = 1,
|
||||
PROFILE_sample = 2,
|
||||
PROFILE_location_indices = 3,
|
||||
PROFILE_time_nanos = 4,
|
||||
PROFILE_duration_nanos = 5;
|
||||
PROFILE_samples = 2,
|
||||
PROFILE_time_unix_nano = 3,
|
||||
PROFILE_duration_nanos = 4;
|
||||
|
||||
static final int
|
||||
VALUE_TYPE_type_strindex = 1,
|
||||
@@ -35,11 +37,13 @@ final class OtlpConstants {
|
||||
VALUE_TYPE_aggregation_temporality = 3;
|
||||
|
||||
static final int
|
||||
SAMPLE_locations_start_index = 1,
|
||||
SAMPLE_locations_length = 2,
|
||||
SAMPLE_value = 3,
|
||||
SAMPLE_attribute_indices = 4,
|
||||
SAMPLE_timestamps_unix_nano = 6;
|
||||
SAMPLE_stack_index = 1,
|
||||
SAMPLE_values = 2,
|
||||
SAMPLE_attribute_indices = 3,
|
||||
SAMPLE_timestamps_unix_nano = 5;
|
||||
|
||||
static final int
|
||||
STACK_location_indices = 1;
|
||||
|
||||
static final int
|
||||
LOCATION_mapping_index = 1,
|
||||
@@ -47,15 +51,13 @@ final class OtlpConstants {
|
||||
|
||||
static final int
|
||||
LINE_function_index = 1,
|
||||
LINE_line = 2;
|
||||
LINE_lines = 2;
|
||||
|
||||
static final int FUNCTION_name_strindex = 1;
|
||||
|
||||
static final int AGGREGATION_TEMPORARALITY_cumulative = 2;
|
||||
|
||||
static final int
|
||||
KEY_VALUE_key = 1,
|
||||
KEY_VALUE_value = 2;
|
||||
KEY_VALUE_AND_UNIT_key_strindex = 1,
|
||||
KEY_VALUE_AND_UNIT_value = 2;
|
||||
|
||||
static final int ANY_VALUE_string_value = 1;
|
||||
}
|
||||
|
||||
79
src/converter/one/convert/TimeIntervals.java
Normal file
79
src/converter/one/convert/TimeIntervals.java
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package one.convert;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.NavigableMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class TimeIntervals {
|
||||
private final long[] startIntervals;
|
||||
private final long[] endIntervals;
|
||||
|
||||
private TimeIntervals(long[] startIntervals, long[] endIntervals) {
|
||||
this.startIntervals = startIntervals;
|
||||
this.endIntervals = endIntervals;
|
||||
}
|
||||
|
||||
public boolean contains(long instant) {
|
||||
int searchOut = Arrays.binarySearch(startIntervals, instant);
|
||||
if (searchOut >= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int insertionPoint = -(searchOut + 1); // First element greater than instant
|
||||
if (insertionPoint == 0) {
|
||||
return false; // First interval start is greater than instant
|
||||
}
|
||||
int startIndex = insertionPoint - 1;
|
||||
return instant <= endIntervals[startIndex];
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
// No overlapping intervals
|
||||
private final TreeMap<Long, Long> timeIntervals = new TreeMap<>();
|
||||
|
||||
public void add(long startInstant, long endInstant) {
|
||||
if (startInstant > endInstant) {
|
||||
throw new IllegalArgumentException("'startInstant' should not be after 'endInstant'");
|
||||
}
|
||||
|
||||
// Are there shorter intervals which overlap with the new interval?
|
||||
NavigableMap<Long, Long> view = timeIntervals.subMap(startInstant, true /* inclusive */, endInstant, true /* inclusive */);
|
||||
Map.Entry<Long, Long> last = view.pollLastEntry();
|
||||
if (last != null) {
|
||||
endInstant = Long.max(last.getValue(), endInstant);
|
||||
}
|
||||
view.clear();
|
||||
|
||||
// Perhaps the end of the interval before 'view' ends after startInstant?
|
||||
Map.Entry<Long, Long> floor = timeIntervals.floorEntry(startInstant);
|
||||
if (floor != null) {
|
||||
long floorEnd = floor.getValue();
|
||||
if (floorEnd >= startInstant) {
|
||||
timeIntervals.remove(floor.getKey());
|
||||
startInstant = floor.getKey();
|
||||
endInstant = Long.max(endInstant, floorEnd);
|
||||
}
|
||||
}
|
||||
|
||||
timeIntervals.put(startInstant, endInstant);
|
||||
}
|
||||
|
||||
public TimeIntervals build() {
|
||||
long[] startIntervals = new long[timeIntervals.size()];
|
||||
long[] endIntervals = new long[timeIntervals.size()];
|
||||
int index = 0;
|
||||
for (Map.Entry<Long, Long> entry : timeIntervals.entrySet()) {
|
||||
startIntervals[index] = entry.getKey();
|
||||
endIntervals[index] = entry.getValue();
|
||||
++index;
|
||||
}
|
||||
return new TimeIntervals(startIntervals, endIntervals);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ package one.heatmap;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import one.convert.*;
|
||||
import one.jfr.DictionaryInt;
|
||||
@@ -37,12 +38,14 @@ public class Heatmap {
|
||||
|
||||
public void beforeChunk() {
|
||||
state.methodCache.clear();
|
||||
state.includeCache.clear();
|
||||
}
|
||||
|
||||
public void finish(long startMs) {
|
||||
this.startMs = startMs;
|
||||
state.methodCache.clear();
|
||||
state.stackTracesCache.clear();
|
||||
state.includeCache.clear();
|
||||
}
|
||||
|
||||
private EvaluationContext evaluate() {
|
||||
@@ -91,7 +94,7 @@ public class Heatmap {
|
||||
stream.print('E');
|
||||
|
||||
tail = ResourceProcessor.printTill(stream, tail, "/*title:*/");
|
||||
stream.print(args.title == null ? "Heatmap" : args.title);
|
||||
stream.print(args.title != null ? args.title : "Heatmap");
|
||||
|
||||
tail = ResourceProcessor.printTill(stream, tail, "/*startMs:*/0");
|
||||
stream.print(startMs);
|
||||
@@ -394,8 +397,11 @@ public class Heatmap {
|
||||
// Maps stack trace ID to prototype ID in stackTracesRemap
|
||||
final DictionaryInt stackTracesCache = new DictionaryInt();
|
||||
final Map<MethodKey, Integer> methodCache = new HashMap<>();
|
||||
final Index<Method> methods = new Index<>(Method.class, Method.EMPTY);
|
||||
final Index<String> symbolTable = new Index<>(String.class, "");
|
||||
final BidirectionalIndex<Method> methods = new BidirectionalIndex<>(Method.class, Method.EMPTY);
|
||||
final BidirectionalIndex<String> symbolTable = new BidirectionalIndex<>(String.class, "");
|
||||
|
||||
// Cache for exclude/include filter results per prototype ID
|
||||
final Map<Integer, Boolean> includeCache = new HashMap<>();
|
||||
|
||||
// reusable array to (temporary) store (potentially) new stack trace
|
||||
int[] cachedStackTrace = new int[4096];
|
||||
@@ -406,14 +412,52 @@ public class Heatmap {
|
||||
this.sampleList = new SampleList(blockDurationMs);
|
||||
}
|
||||
|
||||
private String resolveFrameName(Method method) {
|
||||
if (method.className == 0) {
|
||||
return symbolTable.getKey(method.methodName);
|
||||
}
|
||||
if (method.methodName == 0) {
|
||||
return symbolTable.getKey(method.className);
|
||||
}
|
||||
return symbolTable.getKey(method.className) + '.' + symbolTable.getKey(method.methodName);
|
||||
}
|
||||
|
||||
private boolean includeStack(int prototypeId) {
|
||||
if (args.include == null && args.exclude == null) {
|
||||
return true;
|
||||
}
|
||||
return includeCache.computeIfAbsent(prototypeId, stackId -> applyIncludeExcludeFilter(stackId));
|
||||
}
|
||||
|
||||
// Returns true if the stack should be included
|
||||
private boolean applyIncludeExcludeFilter(int stackId) {
|
||||
int[] stack = stackTracesRemap.get(stackId);
|
||||
Pattern include = args.include;
|
||||
Pattern exclude = args.exclude;
|
||||
for (int i = 0; i < stack.length; i++) {
|
||||
Method method = methods.getKey(stack[i]);
|
||||
String name = resolveFrameName(method);
|
||||
if (exclude != null && exclude.matcher(name).matches()) {
|
||||
return false;
|
||||
}
|
||||
if (include != null && include.matcher(name).matches()) {
|
||||
if (exclude == null) return true;
|
||||
include = null;
|
||||
}
|
||||
}
|
||||
return include == null;
|
||||
}
|
||||
|
||||
public void addEvent(int stackTraceId, int threadId, int classId, byte type, long timeMs) {
|
||||
if (sampleList.getRecordsCount() >= LIMIT) {
|
||||
if (sampleList.getRecordsCount() >= LIMIT || stackTraceId == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int prototypeId = stackTracesCache.get(stackTraceId);
|
||||
if (classId == 0 && !args.threads) {
|
||||
sampleList.add(prototypeId, timeMs);
|
||||
if (includeStack(prototypeId)) {
|
||||
sampleList.add(prototypeId, timeMs);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -435,7 +479,10 @@ public class Heatmap {
|
||||
cachedStackTrace[stackSize - 1] = getMethodIndex(key);
|
||||
}
|
||||
|
||||
sampleList.add(stackTracesRemap.index(cachedStackTrace, stackSize), timeMs);
|
||||
int newStackId = stackTracesRemap.index(cachedStackTrace, stackSize);
|
||||
if (includeStack(newStackId)) {
|
||||
sampleList.add(newStackId, timeMs);
|
||||
}
|
||||
}
|
||||
|
||||
public void addStack(long id, long[] methods, int[] locations, byte[] types, int size) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class Dictionary<T> {
|
||||
private static final int INITIAL_CAPACITY = 16;
|
||||
private static final long USED_BIT = 1L << 63;
|
||||
|
||||
private long[] keys;
|
||||
private Object[] values;
|
||||
@@ -37,12 +38,12 @@ public class Dictionary<T> {
|
||||
}
|
||||
|
||||
// key[i]==0 is used to signal that the i-th position is unset.
|
||||
// Thus, we map key=key+1, so the user can still use key=0.
|
||||
// Thus, we flip USED_BIT, so the user can still use key=0.
|
||||
private static long remapKey(long key) {
|
||||
if (key < 0) {
|
||||
throw new IllegalArgumentException("Negative keys not allowed");
|
||||
if (key == USED_BIT) {
|
||||
throw new IllegalArgumentException("Key not allowed");
|
||||
}
|
||||
return key + 1;
|
||||
return key ^ USED_BIT;
|
||||
}
|
||||
|
||||
public void put(long key, T value) {
|
||||
@@ -82,7 +83,7 @@ public class Dictionary<T> {
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != 0) {
|
||||
// Map key back, see remapKey
|
||||
visitor.visit(keys[i] - 1, (T) values[i]);
|
||||
visitor.visit(keys[i] ^ USED_BIT, (T) values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ public class JfrReader implements Closeable {
|
||||
public long chunkEndNanos;
|
||||
public long chunkStartTicks;
|
||||
public long ticksPerSec;
|
||||
public double nanosPerTick;
|
||||
public boolean stopAtNewChunk;
|
||||
|
||||
public final Dictionary<JfrClass> types = new Dictionary<>();
|
||||
@@ -78,6 +79,8 @@ public class JfrReader implements Closeable {
|
||||
private int malloc;
|
||||
private int free;
|
||||
private int cpuTimeSample;
|
||||
private int nativeLock;
|
||||
private boolean hasWallTimeSpan;
|
||||
|
||||
public JfrReader(String fileName) throws IOException {
|
||||
this.ch = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ);
|
||||
@@ -165,6 +168,10 @@ public class JfrReader implements Closeable {
|
||||
int size = getVarint();
|
||||
int type = getVarint();
|
||||
|
||||
if (size <= 0) {
|
||||
throw new IOException("Corrupted JFR recording: invalid event size");
|
||||
}
|
||||
|
||||
if (type == 'L' && buf.getInt(pos) == CHUNK_SIGNATURE) {
|
||||
if (state != STATE_NEW_CHUNK && stopAtNewChunk) {
|
||||
buf.position(pos);
|
||||
@@ -197,17 +204,21 @@ public class JfrReader implements Closeable {
|
||||
if (cls == null || cls == ContendedLock.class) return (E) readContendedLock(false);
|
||||
} else if (type == threadPark) {
|
||||
if (cls == null || cls == ContendedLock.class) return (E) readContendedLock(true);
|
||||
} else if (type == nativeLock) {
|
||||
if (cls == null || cls == NativeLockEvent.class) return (E) readNativeLockEvent();
|
||||
} else if (type == activeSetting) {
|
||||
readActiveSetting();
|
||||
} else {
|
||||
Constructor<? extends Event> customEvent = customEvents.get(type);
|
||||
if (customEvent != null && (cls == null || cls == customEvent.getDeclaringClass())) {
|
||||
long eventEnd = filePosition + pos + size;
|
||||
ensureBytes(size - (buf.position() - pos));
|
||||
try {
|
||||
return (E) customEvent.newInstance(this);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new IllegalStateException(e);
|
||||
} finally {
|
||||
seek(filePosition + pos + size);
|
||||
seek(eventEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,12 +230,13 @@ public class JfrReader implements Closeable {
|
||||
return null;
|
||||
}
|
||||
|
||||
private ExecutionSample readExecutionSample(boolean hasSamples) {
|
||||
private ExecutionSample readExecutionSample(boolean wall) {
|
||||
long time = getVarlong();
|
||||
int tid = getVarint();
|
||||
int stackTraceId = getVarint();
|
||||
int threadState = getVarint();
|
||||
int samples = hasSamples ? getVarint() : 1;
|
||||
int samples = wall ? getVarint() : 1;
|
||||
if (wall && hasWallTimeSpan) getVarlong(); // timeSpan is ignored
|
||||
return new ExecutionSample(time, tid, stackTraceId, threadState, samples);
|
||||
}
|
||||
|
||||
@@ -257,6 +269,15 @@ public class JfrReader implements Closeable {
|
||||
return new ExecutionSample(time, tid, stackTraceId, ExecutionSample.CPU_TIME_SAMPLE, 1);
|
||||
}
|
||||
|
||||
private NativeLockEvent readNativeLockEvent() {
|
||||
long time = getVarlong();
|
||||
long duration = getVarlong();
|
||||
int tid = getVarint();
|
||||
int stackTraceId = getVarint();
|
||||
long address = getVarlong();
|
||||
return new NativeLockEvent(time, tid, stackTraceId, address, duration);
|
||||
}
|
||||
|
||||
private MallocEvent readMallocEvent(boolean hasSize) {
|
||||
long time = getVarlong();
|
||||
int tid = getVarint();
|
||||
@@ -332,6 +353,7 @@ public class JfrReader implements Closeable {
|
||||
startNanos = Math.min(startNanos, chunkStartNanos);
|
||||
endNanos = Math.max(endNanos, chunkEndNanos);
|
||||
startTicks = Math.min(startTicks, chunkStartTicks);
|
||||
nanosPerTick = 1e9 / ticksPerSec;
|
||||
|
||||
types.clear();
|
||||
typesByName.clear();
|
||||
@@ -592,12 +614,16 @@ public class JfrReader implements Closeable {
|
||||
malloc = getTypeId("profiler.Malloc");
|
||||
free = getTypeId("profiler.Free");
|
||||
cpuTimeSample = getTypeId("jdk.CPUTimeSample");
|
||||
nativeLock = getTypeId("profiler.NativeLock");
|
||||
|
||||
registerEvent("jdk.CPULoad", CPULoad.class);
|
||||
registerEvent("jdk.GCHeapSummary", GCHeapSummary.class);
|
||||
registerEvent("jdk.ObjectCount", ObjectCount.class);
|
||||
registerEvent("jdk.ObjectCountAfterGC", ObjectCount.class);
|
||||
registerEvent("profiler.ProcessSample", ProcessSample.class);
|
||||
|
||||
JfrClass wallClass = typesByName.get("profiler.WallClockSample");
|
||||
hasWallTimeSpan = wallClass != null && wallClass.field("timeSpan") != null;
|
||||
}
|
||||
|
||||
private int getTypeId(String typeName) {
|
||||
@@ -701,6 +727,12 @@ public class JfrReader implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
public void rewind() throws IOException {
|
||||
seek(0);
|
||||
state = STATE_NEW_CHUNK;
|
||||
ensureBytes(CHUNK_HEADER_SIZE);
|
||||
}
|
||||
|
||||
private boolean ensureBytes(int needed) throws IOException {
|
||||
if (buf.remaining() >= needed) {
|
||||
return true;
|
||||
@@ -726,4 +758,8 @@ public class JfrReader implements Closeable {
|
||||
buf.flip();
|
||||
return buf.limit() > 0;
|
||||
}
|
||||
|
||||
public long eventTimeToNanos(long time) {
|
||||
return chunkStartNanos + (long) ((time - chunkStartTicks) * nanosPerTick);
|
||||
}
|
||||
}
|
||||
|
||||
22
src/converter/one/jfr/event/NativeLockEvent.java
Normal file
22
src/converter/one/jfr/event/NativeLockEvent.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package one.jfr.event;
|
||||
|
||||
public class NativeLockEvent extends Event {
|
||||
public final long address;
|
||||
public final long duration;
|
||||
|
||||
public NativeLockEvent(long time, int tid, int stackTraceId, long address, long duration) {
|
||||
super(time, tid, stackTraceId);
|
||||
this.address = address;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long value() {
|
||||
return duration;
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,12 @@ public class Proto {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Proto fieldFixed64(int index, long n) {
|
||||
tag(index, 1);
|
||||
writeFixed64(n);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Proto field(int index, double d) {
|
||||
tag(index, 1);
|
||||
writeDouble(d);
|
||||
@@ -118,8 +124,11 @@ public class Proto {
|
||||
}
|
||||
|
||||
public void writeDouble(double d) {
|
||||
writeFixed64(Double.doubleToRawLongBits(d));
|
||||
}
|
||||
|
||||
public void writeFixed64(long n) {
|
||||
ensureCapacity(8);
|
||||
long n = Double.doubleToRawLongBits(d);
|
||||
buf[pos] = (byte) n;
|
||||
buf[pos + 1] = (byte) (n >>> 8);
|
||||
buf[pos + 2] = (byte) (n >>> 16);
|
||||
|
||||
@@ -42,14 +42,14 @@ static int pthread_setspecific_hook(pthread_key_t key, const void* value) {
|
||||
}
|
||||
|
||||
void CpuEngine::onThreadStart() {
|
||||
CpuEngine* current = __atomic_load_n(&_current, __ATOMIC_ACQUIRE);
|
||||
CpuEngine* current = loadAcquire(_current);
|
||||
if (current != NULL) {
|
||||
current->createForThread(OS::threadId());
|
||||
}
|
||||
}
|
||||
|
||||
void CpuEngine::onThreadEnd() {
|
||||
CpuEngine* current = __atomic_load_n(&_current, __ATOMIC_ACQUIRE);
|
||||
CpuEngine* current = loadAcquire(_current);
|
||||
if (current != NULL) {
|
||||
current->destroyForThread(OS::threadId());
|
||||
}
|
||||
@@ -80,12 +80,12 @@ bool CpuEngine::setupThreadHook() {
|
||||
|
||||
void CpuEngine::enableThreadHook() {
|
||||
*_pthread_entry = (void*)pthread_setspecific_hook;
|
||||
__atomic_store_n(&_current, this, __ATOMIC_RELEASE);
|
||||
storeRelease(_current, this);
|
||||
}
|
||||
|
||||
void CpuEngine::disableThreadHook() {
|
||||
*_pthread_entry = (void*)pthread_setspecific;
|
||||
__atomic_store_n(&_current, NULL, __ATOMIC_RELEASE);
|
||||
storeRelease(_current, nullptr);
|
||||
}
|
||||
|
||||
bool CpuEngine::isResourceLimit(int err) {
|
||||
@@ -124,9 +124,8 @@ void CpuEngine::signalHandlerJ9(int signo, siginfo_t* siginfo, void* ucontext) {
|
||||
if (!_enabled) return;
|
||||
|
||||
J9StackTraceNotification notif;
|
||||
StackContext java_ctx;
|
||||
notif.num_frames = _cstack == CSTACK_NO ? 0 : _cstack == CSTACK_DWARF
|
||||
? StackWalker::walkDwarf(ucontext, notif.addr, MAX_J9_NATIVE_FRAMES, &java_ctx)
|
||||
: StackWalker::walkFP(ucontext, notif.addr, MAX_J9_NATIVE_FRAMES, &java_ctx);
|
||||
? StackWalker::walkDwarf(ucontext, notif.addr, MAX_J9_NATIVE_FRAMES)
|
||||
: StackWalker::walkFP(ucontext, notif.addr, MAX_J9_NATIVE_FRAMES);
|
||||
J9StackTraces::checkpoint(_interval, ¬if);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ class CTimer : public CpuEngine {
|
||||
return "ctimer";
|
||||
}
|
||||
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
|
||||
@@ -36,10 +35,6 @@ class CTimer : public CpuEngine {
|
||||
|
||||
class CTimer : public CpuEngine {
|
||||
public:
|
||||
Error check(Arguments& args) {
|
||||
return Error("CTimer is not supported on this platform");
|
||||
}
|
||||
|
||||
Error start(Arguments& args) {
|
||||
return Error("CTimer is not supported on this platform");
|
||||
}
|
||||
|
||||
@@ -73,20 +73,6 @@ void CTimer::destroyForThread(int tid) {
|
||||
}
|
||||
}
|
||||
|
||||
Error CTimer::check(Arguments& args) {
|
||||
if (!setupThreadHook()) {
|
||||
return Error("Could not set pthread hook");
|
||||
}
|
||||
|
||||
timer_t timer;
|
||||
if (timer_create(CLOCK_THREAD_CPUTIME_ID, NULL, &timer) < 0) {
|
||||
return Error("Failed to create CPU timer");
|
||||
}
|
||||
timer_delete(timer);
|
||||
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error CTimer::start(Arguments& args) {
|
||||
if (!setupThreadHook()) {
|
||||
return Error("Could not set pthread hook");
|
||||
|
||||
@@ -8,10 +8,6 @@
|
||||
|
||||
volatile bool Engine::_enabled = false;
|
||||
|
||||
Error Engine::check(Arguments& args) {
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error Engine::start(Arguments& args) {
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,6 @@ class Engine {
|
||||
return "total";
|
||||
}
|
||||
|
||||
virtual Error check(Arguments& args);
|
||||
virtual Error start(Arguments& args);
|
||||
virtual void stop();
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ enum EventType {
|
||||
PERF_SAMPLE,
|
||||
EXECUTION_SAMPLE,
|
||||
WALL_CLOCK_SAMPLE,
|
||||
NATIVE_LOCK_SAMPLE,
|
||||
MALLOC_SAMPLE,
|
||||
INSTRUMENTED_METHOD,
|
||||
METHOD_TRACE,
|
||||
@@ -55,6 +56,7 @@ class MethodTraceEvent : public Event {
|
||||
class WallClockEvent : public Event {
|
||||
public:
|
||||
u64 _start_time;
|
||||
u64 _time_span;
|
||||
ThreadState _thread_state;
|
||||
u32 _samples;
|
||||
};
|
||||
@@ -74,6 +76,13 @@ class LockEvent : public EventWithClassId {
|
||||
long long _timeout;
|
||||
};
|
||||
|
||||
class NativeLockEvent : public Event {
|
||||
public:
|
||||
u64 _start_time;
|
||||
u64 _end_time;
|
||||
uintptr_t _address;
|
||||
};
|
||||
|
||||
class LiveObject : public EventWithClassId {
|
||||
public:
|
||||
u64 _start_time;
|
||||
|
||||
@@ -58,8 +58,9 @@ 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", "lbr", "vm"};
|
||||
static const char* const SETTING_CSTACK[] = {NULL, "no", "fp", "dwarf", "vm"};
|
||||
|
||||
|
||||
struct CpuTime {
|
||||
@@ -538,28 +539,22 @@ class Recording {
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char* getFeaturesString(char* str, size_t size, StackWalkFeatures& f) {
|
||||
snprintf(str, size, "%s %s %s %s %s %s %s %s %s %s %s %s %s",
|
||||
f.unknown_java ? "unknown_java" : "-",
|
||||
f.unwind_stub ? "unwind_stub" : "-",
|
||||
f.unwind_comp ? "unwind_comp" : "-",
|
||||
f.unwind_native ? "unwind_native" : "-",
|
||||
f.java_anchor ? "java_anchor" : "-",
|
||||
f.gc_traces ? "gc_traces" : "-",
|
||||
f.stats ? "stats" : "-",
|
||||
f.jnienv ? "jnienv" : "-",
|
||||
f.probe_sp ? "probesp" : "-",
|
||||
f.mixed ? "mixed" : "-",
|
||||
f.vtable_target ? "vtable" : "-",
|
||||
f.comp_task ? "comptask" : "-",
|
||||
f.pc_addr ? "pcaddr" : "-");
|
||||
return str;
|
||||
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 : "";
|
||||
}
|
||||
|
||||
void flush(Buffer* buf) {
|
||||
ssize_t result = write(_in_memory ? _memfd : _fd, buf->data(), buf->offset());
|
||||
if (result > 0) {
|
||||
atomicInc(_bytes_written, result);
|
||||
atomicInc(_bytes_written, (u64)result);
|
||||
}
|
||||
buf->reset();
|
||||
}
|
||||
@@ -649,6 +644,7 @@ class Recording {
|
||||
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);
|
||||
|
||||
char str[256];
|
||||
writeStringSetting(buf, T_ACTIVE_RECORDING, "features", getFeaturesString(str, sizeof(str), args._features));
|
||||
@@ -665,6 +661,9 @@ class Recording {
|
||||
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);
|
||||
@@ -1070,6 +1069,7 @@ class Recording {
|
||||
buf->putVar32(call_trace_id);
|
||||
buf->putVar32(event->_thread_state);
|
||||
buf->putVar32(event->_samples);
|
||||
buf->putVar64(event->_time_span);
|
||||
buf->put8(start, buf->offset() - start);
|
||||
}
|
||||
|
||||
@@ -1201,6 +1201,17 @@ class Recording {
|
||||
buf->put8(start, buf->offset() - start);
|
||||
}
|
||||
|
||||
void recordNativeLockSample(Buffer* buf, int tid, u32 call_trace_id, NativeLockEvent* event) {
|
||||
int start = buf->skip(1);
|
||||
buf->put8(T_NATIVE_LOCK);
|
||||
buf->putVar64(event->_start_time);
|
||||
buf->putVar64(event->_end_time - event->_start_time);
|
||||
buf->putVar32(tid);
|
||||
buf->putVar32(call_trace_id);
|
||||
buf->putVar64(event->_address);
|
||||
buf->put8(start, buf->offset() - start);
|
||||
}
|
||||
|
||||
void recordWindow(Buffer* buf, int tid, ProfilingWindow* event) {
|
||||
int start = buf->skip(1);
|
||||
buf->put8(T_WINDOW);
|
||||
@@ -1351,7 +1362,7 @@ Error FlightRecorder::startMasterRecording(Arguments& args, const char* filename
|
||||
jclass cls = env->DefineClass(JFR_SYNC_NAME, NULL, (const jbyte*)JFR_SYNC_CLASS, INCBIN_SIZEOF(JFR_SYNC_CLASS));
|
||||
if (cls == NULL || env->RegisterNatives(cls, &native_method, 1) != 0
|
||||
|| (_start_method = env->GetStaticMethodID(cls, "start", "(Ljava/lang/String;Ljava/lang/String;I)V")) == NULL
|
||||
|| (_stop_method = env->GetStaticMethodID(cls, "stop", "()V")) == NULL
|
||||
|| (_stop_method = env->GetStaticMethodID(cls, "stop", "()Z")) == NULL
|
||||
|| (_box_method = env->GetStaticMethodID(cls, "box", "(I)Ljava/lang/Integer;")) == NULL
|
||||
|| (_jfr_sync_class = (jclass)env->NewGlobalRef(cls)) == NULL) {
|
||||
env->ExceptionDescribe();
|
||||
@@ -1387,7 +1398,9 @@ Error FlightRecorder::startMasterRecording(Arguments& args, const char* filename
|
||||
int event_mask = args.eventMask() |
|
||||
((args._jfr_options ^ JFR_SYNC_OPTS) << EVENT_MASK_SIZE);
|
||||
|
||||
storeRelease(_jfr_starting, true);
|
||||
env->CallStaticVoidMethod(_jfr_sync_class, _start_method, jfilename, jsettings, event_mask);
|
||||
storeRelease(_jfr_starting, false);
|
||||
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
@@ -1399,7 +1412,9 @@ Error FlightRecorder::startMasterRecording(Arguments& args, const char* filename
|
||||
|
||||
void FlightRecorder::stopMasterRecording() {
|
||||
JNIEnv* env = VM::jni();
|
||||
env->CallStaticVoidMethod(_jfr_sync_class, _stop_method);
|
||||
if (env->CallStaticBooleanMethod(_jfr_sync_class, _stop_method) == JNI_FALSE) {
|
||||
Log::warn("Failed to stop JFR recording");
|
||||
}
|
||||
env->ExceptionClear();
|
||||
}
|
||||
|
||||
@@ -1441,6 +1456,9 @@ void FlightRecorder::recordEvent(int lock_index, int tid, u32 call_trace_id,
|
||||
case PARK_SAMPLE:
|
||||
_rec->recordThreadPark(buf, tid, call_trace_id, (LockEvent*)event);
|
||||
break;
|
||||
case NATIVE_LOCK_SAMPLE:
|
||||
_rec->recordNativeLockSample(buf, tid, call_trace_id, (NativeLockEvent*)event);
|
||||
break;
|
||||
case PROFILING_WINDOW:
|
||||
_rec->recordWindow(buf, tid, (ProfilingWindow*)event);
|
||||
break;
|
||||
@@ -1473,3 +1491,7 @@ void FlightRecorder::recordLog(LogLevel level, const char* message, size_t len)
|
||||
|
||||
_rec_lock.unlockShared();
|
||||
}
|
||||
|
||||
bool FlightRecorder::isJfrStarting() {
|
||||
return loadAcquire(_jfr_starting);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ class FlightRecorder {
|
||||
EventType event_type, Event* event);
|
||||
|
||||
void recordLog(LogLevel level, const char* message, size_t len);
|
||||
|
||||
static bool isJfrStarting();
|
||||
};
|
||||
|
||||
#endif // _FLIGHTRECORDER_H
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "callTraceStorage.h"
|
||||
#include "demangle.h"
|
||||
#include "frameName.h"
|
||||
#include "profiler.h"
|
||||
@@ -379,3 +380,24 @@ bool FrameName::exclude(const char* frame_name) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FrameName::excludeTrace(CallTrace* trace) {
|
||||
bool check_include = !_include.empty();
|
||||
bool check_exclude = !_exclude.empty();
|
||||
if (!(check_include || check_exclude)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < trace->num_frames; i++) {
|
||||
const char* frame_name = name(trace->frames[i], true);
|
||||
if (check_exclude && exclude(frame_name)) {
|
||||
return true;
|
||||
}
|
||||
if (check_include && include(frame_name)) {
|
||||
check_include = false;
|
||||
if (!check_exclude) break;
|
||||
}
|
||||
}
|
||||
|
||||
return check_include;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ typedef std::map<jmethodID, std::string> JMethodCache;
|
||||
typedef std::map<int, std::string> ThreadMap;
|
||||
typedef std::map<unsigned int, const char*> ClassMap;
|
||||
|
||||
class CallTrace;
|
||||
|
||||
enum MatchType {
|
||||
MATCH_EQUALS,
|
||||
@@ -70,6 +71,8 @@ class FrameName {
|
||||
const char* typeSuffix(FrameTypeId type);
|
||||
void javaMethodName(jmethodID method);
|
||||
void javaClassName(const char* symbol, size_t length, int style);
|
||||
bool include(const char* frame_name);
|
||||
bool exclude(const char* frame_name);
|
||||
|
||||
public:
|
||||
FrameName(Arguments& args, int style, int epoch, Mutex& thread_names_lock, ThreadMap& thread_names);
|
||||
@@ -78,11 +81,7 @@ class FrameName {
|
||||
const char* name(ASGCT_CallFrame& frame, bool for_matching = false);
|
||||
FrameTypeId type(ASGCT_CallFrame& frame);
|
||||
|
||||
bool hasIncludeList() { return !_include.empty(); }
|
||||
bool hasExcludeList() { return !_exclude.empty(); }
|
||||
|
||||
bool include(const char* frame_name);
|
||||
bool exclude(const char* frame_name);
|
||||
bool excludeTrace(CallTrace* trace);
|
||||
};
|
||||
|
||||
#endif // _FRAMENAME_H
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -16,6 +16,7 @@ import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.ParseException;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
/**
|
||||
* Synchronize async-profiler recording with an existing JFR recording.
|
||||
@@ -27,7 +28,7 @@ class JfrSync implements FlightRecorderListener {
|
||||
private static final int EM_LOCK = 4;
|
||||
|
||||
// Keep in sync with EVENT_MASK_SIZE in C++
|
||||
private static final int EVENT_MASK_SIZE = 6;
|
||||
private static final int EVENT_MASK_SIZE = 7;
|
||||
|
||||
// Keep in sync with JfrOption
|
||||
private static final int NO_SYSTEM_INFO = 1;
|
||||
@@ -77,13 +78,24 @@ class JfrSync implements FlightRecorderListener {
|
||||
recording.start();
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
public static boolean stop() {
|
||||
Recording recording = masterRecording;
|
||||
if (recording != null) {
|
||||
// Disable state change notification before stopping
|
||||
masterRecording = null;
|
||||
recording.stop();
|
||||
try {
|
||||
recording.stop();
|
||||
} catch (IllegalStateException e) {
|
||||
// Workaround the JDK issue: JFR shutdown hook may stop the recording concurrently
|
||||
// then populate the target file outside the state lock.
|
||||
// Once the file is completely written, the recording state is changed to CLOSED.
|
||||
for (int pause = 10; recording.getState() != RecordingState.CLOSED && pause < 1000; pause *= 2) {
|
||||
LockSupport.parkNanos(pause * 1_000_000L);
|
||||
}
|
||||
return recording.getState() == RecordingState.CLOSED;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void disableBuiltinEvents(Recording recording, int eventMask) {
|
||||
@@ -103,6 +115,7 @@ class JfrSync implements FlightRecorderListener {
|
||||
}
|
||||
// No built-in event related to EM_WALL
|
||||
// No built-in event related to EM_NATIVEMEM
|
||||
// No built-in event related to EM_NATIVELOCK
|
||||
// No need to disable built-in event related to EM_METHOD_TRACE
|
||||
|
||||
eventMask >>>= EVENT_MASK_SIZE;
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -17,7 +17,7 @@ import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class Server extends Thread implements Executor, HttpHandler {
|
||||
private static final String[] COMMANDS = "start,resume,stop,dump,check,status,meminfo,list,version".split(",");
|
||||
private static final String[] COMMANDS = "start,resume,stop,dump,status,metrics,list,version".split(",");
|
||||
|
||||
private final HttpServer server;
|
||||
private final AtomicInteger threadNum = new AtomicInteger();
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "asprof.h"
|
||||
#include "cpuEngine.h"
|
||||
#include "mallocTracer.h"
|
||||
#include "nativeLockTracer.h"
|
||||
#include "profiler.h"
|
||||
#include "symbols.h"
|
||||
|
||||
@@ -100,6 +101,7 @@ static void* dlopen_hook_impl(const char* filename, int flags, bool patch) {
|
||||
Hooks::patchLibraries();
|
||||
}
|
||||
MallocTracer::installHooks();
|
||||
NativeLockTracer::installHooks();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class Index {
|
||||
private:
|
||||
std::unordered_map<std::string, size_t> _idx_map;
|
||||
size_t _start_index;
|
||||
|
||||
|
||||
public:
|
||||
Index(size_t start_index = 0) : _start_index(start_index) {
|
||||
// The first index should contain the empty string
|
||||
@@ -49,6 +49,10 @@ class Index {
|
||||
return _idx_map.size();
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return _idx_map.empty();
|
||||
}
|
||||
|
||||
void forEachOrdered(const std::function<void(size_t idx, const std::string&)>& consumer) const {
|
||||
std::vector<const std::string*> arr(_idx_map.size());
|
||||
for (const auto& it : _idx_map) {
|
||||
|
||||
@@ -401,7 +401,7 @@ class BytecodeRewriter {
|
||||
}
|
||||
|
||||
// Return the new offset delta
|
||||
static u16 updateCurrentFrame(int32_t& current_frame_old, int32_t& current_frame_new,
|
||||
static u16 updateCurrentFrame(int32_t& current_frame_old, int32_t& current_frame_new,
|
||||
u16 offset_delta_old, const u16* relocation_table) {
|
||||
current_frame_old += offset_delta_old + 1;
|
||||
long previous_frame_new = current_frame_new;
|
||||
@@ -746,7 +746,7 @@ Result BytecodeRewriter::rewriteStackMapTable(const u16* relocation_table, int n
|
||||
|
||||
if (frame_type <= 127) {
|
||||
// same_frame and same_locals_1_stack_item_frame
|
||||
u16 new_offset_delta = updateCurrentFrame(current_frame_old, current_frame_new,
|
||||
u16 new_offset_delta = updateCurrentFrame(current_frame_old, current_frame_new,
|
||||
frame_type % 64, relocation_table);
|
||||
|
||||
u8 new_frame_type;
|
||||
@@ -1059,7 +1059,7 @@ Latency Instrument::_interval;
|
||||
volatile u64 Instrument::_calls;
|
||||
volatile bool Instrument::_running;
|
||||
|
||||
Error Instrument::check(Arguments& args) {
|
||||
Error Instrument::initialize() {
|
||||
if (!_instrument_class_loaded) {
|
||||
if (!VM::loaded()) {
|
||||
return Error("Profiling event is not supported with non-Java processes");
|
||||
@@ -1083,7 +1083,7 @@ Error Instrument::check(Arguments& args) {
|
||||
}
|
||||
|
||||
Error Instrument::start(Arguments& args) {
|
||||
Error error = check(args);
|
||||
Error error = initialize();
|
||||
if (error) return error;
|
||||
|
||||
error = setupTargetClassAndMethod(args);
|
||||
@@ -1164,7 +1164,7 @@ Error addTarget(Targets& targets, const char* s, Latency default_latency) {
|
||||
|
||||
Error Instrument::setupTargetClassAndMethod(const Arguments& args) {
|
||||
_targets.clear();
|
||||
|
||||
|
||||
if (args._trace.empty()) {
|
||||
Error error = addTarget(_targets, args._event, NO_LATENCY);
|
||||
if (error) return error;
|
||||
|
||||
@@ -27,6 +27,7 @@ class Instrument : public Engine {
|
||||
static volatile u64 _calls;
|
||||
static volatile bool _running;
|
||||
|
||||
static Error initialize();
|
||||
static bool shouldRecordSample() {
|
||||
return _interval <= 1 || ((atomicInc(_calls) + 1) % _interval) == 0;
|
||||
}
|
||||
@@ -44,7 +45,6 @@ class Instrument : public Engine {
|
||||
return "calls";
|
||||
}
|
||||
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
|
||||
|
||||
@@ -10,20 +10,6 @@
|
||||
#include "vmEntry.h"
|
||||
|
||||
|
||||
Error ITimer::check(Arguments& args) {
|
||||
OS::installSignalHandler(SIGPROF, NULL, SIG_IGN);
|
||||
|
||||
struct itimerval tv_on = {{1, 0}, {1, 0}};
|
||||
if (setitimer(ITIMER_PROF, &tv_on, NULL) != 0) {
|
||||
return Error("ITIMER_PROF is not supported on this system");
|
||||
}
|
||||
|
||||
struct itimerval tv_off = {{0, 0}, {0, 0}};
|
||||
setitimer(ITIMER_PROF, &tv_off, NULL);
|
||||
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error ITimer::start(Arguments& args) {
|
||||
if (args._interval < 0) {
|
||||
return Error("interval must be positive");
|
||||
|
||||
@@ -15,7 +15,6 @@ class ITimer : public CpuEngine {
|
||||
return "itimer";
|
||||
}
|
||||
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
};
|
||||
|
||||
@@ -22,18 +22,10 @@ void J9ObjectSampler::VMObjectAlloc(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread
|
||||
}
|
||||
}
|
||||
|
||||
Error J9ObjectSampler::check(Arguments& args) {
|
||||
Error J9ObjectSampler::start(Arguments& args) {
|
||||
if (J9Ext::InstrumentableObjectAlloc_id < 0) {
|
||||
return Error("InstrumentableObjectAlloc is not supported on this JVM");
|
||||
}
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error J9ObjectSampler::start(Arguments& args) {
|
||||
Error error = check(args);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
_interval = args._alloc > 0 ? args._alloc : DEFAULT_ALLOC_INTERVAL;
|
||||
_allocated_bytes = 0;
|
||||
|
||||
@@ -15,7 +15,6 @@ class J9ObjectSampler : public ObjectSampler {
|
||||
return "j9_object_sampler";
|
||||
}
|
||||
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class J9VMThread {
|
||||
}
|
||||
|
||||
void setOverflowMark() {
|
||||
__atomic_store_n(&_overflow_mark, (uintptr_t)-1, __ATOMIC_RELEASE);
|
||||
storeRelease(_overflow_mark, (uintptr_t)-1);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,7 +47,7 @@ pthread_t J9StackTraces::_thread = 0;
|
||||
int J9StackTraces::_max_stack_depth;
|
||||
int J9StackTraces::_pipe[2];
|
||||
|
||||
static JNIEnv* _self_env = NULL;
|
||||
static JNIEnv* _self_env = nullptr;
|
||||
|
||||
|
||||
Error J9StackTraces::start(Arguments& args) {
|
||||
@@ -78,7 +78,7 @@ void J9StackTraces::stop() {
|
||||
|
||||
void J9StackTraces::timerLoop() {
|
||||
JNIEnv* jni = VM::attachThread("Async-profiler Sampler");
|
||||
__atomic_store_n(&_self_env, jni, __ATOMIC_RELEASE);
|
||||
storeRelease(_self_env, jni);
|
||||
|
||||
jni->PushLocalFrame(64);
|
||||
|
||||
@@ -145,13 +145,13 @@ void J9StackTraces::timerLoop() {
|
||||
free(jvmti_frames);
|
||||
free(frames);
|
||||
|
||||
__atomic_store_n(&_self_env, NULL, __ATOMIC_RELEASE);
|
||||
storeRelease(_self_env, nullptr);
|
||||
VM::detachThread();
|
||||
}
|
||||
|
||||
void J9StackTraces::checkpoint(u64 counter, J9StackTraceNotification* notif) {
|
||||
JNIEnv* self_env = __atomic_load_n(&_self_env, __ATOMIC_ACQUIRE);
|
||||
if (self_env == NULL) {
|
||||
JNIEnv* self_env = loadAcquire(_self_env);
|
||||
if (self_env == nullptr) {
|
||||
// Sampler thread is not ready
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ static int start_attach_mechanism(int pid, int nspid) {
|
||||
do {
|
||||
nanosleep(&ts, NULL);
|
||||
result = check_socket(nspid);
|
||||
} while (result != 0 && (ts.tv_nsec += 20000000) < 500000000);
|
||||
} while (result != 0 && (ts.tv_nsec += 20000000) < 500000000 && kill(pid, 0) == 0);
|
||||
|
||||
unlink(path);
|
||||
return result;
|
||||
|
||||
@@ -237,7 +237,8 @@ JfrMetadata::JfrMetadata() : Element("root") {
|
||||
<< field("sampledThread", T_THREAD, "Thread", F_CPOOL)
|
||||
<< field("stackTrace", T_STACK_TRACE, "Stack Trace", F_CPOOL)
|
||||
<< field("state", T_THREAD_STATE, "Thread State", F_CPOOL)
|
||||
<< field("samples", T_INT, "Samples", F_UNSIGNED))
|
||||
<< field("samples", T_INT, "Samples", F_UNSIGNED)
|
||||
<< field("timeSpan", T_LONG, "Time Span", F_DURATION_TICKS))
|
||||
|
||||
<< (type("profiler.Malloc", T_MALLOC, "malloc")
|
||||
<< category("Java Virtual Machine", "Native Memory")
|
||||
@@ -254,6 +255,14 @@ JfrMetadata::JfrMetadata() : Element("root") {
|
||||
<< field("stackTrace", T_STACK_TRACE, "Stack Trace", F_CPOOL)
|
||||
<< field("address", T_LONG, "Address", F_ADDRESS))
|
||||
|
||||
<< (type("profiler.NativeLock", T_NATIVE_LOCK, "Native Lock Sample")
|
||||
<< category("Java Virtual Machine", "Native Lock")
|
||||
<< field("startTime", T_LONG, "Start Time", F_TIME_TICKS)
|
||||
<< field("duration", T_LONG, "Duration", F_DURATION_TICKS)
|
||||
<< field("eventThread", T_THREAD, "Event Thread", F_CPOOL)
|
||||
<< field("stackTrace", T_STACK_TRACE, "Stack Trace", F_CPOOL)
|
||||
<< field("address", T_LONG, "Lock Address", F_ADDRESS))
|
||||
|
||||
<< (type("profiler.UserEvent", T_USER_EVENT, "User-Defined Event")
|
||||
<< category("Profiler")
|
||||
<< field("startTime", T_LONG, "Start Time", F_TIME_TICKS)
|
||||
|
||||
@@ -67,6 +67,7 @@ enum JfrType {
|
||||
T_FREE = 121,
|
||||
T_USER_EVENT = 122,
|
||||
T_PROCESS_SAMPLE = 123,
|
||||
T_NATIVE_LOCK = 124,
|
||||
|
||||
// types after T_ANNOTATION inherit from java.lang.annotation.Annotation, see JfrMetadata::type
|
||||
T_ANNOTATION = 200,
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
LinearAllocator::LinearAllocator(size_t chunk_size) {
|
||||
_chunk_size = chunk_size;
|
||||
_used_memory = chunk_size;
|
||||
_reserve = _tail = allocateChunk(NULL);
|
||||
}
|
||||
|
||||
@@ -28,14 +29,7 @@ void LinearAllocator::clear() {
|
||||
}
|
||||
_reserve = _tail;
|
||||
_tail->offs = sizeof(Chunk);
|
||||
}
|
||||
|
||||
size_t LinearAllocator::usedMemory() {
|
||||
size_t bytes = _reserve->prev == _tail ? _chunk_size : 0;
|
||||
for (Chunk* chunk = _tail; chunk != NULL; chunk = chunk->prev) {
|
||||
bytes += _chunk_size;
|
||||
}
|
||||
return bytes;
|
||||
_used_memory = _chunk_size;
|
||||
}
|
||||
|
||||
void* LinearAllocator::alloc(size_t size) {
|
||||
@@ -99,5 +93,11 @@ Chunk* LinearAllocator::getNextChunk(Chunk* current) {
|
||||
|
||||
// Expected case: a new chunk is already reserved
|
||||
Chunk* tail = __sync_val_compare_and_swap(&_tail, current, reserve);
|
||||
return tail == current ? reserve : tail;
|
||||
if (tail == current) {
|
||||
// CAS succeeded: switched to a new chunk
|
||||
atomicInc(_used_memory, _chunk_size);
|
||||
return reserve;
|
||||
}
|
||||
// Lost the race to another thread
|
||||
return tail;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ struct Chunk {
|
||||
class LinearAllocator {
|
||||
private:
|
||||
size_t _chunk_size;
|
||||
size_t _used_memory;
|
||||
Chunk* _tail;
|
||||
Chunk* _reserve;
|
||||
|
||||
@@ -31,8 +32,11 @@ class LinearAllocator {
|
||||
LinearAllocator(size_t chunk_size);
|
||||
~LinearAllocator();
|
||||
|
||||
size_t usedMemory() const {
|
||||
return _used_memory;
|
||||
}
|
||||
|
||||
void clear();
|
||||
size_t usedMemory();
|
||||
|
||||
void* alloc(size_t size);
|
||||
};
|
||||
|
||||
@@ -66,14 +66,12 @@ Error LockTracer::start(Arguments& args) {
|
||||
|
||||
void LockTracer::stop() {
|
||||
jvmtiEnv* jvmti = VM::jvmti();
|
||||
JNIEnv* env = VM::jni();
|
||||
|
||||
// Disable Java Monitor events
|
||||
jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL);
|
||||
jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL);
|
||||
|
||||
// Reset Unsafe.park() trap
|
||||
setUnsafeParkEntry(env, _orig_unsafe_park);
|
||||
// We don't reset the Unsafe::park hook due to JDK-8369219
|
||||
}
|
||||
|
||||
Error LockTracer::initialize(jvmtiEnv* jvmti, JNIEnv* env) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "demangle.h"
|
||||
#include "dictionary.h"
|
||||
#include "index.h"
|
||||
|
||||
@@ -35,9 +35,8 @@ static const char USAGE_STRING[] =
|
||||
" resume resume profiling without resetting collected data\n"
|
||||
" stop stop profiling\n"
|
||||
" dump dump collected data without stopping profiling session\n"
|
||||
" check check if the specified profiling event is available\n"
|
||||
" status print profiling status\n"
|
||||
" meminfo print profiler memory stats\n"
|
||||
" metrics print profiler metrics in Prometheus format\n"
|
||||
" list list profiling events supported by the target JVM\n"
|
||||
" load load agent library (jattach action)\n"
|
||||
" jcmd run JVM diagnostic command (jattach action)\n"
|
||||
@@ -55,6 +54,7 @@ static const char USAGE_STRING[] =
|
||||
" -g, --sig print method signatures\n"
|
||||
" -a, --ann annotate Java methods\n"
|
||||
" -l, --lib prepend library names\n"
|
||||
" --dot dotted class names\n"
|
||||
" -o fmt output format: flat|traces|collapsed|flamegraph|tree|jfr|otlp\n"
|
||||
" -I include output only stack traces containing the specified pattern\n"
|
||||
" -X exclude exclude stack traces with the specified pattern\n"
|
||||
@@ -70,25 +70,29 @@ static const char USAGE_STRING[] =
|
||||
"\n"
|
||||
" --loop time run profiler in a loop\n"
|
||||
" --alloc bytes allocation profiling interval in bytes\n"
|
||||
" --tlab use TLAB events for allocation profiling\n"
|
||||
" --live build allocation profile from live objects only\n"
|
||||
" --nativemem bytes native allocation profiling interval in bytes\n"
|
||||
" --nofree do not collect free calls in native allocation profiling\n"
|
||||
" --trace method Method to be instrumented with optional latency threshold\n"
|
||||
" --lock duration lock profiling threshold in nanoseconds\n"
|
||||
" --lock time lock profiling threshold in nanoseconds\n"
|
||||
" --nativelock time pthread mutex/rwlock profiling threshold in nanoseconds\n"
|
||||
" --wall interval wall clock profiling interval\n"
|
||||
" --nobatch legacy wall clock sampling without batch events\n"
|
||||
" --proc interval process sampling interval (default: 30s)\n"
|
||||
" --all shorthand for enabling cpu, wall, alloc, live,\n"
|
||||
" nativemem and lock profiling simultaneously\n"
|
||||
" --total accumulate the total value (time, bytes, etc.)\n"
|
||||
" --all-user only include user-mode events\n"
|
||||
" --sched group threads by scheduling policy\n"
|
||||
" --cstack mode how to traverse C stack: fp|dwarf|lbr|vm|no\n"
|
||||
" --cstack mode how to traverse C stack: fp|dwarf|vm|no\n"
|
||||
" --signal num use alternative signal for cpu or wall clock profiling\n"
|
||||
" --clock source clock source for JFR timestamps: tsc|monotonic\n"
|
||||
" --begin function begin profiling when function is executed\n"
|
||||
" --end function end profiling when function is executed\n"
|
||||
" --ttsp only time-to-safepoint profiling \n"
|
||||
" --nostop do not stop profiling outside --begin/--end window\n"
|
||||
" --memlimit bytes limit size of the stack trace storage\n"
|
||||
" --jfropts opts JFR recording options: mem\n"
|
||||
" --jfrsync config synchronize profiler with JFR recording\n"
|
||||
" --libpath path full path to libasyncProfiler.so in the container\n"
|
||||
@@ -412,8 +416,8 @@ int main(int argc, const char** argv) {
|
||||
while (args.count() > 0 && !(jattach_action && pid)) {
|
||||
String arg = args.next();
|
||||
|
||||
if (arg == "start" || arg == "resume" || arg == "stop" || arg == "dump" || arg == "check" ||
|
||||
arg == "status" || arg == "meminfo" || arg == "list" || arg == "collect") {
|
||||
if (arg == "start" || arg == "resume" || arg == "stop" || arg == "dump" ||
|
||||
arg == "status" || arg == "metrics" || arg == "list" || arg == "collect") {
|
||||
action = arg;
|
||||
|
||||
} else if (arg == "load" || arg == "jcmd" || arg == "threaddump" || arg == "dumpheap" || arg == "inspectheap") {
|
||||
@@ -500,31 +504,22 @@ int main(int argc, const char** argv) {
|
||||
} else if (arg == "--width" || arg == "--height" || arg == "--minwidth") {
|
||||
format << "," << (arg.str() + 2) << "=" << args.next();
|
||||
|
||||
} else if (arg == "--reverse" || arg == "--inverted" || arg == "--samples" || arg == "--total" ||
|
||||
arg == "--sched" || arg == "--live" || arg == "--nofree" || arg == "--record-cpu") {
|
||||
} else if (arg == "--reverse" || arg == "--inverted" || arg == "--samples" || arg == "--total") {
|
||||
format << "," << (arg.str() + 2);
|
||||
|
||||
} else if (arg == "--alloc" || arg == "--nativemem" || arg == "--lock" || arg == "--wall" ||
|
||||
arg == "--trace" || arg == "--chunksize" || arg == "--chunktime" ||
|
||||
} else if (arg == "--alloc" || arg == "--nativemem" || arg == "--nativelock" || arg == "--lock" ||
|
||||
arg == "--wall" || arg == "--trace" || arg == "--chunksize" || arg == "--chunktime" ||
|
||||
arg == "--cstack" || arg == "--signal" || arg == "--clock" || arg == "--begin" || arg == "--end" ||
|
||||
arg == "--target-cpu" || arg == "--proc") {
|
||||
arg == "--target-cpu" || arg == "--proc" || arg == "--memlimit") {
|
||||
params << "," << (arg.str() + 2) << "=" << args.next();
|
||||
|
||||
} else if (arg == "--ttsp") {
|
||||
params << ",ttsp";
|
||||
|
||||
} else if (arg == "--nostop") {
|
||||
params << ",nostop";
|
||||
|
||||
} else if (arg == "--all") {
|
||||
params << ",all";
|
||||
} else if (arg == "--all" || arg == "--live" || arg == "--nobatch" || arg == "--nofree" || arg == "--nostop" ||
|
||||
arg == "--record-cpu" || arg == "--sched" || arg == "--tlab" || arg == "--ttsp") {
|
||||
params << "," << (arg.str() + 2);
|
||||
|
||||
} else if (arg == "--all-user") {
|
||||
params << ",alluser";
|
||||
|
||||
} else if (arg == "--safe-mode") {
|
||||
params << ",safemode=" << args.next();
|
||||
|
||||
} else if (arg == "--jfrsync" || arg == "--jfropts") {
|
||||
params << "," << (arg.str() + 2) << "=" << args.next();
|
||||
output = "jfr";
|
||||
@@ -588,12 +583,13 @@ int main(int argc, const char** argv) {
|
||||
signal(SIGTERM, sigint_handler);
|
||||
|
||||
while (time_micros() < end_time) {
|
||||
sleep(1);
|
||||
|
||||
if (kill(pid, 0) != 0) {
|
||||
fprintf(stderr, "Process exited\n");
|
||||
if (use_tmp_file) print_file(file, STDOUT_FILENO);
|
||||
return 0;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
fprintf(stderr, end_time != 0 ? "Done\n" : "Interrupted\n");
|
||||
|
||||
@@ -13,6 +13,10 @@ Mutex::Mutex() {
|
||||
pthread_mutex_init(&_mutex, &attr);
|
||||
}
|
||||
|
||||
bool Mutex::tryLock() {
|
||||
return pthread_mutex_trylock(&_mutex) == 0;
|
||||
}
|
||||
|
||||
void Mutex::lock() {
|
||||
pthread_mutex_lock(&_mutex);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ class Mutex {
|
||||
public:
|
||||
Mutex();
|
||||
|
||||
bool tryLock();
|
||||
void lock();
|
||||
void unlock();
|
||||
};
|
||||
|
||||
152
src/nativeLockTracer.cpp
Normal file
152
src/nativeLockTracer.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "codeCache.h"
|
||||
#include "nativeLockTracer.h"
|
||||
#include "profiler.h"
|
||||
#include "symbols.h"
|
||||
#include "tsc.h"
|
||||
|
||||
|
||||
extern "C" int pthread_mutex_lock_hook(pthread_mutex_t* mutex) {
|
||||
if (!NativeLockTracer::running()) {
|
||||
return pthread_mutex_lock(mutex);
|
||||
}
|
||||
|
||||
if (pthread_mutex_trylock(mutex) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
u64 start_time = TSC::ticks();
|
||||
int ret = pthread_mutex_lock(mutex);
|
||||
u64 end_time = TSC::ticks();
|
||||
|
||||
if (ret == 0) {
|
||||
NativeLockTracer::recordNativeLock(mutex, start_time, end_time);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern "C" int pthread_rwlock_rdlock_hook(pthread_rwlock_t* rwlock) {
|
||||
if (!NativeLockTracer::running()) {
|
||||
return pthread_rwlock_rdlock(rwlock);
|
||||
}
|
||||
|
||||
if (pthread_rwlock_tryrdlock(rwlock) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
u64 start_time = TSC::ticks();
|
||||
int ret = pthread_rwlock_rdlock(rwlock);
|
||||
u64 end_time = TSC::ticks();
|
||||
|
||||
if (ret == 0) {
|
||||
NativeLockTracer::recordNativeLock(rwlock, start_time, end_time);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern "C" int pthread_rwlock_wrlock_hook(pthread_rwlock_t* rwlock) {
|
||||
if (!NativeLockTracer::running()) {
|
||||
return pthread_rwlock_wrlock(rwlock);
|
||||
}
|
||||
|
||||
if (pthread_rwlock_trywrlock(rwlock) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
u64 start_time = TSC::ticks();
|
||||
int ret = pthread_rwlock_wrlock(rwlock);
|
||||
u64 end_time = TSC::ticks();
|
||||
|
||||
if (ret == 0) {
|
||||
NativeLockTracer::recordNativeLock(rwlock, start_time, end_time);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
u64 NativeLockTracer::_interval;
|
||||
double NativeLockTracer::_ticks_to_nanos;
|
||||
Mutex NativeLockTracer::_patch_lock;
|
||||
int NativeLockTracer::_patched_libs = 0;
|
||||
bool NativeLockTracer::_initialized = false;
|
||||
volatile bool NativeLockTracer::_running = false;
|
||||
volatile u64 NativeLockTracer::_total_duration; // for interval sampling
|
||||
|
||||
void NativeLockTracer::initialize() {
|
||||
CodeCache* lib = Profiler::instance()->findLibraryByAddress((void*)NativeLockTracer::initialize);
|
||||
assert(lib);
|
||||
|
||||
lib->mark(
|
||||
[](const char* s) -> bool {
|
||||
return strcmp(s, "pthread_mutex_lock_hook") == 0
|
||||
|| strcmp(s, "pthread_rwlock_rdlock_hook") == 0
|
||||
|| strcmp(s, "pthread_rwlock_wrlock_hook") == 0;
|
||||
},
|
||||
MARK_ASYNC_PROFILER);
|
||||
}
|
||||
|
||||
void NativeLockTracer::patchLibraries() {
|
||||
MutexLocker ml(_patch_lock);
|
||||
|
||||
CodeCacheArray* native_libs = Profiler::instance()->nativeLibs();
|
||||
int native_lib_count = native_libs->count();
|
||||
|
||||
while (_patched_libs < native_lib_count) {
|
||||
CodeCache* cc = (*native_libs)[_patched_libs++];
|
||||
|
||||
if (cc->contains((const void*)NativeLockTracer::initialize)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
UnloadProtection handle(cc);
|
||||
if (!handle.isValid()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cc->patchImport(im_pthread_mutex_lock, (void*)pthread_mutex_lock_hook);
|
||||
cc->patchImport(im_pthread_rwlock_rdlock, (void*)pthread_rwlock_rdlock_hook);
|
||||
cc->patchImport(im_pthread_rwlock_wrlock, (void*)pthread_rwlock_wrlock_hook);
|
||||
}
|
||||
}
|
||||
|
||||
void NativeLockTracer::recordNativeLock(void* address, u64 start_time, u64 end_time) {
|
||||
const u64 duration_ticks = end_time - start_time;
|
||||
if (updateCounter(_total_duration, duration_ticks, _interval)) {
|
||||
u64 duration_nanos = (u64)(duration_ticks * _ticks_to_nanos);
|
||||
NativeLockEvent event;
|
||||
event._start_time = start_time;
|
||||
event._end_time = end_time;
|
||||
event._address = (uintptr_t)address;
|
||||
|
||||
Profiler::instance()->recordSample(NULL, duration_nanos, NATIVE_LOCK_SAMPLE, &event);
|
||||
}
|
||||
}
|
||||
|
||||
Error NativeLockTracer::start(Arguments& args) {
|
||||
_ticks_to_nanos = 1e9 / TSC::frequency();
|
||||
_interval = (u64)(args._nativelock * (TSC::frequency() / 1e9));
|
||||
_total_duration = 0;
|
||||
|
||||
if (!_initialized) {
|
||||
initialize();
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
_running = true;
|
||||
patchLibraries();
|
||||
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
void NativeLockTracer::stop() {
|
||||
_running = false;
|
||||
}
|
||||
57
src/nativeLockTracer.h
Normal file
57
src/nativeLockTracer.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _NATIVELOCKTRACER_H
|
||||
#define _NATIVELOCKTRACER_H
|
||||
|
||||
#include "engine.h"
|
||||
#include "event.h"
|
||||
#include "mutex.h"
|
||||
|
||||
|
||||
class NativeLockTracer : public Engine {
|
||||
private:
|
||||
static u64 _interval;
|
||||
static double _ticks_to_nanos;
|
||||
|
||||
static Mutex _patch_lock;
|
||||
static int _patched_libs;
|
||||
static bool _initialized;
|
||||
static volatile bool _running;
|
||||
static volatile u64 _total_duration;
|
||||
|
||||
static void initialize();
|
||||
static void patchLibraries();
|
||||
|
||||
public:
|
||||
const char* type() {
|
||||
return "native_lock_tracer";
|
||||
}
|
||||
|
||||
const char* title() {
|
||||
return "Native lock profile";
|
||||
}
|
||||
|
||||
const char* units() {
|
||||
return "ns";
|
||||
}
|
||||
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
|
||||
static inline bool running() {
|
||||
return _running;
|
||||
}
|
||||
|
||||
static inline void installHooks() {
|
||||
if (running()) {
|
||||
patchLibraries();
|
||||
}
|
||||
}
|
||||
|
||||
static void recordNativeLock(void* address, u64 start_time, u64 end_time);
|
||||
};
|
||||
|
||||
#endif // _NATIVELOCKTRACER_H
|
||||
@@ -213,7 +213,7 @@ ThreadState OS::threadState(int thread_id) {
|
||||
|
||||
ThreadState state = THREAD_UNKNOWN;
|
||||
if (read(fd, buf, sizeof(buf)) > 0) {
|
||||
char* s = strchr(buf, ')');
|
||||
char* s = strrchr(buf, ')');
|
||||
state = s != NULL && (s[2] == 'R' || s[2] == 'D') ? THREAD_RUNNING : THREAD_SLEEPING;
|
||||
}
|
||||
|
||||
@@ -279,8 +279,9 @@ SigAction OS::replaceCrashHandler(SigAction action) {
|
||||
struct sigaction sa;
|
||||
sigaction(SIGSEGV, NULL, &sa);
|
||||
SigAction old_action = sa.sa_handler == SIG_DFL ? restoreSignalHandler : sa.sa_sigaction;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_sigaction = action;
|
||||
sa.sa_flags |= SA_SIGINFO | SA_RESTART;
|
||||
sa.sa_flags |= SA_SIGINFO | SA_RESTART | SA_NODEFER;
|
||||
sigaction(SIGSEGV, &sa, NULL);
|
||||
return old_action;
|
||||
}
|
||||
|
||||
@@ -247,14 +247,16 @@ SigAction OS::replaceCrashHandler(SigAction action) {
|
||||
|
||||
sigaction(SIGBUS, NULL, &sa);
|
||||
orig_sigbus_handler = sa.sa_handler == SIG_DFL ? restoreSignalHandler : sa.sa_sigaction;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_sigaction = action;
|
||||
sa.sa_flags |= SA_SIGINFO | SA_RESTART;
|
||||
sa.sa_flags |= SA_SIGINFO | SA_RESTART | SA_NODEFER;
|
||||
sigaction(SIGBUS, &sa, NULL);
|
||||
|
||||
sigaction(SIGSEGV, NULL, &sa);
|
||||
orig_sigsegv_handler = sa.sa_handler == SIG_DFL ? restoreSignalHandler : sa.sa_sigaction;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_sigaction = action;
|
||||
sa.sa_flags |= SA_SIGINFO | SA_RESTART;
|
||||
sa.sa_flags |= SA_SIGINFO | SA_RESTART| SA_NODEFER;
|
||||
sigaction(SIGSEGV, &sa, NULL);
|
||||
|
||||
// Return an action that dispatches to one of the original handlers depending on signo,
|
||||
@@ -372,8 +374,9 @@ int OS::createMemoryFile(const char* name) {
|
||||
}
|
||||
|
||||
void OS::copyFile(int src_fd, int dst_fd, off_t offset, size_t size) {
|
||||
char* buf = (char*)mmap(NULL, size + offset, PROT_READ, MAP_PRIVATE, src_fd, 0);
|
||||
if (buf == NULL) {
|
||||
size_t map_size = size + offset;
|
||||
char* buf = (char*)mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, src_fd, 0);
|
||||
if (buf == MAP_FAILED) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -386,7 +389,7 @@ void OS::copyFile(int src_fd, int dst_fd, off_t offset, size_t size) {
|
||||
size -= (size_t)bytes;
|
||||
}
|
||||
|
||||
munmap(buf, offset);
|
||||
munmap(buf, map_size);
|
||||
}
|
||||
|
||||
void OS::freePageCache(int fd, off_t start_offset) {
|
||||
|
||||
134
src/otlp.cpp
Normal file
134
src/otlp.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright The async-profiler authors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "callTraceStorage.h"
|
||||
#include "otlp.h"
|
||||
|
||||
namespace Otlp {
|
||||
|
||||
void Recorder::recordProfilesDictionary(const std::vector<CallTraceSample*>& call_trace_samples) {
|
||||
protobuf_mark_t dictionary_mark = _otlp_buffer.startMessage(ProfilesData::dictionary);
|
||||
|
||||
recordStacks(call_trace_samples);
|
||||
|
||||
// Write mapping_table. Not currently used, but required by some parsers
|
||||
protobuf_mark_t mapping_mark = _otlp_buffer.startMessage(ProfilesDictionary::mapping_table, 1);
|
||||
_otlp_buffer.commitMessage(mapping_mark);
|
||||
|
||||
// Write function_table
|
||||
_functions.forEachOrdered([&] (size_t idx, const std::string& function_name) {
|
||||
protobuf_mark_t function_mark = _otlp_buffer.startMessage(ProfilesDictionary::function_table, 1);
|
||||
_otlp_buffer.field(Function::name_strindex, _strings.indexOf(function_name));
|
||||
_otlp_buffer.commitMessage(function_mark);
|
||||
});
|
||||
|
||||
// Write location_table
|
||||
for (size_t function_idx = 0; function_idx < _functions.size(); ++function_idx) {
|
||||
protobuf_mark_t location_mark = _otlp_buffer.startMessage(ProfilesDictionary::location_table, 1);
|
||||
// TODO: set to the proper mapping when new mappings are added.
|
||||
// For now we keep a dummy default mapping_index for all locations because some parsers
|
||||
// would fail otherwise
|
||||
_otlp_buffer.field(Location::mapping_index, (u64)0);
|
||||
protobuf_mark_t line_mark = _otlp_buffer.startMessage(Location::lines, 1);
|
||||
_otlp_buffer.field(Line::function_index, function_idx);
|
||||
_otlp_buffer.commitMessage(line_mark);
|
||||
_otlp_buffer.commitMessage(location_mark);
|
||||
}
|
||||
|
||||
// Write attribute_table (only threads for now)
|
||||
if (!_thread_names.empty()) {
|
||||
size_t thread_name_key_strindex = _strings.indexOf(OTLP_THREAD_NAME);
|
||||
_thread_names.forEachOrdered([&] (size_t idx, const std::string& s) {
|
||||
protobuf_mark_t attr_mark = _otlp_buffer.startMessage(ProfilesDictionary::attribute_table);
|
||||
_otlp_buffer.field(KeyValueAndUnit::key_strindex, thread_name_key_strindex);
|
||||
protobuf_mark_t value_mark = _otlp_buffer.startMessage(KeyValueAndUnit::value);
|
||||
_otlp_buffer.field(AnyValue::string_value, s.data(), s.length());
|
||||
_otlp_buffer.commitMessage(value_mark);
|
||||
_otlp_buffer.commitMessage(attr_mark);
|
||||
});
|
||||
}
|
||||
|
||||
// Write string_table
|
||||
_strings.forEachOrdered([&] (size_t idx, const std::string& s) {
|
||||
_otlp_buffer.field(ProfilesDictionary::string_table, s.data(), s.length());
|
||||
});
|
||||
|
||||
_otlp_buffer.commitMessage(dictionary_mark);
|
||||
}
|
||||
|
||||
void Recorder::recordStacks(const std::vector<CallTraceSample*>& call_trace_samples) {
|
||||
{
|
||||
// stack_table[0] must always be zero value (Stack{}) and present.
|
||||
protobuf_mark_t stack_mark = _otlp_buffer.startMessage(ProfilesDictionary::stack_table);
|
||||
_otlp_buffer.commitMessage(stack_mark);
|
||||
}
|
||||
|
||||
for (const auto& cts : call_trace_samples) {
|
||||
CallTrace* trace = cts->acquireTrace();
|
||||
if (trace == NULL || _fn.excludeTrace(trace) || cts->samples == 0) continue;
|
||||
|
||||
protobuf_mark_t stack_mark = _otlp_buffer.startMessage(ProfilesDictionary::stack_table);
|
||||
protobuf_mark_t location_indices_mark = _otlp_buffer.startMessage(Stack::location_indices);
|
||||
size_t thread_name_index_value = 0;
|
||||
for (int j = 0; j < trace->num_frames; j++) {
|
||||
if (trace->frames[j].bci == BCI_THREAD_ID) {
|
||||
thread_name_index_value = _thread_names.indexOf(_fn.name(trace->frames[j]));
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t location_idx = _functions.indexOf(_fn.name(trace->frames[j]));
|
||||
_otlp_buffer.putVarInt(location_idx);
|
||||
}
|
||||
_otlp_buffer.commitMessage(location_indices_mark);
|
||||
_otlp_buffer.commitMessage(stack_mark);
|
||||
|
||||
_samples_info.push_back(SampleInfo{cts->samples, cts->counter, thread_name_index_value});
|
||||
}
|
||||
}
|
||||
|
||||
void Recorder::recordSampleType(size_t type_strindex, size_t unit_strindex) {
|
||||
protobuf_mark_t sample_type_mark = _otlp_buffer.startMessage(Profile::sample_type, 1);
|
||||
_otlp_buffer.field(ValueType::type_strindex, type_strindex);
|
||||
_otlp_buffer.field(ValueType::unit_strindex, unit_strindex);
|
||||
_otlp_buffer.commitMessage(sample_type_mark);
|
||||
}
|
||||
|
||||
void Recorder::recordOtlpProfile(size_t type_strindex, size_t unit_strindex, bool samples) {
|
||||
protobuf_mark_t profile_mark = _otlp_buffer.startMessage(ScopeProfiles::profiles);
|
||||
|
||||
_otlp_buffer.fieldFixed64(Profile::time_unix_nano, _start_nanos);
|
||||
_otlp_buffer.field(Profile::duration_nano, _duration_nanos);
|
||||
|
||||
recordSampleType(type_strindex, unit_strindex);
|
||||
|
||||
for (size_t i = 0; i < _samples_info.size(); ++i) {
|
||||
const SampleInfo& si = _samples_info[i];
|
||||
protobuf_mark_t sample_mark = _otlp_buffer.startMessage(Profile::samples, 1);
|
||||
// stack_table[0] is the empty stack
|
||||
_otlp_buffer.field(Sample::stack_index, i + 1);
|
||||
if (si.thread_name_index != 0) {
|
||||
_otlp_buffer.field(Sample::attribute_indices, si.thread_name_index);
|
||||
}
|
||||
_otlp_buffer.field(Sample::values, samples ? si.samples : si.counter);
|
||||
_otlp_buffer.commitMessage(sample_mark);
|
||||
}
|
||||
|
||||
_otlp_buffer.commitMessage(profile_mark);
|
||||
}
|
||||
|
||||
void Recorder::record(const std::vector<CallTraceSample*>& call_trace_samples, bool samples) {
|
||||
recordProfilesDictionary(call_trace_samples);
|
||||
|
||||
protobuf_mark_t resource_profiles_mark = _otlp_buffer.startMessage(ProfilesData::resource_profiles);
|
||||
protobuf_mark_t scope_profiles_mark = _otlp_buffer.startMessage(ResourceProfiles::scope_profiles);
|
||||
|
||||
size_t unit_strindex = samples ? _count_strindex : _engine_unit_strindex;
|
||||
recordOtlpProfile(_engine_type_strindex, unit_strindex, samples);
|
||||
|
||||
_otlp_buffer.commitMessage(scope_profiles_mark);
|
||||
_otlp_buffer.commitMessage(resource_profiles_mark);
|
||||
}
|
||||
|
||||
}
|
||||
91
src/otlp.h
91
src/otlp.h
@@ -6,7 +6,14 @@
|
||||
#ifndef _OTLP_H
|
||||
#define _OTLP_H
|
||||
|
||||
#include "engine.h"
|
||||
#include "frameName.h"
|
||||
#include "index.h"
|
||||
#include "protobuf.h"
|
||||
#include "writer.h"
|
||||
#include <vector>
|
||||
|
||||
struct CallTraceSample;
|
||||
|
||||
namespace Otlp {
|
||||
|
||||
@@ -20,6 +27,7 @@ namespace ProfilesDictionary {
|
||||
const protobuf_index_t function_table = 3;
|
||||
const protobuf_index_t string_table = 5;
|
||||
const protobuf_index_t attribute_table = 6;
|
||||
const protobuf_index_t stack_table = 7;
|
||||
}
|
||||
|
||||
namespace ProfilesData {
|
||||
@@ -37,12 +45,9 @@ namespace ScopeProfiles {
|
||||
|
||||
namespace Profile {
|
||||
const protobuf_index_t sample_type = 1;
|
||||
const protobuf_index_t sample = 2;
|
||||
const protobuf_index_t location_indices = 3;
|
||||
const protobuf_index_t time_nanos = 4;
|
||||
const protobuf_index_t duration_nanos = 5;
|
||||
const protobuf_index_t period_type = 6;
|
||||
const protobuf_index_t period = 7;
|
||||
const protobuf_index_t samples = 2;
|
||||
const protobuf_index_t time_unix_nano = 3;
|
||||
const protobuf_index_t duration_nano = 4;
|
||||
}
|
||||
|
||||
namespace ValueType {
|
||||
@@ -52,15 +57,18 @@ namespace ValueType {
|
||||
}
|
||||
|
||||
namespace Sample {
|
||||
const protobuf_index_t locations_start_index = 1;
|
||||
const protobuf_index_t locations_length = 2;
|
||||
const protobuf_index_t value = 3;
|
||||
const protobuf_index_t attribute_indices = 4;
|
||||
const protobuf_index_t stack_index = 1;
|
||||
const protobuf_index_t values = 2;
|
||||
const protobuf_index_t attribute_indices = 3;
|
||||
}
|
||||
|
||||
namespace Stack {
|
||||
const protobuf_index_t location_indices = 1;
|
||||
}
|
||||
|
||||
namespace Location {
|
||||
const protobuf_index_t mapping_index = 1;
|
||||
const protobuf_index_t line = 3;
|
||||
const protobuf_index_t lines = 3;
|
||||
}
|
||||
|
||||
namespace Function {
|
||||
@@ -72,21 +80,62 @@ namespace Line {
|
||||
const protobuf_index_t function_index = 1;
|
||||
}
|
||||
|
||||
namespace AggregationTemporality {
|
||||
const u64 unspecified = 0;
|
||||
const u64 delta = 1;
|
||||
const u64 cumulative = 2;
|
||||
}
|
||||
|
||||
namespace Key {
|
||||
const u64 key = 1;
|
||||
const u64 value = 2;
|
||||
namespace KeyValueAndUnit {
|
||||
const protobuf_index_t key_strindex = 1;
|
||||
const protobuf_index_t value = 2;
|
||||
}
|
||||
|
||||
namespace AnyValue {
|
||||
const u64 string_value = 1;
|
||||
const protobuf_index_t string_value = 1;
|
||||
}
|
||||
|
||||
struct SampleInfo {
|
||||
u64 samples;
|
||||
u64 counter;
|
||||
size_t thread_name_index;
|
||||
};
|
||||
|
||||
class Recorder {
|
||||
private:
|
||||
ProtoBuffer _otlp_buffer;
|
||||
FrameName& _fn;
|
||||
Index _thread_names;
|
||||
Index _functions;
|
||||
Index _strings;
|
||||
std::vector<SampleInfo> _samples_info;
|
||||
const u64 _start_nanos;
|
||||
const u64 _duration_nanos;
|
||||
const size_t _engine_type_strindex;
|
||||
const size_t _engine_unit_strindex;
|
||||
const size_t _count_strindex;
|
||||
|
||||
// Record a profile with a specified sample type
|
||||
void recordOtlpProfile(size_t type_strindex, size_t unit_strindex, bool samples);
|
||||
void recordSampleType(size_t type_strindex, size_t unit_strindex);
|
||||
void recordStacks(const std::vector<CallTraceSample*>& call_trace_samples);
|
||||
void recordProfilesDictionary(const std::vector<CallTraceSample*>& call_trace_samples);
|
||||
|
||||
public:
|
||||
Recorder(Engine* engine, FrameName& fn, u64 start_nanos, u64 duration_nanos) :
|
||||
_otlp_buffer(Otlp::OTLP_BUFFER_INITIAL_SIZE),
|
||||
_fn(fn),
|
||||
_thread_names(),
|
||||
_functions(),
|
||||
_strings(),
|
||||
_samples_info(),
|
||||
_engine_type_strindex(_strings.indexOf(engine->type())),
|
||||
_engine_unit_strindex(_strings.indexOf(engine->units())),
|
||||
_count_strindex(_strings.indexOf("count")),
|
||||
_start_nanos(start_nanos),
|
||||
_duration_nanos(duration_nanos) {}
|
||||
|
||||
void record(const std::vector<CallTraceSample*>& call_trace_samples, bool samples);
|
||||
|
||||
void write(Writer& out) {
|
||||
out.write((const char*) _otlp_buffer.data(), _otlp_buffer.offset());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // _OTLP_H
|
||||
|
||||
@@ -20,8 +20,10 @@ class PerfEvents : public CpuEngine {
|
||||
static int _max_events;
|
||||
static PerfEvent* _events;
|
||||
static PerfEventType* _event_type;
|
||||
static int _ioc_enable;
|
||||
static bool _alluser;
|
||||
static bool _kernel_stack;
|
||||
static bool _use_perf_mmap;
|
||||
static bool _record_cpu;
|
||||
static int _target_cpu;
|
||||
|
||||
@@ -33,7 +35,6 @@ class PerfEvents : public CpuEngine {
|
||||
void destroyForThread(int tid);
|
||||
|
||||
public:
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args);
|
||||
void stop();
|
||||
|
||||
@@ -44,7 +45,7 @@ class PerfEvents : public CpuEngine {
|
||||
const char* title();
|
||||
const char* units();
|
||||
|
||||
static int walk(int tid, void* ucontext, const void** callchain, int max_depth, StackContext* java_ctx);
|
||||
static int walk(int tid, void* ucontext, const void** callchain, int max_depth, u64* cpu);
|
||||
static void resetBuffer(int tid);
|
||||
|
||||
static bool supported();
|
||||
@@ -57,15 +58,11 @@ class StackContext;
|
||||
|
||||
class PerfEvents : public CpuEngine {
|
||||
public:
|
||||
Error check(Arguments& args) {
|
||||
return Error("PerfEvents are not supported on this platform");
|
||||
}
|
||||
|
||||
Error start(Arguments& args) {
|
||||
return Error("PerfEvents are not supported on this platform");
|
||||
}
|
||||
|
||||
static int walk(int tid, void* ucontext, const void** callchain, int max_depth, StackContext* java_ctx) {
|
||||
static int walk(int tid, void* ucontext, const void** callchain, int max_depth, u64* cpu) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <sys/resource.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <linux/perf_event.h>
|
||||
#include "arch.h"
|
||||
#include "fdtransferClient.h"
|
||||
@@ -57,6 +58,23 @@ enum {
|
||||
HW_BREAKPOINT_X = 4
|
||||
};
|
||||
|
||||
struct PerfCounter {
|
||||
u64 value;
|
||||
u64 time_enabled; /* PERF_FORMAT_TOTAL_TIME_ENABLED */
|
||||
u64 time_running; /* PERF_FORMAT_TOTAL_TIME_RUNNING */
|
||||
};
|
||||
|
||||
// Per-FD struct for storing perf-event multiplexing data
|
||||
struct MultiplexState {
|
||||
u64 time_enabled; /* stores previous time_enabled */
|
||||
u64 time_running; /* stores previous time_running */
|
||||
};
|
||||
|
||||
static const unsigned int MAX_MULTIPLEXED_FD = 65536;
|
||||
|
||||
static MultiplexState multiplex_state[MAX_MULTIPLEXED_FD];
|
||||
static bool multiplex_state_dirty = false;
|
||||
|
||||
static int fetchInt(const char* file_name) {
|
||||
int fd = open(file_name, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
@@ -153,6 +171,17 @@ static void adjustFDLimit() {
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for the kernel bug: PERF_EVENT_IOC_REFRESH can hang
|
||||
// the entire system on Linux 6.16.x and 6.17.x.
|
||||
// See https://github.com/async-profiler/async-profiler/issues/1578
|
||||
static bool hasPerfEventRefreshBug() {
|
||||
static struct utsname u{};
|
||||
if (u.release[0] == 0 && uname(&u) != 0) {
|
||||
return false;
|
||||
}
|
||||
return strncmp(u.release, "6.16.", 5) == 0 || strncmp(u.release, "6.17.", 5) == 0;
|
||||
}
|
||||
|
||||
struct FunctionWithCounter {
|
||||
const char* name;
|
||||
int counter_arg;
|
||||
@@ -169,7 +198,7 @@ struct PerfEventType {
|
||||
|
||||
enum {
|
||||
IDX_CPU = 0,
|
||||
IDX_PREDEFINED = 12,
|
||||
IDX_PREDEFINED = 13,
|
||||
IDX_RAW,
|
||||
IDX_PMU,
|
||||
IDX_BREAKPOINT,
|
||||
@@ -460,6 +489,7 @@ PerfEventType PerfEventType::AVAILABLE_EVENTS[] = {
|
||||
{"branch-instructions", 1000000, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS},
|
||||
{"branch-misses", 1000, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES},
|
||||
{"bus-cycles", 1000000, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES},
|
||||
{"ref-cycles", 1000000, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES},
|
||||
|
||||
{"L1-dcache-load-misses", 1000000, PERF_TYPE_HW_CACHE, LOAD_MISS(PERF_COUNT_HW_CACHE_L1D)},
|
||||
{"LLC-load-misses", 1000, PERF_TYPE_HW_CACHE, LOAD_MISS(PERF_COUNT_HW_CACHE_LL)},
|
||||
@@ -532,8 +562,10 @@ class PerfEvent : public SpinLock {
|
||||
int PerfEvents::_max_events = 0;
|
||||
PerfEvent* PerfEvents::_events = NULL;
|
||||
PerfEventType* PerfEvents::_event_type = NULL;
|
||||
int PerfEvents::_ioc_enable;
|
||||
bool PerfEvents::_alluser;
|
||||
bool PerfEvents::_kernel_stack;
|
||||
bool PerfEvents::_use_perf_mmap;
|
||||
bool PerfEvents::_record_cpu;
|
||||
int PerfEvents::_target_cpu;
|
||||
|
||||
@@ -572,6 +604,9 @@ int PerfEvents::createForThread(int tid) {
|
||||
attr.disabled = 1;
|
||||
attr.wakeup_events = 1;
|
||||
|
||||
// flags for multiplexing support
|
||||
attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
|
||||
|
||||
if (_alluser) {
|
||||
attr.exclude_kernel = 1;
|
||||
}
|
||||
@@ -584,16 +619,6 @@ int PerfEvents::createForThread(int tid) {
|
||||
attr.exclude_callchain_user = 1;
|
||||
}
|
||||
|
||||
#ifdef PERF_ATTR_SIZE_VER5
|
||||
if (_cstack == CSTACK_LBR) {
|
||||
attr.sample_type |= PERF_SAMPLE_BRANCH_STACK | PERF_SAMPLE_REGS_USER;
|
||||
attr.branch_sample_type = PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_CALL_STACK;
|
||||
attr.sample_regs_user = 1ULL << PERF_REG_PC;
|
||||
}
|
||||
#else
|
||||
#warning "Compiling without LBR support. Kernel headers 4.1+ required"
|
||||
#endif
|
||||
|
||||
if (_record_cpu) {
|
||||
attr.sample_type |= PERF_SAMPLE_CPU;
|
||||
}
|
||||
@@ -621,7 +646,7 @@ int PerfEvents::createForThread(int tid) {
|
||||
}
|
||||
|
||||
void* page = NULL;
|
||||
if (_kernel_stack || _cstack == CSTACK_DEFAULT || _cstack == CSTACK_LBR) {
|
||||
if (_use_perf_mmap) {
|
||||
page = mmap(NULL, 2 * OS::page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (page == MAP_FAILED) {
|
||||
Log::warn("perf_event mmap failed: %s", strerror(errno));
|
||||
@@ -633,6 +658,11 @@ int PerfEvents::createForThread(int tid) {
|
||||
_events[tid]._fd = fd;
|
||||
_events[tid]._page = (struct perf_event_mmap_page*)page;
|
||||
|
||||
if (multiplex_state_dirty && fd < MAX_MULTIPLEXED_FD) {
|
||||
multiplex_state[fd].time_enabled = 0;
|
||||
multiplex_state[fd].time_running = 0;
|
||||
}
|
||||
|
||||
struct f_owner_ex ex;
|
||||
ex.type = F_OWNER_TID;
|
||||
ex.pid = tid;
|
||||
@@ -641,7 +671,7 @@ int PerfEvents::createForThread(int tid) {
|
||||
if (fcntl(fd, F_SETFL, O_ASYNC) < 0 || fcntl(fd, F_SETSIG, _signal) < 0 || fcntl(fd, F_SETOWN_EX, &ex) < 0) {
|
||||
err = errno;
|
||||
Log::warn("perf_event fcntl failed: %s", strerror(err));
|
||||
} else if (ioctl(fd, PERF_EVENT_IOC_RESET, 0) < 0 || ioctl(fd, PERF_EVENT_IOC_REFRESH, 1) < 0) {
|
||||
} else if (ioctl(fd, PERF_EVENT_IOC_RESET, 0) < 0 || ioctl(fd, _ioc_enable, 1) < 0) {
|
||||
err = errno;
|
||||
Log::warn("perf_event ioctl failed: %s", strerror(err));
|
||||
} else {
|
||||
@@ -685,8 +715,36 @@ u64 PerfEvents::readCounter(siginfo_t* siginfo, void* ucontext) {
|
||||
case 3: return StackFrame(ucontext).arg2();
|
||||
case 4: return StackFrame(ucontext).arg3();
|
||||
default: {
|
||||
u64 counter;
|
||||
return read(siginfo->si_fd, &counter, sizeof(counter)) == sizeof(counter) ? counter : 1;
|
||||
// Read counter with multiplexing metadata for accurate scaling
|
||||
struct PerfCounter counter;
|
||||
if (read(siginfo->si_fd, &counter, sizeof(counter)) == sizeof(counter)) {
|
||||
u64 current_val = counter.value;
|
||||
if (counter.time_enabled > counter.time_running) {
|
||||
int fd = siginfo->si_fd;
|
||||
if (fd < MAX_MULTIPLEXED_FD) {
|
||||
u64 delta_enabled = counter.time_enabled - multiplex_state[fd].time_enabled;
|
||||
u64 delta_running = counter.time_running - multiplex_state[fd].time_running;
|
||||
|
||||
multiplex_state[fd].time_enabled = counter.time_enabled;
|
||||
multiplex_state[fd].time_running = counter.time_running;
|
||||
|
||||
if (!multiplex_state_dirty) {
|
||||
multiplex_state_dirty = true;
|
||||
}
|
||||
|
||||
if (delta_running > 0 && delta_enabled > delta_running) {
|
||||
// scaled counter = (counter) * (delta_enabled / delta_running)
|
||||
double ratio = (double)delta_enabled / delta_running;
|
||||
return (u64)(current_val * ratio);
|
||||
}
|
||||
} else if (counter.time_running > 0) {
|
||||
double ratio = (double)counter.time_enabled / counter.time_running;
|
||||
return (u64)(current_val * ratio);
|
||||
}
|
||||
}
|
||||
return current_val;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -697,6 +755,10 @@ void PerfEvents::signalHandler(int signo, siginfo_t* siginfo, void* ucontext) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ioc_enable == PERF_EVENT_IOC_ENABLE) {
|
||||
ioctl(siginfo->si_fd, PERF_EVENT_IOC_DISABLE, 0);
|
||||
}
|
||||
|
||||
if (_enabled) {
|
||||
ExecutionEvent event(TSC::ticks());
|
||||
u64 counter = readCounter(siginfo, ucontext);
|
||||
@@ -706,7 +768,7 @@ void PerfEvents::signalHandler(int signo, siginfo_t* siginfo, void* ucontext) {
|
||||
}
|
||||
|
||||
ioctl(siginfo->si_fd, PERF_EVENT_IOC_RESET, 0);
|
||||
ioctl(siginfo->si_fd, PERF_EVENT_IOC_REFRESH, 1);
|
||||
ioctl(siginfo->si_fd, _ioc_enable, 1);
|
||||
}
|
||||
|
||||
void PerfEvents::signalHandlerJ9(int signo, siginfo_t* siginfo, void* ucontext) {
|
||||
@@ -715,18 +777,22 @@ void PerfEvents::signalHandlerJ9(int signo, siginfo_t* siginfo, void* ucontext)
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ioc_enable == PERF_EVENT_IOC_ENABLE) {
|
||||
ioctl(siginfo->si_fd, PERF_EVENT_IOC_DISABLE, 0);
|
||||
}
|
||||
|
||||
if (_enabled) {
|
||||
u64 counter = readCounter(siginfo, ucontext);
|
||||
J9StackTraceNotification notif;
|
||||
StackContext java_ctx;
|
||||
notif.num_frames = _cstack == CSTACK_NO ? 0 : walk(OS::threadId(), ucontext, notif.addr, MAX_J9_NATIVE_FRAMES, &java_ctx);
|
||||
u64 cpu = 0;
|
||||
notif.num_frames = _cstack == CSTACK_NO ? 0 : walk(OS::threadId(), ucontext, notif.addr, MAX_J9_NATIVE_FRAMES, &cpu);
|
||||
J9StackTraces::checkpoint(counter, ¬if);
|
||||
} else {
|
||||
resetBuffer(OS::threadId());
|
||||
}
|
||||
|
||||
ioctl(siginfo->si_fd, PERF_EVENT_IOC_RESET, 0);
|
||||
ioctl(siginfo->si_fd, PERF_EVENT_IOC_REFRESH, 1);
|
||||
ioctl(siginfo->si_fd, _ioc_enable, 1);
|
||||
}
|
||||
|
||||
const char* PerfEvents::title() {
|
||||
@@ -743,59 +809,6 @@ const char* PerfEvents::units() {
|
||||
return _event_type == NULL || strcmp(_event_type->name, "cpu-clock") == 0 ? "ns" : "total";
|
||||
}
|
||||
|
||||
Error PerfEvents::check(Arguments& args) {
|
||||
PerfEventType* event_type = PerfEventType::forName(args._event);
|
||||
if (event_type == NULL) {
|
||||
return Error("Unsupported event type");
|
||||
} else if (event_type->counter_arg > 4) {
|
||||
return Error("Only arguments 1-4 can be counted");
|
||||
}
|
||||
|
||||
if (!setupThreadHook()) {
|
||||
return Error("Could not set pthread hook");
|
||||
}
|
||||
|
||||
struct perf_event_attr attr = {0};
|
||||
attr.size = sizeof(attr);
|
||||
attr.type = event_type->type;
|
||||
|
||||
if (attr.type == PERF_TYPE_BREAKPOINT) {
|
||||
attr.bp_type = event_type->config;
|
||||
} else {
|
||||
attr.config = event_type->config;
|
||||
}
|
||||
attr.config1 = event_type->config1;
|
||||
attr.config2 = event_type->config2;
|
||||
|
||||
attr.sample_period = event_type->default_interval;
|
||||
attr.sample_type = PERF_SAMPLE_CALLCHAIN;
|
||||
attr.disabled = 1;
|
||||
|
||||
if (args._alluser) {
|
||||
attr.exclude_kernel = 1;
|
||||
}
|
||||
|
||||
#ifdef PERF_ATTR_SIZE_VER5
|
||||
if (args._cstack == CSTACK_LBR) {
|
||||
attr.sample_type |= PERF_SAMPLE_BRANCH_STACK | PERF_SAMPLE_REGS_USER;
|
||||
attr.branch_sample_type = PERF_SAMPLE_BRANCH_USER | PERF_SAMPLE_BRANCH_CALL_STACK;
|
||||
attr.sample_regs_user = 1ULL << PERF_REG_PC;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (args._record_cpu) {
|
||||
attr.sample_type |= PERF_SAMPLE_CPU;
|
||||
}
|
||||
|
||||
int fd = syscall(__NR_perf_event_open, &attr, 0, args._target_cpu, -1, 0);
|
||||
if (fd == -1) {
|
||||
return Error(strerror(errno));
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error PerfEvents::start(Arguments& args) {
|
||||
_event_type = PerfEventType::forName(args._event);
|
||||
if (_event_type == NULL) {
|
||||
@@ -829,6 +842,14 @@ Error PerfEvents::start(Arguments& args) {
|
||||
// Automatically switch on alluser for non-CPU events, if kernel profiling is unavailable
|
||||
_alluser = strcmp(args._event, EVENT_CPU) != 0 && !supported();
|
||||
}
|
||||
_use_perf_mmap = _kernel_stack || _cstack == CSTACK_DEFAULT || _record_cpu;
|
||||
|
||||
if (strcmp(_event_type->name, "cpu-clock") == 0 && hasPerfEventRefreshBug()) {
|
||||
Log::debug("Enable workaround for PERF_EVENT_IOC_REFRESH bug");
|
||||
_ioc_enable = PERF_EVENT_IOC_ENABLE; // opt-in for manual enable/disable
|
||||
} else {
|
||||
_ioc_enable = PERF_EVENT_IOC_REFRESH; // autodisable perf_event on counter overflow
|
||||
}
|
||||
|
||||
adjustFDLimit();
|
||||
|
||||
@@ -875,7 +896,7 @@ void PerfEvents::stop() {
|
||||
J9StackTraces::stop();
|
||||
}
|
||||
|
||||
int PerfEvents::walk(int tid, void* ucontext, const void** callchain, int max_depth, StackContext* java_ctx) {
|
||||
int PerfEvents::walk(int tid, void* ucontext, const void** callchain, int max_depth, u64* cpu) {
|
||||
PerfEvent* event = &_events[tid];
|
||||
if (!event->tryLock()) {
|
||||
return 0; // the event is being destroyed
|
||||
@@ -896,7 +917,7 @@ int PerfEvents::walk(int tid, void* ucontext, const void** callchain, int max_de
|
||||
|
||||
if (hdr->type == PERF_RECORD_SAMPLE) {
|
||||
if (_record_cpu) {
|
||||
java_ctx->cpu = ring.next();
|
||||
*cpu = ring.next();
|
||||
}
|
||||
|
||||
u64 nr = ring.next();
|
||||
@@ -906,43 +927,12 @@ int PerfEvents::walk(int tid, void* ucontext, const void** callchain, int max_de
|
||||
const void* iptr = (const void*)ip;
|
||||
if (CodeHeap::contains(iptr) || depth >= max_depth) {
|
||||
// Stop at the first Java frame
|
||||
java_ctx->pc = iptr;
|
||||
goto stack_complete;
|
||||
}
|
||||
callchain[depth++] = iptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (_cstack == CSTACK_LBR) {
|
||||
u64 bnr = ring.next();
|
||||
|
||||
// Last userspace PC is stored right after branch stack
|
||||
const void* pc = (const void*)ring.peek(bnr * 3 + 2);
|
||||
if (CodeHeap::contains(pc) || depth >= max_depth) {
|
||||
java_ctx->pc = pc;
|
||||
goto stack_complete;
|
||||
}
|
||||
callchain[depth++] = pc;
|
||||
|
||||
while (bnr-- > 0) {
|
||||
const void* from = (const void*)ring.next();
|
||||
const void* to = (const void*)ring.next();
|
||||
ring.next();
|
||||
|
||||
if (CodeHeap::contains(to) || depth >= max_depth) {
|
||||
java_ctx->pc = to;
|
||||
goto stack_complete;
|
||||
}
|
||||
callchain[depth++] = to;
|
||||
|
||||
if (CodeHeap::contains(from) || depth >= max_depth) {
|
||||
java_ctx->pc = from;
|
||||
goto stack_complete;
|
||||
}
|
||||
callchain[depth++] = from;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
tail += hdr->size;
|
||||
@@ -955,9 +945,9 @@ stack_complete:
|
||||
event->unlock();
|
||||
|
||||
if (_cstack == CSTACK_FP) {
|
||||
depth += StackWalker::walkFP(ucontext, callchain + depth, max_depth - depth, java_ctx);
|
||||
depth += StackWalker::walkFP(ucontext, callchain + depth, max_depth - depth);
|
||||
} else if (_cstack == CSTACK_DWARF) {
|
||||
depth += StackWalker::walkDwarf(ucontext, callchain + depth, max_depth - depth, java_ctx);
|
||||
depth += StackWalker::walkDwarf(ucontext, callchain + depth, max_depth - depth);
|
||||
}
|
||||
|
||||
return depth;
|
||||
|
||||
@@ -76,4 +76,4 @@ bool ProcessSampler::getProcessInfo(int pid_index, u64 sampling_time, ProcessInf
|
||||
const int pid = _pids[pid_index];
|
||||
return OS::getBasicProcessInfo(pid, &info) && populateCpuPercent(info, sampling_time) &&
|
||||
shouldIncludeProcess(info) && OS::getDetailedProcessInfo(&info);
|
||||
}
|
||||
}
|
||||
|
||||
648
src/profiler.cpp
648
src/profiler.cpp
@@ -20,6 +20,7 @@
|
||||
#include "allocTracer.h"
|
||||
#include "mallocTracer.h"
|
||||
#include "lockTracer.h"
|
||||
#include "nativeLockTracer.h"
|
||||
#include "wallClock.h"
|
||||
#include "j9ObjectSampler.h"
|
||||
#include "j9StackTraces.h"
|
||||
@@ -56,6 +57,7 @@ static PerfEvents perf_events;
|
||||
static AllocTracer alloc_tracer;
|
||||
static MallocTracer malloc_tracer;
|
||||
static LockTracer lock_tracer;
|
||||
static NativeLockTracer native_lock_tracer;
|
||||
static ObjectSampler object_sampler;
|
||||
static J9ObjectSampler j9_object_sampler;
|
||||
static WallClock wall_clock;
|
||||
@@ -66,7 +68,6 @@ static Instrument instrument;
|
||||
|
||||
static ProfilingWindow profiling_window;
|
||||
|
||||
|
||||
struct MethodSample {
|
||||
u64 samples;
|
||||
u64 counter;
|
||||
@@ -86,19 +87,16 @@ static bool sortByCounter(const NamedMethodSample& a, const NamedMethodSample& b
|
||||
|
||||
static inline int hasNativeStack(EventType event_type) {
|
||||
const int events_with_native_stack =
|
||||
(1 << PERF_SAMPLE) |
|
||||
(1 << EXECUTION_SAMPLE) |
|
||||
(1 << WALL_CLOCK_SAMPLE) |
|
||||
(1 << MALLOC_SAMPLE) |
|
||||
(1 << ALLOC_SAMPLE) |
|
||||
(1 << PERF_SAMPLE) |
|
||||
(1 << EXECUTION_SAMPLE) |
|
||||
(1 << WALL_CLOCK_SAMPLE) |
|
||||
(1 << NATIVE_LOCK_SAMPLE) |
|
||||
(1 << MALLOC_SAMPLE) |
|
||||
(1 << ALLOC_SAMPLE) |
|
||||
(1 << ALLOC_OUTSIDE_TLAB);
|
||||
return (1 << event_type) & events_with_native_stack;
|
||||
}
|
||||
|
||||
static inline bool isVTableStub(const char* name) {
|
||||
return name[0] && strcmp(name + 1, "table stub") == 0;
|
||||
}
|
||||
|
||||
static inline int makeFrame(ASGCT_CallFrame* frames, jint type, jmethodID id) {
|
||||
frames[0].bci = type;
|
||||
frames[0].method_id = id;
|
||||
@@ -119,15 +117,16 @@ void Profiler::addJavaMethod(const void* address, int length, jmethodID method)
|
||||
}
|
||||
|
||||
void Profiler::addRuntimeStub(const void* address, int length, const char* name) {
|
||||
if (startsWith(name, "Stub Generator ")) {
|
||||
name += 15; // useless prefix introduced with JDK-8336658
|
||||
} else if (streq(name, "Interpreter")) {
|
||||
CodeHeap::setInterpreterStart(address);
|
||||
}
|
||||
|
||||
_stubs_lock.lock();
|
||||
_runtime_stubs.add(address, length, name, true);
|
||||
_stubs_lock.unlock();
|
||||
|
||||
if (strcmp(name, "call_stub") == 0) {
|
||||
_call_stub_begin = address;
|
||||
_call_stub_end = (const char*)address + length;
|
||||
}
|
||||
|
||||
CodeHeap::updateBounds(address, (const char*)address + length);
|
||||
}
|
||||
|
||||
@@ -291,38 +290,19 @@ CodeBlob* Profiler::findRuntimeStub(const void* address) {
|
||||
return _runtime_stubs.findBlobByAddress(address);
|
||||
}
|
||||
|
||||
bool Profiler::isAddressInCode(const void* pc) {
|
||||
if (CodeHeap::contains(pc)) {
|
||||
return CodeHeap::findNMethod(pc) != NULL && !(pc >= _call_stub_begin && pc < _call_stub_end);
|
||||
} else {
|
||||
return findLibraryByAddress(pc) != NULL;
|
||||
}
|
||||
}
|
||||
|
||||
jmethodID Profiler::getCurrentCompileTask() {
|
||||
VMThread* vm_thread = VMThread::current();
|
||||
if (vm_thread != NULL) {
|
||||
VMMethod* method = vm_thread->compiledMethod();
|
||||
if (method != NULL) {
|
||||
return method->id();
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int Profiler::getNativeTrace(void* ucontext, ASGCT_CallFrame* frames, EventType event_type, int tid, StackContext* java_ctx) {
|
||||
int Profiler::getNativeTrace(void* ucontext, ASGCT_CallFrame* frames, EventType event_type, int tid, u64* cpu) {
|
||||
const void* callchain[MAX_NATIVE_FRAMES];
|
||||
int native_frames;
|
||||
|
||||
// Use PerfEvents stack walker for execution samples, or basic stack walker for other events
|
||||
if (event_type == PERF_SAMPLE) {
|
||||
native_frames = PerfEvents::walk(tid, ucontext, callchain, MAX_NATIVE_FRAMES, java_ctx);
|
||||
native_frames = PerfEvents::walk(tid, ucontext, callchain, MAX_NATIVE_FRAMES, cpu);
|
||||
} else if (_cstack == CSTACK_VM) {
|
||||
return 0;
|
||||
} else if (_cstack == CSTACK_DWARF) {
|
||||
native_frames = StackWalker::walkDwarf(ucontext, callchain, MAX_NATIVE_FRAMES, java_ctx);
|
||||
native_frames = StackWalker::walkDwarf(ucontext, callchain, MAX_NATIVE_FRAMES);
|
||||
} else {
|
||||
native_frames = StackWalker::walkFP(ucontext, callchain, MAX_NATIVE_FRAMES, java_ctx);
|
||||
native_frames = StackWalker::walkFP(ucontext, callchain, MAX_NATIVE_FRAMES);
|
||||
}
|
||||
|
||||
return convertNativeTrace(native_frames, callchain, frames, event_type);
|
||||
@@ -330,7 +310,6 @@ int Profiler::getNativeTrace(void* ucontext, ASGCT_CallFrame* frames, EventType
|
||||
|
||||
int Profiler::convertNativeTrace(int native_frames, const void** callchain, ASGCT_CallFrame* frames, EventType event_type) {
|
||||
int depth = 0;
|
||||
jmethodID prev_method = NULL;
|
||||
|
||||
for (int i = 0; i < native_frames; i++) {
|
||||
const char* current_method_name = findNativeMethod(callchain[i]);
|
||||
@@ -340,39 +319,25 @@ int Profiler::convertNativeTrace(int native_frames, const void** callchain, ASGC
|
||||
// Skip all internal frames above VM runtime entry for allocation samples
|
||||
depth = 0;
|
||||
continue;
|
||||
} else if (mark == MARK_ASYNC_PROFILER && event_type == MALLOC_SAMPLE) {
|
||||
} else if (mark == MARK_ASYNC_PROFILER && (event_type == MALLOC_SAMPLE || event_type == NATIVE_LOCK_SAMPLE)) {
|
||||
// Skip all internal frames above the *_hook functions. Include the hook function itself.
|
||||
depth = 0;
|
||||
} else if (mark == MARK_INTERPRETER) {
|
||||
// This is C++ interpreter frame, this and later frames should be reported
|
||||
// as Java frames returned by AGCT. Terminate the scan here.
|
||||
return depth;
|
||||
} else if (mark == MARK_COMPILER_ENTRY && _features.comp_task) {
|
||||
// Insert current compile task as a pseudo Java frame
|
||||
jmethodID compile_task = getCurrentCompileTask();
|
||||
if (compile_task != NULL) {
|
||||
frames[depth].bci = 0;
|
||||
frames[depth].method_id = compile_task;
|
||||
depth++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jmethodID current_method = (jmethodID)current_method_name;
|
||||
if (current_method == prev_method && _cstack == CSTACK_LBR) {
|
||||
// Skip duplicates in LBR stack, where branch_stack[N].from == branch_stack[N+1].to
|
||||
prev_method = NULL;
|
||||
} else {
|
||||
frames[depth].bci = BCI_NATIVE_FRAME;
|
||||
frames[depth].method_id = prev_method = current_method;
|
||||
depth++;
|
||||
}
|
||||
frames[depth].bci = BCI_NATIVE_FRAME;
|
||||
frames[depth].method_id = (jmethodID)current_method_name;
|
||||
depth++;
|
||||
}
|
||||
|
||||
return depth;
|
||||
}
|
||||
|
||||
int Profiler::getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max_depth, StackContext* java_ctx) {
|
||||
int Profiler::getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max_depth) {
|
||||
// Workaround for JDK-8132510: it's not safe to call GetEnv() inside a signal handler
|
||||
// since JDK 9, so we do it only for threads already registered in ThreadLocalStorage
|
||||
VMThread* vm_thread = VMThread::current();
|
||||
@@ -391,146 +356,14 @@ int Profiler::getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max
|
||||
return 0;
|
||||
}
|
||||
|
||||
StackFrame frame(ucontext);
|
||||
uintptr_t saved_pc, saved_sp, saved_fp;
|
||||
if (ucontext != NULL) {
|
||||
saved_pc = frame.pc();
|
||||
saved_sp = frame.sp();
|
||||
saved_fp = frame.fp();
|
||||
}
|
||||
|
||||
if (_features.unwind_native && vm_thread->inJava()) {
|
||||
if (saved_pc >= (uintptr_t)_call_stub_begin && saved_pc < (uintptr_t)_call_stub_end) {
|
||||
// call_stub is unsafe to walk
|
||||
frames->bci = BCI_ERROR;
|
||||
frames->method_id = (jmethodID)"call_stub";
|
||||
return 1;
|
||||
}
|
||||
if (DWARF_SUPPORTED && java_ctx->sp != 0) {
|
||||
// If a thread is in Java state, unwind manually to the last known Java frame,
|
||||
// since JVM does not always correctly unwind native frames
|
||||
frame.restore((uintptr_t)java_ctx->pc, java_ctx->sp, java_ctx->fp);
|
||||
}
|
||||
}
|
||||
|
||||
JitWriteProtection jit(false);
|
||||
ASGCT_CallTrace trace = {jni, 0, frames};
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
|
||||
if (trace.num_frames > 0) {
|
||||
frame.restore(saved_pc, saved_sp, saved_fp);
|
||||
return trace.num_frames;
|
||||
}
|
||||
|
||||
if ((trace.num_frames == ticks_unknown_Java || trace.num_frames == ticks_not_walkable_Java) && _features.unknown_java && ucontext != NULL) {
|
||||
CodeBlob* stub = NULL;
|
||||
_stubs_lock.lockShared();
|
||||
if (_runtime_stubs.contains((const void*)frame.pc())) {
|
||||
stub = findRuntimeStub((const void*)frame.pc());
|
||||
}
|
||||
_stubs_lock.unlockShared();
|
||||
|
||||
if (stub != NULL) {
|
||||
if (_cstack != CSTACK_NO) {
|
||||
if (_features.vtable_target && isVTableStub(stub->_name)) {
|
||||
uintptr_t receiver = frame.jarg0();
|
||||
if (receiver != 0) {
|
||||
VMSymbol* symbol = VMKlass::fromOop(receiver)->name();
|
||||
u32 class_id = classMap()->lookup(symbol->body(), symbol->length());
|
||||
max_depth -= makeFrame(trace.frames++, BCI_ALLOC, class_id);
|
||||
}
|
||||
}
|
||||
max_depth -= makeFrame(trace.frames++, BCI_NATIVE_FRAME, stub->_name);
|
||||
}
|
||||
if (_features.unwind_stub && frame.unwindStub((instruction_t*)stub->_start, stub->_name)
|
||||
&& isAddressInCode((const void*)frame.pc())) {
|
||||
java_ctx->pc = (const void*)frame.pc();
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
}
|
||||
} else if (VMStructs::hasMethodStructs()) {
|
||||
NMethod* nmethod = CodeHeap::findNMethod((const void*)frame.pc());
|
||||
if (nmethod != NULL && nmethod->isNMethod() && nmethod->isAlive()) {
|
||||
VMMethod* method = nmethod->method();
|
||||
if (method != NULL) {
|
||||
jmethodID method_id = method->id();
|
||||
if (method_id != NULL) {
|
||||
max_depth -= makeFrame(trace.frames++, 0, method_id);
|
||||
}
|
||||
if (_features.unwind_comp && frame.unwindCompiled(nmethod)
|
||||
&& isAddressInCode((const void*)frame.pc())) {
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
}
|
||||
if (_features.probe_sp && trace.num_frames < 0) {
|
||||
if (method_id != NULL) {
|
||||
trace.frames--;
|
||||
}
|
||||
for (int i = 0; trace.num_frames < 0 && i < PROBE_SP_LIMIT; i++) {
|
||||
frame.sp() += sizeof(void*);
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (nmethod != NULL) {
|
||||
if (_cstack != CSTACK_NO) {
|
||||
max_depth -= makeFrame(trace.frames++, BCI_NATIVE_FRAME, nmethod->name());
|
||||
}
|
||||
if (_features.unwind_stub && frame.unwindStub(NULL, nmethod->name())
|
||||
&& isAddressInCode((const void*)frame.pc())) {
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (trace.num_frames == ticks_unknown_not_Java && _features.java_anchor) {
|
||||
JavaFrameAnchor* anchor = vm_thread->anchor();
|
||||
uintptr_t sp = anchor->lastJavaSP();
|
||||
const void* pc = anchor->lastJavaPC();
|
||||
if (sp != 0 && pc == NULL) {
|
||||
// We have the last Java frame anchor, but it is not marked as walkable.
|
||||
// Make it walkable here
|
||||
pc = ((const void**)sp)[-1];
|
||||
anchor->setLastJavaPC(pc);
|
||||
|
||||
NMethod* m = CodeHeap::findNMethod(pc);
|
||||
if (m != NULL) {
|
||||
// AGCT fails if the last Java frame is a Runtime Stub with an invalid _frame_complete_offset.
|
||||
// In this case we patch _frame_complete_offset manually
|
||||
if (!m->isNMethod() && m->frameSize() > 0 && m->frameCompleteOffset() == -1) {
|
||||
m->setFrameCompleteOffset(0);
|
||||
}
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
} else if (findLibraryByAddress(pc) != NULL) {
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
}
|
||||
|
||||
anchor->setLastJavaPC(NULL);
|
||||
}
|
||||
} else if (trace.num_frames == ticks_not_walkable_not_Java && _features.java_anchor) {
|
||||
JavaFrameAnchor* anchor = vm_thread->anchor();
|
||||
uintptr_t sp = anchor->lastJavaSP();
|
||||
const void* pc = anchor->lastJavaPC();
|
||||
if (sp != 0 && pc != NULL) {
|
||||
// Similar to the above: last Java frame is set,
|
||||
// but points to a Runtime Stub with an invalid _frame_complete_offset
|
||||
NMethod* m = CodeHeap::findNMethod(pc);
|
||||
if (m != NULL && !m->isNMethod() && m->frameSize() > 0 && m->frameCompleteOffset() == -1) {
|
||||
m->setFrameCompleteOffset(0);
|
||||
VM::_asyncGetCallTrace(&trace, max_depth, ucontext);
|
||||
}
|
||||
}
|
||||
} else if (trace.num_frames == ticks_GC_active && _features.gc_traces) {
|
||||
if (vm_thread->anchor()->lastJavaSP() == 0) {
|
||||
// Do not add 'GC_active' for threads with no Java frames, e.g. Compiler threads
|
||||
frame.restore(saved_pc, saved_sp, saved_fp);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
frame.restore(saved_pc, saved_sp, saved_fp);
|
||||
|
||||
if (trace.num_frames > 0) {
|
||||
return trace.num_frames + (trace.frames - frames);
|
||||
}
|
||||
|
||||
const char* err_string = asgctError(trace.num_frames);
|
||||
if (err_string == NULL) {
|
||||
// No Java stack, because thread is not in Java context
|
||||
@@ -538,9 +371,7 @@ int Profiler::getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max
|
||||
}
|
||||
|
||||
atomicInc(_failures[-trace.num_frames]);
|
||||
trace.frames->bci = BCI_ERROR;
|
||||
trace.frames->method_id = (jmethodID)err_string;
|
||||
return trace.frames - frames + 1;
|
||||
return makeFrame(frames, BCI_ERROR, err_string);
|
||||
}
|
||||
|
||||
int Profiler::getJavaTraceJvmti(jvmtiFrameInfo* jvmti_frames, ASGCT_CallFrame* frames, int start_depth, int max_depth) {
|
||||
@@ -558,49 +389,6 @@ int Profiler::getJavaTraceJvmti(jvmtiFrameInfo* jvmti_frames, ASGCT_CallFrame* f
|
||||
return num_frames;
|
||||
}
|
||||
|
||||
void Profiler::fillFrameTypes(ASGCT_CallFrame* frames, int num_frames, NMethod* nmethod) {
|
||||
if (nmethod->isNMethod() && nmethod->isAlive()) {
|
||||
VMMethod* method = nmethod->method();
|
||||
if (method == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
jmethodID current_method_id = method->id();
|
||||
if (current_method_id == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the top frame is a runtime stub, skip it
|
||||
if (num_frames > 0 && frames[0].bci == BCI_NATIVE_FRAME) {
|
||||
frames++;
|
||||
num_frames--;
|
||||
}
|
||||
|
||||
// Mark current_method as COMPILED and frames above current_method as INLINED
|
||||
for (int i = 0; i < num_frames; i++) {
|
||||
if (frames[i].method_id == NULL || frames[i].bci <= BCI_NATIVE_FRAME) {
|
||||
break;
|
||||
}
|
||||
if (frames[i].method_id == current_method_id) {
|
||||
int level = nmethod->level();
|
||||
frames[i].bci = FrameType::encode(level >= 1 && level <= 3 ? FRAME_C1_COMPILED : FRAME_JIT_COMPILED, frames[i].bci);
|
||||
for (int j = 0; j < i; j++) {
|
||||
frames[j].bci = FrameType::encode(FRAME_INLINED, frames[j].bci);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (nmethod->isInterpreter()) {
|
||||
// Mark the first Java frame as INTERPRETED
|
||||
for (int i = 0; i < num_frames; i++) {
|
||||
if (frames[i].bci > BCI_NATIVE_FRAME) {
|
||||
frames[i].bci = FrameType::encode(FRAME_INTERPRETED, frames[i].bci);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
u64 Profiler::recordSample(void* ucontext, u64 counter, EventType event_type, Event* event) {
|
||||
atomicInc(_total_samples);
|
||||
|
||||
@@ -635,37 +423,30 @@ u64 Profiler::recordSample(void* ucontext, u64 counter, EventType event_type, Ev
|
||||
}
|
||||
}
|
||||
|
||||
StackContext java_ctx = {0};
|
||||
u64 cpu = 0;
|
||||
if (hasNativeStack(event_type)) {
|
||||
if (_features.pc_addr && event_type <= WALL_CLOCK_SAMPLE) {
|
||||
num_frames += makeFrame(frames + num_frames, BCI_ADDRESS, StackFrame(ucontext).pc());
|
||||
}
|
||||
if (_cstack != CSTACK_NO) {
|
||||
num_frames += getNativeTrace(ucontext, frames + num_frames, event_type, tid, &java_ctx);
|
||||
num_frames += getNativeTrace(ucontext, frames + num_frames, event_type, tid, &cpu);
|
||||
}
|
||||
}
|
||||
|
||||
if (_features.mixed) {
|
||||
num_frames += StackWalker::walkVM(ucontext, frames + num_frames, _max_stack_depth, _features, event_type);
|
||||
num_frames += StackWalker::walkVM(ucontext, frames + num_frames, _max_stack_depth, lock_index, _features, event_type);
|
||||
} else if (event_type <= MALLOC_SAMPLE) {
|
||||
if (_cstack == CSTACK_VM) {
|
||||
num_frames += StackWalker::walkVM(ucontext, frames + num_frames, _max_stack_depth, _features, event_type);
|
||||
num_frames += StackWalker::walkVM(ucontext, frames + num_frames, _max_stack_depth, lock_index, _features, event_type);
|
||||
} else {
|
||||
int java_frames = getJavaTraceAsync(ucontext, frames + num_frames, _max_stack_depth, &java_ctx);
|
||||
if (java_frames > 0 && java_ctx.pc != NULL && VMStructs::hasMethodStructs()) {
|
||||
NMethod* nmethod = CodeHeap::findNMethod(java_ctx.pc);
|
||||
if (nmethod != NULL) {
|
||||
fillFrameTypes(frames + num_frames, java_frames, nmethod);
|
||||
}
|
||||
}
|
||||
num_frames += java_frames;
|
||||
num_frames += getJavaTraceAsync(ucontext, frames + num_frames, _max_stack_depth);
|
||||
}
|
||||
} else if (event_type >= ALLOC_SAMPLE && event_type <= ALLOC_OUTSIDE_TLAB && _alloc_engine == &alloc_tracer) {
|
||||
VMThread* vm_thread;
|
||||
if (VMStructs::hasStackStructs() && (vm_thread = VMThread::current()) != NULL) {
|
||||
num_frames += StackWalker::walkVM(ucontext, frames + num_frames, _max_stack_depth, vm_thread->anchor(), event_type);
|
||||
if (VMStructs::hasStackStructs()) {
|
||||
StackWalkFeatures no_features{};
|
||||
num_frames += StackWalker::walkVM(ucontext, frames + num_frames, _max_stack_depth, lock_index, no_features, event_type);
|
||||
} else {
|
||||
num_frames += getJavaTraceAsync(ucontext, frames + num_frames, _max_stack_depth, &java_ctx);
|
||||
num_frames += getJavaTraceAsync(ucontext, frames + num_frames, _max_stack_depth);
|
||||
}
|
||||
} else {
|
||||
// Lock events and instrumentation events can safely call synchronous JVM TI stack walker.
|
||||
@@ -676,6 +457,9 @@ u64 Profiler::recordSample(void* ucontext, u64 counter, EventType event_type, Ev
|
||||
|
||||
if (num_frames == 0) {
|
||||
num_frames += makeFrame(frames + num_frames, BCI_ERROR, "no_Java_frame");
|
||||
} else if (num_frames >= _max_stack_depth && _truncated_stack_depth < _max_stack_depth) {
|
||||
num_frames = _truncated_stack_depth;
|
||||
num_frames += makeFrame(frames + num_frames, BCI_ERROR, "truncated");
|
||||
}
|
||||
|
||||
if (_add_thread_frame) {
|
||||
@@ -684,8 +468,8 @@ u64 Profiler::recordSample(void* ucontext, u64 counter, EventType event_type, Ev
|
||||
if (_add_sched_frame) {
|
||||
num_frames += makeFrame(frames + num_frames, BCI_ERROR, OS::schedPolicy(0));
|
||||
}
|
||||
if (_add_cpu_frame) {
|
||||
num_frames += makeFrame(frames + num_frames, BCI_CPU, java_ctx.cpu | 0x8000);
|
||||
if (_add_cpu_frame && event_type == PERF_SAMPLE) {
|
||||
num_frames += makeFrame(frames + num_frames, BCI_CPU, cpu | 0x8000);
|
||||
}
|
||||
|
||||
if (stack_walk_begin != 0) {
|
||||
@@ -784,6 +568,7 @@ void* Profiler::dlopen_hook(const char* filename, int flags) {
|
||||
if (result != NULL) {
|
||||
instance()->updateSymbols(false);
|
||||
MallocTracer::installHooks();
|
||||
NativeLockTracer::installHooks();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -791,7 +576,7 @@ void* Profiler::dlopen_hook(const char* filename, int flags) {
|
||||
void Profiler::switchLibraryTrap(bool enable) {
|
||||
if (_dlopen_entry != NULL) {
|
||||
void* impl = enable ? (void*)dlopen_hook : (void*)dlopen;
|
||||
__atomic_store_n(_dlopen_entry, impl, __ATOMIC_RELEASE);
|
||||
storeRelease(*_dlopen_entry, impl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -866,11 +651,6 @@ void Profiler::crashHandler(int signo, siginfo_t* siginfo, void* ucontext) {
|
||||
StackWalker::checkFault();
|
||||
}
|
||||
|
||||
// Workaround for JDK-8313796. Setting cstack=dwarf also helps
|
||||
if (VMStructs::isInterpretedFrameValidFunc((const void*)pc) && frame.skipFaultInstruction()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (WX_MEMORY && Trap::isFaultInstruction(pc)) {
|
||||
return;
|
||||
}
|
||||
@@ -963,32 +743,13 @@ void Profiler::updateNativeThreadNames() {
|
||||
}
|
||||
}
|
||||
|
||||
bool Profiler::excludeTrace(FrameName* fn, CallTrace* trace) {
|
||||
bool check_include = fn->hasIncludeList();
|
||||
bool check_exclude = fn->hasExcludeList();
|
||||
if (!(check_include || check_exclude)) {
|
||||
return false;
|
||||
}
|
||||
Engine* Profiler::selectEngine(Arguments& args) {
|
||||
const char* event_name = args._event;
|
||||
|
||||
for (int i = 0; i < trace->num_frames; i++) {
|
||||
const char* frame_name = fn->name(trace->frames[i], true);
|
||||
if (check_exclude && fn->exclude(frame_name)) {
|
||||
return true;
|
||||
}
|
||||
if (check_include && fn->include(frame_name)) {
|
||||
check_include = false;
|
||||
if (!check_exclude) break;
|
||||
}
|
||||
}
|
||||
|
||||
return check_include;
|
||||
}
|
||||
|
||||
Engine* Profiler::selectEngine(const char* event_name) {
|
||||
if (event_name == NULL) {
|
||||
return &noop_engine;
|
||||
} else if (strcmp(event_name, EVENT_CPU) == 0) {
|
||||
if (FdTransferClient::hasPeer() || PerfEvents::supported()) {
|
||||
if (args._record_cpu || args._target_cpu != -1 || FdTransferClient::hasPeer() || PerfEvents::supported()) {
|
||||
return &perf_events;
|
||||
} else if (CTimer::supported()) {
|
||||
return &ctimer;
|
||||
@@ -1012,8 +773,8 @@ Engine* Profiler::selectEngine(const char* event_name) {
|
||||
}
|
||||
}
|
||||
|
||||
Engine* Profiler::selectAllocEngine(long alloc_interval, bool live) {
|
||||
if (VM::addSampleObjectsCapability()) {
|
||||
Engine* Profiler::selectAllocEngine(bool tlab) {
|
||||
if (!tlab && VM::addSampleObjectsCapability()) {
|
||||
return &object_sampler;
|
||||
} else if (VM::isOpenJ9()) {
|
||||
return &j9_object_sampler;
|
||||
@@ -1032,6 +793,8 @@ Engine* Profiler::activeEngine() {
|
||||
return &wall_clock;
|
||||
case EM_NATIVEMEM:
|
||||
return &malloc_tracer;
|
||||
case EM_NATIVELOCK:
|
||||
return &native_lock_tracer;
|
||||
case EM_METHOD_TRACE:
|
||||
return &instrument;
|
||||
default:
|
||||
@@ -1090,6 +853,10 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
return Error("Profiling event is not supported with non-Java processes");
|
||||
}
|
||||
|
||||
if (args._jfr_sync && !VM::loaded()) {
|
||||
return Error("jfrsync is not supported with non-Java processes");
|
||||
}
|
||||
|
||||
if (args._fdtransfer) {
|
||||
if (!FdTransferClient::connectToServer(args._fdtransfer_path)) {
|
||||
return Error("Failed to initialize FdTransferClient");
|
||||
@@ -1117,7 +884,7 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
lockAll();
|
||||
_class_map.clear();
|
||||
_thread_filter.clear();
|
||||
_call_trace_storage.clear();
|
||||
_call_trace_storage.clear(args._mem_limit);
|
||||
// Make sure frame structure is consistent throughout the entire recording
|
||||
_add_event_frame = args._output != OUTPUT_JFR;
|
||||
_add_thread_frame = args._threads && args._output != OUTPUT_JFR;
|
||||
@@ -1145,12 +912,9 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
}
|
||||
}
|
||||
}
|
||||
_truncated_stack_depth = std::min(std::max(args._truncated_stack_depth, 0), _max_stack_depth);
|
||||
|
||||
_features = args._features;
|
||||
if (VM::hotspot_version() < 8) {
|
||||
_features.java_anchor = 0;
|
||||
_features.gc_traces = 0;
|
||||
}
|
||||
if (!VMStructs::hasClassNames()) {
|
||||
_features.vtable_target = 0;
|
||||
}
|
||||
@@ -1161,7 +925,7 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
_update_thread_names = args._threads || args._output == OUTPUT_JFR;
|
||||
_thread_filter.init(args._filter);
|
||||
|
||||
_engine = selectEngine(args._event);
|
||||
_engine = selectEngine(args);
|
||||
if (_engine == &wall_clock && args._wall >= 0) {
|
||||
return Error("Cannot start wall clock with the selected event");
|
||||
} else if (_engine != &perf_events && args._target_cpu != -1) {
|
||||
@@ -1175,20 +939,22 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
_cstack = args._cstack;
|
||||
if (_cstack == CSTACK_DWARF && !DWARF_SUPPORTED) {
|
||||
return Error("DWARF unwinding is not supported on this platform");
|
||||
} else if (_cstack == CSTACK_LBR && _engine != &perf_events) {
|
||||
return Error("Branch stack is supported only with PMU events");
|
||||
} else if (_cstack == CSTACK_VM && VM::loaded() && !VMStructs::hasStackStructs()) {
|
||||
return Error("VMStructs stack walking is not supported on this JVM/platform");
|
||||
}
|
||||
|
||||
if (_cstack == CSTACK_DEFAULT) {
|
||||
if (VMStructs::hasStackStructs()) {
|
||||
// Use VMStructs by default when possible
|
||||
_cstack = args._cstack = CSTACK_VM;
|
||||
} else if (VM::isOpenJ9() && DWARF_SUPPORTED) {
|
||||
// OpenJ9 libs are compiled with frame pointers omitted
|
||||
_cstack = args._cstack = CSTACK_DWARF;
|
||||
}
|
||||
if ((_cstack == CSTACK_DEFAULT || _cstack == CSTACK_DWARF) && VMStructs::hasStackStructs() && !_features.agct) {
|
||||
// Use VMStructs by default when possible
|
||||
_cstack = args._cstack = CSTACK_VM;
|
||||
} else if (_cstack == CSTACK_DEFAULT && VM::isOpenJ9() && DWARF_SUPPORTED) {
|
||||
// OpenJ9 libs are compiled with frame pointers omitted
|
||||
_cstack = args._cstack = CSTACK_DWARF;
|
||||
}
|
||||
|
||||
if (_cstack == CSTACK_VM && _features.agct) {
|
||||
return Error("agct feature is incompatible with cstack=vm");
|
||||
} else if (_cstack != CSTACK_VM && _features.mixed) {
|
||||
return Error("mixed feature is only allowed with VMStructs stack walking");
|
||||
}
|
||||
|
||||
// Kernel symbols are useful only for perf_events without --all-user
|
||||
@@ -1215,7 +981,7 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
}
|
||||
|
||||
if (_event_mask & EM_ALLOC) {
|
||||
_alloc_engine = selectAllocEngine(args._alloc, args._live);
|
||||
_alloc_engine = selectAllocEngine(args._tlab);
|
||||
error = _alloc_engine->start(args);
|
||||
if (error) {
|
||||
goto error2;
|
||||
@@ -1239,10 +1005,16 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
goto error5;
|
||||
}
|
||||
}
|
||||
if (_event_mask & EM_NATIVELOCK) {
|
||||
error = native_lock_tracer.start(args);
|
||||
if (error) {
|
||||
goto error6;
|
||||
}
|
||||
}
|
||||
if (_event_mask & EM_METHOD_TRACE) {
|
||||
error = instrument.start(args);
|
||||
if (error) {
|
||||
goto error6;
|
||||
goto error7;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1252,18 +1024,24 @@ Error Profiler::start(Arguments& args, bool reset) {
|
||||
_start_time = OS::micros();
|
||||
_epoch++;
|
||||
|
||||
if (args._timeout != 0 || args._output == OUTPUT_JFR) {
|
||||
_stop_time = addTimeout(_start_time, args._timeout);
|
||||
if (args._timeout != 0 || args._loop != 0 || args._output == OUTPUT_JFR) {
|
||||
_loop_time = addTimeout(_start_time, args._loop);
|
||||
if (args._file_num == 0) {
|
||||
_stop_time = addTimeout(_start_time, args._timeout);
|
||||
}
|
||||
startTimer();
|
||||
}
|
||||
|
||||
return Error::OK;
|
||||
|
||||
error7:
|
||||
if (_event_mask & EM_NATIVELOCK) native_lock_tracer.stop();
|
||||
|
||||
error6:
|
||||
if (_event_mask & EM_METHOD_TRACE) instrument.stop();
|
||||
if (_event_mask & EM_NATIVEMEM) malloc_tracer.stop();
|
||||
|
||||
error5:
|
||||
if (_event_mask & EM_NATIVEMEM) malloc_tracer.stop();
|
||||
if (_event_mask & EM_WALL) wall_clock.stop();
|
||||
|
||||
error4:
|
||||
if (_event_mask & EM_LOCK) lock_tracer.stop();
|
||||
@@ -1298,6 +1076,7 @@ Error Profiler::stop(bool restart) {
|
||||
if (_event_mask & EM_LOCK) lock_tracer.stop();
|
||||
if (_event_mask & EM_ALLOC) _alloc_engine->stop();
|
||||
if (_event_mask & EM_NATIVEMEM) malloc_tracer.stop();
|
||||
if (_event_mask & EM_NATIVELOCK) native_lock_tracer.stop();
|
||||
if (_event_mask & EM_METHOD_TRACE) instrument.stop();
|
||||
|
||||
_engine->stop();
|
||||
@@ -1326,49 +1105,6 @@ Error Profiler::stop(bool restart) {
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
Error Profiler::check(Arguments& args) {
|
||||
MutexLocker ml(_state_lock);
|
||||
if (_state > IDLE) {
|
||||
return Error("Profiler already started");
|
||||
}
|
||||
|
||||
Error error = checkJvmCapabilities();
|
||||
|
||||
if (!error && args._event != NULL) {
|
||||
_engine = selectEngine(args._event);
|
||||
error = _engine->check(args);
|
||||
}
|
||||
if (!error && args._alloc >= 0) {
|
||||
_alloc_engine = selectAllocEngine(args._alloc, args._live);
|
||||
error = _alloc_engine->check(args);
|
||||
}
|
||||
if (!error && args._nativemem >= 0) {
|
||||
error = malloc_tracer.check(args);
|
||||
}
|
||||
if (!error && args._lock >= 0) {
|
||||
error = lock_tracer.check(args);
|
||||
}
|
||||
if (!error && !args._trace.empty()) {
|
||||
error = instrument.check(args);
|
||||
}
|
||||
|
||||
if (!error) {
|
||||
if (args._wall >= 0 && _engine == &wall_clock) {
|
||||
return Error("Cannot start wall clock with the selected event");
|
||||
}
|
||||
|
||||
if (args._cstack == CSTACK_DWARF && !DWARF_SUPPORTED) {
|
||||
return Error("DWARF unwinding is not supported on this platform");
|
||||
} else if (args._cstack == CSTACK_LBR && _engine != &perf_events) {
|
||||
return Error("Branch stack is supported only with PMU events");
|
||||
} else if (args._cstack == CSTACK_VM && VM::loaded() && !VMStructs::hasStackStructs()) {
|
||||
return Error("VMStructs stack walking is not supported on this JVM/platform");
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Profiler::flushJfr() {
|
||||
MutexLocker ml(_state_lock);
|
||||
if (_state != RUNNING) {
|
||||
@@ -1387,7 +1123,9 @@ Error Profiler::flushJfr() {
|
||||
|
||||
Error Profiler::dump(Writer& out, Arguments& args) {
|
||||
MutexLocker ml(_state_lock);
|
||||
if (_state != IDLE && _state != RUNNING) {
|
||||
if (_state == TERMINATED && _global_args._file != NULL && args._file != NULL && strcmp(_global_args._file, args._file) == 0) {
|
||||
return Error::OK;
|
||||
} else if (_state != IDLE && _state != RUNNING) {
|
||||
return Error("Profiler has not started");
|
||||
}
|
||||
|
||||
@@ -1426,30 +1164,24 @@ Error Profiler::dump(Writer& out, Arguments& args) {
|
||||
return Error::OK;
|
||||
}
|
||||
|
||||
void Profiler::printUsedMemory(Writer& out) {
|
||||
size_t call_trace_storage = _call_trace_storage.usedMemory();
|
||||
size_t flight_recording = _jfr.usedMemory();
|
||||
size_t dictionaries = _class_map.usedMemory() + _thread_filter.usedMemory();
|
||||
void Profiler::writeMetrics(Writer& out) {
|
||||
constexpr size_t KB = 1024;
|
||||
out << "mem_calltracestorage_kb " << (u64) _call_trace_storage.usedMemory() / KB << '\n';
|
||||
out << "mem_flightrecorder_kb " << (u64) _jfr.usedMemory() / KB << '\n';
|
||||
out << "mem_classmap_kb " << (u64) _class_map.usedMemory() / KB << '\n';
|
||||
out << "mem_threadfilter_kb " << (u64) _thread_filter.usedMemory() / KB << '\n';
|
||||
out << "mem_runtimestubs_kb " << (u64) _runtime_stubs.usedMemory() / KB << '\n';
|
||||
out << "mem_nativelibs_kb " << (u64) _native_libs.usedMemory() / KB << '\n';
|
||||
|
||||
size_t code_cache = _runtime_stubs.usedMemory();
|
||||
int native_lib_count = _native_libs.count();
|
||||
for (int i = 0; i < native_lib_count; i++) {
|
||||
code_cache += _native_libs[i]->usedMemory();
|
||||
out << "samples_total " << _total_samples << '\n';
|
||||
out << "samples_skipped_total " << _failures[-ticks_skipped] << '\n';
|
||||
out << "calltracestorage_overflows_total " << _call_trace_storage.overflow() << '\n';
|
||||
|
||||
if (_total_stack_walk_time != 0) {
|
||||
out << "stackwalk_ns_total " << _total_stack_walk_time << '\n';
|
||||
u64 stacks = _total_samples - _failures[-ticks_skipped];
|
||||
out << "stackwalk_ns_avg " << (_total_stack_walk_time / stacks) << '\n';
|
||||
}
|
||||
code_cache += native_lib_count * sizeof(CodeCache);
|
||||
|
||||
char buf[1024];
|
||||
const size_t KB = 1024;
|
||||
snprintf(buf, sizeof(buf) - 1,
|
||||
"Call trace storage: %7zu KB\n"
|
||||
" Flight recording: %7zu KB\n"
|
||||
" Dictionaries: %7zu KB\n"
|
||||
" Code cache: %7zu KB\n"
|
||||
"------------------------------\n"
|
||||
" Total: %7zu KB\n",
|
||||
call_trace_storage / KB, flight_recording / KB, dictionaries / KB, code_cache / KB,
|
||||
(call_trace_storage + flight_recording + dictionaries + code_cache) / KB);
|
||||
out << buf;
|
||||
}
|
||||
|
||||
void Profiler::logStats() {
|
||||
@@ -1492,7 +1224,7 @@ void Profiler::dumpCollapsed(Writer& out, Arguments& args) {
|
||||
|
||||
for (std::vector<CallTraceSample*>::const_iterator it = samples.begin(); it != samples.end(); ++it) {
|
||||
CallTrace* trace = (*it)->acquireTrace();
|
||||
if (trace == NULL || excludeTrace(&fn, trace)) continue;
|
||||
if (trace == NULL || fn.excludeTrace(trace)) continue;
|
||||
|
||||
u64 counter = args._counter == COUNTER_SAMPLES ? (*it)->samples : (*it)->counter;
|
||||
if (counter == 0) continue;
|
||||
@@ -1530,7 +1262,7 @@ void Profiler::dumpFlameGraph(Writer& out, Arguments& args, bool tree) {
|
||||
|
||||
for (std::vector<CallTraceSample*>::const_iterator it = samples.begin(); it != samples.end(); ++it) {
|
||||
CallTrace* trace = (*it)->acquireTrace();
|
||||
if (trace == NULL || excludeTrace(&fn, trace)) continue;
|
||||
if (trace == NULL || fn.excludeTrace(trace)) continue;
|
||||
|
||||
u64 counter = args._counter == COUNTER_SAMPLES ? (*it)->samples : (*it)->counter;
|
||||
if (counter == 0) continue;
|
||||
@@ -1592,7 +1324,7 @@ void Profiler::dumpText(Writer& out, Arguments& args) {
|
||||
if (trace == NULL || counter == 0) continue;
|
||||
|
||||
total_counter += counter;
|
||||
if (trace->num_frames == 0 || excludeTrace(&fn, trace)) continue;
|
||||
if (trace->num_frames == 0 || fn.excludeTrace(trace)) continue;
|
||||
samples.push_back(it->second);
|
||||
}
|
||||
}
|
||||
@@ -1664,133 +1396,13 @@ void Profiler::dumpText(Writer& out, Arguments& args) {
|
||||
}
|
||||
}
|
||||
|
||||
static void recordSampleType(ProtoBuffer& otlp_buffer, Index& strings, const char* type, const char* units) {
|
||||
using namespace Otlp;
|
||||
protobuf_mark_t sample_type_mark = otlp_buffer.startMessage(Profile::sample_type, 1);
|
||||
otlp_buffer.field(ValueType::type_strindex, strings.indexOf(type));
|
||||
otlp_buffer.field(ValueType::unit_strindex, strings.indexOf(units));
|
||||
otlp_buffer.field(ValueType::aggregation_temporality, AggregationTemporality::cumulative);
|
||||
otlp_buffer.commitMessage(sample_type_mark);
|
||||
}
|
||||
|
||||
void Profiler::dumpOtlp(Writer& out, Arguments& args) {
|
||||
using namespace Otlp;
|
||||
ProtoBuffer otlp_buffer(OTLP_BUFFER_INITIAL_SIZE);
|
||||
Index strings;
|
||||
Index functions;
|
||||
// Eventually this is going to be Index<Attribute>, for now we keep it simple
|
||||
Index thread_names;
|
||||
|
||||
protobuf_mark_t resource_profiles_mark = otlp_buffer.startMessage(ProfilesData::resource_profiles);
|
||||
protobuf_mark_t scope_profiles_mark = otlp_buffer.startMessage(ResourceProfiles::scope_profiles);
|
||||
protobuf_mark_t profile_mark = otlp_buffer.startMessage(ScopeProfiles::profiles);
|
||||
|
||||
u64 time_nanos = _start_time * 1000ULL;
|
||||
u64 duration_nanos = (OS::micros() - _start_time) * 1000ULL;
|
||||
|
||||
otlp_buffer.field(Profile::time_nanos, time_nanos);
|
||||
otlp_buffer.field(Profile::duration_nanos, duration_nanos);
|
||||
|
||||
recordSampleType(otlp_buffer, strings, _engine->type(), "count");
|
||||
recordSampleType(otlp_buffer, strings, _engine->type(), _engine->units());
|
||||
|
||||
FrameName fn(args, args._style & ~STYLE_ANNOTATE, _epoch, _thread_names_lock, _thread_names);
|
||||
Otlp::Recorder recorder(_engine, fn, _start_time * 1000ULL, (OS::micros() - _start_time) * 1000ULL);
|
||||
std::vector<CallTraceSample*> call_trace_samples;
|
||||
_call_trace_storage.collectSamples(call_trace_samples);
|
||||
|
||||
std::vector<size_t> location_indices;
|
||||
location_indices.reserve(call_trace_samples.size());
|
||||
|
||||
FrameName fn(args, args._style & ~STYLE_ANNOTATE, _epoch, _thread_names_lock, _thread_names);
|
||||
size_t frames_seen = 0;
|
||||
for (const auto& cts : call_trace_samples) {
|
||||
CallTrace* trace = cts->acquireTrace();
|
||||
if (trace == NULL || excludeTrace(&fn, trace) || cts->samples == 0) continue;
|
||||
|
||||
protobuf_mark_t sample_mark = otlp_buffer.startMessage(Profile::sample, 1);
|
||||
otlp_buffer.field(Sample::locations_start_index, frames_seen);
|
||||
otlp_buffer.field(Sample::locations_length, trace->num_frames);
|
||||
|
||||
u32 thread_name_idx = 0;
|
||||
for (int j = 0; j < trace->num_frames; j++) {
|
||||
if (trace->frames[j].bci == BCI_THREAD_ID) {
|
||||
int tid = (int)(uintptr_t) trace->frames[j].method_id;
|
||||
MutexLocker ml(_thread_names_lock);
|
||||
ThreadMap::iterator it = _thread_names.find(tid);
|
||||
if (it != _thread_names.end()) {
|
||||
thread_name_idx = thread_names.indexOf(it->second);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// To be written below in Profile.location_indices
|
||||
location_indices.push_back(functions.indexOf(fn.name(trace->frames[j])));
|
||||
++frames_seen;
|
||||
}
|
||||
if (thread_name_idx != 0) {
|
||||
otlp_buffer.field(Sample::attribute_indices, thread_name_idx);
|
||||
}
|
||||
|
||||
protobuf_mark_t sample_value_mark = otlp_buffer.startMessage(Sample::value, 1);
|
||||
otlp_buffer.putVarInt(cts->samples);
|
||||
otlp_buffer.putVarInt(cts->counter);
|
||||
otlp_buffer.commitMessage(sample_value_mark);
|
||||
otlp_buffer.commitMessage(sample_mark);
|
||||
}
|
||||
|
||||
protobuf_mark_t location_indices_mark = otlp_buffer.startMessage(Profile::location_indices);
|
||||
for (size_t i : location_indices) {
|
||||
otlp_buffer.putVarInt(i);
|
||||
}
|
||||
otlp_buffer.commitMessage(location_indices_mark);
|
||||
|
||||
otlp_buffer.commitMessage(profile_mark);
|
||||
otlp_buffer.commitMessage(scope_profiles_mark);
|
||||
otlp_buffer.commitMessage(resource_profiles_mark);
|
||||
|
||||
protobuf_mark_t dictionary_mark = otlp_buffer.startMessage(ProfilesData::dictionary);
|
||||
|
||||
// Write mapping_table. Not currently used, but required by some parsers
|
||||
protobuf_mark_t mapping_mark = otlp_buffer.startMessage(ProfilesDictionary::mapping_table, 1);
|
||||
otlp_buffer.commitMessage(mapping_mark);
|
||||
|
||||
// Write function_table
|
||||
functions.forEachOrdered([&] (size_t idx, const std::string& function_name) {
|
||||
protobuf_mark_t function_mark = otlp_buffer.startMessage(ProfilesDictionary::function_table, 1);
|
||||
otlp_buffer.field(Function::name_strindex, strings.indexOf(function_name));
|
||||
otlp_buffer.commitMessage(function_mark);
|
||||
});
|
||||
|
||||
// Write location_table
|
||||
for (size_t function_idx = 0; function_idx < functions.size(); ++function_idx) {
|
||||
protobuf_mark_t location_mark = otlp_buffer.startMessage(ProfilesDictionary::location_table, 1);
|
||||
// TODO: set to the proper mapping when new mappings are added.
|
||||
// For now we keep a dummy default mapping_index for all locations because some parsers
|
||||
// would fail otherwise
|
||||
otlp_buffer.field(Location::mapping_index, (u64)0);
|
||||
protobuf_mark_t line_mark = otlp_buffer.startMessage(Location::line, 1);
|
||||
otlp_buffer.field(Line::function_index, function_idx);
|
||||
otlp_buffer.commitMessage(line_mark);
|
||||
otlp_buffer.commitMessage(location_mark);
|
||||
}
|
||||
|
||||
// Write string_table
|
||||
strings.forEachOrdered([&] (size_t idx, const std::string& s) {
|
||||
otlp_buffer.field(ProfilesDictionary::string_table, s.data(), s.length());
|
||||
});
|
||||
|
||||
// Write attribute_table (only threads for now)
|
||||
thread_names.forEachOrdered([&] (size_t idx, const std::string& s) {
|
||||
protobuf_mark_t attr_mark = otlp_buffer.startMessage(ProfilesDictionary::attribute_table);
|
||||
otlp_buffer.field(Key::key, OTLP_THREAD_NAME);
|
||||
protobuf_mark_t value_mark = otlp_buffer.startMessage(Key::value);
|
||||
otlp_buffer.field(AnyValue::string_value, s.data(), s.length());
|
||||
otlp_buffer.commitMessage(value_mark);
|
||||
otlp_buffer.commitMessage(attr_mark);
|
||||
});
|
||||
|
||||
otlp_buffer.commitMessage(dictionary_mark);
|
||||
|
||||
out.write((const char*) otlp_buffer.data(), otlp_buffer.offset());
|
||||
recorder.record(call_trace_samples, args._counter == COUNTER_SAMPLES);
|
||||
recorder.write(out);
|
||||
}
|
||||
|
||||
u64 Profiler::addTimeout(u64 start_micros, int timeout) {
|
||||
@@ -1869,7 +1481,8 @@ void Profiler::stopTimer() {
|
||||
|
||||
void Profiler::timerLoop(void* timer_id) {
|
||||
u64 current_micros = OS::micros();
|
||||
u64 sleep_until = _jfr.active() ? current_micros + 1000000 : _stop_time;
|
||||
u64 loop_limit = std::min(_stop_time, _loop_time);
|
||||
u64 sleep_until = _jfr.active() ? current_micros + 1000000 : loop_limit;
|
||||
|
||||
while (true) {
|
||||
{
|
||||
@@ -1881,8 +1494,8 @@ void Profiler::timerLoop(void* timer_id) {
|
||||
if (_timer_id != timer_id) return;
|
||||
}
|
||||
|
||||
if ((current_micros = OS::micros()) >= _stop_time) {
|
||||
restart(_global_args);
|
||||
if ((current_micros = OS::micros()) >= loop_limit) {
|
||||
expire(_global_args, current_micros < _stop_time);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1947,14 +1560,6 @@ Error Profiler::runInternal(Arguments& args, Writer& out) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTION_CHECK: {
|
||||
Error error = check(args);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
out << "OK\n";
|
||||
break;
|
||||
}
|
||||
case ACTION_STATUS: {
|
||||
MutexLocker ml(_state_lock);
|
||||
if (_state == RUNNING) {
|
||||
@@ -1964,9 +1569,9 @@ Error Profiler::runInternal(Arguments& args, Writer& out) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ACTION_MEMINFO: {
|
||||
case ACTION_METRICS: {
|
||||
MutexLocker ml(_state_lock);
|
||||
printUsedMemory(out);
|
||||
writeMetrics(out);
|
||||
break;
|
||||
}
|
||||
case ACTION_LIST: {
|
||||
@@ -1975,6 +1580,7 @@ Error Profiler::runInternal(Arguments& args, Writer& out) {
|
||||
out << " " << EVENT_ALLOC << "\n";
|
||||
out << " " << EVENT_NATIVEMEM << "\n";
|
||||
out << " " << EVENT_LOCK << "\n";
|
||||
out << " " << EVENT_NATIVELOCK << "\n";
|
||||
out << " " << EVENT_WALL << "\n";
|
||||
out << " " << EVENT_ITIMER << "\n";
|
||||
if (CTimer::supported()) {
|
||||
@@ -2018,10 +1624,10 @@ Error Profiler::run(Arguments& args) {
|
||||
}
|
||||
}
|
||||
|
||||
Error Profiler::restart(Arguments& args) {
|
||||
Error Profiler::expire(Arguments& args, bool restart) {
|
||||
MutexLocker ml(_state_lock);
|
||||
|
||||
Error error = stop(args._loop);
|
||||
Error error = stop(restart);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
@@ -2037,7 +1643,7 @@ Error Profiler::restart(Arguments& args) {
|
||||
}
|
||||
}
|
||||
|
||||
if (args._loop) {
|
||||
if (restart) {
|
||||
args._fdtransfer = false; // keep the previous connection
|
||||
args._file_num++;
|
||||
return start(args, true);
|
||||
@@ -2047,7 +1653,15 @@ Error Profiler::restart(Arguments& args) {
|
||||
}
|
||||
|
||||
void Profiler::shutdown(Arguments& args) {
|
||||
MutexLocker ml(_state_lock);
|
||||
// Workaround for JDK-8373439: starting JFR during VM shutdown may hang forever,
|
||||
// so avoid acquiring _state_lock in this case.
|
||||
while (!_state_lock.tryLock()) {
|
||||
if (FlightRecorder::isJfrStarting()) {
|
||||
Log::debug("Skipping shutdown hook due to JFR start");
|
||||
return;
|
||||
}
|
||||
OS::sleep(10000000); // 10ms
|
||||
}
|
||||
|
||||
// The last chance to dump profile before VM terminates
|
||||
if (_state == RUNNING) {
|
||||
@@ -2059,4 +1673,6 @@ void Profiler::shutdown(Arguments& args) {
|
||||
}
|
||||
|
||||
_state = TERMINATED;
|
||||
|
||||
_state_lock.unlock();
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ union CallTraceBuffer {
|
||||
};
|
||||
|
||||
|
||||
class FrameName;
|
||||
class NMethod;
|
||||
class StackContext;
|
||||
|
||||
@@ -68,6 +67,7 @@ class Profiler {
|
||||
|
||||
u64 _start_time;
|
||||
u64 _stop_time;
|
||||
u64 _loop_time;
|
||||
int _epoch;
|
||||
u32 _gc_id;
|
||||
WaitableMutex _timer_lock;
|
||||
@@ -80,6 +80,7 @@ class Profiler {
|
||||
SpinLock _locks[CONCURRENCY_LEVEL];
|
||||
CallTraceBuffer* _calltrace_buffer[CONCURRENCY_LEVEL];
|
||||
int _max_stack_depth;
|
||||
int _truncated_stack_depth;
|
||||
StackWalkFeatures _features;
|
||||
CStack _cstack;
|
||||
bool _add_event_frame;
|
||||
@@ -92,8 +93,6 @@ class Profiler {
|
||||
SpinLock _stubs_lock;
|
||||
CodeCache _runtime_stubs;
|
||||
CodeCacheArray _native_libs;
|
||||
const void* _call_stub_begin;
|
||||
const void* _call_stub_end;
|
||||
|
||||
// dlopen() hook support
|
||||
void** _dlopen_entry;
|
||||
@@ -112,19 +111,16 @@ class Profiler {
|
||||
|
||||
const char* asgctError(int code);
|
||||
u32 getLockIndex(int tid);
|
||||
jmethodID getCurrentCompileTask();
|
||||
int getNativeTrace(void* ucontext, ASGCT_CallFrame* frames, EventType event_type, int tid, StackContext* java_ctx);
|
||||
int getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max_depth, StackContext* java_ctx);
|
||||
int getNativeTrace(void* ucontext, ASGCT_CallFrame* frames, EventType event_type, int tid, u64* cpu);
|
||||
int getJavaTraceAsync(void* ucontext, ASGCT_CallFrame* frames, int max_depth);
|
||||
int getJavaTraceJvmti(jvmtiFrameInfo* jvmti_frames, ASGCT_CallFrame* frames, int start_depth, int max_depth);
|
||||
void fillFrameTypes(ASGCT_CallFrame* frames, int num_frames, NMethod* nmethod);
|
||||
void setThreadInfo(int tid, const char* name, jlong java_thread_id);
|
||||
void updateThreadName(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
|
||||
void updateJavaThreadNames();
|
||||
void updateNativeThreadNames();
|
||||
bool excludeTrace(FrameName* fn, CallTrace* trace);
|
||||
void mangle(const char* name, char* buf, size_t size);
|
||||
Engine* selectEngine(const char* event_name);
|
||||
Engine* selectAllocEngine(long alloc_interval, bool live);
|
||||
Engine* selectEngine(Arguments& args);
|
||||
Engine* selectAllocEngine(bool tlab);
|
||||
Engine* activeEngine();
|
||||
Error checkJvmCapabilities();
|
||||
|
||||
@@ -167,12 +163,11 @@ class Profiler {
|
||||
_gc_id(0),
|
||||
_timer_id(NULL),
|
||||
_max_stack_depth(0),
|
||||
_truncated_stack_depth(0),
|
||||
_thread_events_state(JVMTI_DISABLE),
|
||||
_stubs_lock(),
|
||||
_runtime_stubs("[stubs]"),
|
||||
_native_libs(),
|
||||
_call_stub_begin(NULL),
|
||||
_call_stub_end(NULL),
|
||||
_dlopen_entry(NULL) {
|
||||
|
||||
for (int i = 0; i < CONCURRENCY_LEVEL; i++) {
|
||||
@@ -193,15 +188,14 @@ class Profiler {
|
||||
|
||||
Error run(Arguments& args);
|
||||
Error runInternal(Arguments& args, Writer& out);
|
||||
Error restart(Arguments& args);
|
||||
Error expire(Arguments& args, bool restart);
|
||||
void shutdown(Arguments& args);
|
||||
Error check(Arguments& args);
|
||||
Error start(Arguments& args, bool reset);
|
||||
Error stop(bool restart = false);
|
||||
Error flushJfr();
|
||||
Error dump(Writer& out, Arguments& args);
|
||||
void printUsedMemory(Writer& out);
|
||||
void logStats();
|
||||
void writeMetrics(Writer& out);
|
||||
void switchThreadEvents(jvmtiEventMode mode);
|
||||
int convertNativeTrace(int native_frames, const void** callchain, ASGCT_CallFrame* frames, EventType event_type);
|
||||
u64 recordSample(void* ucontext, u64 counter, EventType event_type, Event* event);
|
||||
@@ -220,7 +214,6 @@ class Profiler {
|
||||
CodeCache* findLibraryByAddress(const void* address);
|
||||
const char* findNativeMethod(const void* address);
|
||||
CodeBlob* findRuntimeStub(const void* address);
|
||||
bool isAddressInCode(const void* pc);
|
||||
|
||||
void trapHandler(int signo, siginfo_t* siginfo, void* ucontext);
|
||||
static void crashHandler(int signo, siginfo_t* siginfo, void* ucontext);
|
||||
|
||||
@@ -53,6 +53,13 @@ void ProtoBuffer::field(protobuf_index_t index, u64 n) {
|
||||
putVarInt(n);
|
||||
}
|
||||
|
||||
void ProtoBuffer::fieldFixed64(protobuf_index_t index, u64 n) {
|
||||
tag(index, I64);
|
||||
ensureCapacity(sizeof(u64));
|
||||
*(u64*)(_data + _offset) = n;
|
||||
_offset += 8;
|
||||
}
|
||||
|
||||
void ProtoBuffer::field(protobuf_index_t index, const char* s) {
|
||||
field(index, s, strlen(s));
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
typedef const u32 protobuf_t;
|
||||
protobuf_t VARINT = 0;
|
||||
protobuf_t I64 = 1;
|
||||
protobuf_t LEN = 2;
|
||||
|
||||
typedef u32 protobuf_index_t;
|
||||
@@ -42,6 +43,7 @@ class ProtoBuffer {
|
||||
void reset() { _offset = 0; }
|
||||
|
||||
void field(protobuf_index_t index, u64 n);
|
||||
void fieldFixed64(protobuf_index_t index, u64 n);
|
||||
// LEN
|
||||
void field(protobuf_index_t index, const char* s);
|
||||
void field(protobuf_index_t index, const char* s, size_t len);
|
||||
|
||||
@@ -3,24 +3,82 @@
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<style>
|
||||
body {margin: 0; padding: 10px 10px 22px 10px; background-color: #ffffff}
|
||||
:root {--bg: #ffffff; --fg: #000000; --hl-bg: #ffffe0; --hl-border: #ffc000; --link: #0366d6; --legend-bg: #ffffe0; --legend-border: #666666}
|
||||
:root.dark {--bg: #1e1e1e; --fg: #cccccc; --hl-bg: #3a3a00; --hl-border: #8a7000; --link: #58a6ff; --legend-bg: #333333; --legend-border: #888888}
|
||||
body {margin: 0; padding: 10px 10px 22px 10px; background-color: var(--bg); color: var(--fg)}
|
||||
h1 {margin: 5px 0 0 0; font-size: 18px; font-weight: normal; text-align: center}
|
||||
header {margin: -24px 0 5px 0; line-height: 24px}
|
||||
button {font: 12px sans-serif; cursor: pointer}
|
||||
p {position: fixed; bottom: 0; margin: 0; padding: 2px 3px 2px 3px; outline: 1px solid #ffc000; display: none; overflow: hidden; white-space: nowrap; background-color: #ffffe0}
|
||||
a {color: #0366d6}
|
||||
#hl {position: absolute; display: none; overflow: hidden; white-space: nowrap; pointer-events: none; background-color: #ffffe0; outline: 1px solid #ffc000; height: 15px}
|
||||
header {margin: -22px 0 6px 0}
|
||||
button {border: none; background: none; width: 24px; height: 24px; cursor: pointer; margin: 0; padding: 2px 0 0 0; text-align: center}
|
||||
button:hover {background-color: var(--hl-bg); outline: 1px solid var(--hl-border); border-radius: 4px}
|
||||
dl {margin: 0 4px 8px 4px}
|
||||
dt {margin: 1px; padding: 2px 0; font-weight: bold}
|
||||
dd {margin: 1px; padding: 2px 4px}
|
||||
dl.frames {float: left; width: 160px}
|
||||
dl.frames > dd {color: #000000}
|
||||
dl.hotkeys {clear: left; border-top: 1px solid var(--legend-border)}
|
||||
dl.hotkeys > dt {float: left; clear: left; width: 158px; margin-right: 4px; text-align: right}
|
||||
dl.hotkeys > dd {float: left}
|
||||
p {position: fixed; bottom: 0; margin: 0; padding: 2px 3px 2px 3px; outline: 1px solid var(--hl-border); display: none; overflow: hidden; white-space: nowrap; background-color: var(--hl-bg); color: var(--fg)}
|
||||
a {color: var(--link)}
|
||||
#legend {padding: 4px; border-radius: 4px; background: var(--legend-bg); border: 1px solid var(--legend-border); display: none}
|
||||
#hl {position: absolute; display: none; overflow: hidden; white-space: nowrap; pointer-events: none; background-color: var(--hl-bg); outline: 1px solid var(--hl-border); height: 15px}
|
||||
#hl span {padding: 0 3px 0 3px}
|
||||
#status {left: 0}
|
||||
#match {right: 0}
|
||||
#reset {cursor: pointer}
|
||||
#canvas {width: 100%; height: /*height:*/300px}
|
||||
</style>
|
||||
<script>
|
||||
{
|
||||
let theme;
|
||||
try { theme = localStorage.getItem('flame-theme'); } catch (ignored) {}
|
||||
if (theme ? theme === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||
document.documentElement.classList.add('dark');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body style='font: 12px Verdana, sans-serif'>
|
||||
<h1>/*title:*/</h1>
|
||||
<header style='text-align: left'><button id='inverted' title='Invert'>🔻</button> <button id='search' title='Search'>🔍</button></header>
|
||||
<header style='text-align: right'>Produced by <a href='https://github.com/async-profiler/async-profiler'>async-profiler</a></header>
|
||||
<header style='float: left'>
|
||||
<button id='inverted' title='Invert (I)'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 392 392'><path d='M196,36 L316,156 L76,156 Z' fill='#004d80'/><path d='M196,356 L76,236 L316,236 Z' fill='#004d80'/><path d='M196,54 L298,156 L94,156 Z' fill='#ff8d40'/><path d='M196,338 L94,236 L298,236 Z' fill='#40b2ff'/><rect x='94' y='188' width='204' height='16' fill='#004d80'/></svg></button>
|
||||
<button id='search' title='Search (Ctrl+F)'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='-39.3 -39.3 471.1 471.1'><circle cx='147.7' cy='147.8' r='125.9' fill='#fff'/><path fill='#40b2ff' d='M370.7 348.7c0 1.4-1.6 6.3-7.2 12.3-6.2 6.7-12.5 9.8-14.7 9.8h-.1c-19.5-1.6-62-43.2-109.6-106.8 9.2-7.2 17.5-15.5 24.6-24.6 63.6 47.6 105.2 90.2 106.8 109.6z'/><path fill='#ff8d40' d='M208.7 86.9l-14.5 14.5c-17.1 17.1-46.5 5-46.5-19.3V61.6c-49 0-88.4 40.8-86.1 90.2 2 43.9 38.1 80 82 82 49.5 2.3 90.2-37.2 90.2-86.1 0-23.7-9.6-45.2-25.1-60.8z'/><path fill='#004d80' d='M276.1 221c12.3-21.5 19.5-46.5 19.5-73.2C295.6 66.3 229.2.1 147.7.1S0 66.3 0 147.9s66.3 147.7 147.7 147.7c26.6 0 51.5-7.1 73.2-19.5 39.8 53.3 91.9 113.5 126.1 116.4 12.3.5 22.9-6.7 32.8-16.7 5.2-5.6 13.8-16.9 12.8-28.8-2.9-34.1-63.1-86.2-116.4-126.1zM147.7 273.8c-69.5 0-125.9-56.5-125.9-125.9S78.3 21.9 147.7 21.9 273.6 78.4 273.6 147.8s-56.4 126-125.9 126zm215.9 87.2c-6.2 6.7-12.4 9.8-14.7 9.8h-.1c-19.5-1.6-62-43.2-109.6-106.8 9.2-7.2 17.5-15.5 24.6-24.6 63.6 47.6 105.2 90.2 106.8 109.6 0 1.4-1.6 6.3-7.2 12.4z'/></svg></button>
|
||||
<button id='darkmode' title='Toggle dark mode (D)'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><path d='M10 4a6 6 0 0 1 0 12z' fill='#ff8d40'/><path d='M10 4a6 6 0 0 0 0 12z' fill='#ffffff'/><circle cx='10' cy='10' r='8' fill='none' stroke='#004d80'/></svg></button>
|
||||
<button id='info'><svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><circle cx='10' cy='10' r='8' stroke='#004d80' fill='none'/><path d='M10 5.5c-1.25 0-2.25 1-2.25 2.25H9a1.25 1.25 0 0 1 2.5 0c0 .65-.55 1-1 1.2-.7.35-1.25.85-1.25 1.8V11h1.5v-.25c0-.37.29-.65.68-.83.73-.34 1.32-.87 1.32-2.17 0-1.25-1.5-2.25-2.75-2.25' fill='#ff8d40' stroke='#ff8d40' stroke-width='.6' stroke-linecap='round' stroke-linejoin='round'/><circle cx='10' cy='13.5' r='1.2' fill='#ff8d40'/></svg></button>
|
||||
</header>
|
||||
<header style='float: right'>Produced by <a href='https://github.com/async-profiler/async-profiler'>async-profiler</a></header>
|
||||
<div id='legend' style='position: absolute'>
|
||||
<dl class='frames'>
|
||||
<dt>Frame types</dt>
|
||||
<dd style='background-color: #e17d00'>Kernel</dd>
|
||||
<dd style='background-color: #e15a5a'>Native</dd>
|
||||
<dd style='background-color: #c8c83c'>C++ (VM)</dd>
|
||||
<dd style='background-color: #50e150'>Java compiled</dd>
|
||||
<dd style='background-color: #cce880'>Java compiled by C1</dd>
|
||||
<dd style='background-color: #50cccc'>Inlined</dd>
|
||||
<dd style='background-color: #b2e1b2'>Interpreted</dd>
|
||||
</dl>
|
||||
<dl class='frames'>
|
||||
<dt>Allocation profile</dt>
|
||||
<dd style='background-color: #50cccc'>Allocated class</dd>
|
||||
<dd style='background-color: #e17d00'>Allocation outside TLAB</dd>
|
||||
<dt>Lock profile</dt>
|
||||
<dd style='background-color: #50cccc'>Lock class</dd>
|
||||
<dt> </dt>
|
||||
<dt>Search</dt>
|
||||
<dd style='background-color: #ee00ee'>Matches regexp</dd>
|
||||
</dl>
|
||||
<dl class='hotkeys'>
|
||||
<dt>Click frame</dt><dd>Zoom into frame</dd>
|
||||
<dt>Ctrl/Alt+Click</dt><dd>Remove stack</dd>
|
||||
<dt>0</dt><dd>Reset zoom</dd>
|
||||
<dt>I</dt><dd>Invert graph</dd>
|
||||
<dt>Ctrl+F</dt><dd>Search</dd>
|
||||
<dt>N</dt><dd>Next match</dd>
|
||||
<dt>Shift+N</dt><dd>Previous match</dd>
|
||||
<dt>Esc</dt><dd>Cancel search</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<canvas id='canvas'></canvas>
|
||||
<div id='hl'><span></span></div>
|
||||
<p id='status'></p>
|
||||
@@ -30,9 +88,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
'use strict';
|
||||
let root, px, pattern;
|
||||
let level0 = 0, left0 = 0, width0 = 0;
|
||||
let level0 = 0, left0 = 0, width0 = 0, d = 0;
|
||||
let nav = [], navIndex, matchval;
|
||||
let inverted = /*inverted:*/false;
|
||||
const U = undefined;
|
||||
const maxdiff = /*maxdiff:*/-1;
|
||||
const levels = Array(/*depth:*/0);
|
||||
for (let h = 0; h < levels.length; h++) {
|
||||
levels[h] = [];
|
||||
@@ -66,10 +126,18 @@
|
||||
return '#' + (p[0] + ((p[1] * v) << 16 | (p[2] * v) << 8 | (p[3] * v))).toString(16);
|
||||
}
|
||||
|
||||
function getDiffColor(diff) {
|
||||
if (diff === U) return '#ffdd33';
|
||||
if (diff === 0) return '#e0e0e0';
|
||||
const v = Math.round(128 * (maxdiff - Math.abs(diff)) / maxdiff) + 96;
|
||||
return diff > 0 ? 'rgb(255,' + v + ',' + v + ')' : 'rgb(' + v + ',' + v + ',255)';
|
||||
}
|
||||
|
||||
function f(key, level, left, width, inln, c1, int) {
|
||||
levels[level0 = level].push({level, left: left0 += left, width: width0 = width || width0,
|
||||
color: getColor(palette[key & 7]), title: cpool[key >>> 3],
|
||||
details: (int ? ', int=' + int : '') + (c1 ? ', c1=' + c1 : '') + (inln ? ', inln=' + inln : '')
|
||||
color: maxdiff >= 0 ? getDiffColor(d) : getColor(palette[key & 7]),
|
||||
title: cpool[key >>> 3],
|
||||
details: (d ? (d > 0 ? ', +' : ', ') + d : '') + (int ? ', int=' + int : '') + (c1 ? ', c1=' + c1 : '') + (inln ? ', inln=' + inln : '')
|
||||
});
|
||||
}
|
||||
|
||||
@@ -141,8 +209,10 @@
|
||||
}
|
||||
|
||||
function render(newRoot, nav) {
|
||||
const bg = getComputedStyle(document.documentElement).getPropertyValue('--bg');
|
||||
|
||||
if (root) {
|
||||
c.fillStyle = '#ffffff';
|
||||
c.fillStyle = bg;
|
||||
c.fillRect(0, 0, canvasWidth, canvasHeight);
|
||||
}
|
||||
|
||||
@@ -184,7 +254,7 @@
|
||||
}
|
||||
|
||||
if (f.level < root.level) {
|
||||
c.fillStyle = 'rgba(255, 255, 255, 0.5)';
|
||||
c.fillStyle = bg + '80';
|
||||
c.fillRect((f.left - x0) * px, y, f.width * px, 15);
|
||||
}
|
||||
}
|
||||
@@ -221,7 +291,7 @@
|
||||
canvas.title = f.title + '\n(' + samples(f.width) + f.details + ', ' + pct(f.width, levels[0][0].width) + '%)';
|
||||
canvas.style.cursor = 'pointer';
|
||||
canvas.onclick = function() {
|
||||
if (event.altKey && h >= root.level) {
|
||||
if ((event.altKey || event.ctrlKey) && h >= root.level && h > 0) {
|
||||
removeStack(f.left, f.width);
|
||||
root.width > f.width ? render(root) : render();
|
||||
} else if (f !== root) {
|
||||
@@ -262,10 +332,30 @@
|
||||
search(false);
|
||||
}
|
||||
|
||||
document.getElementById('darkmode').onclick = function() {
|
||||
const theme = document.documentElement.classList.toggle('dark') ? 'dark' : 'light';
|
||||
try { localStorage.setItem('flame-theme', theme); } catch (ignored) {}
|
||||
render(root);
|
||||
}
|
||||
|
||||
const btnInfo = document.getElementById('info');
|
||||
const legend = document.getElementById('legend');
|
||||
|
||||
btnInfo.onmouseover = function() {
|
||||
legend.style.left = (btnInfo.offsetLeft + 24) + 'px';
|
||||
legend.style.top = (btnInfo.offsetTop + 24) + 'px';
|
||||
legend.style.display = 'block';
|
||||
}
|
||||
|
||||
btnInfo.onmouseout = function() {
|
||||
legend.style.display = 'none';
|
||||
}
|
||||
|
||||
window.onkeydown = function(event) {
|
||||
if ((event.ctrlKey || event.metaKey) && event.key === 'f') {
|
||||
event.preventDefault();
|
||||
search(true);
|
||||
return false;
|
||||
} else if (event.key === 'Escape') {
|
||||
search(false);
|
||||
} else if ((event.key === 'n' || event.key === 'N') && nav.length > 0) {
|
||||
@@ -274,6 +364,19 @@
|
||||
document.getElementById('matchval').textContent = matchval + ' (' + (navIndex + 1) + ' of ' + nav.length + ')';
|
||||
window.scroll(0, inverted ? root.level * 16 : canvasHeight - (root.level + 1) * 16);
|
||||
canvas.onmousemove();
|
||||
return false;
|
||||
} else if (event.key === 'i') {
|
||||
canvas.onmouseout();
|
||||
document.getElementById('inverted').onclick();
|
||||
return false;
|
||||
} else if (event.key === 'd') {
|
||||
document.getElementById('darkmode').onclick();
|
||||
return false;
|
||||
} else if (event.key === '0') {
|
||||
canvas.onmouseout();
|
||||
root = levels[0][0];
|
||||
search(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user