1 #! /usr/bin/env python3
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2020 Intel Corporation
6 Script to be used with V2 Telemetry.
7 Allows the user input commands and read the Telemetry response.
20 TELEMETRY_VERSION = "v2"
21 SOCKET_NAME = 'dpdk_telemetry.{}'.format(TELEMETRY_VERSION)
22 DEFAULT_PREFIX = 'rte'
26 def read_socket(sock, buf_len, echo=True):
27 """ Read data from socket and return it in JSON format """
28 reply = sock.recv(buf_len).decode()
30 ret = json.loads(reply)
31 except json.JSONDecodeError:
32 print("Error in reply: ", reply)
36 print(json.dumps(ret))
40 def get_app_name(pid):
41 """ return the app name for a given PID, for printing """
42 proc_cmdline = os.path.join('/proc', str(pid), 'cmdline')
44 with open(proc_cmdline) as f:
45 argv0 = f.read(1024).split('\0')[0]
46 return os.path.basename(argv0)
48 # ignore file not found errors
49 if e.errno != errno.ENOENT:
54 def find_sockets(path):
55 """ Find any possible sockets to connect to and return them """
56 return glob.glob(os.path.join(path, SOCKET_NAME + '*'))
59 def print_socket_options(prefix, paths):
60 """ Given a set of socket paths, give the commands needed to connect """
62 if prefix != DEFAULT_PREFIX:
63 cmd += " -f " + prefix
64 for s in sorted(paths):
65 sock_name = os.path.basename(s)
66 if sock_name.endswith(TELEMETRY_VERSION):
67 print("- {} # Connect with '{}'".format(os.path.basename(s),
70 print("- {} # Connect with '{} -i {}'".format(os.path.basename(s),
75 def handle_socket(args, path):
76 """ Connect to socket and handle user input """
77 prompt = '' # this evaluates to false in conditions
78 sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
81 if os.isatty(sys.stdin.fileno()):
83 print("Connecting to " + path)
87 print("Error connecting to " + path)
89 # if socket exists but is bad, or if non-interactive just return
90 if os.path.exists(path) or not prompt:
92 # if user didn't give a valid socket path, but there are
93 # some sockets, help the user out by printing how to connect
94 socks = find_sockets(os.path.dirname(path))
96 print("\nOther DPDK telemetry sockets found:")
97 print_socket_options(args.file_prefix, socks)
99 json_reply = read_socket(sock, 1024, prompt)
100 output_buf_len = json_reply["max_output_len"]
101 app_name = get_app_name(json_reply["pid"])
102 if app_name and prompt:
103 print('Connected to application: "%s"' % app_name)
105 # get list of commands for readline completion
106 sock.send("/".encode())
107 CMDS = read_socket(sock, output_buf_len, False)["/"]
111 text = input(prompt).strip()
112 while text != "quit":
113 if text.startswith('/'):
114 sock.send(text.encode())
115 read_socket(sock, output_buf_len)
116 text = input(prompt).strip()
123 def readline_complete(text, state):
124 """ Find any matching commands from the list based on user input """
125 all_cmds = ['quit'] + CMDS
127 matches = [c for c in all_cmds if c.startswith(text)]
130 return matches[state]
133 def get_dpdk_runtime_dir(fp):
134 """ Using the same logic as in DPDK's EAL, get the DPDK runtime directory
135 based on the file-prefix and user """
136 if (os.getuid() == 0):
137 return os.path.join('/var/run/dpdk', fp)
138 return os.path.join(os.environ.get('XDG_RUNTIME_DIR', '/tmp'), 'dpdk', fp)
141 readline.parse_and_bind('tab: complete')
142 readline.set_completer(readline_complete)
143 readline.set_completer_delims(readline.get_completer_delims().replace('/', ''))
145 parser = argparse.ArgumentParser()
146 parser.add_argument('-f', '--file-prefix', default=DEFAULT_PREFIX,
147 help='Provide file-prefix for DPDK runtime directory')
148 parser.add_argument('-i', '--instance', default='0', type=int,
149 help='Provide file-prefix for DPDK runtime directory')
150 args = parser.parse_args()
151 sock_path = os.path.join(get_dpdk_runtime_dir(args.file_prefix), SOCKET_NAME)
152 if args.instance > 0:
153 sock_path += ":{}".format(args.instance)
154 handle_socket(args, sock_path)