-
Notifications
You must be signed in to change notification settings - Fork 62
/
kv_trace_dump
executable file
·81 lines (67 loc) · 2.97 KB
/
kv_trace_dump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# Copyright 2017-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
# in that file, in accordance with the Business Source License, use of this
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.
#
# Tool to dump a KV-Engine trace file to JSON; which can be viewed
# with Chrome's trace viewer (chrome://tracing).
import argparse
import os
import shutil
import subprocess
import sys
parser = argparse.ArgumentParser(
description='Dump a KV-Engine trace file as JSON.')
parser.add_argument('--mcctl', help='Path to mcctl binary')
parser.add_argument('-H', '--hostname', required=True,
help='host:port to connect to (e.g. localhost:11210)')
parser.add_argument('-u', '--username', required=True, help='Username')
parser.add_argument('-P', '--password', required=False, help='Password')
parser.add_argument('-n', '--norestart', dest='restart', action='store_false',
help="Don't restart tracing after dumping the trace file")
parser.add_argument('outfile', type=argparse.FileType('w'))
args = parser.parse_args()
# Locate the mcctl binary
# 1. If the user specified a location, use that one.
mcctl_bin = None
if args.mcctl:
mcctl_bin = args.mcctl
else:
# 2. Try the path
mcctl_bin = shutil.which("mcctl")
# on windows, the current working directory will always be prepended to
# the path used by `which()`.
# If the executable is found in the cwd `which` returns a relative path
# to it. We don't want that though.
if not mcctl_bin or os.path.normpath(mcctl_bin) == "mcctl":
# 3. Look for mcctl in the same directory as the path used to invoke
# kv_trace_dump, which should be INSTALL_DIR/bin. Note: that
# location will likely be a symlink on Unix, but we still want that
# location, not the location the symlink points to.
script_dir = os.path.dirname(sys.argv[0])
mcctl_bin = os.path.abspath(os.path.join(script_dir, "mcctl"))
if not os.path.isfile(mcctl_bin):
print("Error: No mcctl program found at {} - cannot continue.".format(
mcctl_bin), file=sys.stderr)
sys.exit(1)
mcctl_args = [mcctl_bin,
'-h', args.hostname,
'-u', args.username]
if args.password:
mcctl_args.extend(['-P', args.password])
subprocess.check_call(mcctl_args + ['set', 'trace.stop'])
uuid = subprocess.check_output(mcctl_args + ['get', 'trace.dump.begin'],
universal_newlines=True)
uuid = uuid.strip()
chunk = subprocess.check_output(
mcctl_args + ['get', 'trace.dump.get?id=' + uuid],
universal_newlines=True).rstrip()
args.outfile.write(chunk)
# Delete the trace dump on the server
subprocess.check_call(mcctl_args + ['set', 'trace.dump.clear', uuid])
if args.restart:
subprocess.check_call(mcctl_args + ['set', 'trace.start'])