blob: 6b5a5d149e4da6ea21eb4242454dc5704cd63b8c [file] [log] [blame]
Marat Dukhane029fd42017-10-26 00:35:46 +00001#!/usr/bin/env python
2
3import os
4import sys
Marat Dukhane009ce82017-11-26 23:48:13 -08005import string
Marat Dukhane029fd42017-10-26 00:35:46 +00006import argparse
7import subprocess
8import tempfile
9
10
11parser = argparse.ArgumentParser(description='Android system files extractor')
12parser.add_argument("-p", "--prefix", metavar="NAME", required=True,
13 help="Prefix for stored files, e.g. galaxy-s7-us")
14
15
16SYSTEM_FILES = [
17 "/proc/cpuinfo",
18 "/system/build.prop",
19 "/sys/devices/system/cpu/kernel_max",
20 "/sys/devices/system/cpu/possible",
21 "/sys/devices/system/cpu/present",
Marat Dukhane009ce82017-11-26 23:48:13 -080022 "/sys/devices/system/cpu/online",
23 "/sys/devices/system/cpu/offline",
24 "/sys/devices/system/cpu/modalias",
25 "/sys/devices/system/cpu/cpufreq/all_time_in_state",
26 "/sys/devices/system/cpu/cpufreq/current_in_state",
27 "/sys/devices/system/cpu/cpuidle/current_driver",
28 "/sys/devices/system/cpu/cpuidle/current_governor_ro",
29 "/sys/devices/system/cpu/cputopo/cpus_per_cluster",
30 "/sys/devices/system/cpu/cputopo/big_cpumask",
31 "/sys/devices/system/cpu/cputopo/glbinfo",
32 "/sys/devices/system/cpu/cputopo/is_big_little",
33 "/sys/devices/system/cpu/cputopo/is_multi_cluster",
34 "/sys/devices/system/cpu/cputopo/little_cpumask",
35 "/sys/devices/system/cpu/cputopo/nr_clusters",
36 "/sys/devices/system/b.L/big_threads",
37 "/sys/devices/system/b.L/boot_cluster",
38 "/sys/devices/system/b.L/core_status",
39 "/sys/devices/system/b.L/little_threads",
40 "/sys/devices/system/b.L/down_migrations",
41 "/sys/devices/system/b.L/up_migrations",
42 "/sys/devices/system/cpu/clusterhotplug/cur_hstate",
43 "/sys/devices/system/cpu/clusterhotplug/down_freq",
44 "/sys/devices/system/cpu/clusterhotplug/down_tasks",
45 "/sys/devices/system/cpu/clusterhotplug/down_threshold",
46 "/sys/devices/system/cpu/clusterhotplug/sampling_rate",
47 "/sys/devices/system/cpu/clusterhotplug/time_in_state",
48 "/sys/devices/system/cpu/clusterhotplug/up_freq",
49 "/sys/devices/system/cpu/clusterhotplug/up_tasks",
50 "/sys/devices/system/cpu/clusterhotplug/up_threshold",
Marat Dukhane029fd42017-10-26 00:35:46 +000051]
52
53CPU_FILES = [
Marat Dukhane009ce82017-11-26 23:48:13 -080054 "current_driver",
55 "current_governor_ro",
56 "cpuidle/driver/name",
57 "cpufreq/affected_cpus",
Marat Dukhane029fd42017-10-26 00:35:46 +000058 "cpufreq/cpuinfo_max_freq",
59 "cpufreq/cpuinfo_min_freq",
Marat Dukhane009ce82017-11-26 23:48:13 -080060 "cpufreq/cpuinfo_transition_latency",
61 "cpufreq/related_cpus",
62 "cpufreq/scaling_available_frequencies",
63 "cpufreq/scaling_available_governors",
64 "cpufreq/scaling_cur_freq",
65 "cpufreq/scaling_driver",
66 "cpufreq/scaling_governor",
67 "cpufreq/scaling_max_freq",
68 "cpufreq/scaling_min_freq",
69 "cpufreq/sched/down_throttle_nsec",
70 "cpufreq/sched/up_throttle_nsec",
71 "cpufreq/stats/time_in_state",
72 "cpufreq/stats/total_trans",
73 "cpufreq/stats/trans_table",
Marat Dukhane029fd42017-10-26 00:35:46 +000074 "topology/core_id",
Marat Dukhane009ce82017-11-26 23:48:13 -080075 "topology/core_siblings",
76 "topology/core_siblings_list",
77 "topology/cpu_capacity",
78 "topology/max_cpu_capacity",
79 "topology/physical_package_id",
80 "topology/thread_siblings",
Marat Dukhane029fd42017-10-26 00:35:46 +000081 "topology/thread_siblings_list",
82]
83
84CACHE_FILES = [
85 "allocation_policy",
86 "coherency_line_size",
87 "level",
88 "number_of_sets",
89 "shared_cpu_list",
Marat Dukhane009ce82017-11-26 23:48:13 -080090 "shared_cpu_map",
Marat Dukhane029fd42017-10-26 00:35:46 +000091 "size",
92 "type",
93 "ways_of_associativity",
94 "write_policy",
95]
96
97def c_escape(string):
98 c_string = ""
99 for c in string:
100 if c == "\\":
101 c_string += "\\\\"
102 elif c == "\"":
103 c_string += "\\\""
104 elif c == "\t":
105 c_string += "\\t"
106 elif c == "\n":
107 c_string += "\\n"
108 elif c == "\r":
109 c_string += "\\r"
110 elif ord(c) == 0:
111 c_string += "\\0"
112 elif 32 <= ord(c) < 127:
113 c_string += c
114 else:
115 c_string += "x%02X" % ord(c)
116 return c_string
117
118def adb_pull(device_path, local_path):
119 env = os.environ.copy()
120 env["LC_ALL"] = "C"
121
122 adb = subprocess.Popen(["adb", "pull", device_path, local_path], env=env)
123 adb.communicate()
124 return adb.returncode == 0
125
Marat Dukhane009ce82017-11-26 23:48:13 -0800126def adb_shell(commands):
127 env = os.environ.copy()
128 env["LC_ALL"] = "C"
129
130 adb = subprocess.Popen(["adb", "shell"] + commands, env=env, stdout=subprocess.PIPE)
131 stdout, _ = adb.communicate()
132 if adb.returncode == 0:
133 return stdout
134
135def adb_getprop():
136 properties = adb_shell(["getprop"])
137 properties_list = list()
Marat Dukhand5901072017-11-27 17:03:18 -0800138 while properties:
139 assert properties.startswith("[")
140 properties = properties[1:]
141 key, properties = properties.split("]", 1)
142 properties = properties.strip()
143 assert properties.startswith(":")
144 properties = properties[1:].strip()
145 assert properties.startswith("[")
146 properties = properties[1:]
147 value, properties = properties.split("]", 1)
148 properties = properties.strip()
149 properties_list.append((key, value))
Marat Dukhane009ce82017-11-26 23:48:13 -0800150 return properties_list
Marat Dukhane029fd42017-10-26 00:35:46 +0000151
152def dump_device_file(stream, path):
153 temp_fd, temp_path = tempfile.mkstemp()
154 os.close(temp_fd)
155 try:
156 if adb_pull(path, temp_path):
157 with open(temp_path, "rb") as temp_file:
158 content = temp_file.read()
159 stream.write("\t{\n")
160 stream.write("\t\t.path = \"%s\",\n" % path)
161 stream.write("\t\t.size = %d,\n" % len(content))
162 if len(content.splitlines()) > 1:
163 stream.write("\t\t.content =")
164 for line in content.splitlines(True):
165 stream.write("\n\t\t\t\"%s\"" % c_escape(line))
166 stream.write(",\n")
167 else:
168 stream.write("\t\t.content = \"%s\",\n" % c_escape(content))
169 stream.write("\t},\n")
Marat Dukhane009ce82017-11-26 23:48:13 -0800170 return content
Marat Dukhane029fd42017-10-26 00:35:46 +0000171 finally:
Marat Dukhane009ce82017-11-26 23:48:13 -0800172 if os.path.exists(temp_path):
173 os.remove(temp_path)
Marat Dukhane029fd42017-10-26 00:35:46 +0000174
175
176def main(args):
177 options = parser.parse_args(args)
178
Marat Dukhane009ce82017-11-26 23:48:13 -0800179 dmesg_content = adb_shell(["dmesg"])
Marat Dukhan53152762017-11-27 14:34:28 -0800180 if dmesg_content is not None and dmesg_content.strip() == "klogctl: Operation not permitted":
181 dmesg_content = None
Marat Dukhane009ce82017-11-26 23:48:13 -0800182 if dmesg_content is not None:
183 with open(os.path.join("test", "dmesg", options.prefix + ".log"), "w") as dmesg_dump:
184 dmesg_dump.write(dmesg_content)
185
186 build_prop_content = None
187 proc_cpuinfo_content = None
188 kernel_max = 0
189 with open(os.path.join("test", "mock", options.prefix + ".h"), "w") as file_header:
Marat Dukhane029fd42017-10-26 00:35:46 +0000190 file_header.write("struct cpuinfo_mock_file filesystem[] = {\n")
191 for path in SYSTEM_FILES:
Marat Dukhane009ce82017-11-26 23:48:13 -0800192 content = dump_device_file(file_header, path)
193 if content is not None:
194 if path == "/proc/cpuinfo":
195 proc_cpuinfo_content = content
196 elif path == "/system/build.prop":
197 build_prop_content = content
198 elif path == "/sys/devices/system/cpu/kernel_max":
199 kernel_max = int(content.strip())
200 for cpu in range(kernel_max + 1):
Marat Dukhane029fd42017-10-26 00:35:46 +0000201 for filename in CPU_FILES:
202 path = "/sys/devices/system/cpu/cpu%d/%s" % (cpu, filename)
203 dump_device_file(file_header, path)
Marat Dukhane009ce82017-11-26 23:48:13 -0800204 for index in range(5):
Marat Dukhane029fd42017-10-26 00:35:46 +0000205 for filename in CACHE_FILES:
206 path = "/sys/devices/system/cpu/cpu%d/cache/index%d/%s" % (cpu, index, filename)
207 dump_device_file(file_header, path)
208 file_header.write("\t{ NULL },\n")
209 file_header.write("};\n")
Marat Dukhane009ce82017-11-26 23:48:13 -0800210 file_header.write("#ifdef __ANDROID__\n")
211 file_header.write("struct cpuinfo_mock_property properties[] = {\n")
212 for key, value in adb_getprop():
213 file_header.write("\t{\n")
214 file_header.write("\t\t.key = \"%s\",\n" % c_escape(key))
215 file_header.write("\t\t.value = \"%s\",\n" % c_escape(value))
216 file_header.write("\t},\n")
217 file_header.write("\t{ NULL },\n")
218 file_header.write("};\n")
219 file_header.write("#endif /* __ANDROID__ */\n")
220
221 if proc_cpuinfo_content is not None:
222 with open(os.path.join("test", "cpuinfo", options.prefix + ".log"), "w") as proc_cpuinfo_dump:
223 proc_cpuinfo_dump.write(proc_cpuinfo_content)
224 if build_prop_content is not None:
225 with open(os.path.join("test", "build.prop", options.prefix + ".log"), "w") as build_prop_dump:
226 build_prop_dump.write(build_prop_content)
Marat Dukhane029fd42017-10-26 00:35:46 +0000227
228if __name__ == "__main__":
229 main(sys.argv[1:])