perf は Linux perf_events のユーザーランドツールであり、イベントの総数をカウントするだけでなく、イベント発生時に呼び出しスタックをサンプリングすることもできます。まず明確にすべきは、答えたい質問です。プログラムはどのくらい CPU を消費しているのか、ホットスポットはどこか、特定の命令が遅い理由は何なのか、それともリクエストは何を待っているのか。異なる問題には、異なるイベントと観察範囲が必要です。
以下のコマンド例では、Linux ユーザーランドプログラム ./prog を対象としています。イベントのサポートは CPU、カーネル、仮想化環境、および perf のバージョンに依存します。まず perf list で実際に利用可能な項目を確認してください。権限も perf_event_paranoid、capability、およびシステムポリシーに依存し、すべての操作で root 権限が必要とは限りません。
カウント:再現可能なベースラインの確立
perf stat - r 5 - e task-clock,context-switches,page-faults -- ./prog
perf stat - r 5 - e ' {cycles:u,instructions:u}' -- ./prog
perf list
1行目は CPU 時間、スケジューリング、ページフォルトから始めます。2行目はユーザーランドの cycles と instructions を同じグループに配置し、同じ測定ウィンドウでの比較を容易にします。-r 5 はプログラムを5回実際に実行し、入力、データ状態、ウォームアップ戦略を一致させます。プログラムがデータを変更する場合は、再現可能なテストシナリオを事前に準備してください。
指標 答えられる質問 直接導き出せないこと
task-clock タスクが累積して使用した CPU 時間 壁掛け時計(リアルタイム)とは等しくなく、マルチスレッドでは累積値がより大きくなる場合がある
instructions / cycles この負荷の IPC CPU や負荷間で統一された閾値を使って良し悪しを判断できない
branch-misses / branches カウントされた分岐のミス割合 ミス率だけを見ても、総消費時間に占める寄与度を特定できない
page-faults 発生したページフォルトの数 すべてがディスク I/O ではなく、軽微なページフォルトを含む
context-switches スケジューリングの切り替え頻度 切り替えが多いことが必ずしも主要なボトルネックではない
例えば、アルゴリズムを再実装した結果、命令数が半分になり IPC も低下しましたが、総実行時間は依然として短くなりました。IPC は実行時間と完了した作業量と合わせて解釈すべきです。cycles:u の u はユーザーランドのみを意味し、周波数変化を排除するものではありません。CPU 周波数、SMT、コア移動、ハイブリッドコア、およびバックグラウンドの干渉は比較に影響を与え続けます。
汎用の cache-misses の具体的なマッピングは PMU によって決定されるため、デフォルトで L1 ミス率と見なすべきではありません。特定のキャッシュレイヤについて議論する場合は、その CPU のドキュメントで定義されたイベントを選択し、分母を確認してください。
1万件の作業を完了しても、IPC が低下すれば遅くなるわけではない
以下は仮データであり、カウントの解釈を練習するためのものです。特定のマシンの測定結果ではありません。A と B はどちらも 10,000 件の同じ作業を完了し、イベント範囲と実行条件は一致しています。
指標 A B 解釈
壁掛け時計の消費時間 0.50 s 0.40 s B の消費時間は 20% 減少
instructions 2.0 × 10⁹ 1.2 × 10⁹ 作業あたりの命令数が 200,000 から 120,000 に減少
cycles 1.0 × 10⁹ 0.8 × 10⁹ ユーザーランドイベントカウントの減少
IPC 2.0 1.5 B の IPC は低いものの、総作業コストは節約されている
この場合、正しい結論は B が同じ作業をより速く完了したということです。IPC が 2 から 1.5 に低下したからといって、後退と判断してはいけません。もし B が 8,000 件のみ処理し、検証をスキップしたりエラーを無視したりしていた場合、消費時間の減少を最適化の利益として直接見なすことはできません。
from math import isclose
jobs = 10_000
a = { " seconds" : 0. 5 , " instructions" : 2_000_000_000 , " cycles" : 1_000_000_000 }
b = { " seconds" : 0. 4 , " instructions" : 1_200_000_000 , " cycles" : 800_000_000 }
assert a [ " instructions" ] / a [ " cycles" ] == 2
assert b [ " instructions" ] / b [ " cycles" ] == 1. 5
assert a [ " instructions" ] / jobs == 200_000
assert b [ " instructions" ] / jobs == 120_000
assert isclose ( 1 - b [ " seconds" ] / a [ " seconds" ] , 0. 2 )
assert isclose ( jobs / b [ " seconds" ] / ( jobs / a [ " seconds" ] ) , 1. 25 )
消費時間が 20% 減少することは、分母が異なるためスループットが 25% 向上することを意味します。task-clock も正しい分母で使用する必要があります。2秒の壁掛け時計の間に 3.2秒の累積 CPU 時間が計測された場合、平均して約 1.6個の CPU の実行時間を使用していることになります。「CPU 利用率が 100% を超えているのでデータが壊れている」わけではありません。この平均値は、ある瞬間のピークや、各コアでの均一性を表すものではありません。
カウンタの再利用:数値はスケーリングされている可能性がある
ハードウェアで同時に利用可能なカウンタは限られており、イベント数が利用可能なリソースを超えると、リソースが共有されることがあります。perf は実際の実行時間に基づいて結果をスケーリングできますが、負荷が段階的に強い場合、特定の段階を短期間観測して全体を推測すると、誤差が生じる可能性があります。
出力結果における実行比率、not counted(カウントされず)、または not supported(サポートされず)に注目してください。イベント数を減らし、関連する比率のイベントをグループ化し、実験を繰り返すことで、結果が安定しているかどうかを判断するのに役立ちます。グループ化もリソース制約により全体としてスケジューリングできない場合があります。perf stat のスケーリングとイベントグループのセマンティクスについては マニュアル を参照してください。
スケーリング補正は時間をカバーするが、観察されなかった段階を復元できない
イベントが 10秒間有効化され、ハードウェアカウンタによって実際にスケジューリングされた時間が 2.5秒、原始カウントが 200 であると仮定します。時間の比率でスケーリングすると 200 × 10 / 2.5 = 800 となります。これは、サンプリングされた期間がサンプリングされていない期間を代表するという仮定を含みます。もしちょうど初期化のピークのみをカバーし、その後の大部分の時間が別の作業フェーズにある場合、800 は実際の総量から大きく外れる可能性があります。
関連する比率は、同じイベントグループと互換性のある範囲から取得するのが最も良いですが、イベントグループは無料の保証ではありません。ハードウェアリソースが不足している場合、グループ全体が実行できないこともあります。not supported に遭遇した場合は、まずイベントと PMU を確認し、not counted に遭遇した場合は、スケジューリングリソースと実際の稼働時間を確認してください。これらの状態を数値のゼロとして除算に使用しないでください。perf stat のスケーリングとグループ化
時間で拡大しても、なぜ誤算が生じる可能性があるのか?
完全な棒グラフはモデルが提供する既知のイベント率であり、実際の測定ではサンプリングされていない区間は通常観察できません。デフォルトでは最初の 2秒のみをカウントし、原始の 200 が 1000 に拡大され、実際の 280 を大幅に超えます。逆に、後の低率の区間に移動すると、過小評価される可能性があります。running=0 の場合、ゼロ除算したり、ゼロイベントと解釈したりしてはいけません。有効なカウントがない場合は報告すべきです。
N = N r a w t r u nnin g t e nab l e d , t r u nnin g > 0
図を準備しています 再読み込み
時間で拡大しても、なぜ誤算が生じる可能性があるのか? enabled/running によるスケーリングは、カバーされた比率のみを補正し、観察されなかった段階のイベント率を再構築することはできません。ウィンドウがちょうどピークまたはボトムをカバーする場合、推定値は総量から外れる可能性があります。
{"id":"perf-multiplex-estimate","title":"時間で拡大しても、なぜ誤算が生じる可能性があるのか?","summary":"enabled/running によるスケーリングは、カバーされた比率のみを補正し、観察されなかった段階のイベント率を再構築することはできません。ウィンドウがちょうどピークまたはボトムをカバーする場合、推定値は総量から外れる可能性があります。","height":1000,"html":"<h2 data-i18n=\"heading\"></h2><p class=\"intro\" data-i18n=\"intro\"></p><div class=\"control\"><label for=\"start\" data-i18n=\"start\"></label><output for=\"start\" id=\"start-value\"></output><input id=\"start\" type=\"range\" min=\"0\" max=\"9\" value=\"0\" step=\"1\"></div><div class=\"control\"><label for=\"length\" data-i18n=\"length\"></label><output for=\"length\" id=\"length-value\"></output><input id=\"length\" type=\"range\" min=\"0\" max=\"10\" value=\"2\" step=\"1\"></div><div id=\"chart\" class=\"hist\" role=\"img\" data-i18n-label=\"chart\"></div><div class=\"metrics\"><div><span data-i18n=\"raw\"></span><strong id=\"raw\"></strong></div><div><span data-i18n=\"estimate\"></span><strong id=\"estimate\"></strong></div><div><span data-i18n=\"truth\"></span><strong id=\"truth\"></strong></div><div><span data-i18n=\"coverage\"></span><strong id=\"coverage\"></strong></div></div><p class=\"formula\" id=\"equation\"></p><details class=\"scope\"><summary data-i18n=\"scopeLabel\"></summary><p data-i18n=\"scope\"></p></details>","css":".intro{margin:8px 0 18px;color:var(--muted);font-size:14px}.caption,.hint{font-size:12px;color:var(--muted);margin-top:12px}.control{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:6px 12px;align-items:center;margin:14px 0;font-size:13px}.control label{font-size:13px}.control output{color:var(--accent);min-width:5em;text-align:right;font-variant-numeric:tabular-nums}.control input{grid-column:1/-1;width:100%;margin:0}.presets{display:flex;gap:4px;align-items:stretch}.presets button{flex:1 1 0;min-width:0;overflow-wrap:anywhere;font-size:13px}.actions{display:flex;gap:8px;flex-wrap:wrap;margin:14px 0}.actions button{flex:1;min-width:0;font-size:13px;min-height:42px}.copy{display:grid;font-size:14px;margin:16px 0}.copy>*{grid-area:1/1;margin:0}.reserve{visibility:hidden}.scope{margin-top:14px;color:var(--muted);font-size:12px}.scope summary{padding:8px 0;cursor:pointer}.scope p{margin-top:10px}.two{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;margin:16px 0}.card{border:1px solid var(--rule);border-radius:9px;padding:14px;min-width:0}.card header{font-size:14px;font-weight:600;margin-bottom:10px}.card button{font-size:12px;width:100%;min-width:0}.card small{display:block;font-size:12px;color:var(--muted)}.card code{font-size:12px;overflow-wrap:anywhere}.metrics{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;margin:16px 0}.metrics>div{border-left:2px solid var(--accent);padding-left:10px;min-width:0}.metrics span{display:block;min-height:3.2em;color:var(--muted);font-size:12px}.metrics strong{font-size:23px;display:block;font-weight:500;overflow-wrap:anywhere;font-variant-numeric:tabular-nums}.formula{border-top:1px solid var(--rule);padding-top:12px;margin-top:14px;font-size:13px;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}.value{font:500 20px var(--viz-font-ui)}.legend{display:flex;flex-wrap:wrap;gap:6px 14px;font-size:12px;color:var(--muted);margin:12px 0}.legend span:before{content:'';display:inline-block;width:8px;height:8px;border-radius:2px;background:var(--accent);margin-right:6px}.legend .second:before{background:var(--second)}.check{display:flex;align-items:center;gap:8px;font-size:13px;margin:12px 0}.check input{width:16px;height:16px;flex-shrink:0;accent-color:var(--accent)}select,input[type=text],input[type=number]{background:var(--paper);color:var(--ink);font:inherit;font-size:13px;padding:10px;border:1px solid var(--rule);border-radius:7px;max-width:100%;min-width:0}select{width:100%;min-height:42px}.band{height:20px;display:flex;background:var(--surface);border-radius:4px;overflow:hidden}.band i{height:100%;display:block;background:var(--accent);transition:width .25s}.band i.second{background:var(--second)}button:disabled{cursor:default}.status{font-size:13px;min-height:4.8em}.mono{font-family:var(--viz-font-mono);font-variant-numeric:tabular-nums}.muted{color:var(--muted)}\n@media(max-width:480px){.two{gap:8px}.card{padding:12px 9px}.actions button,.presets button{font-size:12px}.metrics strong{font-size:20px}.metrics{gap:8px}}\n@media(prefers-reduced-motion:reduce){*,*::before,*::after{transition:none!important;animation:none!important}}\n.track-row{margin:16px 0}.track-row>span{display:block;font-size:12px;color:var(--muted);margin-bottom:7px}.track{display:flex;gap:5px;min-width:0}.cell{flex:1;min-width:0;min-height:48px;border:1px solid var(--rule);border-radius:5px;background:var(--surface);display:flex;align-items:center;justify-content:center;text-align:center;padding:5px 3px;font-size:12px;overflow-wrap:anywhere;font-variant-numeric:tabular-nums}.cell.on{background:var(--accent-soft);border-color:var(--accent)}.cell.gold{background:var(--second-soft);border-color:var(--second)}.cell.empty{border-style:dashed;color:var(--muted)}.cell.error{border-color:var(--second);text-decoration:line-through}.choices{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:6px;margin:14px 0}.choices button{min-width:0;padding:10px 4px;font-size:12px;overflow-wrap:anywhere}.choices button[aria-current=true]{border-color:var(--accent);background:var(--accent-soft)}.relation{display:grid;grid-template-columns:minmax(0,1fr) 28px minmax(0,1fr);align-items:center;gap:6px;margin:12px 0}.relation .cell{min-height:72px}.relation>i{text-align:center;font-style:normal;color:var(--accent)}.readout{font-size:13px;white-space:pre-line;min-height:6.4em;overflow-wrap:anywhere}.plot{position:relative;border-left:1px solid var(--rule);border-bottom:1px solid var(--rule);padding:8px 0;margin:18px 0}.plot-row{height:28px;position:relative;margin:4px 0;background:var(--surface)}.plot-row i{position:absolute;inset-block:5px;background:var(--accent);min-width:2px;transition:width .2s,left .2s}.plot-row b{position:relative;padding:4px 6px;font:11px/28px var(--viz-font-ui);background:var(--paper);border-radius:3px}.axis{display:flex;justify-content:space-between;font-size:11px;color:var(--muted)}\n\n/* Action labels must remain readable across locales and narrow embeds. */\n.actions{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}\n.actions button{overflow-wrap:anywhere;white-space:normal;min-height:58px;padding:8px}\nbutton{overflow-wrap:anywhere}\n@media(min-width:640px){.actions{grid-template-columns:repeat(3,minmax(0,1fr))}}\n\n.metrics span{overflow-wrap:anywhere}\n.panel{border:1px solid var(--rule);border-radius:9px;padding:12px;margin:14px 0;min-width:0}.panel h3{font-size:13px;margin:0 0 10px;color:var(--muted)}.source{font:13px/1.8 var(--viz-font-mono);white-space:pre-wrap;overflow-wrap:anywhere;margin:12px 0}.source mark{background:var(--accent-soft);color:var(--ink);border-bottom:2px solid var(--accent)}.source .fault{background:var(--second-soft);border-color:var(--second)}.diagram{display:block;width:100%;height:auto;max-height:320px;margin:14px 0;overflow:visible}.diagram text{fill:var(--ink);font:12px var(--viz-font-ui)}.diagram .edge{stroke:var(--muted);stroke-width:1.5;fill:none}.diagram .edge.on{stroke:var(--accent);stroke-width:3}.diagram .node{fill:var(--surface);stroke:var(--rule);stroke-width:1.5}.diagram .node.on{fill:var(--accent-soft);stroke:var(--accent)}.diagram .node.gold{fill:var(--second-soft);stroke:var(--second)}.log{font:12px/1.8 var(--viz-font-mono);white-space:pre-line;overflow-wrap:anywhere;min-height:7.2em}.table{display:grid;gap:5px;margin:14px 0}.table .tr{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:5px}.table .tr>*{padding:8px 5px;background:var(--surface);border-radius:4px;min-width:0;overflow-wrap:anywhere;font-size:12px}.table .tr .on{background:var(--accent-soft)}.table .tr .gold{background:var(--second-soft)}.choices button{min-height:44px}.metrics strong{min-height:1.6em}.code-line{display:block}.code-line.on{background:var(--accent-soft);border-left:2px solid var(--accent);padding-left:6px}.status-badge{font-size:12px;color:var(--accent)}.tile-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:6px}.tile-grid .cell{min-height:40px}.actions button{min-height:48px}.form-field{display:grid;gap:8px;margin:14px 0;font-size:13px}.form-field input{width:100%;font-family:var(--viz-font-ui)}.metrics.three{grid-template-columns:repeat(3,minmax(0,1fr))}.formula{min-height:3.2em}.presets{flex-wrap:wrap}.presets button{flex-basis:25%;min-height:44px}\n\n.budget{display:flex;height:28px;margin:16px 0;background:var(--surface);border-radius:5px;overflow:hidden}.budget i{display:block;background:var(--accent);transition:width .2s}.budget i.gold{background:var(--second)}.intervals{margin:16px 0}.interval{position:relative;height:36px;background:var(--surface);margin:6px 0;border-radius:4px;overflow:hidden}.interval i{position:absolute;height:100%;background:var(--accent-soft);border-left:2px solid var(--accent)}.interval i.gold{background:var(--second-soft);border-color:var(--second)}.interval span{position:relative;z-index:1;font:12px/36px var(--viz-font-ui);padding-left:7px}.table .tr>*{min-height:4.8em}.formula{white-space:pre-line;min-height:7.2em}.actions:has(>button:only-child){grid-template-columns:1fr}.metrics strong{min-height:3.2em}.rangegrid{display:grid;grid-template-columns:repeat(12,minmax(0,1fr));gap:3px;margin:14px 0}.rangegrid .cell{min-height:30px;padding:3px 1px;font-size:10px}.message{min-height:7.2em;white-space:pre-line;font:13px/1.8 var(--viz-font-ui);overflow-wrap:anywhere}\n/* Allow CJK hanging punctuation without overflowing a metric's border box. */\n.metrics>div{padding-right:4px}.metrics span{line-break:anywhere}\n\n.hist{height:165px;display:grid;grid-template-columns:repeat(10,minmax(0,1fr));gap:4px;align-items:end;padding-bottom:25px;margin-top:15px}.hist div{position:relative;background:var(--surface);border:1px solid var(--rule);min-height:1px}.hist div.on{background:var(--accent-soft);border-color:var(--accent)}.hist small{position:absolute;top:100%;width:100%;text-align:center;font-size:10px}.metrics strong{font-size:18px}","js":"const $=s=>document.querySelector(s);const set=(id,v)=>$('#'+id).textContent=v;const t=k=>viz.t(k);function cells(id,values){$('#'+id).replaceChildren(...values.map(v=>{const e=document.createElement('div');e.className='cell '+(v.cls||'');e.textContent=v.text;e.title=v.title||v.text;return e;}));}function pressed(mode){document.querySelectorAll('[data-mode]').forEach(e=>e.setAttribute('aria-pressed',String(e.dataset.mode===mode)));}const esc=s=>String(s).replace(/[&<>\"']/g,c=>({'&':'&','<':'<','>':'>','\"':'"',\"'\":'''}[c]));const fmt=s=>'{'+[...s].sort().join(', ')+'}';function graph(id,nodes,edges){const el=$('#'+id);el.setAttribute('viewBox','0 0 360 200');el.innerHTML='<defs><marker id=\"arrow-'+id+'\" viewBox=\"0 0 10 10\" refX=\"8\" refY=\"5\" markerWidth=\"5\" markerHeight=\"5\" orient=\"auto-start-reverse\"><path d=\"M 0 0 L 10 5 L 0 10 z\" fill=\"var(--muted)\"/></marker></defs>'+edges.map(e=>{const a=nodes[e[0]],b=nodes[e[1]],dx=b.x-a.x,dy=b.y-a.y,d=Math.hypot(dx,dy)||1;if(edges.some(r=>r[0]===e[1]&&r[1]===e[0])){const nx=-dy/d,ny=dx/d;return '<path class=\"edge '+(e[2]?'on':'')+'\" d=\"M '+(a.x+dx/d*22+nx*12)+' '+(a.y+dy/d*22+ny*12)+' Q '+((a.x+b.x)/2+nx*38)+' '+((a.y+b.y)/2+ny*38)+' '+(b.x-dx/d*25+nx*12)+' '+(b.y-dy/d*25+ny*12)+'\" marker-end=\"url(#arrow-'+id+')\"/>';}return '<line class=\"edge '+(e[2]?'on':'')+'\" x1=\"'+(a.x+dx/d*25)+'\" y1=\"'+(a.y+dy/d*25)+'\" x2=\"'+(b.x-dx/d*28)+'\" y2=\"'+(b.y-dy/d*28)+'\" marker-end=\"url(#arrow-'+id+')\"/>';}).join('')+nodes.map(n=>'<g><circle class=\"node '+(n.cls||'')+'\" cx=\"'+n.x+'\" cy=\"'+n.y+'\" r=\"25\"/><text x=\"'+n.x+'\" y=\"'+(n.y+4)+'\" text-anchor=\"middle\">'+esc(n.name)+'</text>'+(n.sub?'<text x=\"'+n.x+'\" y=\"'+(n.y+43)+'\" text-anchor=\"middle\">'+esc(n.sub)+'</text>':'')+'</g>').join('');}function table(id,rows){$('#'+id).classList.add('table');$('#'+id).replaceChildren(...rows.map(r=>{const row=document.createElement('div');row.className='tr';r.forEach(x=>{const c=document.createElement('div');if(typeof x==='object'){c.textContent=x.text;c.className=x.cls||'';}else c.textContent=x;row.append(c)});return row;}));}function band(id,parts,total){$('#'+id).replaceChildren(...parts.map((p,i)=>{const e=document.createElement('i');e.style.width=(100*p.value/total)+'%';e.className=i%2?'gold':'on';e.title=p.name+': '+p.value;return e}));}function intervals(id,items,total){$('#'+id).replaceChildren(...items.map(p=>{const row=document.createElement('div');row.className='interval';const bar=document.createElement('i');bar.style.left=(100*p.start/total)+'%';bar.style.width=(100*(p.end-p.start)/total)+'%';bar.className=p.cls||'';const label=document.createElement('span');label.textContent=p.label;row.append(bar,label);return row;}));}function draw(){const start=+$('#start').value,n=Math.min(+$('#length').value,10-start),rates=Array.from({length:10},(_,i)=>i<2?100:10),raw=rates.slice(start,start+n).reduce((a,b)=>a+b,0);set('start-value',start);$('#length').max=10-start;$('#length').value=n;set('length-value',n);$('#chart').innerHTML=rates.map((r,i)=>'<div class=\"'+(i>=start&&i<start+n?'on':'')+'\" style=\"height:'+r+'%\"><small>'+i+'</small></div>').join('');set('raw',raw);set('estimate',n?(raw*10/n).toFixed(1):'—');set('truth',280);set('coverage',10*n+'%');set('equation',n?'N̂ = '+raw+' × 10 / '+n+' = '+(raw*10/n).toFixed(1):'time_running = 0; N̂ = ∅');$('#estimate').dataset.value=n?raw*10/n:'';}document.querySelectorAll('input').forEach(e=>e.oninput=draw);draw();","audio":false,"strings":{"chart":"各秒のイベント数。選択された列は実際の観測値","coverage":"時間の被覆率","estimate":"スケーリング推定","heading":"時間で拡大しても、なぜ誤算が生じる可能性があるのか?","intro":"カウンタの実際の稼働ウィンドウを移動し、スケーリング推定値とモデルの既知の総量を比較します。","length":"実際のカウント期間(秒)","raw":"原始観測イベント","reset":"リセット","scope":"10個の1秒間隔。最初の2秒間は毎秒100イベント、後の8秒間は毎秒10イベント。完全な総量は280。これは推定偏差を説明するための人工データであり、PMU の実測値ではありません。","scopeLabel":"モデル範囲と規約","start":"ウィンドウ開始点(秒)","truth":"モデルの真の総量"}}
サンプリング:イベント発生時の実行位置の特定
perf record - o perf.data - F 99 - e cycles:u -- call-graph dwarf -- ./prog
perf report - i perf.data -- stdio
perf annotate - i perf.data -- symbol= my_function
この一連のコマンドは、ユーザーランドのサイクルイベントを選択し、目標周波数 99 Hz でサンプリングしてユーザースタック情報を収集します。実際のサンプリング周波数は、カーネルによるスロットリングやワークロードの影響を受ける可能性があります。特定のバージョンのデフォルトイベントやデフォルト周波数に依存しないでください。
-F は目標サンプリング周波数を指し、-c はイベントサンプリング周期を指し、両者の意味は異なります。cycles サンプルは、CPU がアクティブな状態でのイベントの分布を示します。ブロック期間中、スレッドは実行されていないため、通常の on-CPU スタックは、待機時間の全体像を直接教えてくれるわけではありません。ロック待機や I/O 遅延を解釈するには、スケジューリングイベントやエントリ/リターントレースと組み合わせる必要があります。
スタックの取得方法にもトレードオフがあります。frame pointer 方式は通常、関連するコードがフレームポインタを保持していることを要求します。DWARF 方式は展開のためにユーザースタックを記録し、対応する展開情報が必要であり、サンプリングスタックのサイズはデータ量と切り捨てに影響します。LBR はハードウェアのサポートに依存します。-g は、任意のバイナリで完全な呼び出しチェーンが得られることを保証するスイッチではありません。perf record
シンボル名、ソースコード行、および呼び出しチェーンは異なる情報です。ビルドされたバイナリ、build-id、およびデバッグシンボルを一致させて保持し、別のコンパイルで生成されたファイルを使って古い perf.data を解釈しないでください。インライン展開、末尾呼び出しの最適化、およびコンパイラの最適化は、ソースコードとの対応関係に影響を与えます。リンクとロード は、これらの生成物の関係について解説しています。
フレイムグラフ:呼び出しスタックの集約
CPU フレイムグラフの例:幅はサンプル重みであり、タイムラインではない
main:100%
handle_request:60%
background:40%
parse:40%
store:20%
横方向の配置は類似したスタックを集約するために使用されます。縦方向の上は、呼び出し先を示します
親ボックスは子呼び出しのサンプルを含みます。最上位の広いボックスは最適化候補ですが、最適化後もエンドツーエンドの利益を測定する必要があります。
標準的な集約フレイムグラフにおいて、左右の位置には時間の先后の意味はありません。幅は、その呼び出しスタックに帰属されたサンプル量または重みを表します。色は通常、ボックスを区別するためのものであり、温度や消費時間のレベルを自然に表すものではありません。広いボックスの上を見れば深いホットスポットが見つかりますが、ホットスポットは上位の関数が繰り返し呼び出した結果である可能性もあります。Brendan Gregg:Flame Graphs
FlameGraph スクリプトが PATH に存在する場合:
perf script - i perf.data > out.perf
stackcollapse-perf.pl out.perf > out.folded
flamegraph.pl out.folded > flamegraph.svg
これらのスクリプトは FlameGraph プロジェクト に属しており、perf に同梱されているサブコマンドではありません。サンプルの欠落、シンボルの欠如、またはスタック展開の断絶が発生した場合は、まず入力の品質を修正し、生成されたグラフがきれいに見えるかどうかだけを見てはいけません。カウントサンプルの比率と、イベント周期で加重された比率は、レポート設定に応じて区別する必要があります。
self と children は異なる帰属であり、重複して加算してはならない
あるサンプルスタックが main → handle_request → parse であるとします。self 帰属の場合、これは parse の現在の実行位置に属します。children または呼び出しチェーンの累積帰属の場合、handle_request と main もこのサンプルを含みます。したがって、親と子の行のパーセンテージを加算すると 100% を超えることがあり、これは perf の計算が間違っていることを意味するものではありません。
children 累積を有効にした場合と無効にした場合のレポートをそれぞれ確認し、使用されているパーセンテージの基準を記録してください。非常に広い親ボックスは、自分自身のコードをほとんど実行していない可能性があります。それは単に、すべてのホットスポットの共通呼び出し元であるだけです。この場合、親関数のエントリで1行変更しても、子呼び出しのコストが自然に解消されるわけではありません。perf report の帰属
サンプリングが不足している場合は、まず有効な観察量を増やす
99 Hz で、約 20 ms しか実行しないシングルスレッドの CPU 集中フェーズの場合、理想的なサンプル数は約2回程度であり、実際にはイベントとスケジューリングの影響も受けます。この場合、「ある関数が 50% を占める」のは、実際には1つのサンプルに過ぎず、据此に正確なランキングを行う価値はありません。
同じワークロードを1つのプロセス内で十分に繰り返すことで、初期化と定常状態を別々に処理できます。サンプル数を増やすために、入力規模を全く異なるアルゴリズムパスに変更しないでください。周波数を増やすことにもコストがあり、サンプリングデータの拡大、イベントの欠落、または観察対象プログラムの挙動変化を引き起こす可能性があります。まず有効なサンプル数、欠落のヒント、およびスタックの品質を確認し、その後で周波数の向上を検討してください。
親ボックスが非常に広い場合、親関数自体が遅いのか?
デフォルトで handle の幅が 90% を占めていますが、self は 0 です。これらのサンプルの葉はすべて parse または send にあるためです。main の 100%、handle の 90%、および parse の 60% には重複する帰属が含まれており、加算して総消費時間として扱うことはできません。特定の子ボックスを狭めると、親ボックスはサンプル重みとともに変化し、横方向の配置は関数が順次実行されることを示すものではありません。
図を準備しています 再読み込み
親ボックスが非常に広い場合、親関数自体が遅いのか? フレイムグラフの幅はサンプル重みを表し、親ボックスは子呼び出しを含みます。同じサンプルは複数の祖先に帰属できますが、self はサンプリングされた葉にのみ属します。横軸は時間ではありません。
{"id":"flamegraph-inclusive-self","title":"親ボックスが非常に広い場合、親関数自体が遅いのか?","summary":"フレイムグラフの幅はサンプル重みを表し、親ボックスは子呼び出しを含みます。同じサンプルは複数の祖先に帰属できますが、self はサンプリングされた葉にのみ属します。横軸は時間ではありません。","height":1000,"html":"<h2 data-i18n=\"heading\"></h2><p class=\"intro\" data-i18n=\"intro\"></p><div class=\"control\"><label for=\"parse\" data-i18n=\"parse\"></label><output for=\"parse\" id=\"parse-value\"></output><input id=\"parse\" type=\"range\" min=\"0\" max=\"100\" value=\"60\" step=\"1\"></div><div class=\"control\"><label for=\"send\" data-i18n=\"send\"></label><output for=\"send\" id=\"send-value\"></output><input id=\"send\" type=\"range\" min=\"0\" max=\"100\" value=\"30\" step=\"1\"></div><div class=\"control\"><label for=\"idle\" data-i18n=\"idle\"></label><output for=\"idle\" id=\"idle-value\"></output><input id=\"idle\" type=\"range\" min=\"0\" max=\"100\" value=\"10\" step=\"1\"></div><div class=\"flame\" id=\"flame\"><div class=\"flame-row\"><button data-fn=\"parse\">parse</button><button data-fn=\"send\">send</button><i id=\"blank\"></i></div><div class=\"flame-row\"><button data-fn=\"handle\">handle</button><button data-fn=\"idle\">idle</button></div><div class=\"flame-row\"><button data-fn=\"main\">main</button></div></div><div class=\"metrics\"><div><span data-i18n=\"selected\"></span><strong id=\"selected\"></strong></div><div><span data-i18n=\"self\"></span><strong id=\"self\"></strong></div><div><span data-i18n=\"inclusive\"></span><strong id=\"inclusive\"></strong></div><div><span data-i18n=\"total\"></span><strong id=\"total\"></strong></div></div><p class=\"formula\" id=\"equation\"></p><details class=\"scope\"><summary data-i18n=\"scopeLabel\"></summary><p data-i18n=\"scope\"></p></details>","css":".intro{margin:8px 0 18px;color:var(--muted);font-size:14px}.caption,.hint{font-size:12px;color:var(--muted);margin-top:12px}.control{display:grid;grid-template-columns:minmax(0,1fr) auto;gap:6px 12px;align-items:center;margin:14px 0;font-size:13px}.control label{font-size:13px}.control output{color:var(--accent);min-width:5em;text-align:right;font-variant-numeric:tabular-nums}.control input{grid-column:1/-1;width:100%;margin:0}.presets{display:flex;gap:4px;align-items:stretch}.presets button{flex:1 1 0;min-width:0;overflow-wrap:anywhere;font-size:13px}.actions{display:flex;gap:8px;flex-wrap:wrap;margin:14px 0}.actions button{flex:1;min-width:0;font-size:13px;min-height:42px}.copy{display:grid;font-size:14px;margin:16px 0}.copy>*{grid-area:1/1;margin:0}.reserve{visibility:hidden}.scope{margin-top:14px;color:var(--muted);font-size:12px}.scope summary{padding:8px 0;cursor:pointer}.scope p{margin-top:10px}.two{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;margin:16px 0}.card{border:1px solid var(--rule);border-radius:9px;padding:14px;min-width:0}.card header{font-size:14px;font-weight:600;margin-bottom:10px}.card button{font-size:12px;width:100%;min-width:0}.card small{display:block;font-size:12px;color:var(--muted)}.card code{font-size:12px;overflow-wrap:anywhere}.metrics{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px;margin:16px 0}.metrics>div{border-left:2px solid var(--accent);padding-left:10px;min-width:0}.metrics span{display:block;min-height:3.2em;color:var(--muted);font-size:12px}.metrics strong{font-size:23px;display:block;font-weight:500;overflow-wrap:anywhere;font-variant-numeric:tabular-nums}.formula{border-top:1px solid var(--rule);padding-top:12px;margin-top:14px;font-size:13px;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}.value{font:500 20px var(--viz-font-ui)}.legend{display:flex;flex-wrap:wrap;gap:6px 14px;font-size:12px;color:var(--muted);margin:12px 0}.legend span:before{content:'';display:inline-block;width:8px;height:8px;border-radius:2px;background:var(--accent);margin-right:6px}.legend .second:before{background:var(--second)}.check{display:flex;align-items:center;gap:8px;font-size:13px;margin:12px 0}.check input{width:16px;height:16px;flex-shrink:0;accent-color:var(--accent)}select,input[type=text],input[type=number]{background:var(--paper);color:var(--ink);font:inherit;font-size:13px;padding:10px;border:1px solid var(--rule);border-radius:7px;max-width:100%;min-width:0}select{width:100%;min-height:42px}.band{height:20px;display:flex;background:var(--surface);border-radius:4px;overflow:hidden}.band i{height:100%;display:block;background:var(--accent);transition:width .25s}.band i.second{background:var(--second)}button:disabled{cursor:default}.status{font-size:13px;min-height:4.8em}.mono{font-family:var(--viz-font-mono);font-variant-numeric:tabular-nums}.muted{color:var(--muted)}\n@media(max-width:480px){.two{gap:8px}.card{padding:12px 9px}.actions button,.presets button{font-size:12px}.metrics strong{font-size:20px}.metrics{gap:8px}}\n@media(prefers-reduced-motion:reduce){*,*::before,*::after{transition:none!important;animation:none!important}}\n.track-row{margin:16px 0}.track-row>span{display:block;font-size:12px;color:var(--muted);margin-bottom:7px}.track{display:flex;gap:5px;min-width:0}.cell{flex:1;min-width:0;min-height:48px;border:1px solid var(--rule);border-radius:5px;background:var(--surface);display:flex;align-items:center;justify-content:center;text-align:center;padding:5px 3px;font-size:12px;overflow-wrap:anywhere;font-variant-numeric:tabular-nums}.cell.on{background:var(--accent-soft);border-color:var(--accent)}.cell.gold{background:var(--second-soft);border-color:var(--second)}.cell.empty{border-style:dashed;color:var(--muted)}.cell.error{border-color:var(--second);text-decoration:line-through}.choices{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:6px;margin:14px 0}.choices button{min-width:0;padding:10px 4px;font-size:12px;overflow-wrap:anywhere}.choices button[aria-current=true]{border-color:var(--accent);background:var(--accent-soft)}.relation{display:grid;grid-template-columns:minmax(0,1fr) 28px minmax(0,1fr);align-items:center;gap:6px;margin:12px 0}.relation .cell{min-height:72px}.relation>i{text-align:center;font-style:normal;color:var(--accent)}.readout{font-size:13px;white-space:pre-line;min-height:6.4em;overflow-wrap:anywhere}.plot{position:relative;border-left:1px solid var(--rule);border-bottom:1px solid var(--rule);padding:8px 0;margin:18px 0}.plot-row{height:28px;position:relative;margin:4px 0;background:var(--surface)}.plot-row i{position:absolute;inset-block:5px;background:var(--accent);min-width:2px;transition:width .2s,left .2s}.plot-row b{position:relative;padding:4px 6px;font:11px/28px var(--viz-font-ui);background:var(--paper);border-radius:3px}.axis{display:flex;justify-content:space-between;font-size:11px;color:var(--muted)}\n\n/* Action labels must remain readable across locales and narrow embeds. */\n.actions{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}\n.actions button{overflow-wrap:anywhere;white-space:normal;min-height:58px;padding:8px}\nbutton{overflow-wrap:anywhere}\n@media(min-width:640px){.actions{grid-template-columns:repeat(3,minmax(0,1fr))}}\n\n.metrics span{overflow-wrap:anywhere}\n.panel{border:1px solid var(--rule);border-radius:9px;padding:12px;margin:14px 0;min-width:0}.panel h3{font-size:13px;margin:0 0 10px;color:var(--muted)}.source{font:13px/1.8 var(--viz-font-mono);white-space:pre-wrap;overflow-wrap:anywhere;margin:12px 0}.source mark{background:var(--accent-soft);color:var(--ink);border-bottom:2px solid var(--accent)}.source .fault{background:var(--second-soft);border-color:var(--second)}.diagram{display:block;width:100%;height:auto;max-height:320px;margin:14px 0;overflow:visible}.diagram text{fill:var(--ink);font:12px var(--viz-font-ui)}.diagram .edge{stroke:var(--muted);stroke-width:1.5;fill:none}.diagram .edge.on{stroke:var(--accent);stroke-width:3}.diagram .node{fill:var(--surface);stroke:var(--rule);stroke-width:1.5}.diagram .node.on{fill:var(--accent-soft);stroke:var(--accent)}.diagram .node.gold{fill:var(--second-soft);stroke:var(--second)}.log{font:12px/1.8 var(--viz-font-mono);white-space:pre-line;overflow-wrap:anywhere;min-height:7.2em}.table{display:grid;gap:5px;margin:14px 0}.table .tr{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:5px}.table .tr>*{padding:8px 5px;background:var(--surface);border-radius:4px;min-width:0;overflow-wrap:anywhere;font-size:12px}.table .tr .on{background:var(--accent-soft)}.table .tr .gold{background:var(--second-soft)}.choices button{min-height:44px}.metrics strong{min-height:1.6em}.code-line{display:block}.code-line.on{background:var(--accent-soft);border-left:2px solid var(--accent);padding-left:6px}.status-badge{font-size:12px;color:var(--accent)}.tile-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:6px}.tile-grid .cell{min-height:40px}.actions button{min-height:48px}.form-field{display:grid;gap:8px;margin:14px 0;font-size:13px}.form-field input{width:100%;font-family:var(--viz-font-ui)}.metrics.three{grid-template-columns:repeat(3,minmax(0,1fr))}.formula{min-height:3.2em}.presets{flex-wrap:wrap}.presets button{flex-basis:25%;min-height:44px}\n\n.budget{display:flex;height:28px;margin:16px 0;background:var(--surface);border-radius:5px;overflow:hidden}.budget i{display:block;background:var(--accent);transition:width .2s}.budget i.gold{background:var(--second)}.intervals{margin:16px 0}.interval{position:relative;height:36px;background:var(--surface);margin:6px 0;border-radius:4px;overflow:hidden}.interval i{position:absolute;height:100%;background:var(--accent-soft);border-left:2px solid var(--accent)}.interval i.gold{background:var(--second-soft);border-color:var(--second)}.interval span{position:relative;z-index:1;font:12px/36px var(--viz-font-ui);padding-left:7px}.table .tr>*{min-height:4.8em}.formula{white-space:pre-line;min-height:7.2em}.actions:has(>button:only-child){grid-template-columns:1fr}.metrics strong{min-height:3.2em}.rangegrid{display:grid;grid-template-columns:repeat(12,minmax(0,1fr));gap:3px;margin:14px 0}.rangegrid .cell{min-height:30px;padding:3px 1px;font-size:10px}.message{min-height:7.2em;white-space:pre-line;font:13px/1.8 var(--viz-font-ui);overflow-wrap:anywhere}\n/* Allow CJK hanging punctuation without overflowing a metric's border box. */\n.metrics>div{padding-right:4px}.metrics span{line-break:anywhere}\n\n.flame{margin:18px 0}.flame-row{display:flex;height:46px;gap:0}.flame-row button{padding:0;border-radius:3px;min-width:0;flex-shrink:0;overflow:hidden;font:12px var(--viz-font-ui);background:var(--accent-soft);border:1px solid var(--paper)}.flame-row button[aria-pressed=true]{background:var(--second-soft);outline:2px solid var(--second);outline-offset:-2px}.flame-row i{display:block}.metrics strong{font-size:18px}","js":"const $=s=>document.querySelector(s);const set=(id,v)=>$('#'+id).textContent=v;const t=k=>viz.t(k);function cells(id,values){$('#'+id).replaceChildren(...values.map(v=>{const e=document.createElement('div');e.className='cell '+(v.cls||'');e.textContent=v.text;e.title=v.title||v.text;return e;}));}function pressed(mode){document.querySelectorAll('[data-mode]').forEach(e=>e.setAttribute('aria-pressed',String(e.dataset.mode===mode)));}const esc=s=>String(s).replace(/[&<>\"']/g,c=>({'&':'&','<':'<','>':'>','\"':'"',\"'\":'''}[c]));const fmt=s=>'{'+[...s].sort().join(', ')+'}';function graph(id,nodes,edges){const el=$('#'+id);el.setAttribute('viewBox','0 0 360 200');el.innerHTML='<defs><marker id=\"arrow-'+id+'\" viewBox=\"0 0 10 10\" refX=\"8\" refY=\"5\" markerWidth=\"5\" markerHeight=\"5\" orient=\"auto-start-reverse\"><path d=\"M 0 0 L 10 5 L 0 10 z\" fill=\"var(--muted)\"/></marker></defs>'+edges.map(e=>{const a=nodes[e[0]],b=nodes[e[1]],dx=b.x-a.x,dy=b.y-a.y,d=Math.hypot(dx,dy)||1;if(edges.some(r=>r[0]===e[1]&&r[1]===e[0])){const nx=-dy/d,ny=dx/d;return '<path class=\"edge '+(e[2]?'on':'')+'\" d=\"M '+(a.x+dx/d*22+nx*12)+' '+(a.y+dy/d*22+ny*12)+' Q '+((a.x+b.x)/2+nx*38)+' '+((a.y+b.y)/2+ny*38)+' '+(b.x-dx/d*25+nx*12)+' '+(b.y-dy/d*25+ny*12)+'\" marker-end=\"url(#arrow-'+id+')\"/>';}return '<line class=\"edge '+(e[2]?'on':'')+'\" x1=\"'+(a.x+dx/d*25)+'\" y1=\"'+(a.y+dy/d*25)+'\" x2=\"'+(b.x-dx/d*28)+'\" y2=\"'+(b.y-dy/d*28)+'\" marker-end=\"url(#arrow-'+id+')\"/>';}).join('')+nodes.map(n=>'<g><circle class=\"node '+(n.cls||'')+'\" cx=\"'+n.x+'\" cy=\"'+n.y+'\" r=\"25\"/><text x=\"'+n.x+'\" y=\"'+(n.y+4)+'\" text-anchor=\"middle\">'+esc(n.name)+'</text>'+(n.sub?'<text x=\"'+n.x+'\" y=\"'+(n.y+43)+'\" text-anchor=\"middle\">'+esc(n.sub)+'</text>':'')+'</g>').join('');}function table(id,rows){$('#'+id).classList.add('table');$('#'+id).replaceChildren(...rows.map(r=>{const row=document.createElement('div');row.className='tr';r.forEach(x=>{const c=document.createElement('div');if(typeof x==='object'){c.textContent=x.text;c.className=x.cls||'';}else c.textContent=x;row.append(c)});return row;}));}function band(id,parts,total){$('#'+id).replaceChildren(...parts.map((p,i)=>{const e=document.createElement('i');e.style.width=(100*p.value/total)+'%';e.className=i%2?'gold':'on';e.title=p.name+': '+p.value;return e}));}function intervals(id,items,total){$('#'+id).replaceChildren(...items.map(p=>{const row=document.createElement('div');row.className='interval';const bar=document.createElement('i');bar.style.left=(100*p.start/total)+'%';bar.style.width=(100*(p.end-p.start)/total)+'%';bar.className=p.cls||'';const label=document.createElement('span');label.textContent=p.label;row.append(bar,label);return row;}));}let selected='handle';function draw(){const a=+$('#parse').value,b=+$('#send').value,c=+$('#idle').value,total=a+b+c,w={parse:a,send:b,idle:c,handle:a+b,main:total},own={parse:a,send:b,idle:c,handle:0,main:0};for(const [id,v]of[['parse',a],['send',b],['idle',c]])set(id+'-value',v);document.querySelectorAll('[data-fn]').forEach(e=>{const n=e.dataset.fn;e.style.width=total?100*w[n]/total+'%':n==='main'?'100%':'0%';e.textContent=total&&w[n]/total<.09?'':n;e.setAttribute('aria-label',n+' · '+w[n]);e.setAttribute('aria-pressed',String(n===selected));e.disabled=!w[n];e.style.visibility=w[n]?'visible':'hidden';});$('#blank').style.width=(total?c/total*100:100)+'%';set('selected',selected);set('self',own[selected]);set('inclusive',w[selected]);set('total',total);set('equation',total?'self = '+own[selected]+'/'+total+'; inclusive = '+w[selected]+'/'+total:t('empty'));$('#flame').dataset.weights=JSON.stringify(w);}document.querySelectorAll('input').forEach(e=>e.oninput=draw);document.querySelectorAll('[data-fn]').forEach(e=>e.onclick=()=>{selected=e.dataset.fn;draw()});draw();","audio":false,"strings":{"empty":"サンプルがないため、比率を計算できません","heading":"親ボックスが非常に広い場合、親関数自体が遅いのか?","idle":"idle サンプル重み","inclusive":"子呼び出しを含む重み","intro":"3つのサンプルスタックの重みを変更し、任意のボックスをクリックして self と累積帰属を確認します。","parse":"parse サンプル重み","reset":"リセット","scope":"3つの完全なスタック:main→handle→parse、main→handle→send、main→idle。非負の整数重み。画面は集約の例示であり、実際の perf.data ではありません。","scopeLabel":"モデル範囲と規約","selected":"選択された関数","self":"self 重み","send":"send サンプル重み","total":"総サンプル重み"}}
正確なサンプリング:位置のずれの削減
通常のサンプリングでは、イベントのオーバーフローから命令位置の記録までには、skid(ずれ)が発生する可能性があります。ホットスポットとしてマークされた命令が、必ずしもイベントをトリガーした命令であるとは限りません。正確なサンプリング機能は、特定の PMU やイベントバインディングに依存します。Intel PEBS と AMD IBS は異なるハードウェアメカニズムです。
perf の p 修飾子は、サンプリング IP の精度に対する要件を示します。pp はゼロ skid を要求し、ppp の厳格なサポートはイベントとハードウェアによって決定されます。cycles:ppp を「すべてのマシンで適用可能な最高精度モード」として記述してはいけません。サポートされている正確なイベントをまず照会し、そのイベント定義に基づいて結果を解釈してください。perf list のイベント修飾子
正確な IP であっても、その命令が根本原因であると証明されたわけではありません。キャッシュ階層、データソース、依存チェーン、または上位の制御フローなど、さらに観察が必要な要素が残っています。Top-down 指標は、マイクロアーキテクチャに基づいて実行スロットなどのリソースを分類しますが、サポート範囲と指標の定義は、本机の PMU に基づいて確認する必要があり、「特定の世代以降はすべて同じである」と固定して記述してはいけません。
ダイナミックプロービングと観察範囲
perf probe - x ./prog -- add ' work=my_function'
perf probe -- list
ツールは実際のイベント名を報告します。その後、その名前を perf record -e に渡します。コマンドのシンボル解析には、対象にプローブ可能な位置が実際に存在する必要があります。ソースコード行とローカル変数の解析には、DWARF 情報が必要になる場合もあります。インライン展開、最適化で削除された変数、または1つのシンボルが複数の位置に対応する場合、「任意の関数、任意の行でプローブできる」と単純化することはできません。
記録パラメータは、対象関数の ABI とプローブ位置を確認する必要があります。x86-64 のレジスタの例をそのまま arm64 に適用してはいけません。終了時は、共有トレース機構をクリアするのではなく、実際のイベントの完全な名前を使って、今回作成した probe を削除してください。perf probe マニュアル
プロセスへのアタッチには -p PID を、スレッドには -t TID を使用できます。システム全体の短時間収集には perf record -a ... -- sleep 10 を使用できますが、他のワークロードも観察されます。CPU、プロセス、ユーザーランド、またはカーネルランドを選択する際は、結論が実際の範囲と一致するようにしてください。
CPU ホットスポットの比率を、リクエストレイテンシの比率として直接見なしてはならない
単一リクエストの直列実行を仮定:CPU 20 ms + 待機 100 ms
CPU 20
待機 100 ms
parse が CPU の 40% を占める:8 ms。最適化して 4 ms にすると、4 ms 節約
CPU 16
待機は依然として 100 ms
総レイテンシは 120 → 116 ms に減少し、約 3.3% の削減。これは、図に示すように重なりがなく、他の要因が変化しないという仮定にのみ適用されます。
リクエスト時間の勘定で期待される利益を制約する
CPU サンプルが時間的に代表性を持っていると仮定すると、parse は 20 ms の CPU 作業の 40%、つまり 8 ms を占めます。これを2倍に高速化しても、節約できるのは 4 ms に過ぎません。リクエストに 100 ms の直列待機がある場合、全体は 120 ms から 116 ms に減少し、「ホットスポットが2倍に高速化されたので、インターフェースも2倍に高速化した」わけではありません。
from math import isclose
cpu_ms , wait_ms = 20 , 100
hot_ms = cpu_ms * 0. 4
before = cpu_ms + wait_ms
after = before - hot_ms + hot_ms / 2
assert ( before , after ) == ( 120 , 116 )
assert isclose ( 1 - after / before , 1 / 30 )
マルチスレッドパイプライン、非同期オーバーラップ、およびキューイングシステムでは、各スレッドの CPU 時間を単一リクエストのレイテンシに単純に加算することはできません。高負荷下では、CPU サービス時間の削減がキューイングの減少につながる可能性がありますが、低負荷下ではレイテンシの変化がほとんど見られない可能性があります。負荷レベル、完了スループット、およびレイテンシ分布を同時に記録し、単一リクエストモデルをすべてのシナリオに外挿しないようにしてください。
検証可能な最適化結論の形成
まず入力と完了した作業量を固定し、カウントし、その後サンプリングで候補を特定します。説明可能なメカニズムを提案して修正し、最後に同じエンドツーエンドの指標に戻って検証します。Amdahl の法則は、総消費時間の 10% を占める部分を完全に除去しても、元の消費時間を 10% しか削減できないことを警告しています。
ホットスポットの比率が低下したが、リクエスト時間が変化しない場合、ボトルネックが移動したか、負荷が変化したか、または元の指標が CPU の一部しかカバーしていない可能性があります。コマンド、マシン情報、シンボルバージョン、およびベースラインを保持することで、「速く見える」ことを検証可能な結論にすることができます。
キーワード:perf, perf_events, PMU, IPC, multiplexing, call graph, flame graph, PEBS, IBS, skid, sampling
結論ごとに最小限のエビデンスパッケージを保持する
ソースコードまたはビルド識別子、バイナリの build-id、完全なコマンド、イベント修飾子、入力規模、完了数、実行回数、および測定環境を保持してください。サンプリングについては、スタックの取得方法、データファイル、および欠落/スロットリング状況を別途記録します。カウントについては、イベントの実行比率とサポートされていない項目を別途記録します。
新しいバージョンがベースラインより 1% 速いだけであり、繰り返し実行のばらつきがこの値よりも大きい場合は、小さな差異を修正に帰因する急がず、まず実験を改善するか、代表性のある繰り返し回数を増やしてください。A/B を交互に実行して時間によるドリフトの影響を軽減し、コールドスタートと定常状態を別々に報告します。最終的な結論は、「特定の負荷下でどのくらい時間またはリソースを削減したか」に落とし込むべきであり、perf の指標はメカニズムを説明するために使用します。