Skip to content

Commit 108fc82

Browse files
Yoshihiro YUNOMAErustyrussell
authored andcommitted
tools: Add guest trace agent as a user tool
This patch adds a user tool, "trace agent" for sending trace data of a guest to a Host in low overhead. This agent has the following functions: - splice a page of ring-buffer to read_pipe without memory copying - splice the page from write_pipe to virtio-console without memory copying - write trace data to stdout by using -o option - controlled by start/stop orders from a Host Changes in v2: - Cleanup (change fprintf() to pr_err() and an include guard) Signed-off-by: Yoshihiro YUNOMAE <[email protected]> Acked-by: Amit Shah <[email protected]> Signed-off-by: Rusty Russell <[email protected]>
1 parent 8ca84a5 commit 108fc82

File tree

6 files changed

+806
-0
lines changed

6 files changed

+806
-0
lines changed

tools/virtio/virtio-trace/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CC = gcc
2+
CFLAGS = -O2 -Wall
3+
LFLAG = -lpthread
4+
5+
all: trace-agent
6+
7+
.c.o:
8+
$(CC) $(CFLAGS) $(LFLAG) -c $^ -o $@
9+
10+
trace-agent: trace-agent.o trace-agent-ctl.o trace-agent-rw.o
11+
$(CC) $(CFLAGS) $(LFLAG) -o $@ $^
12+
13+
clean:
14+
rm -f *.o trace-agent

