1 /* -*-  Mode:C; c-basic-offset:4; tab-width:4 -*-
2  ****************************************************************************
3  * (C) 2004 - Rolf Neugebauer - Intel Research Cambridge
4  ****************************************************************************
5  *
6  *        File: xenperf.c
7  *      Author: Rolf Neugebauer (rolf.neugebauer@intel.com)
8  *        Date: Nov 2004
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 
20 #define X(name) [__HYPERVISOR_##name] = #name
21 const char *hypercall_name_table[64] =
22 {
23     X(set_trap_table),
24     X(mmu_update),
25     X(set_gdt),
26     X(stack_switch),
27     X(set_callbacks),
28     X(fpu_taskswitch),
29     X(sched_op_compat),
30     X(platform_op),
31     X(set_debugreg),
32     X(get_debugreg),
33     X(update_descriptor),
34     X(memory_op),
35     X(multicall),
36     X(update_va_mapping),
37     X(set_timer_op),
38     X(event_channel_op_compat),
39     X(xen_version),
40     X(console_io),
41     X(physdev_op_compat),
42     X(grant_table_op),
43     X(vm_assist),
44     X(update_va_mapping_otherdomain),
45     X(iret),
46     X(vcpu_op),
47     X(set_segment_base),
48     X(mmuext_op),
49     X(xsm_op),
50     X(nmi_op),
51     X(sched_op),
52     X(callback_op),
53     X(xenoprof_op),
54     X(event_channel_op),
55     X(physdev_op),
56     X(hvm_op),
57     X(sysctl),
58     X(domctl),
59     X(kexec_op),
60     X(arch_0),
61     X(arch_1),
62     X(arch_2),
63     X(arch_3),
64     X(arch_4),
65     X(arch_5),
66     X(arch_6),
67     X(arch_7),
68 };
69 #undef X
70 
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73     int              i, j;
74     xc_interface    *xc_handle;
75     DECLARE_HYPERCALL_BUFFER(xc_perfc_desc_t, pcd);
76     DECLARE_HYPERCALL_BUFFER(xc_perfc_val_t, pcv);
77     xc_perfc_val_t  *val;
78     int num_desc, num_val;
79     unsigned int    sum, reset = 0, full = 0, pretty = 0;
80     char hypercall_name[36];
81 
82     if ( argc > 1 )
83     {
84         char *p = argv[1];
85         if ( p[0] == '-' )
86         {
87             switch ( p[1] )
88             {
89             case 'f':
90                 full = 1;
91                 break;
92             case 'p':
93                 full = 1;
94                 pretty = 1;
95                 break;
96             case 'r':
97                 reset = 1;
98                 break;
99             default:
100                 goto error;
101             }
102         }
103         else
104         {
105         error:
106             printf("%s: [-r]\n", argv[0]);
107             printf("no args: print digested counters\n");
108             printf("    -f : print full arrays/histograms\n");
109             printf("    -p : print full arrays/histograms in pretty format\n");
110             printf("    -r : reset counters\n");
111             return 0;
112         }
113     }
114 
115     if ( (xc_handle = xc_interface_open(0,0,0)) == 0 )
116     {
117         fprintf(stderr, "Error opening xc interface: %d (%s)\n",
118                 errno, strerror(errno));
119         return 1;
120     }
121 
122     if ( reset )
123     {
124         if ( xc_perfc_reset(xc_handle) != 0 )
125         {
126             fprintf(stderr, "Error resetting performance counters: %d (%s)\n",
127                     errno, strerror(errno));
128             return 1;
129         }
130 
131         return 0;
132     }
133 
134     if ( xc_perfc_query_number(xc_handle, &num_desc, &num_val) != 0 )
135     {
136         fprintf(stderr, "Error getting number of perf counters: %d (%s)\n",
137                 errno, strerror(errno));
138         return 1;
139     }
140 
141     pcd = xc_hypercall_buffer_alloc(xc_handle, pcd, sizeof(*pcd) * num_desc);
142     pcv = xc_hypercall_buffer_alloc(xc_handle, pcv, sizeof(*pcv) * num_val);
143 
144     if ( pcd == NULL || pcv == NULL)
145     {
146         fprintf(stderr, "Could not allocate buffers: %d (%s)\n",
147                 errno, strerror(errno));
148         exit(-1);
149     }
150 
151     if ( xc_perfc_query(xc_handle, HYPERCALL_BUFFER(pcd), HYPERCALL_BUFFER(pcv)) != 0 )
152     {
153         fprintf(stderr, "Error getting perf counter: %d (%s)\n",
154                 errno, strerror(errno));
155         return 1;
156     }
157 
158     val = pcv;
159     for ( i = 0; i < num_desc; i++ )
160     {
161         printf ("%-35s ", pcd[i].name);
162 
163         sum = 0;
164         for ( j = 0; j < pcd[i].nr_vals; j++ )
165             sum += val[j];
166         printf ("T=%10u ", (unsigned int)sum);
167 
168         if ( full || (pcd[i].nr_vals <= 4) )
169         {
170             if ( pretty && (strcmp(pcd[i].name, "hypercalls") == 0) )
171             {
172                 printf("\n");
173                 for( j = 0; j < pcd[i].nr_vals; j++ )
174                 {
175                     if ( val[j] == 0 )
176                         continue;
177                     if ( (j < 64) && hypercall_name_table[j] )
178                         strncpy(hypercall_name, hypercall_name_table[j],
179                                 sizeof(hypercall_name));
180                     else
181                         snprintf(hypercall_name, sizeof(hypercall_name), "[%d]", j);
182                     hypercall_name[sizeof(hypercall_name)-1]='\0';
183                     printf("%-35s ", hypercall_name);
184                     printf("%12u\n", (unsigned int)val[j]);
185                 }
186             }
187             else
188             {
189                 for ( j = 0; j < pcd[i].nr_vals; j++ )
190                     printf(" %10u", (unsigned int)val[j]);
191                 printf("\n");
192             }
193         }
194         else
195         {
196             printf("\n");
197         }
198 
199         val += pcd[i].nr_vals;
200     }
201 
202     xc_hypercall_buffer_free(xc_handle, pcd);
203     xc_hypercall_buffer_free(xc_handle, pcv);
204     return 0;
205 }
206