-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind_edges_to_cut.py
More file actions
283 lines (223 loc) · 9.99 KB
/
Copy pathfind_edges_to_cut.py
File metadata and controls
283 lines (223 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
import argparse
import csv
import logging
import os
import sys
from typing import List, Optional, Set, Tuple
import networkx as nx
from cut_header import compute_added_sizes
from include_analysis import IncludeAnalysisOutput, ParseError, load_include_analysis
from utils import create_graph_from_include_analysis
def create_include_graph(
include_analysis: IncludeAnalysisOutput,
skips: Optional[Tuple[Tuple[str, str]]],
) -> nx.DiGraph:
DG: nx.DiGraph = create_graph_from_include_analysis(include_analysis)
files = include_analysis["files"]
if skips:
for includer, included in skips:
if includer in files and included in files:
includer_idx = files.index(includer)
included_idx = files.index(included)
if DG.has_edge(includer_idx, included_idx):
DG.remove_edge(includer_idx, included_idx)
else:
logging.warning(f"Skip edge {includer} -> {included} not found in include graph")
else:
logging.warning(f"Skip edge {includer} -> {included} not found in include analysis")
return DG
def find_entry_points(
include_analysis: IncludeAnalysisOutput,
DG: nx.DiGraph,
subset: Set[str],
) -> List[str]:
"""Find entry points into a subset of nodes.
An entry point is a node in the subset that is still reachable from
at least one root after all edges *between* nodes in the subset have
been removed. This identifies which subset nodes are the first to
appear on paths from roots into the subset cluster.
"""
files = include_analysis["files"]
file_idx_lookup = {filename: idx for idx, filename in enumerate(files)}
subset_indices = {file_idx_lookup[f] for f in subset if f in file_idx_lookup}
DG2 = DG.copy()
# Remove all edges between nodes in the subset
edges_to_remove = []
for u in subset_indices:
for _, v in DG2.out_edges(u):
if v in subset_indices:
edges_to_remove.append((u, v))
for v, _ in DG2.in_edges(u):
if v in subset_indices:
edges_to_remove.append((v, u))
DG2.remove_edges_from(edges_to_remove)
# For each root, find which subset nodes are still reachable
entry_points = set()
root_indices = set()
for root in include_analysis["roots"]:
if root in file_idx_lookup:
root_indices.add(file_idx_lookup[root])
for root_idx in root_indices:
reachable = nx.descendants(DG2, root_idx)
reachable_subset = reachable & subset_indices
entry_points.update(reachable_subset)
return sorted(files[idx] for idx in entry_points)
def find_top_edges(
include_analysis: IncludeAnalysisOutput,
subset: Set[str],
top_n: int = 10,
ignores: Optional[Set[Tuple[str, str]]] = None,
) -> List[Tuple[str, str, float]]:
"""Find the top N edges between nodes inside the subset, ranked by prevalence.
Returns a list of (includer, included, prevalence) tuples sorted by
prevalence descending.
"""
root_count = len(include_analysis["roots"])
edges = []
for included in subset:
for includer in include_analysis["included_by"].get(included, []):
if includer not in subset:
continue
if ignores and (includer, included) in ignores:
continue
prevalence = (100.0 * include_analysis["prevalence"][includer]) / root_count
edges.append((includer, included, prevalence))
# Sort by prevalence descending, take top N
edges.sort(key=lambda x: x[2], reverse=True)
return edges[:top_n]
def find_top_edges_by_dominators(
include_analysis: IncludeAnalysisOutput,
subset: Set[str],
dominators: dict,
top_n: int = 10,
ignores: Optional[Set[Tuple[str, str]]] = None,
) -> List[Tuple[str, str, float, int]]:
"""Find the top N edges between nodes inside the subset, ranked by dominator count.
Returns a list of (includer, included, prevalence, dominator_count) tuples
sorted by dominator count descending.
"""
root_count = len(include_analysis["roots"])
edges = []
for included in subset:
for includer in include_analysis["included_by"].get(included, []):
if includer not in subset:
continue
if ignores and (includer, included) in ignores:
continue
prevalence = (100.0 * include_analysis["prevalence"][includer]) / root_count
dom_count = dominators.get((includer, included), 0)
edges.append((includer, included, prevalence, dom_count))
# Sort by dominator count descending, take top N
edges.sort(key=lambda x: x[3], reverse=True)
return edges[:top_n]
# Adapted from analyze_includes.py in Chromium
def compute_doms(DG: nx.DiGraph, roots):
# Give each node a size of 1 to represent one file
sizes = {data["filename"]: 1 for _, data in DG.nodes(data=True) if "filename" in data}
# Split each src -> dst edge in includes into src -> (src,dst) -> dst, so that
# we can compute how much each include graph edge adds to the size by doing
# dominance analysis on the (src,dst) nodes.
augmented_includes = {}
for src_node_id, src_data in DG.nodes(data=True):
if "filename" not in src_data:
continue
src = src_data["filename"]
if src not in augmented_includes:
augmented_includes[src] = set()
for dst_node_id in DG.successors(src_node_id):
dst = DG.nodes(data=True)[dst_node_id]["filename"]
augmented_includes[src].add((src, dst))
augmented_includes[(src, dst)] = {dst}
return compute_added_sizes((roots, augmented_includes, sizes))
def main():
parser = argparse.ArgumentParser(
description="Find entry points and top edges for high-prevalence headers in the include graph."
)
parser.add_argument(
"include_analysis_output",
type=str,
nargs="?",
help="The include analysis output to use (can be a file path or URL). If not specified, pulls the latest.",
)
parser.add_argument("--skips", action="append", default=[], help="CSV files of edges to skip (remove from graph).")
parser.add_argument("--ignores", action="append", default=[], help="CSV files of edges to ignore.")
parser.add_argument(
"--min-prevalence",
type=float,
required=True,
help="Minimum prevalence percentage for a node to be in the subset.",
)
parser.add_argument(
"--top", type=int, default=10, help="Number of top edges to output by prevalence (default: 10)."
)
parser.add_argument("--verbose", action="store_true", default=False, help="Enable verbose logging.")
args = parser.parse_args()
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.DEBUG if args.verbose else logging.WARNING,
)
try:
include_analysis = load_include_analysis(args.include_analysis_output)
except ParseError as e:
message = str(e)
print("error: Could not parse include analysis output file")
if message:
print(message)
return 2
root_count = len(include_analysis["roots"])
# Load skips and ignores from CSV files
skips: Set[Tuple[str, str]] = set()
ignores: Set[Tuple[str, str]] = set()
for skips_file in args.skips:
with open(skips_file, "r", newline="") as f:
skips.update(
[tuple(row) for row in csv.reader(f) if row and row[0].strip() and not row[0].startswith("#")]
)
for ignores_file in args.ignores:
with open(ignores_file, "r", newline="") as f:
ignores.update([tuple(row) for row in csv.reader(f) if row])
# Build the subset: filter out generated/system/third-party headers and keep those meeting minimum prevalence
EXCLUDED_PREFIXES = ("out/", "buildtools/", "build/", "third_party/", "v8/")
EXCLUDED_EXCEPTIONS = ("third_party/blink/",)
subset: Set[str] = set()
for filename in include_analysis["files"]:
if filename.startswith(EXCLUDED_PREFIXES) and not filename.startswith(EXCLUDED_EXCEPTIONS):
continue
prevalence = (100.0 * include_analysis["prevalence"].get(filename, 0)) / root_count
if prevalence >= args.min_prevalence:
subset.add(filename)
logging.info(f"Subset size: {len(subset)} nodes with >= {args.min_prevalence:.2f}% prevalence")
if not subset:
print(f"No nodes meet the minimum prevalence of {args.min_prevalence:.2f}%", file=sys.stderr)
return 0
DG: nx.DiGraph = create_include_graph(include_analysis, skips)
entry_points = find_entry_points(include_analysis, DG, subset)
dominators = compute_doms(
DG.subgraph([include_analysis["files"].index(node) for node in subset]).copy(), entry_points
)
# Find and output top N edges by prevalence
top_edges = find_top_edges(include_analysis, subset, top_n=args.top, ignores=ignores)
# Find top N edges by dominator count
top_edges_by_doms = find_top_edges_by_dominators(
include_analysis, subset, dominators, top_n=args.top, ignores=ignores
)
print(f"Top {args.top} edges by prevalence:", file=sys.stderr)
try:
csv_writer = csv.writer(sys.stdout)
for includer, included, prevalence in top_edges:
csv_writer.writerow([includer, included, f"{prevalence:.2f}", dominators.get((includer, included), 0)])
print(f"\nTop {args.top} edges by dominator count:", file=sys.stderr)
for includer, included, prevalence, dom_count in top_edges_by_doms:
csv_writer.writerow([includer, included, f"{prevalence:.2f}", dom_count])
sys.stdout.flush()
except BrokenPipeError:
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1)
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
pass # Don't show the user anything