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# See gpu_counter_descriptor.proto 39SECOND = 22 40PIXEL = 26 41 42# Add a counter descriptor for our test counter: 43trace.add_gpu_counter_spec( 44 ts=1, 45 counter_id=42, 46 name="gpu_counter(42)", 47 description="Number of fragments per second", 48 unit_numerators=[PIXEL], 49 unit_denominators=[SECOND]) 50 51# The default trace clock domain is CLOCK_BOOTTIME. 52trace.add_gpu_counter(ts=1, counter_id=42, value=3) 53 54# Emit a ClockSnapshot that sets BOOTTIME = MONOTONIC + 100. 55trace.add_clock_snapshot(clocks={CLOCK_MONOTONIC: 1, CLOCK_BOOTTIME: 101}) 56 57# Emit a counter synced against the global built-in clock CLOCK_MONOTONIC. 58# This should be translated, at import time, to BOOTTIME = 2 + 100 = 102. 59trace.add_gpu_counter(ts=2, clock_id=CLOCK_MONOTONIC, counter_id=42, value=5) 60 61# Use two global custom clocks. We sync them as follows: 62# BOOTTIME = GLOBAL_CLK1 + 1000 63# GLOBAL_CLK1 = GLOBAL_CLK2 + 1 64# Hence, recursively: 65# BOOTTIME = GLOBAL_CLK2 + 1000 + 1 66trace.add_clock_snapshot(clocks={GLOBAL_CLK1: 1, CLOCK_BOOTTIME: 1001}) 67trace.add_clock_snapshot(clocks={GLOBAL_CLK1: 2, GLOBAL_CLK2: 1}) 68 69# This counter should be translated, at import time, to BOOTTIME = 3 + 1000 70trace.add_gpu_counter(ts=3, clock_id=GLOBAL_CLK1, counter_id=42, value=7) 71 72# This one instead to BOOTTIME = 4 + 1000 + 1 = 1005 73trace.add_gpu_counter(ts=4, clock_id=GLOBAL_CLK2, counter_id=42, value=9) 74 75# Use a sequence-scoped clock on two differents sequences. 76# On seq 2, BOOTTIME = SEQ_CLOCK1 + 2000 77# On seq 3, BOOTTIME = SEQ_CLOCK1 + 3000 78trace.add_clock_snapshot(seq_id=2, clocks={SEQ_CLOCK1: 1, CLOCK_BOOTTIME: 2001}) 79trace.add_clock_snapshot(seq_id=3, clocks={SEQ_CLOCK1: 1, CLOCK_BOOTTIME: 3001}) 80 81# This counter should be translated @ BOOTTIME : 3000 + 7 82trace.add_gpu_counter( 83 ts=7, clock_id=SEQ_CLOCK1, counter_id=42, value=14, seq_id=3) 84 85# This counter should be translated @ BOOTTIME : 2000 + 6 86trace.add_gpu_counter( 87 ts=6, clock_id=SEQ_CLOCK1, seq_id=2, counter_id=42, value=11) 88 89# Set default clock for sequence 2. 90defaults_packet = trace.add_packet() 91defaults_packet.trusted_packet_sequence_id = 2 92defaults_packet.trace_packet_defaults.timestamp_clock_id = SEQ_CLOCK1 93 94# This counter should be translated @ BOOTTIME : 2000 + 10 95trace.add_gpu_counter(ts=10, seq_id=2, counter_id=42, value=12) 96 97# Manually specified clock_id overrides the default clock. 98trace.add_gpu_counter( 99 ts=2013, clock_id=CLOCK_BOOTTIME, seq_id=2, counter_id=42, value=13) 100 101# Other sequence's default clock isn't changed, so this should be in BOOTTIME. 102trace.add_gpu_counter(ts=3010, counter_id=42, value=15, seq_id=3) 103 104sys.stdout.buffer.write(trace.trace.SerializeToString()) 105