0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define _GNU_SOURCE
0011 #include <limits.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <unistd.h>
0015 #include "trace-agent.h"
0016
0017 #define PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
0018 #define PIPE_DEF_BUFS 16
0019 #define PIPE_MIN_SIZE (PAGE_SIZE*PIPE_DEF_BUFS)
0020 #define PIPE_MAX_SIZE (1024*1024)
0021 #define READ_PATH_FMT \
0022 "/sys/kernel/debug/tracing/per_cpu/cpu%d/trace_pipe_raw"
0023 #define WRITE_PATH_FMT "/dev/virtio-ports/trace-path-cpu%d"
0024 #define CTL_PATH "/dev/virtio-ports/agent-ctl-path"
0025
0026 pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
0027 pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
0028
0029 static int get_total_cpus(void)
0030 {
0031 int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
0032
0033 if (nr_cpus <= 0) {
0034 pr_err("Could not read cpus\n");
0035 goto error;
0036 } else if (nr_cpus > MAX_CPUS) {
0037 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
0038 goto error;
0039 }
0040
0041 return nr_cpus;
0042
0043 error:
0044 exit(EXIT_FAILURE);
0045 }
0046
0047 static void *agent_info_new(void)
0048 {
0049 struct agent_info *s;
0050 int i;
0051
0052 s = zalloc(sizeof(struct agent_info));
0053 if (s == NULL) {
0054 pr_err("agent_info zalloc error\n");
0055 exit(EXIT_FAILURE);
0056 }
0057
0058 s->pipe_size = PIPE_INIT;
0059 s->use_stdout = false;
0060 s->cpus = get_total_cpus();
0061 s->ctl_fd = -1;
0062
0063
0064 for (i = 0; i < s->cpus; i++)
0065 s->rw_ti[i] = rw_thread_info_new();
0066
0067 return s;
0068 }
0069
0070 static unsigned long parse_size(const char *arg)
0071 {
0072 unsigned long value, round;
0073 char *ptr;
0074
0075 value = strtoul(arg, &ptr, 10);
0076 switch (*ptr) {
0077 case 'K': case 'k':
0078 value <<= 10;
0079 break;
0080 case 'M': case 'm':
0081 value <<= 20;
0082 break;
0083 default:
0084 break;
0085 }
0086
0087 if (value > PIPE_MAX_SIZE) {
0088 pr_err("Pipe size must be less than 1MB\n");
0089 goto error;
0090 } else if (value < PIPE_MIN_SIZE) {
0091 pr_err("Pipe size must be over 64KB\n");
0092 goto error;
0093 }
0094
0095
0096 round = value & (PAGE_SIZE - 1);
0097 value = value - round;
0098
0099 return value;
0100 error:
0101 return 0;
0102 }
0103
0104 static void usage(char const *prg)
0105 {
0106 pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
0107 }
0108
0109 static const char *make_path(int cpu_num, bool this_is_write_path)
0110 {
0111 int ret;
0112 char *buf;
0113
0114 buf = zalloc(PATH_MAX);
0115 if (buf == NULL) {
0116 pr_err("Could not allocate buffer\n");
0117 goto error;
0118 }
0119
0120 if (this_is_write_path)
0121
0122 ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
0123 else
0124
0125 ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, cpu_num);
0126
0127 if (ret <= 0) {
0128 pr_err("Failed to generate %s path(CPU#%d):%d\n",
0129 this_is_write_path ? "read" : "write", cpu_num, ret);
0130 goto error;
0131 }
0132
0133 return buf;
0134
0135 error:
0136 free(buf);
0137 return NULL;
0138 }
0139
0140 static const char *make_input_path(int cpu_num)
0141 {
0142 return make_path(cpu_num, false);
0143 }
0144
0145 static const char *make_output_path(int cpu_num)
0146 {
0147 return make_path(cpu_num, true);
0148 }
0149
0150 static void *agent_info_init(struct agent_info *s)
0151 {
0152 int cpu;
0153 const char *in_path = NULL;
0154 const char *out_path = NULL;
0155
0156
0157 for (cpu = 0; cpu < s->cpus; cpu++) {
0158
0159 in_path = make_input_path(cpu);
0160 if (in_path == NULL)
0161 goto error;
0162
0163
0164 if (!s->use_stdout) {
0165 out_path = make_output_path(cpu);
0166 if (out_path == NULL)
0167 goto error;
0168 } else
0169
0170 pr_debug("stdout mode\n");
0171
0172 rw_thread_init(cpu, in_path, out_path, s->use_stdout,
0173 s->pipe_size, s->rw_ti[cpu]);
0174 }
0175
0176
0177 s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
0178
0179 return NULL;
0180
0181 error:
0182 exit(EXIT_FAILURE);
0183 }
0184
0185 static void *parse_args(int argc, char *argv[], struct agent_info *s)
0186 {
0187 int cmd;
0188 unsigned long size;
0189
0190 while ((cmd = getopt(argc, argv, "hos:")) != -1) {
0191 switch (cmd) {
0192
0193 case 'o':
0194 s->use_stdout = true;
0195 break;
0196
0197 case 's':
0198 size = parse_size(optarg);
0199 if (size == 0)
0200 goto error;
0201 s->pipe_size = size;
0202 break;
0203 case 'h':
0204 default:
0205 usage(argv[0]);
0206 goto error;
0207 }
0208 }
0209
0210 agent_info_init(s);
0211
0212 return NULL;
0213
0214 error:
0215 exit(EXIT_FAILURE);
0216 }
0217
0218 static void agent_main_loop(struct agent_info *s)
0219 {
0220 int cpu;
0221 pthread_t rw_thread_per_cpu[MAX_CPUS];
0222
0223
0224 for (cpu = 0; cpu < s->cpus; cpu++)
0225 rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
0226
0227 rw_ctl_loop(s->ctl_fd);
0228
0229
0230 for (cpu = 0; cpu < s->cpus; cpu++) {
0231 int ret;
0232
0233 ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
0234 if (ret != 0) {
0235 pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
0236 exit(EXIT_FAILURE);
0237 }
0238 }
0239 }
0240
0241 static void agent_info_free(struct agent_info *s)
0242 {
0243 int i;
0244
0245 close(s->ctl_fd);
0246 for (i = 0; i < s->cpus; i++) {
0247 close(s->rw_ti[i]->in_fd);
0248 close(s->rw_ti[i]->out_fd);
0249 close(s->rw_ti[i]->read_pipe);
0250 close(s->rw_ti[i]->write_pipe);
0251 free(s->rw_ti[i]);
0252 }
0253 free(s);
0254 }
0255
0256 int main(int argc, char *argv[])
0257 {
0258 struct agent_info *s = NULL;
0259
0260 s = agent_info_new();
0261 parse_args(argc, argv, s);
0262
0263 agent_main_loop(s);
0264
0265 agent_info_free(s);
0266
0267 return 0;
0268 }