summaryrefslogtreecommitdiff
path: root/tools/tracing/tooling/calculate_time_offset.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/tracing/tooling/calculate_time_offset.py')
-rwxr-xr-xtools/tracing/tooling/calculate_time_offset.py89
1 files changed, 48 insertions, 41 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()