1 /*-
2 * Copyright (c) 2014, Neville-Neil Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * Author: George V. Neville-Neil
27 *
28 */
29
30 /*
31 * Calculate the time overhead of starting, stopping, and recording
32 * pmc counters.
33 *
34 * The only argument is a counter name, such as "instruction-retired"
35 * which is CPU dependent and can be found with pmmcontrol(8) using
36 * pmccontrol -L.
37 *
38 * The start, stop, read and write operations are timed using the
39 * rdtsc() macro which reads the Time Stamp Counter on the CPU.
40 */
41
42 #include <stdio.h>
43 #include <err.h>
44 #include <sysexits.h>
45 #include <sys/types.h>
46 #include <machine/cpufunc.h>
47 #include <pmc.h>
48
49 int
main(int argc,char ** argv)50 main(int argc, char **argv)
51 {
52 pmc_id_t pmcid;
53 pmc_value_t read_value;
54 pmc_value_t read_clear_value;
55 uint64_t tsc1, write_cyc, start_cyc, read_cyc, stop_cyc;
56 char *counter_name;
57
58 if (argc != 2)
59 err(EX_USAGE, "counter-name required");
60
61 counter_name = argv[1];
62
63 if (pmc_init() != 0)
64 err(EX_OSERR, "hwpmc(4) not loaded, kldload or update your kernel");
65
66 if (pmc_allocate(counter_name, PMC_MODE_SC, 0, 0, &pmcid, 64*1024) < 0)
67 err(EX_OSERR, "failed to allocate %s as a system counter in counting mode",
68 counter_name);
69
70 tsc1 = rdtsc();
71 if (pmc_write(pmcid, 0) < 0)
72 err(EX_OSERR, "failed to zero counter %s", counter_name);
73 write_cyc = rdtsc() - tsc1;
74
75 tsc1 = rdtsc();
76 if (pmc_start(pmcid) < 0)
77 err(EX_OSERR, "failed to start counter %s", counter_name);
78 start_cyc = rdtsc() - tsc1;
79
80 tsc1 = rdtsc();
81 if (pmc_read(pmcid, &read_value) < 0)
82 err(EX_OSERR, "failed to read counter %s", counter_name);
83 read_cyc = rdtsc() - tsc1;
84
85 tsc1 = rdtsc();
86 if (pmc_stop(pmcid) < 0)
87 err(EX_OSERR, "failed to stop counter %s", counter_name);
88 stop_cyc = rdtsc() - tsc1;
89
90 if (pmc_rw(pmcid, 0, &read_clear_value))
91 err(EX_OSERR, "failed to read and zero %s", counter_name);
92
93 if (pmc_release(pmcid) < 0)
94 err(EX_OSERR, "failed to release %s as a system counter in counting mode",
95 counter_name);
96
97 printf("Counter %s, read value %ld, read/clear value %ld\n",
98 counter_name, read_value, read_clear_value);
99 printf("Cycles to start: %ld\tstop: %ld\tread: %ld\twrite: %ld\n",
100 start_cyc, stop_cyc, read_cyc, stop_cyc);
101
102 return(0);
103 }
104
105