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