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 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
);
35 pr_err("Could not read cpus\n");
37 } else if (nr_cpus
> MAX_CPUS
) {
38 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS
);
48 static void *agent_info_new(void)
53 s
= zalloc(sizeof(struct agent_info
));
55 pr_err("agent_info zalloc error\n");
59 s
->pipe_size
= PIPE_INIT
;
60 s
->use_stdout
= false;
61 s
->cpus
= get_total_cpus();
64 /* read/write threads init */
65 for (i
= 0; i
< s
->cpus
; i
++)
66 s
->rw_ti
[i
] = rw_thread_info_new();
71 static unsigned long parse_size(const char *arg
)
73 unsigned long value
, round
;
76 value
= strtoul(arg
, &ptr
, 10);
88 if (value
> PIPE_MAX_SIZE
) {
89 pr_err("Pipe size must be less than 1MB\n");
91 } else if (value
< PIPE_MIN_SIZE
) {
92 pr_err("Pipe size must be over 64KB\n");
96 /* Align buffer size with page unit */
97 round
= value
& (PAGE_SIZE
- 1);
98 value
= value
- round
;
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
)
115 buf
= zalloc(PATH_MAX
);
117 pr_err("Could not allocate buffer\n");
121 if (this_is_write_path
)
122 /* write(output) path */
123 ret
= snprintf(buf
, PATH_MAX
, WRITE_PATH_FMT
, cpu_num
);
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
);
132 pr_err("Failed to generate %s path(CPU#%d):%d\n",
133 this_is_write_path
? "read" : "write", cpu_num
, ret
);
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
)
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
);
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
)
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
);
189 static void *parse_args(int argc
, char *argv
[], struct agent_info
*s
)
194 while ((cmd
= getopt(argc
, argv
, "hos:")) != -1) {
198 s
->use_stdout
= true;
202 size
= parse_size(optarg
);
222 static void agent_main_loop(struct agent_info
*s
)
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
++) {
237 ret
= pthread_join(rw_thread_per_cpu
[cpu
], NULL
);
239 pr_err("pthread_join() error:%d (cpu %d)\n", ret
, cpu
);
245 static void agent_info_free(struct agent_info
*s
)
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
);
260 int main(int argc
, char *argv
[])
262 struct agent_info
*s
= NULL
;
264 s
= agent_info_new();
265 parse_args(argc
, argv
, s
);