1# VCPU process state time analysis tool 2 3This tool helps you analyze what virtio device is likely to be the bottleneck of the given workload 4from an output of `trace-cmd report`. This tool analyzes the running time, waiting time, and 5preemption time of crosvm's vcpu processes. The running time is the total time crosvm's vcpu 6processes run on the host processors, the waiting time is the total time crosvm's vcpu processes 7wait for other processes, and the preemption time is the total time crosvm's vcpu processes were 8preempted by other processes. 9 10In practice, it means... if the vcpu processes are mostly waiting for the vitio-blk process, the 11bottleneck is likely to be the block device. If the vcpu processes are frequently preempted by other 12processes, the bottleneck is likely to be the processor performance. 13 14 15 16This mechanism is far from perfect due to the defects outlined in the next section. However, it 17gives you a gut feeling of the bottleneck virtio-device. It also estimates the time savings of your 18workload you can achieve by improving the performance of a virtio device. 19 20## Defect 21 22This tool underestimates the time vcpu processes are waiting for a virtio device. This is due to two 23major reasons; timer/periodic interrupts and soft-irq. 24 25This tool calculates the time a virtio device process blocks vcpu processes by accumulating how long 26a vcpu process was sleeping when a virtio device wakes up the vcpu process. Thus, if a periodic 27interrupt waked up a vcpu process in the middle of the VCPU's sleep, the block time is 28underestimated. Similarly, if a device interrupts is handled by some running vcpu and then delivered 29to other sleeping vcpu via soft-irq inside the guest, the block time is also underestimated. 30 31On the other hand, this tool can estimate the time savings of your workload by improving the 32performance of a virtio device. If we reduce block / preempt time of some virtio-process, that will 33always reduce the total time. 34 35 36 37## How to run 38 39### 1. Make a `trace.dat` file by `trace-cmd record` for your workload. 40 41This tool requires `sched:*` and `task:task_rename` events from `trace-cmd`. 42 43#### When you run crosvm manually 44 45To make a `trace.dat` file, you can use `trace-cmd record`. 46 47```console 48$ trace-cmd record -e sched:* -e task:task_rename -o trace.dat crosvm run ... 49``` 50 51This will generate `trace.dat` in the current directory. 52 53#### When you run ChromeOS Tast tests 54 55Some ChromeOS Tast tests such as `arc.RegularBoot.boot` have built-in `trace-cmd record` feature. 56 57To get a trace file from `arc.RegularBoot.vm`, you can use `tast run` with 58`arc.RegularBoot.traceCmdEvents`. Then you can get `0-trace.dat` in your result directory. 59 60```console 61$ tast run -var arc.RegularBoot.traceCmdEvents="task:task_rename,sched:*" $DUT arc.RegularBoot.vm 62``` 63 64Refer to the source code of your Tast test to see if it supports `traceCmdEvents`. 65 66### 2. Generate the text data via `trace-cmd report` 67 68This tool's input is currently the text data generated by `trace-cmd report`, not `trace.dat` 69itself. 70 71```console 72$ trace-cmd report trace.dat > trace.txt 73``` 74 75### 3. Run this tool. 76 77```console 78$ cargo run --release -- -i trace.txt 79``` 80 81## How to read the result 82 83The output of this tool looks like this. This is taken from a trace of `RegularBoot.vm` on a Brya 84with some modification. The description of each metric is added in the comments inside parentheses. 85 86From the below, you can read that the majority of VCPU states are sleep, so the workload is not so 87processor-heavy on this device, and virtio-blk is the major waker of the VCPU, thus it has an 88opportunity to be improved to make the workload faster. 89 90``` 91(time in seconds by VCPU process state for each VCPU) 92vcpu0_running 3.2823 sec (ratio: 27.913% of vcpu0_time) 93vcpu0_sleep 8.1406 sec (ratio: 69.229% of vcpu0_time) 94vcpu0_runnable 0.2446 sec (ratio: 2.080% of vcpu0_time) 95vcpu0_preempted 0.0914 sec (ratio: 0.778% of vcpu0_time) 96---snip--- 97(That of the total VCPUs instead of each VCPU) 98total_vcpu_running 23.3277 sec (ratio: 25.081% of total_vcpu_time) 99total_vcpu_sleep 67.7202 sec (ratio: 72.810% of total_vcpu_time) 100total_vcpu_runnable 1.3776 sec (ratio: 1.481% of total_vcpu_time) 101total_vcpu_preempted 0.5844 sec (ratio: 0.628% of total_vcpu_time) 102total_vcpu_time 93.0099 sec 103---snip--- 104(Total time VCPU processes were waiting for other processes) 105blocked_virtio_blk 6.2771 sec (ratio: 6.749% of total_vcpu_time) 106---snip--- 107blocked_v_fs:_data:1 1.7611 sec (ratio: 1.893% of total_vcpu_time) 108blocked_v_balloon 1.3707 sec (ratio: 1.474% of total_vcpu_time) 109blocked_v_console 0.6851 sec (ratio: 0.737% of total_vcpu_time) 110---snip--- 111(Total time VCPU processes were preempted by other processes) 112(*This is taken from another Octopus data since Brya had little preemption) 113preempted_v_fs:_data:1 2.2760 sec (ratio: 3.179% of total_vcpu_time) 114preempted_chrome 1.1027 sec (ratio: 1.538% of total_vcpu_time) 115---snip--- 116``` 117 118From the result above, you can see that virtio-blk is one of the bottleneck of this workload; it 119blocks vcpu processes for around 6.3 seconds and preempts 1.6 second. 120