Debian Codegraph @

tools/xenmon/xenbaked.c

raw
1 /******************************************************************************
2 * tools/xenbaked.c
3 *
4 * Tool for collecting raw trace buffer data from Xen and
5 * performing some accumulation operations and other processing
6 * on it.
7 *
8 * Copyright (C) 2004 by Intel Research Cambridge
9 * Copyright (C) 2005 by Hewlett Packard, Palo Alto and Fort Collins
10 * Copyright (C) 2006 by Hewlett Packard Fort Collins
11 *
12 * Authors: Diwaker Gupta, [email protected]
13 * Rob Gardner, [email protected]
14 * Lucy Cherkasova, lucy.cherkasova.hp.com
15 * Much code based on xentrace, authored by Mark Williamson,
16 * [email protected]
17 * Date: November, 2005
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; under version 2 of the License.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; If not, see <http://www.gnu.org/licenses/>.
30 */
31
32 #include <time.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <sys/mman.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <xenevtchn.h>
41 #define XC_WANT_COMPAT_MAP_FOREIGN_API
42 #include <xenctrl.h>
43 #include <xen/xen.h>
44 #include <string.h>
45 #include <sys/select.h>
46 #include <getopt.h>
47
48 #define PERROR(_m, _a...) \
49 do { \
50 int __saved_errno = errno; \
51 fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a , \
52 __saved_errno, strerror(__saved_errno)); \
53 errno = __saved_errno; \
54 } while (0)
55
56 typedef struct { int counter; } atomic_t;
57 #define _atomic_read(v) ((v).counter)
58
59 #include <xen/trace.h>
60 #include "xenbaked.h"
61
62
63 /***** Compile time configuration of defaults ********************************/
64
65 /* when we've got more records than this waiting, we log it to the output */
66 #define NEW_DATA_THRESH 1
67
68 /* sleep for this long (milliseconds) between checking the trace buffers */
69 #define POLL_SLEEP_MILLIS 100
70
71 /* Size of time period represented by each sample */
72 #define MS_PER_SAMPLE 100
73
74 /* CPU Frequency */
75 #define MHZ
76 #define CPU_FREQ 2660 MHZ
77
78 /***** The code **************************************************************/
79
80 typedef struct settings_st {
81 struct timespec poll_sleep;
82 unsigned long new_data_thresh;
83 unsigned long ms_per_sample;
84 double cpu_freq;
85 } settings_t;
86
87 struct t_struct {
88 const struct t_info *t_info; /* Structure with information about individual buffers */
89 struct t_buf **meta; /* Pointers to trace buffer metadata */
90 unsigned char **data; /* Pointers to trace buffer data areas */
91 };
92
93 settings_t opts;
94
95 int interrupted = 0; /* gets set if we get a SIGHUP */
96 int rec_count = 0;
97 int wakeups = 0;
98 time_t start_time;
99 int dom0_flips = 0;
100
101 _new_qos_data *new_qos;
102 _new_qos_data **cpu_qos_data;
103
104 int global_cpu;
105 uint64_t global_now;
106
107 // array of currently running domains, indexed by cpu
108 int *running = NULL;
109
110 // number of cpu's on this platform
111 int NCPU = 0;
112
113
114 static void advance_next_datapoint(uint64_t);
115 static void alloc_qos_data(int ncpu);
116 static int process_record(int, struct t_rec *);
117 static void qos_kill_thread(int domid);
118
119
120 static void init_current(int ncpu)
121 {
122 running = calloc(ncpu, sizeof(int));
123 NCPU = ncpu;
124 printf("Initialized with %d %s\n", ncpu, (ncpu == 1) ? "cpu" : "cpu's");
125 }
126
127 static int is_current(int domain, int cpu)
128 {
129 // int i;
130
131 // for (i=0; i<NCPU; i++)
132 if (running[cpu] == domain)
133 return 1;
134 return 0;
135 }
136
137
138 #if 0 /* unused */
139 // return the domain that's currently running on the given cpu
140 static int current(int cpu)
141 {
142 return running[cpu];
143 }
144 #endif
145
146 static void set_current(int cpu, int domain)
147 {
148 running[cpu] = domain;
149 }
150
151
152
153 static void close_handler(int signal)
154 {
155 interrupted = 1;
156 }
157
158 #if 0
159 void dump_record(int cpu, struct t_rec *x)
160 {
161 printf("record: cpu=%x, tsc=%lx, event=%x, d1=%lx\n",
162 cpu, x->cycles, x->event, x->data[0]);
163 }
164 #endif
165
166 /**
167 * millis_to_timespec - convert a time in milliseconds to a struct timespec
168 * @millis: time interval in milliseconds
169 */
170 static struct timespec millis_to_timespec(unsigned long millis)
171 {
172 struct timespec spec;
173
174 spec.tv_sec = millis / 1000;
175 spec.tv_nsec = (millis % 1000) * 1000;
176
177 return spec;
178 }
179
180
181 typedef struct
182 {
183 int event_count;
184 int event_id;
185 const char *text;
186 } stat_map_t;
187
188 stat_map_t stat_map[] = {
189 { 0, 0, "Other" },
190 { 0, TRC_SCHED_DOM_ADD, "Add Domain" },
191 { 0, TRC_SCHED_DOM_REM, "Remove Domain" },
192 { 0, TRC_SCHED_SLEEP, "Sleep" },
193 { 0, TRC_SCHED_WAKE, "Wake" },
194 { 0, TRC_SCHED_BLOCK, "Block" },
195 { 0, TRC_SCHED_SWITCH, "Switch" },
196 { 0, TRC_SCHED_S_TIMER_FN, "Timer Func"},
197 { 0, TRC_SCHED_SWITCH_INFPREV, "Switch Prev" },
198 { 0, TRC_SCHED_SWITCH_INFNEXT, "Switch Next" },
199 { 0, TRC_MEM_PAGE_GRANT_MAP, "Page Map" },
200 { 0, TRC_MEM_PAGE_GRANT_UNMAP, "Page Unmap" },
201 { 0, TRC_MEM_PAGE_GRANT_TRANSFER, "Page Transfer" },
202 { 0, 0, 0 }
203 };
204
205
206 static void check_gotten_sum(void)
207 {
208 #if 0
209 uint64_t sum, ns;
210 extern uint64_t total_ns_gotten(uint64_t*);
211 double percent;
212 int i;
213
214 for (i=0; i<NCPU; i++) {
215 new_qos = cpu_qos_data[i];
216 ns = billion;
217 sum = total_ns_gotten(&ns);
218
219 printf("[cpu%d] ns_gotten over all domains = %lldns, over %lldns\n",
220 i, sum, ns);
221 percent = (double) sum;
222 percent = (100.0*percent) / (double)ns;
223 printf(" ==> ns_gotten = %7.3f%%\n", percent);
224 }
225 #endif
226 }
227
228
229
230 static void dump_stats(void)
231 {
232 stat_map_t *smt = stat_map;
233 double run_time = difftime(time(NULL), start_time);
234
235 printf("Event counts:\n");
236 while (smt->text != NULL) {
237 printf("%08d\t%s\n", smt->event_count, smt->text);
238 smt++;
239 }
240
241 printf("processed %d total records in %.0f seconds (%.0f per second)\n",
242 rec_count, run_time, run_time ? rec_count / run_time : 0);
243
244 printf("woke up %d times in %.0f seconds (%.0f per second)\n",
245 wakeups, run_time, run_time ? wakeups / run_time : 0);
246
247 check_gotten_sum();
248 }
249
250 static void log_event(int event_id)
251 {
252 stat_map_t *smt = stat_map;
253
254 // printf("event_id = 0x%x\n", event_id);
255
256 while (smt->text != NULL) {
257 if (smt->event_id == event_id) {
258 smt->event_count++;
259 return;
260 }
261 smt++;
262 }
263 if (smt->text == NULL)
264 stat_map[0].event_count++; // other
265 }
266
267 int virq_port;
268 xenevtchn_handle *xce_handle = NULL;
269
270 /* Returns the event channel handle. */
271 /* Stolen from xenstore code */
272 static int eventchn_init(void)
273 {
274 int rc;
275
276 // to revert to old way:
277 if (0)
278 return -1;
279
280 xce_handle = xenevtchn_open(NULL, 0);
281
282 if (xce_handle == NULL)
283 perror("Failed to open evtchn device");
284
285 if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_TBUF)) == -1)
286 perror("Failed to bind to domain exception virq port");
287 virq_port = rc;
288
289 return xce_handle == NULL ? -1 : 0;
290 }
291
292 static void wait_for_event(void)
293 {
294 int ret;
295 fd_set inset;
296 evtchn_port_t port;
297 struct timeval tv;
298 int evtchn_fd;
299
300 if (xce_handle == NULL) {
301 nanosleep(&opts.poll_sleep, NULL);
302 return;
303 }
304
305 evtchn_fd = xenevtchn_fd(xce_handle);
306
307 FD_ZERO(&inset);
308 FD_SET(evtchn_fd, &inset);
309 tv.tv_sec = 1;
310 tv.tv_usec = 0;
311 // tv = millis_to_timespec(&opts.poll_sleep);
312 ret = select(evtchn_fd+1, &inset, NULL, NULL, &tv);
313
314 if ( (ret == 1) && FD_ISSET(evtchn_fd, &inset)) {
315 if ((port = xenevtchn_pending(xce_handle)) == -1)
316 perror("Failed to read from event fd");
317
318 // if (port == virq_port)
319 // printf("got the event I was looking for\r\n");
320
321 if (xenevtchn_unmask(xce_handle, port) == -1)
322 perror("Failed to write to event fd");
323 }
324 }
325
326 static void get_tbufs(unsigned long *mfn, unsigned long *size)
327 {
328 xc_interface *xc_handle = xc_interface_open(0,0,0);
329 int ret;
330
331 if ( !xc_handle )
332 {
333 exit(EXIT_FAILURE);
334 }
335
336 ret = xc_tbuf_enable(xc_handle, DEFAULT_TBUF_SIZE, mfn, size);
337
338 if ( ret != 0 )
339 {
340 perror("Couldn't enable trace buffers");
341 exit(1);
342 }
343
344 xc_interface_close(xc_handle);
345 }
346
347 static void disable_tracing(void)
348 {
349 xc_interface *xc_handle = xc_interface_open(0,0,0);
350 xc_tbuf_disable(xc_handle);
351 xc_interface_close(xc_handle);
352 }
353
354 /**
355 * map_tbufs - memory map Xen trace buffers into user space
356 * @tbufs_mfn: mfn of the trace buffers
357 * @num: number of trace buffers to map
358 * @size: size of each trace buffer
359 *
360 * Maps the Xen trace buffers them into process address space.
361 */
362 static struct t_struct *map_tbufs(unsigned long tbufs_mfn, unsigned int num,
363 unsigned long tinfo_size)
364 {
365 xc_interface *xc_handle;
366 static struct t_struct tbufs = { 0 };
367 int i;
368
369 xc_handle = xc_interface_open(0,0,0);
370 if ( !xc_handle )
371 {
372 exit(EXIT_FAILURE);
373 }
374
375 /* Map t_info metadata structure */
376 tbufs.t_info = xc_map_foreign_range(xc_handle, DOMID_XEN, tinfo_size,
377 PROT_READ, tbufs_mfn);
378
379 if ( tbufs.t_info == 0 )
380 {
381 PERROR("Failed to mmap trace buffers");
382 exit(EXIT_FAILURE);
383 }
384
385 if ( tbufs.t_info->tbuf_size == 0 )
386 {
387 fprintf(stderr, "%s: tbuf_size 0!\n", __func__);
388 exit(EXIT_FAILURE);
389 }
390
391 /* Map per-cpu buffers */
392 tbufs.meta = (struct t_buf **)calloc(num, sizeof(struct t_buf *));
393 tbufs.data = (unsigned char **)calloc(num, sizeof(unsigned char *));
394 if ( tbufs.meta == NULL || tbufs.data == NULL )
395 {
396 PERROR( "Failed to allocate memory for buffer pointers\n");
397 exit(EXIT_FAILURE);
398 }
399
400 for(i=0; i<num; i++)
401 {
402
403 const uint32_t *mfn_list = (const uint32_t *)tbufs.t_info
404 + tbufs.t_info->mfn_offset[i];
405 int j;
406 xen_pfn_t pfn_list[tbufs.t_info->tbuf_size];
407
408 for ( j=0; j<tbufs.t_info->tbuf_size; j++)
409 pfn_list[j] = (xen_pfn_t)mfn_list[j];
410
411 tbufs.meta[i] = xc_map_foreign_pages(xc_handle, DOMID_XEN,
412 PROT_READ | PROT_WRITE,
413 pfn_list,
414 tbufs.t_info->tbuf_size);
415 if ( tbufs.meta[i] == NULL )
416 {
417 PERROR("Failed to map cpu buffer!");
418 exit(EXIT_FAILURE);
419 }
420 tbufs.data[i] = (unsigned char *)(tbufs.meta[i]+1);
421 }
422
423 xc_interface_close(xc_handle);
424
425 return &tbufs;
426 }
427
428 /**
429 * get_num_cpus - get the number of logical CPUs
430 */
431 static unsigned int get_num_cpus(void)
432 {
433 xc_physinfo_t physinfo;
434 xc_interface *xc_handle = xc_interface_open(0,0,0);
435 int ret;
436
437 ret = xc_physinfo(xc_handle, &physinfo);
438
439 if ( ret != 0 )
440 {
441 PERROR("Failure to get logical CPU count from Xen");
442 exit(EXIT_FAILURE);
443 }
444
445 xc_interface_close(xc_handle);
446 opts.cpu_freq = (double)physinfo.cpu_khz/1000.0;
447
448 return physinfo.nr_cpus;
449 }
450
451 /**
452 * monitor_tbufs - monitor the contents of tbufs
453 */
454 static int monitor_tbufs(void)
455 {
456 int i;
457
458 struct t_struct *tbufs; /* Pointer to hypervisor maps */
459 struct t_buf **meta; /* pointers to the trace buffer metadata */
460 unsigned char **data; /* pointers to the trace buffer data areas
461 * where they are mapped into user space. */
462 unsigned long tbufs_mfn; /* mfn of the tbufs */
463 unsigned int num; /* number of trace buffers / logical CPUS */
464 unsigned long tinfo_size; /* size of t_info metadata map */
465 unsigned long size; /* size of a single trace buffer */
466
467 unsigned long data_size, rec_size;
468
469 /* get number of logical CPUs (and therefore number of trace buffers) */
470 num = get_num_cpus();
471
472 init_current(num);
473 alloc_qos_data(num);
474
475 printf("CPU Frequency = %7.2f\n", opts.cpu_freq);
476
477 /* setup access to trace buffers */
478 get_tbufs(&tbufs_mfn, &tinfo_size);
479 tbufs = map_tbufs(tbufs_mfn, num, tinfo_size);
480
481 size = tbufs->t_info->tbuf_size * XC_PAGE_SIZE;
482
483 data_size = size - sizeof(struct t_buf);
484
485 meta = tbufs->meta;
486 data = tbufs->data;
487
488 if ( eventchn_init() < 0 )
489 fprintf(stderr, "Failed to initialize event channel; "
490 "Using POLL method\r\n");
491
492 /* now, scan buffers for events */
493 while ( !interrupted )
494 {
495 for ( i = 0; (i < num) && !interrupted; i++ )
496 {
497 unsigned long start_offset, end_offset, cons, prod;
498
499 cons = meta[i]->cons;
500 prod = meta[i]->prod;
501 xen_rmb(); /* read prod, then read item. */
502
503 if ( cons == prod )
504 continue;
505
506 start_offset = cons % data_size;
507 end_offset = prod % data_size;
508
509 if ( start_offset >= end_offset )
510 {
511 while ( start_offset != data_size )
512 {
513 rec_size = process_record(
514 i, (struct t_rec *)(data[i] + start_offset));
515 start_offset += rec_size;
516 }
517 start_offset = 0;
518 }
519 while ( start_offset != end_offset )
520 {
521 rec_size = process_record(
522 i, (struct t_rec *)(data[i] + start_offset));
523 start_offset += rec_size;
524 }
525 xen_mb(); /* read item, then update cons. */
526 meta[i]->cons = prod;
527 }
528
529 wait_for_event();
530 wakeups++;
531 }
532
533 /* cleanup */
534 free(meta);
535 free(data);
536 /* don't need to munmap - cleanup is automatic */
537
538 return 0;
539 }
540
541
542 /******************************************************************************
543 * Command line handling
544 *****************************************************************************/
545
546 const char *program_version = "xenbaked v1.4";
547 const char *program_bug_address = "<[email protected]>";
548
549 #define xstr(x) str(x)
550 #define str(x) #x
551
552 static void usage(void)
553 {
554 #define USAGE_STR \
555 "Usage: xenbaked [OPTION...]\n" \
556 "Tool to capture and partially process Xen trace buffer data\n" \
557 "\n" \
558 " -m, --ms_per_sample=MS Specify the number of milliseconds per sample\n" \
559 " (default " xstr(MS_PER_SAMPLE) ").\n" \
560 " -s, --poll-sleep=p Set sleep time, p, in milliseconds between\n" \
561 " polling the trace buffer for new data\n" \
562 " (default " xstr(POLL_SLEEP_MILLIS) ").\n" \
563 " -t, --log-thresh=l Set number, l, of new records required to\n" \
564 " trigger a write to output (default " \
565 xstr(NEW_DATA_THRESH) ").\n" \
566 " -?, --help Show this message\n" \
567 " -V, --version Print program version\n" \
568 "\n" \
569 "This tool is used to capture trace buffer data from Xen. The data is\n" \
570 "saved in a shared memory structure to be further processed by xenmon.\n"
571
572 printf(USAGE_STR);
573 printf("\nReport bugs to %s\n", program_bug_address);
574
575 exit(EXIT_FAILURE);
576 }
577
578 /* convert the argument string pointed to by arg to a long int representation */
579 static long argtol(const char *restrict arg, int base)
580 {
581 char *endp;
582 long val;
583
584 errno = 0;
585 val = strtol(arg, &endp, base);
586
587 if (errno != 0) {
588 fprintf(stderr, "Invalid option argument: %s\n", arg);
589 fprintf(stderr, "Error: %s\n\n", strerror(errno));
590 usage();
591 } else if (endp == arg || *endp != '\0') {
592 fprintf(stderr, "Invalid option argument: %s\n\n", arg);
593 usage();
594 }
595
596 return val;
597 }
598
599 /* parse command line arguments */
600 static void parse_args(int argc, char **argv)
601 {
602 int option;
603 static struct option long_options[] = {
604 { "log-thresh", required_argument, 0, 't' },
605 { "poll-sleep", required_argument, 0, 's' },
606 { "ms_per_sample", required_argument, 0, 'm' },
607 { "help", no_argument, 0, '?' },
608 { "version", no_argument, 0, 'V' },
609 { 0, 0, 0, 0 }
610 };
611
612 while ( (option = getopt_long(argc, argv, "m:s:t:?V",
613 long_options, NULL)) != -1)
614 {
615 switch ( option )
616 {
617 case 't': /* set new records threshold for logging */
618 opts.new_data_thresh = argtol(optarg, 0);
619 break;
620
621 case 's': /* set sleep time (given in milliseconds) */
622 opts.poll_sleep = millis_to_timespec(argtol(optarg, 0));
623 break;
624
625 case 'm': /* set ms_per_sample */
626 opts.ms_per_sample = argtol(optarg, 0);
627 break;
628
629 case 'V': /* print program version */
630 printf("%s\n", program_version);
631 exit(EXIT_SUCCESS);
632 break;
633
634 default:
635 usage();
636 }
637 }
638
639 /* all arguments should have been processed */
640 if (optind != argc) {
641 usage();
642 }
643 }
644
645 #define SHARED_MEM_FILE "/var/run/xenq-shm"
646 static void alloc_qos_data(int ncpu)
647 {
648 int i, n, pgsize, off=0;
649 char *dummy;
650 int qos_fd;
651
652 cpu_qos_data = (_new_qos_data **) calloc(ncpu, sizeof(_new_qos_data *));
653
654
655 qos_fd = open(SHARED_MEM_FILE, O_RDWR|O_CREAT|O_TRUNC, 0777);
656 if (qos_fd < 0) {
657 PERROR(SHARED_MEM_FILE);
658 exit(2);
659 }
660 pgsize = getpagesize();
661 dummy = malloc(pgsize);
662 if (!dummy) {
663 PERROR("malloc");
664 exit(EXIT_FAILURE);
665 }
666 memset(dummy, 0, pgsize);
667
668 for (n=0; n<ncpu; n++) {
669
670 for (i=0; i<sizeof(_new_qos_data); i=i+pgsize)
671 if ((write(qos_fd, dummy, pgsize)) != pgsize) {
672 PERROR(SHARED_MEM_FILE);
673 exit(2);
674 }
675
676 new_qos = (_new_qos_data *) mmap(0, sizeof(_new_qos_data), PROT_READ|PROT_WRITE,
677 MAP_SHARED, qos_fd, off);
678 off += i;
679 if (new_qos == MAP_FAILED) {
680 PERROR("mmap");
681 exit(3);
682 }
683 // printf("new_qos = %p\n", new_qos);
684 memset(new_qos, 0, sizeof(_new_qos_data));
685 new_qos->next_datapoint = 0;
686 advance_next_datapoint(0);
687 new_qos->structlen = i;
688 new_qos->ncpu = ncpu;
689 // printf("structlen = 0x%x\n", i);
690 cpu_qos_data[n] = new_qos;
691 }
692 free(dummy);
693 close(qos_fd);
694 new_qos = NULL;
695 }
696
697
698 int main(int argc, char **argv)
699 {
700 int ret;
701 struct sigaction act;
702
703 time(&start_time);
704 opts.poll_sleep = millis_to_timespec(POLL_SLEEP_MILLIS);
705 opts.new_data_thresh = NEW_DATA_THRESH;
706 opts.ms_per_sample = MS_PER_SAMPLE;
707 opts.cpu_freq = CPU_FREQ;
708
709 parse_args(argc, argv);
710 fprintf(stderr, "ms_per_sample = %ld\n", opts.ms_per_sample);
711
712
713 /* ensure that if we get a signal, we'll do cleanup, then exit */
714 act.sa_handler = close_handler;
715 act.sa_flags = 0;
716 sigemptyset(&act.sa_mask);
717 sigaction(SIGHUP, &act, NULL);
718 sigaction(SIGTERM, &act, NULL);
719 sigaction(SIGINT, &act, NULL);
720
721 ret = monitor_tbufs();
722
723 dump_stats();
724 msync(new_qos, sizeof(_new_qos_data), MS_SYNC);
725 disable_tracing();
726
727 return ret;
728 }
729
730 static void qos_init_domain(int domid, int idx)
731 {
732 int i;
733
734 memset(&new_qos->domain_info[idx], 0, sizeof(_domain_info));
735 new_qos->domain_info[idx].last_update_time = global_now;
736 // runnable_start_time[idx] = 0;
737 new_qos->domain_info[idx].runnable_start_time = 0; // invalidate
738 new_qos->domain_info[idx].in_use = 1;
739 new_qos->domain_info[idx].blocked_start_time = 0;
740 new_qos->domain_info[idx].id = domid;
741 if (domid == IDLE_DOMAIN_ID)
742 snprintf(new_qos->domain_info[idx].name,
743 sizeof(new_qos->domain_info[idx].name),
744 "Idle Task%d", global_cpu);
745 else
746 snprintf(new_qos->domain_info[idx].name,
747 sizeof(new_qos->domain_info[idx].name),
748 "Domain#%d", domid);
749
750 for (i=0; i<NSAMPLES; i++) {
751 new_qos->qdata[i].ns_gotten[idx] = 0;
752 new_qos->qdata[i].ns_allocated[idx] = 0;
753 new_qos->qdata[i].ns_waiting[idx] = 0;
754 new_qos->qdata[i].ns_blocked[idx] = 0;
755 new_qos->qdata[i].switchin_count[idx] = 0;
756 new_qos->qdata[i].io_count[idx] = 0;
757 }
758 }
759
760 static void global_init_domain(int domid, int idx)
761 {
762 int cpu;
763 _new_qos_data *saved_qos;
764
765 saved_qos = new_qos;
766
767 for (cpu=0; cpu<NCPU; cpu++) {
768 new_qos = cpu_qos_data[cpu];
769 qos_init_domain(domid, idx);
770 }
771 new_qos = saved_qos;
772 }
773
774 // give index of this domain in the qos data array
775 static int indexof(int domid)
776 {
777 int idx;
778 xc_domaininfo_t dominfo[NDOMAINS];
779 xc_interface *xc_handle;
780 int ndomains;
781
782 if (domid < 0) { // shouldn't happen
783 printf("bad domain id: %d\r\n", domid);
784 return 0;
785 }
786
787 for (idx=0; idx<NDOMAINS; idx++)
788 if ( (new_qos->domain_info[idx].id == domid) && new_qos->domain_info[idx].in_use)
789 return idx;
790
791 // not found, make a new entry
792 for (idx=0; idx<NDOMAINS; idx++)
793 if (new_qos->domain_info[idx].in_use == 0) {
794 global_init_domain(domid, idx);
795 return idx;
796 }
797
798 // call domaininfo hypercall to try and garbage collect unused entries
799 xc_handle = xc_interface_open(0,0,0);
800 ndomains = xc_domain_getinfolist(xc_handle, 0, NDOMAINS, dominfo);
801 xc_interface_close(xc_handle);
802
803 // for each domain in our data, look for it in the system dominfo structure
804 // and purge the domain's data from our state if it does not exist in the
805 // dominfo structure
806 for (idx=0; idx<NDOMAINS; idx++) {
807 int domid = new_qos->domain_info[idx].id;
808 int jdx;
809
810 for (jdx=0; jdx<ndomains; jdx++) {
811 if (dominfo[jdx].domain == domid)
812 break;
813 }
814 if (jdx == ndomains) // we didn't find domid in the dominfo struct
815 if (domid != IDLE_DOMAIN_ID) // exception for idle domain, which is not
816 // contained in dominfo
817 qos_kill_thread(domid); // purge our stale data
818 }
819
820 // look again for a free slot
821 for (idx=0; idx<NDOMAINS; idx++)
822 if (new_qos->domain_info[idx].in_use == 0) {
823 global_init_domain(domid, idx);
824 return idx;
825 }
826
827 // still no space found, so bail
828 fprintf(stderr, "out of space in domain table, increase NDOMAINS\r\n");
829 exit(2);
830 }
831
832 static int domain_runnable(int domid)
833 {
834 return new_qos->domain_info[indexof(domid)].runnable;
835 }
836
837
838 static void update_blocked_time(int domid, uint64_t now)
839 {
840 uint64_t t_blocked;
841 int id = indexof(domid);
842
843 if (new_qos->domain_info[id].blocked_start_time != 0) {
844 if (now >= new_qos->domain_info[id].blocked_start_time)
845 t_blocked = now - new_qos->domain_info[id].blocked_start_time;
846 else
847 t_blocked = now + (~0ULL - new_qos->domain_info[id].blocked_start_time);
848 new_qos->qdata[new_qos->next_datapoint].ns_blocked[id] += t_blocked;
849 }
850
851 if (domain_runnable(domid))
852 new_qos->domain_info[id].blocked_start_time = 0;
853 else
854 new_qos->domain_info[id].blocked_start_time = now;
855 }
856
857
858 // advance to next datapoint for all domains
859 static void advance_next_datapoint(uint64_t now)
860 {
861 int new, old, didx;
862
863 old = new_qos->next_datapoint;
864 new = QOS_INCR(old);
865 new_qos->next_datapoint = new;
866 // memset(&new_qos->qdata[new], 0, sizeof(uint64_t)*(2+5*NDOMAINS));
867 for (didx = 0; didx < NDOMAINS; didx++) {
868 new_qos->qdata[new].ns_gotten[didx] = 0;
869 new_qos->qdata[new].ns_allocated[didx] = 0;
870 new_qos->qdata[new].ns_waiting[didx] = 0;
871 new_qos->qdata[new].ns_blocked[didx] = 0;
872 new_qos->qdata[new].switchin_count[didx] = 0;
873 new_qos->qdata[new].io_count[didx] = 0;
874 }
875 new_qos->qdata[new].ns_passed = 0;
876 new_qos->qdata[new].lost_records = 0;
877 new_qos->qdata[new].flip_free_periods = 0;
878
879 new_qos->qdata[new].timestamp = now;
880 }
881
882
883
884 static void qos_update_thread(int cpu, int domid, uint64_t now)
885 {
886 int n, id;
887 uint64_t last_update_time, start;
888 int64_t time_since_update, run_time = 0;
889
890 id = indexof(domid);
891
892 n = new_qos->next_datapoint;
893 last_update_time = new_qos->domain_info[id].last_update_time;
894
895 time_since_update = now - last_update_time;
896
897 if (time_since_update < 0) {
898 // what happened here? either a timestamp wraparound, or more likely,
899 // a slight inconsistency among timestamps from various cpu's
900 if (-time_since_update < billion) {
901 // fairly small difference, let's just adjust 'now' to be a little
902 // beyond last_update_time
903 time_since_update = -time_since_update;
904 }
905 else if ( ((~0ULL - last_update_time) < billion) && (now < billion) ) {
906 // difference is huge, must be a wraparound
907 // last_update time should be "near" ~0ULL,
908 // and now should be "near" 0
909 time_since_update = now + (~0ULL - last_update_time);
910 printf("time wraparound\n");
911 }
912 else {
913 // none of the above, may be an out of order record
914 // no good solution, just ignore and update again later
915 return;
916 }
917 }
918
919 new_qos->domain_info[id].last_update_time = now;
920
921 if (new_qos->domain_info[id].runnable_at_last_update && is_current(domid, cpu)) {
922 start = new_qos->domain_info[id].start_time;
923 if (start > now) { // wrapped around
924 run_time = now + (~0ULL - start);
925 // this could happen if there is nothing going on within a cpu;
926 // in this case the idle domain would run forever
927 // printf("warning: start > now\n");
928 }
929 else
930 run_time = now - start;
931 // if (run_time < 0) // should not happen
932 // printf("warning: run_time < 0; start = %lld now= %lld\n", start, now);
933 new_qos->domain_info[id].ns_oncpu_since_boot += run_time;
934 new_qos->domain_info[id].start_time = now;
935 new_qos->domain_info[id].ns_since_boot += time_since_update;
936
937 new_qos->qdata[n].ns_gotten[id] += run_time;
938 // if (domid == 0 && cpu == 1)
939 // printf("adding run time for dom0 on cpu1\r\n");
940
941 }
942
943 new_qos->domain_info[id].runnable_at_last_update = domain_runnable(domid);
944
945 update_blocked_time(domid, now);
946
947 // how much time passed since this datapoint was updated?
948 if (now >= new_qos->qdata[n].timestamp) {
949 // all is right with the world, time is increasing
950 new_qos->qdata[n].ns_passed += (now - new_qos->qdata[n].timestamp);
951 }
952 else {
953 // time wrapped around
954 //new_qos->qdata[n].ns_passed += (now + (~0LL - new_qos->qdata[n].timestamp));
955 // printf("why timewrap?\r\n");
956 }
957 new_qos->qdata[n].timestamp = now;
958 }
959
960
961 // called by dump routines to update all structures
962 static void qos_update_all(uint64_t now, int cpu)
963 {
964 int i;
965
966 for (i=0; i<NDOMAINS; i++)
967 if (new_qos->domain_info[i].in_use)
968 qos_update_thread(cpu, new_qos->domain_info[i].id, now);
969 }
970
971
972 static void qos_update_thread_stats(int cpu, int domid, uint64_t now)
973 {
974 if (new_qos->qdata[new_qos->next_datapoint].ns_passed > (million*opts.ms_per_sample)) {
975 qos_update_all(now, cpu);
976 advance_next_datapoint(now);
977 return;
978 }
979 qos_update_thread(cpu, domid, now);
980 }
981
982
983
984 // called when a new thread gets the cpu
985 static void qos_switch_in(int cpu, int domid, uint64_t now, unsigned long ns_alloc, unsigned long ns_waited)
986 {
987 int idx = indexof(domid);
988
989 new_qos->domain_info[idx].runnable = 1;
990 update_blocked_time(domid, now);
991 new_qos->domain_info[idx].blocked_start_time = 0; // invalidate
992 new_qos->domain_info[idx].runnable_start_time = 0; // invalidate
993 //runnable_start_time[idx] = 0;
994
995 new_qos->domain_info[idx].start_time = now;
996 new_qos->qdata[new_qos->next_datapoint].switchin_count[idx]++;
997 new_qos->qdata[new_qos->next_datapoint].ns_allocated[idx] += ns_alloc;
998 new_qos->qdata[new_qos->next_datapoint].ns_waiting[idx] += ns_waited;
999 qos_update_thread_stats(cpu, domid, now);
1000 set_current(cpu, domid);
1001
1002 // count up page flips for dom0 execution
1003 if (domid == 0)
1004 dom0_flips = 0;
1005 }
1006
1007 // called when the current thread is taken off the cpu
1008 static void qos_switch_out(int cpu, int domid, uint64_t now, unsigned long gotten)
1009 {
1010 int idx = indexof(domid);
1011 int n;
1012
1013 if (!is_current(domid, cpu)) {
1014 // printf("switching out domain %d but it is not current. gotten=%ld\r\n", id, gotten);
1015 }
1016
1017 if (gotten == 0) {
1018 printf("gotten==0 in qos_switchout(domid=%d)\n", domid);
1019 }
1020
1021 if (gotten < 100) {
1022 printf("gotten<100ns in qos_switchout(domid=%d)\n", domid);
1023 }
1024
1025
1026 n = new_qos->next_datapoint;
1027 #if 0
1028 new_qos->qdata[n].ns_gotten[idx] += gotten;
1029 if (gotten > new_qos->qdata[n].ns_passed)
1030 printf("inconsistency #257, diff = %lld\n",
1031 gotten - new_qos->qdata[n].ns_passed );
1032 #endif
1033 new_qos->domain_info[idx].ns_oncpu_since_boot += gotten;
1034 new_qos->domain_info[idx].runnable_start_time = now;
1035 // runnable_start_time[id] = now;
1036 qos_update_thread_stats(cpu, domid, now);
1037
1038 // process dom0 page flips
1039 if (domid == 0)
1040 if (dom0_flips == 0)
1041 new_qos->qdata[n].flip_free_periods++;
1042 }
1043
1044 // called when domain is put to sleep, may also be called
1045 // when thread is already asleep
1046 static void qos_state_sleeping(int cpu, int domid, uint64_t now)
1047 {
1048 int idx;
1049
1050 if (!domain_runnable(domid)) // double call?
1051 return;
1052
1053 idx = indexof(domid);
1054 new_qos->domain_info[idx].runnable = 0;
1055 new_qos->domain_info[idx].blocked_start_time = now;
1056 new_qos->domain_info[idx].runnable_start_time = 0; // invalidate
1057 // runnable_start_time[idx] = 0; // invalidate
1058 qos_update_thread_stats(cpu, domid, now);
1059 }
1060
1061
1062
1063 // domain died, presume it's dead on all cpu's, not just mostly dead
1064 static void qos_kill_thread(int domid)
1065 {
1066 int cpu;
1067
1068 for (cpu=0; cpu<NCPU; cpu++) {
1069 cpu_qos_data[cpu]->domain_info[indexof(domid)].in_use = 0;
1070 }
1071
1072 }
1073
1074
1075 // called when thread becomes runnable, may also be called
1076 // when thread is already runnable
1077 static void qos_state_runnable(int cpu, int domid, uint64_t now)
1078 {
1079 int idx;
1080
1081
1082 qos_update_thread_stats(cpu, domid, now);
1083
1084 if (domain_runnable(domid)) // double call?
1085 return;
1086
1087 idx = indexof(domid);
1088 new_qos->domain_info[idx].runnable = 1;
1089 update_blocked_time(domid, now);
1090
1091 new_qos->domain_info[idx].blocked_start_time = 0; /* invalidate */
1092 new_qos->domain_info[idx].runnable_start_time = now;
1093 // runnable_start_time[id] = now;
1094 }
1095
1096
1097 static void qos_count_packets(domid_t domid, uint64_t now)
1098 {
1099 int i, idx = indexof(domid);
1100 _new_qos_data *cpu_data;
1101
1102 for (i=0; i<NCPU; i++) {
1103 cpu_data = cpu_qos_data[i];
1104 if (cpu_data->domain_info[idx].in_use) {
1105 cpu_data->qdata[cpu_data->next_datapoint].io_count[idx]++;
1106 }
1107 }
1108
1109 new_qos->qdata[new_qos->next_datapoint].io_count[0]++;
1110 dom0_flips++;
1111 }
1112
1113
1114 static int process_record(int cpu, struct t_rec *r)
1115 {
1116 uint64_t now = 0;
1117 uint32_t *extra_u32 = r->u.nocycles.extra_u32;
1118
1119 new_qos = cpu_qos_data[cpu];
1120
1121 rec_count++;
1122
1123 if ( r->cycles_included )
1124 {
1125 now = ((uint64_t)r->u.cycles.cycles_hi << 32) | r->u.cycles.cycles_lo;
1126 now = ((double)now) / (opts.cpu_freq / 1000.0);
1127 extra_u32 = r->u.cycles.extra_u32;
1128 }
1129
1130 global_now = now;
1131 global_cpu = cpu;
1132
1133 log_event(r->event);
1134
1135 switch (r->event) {
1136
1137 case TRC_SCHED_SWITCH_INFPREV:
1138 // domain data[0] just switched out and received data[1] ns of cpu time
1139 qos_switch_out(cpu, extra_u32[0], now, extra_u32[1]);
1140 // printf("ns_gotten %ld\n", extra_u32[1]);
1141 break;
1142
1143 case TRC_SCHED_SWITCH_INFNEXT:
1144 // domain data[0] just switched in and
1145 // waited data[1] ns, and was allocated data[2] ns of cpu time
1146 qos_switch_in(cpu, extra_u32[0], now, extra_u32[2], extra_u32[1]);
1147 break;
1148
1149 case TRC_SCHED_DOM_ADD:
1150 (void) indexof(extra_u32[0]);
1151 break;
1152
1153 case TRC_SCHED_DOM_REM:
1154 qos_kill_thread(extra_u32[0]);
1155 break;
1156
1157 case TRC_SCHED_SLEEP:
1158 qos_state_sleeping(cpu, extra_u32[0], now);
1159 break;
1160
1161 case TRC_SCHED_WAKE:
1162 qos_state_runnable(cpu, extra_u32[0], now);
1163 break;
1164
1165 case TRC_SCHED_BLOCK:
1166 qos_state_sleeping(cpu, extra_u32[0], now);
1167 break;
1168
1169 case TRC_MEM_PAGE_GRANT_TRANSFER:
1170 qos_count_packets(extra_u32[0], now);
1171 break;
1172
1173 default:
1174 break;
1175 }
1176
1177 new_qos = NULL;
1178
1179 return 4 + (r->cycles_included ? 8 : 0) + (r->extra_u32 * 4);
1180 }
1181
1182 /*
1183 * Local variables:
1184 * mode: C
1185 * c-file-style: "BSD"
1186 * c-basic-offset: 4
1187 * tab-width: 4
1188 * indent-tabs-mode: nil
1189 * End:
1190 */