forked from eclipse-cdt-cloud/tsp-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp-cli-client
executable file
·405 lines (354 loc) · 15.9 KB
/
tsp-cli-client
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# PYTHON_ARGCOMPLETE_OK
#
# The MIT License (MIT)
#
# Copyright (C) 2020 - Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argcomplete
import argparse
import sys
from requests.exceptions import ConnectionError
from termcolor import colored
from datetime import timedelta
from decimal import Decimal
from os.path import os
from tsp.tree_model import TreeModel
from tsp.tsp_client import TspClient
from tsp.xy_model import XYModel
#api_token = 'your_token_goes_here'
NS_PER_SEC = 1000000000
def print_uuid(elem):
return colored(elem.UUID, 'yellow')
def print_experiment(elem):
print('Experiment:')
print('----------')
print(' {0}: ({1}) start={2} end={3} nbEvents={4} indexing={5}'.format(
elem.name, print_uuid(elem), elem.start, elem.end, elem.number_of_events, elem.indexin_status))
print('Trace(s):')
print('------')
for trace in elem.traces.traces:
print_trace(trace)
def print_trace(elem):
print(' {0}: {1} ({2}) start={3} end={4} nbEvents={5} indexing={6}'.format(
elem.name, elem.path, print_uuid(elem), elem.start, elem.end, elem.number_of_events, elem.indexin_status))
def print_time(elem):
try:
t = Decimal(elem)
time = int(int(t) / NS_PER_SEC)
ns = int(t) % NS_PER_SEC
if (time != 0):
return '{0} s'.format(timedelta(seconds=time, microseconds=ns/1000))
if ns > 1000000:
return '{0} ms'.format(ns / 1000000)
if ns > 1000:
return '{0} µs'.format(ns / 1000)
return '{0} ns'.format(ns)
except ValueError as e:
print(e)
return elem
def print_output(output):
print(' {0}: {1} {2} ({3})'.format(output.name,
output.description, output.type, output.id))
def print_outputs(descriptors):
if not descriptors:
print('No output descriptors for trace {0} ({1}))'.format(
t.name, print_uuid(t)))
for o in output_descriptors.descriptors:
print_output(o)
# TODO remove when available
#if (o.id == "org.eclipse.tracecompass.internal.analysis.profiling.callstack.provider.CallStackDataProvider"):
# print(' {0}: {1} ({2})'.format("Function Duration Statistics", "Function Duration Statistics",
# "org.eclipse.tracecompass.internal.analysis.profiling.core.callgraph.callgraphanalysis.statistics"))
def get_descriptor(uuid, id):
response = tsp_client.fetch_experiment_output(uuid, id)
if response.status_code == 200:
return response.model
return None
description = """CLI client to send Trace Server Protocol commands to a Trace Server."""
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=description)
parser.add_argument("--ip", dest="ip_address",
help="IP address of trace server", metavar="IP", default="localhost")
parser.add_argument("--port", dest="port",
help="port of trace server", metavar="PORT", default="8080")
parser.add_argument("--open-trace", dest="trace",
help="Path to trace to open", metavar="TRACE_PATH")
parser.add_argument("--name", dest="name",
metavar="NAME", help="trace name")
parser.add_argument("--list-trace", dest="list_trace",
help="Get details on the given trace", metavar="UUID")
parser.add_argument("--list-traces", dest="list_traces",
action='store_true', help="List all open traces on the server")
parser.add_argument("--delete-trace", dest="delete_trace",
help="Delete a trace on the server", metavar="UUID")
parser.add_argument("--open-experiment", dest="experiment",
help="Open experiment on the server", metavar="EXP_NAME")
parser.add_argument("--list-experiment", dest="list_experiment",
help="Get details on the given experiment", metavar="UUID")
parser.add_argument("--list-experiments", dest="list_experiments",
action='store_true', help="List all open experiments on the server")
parser.add_argument("--delete-experiment", dest="delete_experiment",
help="Delete an experiment on the server", metavar="UUID")
parser.add_argument("--list-outputs", dest="list_outputs",
help="Get details on the given trace", metavar="UUID")
parser.add_argument("--list-output", dest="list_output",
help="Get details on the given output of a trace", metavar="OUTPUT_ID")
parser.add_argument("--get-tree", dest="get_tree",
help="Get the timegraph tree of an output", metavar="OUTPUT_ID")
parser.add_argument("--get-xy-tree", dest="get_xy_tree",
help="Get the XY tree of an output", metavar="OUTPUT_ID")
parser.add_argument("--get-xy", dest="get_xy",
help="Get the XY data of an output", metavar="OUTPUT_ID")
parser.add_argument("--items", dest="items",
help="The list of XY items requested", nargs="*")
parser.add_argument("--times", dest="times",
help="The list of XY times requested", nargs="*")
parser.add_argument("--uuid", dest="uuid", help="The UUID of a trace")
parser.add_argument("--uuids", dest="uuids",
help="The list of UUIDs", nargs="*")
parser.add_argument("--do-delete-traces", dest="do_delete_traces",
action='store_true', help="Also delete traces when deleting experiment")
parser.add_argument("--paths", dest="paths",
help="List of trace paths to be part of an experiment", nargs="*")
parser.add_argument("--list-extensions", dest="extensions",
action='store_true', help="Get the extensions loaded")
parser.add_argument("--load-extension", dest="load_extension",
help="Load an extension", metavar="EXTENSION_PATH")
parser.add_argument("--delete-extension", dest="delete_extension",
help="Delete an extension", metavar="EXTENSION_NAME")
argcomplete.autocomplete(parser)
options = parser.parse_args()
# Check for arguments
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
api_url_base = 'http://{0}:{1}/tsp/api/'.format(
options.ip_address, options.port)
tsp_client = TspClient(api_url_base)
try:
if (options.trace):
if options.name is not None:
response = tsp_client.open_trace(options.name, options.trace)
if response.status_code == 200:
res = response.model
print_trace(res)
sys.exit(0)
else:
sys.exit(1)
else:
print("Provide a name for the trace using option --name")
sys.exit(1)
if (options.list_trace):
response = tsp_client.fetch_trace(options.list_trace)
if response.status_code == 200:
res = response.model
print_trace(res)
sys.exit(0)
else:
sys.exit(1)
if (options.list_traces):
response = tsp_client.fetch_traces()
if response.status_code == 200:
if not response.model.traces:
print("No traces open on the server")
for t in response.model.traces:
print_trace(t)
sys.exit(0)
else:
sys.exit(1)
if (options.delete_trace):
response = tsp_client.delete_trace(options.delete_trace, False)
if response.status_code == 200:
print("Successfully deleted trace")
sys.exit(0)
else:
sys.exit(1)
trace_uuids = []
if (options.experiment):
if options.paths is not None:
for path in options.paths:
name = os.path.basename(os.path.normpath(path))
response = tsp_client.open_trace(name, path)
if response.status_code == 200:
res = response.model
trace_uuids.append(res.UUID)
else:
sys.exit(1)
elif options.uuids is not None:
trace_uuids = options.uuids
else:
print("Provide a path for the trace using option --paths")
sys.exit(1)
response = tsp_client.open_experiment(
options.experiment, trace_uuids)
if response.status_code == 200:
res = response.model
print_experiment(res)
sys.exit(0)
else:
sys.exit(1)
if (options.list_experiment):
response = tsp_client.fetch_experiment(options.list_experiment)
if response.status_code == 200:
res = response.model
print_experiment(res)
sys.exit(0)
else:
sys.exit(1)
if (options.list_experiments):
response = tsp_client.fetch_experiments()
if response.status_code == 200:
if not response.model.experiments:
print("No experiments open on the server")
for exp in response.model.experiments:
print_experiment(exp)
sys.exit(0)
else:
sys.exit(1)
if (options.delete_experiment):
response = tsp_client.delete_experiment(options.delete_experiment)
if response.status_code == 200:
print("Successfully deleted experiment")
if (options.do_delete_traces):
for trace in response.model.traces.traces:
del_response = tsp_client.delete_trace(
trace.UUID, False)
if del_response.status_code == 200:
print("Successfully deleted trace")
else:
print("Failed deleting trace " + trace.name)
sys.exit(0)
else:
sys.exit(1)
if (options.list_outputs):
response = tsp_client.fetch_experiment_outputs(
options.list_outputs)
if response.status_code == 200:
output_descriptors = response.model
print_outputs(output_descriptors.descriptors)
sys.exit(0)
else:
print("List outputs failed")
sys.exit(1)
if (options.list_output):
if options.uuid is not None:
output_descriptor = get_descriptor(
options.uuid, options.list_output)
if (output_descriptor is not None):
print(output_descriptor)
else:
print("Trace UUID is missing")
sys.exit(1)
if (options.get_tree):
if options.uuid is not None:
output_descriptor = get_descriptor(
options.uuid, options.get_tree)
if (output_descriptor is None):
sys.exit(1)
response = tsp_client.fetch_timegraph_tree(
options.uuid, options.get_tree)
if response.status_code == 200:
tree = response.model.model
if tree is None:
print("Tree had no model; retry?")
sys.exit(1)
if (output_descriptor.type == "DATA_TREE"):
treeModel = TreeModel(tree.entries, tree.descriptors)
else:
treeModel = TreeModel(tree.entries)
treeModel.print()
sys.exit(0)
else:
sys.exit(1)
else:
print("Trace UUID is missing")
sys.exit(1)
if (options.get_xy_tree):
if options.uuid is not None:
output_descriptor = get_descriptor(
options.uuid, options.get_xy_tree)
if (output_descriptor is None):
sys.exit(1)
response = tsp_client.fetch_xy_tree(
options.uuid, options.get_xy_tree)
if response.status_code == 200:
tree = response.model.model
if tree is None:
print("Tree had no model; retry?")
sys.exit(1)
if (output_descriptor.type == "TREE_TIME_XY"):
treeModel = TreeModel(tree.entries, tree.descriptors)
else:
treeModel = TreeModel(tree.entries)
treeModel.print()
sys.exit(0)
else:
sys.exit(1)
else:
print("Trace UUID is missing")
sys.exit(1)
if (options.get_xy):
if not options.items or not options.times:
print("Provide requested --items and --times for the XY data")
sys.exit(1)
if options.uuid is not None:
parameters = {TspClient.REQUESTED_TIME_KEY: list(map(int, options.times)),
TspClient.REQUESTED_ITEM_KEY: list(map(int, options.items))}
params = {TspClient.PARAMETERS_KEY: parameters}
response = tsp_client.fetch_xy(
options.uuid, options.get_xy, params)
if response.status_code == 200:
xyModel = response.model.model
xyModel.print()
sys.exit(0)
else:
sys.exit(1)
else:
print("Trace UUID is missing")
sys.exit(1)
if (options.extensions):
response = tsp_client.fetch_extensions()
if response.status_code == 200:
extension_set = response.model
if not extension_set:
print('No extensions loaded')
for e in extension_set.extension_set:
print(' {0}'.format(e.extension))
sys.exit(0)
else:
sys.exit(1)
if (options.load_extension):
response = tsp_client.post_extension(options.load_extension)
if response.status_code == 200:
sys.exit(0)
else:
sys.exit(1)
if (options.delete_extension):
response = tsp_client.delete_extension(options.delete_extension)
if response.status_code == 200:
sys.exit(0)
else:
sys.exit(1)
except ConnectionError as e:
print('Unexpected error: {0}'.format(e))
sys.exit(1)
sys.exit(0)