annotate lldb/scripts/analyze-project-deps.py @ 220:42394fc6a535

Added tag llvm12 for changeset 0572611fdcc8
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:13:43 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 #! /usr/bin/env python
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 import argparse
anatofuz
parents:
diff changeset
4 import itertools
anatofuz
parents:
diff changeset
5 import os
anatofuz
parents:
diff changeset
6 import re
anatofuz
parents:
diff changeset
7 import sys
anatofuz
parents:
diff changeset
8 from collections import defaultdict
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 from use_lldb_suite import lldb_root
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 parser = argparse.ArgumentParser(
anatofuz
parents:
diff changeset
13 description='Analyze LLDB project #include dependencies.')
anatofuz
parents:
diff changeset
14 parser.add_argument('--show-counts', default=False, action='store_true',
anatofuz
parents:
diff changeset
15 help='When true, show the number of dependencies from each subproject')
anatofuz
parents:
diff changeset
16 parser.add_argument('--discover-cycles', default=False, action='store_true',
anatofuz
parents:
diff changeset
17 help='When true, find and display all project dependency cycles. Note,'
anatofuz
parents:
diff changeset
18 'this option is very slow')
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 args = parser.parse_args()
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 src_dir = os.path.join(lldb_root, "source")
anatofuz
parents:
diff changeset
23 inc_dir = os.path.join(lldb_root, "include")
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 src_map = {}
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 include_regex = re.compile('#include \"((lldb|Plugins|clang)(.*/)+).*\"')
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 def is_sublist(small, big):
anatofuz
parents:
diff changeset
30 it = iter(big)
anatofuz
parents:
diff changeset
31 return all(c in it for c in small)
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 def normalize_host(str):
anatofuz
parents:
diff changeset
34 if str.startswith("lldb/Host"):
anatofuz
parents:
diff changeset
35 return "lldb/Host"
anatofuz
parents:
diff changeset
36 if str.startswith("Plugins"):
anatofuz
parents:
diff changeset
37 return "lldb/" + str
anatofuz
parents:
diff changeset
38 if str.startswith("lldb/../../source"):
anatofuz
parents:
diff changeset
39 return str.replace("lldb/../../source", "lldb")
anatofuz
parents:
diff changeset
40 return str
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 def scan_deps(this_dir, file):
anatofuz
parents:
diff changeset
43 global src_map
anatofuz
parents:
diff changeset
44 deps = {}
anatofuz
parents:
diff changeset
45 this_dir = normalize_host(this_dir)
anatofuz
parents:
diff changeset
46 if this_dir in src_map:
anatofuz
parents:
diff changeset
47 deps = src_map[this_dir]
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 with open(file) as f:
anatofuz
parents:
diff changeset
50 for line in list(f):
anatofuz
parents:
diff changeset
51 m = include_regex.match(line)
anatofuz
parents:
diff changeset
52 if m is None:
anatofuz
parents:
diff changeset
53 continue
anatofuz
parents:
diff changeset
54 relative = m.groups()[0].rstrip("/")
anatofuz
parents:
diff changeset
55 if relative == this_dir:
anatofuz
parents:
diff changeset
56 continue
anatofuz
parents:
diff changeset
57 relative = normalize_host(relative)
anatofuz
parents:
diff changeset
58 if relative in deps:
anatofuz
parents:
diff changeset
59 deps[relative] += 1
anatofuz
parents:
diff changeset
60 elif relative != this_dir:
anatofuz
parents:
diff changeset
61 deps[relative] = 1
anatofuz
parents:
diff changeset
62 if this_dir not in src_map and len(deps) > 0:
anatofuz
parents:
diff changeset
63 src_map[this_dir] = deps
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 for (base, dirs, files) in os.walk(inc_dir):
anatofuz
parents:
diff changeset
66 dir = os.path.basename(base)
anatofuz
parents:
diff changeset
67 relative = os.path.relpath(base, inc_dir)
anatofuz
parents:
diff changeset
68 inc_files = [x for x in files if os.path.splitext(x)[1] in [".h"]]
anatofuz
parents:
diff changeset
69 relative = relative.replace("\\", "/")
anatofuz
parents:
diff changeset
70 for inc in inc_files:
anatofuz
parents:
diff changeset
71 inc_path = os.path.join(base, inc)
anatofuz
parents:
diff changeset
72 scan_deps(relative, inc_path)
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 for (base, dirs, files) in os.walk(src_dir):
anatofuz
parents:
diff changeset
75 dir = os.path.basename(base)
anatofuz
parents:
diff changeset
76 relative = os.path.relpath(base, src_dir)
anatofuz
parents:
diff changeset
77 src_files = [x for x in files if os.path.splitext(x)[1] in [".cpp", ".h", ".mm"]]
anatofuz
parents:
diff changeset
78 norm_base_path = os.path.normpath(os.path.join("lldb", relative))
anatofuz
parents:
diff changeset
79 norm_base_path = norm_base_path.replace("\\", "/")
anatofuz
parents:
diff changeset
80 for src in src_files:
anatofuz
parents:
diff changeset
81 src_path = os.path.join(base, src)
anatofuz
parents:
diff changeset
82 scan_deps(norm_base_path, src_path)
anatofuz
parents:
diff changeset
83 pass
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 def is_existing_cycle(path, cycles):
anatofuz
parents:
diff changeset
86 # If we have a cycle like # A -> B -> C (with an implicit -> A at the end)
anatofuz
parents:
diff changeset
87 # then we don't just want to check for an occurrence of A -> B -> C in the
anatofuz
parents:
diff changeset
88 # list of known cycles, but every possible rotation of A -> B -> C. For
anatofuz
parents:
diff changeset
89 # example, if we previously encountered B -> C -> A (with an implicit -> B
anatofuz
parents:
diff changeset
90 # at the end), then A -> B -> C is also a cycle. This is an important
anatofuz
parents:
diff changeset
91 # optimization which reduces the search space by multiple orders of
anatofuz
parents:
diff changeset
92 # magnitude.
anatofuz
parents:
diff changeset
93 for i in range(0,len(path)):
anatofuz
parents:
diff changeset
94 if any(is_sublist(x, path) for x in cycles):
anatofuz
parents:
diff changeset
95 return True
anatofuz
parents:
diff changeset
96 path = [path[-1]] + path[0:-1]
anatofuz
parents:
diff changeset
97 return False
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 def expand(path_queue, path_lengths, cycles, src_map):
anatofuz
parents:
diff changeset
100 # We do a breadth first search, to make sure we visit all paths in order
anatofuz
parents:
diff changeset
101 # of ascending length. This is an important optimization to make sure that
anatofuz
parents:
diff changeset
102 # short cycles are discovered first, which will allow us to discard longer
anatofuz
parents:
diff changeset
103 # cycles which grow the search space exponentially the longer they get.
anatofuz
parents:
diff changeset
104 while len(path_queue) > 0:
anatofuz
parents:
diff changeset
105 cur_path = path_queue.pop(0)
anatofuz
parents:
diff changeset
106 if is_existing_cycle(cur_path, cycles):
anatofuz
parents:
diff changeset
107 continue
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 next_len = path_lengths.pop(0) + 1
anatofuz
parents:
diff changeset
110 last_component = cur_path[-1]
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 for item in src_map[last_component]:
anatofuz
parents:
diff changeset
113 if item.startswith("clang"):
anatofuz
parents:
diff changeset
114 continue
anatofuz
parents:
diff changeset
115
anatofuz
parents:
diff changeset
116 if item in cur_path:
anatofuz
parents:
diff changeset
117 # This is a cycle. Minimize it and then check if the result is
anatofuz
parents:
diff changeset
118 # already in the list of cycles. Insert it (or not) and then
anatofuz
parents:
diff changeset
119 # exit.
anatofuz
parents:
diff changeset
120 new_index = cur_path.index(item)
anatofuz
parents:
diff changeset
121 cycle = cur_path[new_index:]
anatofuz
parents:
diff changeset
122 if not is_existing_cycle(cycle, cycles):
anatofuz
parents:
diff changeset
123 cycles.append(cycle)
anatofuz
parents:
diff changeset
124 continue
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 path_lengths.append(next_len)
anatofuz
parents:
diff changeset
127 path_queue.append(cur_path + [item])
anatofuz
parents:
diff changeset
128 pass
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 cycles = []
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 path_queue = [[x] for x in iter(src_map)]
anatofuz
parents:
diff changeset
133 path_lens = [1] * len(path_queue)
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 items = list(src_map.items())
anatofuz
parents:
diff changeset
136 items.sort(key = lambda A : A[0])
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 for (path, deps) in items:
anatofuz
parents:
diff changeset
139 print(path + ":")
anatofuz
parents:
diff changeset
140 sorted_deps = list(deps.items())
anatofuz
parents:
diff changeset
141 if args.show_counts:
anatofuz
parents:
diff changeset
142 sorted_deps.sort(key = lambda A: (A[1], A[0]))
anatofuz
parents:
diff changeset
143 for dep in sorted_deps:
anatofuz
parents:
diff changeset
144 print("\t{} [{}]".format(dep[0], dep[1]))
anatofuz
parents:
diff changeset
145 else:
anatofuz
parents:
diff changeset
146 sorted_deps.sort(key = lambda A: A[0])
anatofuz
parents:
diff changeset
147 for dep in sorted_deps:
anatofuz
parents:
diff changeset
148 print("\t{}".format(dep[0]))
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 def iter_cycles(cycles):
anatofuz
parents:
diff changeset
151 global src_map
anatofuz
parents:
diff changeset
152 for cycle in cycles:
anatofuz
parents:
diff changeset
153 cycle.append(cycle[0])
anatofuz
parents:
diff changeset
154 zipper = list(zip(cycle[0:-1], cycle[1:]))
anatofuz
parents:
diff changeset
155 result = [(x, src_map[x][y], y) for (x,y) in zipper]
anatofuz
parents:
diff changeset
156 total = 0
anatofuz
parents:
diff changeset
157 smallest = result[0][1]
anatofuz
parents:
diff changeset
158 for (first, value, last) in result:
anatofuz
parents:
diff changeset
159 total += value
anatofuz
parents:
diff changeset
160 smallest = min(smallest, value)
anatofuz
parents:
diff changeset
161 yield (total, smallest, result)
anatofuz
parents:
diff changeset
162
anatofuz
parents:
diff changeset
163 if args.discover_cycles:
anatofuz
parents:
diff changeset
164 print("Analyzing cycles...")
anatofuz
parents:
diff changeset
165
anatofuz
parents:
diff changeset
166 expand(path_queue, path_lens, cycles, src_map)
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 average = sum([len(x)+1 for x in cycles]) / len(cycles)
anatofuz
parents:
diff changeset
169
anatofuz
parents:
diff changeset
170 print("Found {} cycles. Average cycle length = {}.".format(len(cycles), average))
anatofuz
parents:
diff changeset
171 counted = list(iter_cycles(cycles))
anatofuz
parents:
diff changeset
172 if args.show_counts:
anatofuz
parents:
diff changeset
173 counted.sort(key = lambda A: A[0])
anatofuz
parents:
diff changeset
174 for (total, smallest, cycle) in counted:
anatofuz
parents:
diff changeset
175 sys.stdout.write("{} deps to break: ".format(total))
anatofuz
parents:
diff changeset
176 sys.stdout.write(cycle[0][0])
anatofuz
parents:
diff changeset
177 for (first, count, last) in cycle:
anatofuz
parents:
diff changeset
178 sys.stdout.write(" [{}->] {}".format(count, last))
anatofuz
parents:
diff changeset
179 sys.stdout.write("\n")
anatofuz
parents:
diff changeset
180 else:
anatofuz
parents:
diff changeset
181 for cycle in cycles:
anatofuz
parents:
diff changeset
182 cycle.append(cycle[0])
anatofuz
parents:
diff changeset
183 print(" -> ".join(cycle))
anatofuz
parents:
diff changeset
184
anatofuz
parents:
diff changeset
185 print("Analyzing islands...")
anatofuz
parents:
diff changeset
186 islands = []
anatofuz
parents:
diff changeset
187 outgoing_counts = defaultdict(int)
anatofuz
parents:
diff changeset
188 incoming_counts = defaultdict(int)
anatofuz
parents:
diff changeset
189 for (total, smallest, cycle) in counted:
anatofuz
parents:
diff changeset
190 for (first, count, last) in cycle:
anatofuz
parents:
diff changeset
191 outgoing_counts[first] += count
anatofuz
parents:
diff changeset
192 incoming_counts[last] += count
anatofuz
parents:
diff changeset
193 for cycle in cycles:
anatofuz
parents:
diff changeset
194 this_cycle = set(cycle)
anatofuz
parents:
diff changeset
195 disjoints = [x for x in islands if this_cycle.isdisjoint(x)]
anatofuz
parents:
diff changeset
196 overlaps = [x for x in islands if not this_cycle.isdisjoint(x)]
anatofuz
parents:
diff changeset
197 islands = disjoints + [set.union(this_cycle, *overlaps)]
anatofuz
parents:
diff changeset
198 print("Found {} disjoint cycle islands...".format(len(islands)))
anatofuz
parents:
diff changeset
199 for island in islands:
anatofuz
parents:
diff changeset
200 print("Island ({} elements)".format(len(island)))
anatofuz
parents:
diff changeset
201 sorted = []
anatofuz
parents:
diff changeset
202 for node in island:
anatofuz
parents:
diff changeset
203 sorted.append((node, incoming_counts[node], outgoing_counts[node]))
anatofuz
parents:
diff changeset
204 sorted.sort(key = lambda x: x[1]+x[2])
anatofuz
parents:
diff changeset
205 for (node, inc, outg) in sorted:
anatofuz
parents:
diff changeset
206 print(" {} [{} in, {} out]".format(node, inc, outg))
anatofuz
parents:
diff changeset
207 sys.stdout.flush()
anatofuz
parents:
diff changeset
208 pass