radix: Implement radix_tree_destroy()
[akaros.git] / user / perfmon / tests / validate_mips.c
1 /*
2  * validate_mips.c - validate MIPS event tables + encodings
3  *
4  * Copyright (c) 2011 Google, Inc
5  * Contributed by Stephane Eranian <eranian@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <inttypes.h>
29 #include <stdarg.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #include <perfmon/pfmlib.h>
35
36 #define MAX_ENCODING    2
37 #define SRC_LINE        .line = __LINE__
38
39 typedef struct {
40         const char *name;
41         const char *fstr;
42         uint64_t codes[MAX_ENCODING];
43         int ret, count, line;
44 } test_event_t;
45
46 static const test_event_t mips_test_events[]={
47         { SRC_LINE,
48           .name = "mips_74k::cycles",
49           .ret  = PFM_SUCCESS,
50           .count = 2,
51           .codes[0] = 0xa,
52           .codes[1] = 0xf,
53           .fstr = "mips_74k::CYCLES:k=1:u=1:s=0:e=0",
54         },
55         { SRC_LINE,
56           .name = "mips_74k::cycles:k",
57           .ret  = PFM_SUCCESS,
58           .count = 2,
59           .codes[0] = 0x2,
60           .codes[1] = 0xf,
61           .fstr = "mips_74k::CYCLES:k=1:u=0:s=0:e=0",
62         },
63         { SRC_LINE,
64           .name = "mips_74k::cycles:u",
65           .ret  = PFM_SUCCESS,
66           .count = 2,
67           .codes[0] = 0x8,
68           .codes[1] = 0xf,
69           .fstr = "mips_74k::CYCLES:k=0:u=1:s=0:e=0",
70         },
71         { SRC_LINE,
72           .name = "mips_74k::cycles:s",
73           .ret  = PFM_SUCCESS,
74           .count = 2,
75           .codes[0] = 0x4,
76           .codes[1] = 0xf,
77           .fstr = "mips_74k::CYCLES:k=0:u=0:s=1:e=0",
78         },
79         { SRC_LINE,
80           .name = "mips_74k::cycles:e",
81           .ret  = PFM_SUCCESS,
82           .count = 2,
83           .codes[0] = 0x1,
84           .codes[1] = 0xf,
85           .fstr = "mips_74k::CYCLES:k=0:u=0:s=0:e=1",
86         },
87         { SRC_LINE,
88           .name = "mips_74k::cycles:u:k",
89           .ret  = PFM_SUCCESS,
90           .count = 2,
91           .codes[0] = 0xa,
92           .codes[1] = 0xf,
93           .fstr = "mips_74k::CYCLES:k=1:u=1:s=0:e=0",
94         },
95
96         { SRC_LINE,
97           .name = "mips_74k::instructions",
98           .ret  = PFM_SUCCESS,
99           .count = 2,
100           .codes[0] = 0x2a,
101           .codes[1] = 0xf,
102           .fstr = "mips_74k::INSTRUCTIONS:k=1:u=1:s=0:e=0",
103         },
104         { SRC_LINE,
105           .name = "mips_74k::instructions:k",
106           .ret  = PFM_SUCCESS,
107           .count = 2,
108           .codes[0] = 0x22,
109           .codes[1] = 0xf,
110           .fstr = "mips_74k::INSTRUCTIONS:k=1:u=0:s=0:e=0",
111         },
112         { SRC_LINE,
113           .name = "mips_74k::instructions:u",
114           .ret  = PFM_SUCCESS,
115           .count = 2,
116           .codes[0] = 0x28,
117           .codes[1] = 0xf,
118           .fstr = "mips_74k::INSTRUCTIONS:k=0:u=1:s=0:e=0",
119         },
120         { SRC_LINE,
121           .name = "mips_74k::instructions:s",
122           .ret  = PFM_SUCCESS,
123           .count = 2,
124           .codes[0] = 0x24,
125           .codes[1] = 0xf,
126           .fstr = "mips_74k::INSTRUCTIONS:k=0:u=0:s=1:e=0",
127         },
128         { SRC_LINE,
129           .name = "mips_74k::instructions:e",
130           .ret  = PFM_SUCCESS,
131           .count = 2,
132           .codes[0] = 0x21,
133           .codes[1] = 0xf,
134           .fstr = "mips_74k::INSTRUCTIONS:k=0:u=0:s=0:e=1",
135         },
136         { SRC_LINE,
137           .name = "mips_74k::instructions:u:k",
138           .ret  = PFM_SUCCESS,
139           .count = 2,
140           .codes[0] = 0x2a,
141           .codes[1] = 0xf,
142           .fstr = "mips_74k::INSTRUCTIONS:k=1:u=1:s=0:e=0",
143         },
144
145         { SRC_LINE,
146           .name = "mips_74k::PREDICTED_JR_31:u:k",
147           .ret  = PFM_SUCCESS,
148           .count = 2,
149           .codes[0] = 0x4a,
150           .codes[1] = 0x5,
151           .fstr = "mips_74k::PREDICTED_JR_31:k=1:u=1:s=0:e=0",
152         },
153         { SRC_LINE,
154           .name = "mips_74k::JR_31_MISPREDICTIONS:s:e",
155           .ret  = PFM_SUCCESS,
156           .count = 2,
157           .codes[0] = 0x45,
158           .codes[1] = 0xa,
159           .fstr = "mips_74k::JR_31_MISPREDICTIONS:k=0:u=0:s=1:e=1",
160         },
161 };
162 #define NUM_TEST_EVENTS (int)(sizeof(mips_test_events)/sizeof(test_event_t))
163
164 static int check_test_events(FILE *fp)
165 {
166         const test_event_t *e;
167         char *fstr;
168         uint64_t *codes;
169         int count, i, j;
170         int ret, errors = 0;
171
172         for (i = 0, e = mips_test_events; i < NUM_TEST_EVENTS; i++, e++) {
173                 codes = NULL;
174                 count = 0;
175                 fstr = NULL;
176                 ret = pfm_get_event_encoding(e->name, PFM_PLM0 | PFM_PLM3, &fstr, NULL, &codes, &count);
177                 if (ret != e->ret) {
178                         fprintf(fp,"Event%d %s, ret=%s(%d) expected %s(%d)\n", i, e->name, pfm_strerror(ret), ret, pfm_strerror(e->ret), e->ret);
179                         errors++;
180                 } else {
181                         if (ret != PFM_SUCCESS) {
182                                 if (fstr) {
183                                         fprintf(fp,"Event%d %s, expected fstr NULL but it is not\n", i, e->name);
184                                         errors++;
185                                 }
186                                 if (count != 0) {
187                                         fprintf(fp,"Event%d %s, expected count=0 instead of %d\n", i, e->name, count);
188                                         errors++;
189                                 }
190                                 if (codes) {
191                                         fprintf(fp,"Event%d %s, expected codes[] NULL but it is not\n", i, e->name);
192                                         errors++;
193                                 }
194                         } else {
195                                 if (count != e->count) {
196                                         fprintf(fp,"Event%d %s, count=%d expected %d\n", i, e->name, count, e->count);
197                                         errors++;
198                                 }
199                                 for (j=0; j < count; j++) {
200                                         if (codes[j] != e->codes[j]) {
201                                                 fprintf(fp,"Event%d %s, codes[%d]=%#"PRIx64" expected %#"PRIx64"\n", i, e->name, j, codes[j], e->codes[j]);
202                                                 errors++;
203                                         }
204                                 }
205                                 if (e->fstr && strcmp(fstr, e->fstr)) {
206                                         fprintf(fp,"Event%d %s, fstr=%s expected %s\n", i, e->name, fstr, e->fstr);
207                                         errors++;
208                                 }
209                         }
210                 }
211                 if (codes)
212                         free(codes);
213                 if (fstr)
214                         free(fstr);
215         }
216         printf("\t %d MIPS events: %d errors\n", i, errors);
217         return errors;
218 }
219
220 int
221 validate_arch(FILE *fp)
222 {
223         return check_test_events(fp);
224 }