1 // SPDX-License-Identifier: GPL-2.0-only
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>
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
);
34 pr_err("Could not read cpus\n");
36 } else if (nr_cpus
> MAX_CPUS
) {
37 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS
);
47 static void *agent_info_new(void)
52 s
= zalloc(sizeof(struct agent_info
));
54 pr_err("agent_info zalloc error\n");
58 s
->pipe_size
= PIPE_INIT
;
59 s
->use_stdout
= false;
60 s
->cpus
= get_total_cpus();
63 /* read/write threads init */
64 for (i
= 0; i
< s
->cpus
; i
++)
65 s
->rw_ti
[i
] = rw_thread_info_new();
70 static unsigned long parse_size(const char *arg
)
72 unsigned long value
, round
;
75 value
= strtoul(arg
, &ptr
, 10);
87 if (value
> PIPE_MAX_SIZE
) {
88 pr_err("Pipe size must be less than 1MB\n");
90 } else if (value
< PIPE_MIN_SIZE
) {
91 pr_err("Pipe size must be over 64KB\n");
95 /* Align buffer size with page unit */
96 round
= value
& (PAGE_SIZE
- 1);
97 value
= value
- round
;
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
)
114 buf
= zalloc(PATH_MAX
);
116 pr_err("Could not allocate buffer\n");
120 if (this_is_write_path
)
121 /* write(output) path */
122 ret
= snprintf(buf
, PATH_MAX
, WRITE_PATH_FMT
, cpu_num
);
124 /* read(input) path */
125 ret
= snprintf(buf
, PATH_MAX
, READ_PATH_FMT
, cpu_num
);
128 pr_err("Failed to generate %s path(CPU#%d):%d\n",
129 this_is_write_path
? "read" : "write", cpu_num
, ret
);
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
)
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
);
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
)
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
);
185 static void *parse_args(int argc
, char *argv
[], struct agent_info
*s
)
190 while ((cmd
= getopt(argc
, argv
, "hos:")) != -1) {
194 s
->use_stdout
= true;
198 size
= parse_size(optarg
);
218 static void agent_main_loop(struct agent_info
*s
)
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
++) {
233 ret
= pthread_join(rw_thread_per_cpu
[cpu
], NULL
);
235 pr_err("pthread_join() error:%d (cpu %d)\n", ret
, cpu
);
241 static void agent_info_free(struct agent_info
*s
)
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
);
256 int main(int argc
, char *argv
[])
258 struct agent_info
*s
= NULL
;
260 s
= agent_info_new();
261 parse_args(argc
, argv
, s
);