Add yapf python formatter and format all files
yapf seems widely used in chromium. Adding here and
formatting all existing sources.
Change-Id: I5fd223a46cf3a00e6b7e6266850825b484af1558
diff --git a/tools/heap_profile b/tools/heap_profile
index 5680a68..d705914 100755
--- a/tools/heap_profile
+++ b/tools/heap_profile
@@ -31,17 +31,16 @@
import urllib
TRACE_TO_TEXT_SHAS = {
- 'linux': '74cb40d9413d7ad756e327e09e46a8adde90db92',
- 'mac': '47045bc6abd4f9113969211f37fdd2259b0c1cc3',
+ 'linux': '74cb40d9413d7ad756e327e09e46a8adde90db92',
+ 'mac': '47045bc6abd4f9113969211f37fdd2259b0c1cc3',
}
TRACE_TO_TEXT_PATH = tempfile.gettempdir()
-TRACE_TO_TEXT_BASE_URL = (
- 'https://storage.googleapis.com/perfetto/')
+TRACE_TO_TEXT_BASE_URL = ('https://storage.googleapis.com/perfetto/')
NULL = open(os.devnull)
NOOUT = {
- 'stdout': NULL,
- 'stderr': NULL,
+ 'stdout': NULL,
+ 'stderr': NULL,
}
@@ -71,16 +70,16 @@
os.chmod(local_file, 0o755)
return local_file
-PACKAGES_LIST_CFG='''data_sources {
+
+PACKAGES_LIST_CFG = '''data_sources {
config {
name: "android.packages_list"
}
}
'''
-
CFG_IDENT = ' '
-CFG='''buffers {{
+CFG = '''buffers {{
size_kb: 32768
}}
@@ -109,10 +108,12 @@
}}
"""
-PERFETTO_CMD=('CFG=\'{cfg}\'; echo ${{CFG}} | '
- 'perfetto --txt -c - -o '
- '/data/misc/perfetto-traces/profile-{user} -d')
+PERFETTO_CMD = ('CFG=\'{cfg}\'; echo ${{CFG}} | '
+ 'perfetto --txt -c - -o '
+ '/data/misc/perfetto-traces/profile-{user} -d')
IS_INTERRUPTED = False
+
+
def sigint_handler(sig, frame):
global IS_INTERRUPTED
IS_INTERRUPTED = True
@@ -120,59 +121,107 @@
def main(argv):
parser = argparse.ArgumentParser()
- parser.add_argument("-i", "--interval", help="Sampling interval. "
- "Default 4096 (4KiB)", type=int, default=4096)
- parser.add_argument("-d", "--duration", help="Duration of profile (ms). "
- "Default 7 days.", type=int, default=604800000)
- parser.add_argument("--no-start", help="Do not start heapprofd.",
- action='store_true')
- parser.add_argument("-p", "--pid", help="Comma-separated list of PIDs to "
- "profile.", metavar="PIDS")
- parser.add_argument("-n", "--name", help="Comma-separated list of process "
- "names to profile.", metavar="NAMES")
- parser.add_argument("-c", "--continuous-dump",
- help="Dump interval in ms. 0 to disable continuous dump.",
- type=int, default=0)
- parser.add_argument("--disable-selinux", action="store_true",
- help="Disable SELinux enforcement for duration of "
- "profile.")
- parser.add_argument("--no-versions", action="store_true",
- help="Do not get version information about APKs.")
- parser.add_argument("--no-running", action="store_true",
- help="Do not target already running processes.")
- parser.add_argument("--no-startup", action="store_true",
- help="Do not target processes that start during "
- "the profile.")
- parser.add_argument("--shmem-size", help="Size of buffer between client and "
- "heapprofd. Default 8MiB. Needs to be a power of two "
- "multiple of 4096, at least 8192.", type=int,
- default=8 * 1048576)
- parser.add_argument("--block-client", help="When buffer is full, block the "
- "client to wait for buffer space. Use with caution as "
- "this can significantly slow down the client. "
- "This is the default", action="store_true")
- parser.add_argument("--no-block-client", help="When buffer is full, stop the "
- "profile early.", action="store_true")
- parser.add_argument("--idle-allocations", help="Keep track of how many "
- "bytes were unused since the last dump, per "
- "callstack", action="store_true")
- parser.add_argument("--dump-at-max", help="Dump the maximum memory usage"
- "rather than at the time of the dump.",
- action="store_true")
- parser.add_argument("--simpleperf", action="store_true",
- help="Get simpleperf profile of heapprofd. This is "
- "only for heapprofd development.")
- parser.add_argument("--trace-to-text-binary",
- help="Path to local trace to text. For debugging.")
- parser.add_argument("--print-config", action="store_true",
- help="Print config instead of running. For debugging.")
+ parser.add_argument(
+ "-i",
+ "--interval",
+ help="Sampling interval. "
+ "Default 4096 (4KiB)",
+ type=int,
+ default=4096)
+ parser.add_argument(
+ "-d",
+ "--duration",
+ help="Duration of profile (ms). "
+ "Default 7 days.",
+ type=int,
+ default=604800000)
+ parser.add_argument(
+ "--no-start", help="Do not start heapprofd.", action='store_true')
+ parser.add_argument(
+ "-p",
+ "--pid",
+ help="Comma-separated list of PIDs to "
+ "profile.",
+ metavar="PIDS")
+ parser.add_argument(
+ "-n",
+ "--name",
+ help="Comma-separated list of process "
+ "names to profile.",
+ metavar="NAMES")
+ parser.add_argument(
+ "-c",
+ "--continuous-dump",
+ help="Dump interval in ms. 0 to disable continuous dump.",
+ type=int,
+ default=0)
+ parser.add_argument(
+ "--disable-selinux",
+ action="store_true",
+ help="Disable SELinux enforcement for duration of "
+ "profile.")
+ parser.add_argument(
+ "--no-versions",
+ action="store_true",
+ help="Do not get version information about APKs.")
+ parser.add_argument(
+ "--no-running",
+ action="store_true",
+ help="Do not target already running processes.")
+ parser.add_argument(
+ "--no-startup",
+ action="store_true",
+ help="Do not target processes that start during "
+ "the profile.")
+ parser.add_argument(
+ "--shmem-size",
+ help="Size of buffer between client and "
+ "heapprofd. Default 8MiB. Needs to be a power of two "
+ "multiple of 4096, at least 8192.",
+ type=int,
+ default=8 * 1048576)
+ parser.add_argument(
+ "--block-client",
+ help="When buffer is full, block the "
+ "client to wait for buffer space. Use with caution as "
+ "this can significantly slow down the client. "
+ "This is the default",
+ action="store_true")
+ parser.add_argument(
+ "--no-block-client",
+ help="When buffer is full, stop the "
+ "profile early.",
+ action="store_true")
+ parser.add_argument(
+ "--idle-allocations",
+ help="Keep track of how many "
+ "bytes were unused since the last dump, per "
+ "callstack",
+ action="store_true")
+ parser.add_argument(
+ "--dump-at-max",
+ help="Dump the maximum memory usage"
+ "rather than at the time of the dump.",
+ action="store_true")
+ parser.add_argument(
+ "--simpleperf",
+ action="store_true",
+ help="Get simpleperf profile of heapprofd. This is "
+ "only for heapprofd development.")
+ parser.add_argument(
+ "--trace-to-text-binary",
+ help="Path to local trace to text. For debugging.")
+ parser.add_argument(
+ "--print-config",
+ action="store_true",
+ help="Print config instead of running. For debugging.")
args = parser.parse_args()
fail = False
if args.block_client and args.no_block_client:
- print("FATAL: Both block-client and no-block-client given.",
- file=sys.stderr)
+ print(
+ "FATAL: Both block-client and no-block-client given.", file=sys.stderr)
fail = True
if args.pid is None and args.name is None:
print("FATAL: Neither PID nor NAME given.", file=sys.stderr)
@@ -237,10 +286,12 @@
if args.continuous_dump:
continuous_dump_cfg = CONTINUOUS_DUMP.format(
dump_interval=args.continuous_dump)
- cfg = CFG.format(interval=args.interval,
- duration=args.duration, target_cfg=target_cfg,
- continuous_dump_cfg=continuous_dump_cfg,
- shmem_size=args.shmem_size)
+ cfg = CFG.format(
+ interval=args.interval,
+ duration=args.duration,
+ target_cfg=target_cfg,
+ continuous_dump_cfg=continuous_dump_cfg,
+ shmem_size=args.shmem_size)
if not args.no_versions:
cfg += PACKAGES_LIST_CFG
@@ -250,7 +301,8 @@
if args.disable_selinux:
enforcing = subprocess.check_output(['adb', 'shell', 'getenforce'])
- atexit.register(subprocess.check_call,
+ atexit.register(
+ subprocess.check_call,
['adb', 'shell', 'su root setenforce %s' % enforcing])
subprocess.check_call(['adb', 'shell', 'su root setenforce 0'])
@@ -261,25 +313,25 @@
subprocess.check_call(
['adb', 'shell', 'setprop persist.heapprofd.enable 1'])
atexit.register(subprocess.check_call,
- ['adb', 'shell', 'setprop persist.heapprofd.enable 0'])
+ ['adb', 'shell', 'setprop persist.heapprofd.enable 0'])
user = subprocess.check_output(['adb', 'shell', 'whoami']).strip()
if args.simpleperf:
- subprocess.check_call(
- ['adb', 'shell',
- 'mkdir -p /data/local/tmp/heapprofd_profile && '
- 'cd /data/local/tmp/heapprofd_profile &&'
- '(nohup simpleperf record -g -p $(pidof heapprofd) 2>&1 &) '
- '> /dev/null'])
+ subprocess.check_call([
+ 'adb', 'shell', 'mkdir -p /data/local/tmp/heapprofd_profile && '
+ 'cd /data/local/tmp/heapprofd_profile &&'
+ '(nohup simpleperf record -g -p $(pidof heapprofd) 2>&1 &) '
+ '> /dev/null'
+ ])
perfetto_pid = subprocess.check_output(
- ['adb', 'exec-out', PERFETTO_CMD.format(cfg=cfg, user=user)]).strip()
+ ['adb', 'exec-out',
+ PERFETTO_CMD.format(cfg=cfg, user=user)]).strip()
try:
int(perfetto_pid.strip())
except ValueError:
- print("Failed to invoke perfetto: {}".format(perfetto_pid),
- file=sys.stderr)
+ print("Failed to invoke perfetto: {}".format(perfetto_pid), file=sys.stderr)
return 1
old_handler = signal.signal(signal.SIGINT, sigint_handler)
@@ -289,8 +341,7 @@
device_connected = True
while not device_connected or (exists and not IS_INTERRUPTED):
exists = subprocess.call(
- ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)],
- **NOOUT) == 0
+ ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)], **NOOUT) == 0
device_connected = subprocess.call(['adb', 'shell', 'true'], **NOOUT) == 0
time.sleep(1)
signal.signal(signal.SIGINT, old_handler)
@@ -301,11 +352,10 @@
subprocess.check_call(['adb', 'shell', 'killall', '-INT', 'simpleperf'])
print("Waiting for simpleperf to exit.")
while subprocess.call(
- ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'],
- **NOOUT) == 0:
+ ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'], **NOOUT) == 0:
time.sleep(1)
- subprocess.check_call(['adb', 'pull', '/data/local/tmp/heapprofd_profile',
- '/tmp'])
+ subprocess.check_call(
+ ['adb', 'pull', '/data/local/tmp/heapprofd_profile', '/tmp'])
print("Pulled simpleperf profile to /tmp/heapprofd_profile")
# Wait for perfetto cmd to return.
@@ -314,9 +364,11 @@
['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0
time.sleep(1)
- subprocess.check_call(['adb', 'pull',
- '/data/misc/perfetto-traces/profile-{}'.format(user),
- '/tmp/profile'], stdout=NULL)
+ subprocess.check_call([
+ 'adb', 'pull', '/data/misc/perfetto-traces/profile-{}'.format(user),
+ '/tmp/profile'
+ ],
+ stdout=NULL)
trace_to_text_output = subprocess.check_output(
[trace_to_text_binary, 'profile', '/tmp/profile'])
profile_path = None
@@ -335,11 +387,12 @@
file=sys.stderr)
return 1
- subprocess.check_call(['gzip'] + [os.path.join(profile_path, x) for x in
- os.listdir(profile_path)])
+ subprocess.check_call(
+ ['gzip'] +
+ [os.path.join(profile_path, x) for x in os.listdir(profile_path)])
- symlink_path = os.path.join(os.path.dirname(profile_path),
- "heap_profile-latest")
+ symlink_path = os.path.join(
+ os.path.dirname(profile_path), "heap_profile-latest")
if os.path.lexists(symlink_path):
os.unlink(symlink_path)
os.symlink(profile_path, symlink_path)