Lines Matching refs:self

39     def __init__(self, path, name, score):  argument
40 self.path = path
41 self.name = name
42 self.score = score
44 def __repr__(self): argument
45 return self.__str__()
47 def __str__(self): argument
48 return '[path: %s, name %s, score %s]' % (self.path, self.name, self.score)
58 def __init__(self, ndk_path, device_arch, adb): argument
59 self.adb = adb
60 self.readelf = ReadElf(ndk_path)
61 self.device_arch = device_arch
62 self.need_archs = self._get_need_archs()
63 self.host_build_id_map = {} # Map from build_id to HostElfEntry.
64 self.device_build_id_map = {} # Map from build_id to relative_path on device.
66 self.no_build_id_file_map = {}
67 self.name_count_map = {} # Used to give a unique name for each library.
68 self.dir_on_device = NATIVE_LIBS_DIR_ON_DEVICE
69 self.build_id_list_file = 'build_id_list'
71 def _get_need_archs(self): argument
73 if self.device_arch == 'arm64':
75 if self.device_arch == 'arm':
77 if self.device_arch == 'x86_64':
79 if self.device_arch == 'x86':
83 def collect_native_libs_on_host(self, native_lib_dir): argument
84 self.host_build_id_map.clear()
89 self.add_native_lib_on_host(os.path.join(root, name), name)
91 def add_native_lib_on_host(self, path, name): argument
92 arch = self.readelf.get_arch(path)
93 if arch not in self.need_archs:
95 sections = self.readelf.get_sections(path)
103 build_id = self.readelf.get_build_id(path)
105 entry = self.host_build_id_map.get(build_id)
111 repeat_count = self.name_count_map.get(name, 0)
112 self.name_count_map[name] = repeat_count + 1
114 self.host_build_id_map[build_id] = HostElfEntry(path, unique_name, score)
116 entry = self.no_build_id_file_map.get(name)
122 self.no_build_id_file_map[name] = HostElfEntry(path, name, score)
124 def collect_native_libs_on_device(self): argument
125 self.device_build_id_map.clear()
126 self.adb.check_run(['shell', 'mkdir', '-p', self.dir_on_device])
127 if os.path.exists(self.build_id_list_file):
128 os.remove(self.build_id_list_file)
129 result, output = self.adb.run_and_return_output(['shell', 'ls', self.dir_on_device])
133 if self.build_id_list_file not in file_set:
135 self.adb.run(['pull', self.dir_on_device + self.build_id_list_file])
136 if os.path.exists(self.build_id_list_file):
137 with open(self.build_id_list_file, 'rb') as fh:
144 self.device_build_id_map[build_id] = filename
145 remove(self.build_id_list_file)
147 def sync_native_libs_on_device(self): argument
149 for build_id in self.host_build_id_map:
150 if build_id not in self.device_build_id_map:
151 entry = self.host_build_id_map[build_id]
152 self.adb.check_run(['push', entry.path, self.dir_on_device + entry.name])
154 for build_id in self.device_build_id_map:
155 if build_id not in self.host_build_id_map:
156 name = self.device_build_id_map[build_id]
157 self.adb.run(['shell', 'rm', self.dir_on_device + name])
159 with open(self.build_id_list_file, 'wb') as fh:
160 for build_id in self.host_build_id_map:
161 s = str_to_bytes('%s=%s\n' % (build_id, self.host_build_id_map[build_id].name))
163 self.adb.check_run(['push', self.build_id_list_file,
164 self.dir_on_device + self.build_id_list_file])
165 os.remove(self.build_id_list_file)
168 for entry in self.no_build_id_file_map.values():
169 target = self.dir_on_device + entry.name
172 result, output = self.adb.run_and_return_output(
183 self.adb.check_run(['push', entry.path, target])
188 def __init__(self, args): argument
189 self.args = args
190 self.adb = AdbHelper(enable_switch_to_root=not args.disable_adb_root)
191 self.is_root_device = self.adb.switch_to_root()
192 self.android_version = self.adb.get_android_version()
193 if self.android_version < 7:
196 self.device_arch = self.adb.get_device_arch()
197 self.record_subproc = None
199 def profile(self): argument
201 self.prepare()
203 self.start()
204 self.wait_profiling()
206 self.collect_profiling_data()
209 def prepare(self): argument
211 self.download_simpleperf()
212 if self.args.native_lib_dir:
213 self.download_libs()
215 def download_simpleperf(self): argument
216 simpleperf_binary = get_target_binary_path(self.device_arch, 'simpleperf')
217 self.adb.check_run(['push', simpleperf_binary, '/data/local/tmp'])
218 self.adb.check_run(['shell', 'chmod', 'a+x', '/data/local/tmp/simpleperf'])
220 def download_libs(self): argument
221 downloader = NativeLibDownloader(self.args.ndk_path, self.device_arch, self.adb)
222 downloader.collect_native_libs_on_host(self.args.native_lib_dir)
226 def start(self): argument
229 def start_profiling(self, target_args): argument
232 self.args.record_options]
233 if self.adb.run(['shell', 'ls', NATIVE_LIBS_DIR_ON_DEVICE, '>/dev/null', '2>&1']):
235 args += ['--log', self.args.log]
237 adb_args = [self.adb.adb_path, 'shell'] + args
239 self.record_subproc = subprocess.Popen(adb_args)
241 def wait_profiling(self): argument
245 returncode = self.record_subproc.wait()
247 self.stop_profiling()
248 self.record_subproc = None
256 def stop_profiling(self): argument
261 (result, _) = self.adb.run_and_return_output(['shell', 'pidof', 'simpleperf'])
266 self.adb.run_and_return_output(['shell', 'pkill', '-l', '2', 'simpleperf'])
269 def collect_profiling_data(self): argument
270 self.adb.check_run_and_return_output(['pull', '/data/local/tmp/perf.data',
271 self.args.perf_data_path])
272 if not self.args.skip_collect_binaries:
275 binary_cache_args += ['-i', self.args.perf_data_path, '--log', self.args.log]
276 if self.args.native_lib_dir:
277 binary_cache_args += ['-lib', self.args.native_lib_dir]
278 if self.args.disable_adb_root:
280 if self.args.ndk_path:
281 binary_cache_args += ['--ndk_path', self.args.ndk_path]
287 def prepare(self): argument
288 super(AppProfiler, self).prepare()
289 if self.args.compile_java_code:
290 self.compile_java_code()
292 def compile_java_code(self): argument
293 self.kill_app_process()
295 self.adb.set_property('debug.generate-debug-info', 'true')
296 self.adb.check_run(['shell', 'cmd', 'package', 'compile', '-f', '-m', 'speed',
297 self.args.app])
299 def kill_app_process(self): argument
300 if self.find_app_process():
301 self.adb.check_run(['shell', 'am', 'force-stop', self.args.app])
305 pid = self.find_app_process()
312 self.run_in_app_dir(['kill', '-9', str(pid)])
314 def find_app_process(self): argument
315 result, output = self.adb.run_and_return_output(['shell', 'pidof', self.args.app])
318 def run_in_app_dir(self, args): argument
319 if self.is_root_device:
320 adb_args = ['shell', 'cd /data/data/' + self.args.app + ' && ' + (' '.join(args))]
322 adb_args = ['shell', 'run-as', self.args.app] + args
323 return self.adb.run_and_return_output(adb_args, log_output=False)
325 def start(self): argument
326 if self.args.activity or self.args.test:
327 self.kill_app_process()
328 self.start_profiling(['--app', self.args.app])
329 if self.args.activity:
330 self.start_activity()
331 elif self.args.test:
332 self.start_test()
335 def start_activity(self): argument
336 activity = self.args.app + '/' + self.args.activity
337 result = self.adb.run(['shell', 'am', 'start', '-n', activity])
339 self.record_subproc.terminate()
342 def start_test(self): argument
343 runner = self.args.app + '/androidx.test.runner.AndroidJUnitRunner'
344 result = self.adb.run(['shell', 'am', 'instrument', '-e', 'class',
345 self.args.test, runner])
347 self.record_subproc.terminate()
348 log_exit("Can't start instrumentation test %s" % self.args.test)
353 def start(self): argument
354 pid = int(self.adb.check_run_and_return_output(['shell', 'pidof',
355 self.args.native_program]))
356 self.start_profiling(['-p', str(pid)])
361 def start(self): argument
362 self.start_profiling([self.args.cmd])
367 def start(self): argument
368 self.start_profiling(['-p', ','.join(self.args.pid)])
373 def start(self): argument
374 self.start_profiling(['-t', ','.join(self.args.tid)])
379 def start(self): argument
380 self.start_profiling(['-a'])