ia64/xen-unstable

view tools/xentrace/xentrace.c @ 15263:6c636bd3f874

xentrace: Bit-field boolean must be unsigned.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Mon Jun 04 14:24:46 2007 +0100 (2007-06-04)
parents 0a9da94c904a
children 89d2192942be
line source
1 /******************************************************************************
2 * tools/xentrace/xentrace.c
3 *
4 * Tool for collecting trace buffer data from Xen.
5 *
6 * Copyright (C) 2004 by Intel Research Cambridge
7 *
8 * Author: Mark Williamson, mark.a.williamson@intel.com
9 * Date: February 2004
10 */
12 #include <time.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <argp.h>
22 #include <signal.h>
23 #include <inttypes.h>
24 #include <string.h>
26 #include <xen/xen.h>
27 #include <xen/trace.h>
29 #include <xenctrl.h>
31 #define PERROR(_m, _a...) \
32 do { \
33 int __saved_errno = errno; \
34 fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a , \
35 __saved_errno, strerror(__saved_errno)); \
36 errno = __saved_errno; \
37 } while (0)
39 extern FILE *stderr;
41 /***** Compile time configuration of defaults ********************************/
43 /* when we've got more records than this waiting, we log it to the output */
44 #define NEW_DATA_THRESH 1
46 /* sleep for this long (milliseconds) between checking the trace buffers */
47 #define POLL_SLEEP_MILLIS 100
49 #define DEFAULT_TBUF_SIZE 20
50 /***** The code **************************************************************/
52 typedef struct settings_st {
53 char *outfile;
54 struct timespec poll_sleep;
55 unsigned long new_data_thresh;
56 uint32_t evt_mask;
57 uint32_t cpu_mask;
58 unsigned long tbuf_size;
59 uint8_t discard:1;
60 } settings_t;
62 settings_t opts;
64 int interrupted = 0; /* gets set if we get a SIGHUP */
66 void close_handler(int signal)
67 {
68 interrupted = 1;
69 }
71 /**
72 * millis_to_timespec - convert a time in milliseconds to a struct timespec
73 * @millis: time interval in milliseconds
74 */
75 struct timespec millis_to_timespec(unsigned long millis)
76 {
77 struct timespec spec;
79 spec.tv_sec = millis / 1000;
80 spec.tv_nsec = (millis % 1000) * 1000;
82 return spec;
83 }
85 /**
86 * write_rec - output a trace record in binary format
87 * @cpu - source buffer CPU ID
88 * @rec - trace record to output
89 * @out - output stream
90 *
91 * Outputs the trace record to a filestream, prepending the CPU ID of the
92 * source trace buffer.
93 */
94 void write_rec(unsigned int cpu, struct t_rec *rec, FILE *out)
95 {
96 size_t written = 0;
97 written += fwrite(&cpu, sizeof(cpu), 1, out);
98 written += fwrite(rec, sizeof(*rec), 1, out);
99 if ( written != 2 )
100 {
101 PERROR("Failed to write trace record");
102 exit(EXIT_FAILURE);
103 }
104 }
106 static void get_tbufs(unsigned long *mfn, unsigned long *size)
107 {
108 int xc_handle = xc_interface_open();
109 int ret;
111 if ( xc_handle < 0 )
112 {
113 exit(EXIT_FAILURE);
114 }
116 if(!opts.tbuf_size)
117 opts.tbuf_size = DEFAULT_TBUF_SIZE;
119 ret = xc_tbuf_enable(xc_handle, opts.tbuf_size, mfn, size);
121 if ( ret != 0 )
122 {
123 perror("Couldn't enable trace buffers");
124 exit(1);
125 }
127 xc_interface_close(xc_handle);
128 }
130 /**
131 * map_tbufs - memory map Xen trace buffers into user space
132 * @tbufs_mfn: mfn of the trace buffers
133 * @num: number of trace buffers to map
134 * @size: size of each trace buffer
135 *
136 * Maps the Xen trace buffers them into process address space.
137 */
138 struct t_buf *map_tbufs(unsigned long tbufs_mfn, unsigned int num,
139 unsigned long size)
140 {
141 int xc_handle;
142 struct t_buf *tbufs_mapped;
144 xc_handle = xc_interface_open();
146 if ( xc_handle < 0 )
147 {
148 exit(EXIT_FAILURE);
149 }
151 tbufs_mapped = xc_map_foreign_range(xc_handle, DOMID_XEN,
152 size * num, PROT_READ | PROT_WRITE,
153 tbufs_mfn);
155 xc_interface_close(xc_handle);
157 if ( tbufs_mapped == 0 )
158 {
159 PERROR("Failed to mmap trace buffers");
160 exit(EXIT_FAILURE);
161 }
163 return tbufs_mapped;
164 }
166 /**
167 * set_mask - set the cpu/event mask in HV
168 * @mask: the new mask
169 * @type: the new mask type,0-event mask, 1-cpu mask
170 *
171 */
172 void set_mask(uint32_t mask, int type)
173 {
174 int ret = 0;
175 int xc_handle = xc_interface_open(); /* for accessing control interface */
177 if (type == 1) {
178 ret = xc_tbuf_set_cpu_mask(xc_handle, mask);
179 fprintf(stderr, "change cpumask to 0x%x\n", mask);
180 } else if (type == 0) {
181 ret = xc_tbuf_set_evt_mask(xc_handle, mask);
182 fprintf(stderr, "change evtmask to 0x%x\n", mask);
183 }
185 xc_interface_close(xc_handle);
187 if ( ret != 0 )
188 {
189 PERROR("Failure to get trace buffer pointer from Xen and set the new mask");
190 exit(EXIT_FAILURE);
191 }
192 }
194 /**
195 * init_bufs_ptrs - initialises an array of pointers to the trace buffers
196 * @bufs_mapped: the userspace address where the trace buffers are mapped
197 * @num: number of trace buffers
198 * @size: trace buffer size
199 *
200 * Initialises an array of pointers to individual trace buffers within the
201 * mapped region containing all trace buffers.
202 */
203 struct t_buf **init_bufs_ptrs(void *bufs_mapped, unsigned int num,
204 unsigned long size)
205 {
206 int i;
207 struct t_buf **user_ptrs;
209 user_ptrs = (struct t_buf **)calloc(num, sizeof(struct t_buf *));
210 if ( user_ptrs == NULL )
211 {
212 PERROR( "Failed to allocate memory for buffer pointers\n");
213 exit(EXIT_FAILURE);
214 }
216 /* initialise pointers to the trace buffers - given the size of a trace
217 * buffer and the value of bufs_maped, we can easily calculate these */
218 for ( i = 0; i<num; i++ )
219 user_ptrs[i] = (struct t_buf *)((unsigned long)bufs_mapped + size * i);
221 return user_ptrs;
222 }
225 /**
226 * init_rec_ptrs - initialises data area pointers to locations in user space
227 * @tbufs_mfn: base mfn of the trace buffer area
228 * @tbufs_mapped: user virtual address of base of trace buffer area
229 * @meta: array of user-space pointers to struct t_buf's of metadata
230 * @num: number of trace buffers
231 *
232 * Initialises data area pointers to the locations that data areas have been
233 * mapped in user space. Note that the trace buffer metadata contains machine
234 * pointers - the array returned allows more convenient access to them.
235 */
236 struct t_rec **init_rec_ptrs(struct t_buf **meta, unsigned int num)
237 {
238 int i;
239 struct t_rec **data;
241 data = calloc(num, sizeof(struct t_rec *));
242 if ( data == NULL )
243 {
244 PERROR("Failed to allocate memory for data pointers\n");
245 exit(EXIT_FAILURE);
246 }
248 for ( i = 0; i < num; i++ )
249 data[i] = (struct t_rec *)(meta[i] + 1);
251 return data;
252 }
254 /**
255 * get_num_cpus - get the number of logical CPUs
256 */
257 unsigned int get_num_cpus(void)
258 {
259 xc_physinfo_t physinfo;
260 int xc_handle = xc_interface_open();
261 int ret;
263 ret = xc_physinfo(xc_handle, &physinfo);
265 if ( ret != 0 )
266 {
267 PERROR("Failure to get logical CPU count from Xen");
268 exit(EXIT_FAILURE);
269 }
271 xc_interface_close(xc_handle);
273 return (physinfo.threads_per_core *
274 physinfo.cores_per_socket *
275 physinfo.sockets_per_node *
276 physinfo.nr_nodes);
277 }
280 /**
281 * monitor_tbufs - monitor the contents of tbufs and output to a file
282 * @logfile: the FILE * representing the file to log to
283 */
284 int monitor_tbufs(FILE *logfile)
285 {
286 int i;
288 void *tbufs_mapped; /* pointer to where the tbufs are mapped */
289 struct t_buf **meta; /* pointers to the trace buffer metadata */
290 struct t_rec **data; /* pointers to the trace buffer data areas
291 * where they are mapped into user space. */
292 unsigned long tbufs_mfn; /* mfn of the tbufs */
293 unsigned int num; /* number of trace buffers / logical CPUS */
294 unsigned long size; /* size of a single trace buffer */
296 int size_in_recs;
298 /* get number of logical CPUs (and therefore number of trace buffers) */
299 num = get_num_cpus();
301 /* setup access to trace buffers */
302 get_tbufs(&tbufs_mfn, &size);
303 tbufs_mapped = map_tbufs(tbufs_mfn, num, size);
305 size_in_recs = (size - sizeof(struct t_buf)) / sizeof(struct t_rec);
307 /* build arrays of convenience ptrs */
308 meta = init_bufs_ptrs(tbufs_mapped, num, size);
309 data = init_rec_ptrs(meta, num);
311 if ( opts.discard )
312 for ( i = 0; i < num; i++ )
313 meta[i]->cons = meta[i]->prod;
315 /* now, scan buffers for events */
316 while ( !interrupted )
317 {
318 for ( i = 0; (i < num) && !interrupted; i++ )
319 {
320 while ( meta[i]->cons != meta[i]->prod )
321 {
322 rmb(); /* read prod, then read item. */
323 write_rec(i, data[i] + meta[i]->cons % size_in_recs, logfile);
324 mb(); /* read item, then update cons. */
325 meta[i]->cons++;
326 }
327 }
329 nanosleep(&opts.poll_sleep, NULL);
330 }
332 /* cleanup */
333 free(meta);
334 free(data);
335 /* don't need to munmap - cleanup is automatic */
336 fclose(logfile);
338 return 0;
339 }
342 /******************************************************************************
343 * Various declarations / definitions GNU argp needs to do its work
344 *****************************************************************************/
346 int parse_evtmask(char *arg, struct argp_state *state)
347 {
348 settings_t *setup = (settings_t *)state->input;
349 char *inval;
351 /* search filtering class */
352 if (strcmp(arg, "gen") == 0){
353 setup->evt_mask |= TRC_GEN;
354 } else if(strcmp(arg, "sched") == 0){
355 setup->evt_mask |= TRC_SCHED;
356 } else if(strcmp(arg, "dom0op") == 0){
357 setup->evt_mask |= TRC_DOM0OP;
358 } else if(strcmp(arg, "hvm") == 0){
359 setup->evt_mask |= TRC_HVM;
360 } else if(strcmp(arg, "all") == 0){
361 setup->evt_mask |= TRC_ALL;
362 } else {
363 setup->evt_mask = strtol(arg, &inval, 0);
364 if ( inval == arg )
365 argp_usage(state);
366 }
368 return 0;
370 }
372 /* command parser for GNU argp - see GNU docs for more info */
373 error_t cmd_parser(int key, char *arg, struct argp_state *state)
374 {
375 settings_t *setup = (settings_t *)state->input;
377 switch ( key )
378 {
379 case 't': /* set new records threshold for logging */
380 {
381 char *inval;
382 setup->new_data_thresh = strtol(arg, &inval, 0);
383 if ( inval == arg )
384 argp_usage(state);
385 }
386 break;
388 case 's': /* set sleep time (given in milliseconds) */
389 {
390 char *inval;
391 setup->poll_sleep = millis_to_timespec(strtol(arg, &inval, 0));
392 if ( inval == arg )
393 argp_usage(state);
394 }
395 break;
397 case 'c': /* set new cpu mask for filtering*/
398 {
399 char *inval;
400 setup->cpu_mask = strtol(arg, &inval, 0);
401 if ( inval == arg )
402 argp_usage(state);
403 }
404 break;
406 case 'e': /* set new event mask for filtering*/
407 {
408 parse_evtmask(arg, state);
409 }
410 break;
412 case 'S': /* set tbuf size (given in pages) */
413 {
414 char *inval;
415 setup->tbuf_size = strtol(arg, &inval, 0);
416 if ( inval == arg )
417 argp_usage(state);
418 }
419 break;
421 case 'D': /* Discard traces currently in the buffer before beginning */
422 {
423 opts.discard = 1;
424 }
425 break;
427 case ARGP_KEY_ARG:
428 {
429 if ( state->arg_num == 0 )
430 setup->outfile = arg;
431 else
432 argp_usage(state);
433 }
434 break;
436 default:
437 return ARGP_ERR_UNKNOWN;
438 }
440 return 0;
441 }
443 #define xstr(x) str(x)
444 #define str(x) #x
446 const struct argp_option cmd_opts[] =
447 {
448 { .name = "log-thresh", .key='t', .arg="l",
449 .doc =
450 "Set number, l, of new records required to trigger a write to output "
451 "(default " xstr(NEW_DATA_THRESH) ")." },
453 { .name = "poll-sleep", .key='s', .arg="p",
454 .doc =
455 "Set sleep time, p, in milliseconds between polling the trace buffer "
456 "for new data (default " xstr(POLL_SLEEP_MILLIS) ")." },
458 { .name = "cpu-mask", .key='c', .arg="c",
459 .doc =
460 "Set cpu-mask." },
462 { .name = "evt-mask", .key='e', .arg="e",
463 .doc =
464 "Set trace event mask. This can accept a numerical (including hex) "
465 " argument or a symbolic name. Symbolic names include: gen, sched, "
466 "dom0op, hvm, and all." },
468 { .name = "trace-buf-size", .key='S', .arg="N",
469 .doc =
470 "Set trace buffer size in pages (default " xstr(DEFAULT_TBUF_SIZE) "). "
471 "N.B. that the trace buffer cannot be resized. If it has "
472 "already been set this boot cycle, this argument will be ignored." },
474 { .name = "discard-buffers", .key='D', .arg=NULL,
475 .flags=OPTION_ARG_OPTIONAL,
476 .doc = "Discard all records currently in the trace buffers before "
477 " beginning." },
479 {0}
480 };
482 const struct argp parser_def =
483 {
484 .options = cmd_opts,
485 .parser = cmd_parser,
486 .args_doc = "[output file]",
487 .doc =
488 "Tool to capure Xen trace buffer data"
489 "\v"
490 "This tool is used to capture trace buffer data from Xen. The data is "
491 "output in a binary format, in the following order:\n\n"
492 " CPU(uint) TSC(uint64_t) EVENT(uint32_t) D1 D2 D3 D4 D5 "
493 "(all uint32_t)\n\n"
494 "The output should be parsed using the tool xentrace_format, which can "
495 "produce human-readable output in ASCII format."
496 };
499 const char *argp_program_version = "xentrace v1.1";
500 const char *argp_program_bug_address = "<mark.a.williamson@intel.com>";
503 int main(int argc, char **argv)
504 {
505 int outfd = 1, ret;
506 FILE *logfile;
507 struct sigaction act;
509 opts.outfile = 0;
510 opts.poll_sleep = millis_to_timespec(POLL_SLEEP_MILLIS);
511 opts.new_data_thresh = NEW_DATA_THRESH;
512 opts.evt_mask = 0;
513 opts.cpu_mask = 0;
515 argp_parse(&parser_def, argc, argv, 0, 0, &opts);
517 if (opts.evt_mask != 0) {
518 set_mask(opts.evt_mask, 0);
519 }
521 if (opts.cpu_mask != 0) {
522 set_mask(opts.cpu_mask, 1);
523 }
525 if ( opts.outfile )
526 outfd = open(opts.outfile, O_WRONLY | O_CREAT | O_LARGEFILE, 0644);
528 if(outfd < 0)
529 {
530 perror("Could not open output file");
531 exit(EXIT_FAILURE);
532 }
534 if(isatty(outfd))
535 {
536 fprintf(stderr, "Cannot output to a TTY, specify a log file.\n");
537 exit(EXIT_FAILURE);
538 }
540 logfile = fdopen(outfd, "w");
542 /* ensure that if we get a signal, we'll do cleanup, then exit */
543 act.sa_handler = close_handler;
544 act.sa_flags = 0;
545 sigemptyset(&act.sa_mask);
546 sigaction(SIGHUP, &act, NULL);
547 sigaction(SIGTERM, &act, NULL);
548 sigaction(SIGINT, &act, NULL);
550 ret = monitor_tbufs(logfile);
552 return ret;
553 }