~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/virtio/virtio-trace/trace-agent.c

Version: ~ [ linux-5.17-rc1 ] ~ [ linux-5.16.2 ] ~ [ linux-5.15.16 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.93 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.173 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.225 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.262 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.297 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.299 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Guest agent for virtio-trace
  4  *
  5  * Copyright (C) 2012 Hitachi, Ltd.
  6  * Created by Yoshihiro Yunomae <yoshihiro.yunomae.ez@hitachi.com>
  7  *            Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
  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         /* read/write threads init */
 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         /* Align buffer size with page unit */
 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                 /* write(output) path */
122                 ret = snprintf(buf, PATH_MAX, WRITE_PATH_FMT, cpu_num);
123         else
124                 /* read(input) path */
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         /* init read/write threads */
157         for (cpu = 0; cpu < s->cpus; cpu++) {
158                 /* set read(input) path per read/write thread */
159                 in_path = make_input_path(cpu);
160                 if (in_path == NULL)
161                         goto error;
162 
163                 /* set write(output) path per read/write thread*/
164                 if (!s->use_stdout) {
165                         out_path = make_output_path(cpu);
166                         if (out_path == NULL)
167                                 goto error;
168                 } else
169                         /* stdout mode */
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         /* init controller of read/write threads */
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                 /* stdout mode */
193                 case 'o':
194                         s->use_stdout = true;
195                         break;
196                 /* size of pipe */
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         /* Start all read/write threads */
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         /* Finish all read/write threads */
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 }
269 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp