1#!/usr/bin/env python3
2# Copyright (C) 2021 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 is intended to test the handling of simple_watcher style mojo events,
17# which are often missing trace events below them and so are all aggregated
18# together despite them coming into different mojo interfaces.
19
20from os import sys
21
22import synth_common
23
24from synth_common import ms_to_ns
25trace = synth_common.create_trace()
26
27process_track1 = 1234
28
29trace.add_process_track_descriptor(process_track1, pid=0)
30
31process_pid1 = 2345
32
33thread_track1 = 1235
34
35# Main threads have the same ID as the process
36thread_tid1 = process_pid1
37
38seq1 = 9876
39
40thread1_counter = 60
41
42scroll_begin_trace_id = 34576
43trace_id1 = scroll_begin_trace_id + 1
44trace_id2 = trace_id1 + 1
45trace_id3 = trace_id2 + 1
46scroll_end_trace_id = trace_id3 + 1
47
48gesture_scroll_id = 87654
49
50flow_id1 = 45678
51flow_id2 = 45679
52flow_id3 = 45680
53
54trace.add_input_latency_event_slice(
55    "GestureScrollBegin",
56    ts=ms_to_ns(0),
57    dur=ms_to_ns(1),
58    track=scroll_begin_trace_id,
59    trace_id=scroll_begin_trace_id,
60    gesture_scroll_id=gesture_scroll_id)
61
62trace.add_chrome_process_track_descriptor(process_track1, process_pid1)
63
64trace.add_chrome_thread_with_cpu_counter(
65    process_track1,
66    thread_track1,
67    trusted_packet_sequence_id=seq1,
68    counter_track=thread1_counter,
69    pid=process_pid1,
70    tid=thread_tid1,
71    thread_type=trace.prototypes.ThreadDescriptor.ChromeThreadType
72    .CHROME_THREAD_MAIN)
73
74# Scroll update 1 - not janky
75trace.add_input_latency_event_slice(
76    "GestureScrollUpdate",
77    ts=ms_to_ns(0),
78    dur=ms_to_ns(10),
79    track=trace_id1,
80    trace_id=trace_id1,
81    gesture_scroll_id=gesture_scroll_id,
82    is_coalesced=0)
83
84trace.add_latency_info_flow(
85    ts=ms_to_ns(0),
86    dur=ms_to_ns(1),
87    trusted_sequence_id=seq1,
88    trace_id=trace_id1,
89    flow_ids=[flow_id1])
90
91# The slices below will block this "not janky" scroll update 1.
92trace.add_track_event_slice(
93    "task", ts=ms_to_ns(2), dur=ms_to_ns(6), trusted_sequence_id=seq1)
94
95trace.add_track_event_slice(
96    "subtask", ts=ms_to_ns(3), dur=ms_to_ns(4), trusted_sequence_id=seq1)
97# This ends the blocking slices of "not janky" scroll update 1.
98
99trace.add_latency_info_flow(
100    ts=ms_to_ns(11),
101    dur=ms_to_ns(1),
102    trusted_sequence_id=seq1,
103    trace_id=trace_id1,
104    terminating_flow_ids=[flow_id1])
105
106# Scroll update 2 - janky
107trace.add_input_latency_event_slice(
108    "GestureScrollUpdate",
109    ts=ms_to_ns(16),
110    dur=ms_to_ns(33),
111    track=trace_id2,
112    trace_id=trace_id2,
113    gesture_scroll_id=gesture_scroll_id,
114    is_coalesced=0)
115
116# This is a special event that adds a track event that stretches the entire
117# trace. This should not impact our metric (our metric should ignore it).
118# See b/184134310 for details of why we are testing this.
119trace.add_track_event_slice_begin(
120    "ThreadController active", ts=ms_to_ns(16), trusted_sequence_id=seq1)
121
122trace.add_latency_info_flow(
123    ts=ms_to_ns(16),
124    dur=ms_to_ns(1),
125    trusted_sequence_id=seq1,
126    trace_id=trace_id2,
127    flow_ids=[flow_id2])
128
129# The slices below will block this "janky" scroll update 2.
130trace.add_track_event_slice(
131    "task", ts=ms_to_ns(18), dur=ms_to_ns(29), trusted_sequence_id=seq1)
132
133trace.add_track_event_slice(
134    "subtask", ts=ms_to_ns(19), dur=ms_to_ns(27), trusted_sequence_id=seq1)
135# This ends the blocking slices of "janky" scroll update 2.
136
137trace.add_latency_info_flow(
138    ts=ms_to_ns(50),
139    dur=ms_to_ns(1),
140    trusted_sequence_id=seq1,
141    trace_id=trace_id2,
142    terminating_flow_ids=[flow_id2])
143
144# Scroll update 3 - janky
145trace.add_input_latency_event_slice(
146    "GestureScrollUpdate",
147    ts=ms_to_ns(55),
148    dur=ms_to_ns(33),
149    track=trace_id3,
150    trace_id=trace_id3,
151    gesture_scroll_id=gesture_scroll_id,
152    is_coalesced=0)
153
154trace.add_latency_info_flow(
155    ts=ms_to_ns(55),
156    dur=ms_to_ns(1),
157    trusted_sequence_id=seq1,
158    trace_id=trace_id3,
159    flow_ids=[flow_id3])
160
161# The slices below will block this "janky" scroll update 3.
162trace.add_track_event_slice(
163    "task", ts=ms_to_ns(57), dur=ms_to_ns(29), trusted_sequence_id=seq1)
164
165packet = trace.add_track_event_slice(
166    "subtask", ts=ms_to_ns(58), dur=ms_to_ns(27), trusted_sequence_id=seq1)
167
168packet.track_event.chrome_mojo_event_info.watcher_notify_interface_tag = "foo"
169# This ends the blocking slices of "janky" scroll update 3.
170
171trace.add_latency_info_flow(
172    ts=ms_to_ns(89),
173    dur=ms_to_ns(1),
174    trusted_sequence_id=seq1,
175    trace_id=trace_id3,
176    terminating_flow_ids=[flow_id3])
177
178trace.add_input_latency_event_slice(
179    "GestureScrollEnd",
180    ts=ms_to_ns(90),
181    dur=ms_to_ns(2),
182    track=scroll_end_trace_id,
183    trace_id=scroll_end_trace_id,
184    gesture_scroll_id=gesture_scroll_id)
185
186# This ends the "ThreadController active" event.
187trace.add_track_event_slice_end(ts=ms_to_ns(94), trusted_sequence_id=seq1)
188
189sys.stdout.buffer.write(trace.trace.SerializeToString())
190