18c2ecf20Sopenharmony_ciTrace Agent for virtio-trace
28c2ecf20Sopenharmony_ci============================
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ciTrace agent is a user tool for sending trace data of a guest to a Host in low
58c2ecf20Sopenharmony_cioverhead. Trace agent has the following functions:
68c2ecf20Sopenharmony_ci - splice a page of ring-buffer to read_pipe without memory copying
78c2ecf20Sopenharmony_ci - splice the page from write_pipe to virtio-console without memory copying
88c2ecf20Sopenharmony_ci - write trace data to stdout by using -o option
98c2ecf20Sopenharmony_ci - controlled by start/stop orders from a Host
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciThe trace agent operates as follows:
128c2ecf20Sopenharmony_ci 1) Initialize all structures.
138c2ecf20Sopenharmony_ci 2) Create a read/write thread per CPU. Each thread is bound to a CPU.
148c2ecf20Sopenharmony_ci    The read/write threads hold it.
158c2ecf20Sopenharmony_ci 3) A controller thread does poll() for a start order of a host.
168c2ecf20Sopenharmony_ci 4) After the controller of the trace agent receives a start order from a host,
178c2ecf20Sopenharmony_ci    the controller wake read/write threads.
188c2ecf20Sopenharmony_ci 5) The read/write threads start to read trace data from ring-buffers and
198c2ecf20Sopenharmony_ci    write the data to virtio-serial.
208c2ecf20Sopenharmony_ci 6) If the controller receives a stop order from a host, the read/write threads
218c2ecf20Sopenharmony_ci    stop to read trace data.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciFiles
258c2ecf20Sopenharmony_ci=====
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciREADME: this file
288c2ecf20Sopenharmony_ciMakefile: Makefile of trace agent for virtio-trace
298c2ecf20Sopenharmony_citrace-agent.c: includes main function, sets up for operating trace agent
308c2ecf20Sopenharmony_citrace-agent.h: includes all structures and some macros
318c2ecf20Sopenharmony_citrace-agent-ctl.c: includes controller function for read/write threads
328c2ecf20Sopenharmony_citrace-agent-rw.c: includes read/write threads function
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ciSetup
368c2ecf20Sopenharmony_ci=====
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciTo use this trace agent for virtio-trace, we need to prepare some virtio-serial
398c2ecf20Sopenharmony_ciI/Fs.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci1) Make FIFO in a host
428c2ecf20Sopenharmony_ci virtio-trace uses virtio-serial pipe as trace data paths as to the number
438c2ecf20Sopenharmony_ciof CPUs and a control path, so FIFO (named pipe) should be created as follows:
448c2ecf20Sopenharmony_ci	# mkdir /tmp/virtio-trace/
458c2ecf20Sopenharmony_ci	# mkfifo /tmp/virtio-trace/trace-path-cpu{0,1,2,...,X}.{in,out}
468c2ecf20Sopenharmony_ci	# mkfifo /tmp/virtio-trace/agent-ctl-path.{in,out}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciFor example, if a guest use three CPUs, the names are
498c2ecf20Sopenharmony_ci	trace-path-cpu{0,1,2}.{in.out}
508c2ecf20Sopenharmony_ciand
518c2ecf20Sopenharmony_ci	agent-ctl-path.{in,out}.
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci2) Set up of virtio-serial pipe in a host
548c2ecf20Sopenharmony_ci Add qemu option to use virtio-serial pipe.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci ##virtio-serial device##
578c2ecf20Sopenharmony_ci     -device virtio-serial-pci,id=virtio-serial0\
588c2ecf20Sopenharmony_ci ##control path##
598c2ecf20Sopenharmony_ci     -chardev pipe,id=charchannel0,path=/tmp/virtio-trace/agent-ctl-path\
608c2ecf20Sopenharmony_ci     -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,\
618c2ecf20Sopenharmony_ci      id=channel0,name=agent-ctl-path\
628c2ecf20Sopenharmony_ci ##data path##
638c2ecf20Sopenharmony_ci     -chardev pipe,id=charchannel1,path=/tmp/virtio-trace/trace-path-cpu0\
648c2ecf20Sopenharmony_ci     -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel0,\
658c2ecf20Sopenharmony_ci      id=channel1,name=trace-path-cpu0\
668c2ecf20Sopenharmony_ci      ...
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciIf you manage guests with libvirt, add the following tags to domain XML files.
698c2ecf20Sopenharmony_ciThen, libvirt passes the same command option to qemu.
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	<channel type='pipe'>
728c2ecf20Sopenharmony_ci	   <source path='/tmp/virtio-trace/agent-ctl-path'/>
738c2ecf20Sopenharmony_ci	   <target type='virtio' name='agent-ctl-path'/>
748c2ecf20Sopenharmony_ci	   <address type='virtio-serial' controller='0' bus='0' port='0'/>
758c2ecf20Sopenharmony_ci	</channel>
768c2ecf20Sopenharmony_ci	<channel type='pipe'>
778c2ecf20Sopenharmony_ci	   <source path='/tmp/virtio-trace/trace-path-cpu0'/>
788c2ecf20Sopenharmony_ci	   <target type='virtio' name='trace-path-cpu0'/>
798c2ecf20Sopenharmony_ci	   <address type='virtio-serial' controller='0' bus='0' port='1'/>
808c2ecf20Sopenharmony_ci	</channel>
818c2ecf20Sopenharmony_ci	...
828c2ecf20Sopenharmony_ciHere, chardev names are restricted to trace-path-cpuX and agent-ctl-path. For
838c2ecf20Sopenharmony_ciexample, if a guest use three CPUs, chardev names should be trace-path-cpu0,
848c2ecf20Sopenharmony_citrace-path-cpu1, trace-path-cpu2, and agent-ctl-path.
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci3) Boot the guest
878c2ecf20Sopenharmony_ci You can find some chardev in /dev/virtio-ports/ in the guest.
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciRun
918c2ecf20Sopenharmony_ci===
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci0) Build trace agent in a guest
948c2ecf20Sopenharmony_ci	$ make
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci1) Enable ftrace in the guest
978c2ecf20Sopenharmony_ci <Example>
988c2ecf20Sopenharmony_ci	# echo 1 > /sys/kernel/debug/tracing/events/sched/enable
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci2) Run trace agent in the guest
1018c2ecf20Sopenharmony_ci This agent must be operated as root.
1028c2ecf20Sopenharmony_ci	# ./trace-agent
1038c2ecf20Sopenharmony_ciread/write threads in the agent wait for start order from host. If you add -o
1048c2ecf20Sopenharmony_cioption, trace data are output via stdout in the guest.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci3) Open FIFO in a host
1078c2ecf20Sopenharmony_ci	# cat /tmp/virtio-trace/trace-path-cpu0.out
1088c2ecf20Sopenharmony_ciIf a host does not open these, trace data get stuck in buffers of virtio. Then,
1098c2ecf20Sopenharmony_cithe guest will stop by specification of chardev in QEMU. This blocking mode may
1108c2ecf20Sopenharmony_cibe solved in the future.
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci4) Start to read trace data by ordering from a host
1138c2ecf20Sopenharmony_ci A host injects read start order to the guest via virtio-serial.
1148c2ecf20Sopenharmony_ci	# echo 1 > /tmp/virtio-trace/agent-ctl-path.in
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci5) Stop to read trace data by ordering from a host
1178c2ecf20Sopenharmony_ci A host injects read stop order to the guest via virtio-serial.
1188c2ecf20Sopenharmony_ci	# echo 0 > /tmp/virtio-trace/agent-ctl-path.in
119