tools/virtio/virtio-trace/README

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
Trace Agent for virtio-trace
2+
============================
3+
4+
Trace agent is a user tool for sending trace data of a guest to a Host in low
5+
overhead. Trace agent has the following functions:
6+
- splice a page of ring-buffer to read_pipe without memory copying
7+
- splice the page from write_pipe to virtio-console without memory copying
8+
- write trace data to stdout by using -o option
9+
- controlled by start/stop orders from a Host
10+
11+
The trace agent operates as follows:
12+
1) Initialize all structures.
13+
2) Create a read/write thread per CPU. Each thread is bound to a CPU.
14+
The read/write threads hold it.
15+
3) A controller thread does poll() for a start order of a host.
16+
4) After the controller of the trace agent receives a start order from a host,
17+
the controller wake read/write threads.
18+
5) The read/write threads start to read trace data from ring-buffers and
19+
write the data to virtio-serial.
20+
6) If the controller receives a stop order from a host, the read/write threads
21+
stop to read trace data.
22+
23+
24+
Files
25+
=====
26+
27+
README: this file
28+
Makefile: Makefile of trace agent for virtio-trace
29+
trace-agent.c: includes main function, sets up for operating trace agent
30+
trace-agent.h: includes all structures and some macros
31+
trace-agent-ctl.c: includes controller function for read/write threads
32+
trace-agent-rw.c: includes read/write threads function
33+
34+
35+
Setup
36+
=====
37+
38+
To use this trace agent for virtio-trace, we need to prepare some virtio-serial
39+
I/Fs.
40+
41+
1) Make FIFO in a host
42+
virtio-trace uses virtio-serial pipe as trace data paths as to the number
43+
of CPUs and a control path, so FIFO (named pipe) should be created as follows:
44+
# mkdir /tmp/virtio-trace/
45+
# mkfifo /tmp/virtio-trace/trace-path-cpu{0,1,2,...,X}.{in,out}
46+
# mkfifo /tmp/virtio-trace/agent-ctl-path.{in,out}
47+
48+
For example, if a guest use three CPUs, the names are
49+
trace-path-cpu{0,1,2}.{in.out}
50+
and
51+
agent-ctl-path.{in,out}.
52+
53+
2) Set up of virtio-serial pipe in a host
54+
Add qemu option to use virtio-serial pipe.
55+
56+
##virtio-serial device##
57+
-device virtio-serial-pci,id=virtio-serial0\
58+
##control path##
59+
-chardev pipe,id=charchannel0,path=/tmp/virtio-trace/agent-ctl-path\
60+
-device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,\
61+
id=channel0,name=agent-ctl-path\
62+
##data path##
63+
-chardev pipe,id=charchannel1,path=/tmp/virtio-trace/trace-path-cpu0\
64+
-device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel0,\
65+
id=channel1,name=trace-path-cpu0\
66+
...
67+
68+
If you manage guests with libvirt, add the following tags to domain XML files.
69+
Then, libvirt passes the same command option to qemu.
70+
71+
<channel type='pipe'>
72+
<source path='/tmp/virtio-trace/agent-ctl-path'/>
73+
<target type='virtio' name='agent-ctl-path'/>
74+
<address type='virtio-serial' controller='0' bus='0' port='0'/>
75+
</channel>
76+
<channel type='pipe'>
77+
<source path='/tmp/virtio-trace/trace-path-cpu0'/>
78+
<target type='virtio' name='trace-path-cpu0'/>
79+
<address type='virtio-serial' controller='0' bus='0' port='1'/>
80+
</channel>
81+
...
82+
Here, chardev names are restricted to trace-path-cpuX and agent-ctl-path. For
83+
example, if a guest use three CPUs, chardev names should be trace-path-cpu0,
84+
trace-path-cpu1, trace-path-cpu2, and agent-ctl-path.
85+
86+
3) Boot the guest
87+
You can find some chardev in /dev/virtio-ports/ in the guest.
88+
89+
90+
Run
91+
===
92+
93+
0) Build trace agent in a guest
94+
$ make
95+
96+
1) Enable ftrace in the guest
97+
<Example>
98+
# echo 1 > /sys/kernel/debug/tracing/events/sched/enable
99+
100+
2) Run trace agent in the guest
101+
This agent must be operated as root.
102+
# ./trace-agent
103+
read/write threads in the agent wait for start order from host. If you add -o
104+
option, trace data are output via stdout in the guest.
105+
106+
3) Open FIFO in a host
107+
# cat /tmp/virtio-trace/trace-path-cpu0.out
108+
If a host does not open these, trace data get stuck in buffers of virtio. Then,
109+
the guest will stop by specification of chardev in QEMU. This blocking mode may
110+
be solved in the future.
111+
112+
4) Start to read trace data by ordering from a host
113+
A host injects read start order to the guest via virtio-serial.
114+
# echo 1 > /tmp/virtio-trace/agent-ctl-path.in
115+
116+
5) Stop to read trace data by ordering from a host
117+
A host injects read stop order to the guest via virtio-serial.
118+
# echo 0 > /tmp/virtio-trace/agent-ctl-path.in
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Controller of read/write threads for virtio-trace
3+
*
4+
* Copyright (C) 2012 Hitachi, Ltd.
5+
* Created by Yoshihiro Yunomae <[email protected]>
6+
* Masami Hiramatsu <[email protected]>
7+
*
8+
* Licensed under GPL version 2 only.
9+
*
10+
*/
11+
12+
#define _GNU_SOURCE
13+
#include <fcntl.h>
14+
#include <poll.h>
15+
#include <signal.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <unistd.h>
19+
#include "trace-agent.h"
20+
21+
#define HOST_MSG_SIZE 256
22+
#define EVENT_WAIT_MSEC 100
23+
24+
static volatile sig_atomic_t global_signal_val;
25+
bool global_sig_receive; /* default false */
26+
bool global_run_operation; /* default false*/
27+
28+
/* Handle SIGTERM/SIGINT/SIGQUIT to exit */
29+
static void signal_handler(int sig)
30+
{
31+
global_signal_val = sig;
32+
}
33+
34+
int rw_ctl_init(const char *ctl_path)
35+
{
36+
int ctl_fd;
37+
38+
ctl_fd = open(ctl_path, O_RDONLY);
39+
if (ctl_fd == -1) {
40+
pr_err("Cannot open ctl_fd\n");
41+
goto error;
42+
}
43+
44+
return ctl_fd;
45+
46+
error:
47+
exit(EXIT_FAILURE);
48+
}
49+
50+
static int wait_order(int ctl_fd)
51+
{
52+
struct pollfd poll_fd;
53+
int ret = 0;
54+
55+
while (!global_sig_receive) {
56+
poll_fd.fd = ctl_fd;
57+
poll_fd.events = POLLIN;
58+
59+
ret = poll(&poll_fd, 1, EVENT_WAIT_MSEC);
60+
61+
if (global_signal_val) {
62+
global_sig_receive = true;
63+
pr_info("Receive interrupt %d\n", global_signal_val);
64+
65+
/* Wakes rw-threads when they are sleeping */
66+
if (!global_run_operation)
67+
pthread_cond_broadcast(&cond_wakeup);
68+
69+
ret = -1;
70+
break;
71+
}
72+
73+
if (ret < 0) {
74+
pr_err("Polling error\n");
75+
goto error;
76+
}
77+
78+
if (ret)
79+
break;
80+
};
81+
82+
return ret;
83+
84+
error:
85+
exit(EXIT_FAILURE);
86+
}
87+
88+
/*
89+
* contol read/write threads by handling global_run_operation
90+
*/
91+
void *rw_ctl_loop(int ctl_fd)
92+
{
93+
ssize_t rlen;
94+
char buf[HOST_MSG_SIZE];
95+
int ret;
96+
97+
/* Setup signal handlers */
98+
signal(SIGTERM, signal_handler);
99+
signal(SIGINT, signal_handler);
100+
signal(SIGQUIT, signal_handler);
101+
102+
while (!global_sig_receive) {
103+
104+
ret = wait_order(ctl_fd);
105+
if (ret < 0)
106+
break;
107+
108+
rlen = read(ctl_fd, buf, sizeof(buf));
109+
if (rlen < 0) {
110+
pr_err("read data error in ctl thread\n");
111+
goto error;
112+
}
113+
114+
if (rlen == 2 && buf[0] == '1') {
115+
/*
116+
* If host writes '1' to a control path,
117+
* this controller wakes all read/write threads.
118+
*/
119+
global_run_operation = true;
120+
pthread_cond_broadcast(&cond_wakeup);
121+
pr_debug("Wake up all read/write threads\n");
122+
} else if (rlen == 2 && buf[0] == '0') {
123+
/*
124+
* If host writes '0' to a control path, read/write
125+
* threads will wait for notification from Host.
126+
*/
127+
global_run_operation = false;
128+
pr_debug("Stop all read/write threads\n");
129+
} else
130+
pr_info("Invalid host notification: %s\n", buf);
131+
}
132+
133+
return NULL;
134+
135+
error:
136+
exit(EXIT_FAILURE);
137+
}

0 commit comments

Comments
 (0)