Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / virtio / virtio-trace / trace-agent.c
blob7e2d9bbf0b843ebf2d243596580e97ee47c588e5
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 TRACEFS "/sys/kernel/tracing"
22 #define DEBUGFS "/sys/kernel/debug/tracing"
23 #define READ_PATH_FMT "%s/per_cpu/cpu%d/trace_pipe_raw"
24 #define WRITE_PATH_FMT "/dev/virtio-ports/trace-path-cpu%d"
25 #define CTL_PATH "/dev/virtio-ports/agent-ctl-path"
27 pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
28 pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
30 static int get_total_cpus(void)
32 int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
34 if (nr_cpus <= 0) {
35 pr_err("Could not read cpus\n");
36 goto error;
37 } else if (nr_cpus > MAX_CPUS) {
38 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
39 goto error;
42 return nr_cpus;
44 error:
45 exit(EXIT_FAILURE);
48 static void *agent_info_new(void)
50 struct agent_info *s;
51 int i;
53 s = zalloc(sizeof(struct agent_info));
54 if (s == NULL) {
55 pr_err("agent_info zalloc error\n");
56 exit(EXIT_FAILURE);
59 s->pipe_size = PIPE_INIT;
60 s->use_stdout = false;
61 s->cpus = get_total_cpus();
62 s->ctl_fd = -1;
64 /* read/write threads init */
65 for (i = 0; i < s->cpus; i++)
66 s->rw_ti[i] = rw_thread_info_new();
68 return s;
71 static unsigned long parse_size(const char *arg)
73 unsigned long value, round;
74 char *ptr;
76 value = strtoul(arg, &ptr, 10);
77 switch (*ptr) {
78 case 'K': case 'k':
79 value <<= 10;
80 break;
81 case 'M': case 'm':
82 value <<= 20;
83 break;
84 default:
85 break;
88 if (value > PIPE_MAX_SIZE) {
89 pr_err("Pipe size must be less than 1MB\n");
90 goto error;
91 } else if (value < PIPE_MIN_SIZE) {
92 pr_err("Pipe size must be over 64KB\n");
93 goto error;
96 /* Align buffer size with page unit */
97 round = value & (PAGE_SIZE - 1);
98 value = value - round;
100 return value;
101 error:
102 return 0;
105 static void usage(char const *prg)
107 pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
110 static const char *make_path(int cpu_num, bool this_is_write_path)
112 int ret;
113 char *buf;
115 buf = zalloc(PATH_MAX);
116 if (buf == NULL) {
117 pr_err("Could not allocate buffer\n");
118 goto error;
121 if (this_is_write_path)
122 /* write(output) path */
123 ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
124 else {
125 /* read(input) path */
126 ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, TRACEFS, cpu_num);
127 if (ret > 0 && access(buf, F_OK) != 0)
128 ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, DEBUGFS, cpu_num);
131 if (ret <= 0) {
132 pr_err("Failed to generate %s path(CPU#%d):%d\n",
133 this_is_write_path ? "read" : "write", cpu_num, ret);
134 goto error;
137 return buf;
139 error:
140 free(buf);
141 return NULL;
144 static const char *make_input_path(int cpu_num)
146 return make_path(cpu_num, false);
149 static const char *make_output_path(int cpu_num)
151 return make_path(cpu_num, true);
154 static void *agent_info_init(struct agent_info *s)
156 int cpu;
157 const char *in_path = NULL;
158 const char *out_path = NULL;
160 /* init read/write threads */
161 for (cpu = 0; cpu < s->cpus; cpu++) {
162 /* set read(input) path per read/write thread */
163 in_path = make_input_path(cpu);
164 if (in_path == NULL)
165 goto error;
167 /* set write(output) path per read/write thread*/
168 if (!s->use_stdout) {
169 out_path = make_output_path(cpu);
170 if (out_path == NULL)
171 goto error;
172 } else
173 /* stdout mode */
174 pr_debug("stdout mode\n");
176 rw_thread_init(cpu, in_path, out_path, s->use_stdout,
177 s->pipe_size, s->rw_ti[cpu]);
180 /* init controller of read/write threads */
181 s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
183 return NULL;
185 error:
186 exit(EXIT_FAILURE);
189 static void *parse_args(int argc, char *argv[], struct agent_info *s)
191 int cmd;
192 unsigned long size;
194 while ((cmd = getopt(argc, argv, "hos:")) != -1) {
195 switch (cmd) {
196 /* stdout mode */
197 case 'o':
198 s->use_stdout = true;
199 break;
200 /* size of pipe */
201 case 's':
202 size = parse_size(optarg);
203 if (size == 0)
204 goto error;
205 s->pipe_size = size;
206 break;
207 case 'h':
208 default:
209 usage(argv[0]);
210 goto error;
214 agent_info_init(s);
216 return NULL;
218 error:
219 exit(EXIT_FAILURE);
222 static void agent_main_loop(struct agent_info *s)
224 int cpu;
225 pthread_t rw_thread_per_cpu[MAX_CPUS];
227 /* Start all read/write threads */
228 for (cpu = 0; cpu < s->cpus; cpu++)
229 rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
231 rw_ctl_loop(s->ctl_fd);
233 /* Finish all read/write threads */
234 for (cpu = 0; cpu < s->cpus; cpu++) {
235 int ret;
237 ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
238 if (ret != 0) {
239 pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
240 exit(EXIT_FAILURE);
245 static void agent_info_free(struct agent_info *s)
247 int i;
249 close(s->ctl_fd);
250 for (i = 0; i < s->cpus; i++) {
251 close(s->rw_ti[i]->in_fd);
252 close(s->rw_ti[i]->out_fd);
253 close(s->rw_ti[i]->read_pipe);
254 close(s->rw_ti[i]->write_pipe);
255 free(s->rw_ti[i]);
257 free(s);
260 int main(int argc, char *argv[])
262 struct agent_info *s = NULL;
264 s = agent_info_new();
265 parse_args(argc, argv, s);
267 agent_main_loop(s);
269 agent_info_free(s);
271 return 0;