1#!/usr/bin/env python3
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This synthetic trace tests the clock-sync logic. It synthesizes a trace with
17# (i) builtin clocks, (ii) custom global clocks, (iii) sequence-scoped clocks.
18# It uses gpu counters because that is a quite simple packet and doesn't have
19# special treatement. We can't use ftrace because ftrace events use nested
20# per-event timestamps and they are assumed to be in the CLOCK_MONOTONIC
21# domains regardless of the TracePacket's timestamp_clock_id.
22
23from os import sys, path
24
25import synth_common
26from synth_common import CLONE_THREAD
27
28# Clock IDs in the range [64, 128) are sequence-scoped. See comments in
29# clock_snapshot.proto.
30CLOCK_MONOTONIC = 3  # Builtin clock, see clock_snapshot.proto.
31CLOCK_BOOTTIME = 6  # Builtin clock, see clock_snapshot.proto.
32GLOBAL_CLK1 = 128
33GLOBAL_CLK2 = 129
34SEQ_CLOCK1 = 64
35
36trace = synth_common.create_trace()
37
38# The default trace clock domain is CLOCK_BOOTTIME.
39trace.add_gpu_counter(ts=1, counter_id=42, value=3)
40
41# Emit a ClockSnapshot that sets BOOTTIME = MONOTONIC + 100.
42trace.add_clock_snapshot(clocks={CLOCK_MONOTONIC: 1, CLOCK_BOOTTIME: 101})
43
44# Emit a counter synced against the global built-in clock CLOCK_MONOTONIC.
45# This should be translated, at import time, to BOOTTIME = 2 + 100 = 102.
46trace.add_gpu_counter(ts=2, clock_id=CLOCK_MONOTONIC, counter_id=42, value=5)
47
48# Use two global custom clocks. We sync them as follows:
49# BOOTTIME = GLOBAL_CLK1 + 1000
50# GLOBAL_CLK1 = GLOBAL_CLK2 + 1
51# Hence, recursively:
52# BOOTTIME = GLOBAL_CLK2 + 1000 + 1
53trace.add_clock_snapshot(clocks={GLOBAL_CLK1: 1, CLOCK_BOOTTIME: 1001})
54trace.add_clock_snapshot(clocks={GLOBAL_CLK1: 2, GLOBAL_CLK2: 1})
55
56# This counter should be translated, at import time, to BOOTTIME = 3 + 1000
57trace.add_gpu_counter(ts=3, clock_id=GLOBAL_CLK1, counter_id=42, value=7)
58
59# This one instead to BOOTTIME = 4 + 1000 + 1 = 1005
60trace.add_gpu_counter(ts=4, clock_id=GLOBAL_CLK2, counter_id=42, value=9)
61
62# Use a sequence-scoped clock on two differents sequences.
63# On seq 2, BOOTTIME = SEQ_CLOCK1 + 2000
64# On seq 3, BOOTTIME = SEQ_CLOCK1 + 3000
65trace.add_clock_snapshot(seq_id=2, clocks={SEQ_CLOCK1: 1, CLOCK_BOOTTIME: 2001})
66trace.add_clock_snapshot(seq_id=3, clocks={SEQ_CLOCK1: 1, CLOCK_BOOTTIME: 3001})
67
68# This counter should be translated @ BOOTTIME : 3000 + 7
69trace.add_gpu_counter(
70    ts=7, clock_id=SEQ_CLOCK1, counter_id=42, value=14, seq_id=3)
71
72# This counter should be translated @ BOOTTIME : 2000 + 6
73trace.add_gpu_counter(
74    ts=6, clock_id=SEQ_CLOCK1, seq_id=2, counter_id=42, value=11)
75
76# Set default clock for sequence 2.
77defaults_packet = trace.add_packet()
78defaults_packet.trusted_packet_sequence_id = 2
79defaults_packet.trace_packet_defaults.timestamp_clock_id = SEQ_CLOCK1
80
81# This counter should be translated @ BOOTTIME : 2000 + 10
82trace.add_gpu_counter(ts=10, seq_id=2, counter_id=42, value=12)
83
84# Manually specified clock_id overrides the default clock.
85trace.add_gpu_counter(
86    ts=2013, clock_id=CLOCK_BOOTTIME, seq_id=2, counter_id=42, value=13)
87
88# Other sequence's default clock isn't changed, so this should be in BOOTTIME.
89trace.add_gpu_counter(ts=3010, counter_id=42, value=15, seq_id=3)
90
91sys.stdout.buffer.write(trace.trace.SerializeToString())
92