Home | History | Annotate | Line # | Download | only in arch
tprof_x86.c revision 1.4.2.2
      1  1.4.2.2  pgoyette /*	$NetBSD: tprof_x86.c,v 1.4.2.2 2018/07/28 04:38:15 pgoyette Exp $	*/
      2  1.4.2.2  pgoyette 
      3  1.4.2.2  pgoyette /*
      4  1.4.2.2  pgoyette  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.4.2.2  pgoyette  * All rights reserved.
      6  1.4.2.2  pgoyette  *
      7  1.4.2.2  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8  1.4.2.2  pgoyette  * by Maxime Villard.
      9  1.4.2.2  pgoyette  *
     10  1.4.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
     11  1.4.2.2  pgoyette  * modification, are permitted provided that the following conditions
     12  1.4.2.2  pgoyette  * are met:
     13  1.4.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14  1.4.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15  1.4.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.4.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17  1.4.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     18  1.4.2.2  pgoyette  *
     19  1.4.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.4.2.2  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.4.2.2  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.4.2.2  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.4.2.2  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.4.2.2  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.4.2.2  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.4.2.2  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.4.2.2  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.4.2.2  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.4.2.2  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30  1.4.2.2  pgoyette  */
     31  1.4.2.2  pgoyette 
     32  1.4.2.2  pgoyette #include <sys/cdefs.h>
     33  1.4.2.2  pgoyette #include <stdio.h>
     34  1.4.2.2  pgoyette #include <stdlib.h>
     35  1.4.2.2  pgoyette #include <stdbool.h>
     36  1.4.2.2  pgoyette #include <string.h>
     37  1.4.2.2  pgoyette #include <unistd.h>
     38  1.4.2.2  pgoyette #include <err.h>
     39  1.4.2.2  pgoyette #include <machine/specialreg.h>
     40  1.4.2.2  pgoyette #include <dev/tprof/tprof_ioctl.h>
     41  1.4.2.2  pgoyette #include "../tprof.h"
     42  1.4.2.2  pgoyette 
     43  1.4.2.2  pgoyette int tprof_event_init(uint32_t);
     44  1.4.2.2  pgoyette void tprof_event_list(void);
     45  1.4.2.2  pgoyette void tprof_event_lookup(const char *, struct tprof_param *);
     46  1.4.2.2  pgoyette 
     47  1.4.2.2  pgoyette struct name_to_event {
     48  1.4.2.2  pgoyette 	const char *name;
     49  1.4.2.2  pgoyette 	uint64_t event;
     50  1.4.2.2  pgoyette 	uint64_t unit;
     51  1.4.2.2  pgoyette 	bool enabled;
     52  1.4.2.2  pgoyette };
     53  1.4.2.2  pgoyette 
     54  1.4.2.2  pgoyette struct event_table {
     55  1.4.2.2  pgoyette 	const char *tablename;
     56  1.4.2.2  pgoyette 	struct name_to_event *names;
     57  1.4.2.2  pgoyette 	size_t nevents;
     58  1.4.2.2  pgoyette 	struct event_table *next;
     59  1.4.2.2  pgoyette };
     60  1.4.2.2  pgoyette 
     61  1.4.2.2  pgoyette static struct event_table *cpuevents = NULL;
     62  1.4.2.2  pgoyette 
     63  1.4.2.2  pgoyette static void x86_cpuid(unsigned int *eax, unsigned int *ebx,
     64  1.4.2.2  pgoyette     unsigned int *ecx, unsigned int *edx)
     65  1.4.2.2  pgoyette {
     66  1.4.2.2  pgoyette 	asm volatile("cpuid"
     67  1.4.2.2  pgoyette 	    : "=a" (*eax),
     68  1.4.2.2  pgoyette 	      "=b" (*ebx),
     69  1.4.2.2  pgoyette 	      "=c" (*ecx),
     70  1.4.2.2  pgoyette 	      "=d" (*edx)
     71  1.4.2.2  pgoyette 	    : "0" (*eax), "2" (*ecx));
     72  1.4.2.2  pgoyette }
     73  1.4.2.2  pgoyette 
     74  1.4.2.2  pgoyette /* -------------------------------------------------------------------------- */
     75  1.4.2.2  pgoyette 
     76  1.4.2.2  pgoyette /*
     77  1.4.2.2  pgoyette  * Intel Architectural Version 1.
     78  1.4.2.2  pgoyette  */
     79  1.4.2.2  pgoyette static struct name_to_event intel_arch1_names[] = {
     80  1.4.2.2  pgoyette 	/* Event Name - Event Select - UMask */
     81  1.4.2.2  pgoyette 	{ "unhalted-core-cycles",	0x3C, 0x00, true },
     82  1.4.2.2  pgoyette 	{ "instruction-retired",	0xC0, 0x00, true },
     83  1.4.2.2  pgoyette 	{ "unhalted-reference-cycles",	0x3C, 0x01, true },
     84  1.4.2.2  pgoyette 	{ "llc-reference",		0x2E, 0x4F, true },
     85  1.4.2.2  pgoyette 	{ "llc-misses",			0x2E, 0x41, true },
     86  1.4.2.2  pgoyette 	{ "branch-instruction-retired",	0xC4, 0x00, true },
     87  1.4.2.2  pgoyette 	{ "branch-misses-retired",	0xC5, 0x00, true },
     88  1.4.2.2  pgoyette };
     89  1.4.2.2  pgoyette 
     90  1.4.2.2  pgoyette static struct event_table intel_arch1 = {
     91  1.4.2.2  pgoyette 	.tablename = "Intel Architectural Version 1",
     92  1.4.2.2  pgoyette 	.names = intel_arch1_names,
     93  1.4.2.2  pgoyette 	.nevents = sizeof(intel_arch1_names) /
     94  1.4.2.2  pgoyette 	    sizeof(struct name_to_event),
     95  1.4.2.2  pgoyette 	.next = NULL
     96  1.4.2.2  pgoyette };
     97  1.4.2.2  pgoyette 
     98  1.4.2.2  pgoyette static struct event_table *
     99  1.4.2.2  pgoyette init_intel_arch1(void)
    100  1.4.2.2  pgoyette {
    101  1.4.2.2  pgoyette 	unsigned int eax, ebx, ecx, edx;
    102  1.4.2.2  pgoyette 	struct event_table *table;
    103  1.4.2.2  pgoyette 	size_t i;
    104  1.4.2.2  pgoyette 
    105  1.4.2.2  pgoyette 	eax = 0x0A;
    106  1.4.2.2  pgoyette 	ebx = 0;
    107  1.4.2.2  pgoyette 	ecx = 0;
    108  1.4.2.2  pgoyette 	edx = 0;
    109  1.4.2.2  pgoyette 	x86_cpuid(&eax, &ebx, &ecx, &edx);
    110  1.4.2.2  pgoyette 
    111  1.4.2.2  pgoyette 	table = &intel_arch1;
    112  1.4.2.2  pgoyette 	for (i = 0; i < table->nevents; i++) {
    113  1.4.2.2  pgoyette 		/* Disable the unsupported events. */
    114  1.4.2.2  pgoyette 		if ((ebx & (i << 1)) != 0)
    115  1.4.2.2  pgoyette 			table->names[i].enabled = false;
    116  1.4.2.2  pgoyette 	}
    117  1.4.2.2  pgoyette 
    118  1.4.2.2  pgoyette 	return table;
    119  1.4.2.2  pgoyette }
    120  1.4.2.2  pgoyette 
    121  1.4.2.2  pgoyette /*
    122  1.4.2.2  pgoyette  * Intel Skylake/Kabylake.
    123  1.4.2.2  pgoyette  *
    124  1.4.2.2  pgoyette  * The events that are not listed, because they are of little interest or
    125  1.4.2.2  pgoyette  * require extra configuration:
    126  1.4.2.2  pgoyette  *     TX_*
    127  1.4.2.2  pgoyette  *     FRONTEND_RETIRED.*
    128  1.4.2.2  pgoyette  *     FP_ARITH_INST_RETIRED.*
    129  1.4.2.2  pgoyette  *     HLE_RETIRED.*
    130  1.4.2.2  pgoyette  *     RTM_RETIRED.*
    131  1.4.2.2  pgoyette  *     MEM_TRANS_RETIRED.*
    132  1.4.2.2  pgoyette  *     UOPS_DISPATCHED_PORT.*
    133  1.4.2.2  pgoyette  */
    134  1.4.2.2  pgoyette static struct name_to_event intel_skylake_kabylake_names[] = {
    135  1.4.2.2  pgoyette 	/* Event Name - Event Select - UMask */
    136  1.4.2.2  pgoyette 	{ "LD_BLOCKS.STORE_FORWARD",					0x03, 0x02, true },
    137  1.4.2.2  pgoyette 	{ "LD_BLOCKS.NO_SR",						0x03, 0x08, true },
    138  1.4.2.2  pgoyette 	{ "LD_BLOCKS_PARTIAL.ADDRESS_ALIAS",				0x07, 0x01, true },
    139  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK",			0x08, 0x01, true },
    140  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_4K",				0x08, 0x02, true },
    141  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_2M_4M",			0x08, 0x04, true },
    142  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_1G",				0x08, 0x08, true },
    143  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED",				0x08, 0x0E, true },
    144  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.WALK_PENDING",				0x08, 0x10, true },
    145  1.4.2.2  pgoyette 	{ "DTLB_LOAD_MISSES.STLB_HIT",					0x08, 0x20, true },
    146  1.4.2.2  pgoyette 	{ "INT_MISC.RECOVERY_CYCLES",					0x0D, 0x01, true },
    147  1.4.2.2  pgoyette 	{ "INT_MISC.CLEAR_RESTEER_CYCLES",				0x0D, 0x80, true },
    148  1.4.2.2  pgoyette 	{ "UOPS_ISSUED.ANY",						0x0E, 0x01, true },
    149  1.4.2.2  pgoyette 	{ "UOPS_ISSUED.VECTOR_WIDTH_MISMATCH",				0x0E, 0x02, true },
    150  1.4.2.2  pgoyette 	{ "UOPS_ISSUED.SLOW_LEA",					0x0E, 0x20, true },
    151  1.4.2.2  pgoyette 	{ "L2_RQSTS.DEMAND_DATA_RD_MISS",				0x24, 0x21, true },
    152  1.4.2.2  pgoyette 	{ "L2_RQSTS.RFO_MISS",						0x24, 0x22, true },
    153  1.4.2.2  pgoyette 	{ "L2_RQSTS.CODE_RD_MISS",					0x24, 0x24, true },
    154  1.4.2.2  pgoyette 	{ "L2_RQSTS.ALL_DEMAND_MISS",					0x24, 0x27, true },
    155  1.4.2.2  pgoyette 	{ "L2_RQSTS.PF_MISS",						0x24, 0x38, true },
    156  1.4.2.2  pgoyette 	{ "L2_RQSTS.MISS",						0x24, 0x3F, true },
    157  1.4.2.2  pgoyette 	{ "L2_RQSTS.DEMAND_DATA_RD_HIT",				0x24, 0x41, true },
    158  1.4.2.2  pgoyette 	{ "L2_RQSTS.RFO_HIT",						0x24, 0x42, true },
    159  1.4.2.2  pgoyette 	{ "L2_RQSTS.CODE_RD_HIT",					0x24, 0x44, true },
    160  1.4.2.2  pgoyette 	{ "L2_RQSTS.PF_HIT",						0x24, 0xD8, true },
    161  1.4.2.2  pgoyette 	{ "L2_RQSTS.ALL_DEMAND_DATA_RD",				0x24, 0xE1, true },
    162  1.4.2.2  pgoyette 	{ "L2_RQSTS.ALL_RFO",						0x24, 0xE2, true },
    163  1.4.2.2  pgoyette 	{ "L2_RQSTS.ALL_CODE_RD",					0x24, 0xE4, true },
    164  1.4.2.2  pgoyette 	{ "L2_RQSTS.ALL_DEMAND_REFERENCES",				0x24, 0xE7, true },
    165  1.4.2.2  pgoyette 	{ "L2_RQSTS.ALL_PF",						0x24, 0xF8, true },
    166  1.4.2.2  pgoyette 	{ "L2_RQSTS.REFERENCES",					0x24, 0xFF, true },
    167  1.4.2.2  pgoyette 	{ "SW_PREFETCH_ACCESS.NTA",					0x32, 0x01, true },
    168  1.4.2.2  pgoyette 	{ "SW_PREFETCH_ACCESS.T0",					0x32, 0x02, true },
    169  1.4.2.2  pgoyette 	{ "SW_PREFETCH_ACCESS.T1_T2",					0x32, 0x04, true },
    170  1.4.2.2  pgoyette 	{ "SW_PREFETCH_ACCESS.PREFETCHW",				0x32, 0x08, true },
    171  1.4.2.2  pgoyette 	{ "CPU_CLK_THREAD_UNHALTED.ONE_THREAD_ACTIVE",			0x3C, 0x02, true },
    172  1.4.2.2  pgoyette 	{ "CPU_CLK_UNHALTED.ONE_THREAD_ACTIVE",				0x3C, 0x02, true },
    173  1.4.2.2  pgoyette 	{ "L1D_PEND_MISS.PENDING",					0x48, 0x01, true },
    174  1.4.2.2  pgoyette 	{ "L1D_PEND_MISS.FB_FULL",					0x48, 0x02, true },
    175  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.MISS_CAUSES_A_WALK",			0x49, 0x01, true },
    176  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_4K",			0x49, 0x02, true },
    177  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_2M_4M",			0x49, 0x04, true },
    178  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_1G",			0x49, 0x08, true },
    179  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.WALK_COMPLETED",				0x49, 0x0E, true },
    180  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.WALK_PENDING",				0x49, 0x10, true },
    181  1.4.2.2  pgoyette 	{ "DTLB_STORE_MISSES.STLB_HIT",					0x49, 0x20, true },
    182  1.4.2.2  pgoyette 	{ "LOAD_HIT_PRE.SW_PF",						0x4C, 0x01, true },
    183  1.4.2.2  pgoyette 	{ "EPT.WALK_PENDING",						0x4F, 0x10, true },
    184  1.4.2.2  pgoyette 	{ "L1D.REPLACEMENT",						0x51, 0x01, true },
    185  1.4.2.2  pgoyette 	{ "RS_EVENTS.EMPTY_CYCLES",					0x5E, 0x01, true },
    186  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS_OUTSTANDING.DEMAND_DATA_RD",		0x60, 0x01, true },
    187  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS_OUTSTANDING.DEMAND_CODE_RD",		0x60, 0x02, true },
    188  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS_OUTSTANDING.DEMAND_RFO",			0x60, 0x04, true },
    189  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS_OUTSTANDING.ALL_DATA_RD",			0x60, 0x08, true },
    190  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS_OUTSTANDING.L3_MISS_DEMAND_DATA_RD",	0x60, 0x10, true },
    191  1.4.2.2  pgoyette 	{ "IDQ.MITE_UOPS",						0x79, 0x04, true },
    192  1.4.2.2  pgoyette 	{ "IDQ.DSB_UOPS",						0x79, 0x08, true },
    193  1.4.2.2  pgoyette 	{ "IDQ.MS_MITE_UOPS",						0x79, 0x20, true },
    194  1.4.2.2  pgoyette 	{ "IDQ.MS_UOPS",						0x79, 0x30, true },
    195  1.4.2.2  pgoyette 	{ "ICACHE_16B.IFDATA_STALL",					0x80, 0x04, true },
    196  1.4.2.2  pgoyette 	{ "ICACHE_64B.IFTAG_HIT",					0x83, 0x01, true },
    197  1.4.2.2  pgoyette 	{ "ICACHE_64B.IFTAG_MISS",					0x83, 0x02, true },
    198  1.4.2.2  pgoyette 	{ "ICACHE_64B.IFTAG_STALL",					0x83, 0x04, true },
    199  1.4.2.2  pgoyette 	{ "ITLB_MISSES.MISS_CAUSES_A_WALK",				0x85, 0x01, true },
    200  1.4.2.2  pgoyette 	{ "ITLB_MISSES.WALK_COMPLETED_4K",				0x85, 0x02, true },
    201  1.4.2.2  pgoyette 	{ "ITLB_MISSES.WALK_COMPLETED_2M_4M",				0x85, 0x04, true },
    202  1.4.2.2  pgoyette 	{ "ITLB_MISSES.WALK_COMPLETED_1G",				0x85, 0x08, true },
    203  1.4.2.2  pgoyette 	{ "ITLB_MISSES.WALK_COMPLETED",					0x85, 0x0E, true },
    204  1.4.2.2  pgoyette 	{ "ITLB_MISSES.WALK_PENDING",					0x85, 0x10, true },
    205  1.4.2.2  pgoyette 	{ "ITLB_MISSES.STLB_HIT",					0x85, 0x20, true },
    206  1.4.2.2  pgoyette 	{ "ILD_STALL.LCP",						0x87, 0x01, true },
    207  1.4.2.2  pgoyette 	{ "IDQ_UOPS_NOT_DELIVERED.CORE",				0x9C, 0x01, true },
    208  1.4.2.2  pgoyette 	{ "RESOURCE_STALLS.ANY",					0xA2, 0x01, true },
    209  1.4.2.2  pgoyette 	{ "RESOURCE_STALLS.SB",						0xA2, 0x08, true },
    210  1.4.2.2  pgoyette 	{ "EXE_ACTIVITY.EXE_BOUND_0_PORTS",				0xA6, 0x01, true },
    211  1.4.2.2  pgoyette 	{ "EXE_ACTIVITY.1_PORTS_UTIL",					0xA6, 0x02, true },
    212  1.4.2.2  pgoyette 	{ "EXE_ACTIVITY.2_PORTS_UTIL",					0xA6, 0x04, true },
    213  1.4.2.2  pgoyette 	{ "EXE_ACTIVITY.3_PORTS_UTIL",					0xA6, 0x08, true },
    214  1.4.2.2  pgoyette 	{ "EXE_ACTIVITY.4_PORTS_UTIL",					0xA6, 0x10, true },
    215  1.4.2.2  pgoyette 	{ "EXE_ACTIVITY.BOUND_ON_STORES",				0xA6, 0x40, true },
    216  1.4.2.2  pgoyette 	{ "LSD.UOPS",							0xA8, 0x01, true },
    217  1.4.2.2  pgoyette 	{ "DSB2MITE_SWITCHES.PENALTY_CYCLES",				0xAB, 0x02, true },
    218  1.4.2.2  pgoyette 	{ "ITLB.ITLB_FLUSH",						0xAE, 0x01, true },
    219  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS.DEMAND_DATA_RD",				0xB0, 0x01, true },
    220  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS.DEMAND_CODE_RD",				0xB0, 0x02, true },
    221  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS.DEMAND_RFO",				0xB0, 0x04, true },
    222  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS.ALL_DATA_RD",				0xB0, 0x08, true },
    223  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS.L3_MISS_DEMAND_DATA_RD",			0xB0, 0x10, true },
    224  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS.ALL_REQUESTS",				0xB0, 0x80, true },
    225  1.4.2.2  pgoyette 	{ "UOPS_EXECUTED.THREAD",					0xB1, 0x01, true },
    226  1.4.2.2  pgoyette 	{ "UOPS_EXECUTED.CORE",						0xB1, 0x02, true },
    227  1.4.2.2  pgoyette 	{ "UOPS_EXECUTED.X87",						0xB1, 0x10, true },
    228  1.4.2.2  pgoyette 	{ "OFFCORE_REQUESTS_BUFFER.SQ_FULL",				0xB2, 0x01, true },
    229  1.4.2.2  pgoyette 	{ "TLB_FLUSH.DTLB_THREAD",					0xBD, 0x01, true },
    230  1.4.2.2  pgoyette 	{ "TLB_FLUSH.STLB_ANY",						0xBD, 0x20, true },
    231  1.4.2.2  pgoyette 	{ "INST_RETIRED.PREC_DIST",					0xC0, 0x01, true },
    232  1.4.2.2  pgoyette 	{ "OTHER_ASSISTS.ANY",						0xC1, 0x3F, true },
    233  1.4.2.2  pgoyette 	{ "UOPS_RETIRED.RETIRE_SLOTS",					0xC2, 0x02, true },
    234  1.4.2.2  pgoyette 	{ "MACHINE_CLEARS.MEMORY_ORDERING",				0xC3, 0x02, true },
    235  1.4.2.2  pgoyette 	{ "MACHINE_CLEARS.SMC",						0xC3, 0x04, true },
    236  1.4.2.2  pgoyette 	{ "BR_INST_RETIRED.CONDITIONAL",				0xC4, 0x01, true },
    237  1.4.2.2  pgoyette 	{ "BR_INST_RETIRED.NEAR_CALL",					0xC4, 0x02, true },
    238  1.4.2.2  pgoyette 	{ "BR_INST_RETIRED.NEAR_RETURN",				0xC4, 0x08, true },
    239  1.4.2.2  pgoyette 	{ "BR_INST_RETIRED.NOT_TAKEN",					0xC4, 0x10, true },
    240  1.4.2.2  pgoyette 	{ "BR_INST_RETIRED.NEAR_TAKEN",					0xC4, 0x20, true },
    241  1.4.2.2  pgoyette 	{ "BR_INST_RETIRED.FAR_BRANCH",					0xC4, 0x40, true },
    242  1.4.2.2  pgoyette 	{ "BR_MISP_RETIRED.CONDITIONAL",				0xC5, 0x01, true },
    243  1.4.2.2  pgoyette 	{ "BR_MISP_RETIRED.NEAR_CALL",					0xC5, 0x02, true },
    244  1.4.2.2  pgoyette 	{ "BR_MISP_RETIRED.NEAR_TAKEN",					0xC5, 0x20, true },
    245  1.4.2.2  pgoyette 	{ "HW_INTERRUPTS.RECEIVED",					0xCB, 0x01, true },
    246  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.STLB_MISS_LOADS",				0xD0, 0x11, true },
    247  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.STLB_MISS_STORES",				0xD0, 0x12, true },
    248  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.LOCK_LOADS",				0xD0, 0x21, true },
    249  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.SPLIT_LOADS",				0xD0, 0x41, true },
    250  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.SPLIT_STORES",				0xD0, 0x42, true },
    251  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.ALL_LOADS",					0xD0, 0x81, true },
    252  1.4.2.2  pgoyette 	{ "MEM_INST_RETIRED.ALL_STORES",				0xD0, 0x82, true },
    253  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.L1_HIT",					0xD1, 0x01, true },
    254  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.L2_HIT",					0xD1, 0x02, true },
    255  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.L3_HIT",					0xD1, 0x04, true },
    256  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.L1_MISS",					0xD1, 0x08, true },
    257  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.L2_MISS",					0xD1, 0x10, true },
    258  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.L3_MISS",					0xD1, 0x20, true },
    259  1.4.2.2  pgoyette 	{ "MEM_LOAD_RETIRED.FB_HIT",					0xD1, 0x40, true },
    260  1.4.2.2  pgoyette 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_MISS",				0xD2, 0x01, true },
    261  1.4.2.2  pgoyette 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_HIT",				0xD2, 0x02, true },
    262  1.4.2.2  pgoyette 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_HITM",				0xD2, 0x04, true },
    263  1.4.2.2  pgoyette 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_NONE",				0xD2, 0x08, true },
    264  1.4.2.2  pgoyette 	{ "MEM_LOAD_MISC_RETIRED.UC",					0xD4, 0x04, true },
    265  1.4.2.2  pgoyette 	{ "BACLEARS.ANY",						0xE6, 0x01, true },
    266  1.4.2.2  pgoyette 	{ "L2_TRANS.L2_WB",						0xF0, 0x40, true },
    267  1.4.2.2  pgoyette 	{ "L2_LINES_IN.ALL",						0xF1, 0x1F, true },
    268  1.4.2.2  pgoyette 	{ "L2_LINES_OUT.SILENT",					0xF2, 0x01, true },
    269  1.4.2.2  pgoyette 	{ "L2_LINES_OUT.NON_SILENT",					0xF2, 0x02, true },
    270  1.4.2.2  pgoyette 	{ "L2_LINES_OUT.USELESS_HWPF",					0xF2, 0x04, true },
    271  1.4.2.2  pgoyette 	{ "SQ_MISC.SPLIT_LOCK",						0xF4, 0x10, true },
    272  1.4.2.2  pgoyette };
    273  1.4.2.2  pgoyette 
    274  1.4.2.2  pgoyette static struct event_table intel_skylake_kabylake = {
    275  1.4.2.2  pgoyette 	.tablename = "Intel Skylake/Kabylake",
    276  1.4.2.2  pgoyette 	.names = intel_skylake_kabylake_names,
    277  1.4.2.2  pgoyette 	.nevents = sizeof(intel_skylake_kabylake_names) /
    278  1.4.2.2  pgoyette 	    sizeof(struct name_to_event),
    279  1.4.2.2  pgoyette 	.next = NULL
    280  1.4.2.2  pgoyette };
    281  1.4.2.2  pgoyette 
    282  1.4.2.2  pgoyette static struct event_table *
    283  1.4.2.2  pgoyette init_intel_skylake_kabylake(void)
    284  1.4.2.2  pgoyette {
    285  1.4.2.2  pgoyette 	return &intel_skylake_kabylake;
    286  1.4.2.2  pgoyette }
    287  1.4.2.2  pgoyette 
    288  1.4.2.2  pgoyette static struct event_table *
    289  1.4.2.2  pgoyette init_intel_generic(void)
    290  1.4.2.2  pgoyette {
    291  1.4.2.2  pgoyette 	unsigned int eax, ebx, ecx, edx;
    292  1.4.2.2  pgoyette 	struct event_table *table;
    293  1.4.2.2  pgoyette 
    294  1.4.2.2  pgoyette 	/*
    295  1.4.2.2  pgoyette 	 * The kernel made sure the Architectural Version 1 PMCs were
    296  1.4.2.2  pgoyette 	 * present.
    297  1.4.2.2  pgoyette 	 */
    298  1.4.2.2  pgoyette 	table = init_intel_arch1();
    299  1.4.2.2  pgoyette 
    300  1.4.2.2  pgoyette 	/*
    301  1.4.2.2  pgoyette 	 * Now query the additional (non-architectural) events. They
    302  1.4.2.2  pgoyette 	 * depend on the CPU model.
    303  1.4.2.2  pgoyette 	 */
    304  1.4.2.2  pgoyette 	eax = 0x01;
    305  1.4.2.2  pgoyette 	ebx = 0;
    306  1.4.2.2  pgoyette 	ecx = 0;
    307  1.4.2.2  pgoyette 	edx = 0;
    308  1.4.2.2  pgoyette 	x86_cpuid(&eax, &ebx, &ecx, &edx);
    309  1.4.2.2  pgoyette 
    310  1.4.2.2  pgoyette 	if (CPUID_TO_FAMILY(eax) == 6) {
    311  1.4.2.2  pgoyette 		switch (CPUID_TO_MODEL(eax)) {
    312  1.4.2.2  pgoyette 		case 0x4E: /* Skylake */
    313  1.4.2.2  pgoyette 		case 0x5E: /* Skylake */
    314  1.4.2.2  pgoyette 		case 0x8E: /* Kabylake */
    315  1.4.2.2  pgoyette 		case 0x9E: /* Kabylake */
    316  1.4.2.2  pgoyette 			table->next = init_intel_skylake_kabylake();
    317  1.4.2.2  pgoyette 			break;
    318  1.4.2.2  pgoyette 		}
    319  1.4.2.2  pgoyette 	}
    320  1.4.2.2  pgoyette 
    321  1.4.2.2  pgoyette 	return table;
    322  1.4.2.2  pgoyette }
    323  1.4.2.2  pgoyette 
    324  1.4.2.2  pgoyette /* -------------------------------------------------------------------------- */
    325  1.4.2.2  pgoyette 
    326  1.4.2.2  pgoyette /*
    327  1.4.2.2  pgoyette  * AMD Family 10h
    328  1.4.2.2  pgoyette  */
    329  1.4.2.2  pgoyette static struct name_to_event amd_f10h_names[] = {
    330  1.4.2.2  pgoyette 	{ "seg-load-all",		0x20, 0x7f, true },
    331  1.4.2.2  pgoyette 	{ "seg-load-es",		0x20, 0x01, true },
    332  1.4.2.2  pgoyette 	{ "seg-load-cs",		0x20, 0x02, true },
    333  1.4.2.2  pgoyette 	{ "seg-load-ss",		0x20, 0x04, true },
    334  1.4.2.2  pgoyette 	{ "seg-load-ds",		0x20, 0x08, true },
    335  1.4.2.2  pgoyette 	{ "seg-load-fs",		0x20, 0x10, true },
    336  1.4.2.2  pgoyette 	{ "seg-load-gs",		0x20, 0x20, true },
    337  1.4.2.2  pgoyette 	{ "seg-load-hs",		0x20, 0x40, true },
    338  1.4.2.2  pgoyette 	{ "l1cache-access",		0x40, 0x00, true },
    339  1.4.2.2  pgoyette 	{ "l1cache-miss",		0x41, 0x00, true },
    340  1.4.2.2  pgoyette 	{ "l1cache-refill",		0x42, 0x1f, true },
    341  1.4.2.2  pgoyette 	{ "l1cache-refill-invalid",	0x42, 0x01, true },
    342  1.4.2.2  pgoyette 	{ "l1cache-refill-shared",	0x42, 0x02, true },
    343  1.4.2.2  pgoyette 	{ "l1cache-refill-exclusive",	0x42, 0x04, true },
    344  1.4.2.2  pgoyette 	{ "l1cache-refill-owner",	0x42, 0x08, true },
    345  1.4.2.2  pgoyette 	{ "l1cache-refill-modified",	0x42, 0x10, true },
    346  1.4.2.2  pgoyette 	{ "l1cache-load",		0x43, 0x1f, true },
    347  1.4.2.2  pgoyette 	{ "l1cache-load-invalid",	0x43, 0x01, true },
    348  1.4.2.2  pgoyette 	{ "l1cache-load-shared",	0x43, 0x02, true },
    349  1.4.2.2  pgoyette 	{ "l1cache-load-exclusive",	0x43, 0x04, true },
    350  1.4.2.2  pgoyette 	{ "l1cache-load-owner",		0x43, 0x08, true },
    351  1.4.2.2  pgoyette 	{ "l1cache-load-modified",	0x43, 0x10, true },
    352  1.4.2.2  pgoyette 	{ "l1cache-writeback",		0x44, 0x1f, true },
    353  1.4.2.2  pgoyette 	{ "l1cache-writeback-invalid",	0x44, 0x01, true },
    354  1.4.2.2  pgoyette 	{ "l1cache-writeback-shared",	0x44, 0x02, true },
    355  1.4.2.2  pgoyette 	{ "l1cache-writeback-exclusive",0x44, 0x04, true },
    356  1.4.2.2  pgoyette 	{ "l1cache-writeback-owner",	0x44, 0x08, true },
    357  1.4.2.2  pgoyette 	{ "l1cache-writeback-modified",	0x44, 0x10, true },
    358  1.4.2.2  pgoyette 	{ "l1DTLB-hit-all",		0x4D, 0x07, true },
    359  1.4.2.2  pgoyette 	{ "l1DTLB-hit-4Kpage",		0x4D, 0x01, true },
    360  1.4.2.2  pgoyette 	{ "l1DTLB-hit-2Mpage",		0x4D, 0x02, true },
    361  1.4.2.2  pgoyette 	{ "l1DTLB-hit-1Gpage",		0x4D, 0x04, true },
    362  1.4.2.2  pgoyette 	{ "l1DTLB-miss-all",		0x45, 0x07, true },
    363  1.4.2.2  pgoyette 	{ "l1DTLB-miss-4Kpage",		0x45, 0x01, true },
    364  1.4.2.2  pgoyette 	{ "l1DTLB-miss-2Mpage",		0x45, 0x02, true },
    365  1.4.2.2  pgoyette 	{ "l1DTLB-miss-1Gpage",		0x45, 0x04, true },
    366  1.4.2.2  pgoyette 	{ "l2DTLB-miss-all",		0x46, 0x03, true },
    367  1.4.2.2  pgoyette 	{ "l2DTLB-miss-4Kpage",		0x46, 0x01, true },
    368  1.4.2.2  pgoyette 	{ "l2DTLB-miss-2Mpage",		0x46, 0x02, true },
    369  1.4.2.2  pgoyette 	/* l2DTLB-miss-1Gpage: reserved on some revisions, so disabled */
    370  1.4.2.2  pgoyette 	{ "l1ITLB-miss",		0x84, 0x00, true },
    371  1.4.2.2  pgoyette 	{ "l2ITLB-miss-all",		0x85, 0x03, true },
    372  1.4.2.2  pgoyette 	{ "l2ITLB-miss-4Kpage",		0x85, 0x01, true },
    373  1.4.2.2  pgoyette 	{ "l2ITLB-miss-2Mpage",		0x85, 0x02, true },
    374  1.4.2.2  pgoyette 	{ "mem-misalign-ref",		0x47, 0x00, true },
    375  1.4.2.2  pgoyette 	{ "ins-fetch",			0x80, 0x00, true },
    376  1.4.2.2  pgoyette 	{ "ins-fetch-miss",		0x81, 0x00, true },
    377  1.4.2.2  pgoyette 	{ "ins-refill-l2",		0x82, 0x00, true },
    378  1.4.2.2  pgoyette 	{ "ins-refill-sys",		0x83, 0x00, true },
    379  1.4.2.2  pgoyette 	{ "ins-fetch-stall",		0x87, 0x00, true },
    380  1.4.2.2  pgoyette 	{ "ins-retired",		0xC0, 0x00, true },
    381  1.4.2.2  pgoyette 	{ "ins-empty",			0xD0, 0x00, true },
    382  1.4.2.2  pgoyette 	{ "ops-retired",		0xC1, 0x00, true },
    383  1.4.2.2  pgoyette 	{ "branch-retired",		0xC2, 0x00, true },
    384  1.4.2.2  pgoyette 	{ "branch-miss-retired",	0xC3, 0x00, true },
    385  1.4.2.2  pgoyette 	{ "branch-taken-retired",	0xC4, 0x00, true },
    386  1.4.2.2  pgoyette 	{ "branch-taken-miss-retired",	0xC5, 0x00, true },
    387  1.4.2.2  pgoyette 	{ "branch-far-retired",		0xC6, 0x00, true },
    388  1.4.2.2  pgoyette 	{ "branch-resync-retired",	0xC7, 0x00, true },
    389  1.4.2.2  pgoyette 	{ "branch-near-retired",	0xC8, 0x00, true },
    390  1.4.2.2  pgoyette 	{ "branch-near-miss-retired",	0xC9, 0x00, true },
    391  1.4.2.2  pgoyette 	{ "branch-indirect-miss-retired", 0xCA, 0x00, true },
    392  1.4.2.2  pgoyette 	{ "int-hw",			0xCF, 0x00, true },
    393  1.4.2.2  pgoyette 	{ "int-cycles-masked",		0xCD, 0x00, true },
    394  1.4.2.2  pgoyette 	{ "int-cycles-masked-pending",	0xCE, 0x00, true },
    395  1.4.2.2  pgoyette 	{ "fpu-exceptions",		0xDB, 0x00, true },
    396  1.4.2.2  pgoyette 	{ "break-match0",		0xDC, 0x00, true },
    397  1.4.2.2  pgoyette 	{ "break-match1",		0xDD, 0x00, true },
    398  1.4.2.2  pgoyette 	{ "break-match2",		0xDE, 0x00, true },
    399  1.4.2.2  pgoyette 	{ "break-match3",		0xDF, 0x00, true },
    400  1.4.2.2  pgoyette };
    401  1.4.2.2  pgoyette 
    402  1.4.2.2  pgoyette static struct event_table amd_f10h = {
    403  1.4.2.2  pgoyette 	.tablename = "AMD Family 10h",
    404  1.4.2.2  pgoyette 	.names = amd_f10h_names,
    405  1.4.2.2  pgoyette 	.nevents = sizeof(amd_f10h_names) /
    406  1.4.2.2  pgoyette 	    sizeof(struct name_to_event),
    407  1.4.2.2  pgoyette 	.next = NULL
    408  1.4.2.2  pgoyette };
    409  1.4.2.2  pgoyette 
    410  1.4.2.2  pgoyette static struct event_table *
    411  1.4.2.2  pgoyette init_amd_f10h(void)
    412  1.4.2.2  pgoyette {
    413  1.4.2.2  pgoyette 	return &amd_f10h;
    414  1.4.2.2  pgoyette }
    415  1.4.2.2  pgoyette 
    416  1.4.2.2  pgoyette static struct event_table *
    417  1.4.2.2  pgoyette init_amd_generic(void)
    418  1.4.2.2  pgoyette {
    419  1.4.2.2  pgoyette 	unsigned int eax, ebx, ecx, edx;
    420  1.4.2.2  pgoyette 
    421  1.4.2.2  pgoyette 	eax = 0x01;
    422  1.4.2.2  pgoyette 	ebx = 0;
    423  1.4.2.2  pgoyette 	ecx = 0;
    424  1.4.2.2  pgoyette 	edx = 0;
    425  1.4.2.2  pgoyette 	x86_cpuid(&eax, &ebx, &ecx, &edx);
    426  1.4.2.2  pgoyette 
    427  1.4.2.2  pgoyette 	switch (CPUID_TO_FAMILY(eax)) {
    428  1.4.2.2  pgoyette 	case 0x10:
    429  1.4.2.2  pgoyette 		return init_amd_f10h();
    430  1.4.2.2  pgoyette 	}
    431  1.4.2.2  pgoyette 
    432  1.4.2.2  pgoyette 	return NULL;
    433  1.4.2.2  pgoyette }
    434  1.4.2.2  pgoyette 
    435  1.4.2.2  pgoyette /* -------------------------------------------------------------------------- */
    436  1.4.2.2  pgoyette 
    437  1.4.2.2  pgoyette int
    438  1.4.2.2  pgoyette tprof_event_init(uint32_t ident)
    439  1.4.2.2  pgoyette {
    440  1.4.2.2  pgoyette 	switch (ident) {
    441  1.4.2.2  pgoyette 	case TPROF_IDENT_NONE:
    442  1.4.2.2  pgoyette 		return -1;
    443  1.4.2.2  pgoyette 	case TPROF_IDENT_INTEL_GENERIC:
    444  1.4.2.2  pgoyette 		cpuevents = init_intel_generic();
    445  1.4.2.2  pgoyette 		break;
    446  1.4.2.2  pgoyette 	case TPROF_IDENT_AMD_GENERIC:
    447  1.4.2.2  pgoyette 		cpuevents = init_amd_generic();
    448  1.4.2.2  pgoyette 		break;
    449  1.4.2.2  pgoyette 	}
    450  1.4.2.2  pgoyette 	return (cpuevents == NULL) ? -1 : 0;
    451  1.4.2.2  pgoyette }
    452  1.4.2.2  pgoyette 
    453  1.4.2.2  pgoyette static void
    454  1.4.2.2  pgoyette recursive_event_list(struct event_table *table)
    455  1.4.2.2  pgoyette {
    456  1.4.2.2  pgoyette 	size_t i;
    457  1.4.2.2  pgoyette 
    458  1.4.2.2  pgoyette 	printf("%s:\n", table->tablename);
    459  1.4.2.2  pgoyette 	for (i = 0; i < table->nevents; i++) {
    460  1.4.2.2  pgoyette 		if (!table->names[i].enabled)
    461  1.4.2.2  pgoyette 			continue;
    462  1.4.2.2  pgoyette 		printf("\t%s\n", table->names[i].name);
    463  1.4.2.2  pgoyette 	}
    464  1.4.2.2  pgoyette 
    465  1.4.2.2  pgoyette 	if (table->next != NULL) {
    466  1.4.2.2  pgoyette 		recursive_event_list(table->next);
    467  1.4.2.2  pgoyette 	}
    468  1.4.2.2  pgoyette }
    469  1.4.2.2  pgoyette 
    470  1.4.2.2  pgoyette void
    471  1.4.2.2  pgoyette tprof_event_list(void)
    472  1.4.2.2  pgoyette {
    473  1.4.2.2  pgoyette 	recursive_event_list(cpuevents);
    474  1.4.2.2  pgoyette }
    475  1.4.2.2  pgoyette 
    476  1.4.2.2  pgoyette static void
    477  1.4.2.2  pgoyette recursive_event_lookup(struct event_table *table, const char *name,
    478  1.4.2.2  pgoyette     struct tprof_param *param)
    479  1.4.2.2  pgoyette {
    480  1.4.2.2  pgoyette 	size_t i;
    481  1.4.2.2  pgoyette 
    482  1.4.2.2  pgoyette 	for (i = 0; i < table->nevents; i++) {
    483  1.4.2.2  pgoyette 		if (!table->names[i].enabled)
    484  1.4.2.2  pgoyette 			continue;
    485  1.4.2.2  pgoyette 		if (!strcmp(table->names[i].name, name)) {
    486  1.4.2.2  pgoyette 			param->p_event = table->names[i].event;
    487  1.4.2.2  pgoyette 			param->p_unit = table->names[i].unit;
    488  1.4.2.2  pgoyette 			return;
    489  1.4.2.2  pgoyette 		}
    490  1.4.2.2  pgoyette 	}
    491  1.4.2.2  pgoyette 
    492  1.4.2.2  pgoyette 	if (table->next != NULL) {
    493  1.4.2.2  pgoyette 		recursive_event_lookup(table->next, name, param);
    494  1.4.2.2  pgoyette 	} else {
    495  1.4.2.2  pgoyette 		errx(EXIT_FAILURE, "event '%s' unknown", name);
    496  1.4.2.2  pgoyette 	}
    497  1.4.2.2  pgoyette }
    498  1.4.2.2  pgoyette 
    499  1.4.2.2  pgoyette void
    500  1.4.2.2  pgoyette tprof_event_lookup(const char *name, struct tprof_param *param)
    501  1.4.2.2  pgoyette {
    502  1.4.2.2  pgoyette 	recursive_event_lookup(cpuevents, name, param);
    503  1.4.2.2  pgoyette }
    504