This source file includes following definitions.
- get_total_cpus
- agent_info_new
- parse_size
- usage
- make_path
- make_input_path
- make_output_path
- agent_info_init
- parse_args
- agent_main_loop
- agent_info_free
- main
1
2
3
4
5
6
7
8
9
10 #define _GNU_SOURCE
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include "trace-agent.h"
16
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"
25
26 pthread_mutex_t mutex_notify = PTHREAD_MUTEX_INITIALIZER;
27 pthread_cond_t cond_wakeup = PTHREAD_COND_INITIALIZER;
28
29 static int get_total_cpus(void)
30 {
31 int nr_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
32
33 if (nr_cpus <= 0) {
34 pr_err("Could not read cpus\n");
35 goto error;
36 } else if (nr_cpus > MAX_CPUS) {
37 pr_err("Exceed max cpus(%d)\n", (int)MAX_CPUS);
38 goto error;
39 }
40
41 return nr_cpus;
42
43 error:
44 exit(EXIT_FAILURE);
45 }
46
47 static void *agent_info_new(void)
48 {
49 struct agent_info *s;
50 int i;
51
52 s = zalloc(sizeof(struct agent_info));
53 if (s == NULL) {
54 pr_err("agent_info zalloc error\n");
55 exit(EXIT_FAILURE);
56 }
57
58 s->pipe_size = PIPE_INIT;
59 s->use_stdout = false;
60 s->cpus = get_total_cpus();
61 s->ctl_fd = -1;
62
63
64 for (i = 0; i < s->cpus; i++)
65 s->rw_ti[i] = rw_thread_info_new();
66
67 return s;
68 }
69
70 static unsigned long parse_size(const char *arg)
71 {
72 unsigned long value, round;
73 char *ptr;
74
75 value = strtoul(arg, &ptr, 10);
76 switch (*ptr) {
77 case 'K': case 'k':
78 value <<= 10;
79 break;
80 case 'M': case 'm':
81 value <<= 20;
82 break;
83 default:
84 break;
85 }
86
87 if (value > PIPE_MAX_SIZE) {
88 pr_err("Pipe size must be less than 1MB\n");
89 goto error;
90 } else if (value < PIPE_MIN_SIZE) {
91 pr_err("Pipe size must be over 64KB\n");
92 goto error;
93 }
94
95
96 round = value & (PAGE_SIZE - 1);
97 value = value - round;
98
99 return value;
100 error:
101 return 0;
102 }
103
104 static void usage(char const *prg)
105 {
106 pr_err("usage: %s [-h] [-o] [-s <size of pipe>]\n", prg);
107 }
108
109 static const char *make_path(int cpu_num, bool this_is_write_path)
110 {
111 int ret;
112 char *buf;
113
114 buf = zalloc(PATH_MAX);
115 if (buf == NULL) {
116 pr_err("Could not allocate buffer\n");
117 goto error;
118 }
119
120 if (this_is_write_path)
121
122 ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
123 else
124
125 ret = snprintf(buf, PATH_MAX, READ_PATH_FMT, cpu_num);
126
127 if (ret <= 0) {
128 pr_err("Failed to generate %s path(CPU#%d):%d\n",
129 this_is_write_path ? "read" : "write", cpu_num, ret);
130 goto error;
131 }
132
133 return buf;
134
135 error:
136 free(buf);
137 return NULL;
138 }
139
140 static const char *make_input_path(int cpu_num)
141 {
142 return make_path(cpu_num, false);
143 }
144
145 static const char *make_output_path(int cpu_num)
146 {
147 return make_path(cpu_num, true);
148 }
149
150 static void *agent_info_init(struct agent_info *s)
151 {
152 int cpu;
153 const char *in_path = NULL;
154 const char *out_path = NULL;
155
156
157 for (cpu = 0; cpu < s->cpus; cpu++) {
158
159 in_path = make_input_path(cpu);
160 if (in_path == NULL)
161 goto error;
162
163
164 if (!s->use_stdout) {
165 out_path = make_output_path(cpu);
166 if (out_path == NULL)
167 goto error;
168 } else
169
170 pr_debug("stdout mode\n");
171
172 rw_thread_init(cpu, in_path, out_path, s->use_stdout,
173 s->pipe_size, s->rw_ti[cpu]);
174 }
175
176
177 s->ctl_fd = rw_ctl_init((const char *)CTL_PATH);
178
179 return NULL;
180
181 error:
182 exit(EXIT_FAILURE);
183 }
184
185 static void *parse_args(int argc, char *argv[], struct agent_info *s)
186 {
187 int cmd;
188 unsigned long size;
189
190 while ((cmd = getopt(argc, argv, "hos:")) != -1) {
191 switch (cmd) {
192
193 case 'o':
194 s->use_stdout = true;
195 break;
196
197 case 's':
198 size = parse_size(optarg);
199 if (size == 0)
200 goto error;
201 s->pipe_size = size;
202 break;
203 case 'h':
204 default:
205 usage(argv[0]);
206 goto error;
207 }
208 }
209
210 agent_info_init(s);
211
212 return NULL;
213
214 error:
215 exit(EXIT_FAILURE);
216 }
217
218 static void agent_main_loop(struct agent_info *s)
219 {
220 int cpu;
221 pthread_t rw_thread_per_cpu[MAX_CPUS];
222
223
224 for (cpu = 0; cpu < s->cpus; cpu++)
225 rw_thread_per_cpu[cpu] = rw_thread_run(s->rw_ti[cpu]);
226
227 rw_ctl_loop(s->ctl_fd);
228
229
230 for (cpu = 0; cpu < s->cpus; cpu++) {
231 int ret;
232
233 ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
234 if (ret != 0) {
235 pr_err("pthread_join() error:%d (cpu %d)\n", ret, cpu);
236 exit(EXIT_FAILURE);
237 }
238 }
239 }
240
241 static void agent_info_free(struct agent_info *s)
242 {
243 int i;
244
245 close(s->ctl_fd);
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);
251 free(s->rw_ti[i]);
252 }
253 free(s);
254 }
255
256 int main(int argc, char *argv[])
257 {
258 struct agent_info *s = NULL;
259
260 s = agent_info_new();
261 parse_args(argc, argv, s);
262
263 agent_main_loop(s);
264
265 agent_info_free(s);
266
267 return 0;
268 }