Lines Matching refs:self
44 def __init__(self, path, name, score): argument
45 self.path = path
46 self.name = name
47 self.score = score
49 def __repr__(self): argument
50 return self.__str__()
52 def __str__(self): argument
53 return '[path: %s, name %s, score %s]' % (self.path, self.name, self.score)
64 def __init__(self, ndk_path, device_arch, adb): argument
65 self.adb = adb
66 self.readelf = ReadElf(ndk_path)
67 self.device_arch = device_arch
68 self.need_archs = self._get_need_archs()
69 self.host_build_id_map = {} # Map from build_id to HostElfEntry.
70 self.device_build_id_map = {} # Map from build_id to relative_path on device.
72 self.no_build_id_file_map = {}
73 self.name_count_map = {} # Used to give a unique name for each library.
74 self.dir_on_device = NATIVE_LIBS_DIR_ON_DEVICE
75 self.build_id_list_file = 'build_id_list'
77 def _get_need_archs(self): argument
79 if self.device_arch == 'arm64':
81 if self.device_arch == 'arm':
83 if self.device_arch == 'x86_64':
85 if self.device_arch == 'x86':
89 def collect_native_libs_on_host(self, native_lib_dir): argument
90 self.host_build_id_map.clear()
95 self.add_native_lib_on_host(os.path.join(root, name), name)
97 def add_native_lib_on_host(self, path, name): argument
98 arch = self.readelf.get_arch(path)
99 if arch not in self.need_archs:
101 sections = self.readelf.get_sections(path)
109 build_id = self.readelf.get_build_id(path)
111 entry = self.host_build_id_map.get(build_id)
117 repeat_count = self.name_count_map.get(name, 0)
118 self.name_count_map[name] = repeat_count + 1
120 self.host_build_id_map[build_id] = HostElfEntry(path, unique_name, score)
122 entry = self.no_build_id_file_map.get(name)
128 self.no_build_id_file_map[name] = HostElfEntry(path, name, score)
130 def collect_native_libs_on_device(self): argument
131 self.device_build_id_map.clear()
132 self.adb.check_run(['shell', 'mkdir', '-p', self.dir_on_device])
133 if os.path.exists(self.build_id_list_file):
134 os.remove(self.build_id_list_file)
135 result, output = self.adb.run_and_return_output(['shell', 'ls', self.dir_on_device])
139 if self.build_id_list_file not in file_set:
141 self.adb.run(['pull', self.dir_on_device + self.build_id_list_file])
142 if os.path.exists(self.build_id_list_file):
143 with open(self.build_id_list_file, 'rb') as fh:
150 self.device_build_id_map[build_id] = filename
151 remove(self.build_id_list_file)
153 def sync_native_libs_on_device(self): argument
155 for build_id in self.host_build_id_map:
156 if build_id not in self.device_build_id_map:
157 entry = self.host_build_id_map[build_id]
158 self.adb.check_run(['push', entry.path, self.dir_on_device + entry.name])
160 for build_id in self.device_build_id_map:
161 if build_id not in self.host_build_id_map:
162 name = self.device_build_id_map[build_id]
163 self.adb.run(['shell', 'rm', self.dir_on_device + name])
165 with open(self.build_id_list_file, 'wb') as fh:
166 for build_id in self.host_build_id_map:
167 s = str_to_bytes('%s=%s\n' % (build_id, self.host_build_id_map[build_id].name))
169 self.adb.check_run(['push', self.build_id_list_file,
170 self.dir_on_device + self.build_id_list_file])
171 os.remove(self.build_id_list_file)
174 for entry in self.no_build_id_file_map.values():
175 target = self.dir_on_device + entry.name
178 result, output = self.adb.run_and_return_output(['shell', 'ls', '-l', target])
188 self.adb.check_run(['push', entry.path, target])
194 def __init__(self, args): argument
195 self.args = args
196 self.adb = AdbHelper(enable_switch_to_root=not args.disable_adb_root)
197 self.is_root_device = self.adb.switch_to_root()
198 self.android_version = self.adb.get_android_version()
199 if self.android_version < 7:
202 self.device_arch = self.adb.get_device_arch()
203 self.record_subproc = None
205 def profile(self): argument
207 self.prepare()
209 self.start()
210 self.wait_profiling()
212 self.collect_profiling_data()
215 def prepare(self): argument
217 self.download_simpleperf()
218 if self.args.native_lib_dir:
219 self.download_libs()
221 def download_simpleperf(self): argument
222 simpleperf_binary = get_target_binary_path(self.device_arch, 'simpleperf')
223 self.adb.check_run(['push', simpleperf_binary, '/data/local/tmp'])
224 self.adb.check_run(['shell', 'chmod', 'a+x', '/data/local/tmp/simpleperf'])
226 def download_libs(self): argument
227 downloader = NativeLibDownloader(self.args.ndk_path, self.device_arch, self.adb)
228 downloader.collect_native_libs_on_host(self.args.native_lib_dir)
232 def start(self): argument
235 def start_profiling(self, target_args): argument
238 self.args.record_options]
239 if self.adb.run(['shell', 'ls', NATIVE_LIBS_DIR_ON_DEVICE]):
241 args += ['--log', self.args.log]
243 adb_args = [self.adb.adb_path, 'shell'] + args
245 self.record_subproc = subprocess.Popen(adb_args)
247 def wait_profiling(self): argument
251 returncode = self.record_subproc.wait()
253 self.stop_profiling()
254 self.record_subproc = None
262 def stop_profiling(self): argument
267 (result, _) = self.adb.run_and_return_output(['shell', 'pidof', 'simpleperf'])
272 self.adb.run_and_return_output(['shell', 'pkill', '-l', '2', 'simpleperf'])
275 def collect_profiling_data(self): argument
276 self.adb.check_run_and_return_output(['pull', '/data/local/tmp/perf.data',
277 self.args.perf_data_path])
278 if not self.args.skip_collect_binaries:
281 binary_cache_args += ['-i', self.args.perf_data_path, '--log', self.args.log]
282 if self.args.native_lib_dir:
283 binary_cache_args += ['-lib', self.args.native_lib_dir]
284 if self.args.disable_adb_root:
286 if self.args.ndk_path:
287 binary_cache_args += ['--ndk_path', self.args.ndk_path]
294 def prepare(self): argument
295 super(AppProfiler, self).prepare()
296 if self.args.compile_java_code:
297 self.compile_java_code()
299 def compile_java_code(self): argument
300 self.kill_app_process()
302 self.adb.set_property('debug.generate-debug-info', 'true')
303 self.adb.check_run(['shell', 'cmd', 'package', 'compile', '-f', '-m', 'speed',
304 self.args.app])
306 def kill_app_process(self): argument
307 if self.find_app_process():
308 self.adb.check_run(['shell', 'am', 'force-stop', self.args.app])
312 pid = self.find_app_process()
317 logging.info('unable to kill %s, skipping...' % self.args.app)
322 self.run_in_app_dir(['kill', '-9', str(pid)])
324 def find_app_process(self): argument
325 result, pidof_output = self.adb.run_and_return_output(
326 ['shell', 'pidof', self.args.app])
329 result, current_user = self.adb.run_and_return_output(
335 result, ps_output = self.adb.run_and_return_output(
344 def run_in_app_dir(self, args): argument
345 if self.is_root_device:
346 adb_args = ['shell', 'cd /data/data/' + self.args.app + ' && ' + (' '.join(args))]
348 adb_args = ['shell', 'run-as', self.args.app] + args
349 return self.adb.run_and_return_output(adb_args)
351 def start(self): argument
352 if self.args.activity or self.args.test:
353 self.kill_app_process()
354 self.start_profiling(['--app', self.args.app])
355 if self.args.activity:
356 self.start_activity()
357 elif self.args.test:
358 self.start_test()
361 def start_activity(self): argument
362 activity = self.args.app + '/' + self.args.activity
363 result = self.adb.run(['shell', 'am', 'start', '-n', activity])
365 self.record_subproc.terminate()
368 def start_test(self): argument
369 runner = self.args.app + '/androidx.test.runner.AndroidJUnitRunner'
370 result = self.adb.run(['shell', 'am', 'instrument', '-e', 'class',
371 self.args.test, runner])
373 self.record_subproc.terminate()
374 log_exit("Can't start instrumentation test %s" % self.args.test)
380 def start(self): argument
381 logging.info('Waiting for native process %s' % self.args.native_program)
383 (result, pid) = self.adb.run_and_return_output(['shell', 'pidof',
384 self.args.native_program])
389 self.start_profiling(['-p', str(int(pid))])
396 def start(self): argument
397 self.start_profiling([self.args.cmd])
403 def start(self): argument
404 self.start_profiling(['-p', ','.join(self.args.pid)])
410 def start(self): argument
411 self.start_profiling(['-t', ','.join(self.args.tid)])
417 def start(self): argument
418 self.start_profiling(['-a'])