1#!/usr/bin/python 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 is designed to test the process tracking logic. It 17# synthesizes a combination of sched_switch, task_newtask, and process tree 18# packets (i.e. the result of the userspace /proc/pid scraper). 19 20from os import sys, path 21sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) 22from synth_common import CLONE_THREAD 23import synth_common 24 25trace = synth_common.create_trace() 26 27# Create the first process (pid=10) with three threads(tids=10,11,12). 28# In this synthetic trace we will pretend we never saw the start of this threads 29# (i.e. the task_newtask event). Process association for these threads will 30# leverage only the /proc/pid scraper. 31trace.add_ftrace_packet(0) 32trace.add_sched(ts=1, prev_pid=0, next_pid=10, next_comm='p1-t0') 33trace.add_sched( 34 ts=2, prev_pid=10, next_pid=11, prev_comm='p1-t0', next_comm='p1-t1') 35trace.add_sched( 36 ts=3, prev_pid=11, next_pid=12, prev_comm='p1-t1', next_comm='p1-t2') 37trace.add_sched(ts=4, prev_pid=12, next_pid=0, prev_comm='p1-t2') 38 39# In the synthetic /proc/pic scraper packet, we pretend we missed p1-1. At the 40# SQL level we should be able to tell that p1-t0 and p1-t2 belong to 'process1' 41# but p1-t1 should be left unjoinable. 42trace.add_packet(ts=5) 43trace.add_process(10, 0, "process1", 1001) 44trace.add_thread(12, 10, "p1-t2") 45 46# Now create another process (pid=20) with three threads(tids=20,21,22). 47# This will be slightly more complicated. We'll pretend that tids=20,21 were 48# created before the start of the trace, and 22 is created only later. 49trace.add_ftrace_packet(0) 50trace.add_sched(ts=10, prev_pid=0, next_pid=20, next_comm='p2-t0') 51trace.add_sched( 52 ts=11, prev_pid=20, next_pid=21, prev_comm='p2-t0', next_comm='p2-t1') 53trace.add_newtask( 54 ts=12, tid=21, new_tid=22, new_comm='p2-t2', flags=CLONE_THREAD) 55trace.add_sched( 56 ts=13, prev_pid=21, next_pid=22, prev_comm='p2-t1', next_comm='p2-t2') 57trace.add_sched(ts=14, prev_pid=22, next_pid=0, prev_comm='p2-t2') 58 59# From the process tracker viewpoint we pretend we only scraped tids=20,21. 60trace.add_packet(ts=15) 61trace.add_process(20, 0, "process_2", 1002) 62trace.add_thread(21, 20, "p2-t1") 63 64# Finally the very complex case: a third process (pid=30) which spawns threads 65# in the following order (notation: A->B means thread A spawns thread B). 66# 31->32, 31->33, 32->34. 67# Last but not least we spawn a further process (pid=40) which spawns a thread 68# which, unluckily, recycles TID=34. We expect a new UTID for TID=34 then. 69trace.add_ftrace_packet(0) 70trace.add_sched(ts=20, prev_pid=0, next_pid=30, next_comm='p3-t0') 71trace.add_sched( 72 ts=21, prev_pid=30, next_pid=31, prev_comm='p3-t0', next_comm='p3-t1') 73trace.add_newtask( 74 ts=22, tid=31, new_tid=32, new_comm='p3-t2', flags=CLONE_THREAD) 75trace.add_newtask( 76 ts=23, tid=31, new_tid=33, new_comm='p3-t3', flags=CLONE_THREAD) 77trace.add_sched( 78 ts=24, prev_pid=31, next_pid=32, prev_comm='p3-t1', next_comm='p3-t2') 79trace.add_newtask( 80 ts=25, tid=32, new_tid=34, new_comm='p3-t4', flags=CLONE_THREAD) 81trace.add_packet(ts=26) 82trace.add_process(30, 0, "process_3") 83trace.add_thread(31, 30, "p3-t1") 84 85# This event pretends that TID=32 forks() a new process 40 (note the absence of 86# CLONE_THREAD in the add_newtask flags). 87trace.add_ftrace_packet(0) 88trace.add_newtask(ts=27, tid=32, new_tid=40, new_comm='p4-t0', flags=0) 89trace.add_sched( 90 ts=28, prev_pid=32, next_pid=40, prev_comm='p3-t2', next_comm='p4-t0') 91 92trace.add_packet(ts=29) 93trace.add_process(40, 0, "process_4") 94 95# And now, this new process starts a new thread that recycles TID=31 (previously 96# used as p3-t1, now becomes p4-t1). 97trace.add_ftrace_packet(0) 98trace.add_newtask( 99 ts=30, tid=40, new_tid=31, new_comm='p4-t1', flags=CLONE_THREAD) 100trace.add_sched( 101 ts=31, prev_pid=40, next_pid=31, prev_comm='p4-t0', next_comm='p4-t1') 102 103print(trace.trace.SerializeToString()) 104