Linux 3.7.1
[linux/fpc-iii.git] / tools / virtio / virtio-trace / trace-agent.c
blob0a0a7dd4eff7cb12e9300992c2d7823e1ab9ffab
1 /*
2 * Guest agent for virtio-trace
4 * Copyright (C) 2012 Hitachi, Ltd.
5 * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 * Licensed under GPL version 2 only.
12 #define _GNU_SOURCE
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include "trace-agent.h"
19 #define PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
20 #define PIPE_DEF_BUFS 16
21 #define PIPE_MIN_SIZE (PAGE_SIZE*PIPE_DEF_BUFS)
22 #define PIPE_MAX_SIZE (1024*1024)
23 #define READ_PATH_FMT \
24 "/sys/kernel/debug/tracing/per_cpu/cpu%d/trace_pipe_raw"
25 #define WRITE_PATH_FMT "/dev/virtio-ports/trace-path-cpu%d"
26 #define CTL_PATH "/dev/virtio-ports/agent-ctl-path"
28 pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
29 pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
31 static int get_total_cpus(void)
33 int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
35 if (nr_cpus <= 0) {
36 pr_err("Could not read cpus\n");
37 goto error;
38 } else if (nr_cpus > MAX_CPUS) {
39 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
40 goto error;
43 return nr_cpus;
45 error:
46 exit(EXIT_FAILURE);
49 static void *agent_info_new(void)
51 struct agent_info *s;
52 int i;
54 s = zalloc(sizeof(struct agent_info));
55 if (s == NULL) {
56 pr_err("agent_info zalloc error\n");
57 exit(EXIT_FAILURE);
60 s->pipe_size = PIPE_INIT;
61 s->use_stdout = false;
62 s->cpus = get_total_cpus();
63 s->ctl_fd = -1;
65 /* read/write threads init */
66 for (i = 0; i < s->cpus; i++)
67 s->rw_ti[i] = rw_thread_info_new();
69 return s;
72 static unsigned long parse_size(const char *arg)
74 unsigned long value, round;
75 char *ptr;
77 value = strtoul(arg, &ptr, 10);
78 switch (*ptr) {
79 case 'K': case 'k':
80 value <<= 10;
81 break;
82 case 'M': case 'm':
83 value <<= 20;
84 break;
85 default:
86 break;
89 if (value > PIPE_MAX_SIZE) {
90 pr_err("Pipe size must be less than 1MB\n");
91 goto error;
92 } else if (value < PIPE_MIN_SIZE) {
93 pr_err("Pipe size must be over 64KB\n");
94 goto error;
97 /* Align buffer size with page unit */
98 round = value & (PAGE_SIZE - 1);
99 value = value - round;
101 return value;
102 error:
103 return 0;
106 static void usage(char const *prg)
108 pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
111 static const char *make_path(int cpu_num, bool this_is_write_path)
113 int ret;
114 char *buf;
116 buf = zalloc(PATH_MAX);
117 if (buf == NULL) {
118 pr_err("Could not allocate buffer\n");
119 goto error;
122 if (this_is_write_path)
123 /* write(output) path */
124 ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
125 else
126 /* read(input) path */
127 ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, cpu_num);
129 if (ret <= 0) {
130 pr_err("Failed to generate %s path(CPU#%d):%d\n",
131 this_is_write_path ? "read" : "write", cpu_num, ret);
132 goto error;
135 return buf;
137 error:
138 free(buf);
139 return NULL;
142 static const char *make_input_path(int cpu_num)
144 return make_path(cpu_num, false);
147 static const char *make_output_path(int cpu_num)
149 return make_path(cpu_num, true);
152 static void *agent_info_init(struct agent_info *s)
154 int cpu;
155 const char *in_path = NULL;
156 const char *out_path = NULL;
158 /* init read/write threads */
159 for (cpu = 0; cpu < s->cpus; cpu++) {
160 /* set read(input) path per read/write thread */
161 in_path = make_input_path(cpu);
162 if (in_path == NULL)
163 goto error;
165 /* set write(output) path per read/write thread*/
166 if (!s->use_stdout) {
167 out_path = make_output_path(cpu);
168 if (out_path == NULL)
169 goto error;
170 } else
171 /* stdout mode */
172 pr_debug("stdout mode\n");
174 rw_thread_init(cpu, in_path, out_path, s->use_stdout,
175 s->pipe_size, s->rw_ti[cpu]);
178 /* init controller of read/write threads */
179 s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
181 return NULL;
183 error:
184 exit(EXIT_FAILURE);
187 static void *parse_args(int argc, char *argv[], struct agent_info *s)
189 int cmd;
190 unsigned long size;
192 while ((cmd = getopt(argc, argv, "hos:")) != -1) {
193 switch (cmd) {
194 /* stdout mode */
195 case 'o':
196 s->use_stdout = true;
197 break;
198 /* size of pipe */
199 case 's':
200 size = parse_size(optarg);
201 if (size == 0)
202 goto error;
203 s->pipe_size = size;
204 break;
205 case 'h':
206 default:
207 usage(argv[0]);
208 goto error;
212 agent_info_init(s);
214 return NULL;
216 error:
217 exit(EXIT_FAILURE);
220 static void agent_main_loop(struct agent_info *s)
222 int cpu;
223 pthread_t rw_thread_per_cpu[MAX_CPUS];
225 /* Start all read/write threads */
226 for (cpu = 0; cpu < s->cpus; cpu++)
227 rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
229 rw_ctl_loop(s->ctl_fd);
231 /* Finish all read/write threads */
232 for (cpu = 0; cpu < s->cpus; cpu++) {
233 int ret;
235 ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
236 if (ret != 0) {
237 pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
238 exit(EXIT_FAILURE);
243 static void agent_info_free(struct agent_info *s)
245 int i;
247 close(s->ctl_fd);
248 for (i = 0; i < s->cpus; i++) {
249 close(s->rw_ti[i]->in_fd);
250 close(s->rw_ti[i]->out_fd);
251 close(s->rw_ti[i]->read_pipe);
252 close(s->rw_ti[i]->write_pipe);
253 free(s->rw_ti[i]);
255 free(s);
258 int main(int argc, char *argv[])
260 struct agent_info *s = NULL;
262 s = agent_info_new();
263 parse_args(argc, argv, s);
265 agent_main_loop(s);
267 agent_info_free(s);
269 return 0;