Path: blob/master/tools/virtio/virtio-trace/trace-agent.c
26283 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Guest agent for virtio-trace3*4* Copyright (C) 2012 Hitachi, Ltd.5* Created by Yoshihiro Yunomae <[email protected]>6* Masami Hiramatsu <[email protected]>7*/89#define _GNU_SOURCE10#include <limits.h>11#include <stdio.h>12#include <stdlib.h>13#include <unistd.h>14#include "trace-agent.h"1516#define PAGE_SIZE (sysconf(_SC_PAGE_SIZE))17#define PIPE_DEF_BUFS 1618#define PIPE_MIN_SIZE (PAGE_SIZE*PIPE_DEF_BUFS)19#define PIPE_MAX_SIZE (1024*1024)20#define TRACEFS "/sys/kernel/tracing"21#define DEBUGFS "/sys/kernel/debug/tracing"22#define READ_PATH_FMT "%s/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"2526pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;27pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;2829static int get_total_cpus(void)30{31int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);3233if (nr_cpus <= 0) {34pr_err("Could not read cpus\n");35goto error;36} else if (nr_cpus > MAX_CPUS) {37pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);38goto error;39}4041return nr_cpus;4243error:44exit(EXIT_FAILURE);45}4647static void *agent_info_new(void)48{49struct agent_info *s;50int i;5152s = zalloc(sizeof(struct agent_info));53if (s == NULL) {54pr_err("agent_info zalloc error\n");55exit(EXIT_FAILURE);56}5758s->pipe_size = PIPE_INIT;59s->use_stdout = false;60s->cpus = get_total_cpus();61s->ctl_fd = -1;6263/* read/write threads init */64for (i = 0; i < s->cpus; i++)65s->rw_ti[i] = rw_thread_info_new();6667return s;68}6970static unsigned long parse_size(const char *arg)71{72unsigned long value, round;73char *ptr;7475value = strtoul(arg, &ptr, 10);76switch (*ptr) {77case 'K': case 'k':78value <<= 10;79break;80case 'M': case 'm':81value <<= 20;82break;83default:84break;85}8687if (value > PIPE_MAX_SIZE) {88pr_err("Pipe size must be less than 1MB\n");89goto error;90} else if (value < PIPE_MIN_SIZE) {91pr_err("Pipe size must be over 64KB\n");92goto error;93}9495/* Align buffer size with page unit */96round = value & (PAGE_SIZE - 1);97value = value - round;9899return value;100error:101return 0;102}103104static void usage(char const *prg)105{106pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);107}108109static const char *make_path(int cpu_num, bool this_is_write_path)110{111int ret;112char *buf;113114buf = zalloc(PATH_MAX);115if (buf == NULL) {116pr_err("Could not allocate buffer\n");117goto error;118}119120if (this_is_write_path)121/* write(output) path */122ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);123else {124/* read(input) path */125ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, TRACEFS, cpu_num);126if (ret > 0 && access(buf, F_OK) != 0)127ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, DEBUGFS, cpu_num);128}129130if (ret <= 0) {131pr_err("Failed to generate %s path(CPU#%d):%d\n",132this_is_write_path ? "read" : "write", cpu_num, ret);133goto error;134}135136return buf;137138error:139free(buf);140return NULL;141}142143static const char *make_input_path(int cpu_num)144{145return make_path(cpu_num, false);146}147148static const char *make_output_path(int cpu_num)149{150return make_path(cpu_num, true);151}152153static void *agent_info_init(struct agent_info *s)154{155int cpu;156const char *in_path = NULL;157const char *out_path = NULL;158159/* init read/write threads */160for (cpu = 0; cpu < s->cpus; cpu++) {161/* set read(input) path per read/write thread */162in_path = make_input_path(cpu);163if (in_path == NULL)164goto error;165166/* set write(output) path per read/write thread*/167if (!s->use_stdout) {168out_path = make_output_path(cpu);169if (out_path == NULL)170goto error;171} else172/* stdout mode */173pr_debug("stdout mode\n");174175rw_thread_init(cpu, in_path, out_path, s->use_stdout,176s->pipe_size, s->rw_ti[cpu]);177}178179/* init controller of read/write threads */180s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);181182return NULL;183184error:185exit(EXIT_FAILURE);186}187188static void *parse_args(int argc, char *argv[], struct agent_info *s)189{190int cmd;191unsigned long size;192193while ((cmd = getopt(argc, argv, "hos:")) != -1) {194switch (cmd) {195/* stdout mode */196case 'o':197s->use_stdout = true;198break;199/* size of pipe */200case 's':201size = parse_size(optarg);202if (size == 0)203goto error;204s->pipe_size = size;205break;206case 'h':207default:208usage(argv[0]);209goto error;210}211}212213agent_info_init(s);214215return NULL;216217error:218exit(EXIT_FAILURE);219}220221static void agent_main_loop(struct agent_info *s)222{223int cpu;224pthread_t rw_thread_per_cpu[MAX_CPUS];225226/* Start all read/write threads */227for (cpu = 0; cpu < s->cpus; cpu++)228rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);229230rw_ctl_loop(s->ctl_fd);231232/* Finish all read/write threads */233for (cpu = 0; cpu < s->cpus; cpu++) {234int ret;235236ret = pthread_join(rw_thread_per_cpu[cpu], NULL);237if (ret != 0) {238pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);239exit(EXIT_FAILURE);240}241}242}243244static void agent_info_free(struct agent_info *s)245{246int i;247248close(s->ctl_fd);249for (i = 0; i < s->cpus; i++) {250close(s->rw_ti[i]->in_fd);251close(s->rw_ti[i]->out_fd);252close(s->rw_ti[i]->read_pipe);253close(s->rw_ti[i]->write_pipe);254free(s->rw_ti[i]);255}256free(s);257}258259int main(int argc, char *argv[])260{261struct agent_info *s = NULL;262263s = agent_info_new();264parse_args(argc, argv, s);265266agent_main_loop(s);267268agent_info_free(s);269270return 0;271}272273274