[Python-checkins] Add pair counts to stats output and summary. (GH-31324)

markshannon webhook-mailer at python.org
Mon Feb 14 10:54:01 EST 2022


https://github.com/python/cpython/commit/0ade875ebe0d83709ec338616d6abe439c81f29b
commit: 0ade875ebe0d83709ec338616d6abe439c81f29b
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2022-02-14T15:53:38Z
summary:

Add pair counts to stats output and summary. (GH-31324)

files:
M Python/specialize.c
M Tools/scripts/summarize_stats.py

diff --git a/Python/specialize.c b/Python/specialize.c
index b54a2ecd506fc..0e1ffad1b9159 100644
--- a/Python/specialize.c
+++ b/Python/specialize.c
@@ -174,13 +174,13 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
 {
     /* Mark some opcodes as specializable for stats,
      * even though we don't specialize them yet. */
-    fprintf(out, "    opcode[%d].specializable : 1\n", FOR_ITER);
-    fprintf(out, "    opcode[%d].specializable : 1\n", PRECALL_FUNCTION);
-    fprintf(out, "    opcode[%d].specializable : 1\n", PRECALL_METHOD);
-    fprintf(out, "    opcode[%d].specializable : 1\n", UNPACK_SEQUENCE);
+    fprintf(out, "opcode[%d].specializable : 1\n", FOR_ITER);
+    fprintf(out, "opcode[%d].specializable : 1\n", PRECALL_FUNCTION);
+    fprintf(out, "opcode[%d].specializable : 1\n", PRECALL_METHOD);
+    fprintf(out, "opcode[%d].specializable : 1\n", UNPACK_SEQUENCE);
     for (int i = 0; i < 256; i++) {
         if (adaptive_opcodes[i]) {
-            fprintf(out, "    opcode[%d].specializable : 1\n", i);
+            fprintf(out, "opcode[%d].specializable : 1\n", i);
         }
         PRINT_STAT(i, specialization.success);
         PRINT_STAT(i, specialization.failure);
@@ -196,6 +196,12 @@ print_spec_stats(FILE *out, OpcodeStats *stats)
                     PRIu64 "\n", i, j, val);
             }
         }
+        for(int j = 0; j < 256; j++) {
+            if (stats[i].pair_count[j]) {
+                fprintf(out, "opcode[%d].pair_count[%d] : %" PRIu64 "\n",
+                        i, j, stats[i].pair_count[j]);
+            }
+        }
     }
 }
 #undef PRINT_STAT
diff --git a/Tools/scripts/summarize_stats.py b/Tools/scripts/summarize_stats.py
index f6e5b333b09fc..5805ba46afed6 100644
--- a/Tools/scripts/summarize_stats.py
+++ b/Tools/scripts/summarize_stats.py
@@ -6,6 +6,8 @@
 import os.path
 import opcode
 from datetime import date
+import itertools
+import argparse
 
 if os.name == "nt":
     DEFAULT_DIR = "c:\\temp\\py_stats\\"
@@ -80,7 +82,7 @@ def gather_stats():
             for line in fd:
                 key, value = line.split(":")
                 key = key.strip()
-                value = int(value.strip())
+                value = int(value)
                 stats[key] += value
     return stats
 
@@ -268,14 +270,42 @@ def emit_object_stats(stats):
                 rows.append((label, value, materialize))
         emit_table(("",  "Count:", "Ratio:"), rows)
 
-def main():
-    stats = gather_stats()
-    opcode_stats = extract_opcode_stats(stats)
+def get_total(opcode_stats):
     total = 0
-    for i, opcode_stat in enumerate(opcode_stats):
+    for opcode_stat in opcode_stats:
         if "execution_count" in opcode_stat:
             total += opcode_stat['execution_count']
+    return total
+
+def emit_pair_counts(opcode_stats, total):
+    with Section("Pair counts", summary="Pair counts for top 100 pairs"):
+        pair_counts = []
+        for i, opcode_stat in enumerate(opcode_stats):
+            if i == 0:
+                continue
+            for key, value in opcode_stat.items():
+                if key.startswith("pair_count"):
+                    x, _, _ = key[11:].partition("]")
+                    if value:
+                        pair_counts.append((value, (i, int(x))))
+        pair_counts.sort(reverse=True)
+        cumulative = 0
+        rows = []
+        for (count, pair) in itertools.islice(pair_counts, 100):
+            i, j = pair
+            cumulative += count
+            rows.append((opname[i] + " " + opname[j], count, f"{100*count/total:0.1f}%",
+                        f"{100*cumulative/total:0.1f}%"))
+        emit_table(("Pair", "Count:", "Self:", "Cumulative:"),
+            rows
+        )
+
+def main():
+    stats = gather_stats()
+    opcode_stats = extract_opcode_stats(stats)
+    total = get_total(opcode_stats)
     emit_execution_counts(opcode_stats, total)
+    emit_pair_counts(opcode_stats, total)
     emit_specialization_stats(opcode_stats)
     emit_specialization_overview(opcode_stats, total)
     emit_call_stats(stats)



More information about the Python-checkins mailing list