2 * Copyright (c) 2014-2018, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
36 /* A time unit test fixture. */
39 /* The configuration to use. */
40 struct pt_config config;
42 /* The calibration to use. */
43 struct pt_time_cal tcal;
45 /* The time struct to update. */
48 /* The test fixture initialization and finalization functions. */
49 struct ptunit_result (*init)(struct time_fixture *);
50 struct ptunit_result (*fini)(struct time_fixture *);
53 static struct ptunit_result tfix_init(struct time_fixture *tfix)
55 memset(&tfix->config, 0, sizeof(tfix->config));
56 tfix->config.size = sizeof(tfix->config);
57 tfix->config.cpuid_0x15_eax = 2;
58 tfix->config.cpuid_0x15_ebx = 1;
59 tfix->config.mtc_freq = 4;
61 pt_tcal_init(&tfix->tcal);
62 pt_tcal_set_fcr(&tfix->tcal, 0x2ull << pt_tcal_fcr_shr);
64 pt_time_init(&tfix->time);
70 static struct ptunit_result tsc_null(struct time_fixture *tfix)
72 struct pt_packet_tsc packet;
75 errcode = pt_time_update_tsc(NULL, &packet, &tfix->config);
76 ptu_int_eq(errcode, -pte_internal);
78 errcode = pt_time_update_tsc(&tfix->time, NULL, &tfix->config);
79 ptu_int_eq(errcode, -pte_internal);
84 static struct ptunit_result cbr_null(struct time_fixture *tfix)
86 struct pt_packet_cbr packet;
89 errcode = pt_time_update_cbr(NULL, &packet, &tfix->config);
90 ptu_int_eq(errcode, -pte_internal);
92 errcode = pt_time_update_cbr(&tfix->time, NULL, &tfix->config);
93 ptu_int_eq(errcode, -pte_internal);
98 static struct ptunit_result tma_null(struct time_fixture *tfix)
100 struct pt_packet_tma packet;
103 errcode = pt_time_update_tma(NULL, &packet, &tfix->config);
104 ptu_int_eq(errcode, -pte_internal);
106 errcode = pt_time_update_tma(&tfix->time, NULL, &tfix->config);
107 ptu_int_eq(errcode, -pte_internal);
109 errcode = pt_time_update_tma(&tfix->time, &packet, NULL);
110 ptu_int_eq(errcode, -pte_internal);
115 static struct ptunit_result mtc_null(struct time_fixture *tfix)
117 struct pt_packet_mtc packet;
120 errcode = pt_time_update_mtc(NULL, &packet, &tfix->config);
121 ptu_int_eq(errcode, -pte_internal);
123 errcode = pt_time_update_mtc(&tfix->time, NULL, &tfix->config);
124 ptu_int_eq(errcode, -pte_internal);
126 errcode = pt_time_update_mtc(&tfix->time, &packet, NULL);
127 ptu_int_eq(errcode, -pte_internal);
132 static struct ptunit_result cyc_null(struct time_fixture *tfix)
134 struct pt_packet_cyc packet;
137 errcode = pt_time_update_cyc(NULL, &packet, &tfix->config, 0ull);
138 ptu_int_eq(errcode, -pte_internal);
140 errcode = pt_time_update_cyc(&tfix->time, NULL, &tfix->config, 0ull);
141 ptu_int_eq(errcode, -pte_internal);
143 errcode = pt_time_update_cyc(&tfix->time, &packet, NULL, 0ull);
144 ptu_int_eq(errcode, -pte_internal);
149 static struct ptunit_result query_tsc_null(struct time_fixture *tfix)
154 errcode = pt_time_query_tsc(NULL, NULL, NULL, &tfix->time);
155 ptu_int_eq(errcode, -pte_internal);
157 errcode = pt_time_query_tsc(&tsc, NULL, NULL, NULL);
158 ptu_int_eq(errcode, -pte_internal);
163 static struct ptunit_result query_tsc_none(struct time_fixture *tfix)
168 errcode = pt_time_query_tsc(&tsc, NULL, NULL, &tfix->time);
169 ptu_int_eq(errcode, -pte_no_time);
174 static struct ptunit_result query_cbr_null(struct time_fixture *tfix)
179 errcode = pt_time_query_cbr(NULL, &tfix->time);
180 ptu_int_eq(errcode, -pte_internal);
182 errcode = pt_time_query_cbr(&cbr, NULL);
183 ptu_int_eq(errcode, -pte_internal);
188 static struct ptunit_result query_cbr_none(struct time_fixture *tfix)
193 errcode = pt_time_query_cbr(&cbr, &tfix->time);
194 ptu_int_eq(errcode, -pte_no_cbr);
199 static struct ptunit_result tcal_cbr_null(struct time_fixture *tfix)
201 struct pt_packet_cbr packet;
204 errcode = pt_tcal_update_cbr(NULL, &packet, &tfix->config);
205 ptu_int_eq(errcode, -pte_internal);
210 static struct ptunit_result tcal_mtc_null(struct time_fixture *tfix)
212 struct pt_packet_mtc packet;
215 errcode = pt_tcal_update_mtc(NULL, &packet, &tfix->config);
216 ptu_int_eq(errcode, -pte_internal);
218 errcode = pt_tcal_update_mtc(&tfix->tcal, NULL, &tfix->config);
219 ptu_int_eq(errcode, -pte_internal);
221 errcode = pt_tcal_update_mtc(&tfix->tcal, &packet, NULL);
222 ptu_int_eq(errcode, -pte_internal);
227 static struct ptunit_result tcal_cyc_null(struct time_fixture *tfix)
229 struct pt_packet_cyc packet;
232 errcode = pt_tcal_update_cyc(NULL, &packet, &tfix->config);
233 ptu_int_eq(errcode, -pte_internal);
235 errcode = pt_tcal_update_cyc(&tfix->tcal, NULL, &tfix->config);
236 ptu_int_eq(errcode, -pte_internal);
241 static struct ptunit_result tsc(struct time_fixture *tfix)
243 struct pt_packet_tsc packet;
245 uint32_t lost_mtc, lost_cyc;
248 packet.tsc = 0xdedededeull;
250 errcode = pt_time_update_tsc(&tfix->time, &packet, &tfix->config);
251 ptu_int_eq(errcode, 0);
253 errcode = pt_time_query_tsc(&tsc, &lost_mtc, &lost_cyc, &tfix->time);
254 ptu_int_eq(errcode, 0);
256 ptu_uint_eq(tsc, 0xdedededeull);
257 ptu_uint_eq(lost_mtc, 0);
258 ptu_uint_eq(lost_cyc, 0);
263 static struct ptunit_result cbr(struct time_fixture *tfix)
265 struct pt_packet_cbr packet;
271 errcode = pt_time_update_cbr(&tfix->time, &packet, &tfix->config);
272 ptu_int_eq(errcode, 0);
274 errcode = pt_time_query_cbr(&cbr, &tfix->time);
275 ptu_int_eq(errcode, 0);
277 ptu_uint_eq(cbr, 0x38);
282 static struct ptunit_result tma(struct time_fixture *tfix)
284 struct pt_packet_tma packet;
290 errcode = pt_time_update_tma(&tfix->time, &packet, &tfix->config);
291 ptu_int_eq(errcode, -pte_bad_context);
296 static struct ptunit_result mtc(struct time_fixture *tfix)
298 struct pt_packet_mtc packet;
304 errcode = pt_time_update_mtc(&tfix->time, &packet, &tfix->config);
305 ptu_int_eq(errcode, 0);
307 errcode = pt_time_query_tsc(&tsc, NULL, NULL, &tfix->time);
308 ptu_int_eq(errcode, -pte_no_time);
313 static struct ptunit_result cyc(struct time_fixture *tfix)
315 struct pt_packet_cyc packet;
319 errcode = pt_tcal_fcr(&fcr, &tfix->tcal);
320 ptu_int_eq(errcode, 0);
324 errcode = pt_time_update_cyc(&tfix->time, &packet, &tfix->config, fcr);
325 ptu_int_eq(errcode, 0);
327 errcode = pt_time_query_tsc(&tsc, NULL, NULL, &tfix->time);
328 ptu_int_eq(errcode, -pte_no_time);
334 int main(int argc, char **argv)
336 struct ptunit_suite suite;
337 struct time_fixture tfix;
339 suite = ptunit_mk_suite(argc, argv);
341 tfix.init = tfix_init;
344 ptu_run_f(suite, tsc_null, tfix);
345 ptu_run_f(suite, cbr_null, tfix);
346 ptu_run_f(suite, tma_null, tfix);
347 ptu_run_f(suite, mtc_null, tfix);
348 ptu_run_f(suite, cyc_null, tfix);
350 ptu_run_f(suite, query_tsc_null, tfix);
351 ptu_run_f(suite, query_tsc_none, tfix);
352 ptu_run_f(suite, query_cbr_null, tfix);
353 ptu_run_f(suite, query_cbr_none, tfix);
355 ptu_run_f(suite, tcal_cbr_null, tfix);
356 ptu_run_f(suite, tcal_mtc_null, tfix);
357 ptu_run_f(suite, tcal_cyc_null, tfix);
359 ptu_run_f(suite, tsc, tfix);
360 ptu_run_f(suite, cbr, tfix);
361 ptu_run_f(suite, tma, tfix);
362 ptu_run_f(suite, mtc, tfix);
363 ptu_run_f(suite, cyc, tfix);
365 /* The bulk is covered in ptt tests. */
367 return ptunit_report(&suite);