Skip to content

Commit 0ade875

Browse files
authored
Add pair counts to stats output and summary. (GH-31324)
1 parent 15ee555 commit 0ade875

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

Python/specialize.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
174174
{
175175
/* Mark some opcodes as specializable for stats,
176176
* even though we don't specialize them yet. */
177-
fprintf(out, " opcode[%d].specializable : 1\n", FOR_ITER);
178-
fprintf(out, " opcode[%d].specializable : 1\n", PRECALL_FUNCTION);
179-
fprintf(out, " opcode[%d].specializable : 1\n", PRECALL_METHOD);
180-
fprintf(out, " opcode[%d].specializable : 1\n", UNPACK_SEQUENCE);
177+
fprintf(out, "opcode[%d].specializable : 1\n", FOR_ITER);
178+
fprintf(out, "opcode[%d].specializable : 1\n", PRECALL_FUNCTION);
179+
fprintf(out, "opcode[%d].specializable : 1\n", PRECALL_METHOD);
180+
fprintf(out, "opcode[%d].specializable : 1\n", UNPACK_SEQUENCE);
181181
for (int i = 0; i < 256; i++) {
182182
if (adaptive_opcodes[i]) {
183-
fprintf(out, " opcode[%d].specializable : 1\n", i);
183+
fprintf(out, "opcode[%d].specializable : 1\n", i);
184184
}
185185
PRINT_STAT(i, specialization.success);
186186
PRINT_STAT(i, specialization.failure);
@@ -196,6 +196,12 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
196196
PRIu64 "\n", i, j, val);
197197
}
198198
}
199+
for(int j = 0; j < 256; j++) {
200+
if (stats[i].pair_count[j]) {
201+
fprintf(out, "opcode[%d].pair_count[%d] : %" PRIu64 "\n",
202+
i, j, stats[i].pair_count[j]);
203+
}
204+
}
199205
}
200206
}
201207
#undef PRINT_STAT

Tools/scripts/summarize_stats.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import os.path
77
import opcode
88
from datetime import date
9+
import itertools
10+
import argparse
911

1012
if os.name == "nt":
1113
DEFAULT_DIR = "c:\\temp\\py_stats\\"
@@ -80,7 +82,7 @@ def gather_stats():
8082
for line in fd:
8183
key, value = line.split(":")
8284
key = key.strip()
83-
value = int(value.strip())
85+
value = int(value)
8486
stats[key] += value
8587
return stats
8688

@@ -268,14 +270,42 @@ def emit_object_stats(stats):
268270
rows.append((label, value, materialize))
269271
emit_table(("", "Count:", "Ratio:"), rows)
270272

271-
def main():
272-
stats = gather_stats()
273-
opcode_stats = extract_opcode_stats(stats)
273+
def get_total(opcode_stats):
274274
total = 0
275-
for i, opcode_stat in enumerate(opcode_stats):
275+
for opcode_stat in opcode_stats:
276276
if "execution_count" in opcode_stat:
277277
total += opcode_stat['execution_count']
278+
return total
279+
280+
def emit_pair_counts(opcode_stats, total):
281+
with Section("Pair counts", summary="Pair counts for top 100 pairs"):
282+
pair_counts = []
283+
for i, opcode_stat in enumerate(opcode_stats):
284+
if i == 0:
285+
continue
286+
for key, value in opcode_stat.items():
287+
if key.startswith("pair_count"):
288+
x, _, _ = key[11:].partition("]")
289+
if value:
290+
pair_counts.append((value, (i, int(x))))
291+
pair_counts.sort(reverse=True)
292+
cumulative = 0
293+
rows = []
294+
for (count, pair) in itertools.islice(pair_counts, 100):
295+
i, j = pair
296+
cumulative += count
297+
rows.append((opname[i] + " " + opname[j], count, f"{100*count/total:0.1f}%",
298+
f"{100*cumulative/total:0.1f}%"))
299+
emit_table(("Pair", "Count:", "Self:", "Cumulative:"),
300+
rows
301+
)
302+
303+
def main():
304+
stats = gather_stats()
305+
opcode_stats = extract_opcode_stats(stats)
306+
total = get_total(opcode_stats)
278307
emit_execution_counts(opcode_stats, total)
308+
emit_pair_counts(opcode_stats, total)
279309
emit_specialization_stats(opcode_stats)
280310
emit_specialization_overview(opcode_stats, total)
281311
emit_call_stats(stats)

0 commit comments

Comments
 (0)