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.
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
);
36 pr_err("Could not read cpus\n");
38 } else if (nr_cpus
> MAX_CPUS
) {
39 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS
);
49 static void *agent_info_new(void)
54 s
= zalloc(sizeof(struct agent_info
));
56 pr_err("agent_info zalloc error\n");
60 s
->pipe_size
= PIPE_INIT
;
61 s
->use_stdout
= false;
62 s
->cpus
= get_total_cpus();
65 /* read/write threads init */
66 for (i
= 0; i
< s
->cpus
; i
++)
67 s
->rw_ti
[i
] = rw_thread_info_new();
72 static unsigned long parse_size(const char *arg
)
74 unsigned long value
, round
;
77 value
= strtoul(arg
, &ptr
, 10);
89 if (value
> PIPE_MAX_SIZE
) {
90 pr_err("Pipe size must be less than 1MB\n");
92 } else if (value
< PIPE_MIN_SIZE
) {
93 pr_err("Pipe size must be over 64KB\n");
97 /* Align buffer size with page unit */
98 round
= value
& (PAGE_SIZE
- 1);
99 value
= value
- round
;
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
)
116 buf
= zalloc(PATH_MAX
);
118 pr_err("Could not allocate buffer\n");
122 if (this_is_write_path
)
123 /* write(output) path */
124 ret
= snprintf(buf
, PATH_MAX
, WRITE_PATH_FMT
, cpu_num
);
126 /* read(input) path */
127 ret
= snprintf(buf
, PATH_MAX
, READ_PATH_FMT
, cpu_num
);
130 pr_err("Failed to generate %s path(CPU#%d):%d\n",
131 this_is_write_path
? "read" : "write", cpu_num
, ret
);
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
)
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
);
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
)
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
);
187 static void *parse_args(int argc
, char *argv
[], struct agent_info
*s
)
192 while ((cmd
= getopt(argc
, argv
, "hos:")) != -1) {
196 s
->use_stdout
= true;
200 size
= parse_size(optarg
);
220 static void agent_main_loop(struct agent_info
*s
)
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
++) {
235 ret
= pthread_join(rw_thread_per_cpu
[cpu
], NULL
);
237 pr_err("pthread_join() error:%d (cpu %d)\n", ret
, cpu
);
243 static void agent_info_free(struct agent_info
*s
)
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
);
258 int main(int argc
, char *argv
[])
260 struct agent_info
*s
= NULL
;
262 s
= agent_info_new();
263 parse_args(argc
, argv
, s
);