162306a36Sopenharmony_ciTrace Agent for virtio-trace 262306a36Sopenharmony_ci============================ 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciTrace agent is a user tool for sending trace data of a guest to a Host in low 562306a36Sopenharmony_cioverhead. Trace agent has the following functions: 662306a36Sopenharmony_ci - splice a page of ring-buffer to read_pipe without memory copying 762306a36Sopenharmony_ci - splice the page from write_pipe to virtio-console without memory copying 862306a36Sopenharmony_ci - write trace data to stdout by using -o option 962306a36Sopenharmony_ci - controlled by start/stop orders from a Host 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ciThe trace agent operates as follows: 1262306a36Sopenharmony_ci 1) Initialize all structures. 1362306a36Sopenharmony_ci 2) Create a read/write thread per CPU. Each thread is bound to a CPU. 1462306a36Sopenharmony_ci The read/write threads hold it. 1562306a36Sopenharmony_ci 3) A controller thread does poll() for a start order of a host. 1662306a36Sopenharmony_ci 4) After the controller of the trace agent receives a start order from a host, 1762306a36Sopenharmony_ci the controller wake read/write threads. 1862306a36Sopenharmony_ci 5) The read/write threads start to read trace data from ring-buffers and 1962306a36Sopenharmony_ci write the data to virtio-serial. 2062306a36Sopenharmony_ci 6) If the controller receives a stop order from a host, the read/write threads 2162306a36Sopenharmony_ci stop to read trace data. 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciFiles 2562306a36Sopenharmony_ci===== 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciREADME: this file 2862306a36Sopenharmony_ciMakefile: Makefile of trace agent for virtio-trace 2962306a36Sopenharmony_citrace-agent.c: includes main function, sets up for operating trace agent 3062306a36Sopenharmony_citrace-agent.h: includes all structures and some macros 3162306a36Sopenharmony_citrace-agent-ctl.c: includes controller function for read/write threads 3262306a36Sopenharmony_citrace-agent-rw.c: includes read/write threads function 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ciSetup 3662306a36Sopenharmony_ci===== 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciTo use this trace agent for virtio-trace, we need to prepare some virtio-serial 3962306a36Sopenharmony_ciI/Fs. 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci1) Make FIFO in a host 4262306a36Sopenharmony_ci virtio-trace uses virtio-serial pipe as trace data paths as to the number 4362306a36Sopenharmony_ciof CPUs and a control path, so FIFO (named pipe) should be created as follows: 4462306a36Sopenharmony_ci # mkdir /tmp/virtio-trace/ 4562306a36Sopenharmony_ci # mkfifo /tmp/virtio-trace/trace-path-cpu{0,1,2,...,X}.{in,out} 4662306a36Sopenharmony_ci # mkfifo /tmp/virtio-trace/agent-ctl-path.{in,out} 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ciFor example, if a guest use three CPUs, the names are 4962306a36Sopenharmony_ci trace-path-cpu{0,1,2}.{in.out} 5062306a36Sopenharmony_ciand 5162306a36Sopenharmony_ci agent-ctl-path.{in,out}. 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci2) Set up of virtio-serial pipe in a host 5462306a36Sopenharmony_ci Add qemu option to use virtio-serial pipe. 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci ##virtio-serial device## 5762306a36Sopenharmony_ci -device virtio-serial-pci,id=virtio-serial0\ 5862306a36Sopenharmony_ci ##control path## 5962306a36Sopenharmony_ci -chardev pipe,id=charchannel0,path=/tmp/virtio-trace/agent-ctl-path\ 6062306a36Sopenharmony_ci -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,\ 6162306a36Sopenharmony_ci id=channel0,name=agent-ctl-path\ 6262306a36Sopenharmony_ci ##data path## 6362306a36Sopenharmony_ci -chardev pipe,id=charchannel1,path=/tmp/virtio-trace/trace-path-cpu0\ 6462306a36Sopenharmony_ci -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel1,\ 6562306a36Sopenharmony_ci id=channel1,name=trace-path-cpu0\ 6662306a36Sopenharmony_ci ... 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ciIf you manage guests with libvirt, add the following tags to domain XML files. 6962306a36Sopenharmony_ciThen, libvirt passes the same command option to qemu. 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci <channel type='pipe'> 7262306a36Sopenharmony_ci <source path='/tmp/virtio-trace/agent-ctl-path'/> 7362306a36Sopenharmony_ci <target type='virtio' name='agent-ctl-path'/> 7462306a36Sopenharmony_ci <address type='virtio-serial' controller='0' bus='0' port='0'/> 7562306a36Sopenharmony_ci </channel> 7662306a36Sopenharmony_ci <channel type='pipe'> 7762306a36Sopenharmony_ci <source path='/tmp/virtio-trace/trace-path-cpu0'/> 7862306a36Sopenharmony_ci <target type='virtio' name='trace-path-cpu0'/> 7962306a36Sopenharmony_ci <address type='virtio-serial' controller='0' bus='0' port='1'/> 8062306a36Sopenharmony_ci </channel> 8162306a36Sopenharmony_ci ... 8262306a36Sopenharmony_ciHere, chardev names are restricted to trace-path-cpuX and agent-ctl-path. For 8362306a36Sopenharmony_ciexample, if a guest use three CPUs, chardev names should be trace-path-cpu0, 8462306a36Sopenharmony_citrace-path-cpu1, trace-path-cpu2, and agent-ctl-path. 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci3) Boot the guest 8762306a36Sopenharmony_ci You can find some chardev in /dev/virtio-ports/ in the guest. 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciRun 9162306a36Sopenharmony_ci=== 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci0) Build trace agent in a guest 9462306a36Sopenharmony_ci $ make 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci1) Enable ftrace in the guest 9762306a36Sopenharmony_ci <Example> 9862306a36Sopenharmony_ci # echo 1 > /sys/kernel/tracing/events/sched/enable 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci2) Run trace agent in the guest 10162306a36Sopenharmony_ci This agent must be operated as root. 10262306a36Sopenharmony_ci # ./trace-agent 10362306a36Sopenharmony_ciread/write threads in the agent wait for start order from host. If you add -o 10462306a36Sopenharmony_cioption, trace data are output via stdout in the guest. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci3) Open FIFO in a host 10762306a36Sopenharmony_ci # cat /tmp/virtio-trace/trace-path-cpu0.out 10862306a36Sopenharmony_ciIf a host does not open these, trace data get stuck in buffers of virtio. Then, 10962306a36Sopenharmony_cithe guest will stop by specification of chardev in QEMU. This blocking mode may 11062306a36Sopenharmony_cibe solved in the future. 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci4) Start to read trace data by ordering from a host 11362306a36Sopenharmony_ci A host injects read start order to the guest via virtio-serial. 11462306a36Sopenharmony_ci # echo 1 > /tmp/virtio-trace/agent-ctl-path.in 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci5) Stop to read trace data by ordering from a host 11762306a36Sopenharmony_ci A host injects read stop order to the guest via virtio-serial. 11862306a36Sopenharmony_ci # echo 0 > /tmp/virtio-trace/agent-ctl-path.in 119