/*-1* Copyright (c) 2014, Neville-Neil Consulting2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* Author: George V. Neville-Neil26*27*/2829/*30* Calculate the time overhead of starting, stopping, and recording31* pmc counters.32*33* The only argument is a counter name, such as "instruction-retired"34* which is CPU dependent and can be found with pmmcontrol(8) using35* pmccontrol -L.36*37* The start, stop, read and write operations are timed using the38* rdtsc() macro which reads the Time Stamp Counter on the CPU.39*/4041#include <stdio.h>42#include <err.h>43#include <sysexits.h>44#include <sys/types.h>45#include <machine/cpufunc.h>46#include <pmc.h>4748int49main(int argc, char **argv)50{51pmc_id_t pmcid;52pmc_value_t read_value;53pmc_value_t read_clear_value;54uint64_t tsc1, write_cyc, start_cyc, read_cyc, stop_cyc;55char *counter_name;5657if (argc != 2)58err(EX_USAGE, "counter-name required");5960counter_name = argv[1];6162if (pmc_init() != 0)63err(EX_OSERR, "hwpmc(4) not loaded, kldload or update your kernel");6465if (pmc_allocate(counter_name, PMC_MODE_SC, 0, 0, &pmcid, 64*1024) < 0)66err(EX_OSERR, "failed to allocate %s as a system counter in counting mode",67counter_name);6869tsc1 = rdtsc();70if (pmc_write(pmcid, 0) < 0)71err(EX_OSERR, "failed to zero counter %s", counter_name);72write_cyc = rdtsc() - tsc1;7374tsc1 = rdtsc();75if (pmc_start(pmcid) < 0)76err(EX_OSERR, "failed to start counter %s", counter_name);77start_cyc = rdtsc() - tsc1;7879tsc1 = rdtsc();80if (pmc_read(pmcid, &read_value) < 0)81err(EX_OSERR, "failed to read counter %s", counter_name);82read_cyc = rdtsc() - tsc1;8384tsc1 = rdtsc();85if (pmc_stop(pmcid) < 0)86err(EX_OSERR, "failed to stop counter %s", counter_name);87stop_cyc = rdtsc() - tsc1;8889if (pmc_rw(pmcid, 0, &read_clear_value))90err(EX_OSERR, "failed to read and zero %s", counter_name);9192if (pmc_release(pmcid) < 0)93err(EX_OSERR, "failed to release %s as a system counter in counting mode",94counter_name);9596printf("Counter %s, read value %ld, read/clear value %ld\n",97counter_name, read_value, read_clear_value);98printf("Cycles to start: %ld\tstop: %ld\tread: %ld\twrite: %ld\n",99start_cyc, stop_cyc, read_cyc, stop_cyc);100101return(0);102}103104105106