xref: /OK3568_Linux_fs/kernel/tools/virtio/virtio-trace/README (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunTrace Agent for virtio-trace
2*4882a593Smuzhiyun============================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunTrace agent is a user tool for sending trace data of a guest to a Host in low
5*4882a593Smuzhiyunoverhead. Trace agent has the following functions:
6*4882a593Smuzhiyun - splice a page of ring-buffer to read_pipe without memory copying
7*4882a593Smuzhiyun - splice the page from write_pipe to virtio-console without memory copying
8*4882a593Smuzhiyun - write trace data to stdout by using -o option
9*4882a593Smuzhiyun - controlled by start/stop orders from a Host
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunThe trace agent operates as follows:
12*4882a593Smuzhiyun 1) Initialize all structures.
13*4882a593Smuzhiyun 2) Create a read/write thread per CPU. Each thread is bound to a CPU.
14*4882a593Smuzhiyun    The read/write threads hold it.
15*4882a593Smuzhiyun 3) A controller thread does poll() for a start order of a host.
16*4882a593Smuzhiyun 4) After the controller of the trace agent receives a start order from a host,
17*4882a593Smuzhiyun    the controller wake read/write threads.
18*4882a593Smuzhiyun 5) The read/write threads start to read trace data from ring-buffers and
19*4882a593Smuzhiyun    write the data to virtio-serial.
20*4882a593Smuzhiyun 6) If the controller receives a stop order from a host, the read/write threads
21*4882a593Smuzhiyun    stop to read trace data.
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunFiles
25*4882a593Smuzhiyun=====
26*4882a593Smuzhiyun
27*4882a593SmuzhiyunREADME: this file
28*4882a593SmuzhiyunMakefile: Makefile of trace agent for virtio-trace
29*4882a593Smuzhiyuntrace-agent.c: includes main function, sets up for operating trace agent
30*4882a593Smuzhiyuntrace-agent.h: includes all structures and some macros
31*4882a593Smuzhiyuntrace-agent-ctl.c: includes controller function for read/write threads
32*4882a593Smuzhiyuntrace-agent-rw.c: includes read/write threads function
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunSetup
36*4882a593Smuzhiyun=====
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunTo use this trace agent for virtio-trace, we need to prepare some virtio-serial
39*4882a593SmuzhiyunI/Fs.
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun1) Make FIFO in a host
42*4882a593Smuzhiyun virtio-trace uses virtio-serial pipe as trace data paths as to the number
43*4882a593Smuzhiyunof CPUs and a control path, so FIFO (named pipe) should be created as follows:
44*4882a593Smuzhiyun	# mkdir /tmp/virtio-trace/
45*4882a593Smuzhiyun	# mkfifo /tmp/virtio-trace/trace-path-cpu{0,1,2,...,X}.{in,out}
46*4882a593Smuzhiyun	# mkfifo /tmp/virtio-trace/agent-ctl-path.{in,out}
47*4882a593Smuzhiyun
48*4882a593SmuzhiyunFor example, if a guest use three CPUs, the names are
49*4882a593Smuzhiyun	trace-path-cpu{0,1,2}.{in.out}
50*4882a593Smuzhiyunand
51*4882a593Smuzhiyun	agent-ctl-path.{in,out}.
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun2) Set up of virtio-serial pipe in a host
54*4882a593Smuzhiyun Add qemu option to use virtio-serial pipe.
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun ##virtio-serial device##
57*4882a593Smuzhiyun     -device virtio-serial-pci,id=virtio-serial0\
58*4882a593Smuzhiyun ##control path##
59*4882a593Smuzhiyun     -chardev pipe,id=charchannel0,path=/tmp/virtio-trace/agent-ctl-path\
60*4882a593Smuzhiyun     -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,\
61*4882a593Smuzhiyun      id=channel0,name=agent-ctl-path\
62*4882a593Smuzhiyun ##data path##
63*4882a593Smuzhiyun     -chardev pipe,id=charchannel1,path=/tmp/virtio-trace/trace-path-cpu0\
64*4882a593Smuzhiyun     -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel0,\
65*4882a593Smuzhiyun      id=channel1,name=trace-path-cpu0\
66*4882a593Smuzhiyun      ...
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunIf you manage guests with libvirt, add the following tags to domain XML files.
69*4882a593SmuzhiyunThen, libvirt passes the same command option to qemu.
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun	<channel type='pipe'>
72*4882a593Smuzhiyun	   <source path='/tmp/virtio-trace/agent-ctl-path'/>
73*4882a593Smuzhiyun	   <target type='virtio' name='agent-ctl-path'/>
74*4882a593Smuzhiyun	   <address type='virtio-serial' controller='0' bus='0' port='0'/>
75*4882a593Smuzhiyun	</channel>
76*4882a593Smuzhiyun	<channel type='pipe'>
77*4882a593Smuzhiyun	   <source path='/tmp/virtio-trace/trace-path-cpu0'/>
78*4882a593Smuzhiyun	   <target type='virtio' name='trace-path-cpu0'/>
79*4882a593Smuzhiyun	   <address type='virtio-serial' controller='0' bus='0' port='1'/>
80*4882a593Smuzhiyun	</channel>
81*4882a593Smuzhiyun	...
82*4882a593SmuzhiyunHere, chardev names are restricted to trace-path-cpuX and agent-ctl-path. For
83*4882a593Smuzhiyunexample, if a guest use three CPUs, chardev names should be trace-path-cpu0,
84*4882a593Smuzhiyuntrace-path-cpu1, trace-path-cpu2, and agent-ctl-path.
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun3) Boot the guest
87*4882a593Smuzhiyun You can find some chardev in /dev/virtio-ports/ in the guest.
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunRun
91*4882a593Smuzhiyun===
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun0) Build trace agent in a guest
94*4882a593Smuzhiyun	$ make
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun1) Enable ftrace in the guest
97*4882a593Smuzhiyun <Example>
98*4882a593Smuzhiyun	# echo 1 > /sys/kernel/debug/tracing/events/sched/enable
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun2) Run trace agent in the guest
101*4882a593Smuzhiyun This agent must be operated as root.
102*4882a593Smuzhiyun	# ./trace-agent
103*4882a593Smuzhiyunread/write threads in the agent wait for start order from host. If you add -o
104*4882a593Smuzhiyunoption, trace data are output via stdout in the guest.
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun3) Open FIFO in a host
107*4882a593Smuzhiyun	# cat /tmp/virtio-trace/trace-path-cpu0.out
108*4882a593SmuzhiyunIf a host does not open these, trace data get stuck in buffers of virtio. Then,
109*4882a593Smuzhiyunthe guest will stop by specification of chardev in QEMU. This blocking mode may
110*4882a593Smuzhiyunbe solved in the future.
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun4) Start to read trace data by ordering from a host
113*4882a593Smuzhiyun A host injects read start order to the guest via virtio-serial.
114*4882a593Smuzhiyun	# echo 1 > /tmp/virtio-trace/agent-ctl-path.in
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun5) Stop to read trace data by ordering from a host
117*4882a593Smuzhiyun A host injects read stop order to the guest via virtio-serial.
118*4882a593Smuzhiyun	# echo 0 > /tmp/virtio-trace/agent-ctl-path.in
119