1 /* -*-  Mode:C; c-basic-offset:4; tab-width:4 -*-
2  ****************************************************************************
3  * (C) 2009 - Juergen Gross - Fujitsu Technology Solutions
4  ****************************************************************************
5  *
6  *        File: xenlockprof.c
7  *      Author: Juergen Gross (juergen.gross@ts.fujitsu.com)
8  *        Date: Oct 2009
9  *
10  * Description:
11  */
12 
13 #include <xenctrl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/mman.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <inttypes.h>
20 
main(int argc,char * argv[])21 int main(int argc, char *argv[])
22 {
23     xc_interface      *xc_handle;
24     uint32_t           i, j, n;
25     uint64_t           time;
26     double             l, b, sl, sb;
27     char               name[100];
28     DECLARE_HYPERCALL_BUFFER(xc_lockprof_data_t, data);
29 
30     if ( (argc > 2) || ((argc == 2) && (strcmp(argv[1], "-r") != 0)) )
31     {
32         printf("%s: [-r]\n", argv[0]);
33         printf("no args: print lock profile data\n");
34         printf("    -r : reset profile data\n");
35         return 1;
36     }
37 
38     if ( (xc_handle = xc_interface_open(0,0,0)) == 0 )
39     {
40         fprintf(stderr, "Error opening xc interface: %d (%s)\n",
41                 errno, strerror(errno));
42         return 1;
43     }
44 
45     if ( argc > 1 )
46     {
47         if ( xc_lockprof_reset(xc_handle) != 0 )
48         {
49             fprintf(stderr, "Error resetting profile data: %d (%s)\n",
50                     errno, strerror(errno));
51             return 1;
52         }
53         return 0;
54     }
55 
56     n = 0;
57     if ( xc_lockprof_query_number(xc_handle, &n) != 0 )
58     {
59         fprintf(stderr, "Error getting number of profile records: %d (%s)\n",
60                 errno, strerror(errno));
61         return 1;
62     }
63 
64     n += 32;    /* just to be sure */
65     data = xc_hypercall_buffer_alloc(xc_handle, data, sizeof(*data) * n);
66     if ( data == NULL )
67     {
68         fprintf(stderr, "Could not allocate buffers: %d (%s)\n",
69                 errno, strerror(errno));
70         return 1;
71     }
72 
73     i = n;
74     if ( xc_lockprof_query(xc_handle, &i, &time, HYPERCALL_BUFFER(data)) != 0 )
75     {
76         fprintf(stderr, "Error getting profile records: %d (%s)\n",
77                 errno, strerror(errno));
78         return 1;
79     }
80 
81     if ( i > n )
82     {
83         printf("data incomplete, %d records are missing!\n\n", i - n);
84         i = n;
85     }
86 
87     sl = 0;
88     sb = 0;
89     for ( j = 0; j < i; j++ )
90     {
91         switch ( data[j].type )
92         {
93         case LOCKPROF_TYPE_GLOBAL:
94             sprintf(name, "global lock %s", data[j].name);
95             break;
96         case LOCKPROF_TYPE_PERDOM:
97             sprintf(name, "domain %d lock %s", data[j].idx, data[j].name);
98             break;
99         default:
100             sprintf(name, "unknown type(%d) %d lock %s", data[j].type,
101                     data[j].idx, data[j].name);
102             break;
103         }
104         l = (double)(data[j].lock_time) / 1E+09;
105         b = (double)(data[j].block_time) / 1E+09;
106         sl += l;
107         sb += b;
108         printf("%-50s: lock:%12"PRId64"(%20.9fs), "
109                "block:%12"PRId64"(%20.9fs)\n",
110                name, data[j].lock_cnt, l, data[j].block_cnt, b);
111     }
112     l = (double)time / 1E+09;
113     printf("total profiling time: %20.9fs\n", l);
114     printf("total locked time:    %20.9fs\n", sl);
115     printf("total blocked time:   %20.9fs\n", sb);
116 
117     xc_hypercall_buffer_free(xc_handle, data);
118 
119     return 0;
120 }
121