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"
31 static int get_total_cpus(
void)
33 int nr_cpus = (
int)sysconf(_SC_NPROCESSORS_CONF);
36 pr_err(
"Could not read cpus\n");
49 static void *agent_info_new(
void)
56 pr_err(
"agent_info zalloc error\n");
62 s->
cpus = get_total_cpus();
66 for (i = 0; i < s->
cpus; i++)
72 static unsigned long parse_size(
const char *
arg)
74 unsigned long value, round;
77 value = strtoul(arg, &ptr, 10);
90 pr_err(
"Pipe size must be less than 1MB\n");
93 pr_err(
"Pipe size must be over 64KB\n");
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)
118 pr_err(
"Could not allocate buffer\n");
122 if (this_is_write_path)
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;
159 for (cpu = 0; cpu < s->
cpus; cpu++) {
161 in_path = make_input_path(cpu);
167 out_path = make_output_path(cpu);
168 if (out_path ==
NULL)
192 while ((cmd = getopt(argc, argv,
"hos:")) != -1) {
200 size = parse_size(optarg);
220 static void agent_main_loop(
struct agent_info *s)
223 pthread_t rw_thread_per_cpu[
MAX_CPUS];
226 for (cpu = 0; cpu < s->
cpus; cpu++)
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[])
262 s = agent_info_new();