summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXiaoqin Ma <xiaoqinma@google.com>2023-05-26 16:50:33 +0000
committerXiaoqin Ma <xiaoqinma@google.com>2023-06-14 16:41:11 +0000
commitfe90a84422c6eff58bc6b37225d619b51da66ad1 (patch)
tree4591813a3fa7a8156df2ac452f570c68a04b4d91
parent0101c0f693e0286005d933ff0e0c73cf7efc7379 (diff)
downloadtrout-fe90a84422c6eff58bc6b37225d619b51da66ad1.tar.gz
Replace subprocess.run with python library function.android14-dev
Using update_trace and calculate_time_offset library functions. Bug: 217373918 Test: manually. Change-Id: I6c21512519d0033d90765132961d8e93ecfe2dee
-rwxr-xr-xtools/tracing/tooling/calculate_time_offset.py89
-rwxr-xr-xtools/tracing/tooling/prepare_tracing.py25
-rwxr-xr-xtools/tracing/tooling/tracing_agent.py51
-rwxr-xr-xtools/tracing/tooling/update_trace.py40
4 files changed, 109 insertions, 96 deletions
diff --git a/tools/tracing/tooling/calculate_time_offset.py b/tools/tracing/tooling/calculate_time_offset.py
index 0a84055..39de83b 100755
--- a/tools/tracing/tooling/calculate_time_offset.py
+++ b/tools/tracing/tooling/calculate_time_offset.py
@@ -20,7 +20,6 @@ from threading import Thread
import os
import re
import sys
-import subprocess
import time
import traceback
@@ -29,6 +28,8 @@ import traceback
from paramiko import SSHClient
from paramiko import AutoAddPolicy
+from prepare_tracing import adb_run
+
# Usage:
# ./calculate_time_offset.py --host_username root --host_ip 10.42.0.247
# --guest_serial 10.42.0.247 --clock_name CLOCK_REALTIME
@@ -36,22 +37,13 @@ from paramiko import AutoAddPolicy
# ./calculate_time_offset.py --host_username root --host_ip 10.42.0.247
# --guest_serial 10.42.0.247 --clock_name CLOCK_REALTIME --mode trace
-def subprocessRun(cmd):
- try :
- result = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
- except Exception as e:
- traceresult = traceback.format_exc()
- error_msg = f'subprocessRun caught an exception: {traceback.format_exc()}'
- sys.exit(error_msg)
- return result.stdout.decode('utf-8')
-
class Device:
# Get the machine time
- def __init__(self, args):
- if args.clock_name != None:
- self.time_cmd += f' {args.clock_name}'
- if args.mode == "trace":
- if args.clock_name == None:
+ def __init__(self, clock_name, mode):
+ if clock_name != None:
+ self.time_cmd += f' {clock_name}'
+ if mode == "trace":
+ if clock_name == None:
raise SystemExit("Error: with trace mode, clock_name must be specified")
self.time_cmd = f'{self.time_cmd} --trace'
def GetTime(self):
@@ -61,31 +53,41 @@ class Device:
pattern = r'\d+'
match = re.search(pattern, time_str)
if match is None:
- traceresult = traceback.format_exc()
- error_msg = f'ParseTime no match time string({time_str}): {traceback.format_exc()}'
- sys.exit(error_msg)
+ raise Exception(f'Error: ParseTime no match time string: {time_str}')
return int(match.group())
+ # Here is an example of time_util with --trace flag enable and given a clockname
+ # will give a snapshot of the CPU counter and clock timestamp.
+ # time_util CLOCK_REALTIME --trace
+ # 6750504532818 CPU tick value
+ # 1686355159395639260 CLOCK_REATIME
+ # 0.0192 CPU tick per nanosecond
+ #
+ # The example's output is ts_str
def TraceTime(self, ts_str):
lines = ts_str.split("\n")
if len(lines) < 3:
- sys.exit(f'{ts_str} should be three lines')
+ raise Exception(f'Error: TraceTime input is wrong {ts_str}.'
+ 'Expecting three lines of input: '
+ 'cpu_tick_value, CLOCK value, and CPU cycles per nanoseconds')
+
self.cpu_ts = int(lines[0])
self.clock_ts = int(lines[1])
self.cpu_cycles = float(lines[2])
class QnxDevice(Device):
- def __init__(self, args):
+ def __init__(self, host_username, host_ip, clock_name, mode):
self.sshclient = SSHClient()
self.sshclient.load_system_host_keys()
self.sshclient.set_missing_host_key_policy(AutoAddPolicy())
- self.sshclient.connect(args.host_ip, username=args.host_username)
+ self.sshclient.connect(host_ip, username=host_username)
self.time_cmd = "/bin/QnxClocktime"
- super().__init__(args)
+ super().__init__(clock_name, mode)
def GetTime(self):
(stdin, stdout, stderr) = self.sshclient.exec_command(self.time_cmd)
return stdout
+
def ParseTime(self, time_str):
time_decoded_str = time_str.read().decode()
return super().ParseTime(time_decoded_str)
@@ -96,16 +98,19 @@ class QnxDevice(Device):
super().TraceTime(ts_str)
class AndroidDevice(Device):
- def __init__(self, args):
- subprocessRun(['adb', 'connect', args.guest_serial])
+ def __init__(self, guest_serial, clock_name, mode):
+ adb_run(guest_serial, ['connect'])
self.time_cmd = "/vendor/bin/android.automotive.time_util"
- self.serial = args.guest_serial
- super().__init__(args)
+ self.serial = guest_serial
+ super().__init__(clock_name, mode)
+
def GetTime(self):
- ts = subprocessRun(['adb', '-s', self.serial, 'shell', self.time_cmd])
+ ts = adb_run(self.serial, ['shell', self.time_cmd])
return ts
+
def TraceTime(self):
super().TraceTime(self.GetTime())
+
# measure the time offset between device1 and device2 with ptp,
# return the average value over cnt times.
def Ptp(device1, device2):
@@ -132,16 +137,23 @@ def Ptp(device1, device2):
return int(offset)
raise SystemExit(f"Network delay is still too big after {max_retry} retries")
+# It assumes device1 and device2 have access to the same CPU counter and uses the cpu counter
+# as the time source to calculate the time offset between device1 and device2.
def TraceTimeOffset(device1, device2):
- device1.TraceTime()
- device2.TraceTime()
offset = device2.clock_ts - device1.clock_ts - ((device2.cpu_ts - device1.cpu_ts)/device2.cpu_cycles)
return int(offset)
+def CalculateTimeOffset(host_username, hostip, guest_serial, clock_name, mode):
+ qnx = QnxDevice(host_username, hostip, clock_name, mode)
+ android = AndroidDevice(guest_serial, clock_name, mode)
+ if mode == "trace":
+ return TraceTimeOffset(qnx, android)
+ else:
+ return Ptp(qnx, android)
+
+
def ParseArguments():
- parser = argparse.ArgumentParser(
- prog = 'ptp_qnx_android.py',
- description='Test PTP')
+ parser = argparse.ArgumentParser()
parser.add_argument('--host_ip', required=True,
help = 'host IP address')
parser.add_argument('--host_username', required=True,
@@ -151,18 +163,13 @@ def ParseArguments():
parser.add_argument('--clock_name', required=False, choices =['CLOCK_REALTIME','CLOCK_MONOTONIC'],
help = 'clock that will be used for the measument. By default CPU counter is used.')
parser.add_argument('--mode', choices=['ptp', 'trace'], default='ptp',
- help='select the mode of operation. trace option meaning using CPU counter to calculate the time offset between devices')
+ help='select the mode of operation. If the two devices have access of the same CPU counter, '
+ 'use trace option. Otherwise use ptp option.')
return parser.parse_args()
def main():
args = ParseArguments()
- qnx = QnxDevice(args)
- android = AndroidDevice(args)
- if args.mode == "trace":
- offset = TraceTimeOffset(qnx, android)
- else:
- offset = Ptp(qnx, android)
- print(f'Time offset between {type(qnx).__name__} and {type(android).__name__} is {offset} nanoseconds')
-
+ time_offset = CalculateTimeOffset(args.host_username, args.host_ip, args.guest_serial, args.clock_name, args.mode)
+ print(f'Time offset between host and guest is {time_offset} nanoseconds')
if __name__ == "__main__":
main()
diff --git a/tools/tracing/tooling/prepare_tracing.py b/tools/tracing/tooling/prepare_tracing.py
index fb7f8d3..ad4f456 100755
--- a/tools/tracing/tooling/prepare_tracing.py
+++ b/tools/tracing/tooling/prepare_tracing.py
@@ -20,20 +20,21 @@ import os
import subprocess
import sys
-def subprocess_run(command):
+def subprocess_run(command, shell=True):
try:
print(f'subprocess run {command}')
- subprocess.run(command, shell=True, check=True)
+ result = subprocess.run(command, shell=shell, stdout=subprocess.PIPE, check=True)
except Exception as e:
- print(f'Execution error: {command}')
- sys.exit(f"{type(e).__name__}: {e}")
+ raise Exception(f'Execution error for {command}: {e}')
+
+ return result.stdout.decode('utf-8')
def adb_run(ip, cmd):
- if cmd == 'connect':
- adb_cmd = f'adb connect {ip}'
+ if cmd == ['connect']:
+ adb_cmd = ['adb', 'connect', ip]
else:
- adb_cmd = f'adb -s {ip} {cmd}'
- subprocess_run(adb_cmd)
+ adb_cmd = ['adb', '-s', ip] + cmd
+ return subprocess_run(adb_cmd, False)
def prepare_qnx(tracing_dir, qnx_dev_dir, qnx_ip, filepath):
source_file = os.path.join(tracing_dir, 'tooling', 'qnx_perfetto.py')
@@ -55,11 +56,11 @@ def prepare_qnx(tracing_dir, qnx_dev_dir, qnx_ip, filepath):
subprocess_run(command)
def prepare_android(serial_num, aaos_time_util):
- adb_run(serial_num, 'connect')
- adb_run(serial_num, 'root')
- adb_run(serial_num, 'remount')
+ adb_run(serial_num, ['connect'])
+ adb_run(serial_num, ['root'])
+ adb_run(serial_num, ['remount'])
- command = f'push {aaos_time_util} /vendor/bin/android.automotive.time_util'
+ command = ['push', aaos_time_util, '/vendor/bin/android.automotive.time_util']
adb_run(serial_num, command)
def parse_arguments():
diff --git a/tools/tracing/tooling/tracing_agent.py b/tools/tracing/tooling/tracing_agent.py
index d9710b8..8cfe1d9 100755
--- a/tools/tracing/tooling/tracing_agent.py
+++ b/tools/tracing/tooling/tracing_agent.py
@@ -30,7 +30,9 @@ import traceback
from paramiko import SSHClient
from paramiko import AutoAddPolicy
+import calculate_time_offset
from remote_slay import slay_process
+import update_trace
# This script works on Linux workstation.
# We haven't tested on Windows/macOS.
@@ -209,7 +211,7 @@ class QnxTracingAgent(TracingAgent):
# for traceprinter options, reference:
# http://www.qnx.com/developers/docs/7.0.0/index.html#com.qnx.doc.neutrino.utilities/topic/t/traceprinter.html
global qnx_dev_dir
- traceprinter = os.path.join(qnx_dev_dir, 'traceprinter')
+ traceprinter = os.path.join(qnx_dev_dir, 'host/linux/x86_64/usr/bin/', 'traceprinter')
traceprinter_cmd = [traceprinter,
'-p', '%C %t %Z %z',
'-f', f'{self.tracing_kev_file_path}',
@@ -217,7 +219,7 @@ class QnxTracingAgent(TracingAgent):
subprocessRun(traceprinter_cmd)
# convert tracing file in text format to json format:
- qnx2perfetto = os.path.join(qnx_dev_dir, 'qnx_perfetto.py')
+ qnx2perfetto = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'qnx_perfetto.py')
convert_cmd = [qnx2perfetto,
f'{self.tracing_printer_file_path}']
subprocessRun(convert_cmd)
@@ -263,41 +265,32 @@ class AndroidTracingAgent(TracingAgent):
return subprocessRun(adb_cmd)
def merge_files(in_file1, in_file2, out_file):
- with open(in_file1, 'r') as f:
- trace_dict1 = json.loads(f.read())
+ try:
+ with open(in_file1, 'r') as f:
+ trace_dict1 = json.loads(f.read())
- with open(in_file2, 'r') as f:
- trace_dict2 = json.loads(f.read())
+ with open(in_file2, 'r') as f:
+ trace_dict2 = json.loads(f.read())
- trace_dict1.update(trace_dict2)
- with open(out_file, 'w') as f:
- json.dump(trace_dict1, f)
- print(f"Updated trace data saved to {out_file}")
+ trace_dict1.update(trace_dict2)
+ with open(out_file, 'w') as f:
+ json.dump(trace_dict1, f)
+ print(f"Updated trace data saved to {out_file}")
+ except Exception as e:
+ sys.exit(f'merge_files failure due to: {e}')
def update_and_merge_files(args, host_agent, guest_agent):
# calculate the time offset
- # TODO(b/267675642): plugin calculate_time_offset library function.
- calculate_time_offset_cmd = os.path.join(os.getcwd(), 'calculate_time_offset.py')
- result = subprocessRun([calculate_time_offset_cmd,
- "--host_username", args.host_username,
- "--host_ip", args.host_ip,
- "--guest_serial", args.guest_serial,
- "--clock_name", "CLOCK_REALTIME",
- "--mode", "trace"])
- pattern = r'\d+'
- match = re.search(pattern, result.stdout.decode('utf-8'))
- if match is None:
- traceresult = traceback.format_exc()
- error_msg = f'ParseTime no match time string({result.stdout}): {traceback.format_exc()}'
- sys.exit(error_msg)
- time_offset = int(match.group())
+ try:
+ time_offset = calculate_time_offset.CalculateTimeOffset(
+ args.host_username, args.host_ip, args.guest_serial, "CLOCK_REALTIME", "trace")
+ except Exception as e:
+ sys.exit(f'Exception: catch calculate_time_offset exception {e}')
# update the timestamp and process id in the host json file
- # TODO(b/267675642): plugin update_trace library function.
host_json_file = '{}.json'.format(host_agent.tracing_printer_file_path)
- update_trace_cmd = os.path.join(os.getcwd(), 'update_trace.py')
- subprocessRun([update_trace_cmd, '--input_file', f'{host_json_file}',
- '--time_offset', f'{time_offset}'])
+ if not update_trace.update_trace_file(host_json_file, time_offset):
+ sys.exit('Error: update_trace_file')
# convert guest trace file to .json format
global perfetto_dev_dir
diff --git a/tools/tracing/tooling/update_trace.py b/tools/tracing/tooling/update_trace.py
index e689282..d0d2100 100755
--- a/tools/tracing/tooling/update_trace.py
+++ b/tools/tracing/tooling/update_trace.py
@@ -49,6 +49,31 @@ def update_pid_perfetto_trace(trace_dict, start_pid, max_pid):
raise SystemExit("Error: due to out of range for allocating pids")
event['pid'] = new_pid
+def update_trace_file(input_file, time_offset, start_pid=(1<<16), max_pid = (1<<32)):
+ try:
+ with open(input_file, 'r') as f:
+ trace_dict = json.loads(f.read())
+ except Exception as e:
+ print(f'Error: update_trace_file open input file: {input_file} : {e}')
+ return False
+
+ update_timestamp_perfetto_trace(trace_dict, time_offset)
+ update_pid_perfetto_trace(trace_dict, start_pid, max_pid)
+
+ # Save the updated trace data to a new JSON file
+ # add '_updated' to the output filename
+ file_path = os.path.splitext(input_file)
+ output_file = f"{file_path[0]}_updated.json"
+ try:
+ with open(output_file, 'w') as f:
+ json.dump(trace_dict, f)
+ except Exception as e:
+ print(f'Error: update_trace_file open output_file {output_file} : {e}')
+ return False
+
+ print(f"Updated trace data saved to {output_file}")
+ return True
+
def parseArguments():
parser = argparse.ArgumentParser(
description='Update perfetto trace event timestamp with the given offset.')
@@ -68,17 +93,4 @@ def parseArguments():
if __name__ == '__main__':
args = parseArguments();
-
- with open(args.input_file, 'r') as f:
- trace_dict = json.loads(f.read())
-
- update_timestamp_perfetto_trace(trace_dict, args.time_offset)
- update_pid_perfetto_trace(trace_dict, args.start_pid, args.max_pid)
-
- # Save the updated trace data to a new JSON file
- # add '_updated' to the output filename
- file_path = os.path.splitext(args.input_file)
- output_file = f"{file_path[0]}_updated.json"
- with open(output_file, 'w') as f:
- json.dump(trace_dict, f)
- print(f"Updated trace data saved to {output_file}")
+ update_trace_file(args.input_file, args.time_offset, args.start_pid, args.max_pid)