Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / virtio / virtio-trace / trace-agent.c
blobcdfe77c2b4c8fa561671b1671a9788d457b973c4
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Guest agent for virtio-trace
5 * Copyright (C) 2012 Hitachi, Ltd.
6 * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
7 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
8 */
10 #define _GNU_SOURCE
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include "trace-agent.h"
17 #define PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
18 #define PIPE_DEF_BUFS 16
19 #define PIPE_MIN_SIZE (PAGE_SIZE*PIPE_DEF_BUFS)
20 #define PIPE_MAX_SIZE (1024*1024)
21 #define READ_PATH_FMT \
22 "/sys/kernel/debug/tracing/per_cpu/cpu%d/trace_pipe_raw"
23 #define WRITE_PATH_FMT "/dev/virtio-ports/trace-path-cpu%d"
24 #define CTL_PATH "/dev/virtio-ports/agent-ctl-path"
26 pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
27 pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
29 static int get_total_cpus(void)
31 int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
33 if (nr_cpus <= 0) {
34 pr_err("Could not read cpus\n");
35 goto error;
36 } else if (nr_cpus > MAX_CPUS) {
37 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
38 goto error;
41 return nr_cpus;
43 error:
44 exit(EXIT_FAILURE);
47 static void *agent_info_new(void)
49 struct agent_info *s;
50 int i;
52 s = zalloc(sizeof(struct agent_info));
53 if (s == NULL) {
54 pr_err("agent_info zalloc error\n");
55 exit(EXIT_FAILURE);
58 s->pipe_size = PIPE_INIT;
59 s->use_stdout = false;
60 s->cpus = get_total_cpus();
61 s->ctl_fd = -1;
63 /* read/write threads init */
64 for (i = 0; i < s->cpus; i++)
65 s->rw_ti[i] = rw_thread_info_new();
67 return s;
70 static unsigned long parse_size(const char *arg)
72 unsigned long value, round;
73 char *ptr;
75 value = strtoul(arg, &ptr, 10);
76 switch (*ptr) {
77 case 'K': case 'k':
78 value <<= 10;
79 break;
80 case 'M': case 'm':
81 value <<= 20;
82 break;
83 default:
84 break;
87 if (value > PIPE_MAX_SIZE) {
88 pr_err("Pipe size must be less than 1MB\n");
89 goto error;
90 } else if (value < PIPE_MIN_SIZE) {
91 pr_err("Pipe size must be over 64KB\n");
92 goto error;
95 /* Align buffer size with page unit */
96 round = value & (PAGE_SIZE - 1);
97 value = value - round;
99 return value;
100 error:
101 return 0;
104 static void usage(char const *prg)
106 pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
109 static const char *make_path(int cpu_num, bool this_is_write_path)
111 int ret;
112 char *buf;
114 buf = zalloc(PATH_MAX);
115 if (buf == NULL) {
116 pr_err("Could not allocate buffer\n");
117 goto error;
120 if (this_is_write_path)
121 /* write(output) path */
122 ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
123 else
124 /* read(input) path */
125 ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, cpu_num);
127 if (ret <= 0) {
128 pr_err("Failed to generate %s path(CPU#%d):%d\n",
129 this_is_write_path ? "read" : "write", cpu_num, ret);
130 goto error;
133 return buf;
135 error:
136 free(buf);
137 return NULL;
140 static const char *make_input_path(int cpu_num)
142 return make_path(cpu_num, false);
145 static const char *make_output_path(int cpu_num)
147 return make_path(cpu_num, true);
150 static void *agent_info_init(struct agent_info *s)
152 int cpu;
153 const char *in_path = NULL;
154 const char *out_path = NULL;
156 /* init read/write threads */
157 for (cpu = 0; cpu < s->cpus; cpu++) {
158 /* set read(input) path per read/write thread */
159 in_path = make_input_path(cpu);
160 if (in_path == NULL)
161 goto error;
163 /* set write(output) path per read/write thread*/
164 if (!s->use_stdout) {
165 out_path = make_output_path(cpu);
166 if (out_path == NULL)
167 goto error;
168 } else
169 /* stdout mode */
170 pr_debug("stdout mode\n");
172 rw_thread_init(cpu, in_path, out_path, s->use_stdout,
173 s->pipe_size, s->rw_ti[cpu]);
176 /* init controller of read/write threads */
177 s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
179 return NULL;
181 error:
182 exit(EXIT_FAILURE);
185 static void *parse_args(int argc, char *argv[], struct agent_info *s)
187 int cmd;
188 unsigned long size;
190 while ((cmd = getopt(argc, argv, "hos:")) != -1) {
191 switch (cmd) {
192 /* stdout mode */
193 case 'o':
194 s->use_stdout = true;
195 break;
196 /* size of pipe */
197 case 's':
198 size = parse_size(optarg);
199 if (size == 0)
200 goto error;
201 s->pipe_size = size;
202 break;
203 case 'h':
204 default:
205 usage(argv[0]);
206 goto error;
210 agent_info_init(s);
212 return NULL;
214 error:
215 exit(EXIT_FAILURE);
218 static void agent_main_loop(struct agent_info *s)
220 int cpu;
221 pthread_t rw_thread_per_cpu[MAX_CPUS];
223 /* Start all read/write threads */
224 for (cpu = 0; cpu < s->cpus; cpu++)
225 rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
227 rw_ctl_loop(s->ctl_fd);
229 /* Finish all read/write threads */
230 for (cpu = 0; cpu < s->cpus; cpu++) {
231 int ret;
233 ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
234 if (ret != 0) {
235 pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
236 exit(EXIT_FAILURE);
241 static void agent_info_free(struct agent_info *s)
243 int i;
245 close(s->ctl_fd);
246 for (i = 0; i < s->cpus; i++) {
247 close(s->rw_ti[i]->in_fd);
248 close(s->rw_ti[i]->out_fd);
249 close(s->rw_ti[i]->read_pipe);
250 close(s->rw_ti[i]->write_pipe);
251 free(s->rw_ti[i]);
253 free(s);
256 int main(int argc, char *argv[])
258 struct agent_info *s = NULL;
260 s = agent_info_new();
261 parse_args(argc, argv, s);
263 agent_main_loop(s);
265 agent_info_free(s);
267 return 0;