Blame src/libpfm4/lib/pfmlib_intel_rapl.c

Packit 577717
/*
Packit 577717
 * pfmlib_intel_rapl.c : Intel RAPL PMU
Packit 577717
 *
Packit 577717
 * Copyright (c) 2013 Google, Inc
Packit 577717
 * Contributed by Stephane Eranian <eranian@gmail.com>
Packit 577717
 *
Packit 577717
 * Based on:
Packit 577717
 * Copyright (c) 2006 Hewlett-Packard Development Company, L.P.
Packit 577717
 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
Packit 577717
 *
Packit 577717
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 577717
 * of this software and associated documentation files (the "Software"), to deal
Packit 577717
 * in the Software without restriction, including without limitation the rights
Packit 577717
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 577717
 * of the Software, and to permit persons to whom the Software is furnished to do so,
Packit 577717
 * subject to the following conditions:
Packit 577717
 *
Packit 577717
 * The above copyright notice and this permission notice shall be included in all
Packit 577717
 * copies or substantial portions of the Software.
Packit 577717
 *
Packit 577717
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Packit 577717
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
Packit 577717
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 577717
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Packit 577717
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
Packit 577717
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 577717
 *
Packit 577717
 * RAPL PMU (SNB, IVB, HSW)
Packit 577717
 */
Packit 577717
Packit 577717
/* private headers */
Packit 577717
#include "pfmlib_priv.h"
Packit 577717
/*
Packit 577717
 * for now, we reuse the x86 table entry format and callback to avoid duplicating
Packit 577717
 * code. We may revisit this later on
Packit 577717
 */
Packit 577717
#include "pfmlib_intel_x86_priv.h"
Packit 577717
Packit 577717
extern pfmlib_pmu_t intel_rapl_support;
Packit 577717
Packit 577717
#define RAPL_COMMON_EVENTS \
Packit 577717
  { .name   = "RAPL_ENERGY_CORES",\
Packit 577717
    .desc   = "Number of Joules consumed by all cores on the package. Unit is 2^-32 Joules",\
Packit 577717
    .cntmsk = 0x1,\
Packit 577717
    .code   = 0x1,\
Packit 577717
  },\
Packit 577717
  { .name   = "RAPL_ENERGY_PKG",\
Packit 577717
    .desc   = "Number of Joules consumed by all cores and Last level cache on the package. Unit is 2^-32 Joules",\
Packit 577717
    .cntmsk = 0x2,\
Packit 577717
    .code   = 0x2,\
Packit 577717
  }
Packit 577717
Packit 577717
static const intel_x86_entry_t intel_rapl_cln_pe[]={
Packit 577717
  RAPL_COMMON_EVENTS,
Packit 577717
  { .name   = "RAPL_ENERGY_GPU",
Packit 577717
    .desc   = "Number of Joules consumed by the builtin GPU. Unit is 2^-32 Joules",
Packit 577717
    .cntmsk = 0x8,
Packit 577717
    .code   = 0x4,
Packit 577717
  }
Packit 577717
};
Packit 577717
Packit 577717
static const intel_x86_entry_t intel_rapl_skl_cln_pe[]={
Packit 577717
  RAPL_COMMON_EVENTS,
Packit 577717
  { .name   = "RAPL_ENERGY_GPU",
Packit 577717
    .desc   = "Number of Joules consumed by the builtin GPU. Unit is 2^-32 Joules",
Packit 577717
    .cntmsk = 0x8,
Packit 577717
    .code   = 0x4,
Packit 577717
  },
Packit 577717
  { .name   = "RAPL_ENERGY_PSYS",
Packit 577717
    .desc   = "Number of Joules consumed by the builtin PSYS. Unit is 2^-32 Joules",
Packit 577717
    .cntmsk = 0x8,
Packit 577717
    .code   = 0x5,
Packit 577717
  }
Packit 577717
};
Packit 577717
Packit 577717
static const intel_x86_entry_t intel_rapl_srv_pe[]={
Packit 577717
  RAPL_COMMON_EVENTS,
Packit 577717
  { .name   = "RAPL_ENERGY_DRAM",
Packit 577717
    .desc   = "Number of Joules consumed by the DRAM. Unit is 2^-32 Joules",
Packit 577717
    .cntmsk = 0x4,
Packit 577717
    .code   = 0x3,
Packit 577717
  },
Packit 577717
};
Packit 577717
Packit 577717
static const intel_x86_entry_t intel_rapl_hswep_pe[]={
Packit 577717
/*
Packit 577717
 * RAPL_ENERGY_CORES not supported in HSW-EP
Packit 577717
 */
Packit 577717
  { .name   = "RAPL_ENERGY_PKG",
Packit 577717
    .desc   = "Number of Joules consumed by all cores and Last level cache on the package. Unit is 2^-32 Joules",
Packit 577717
    .cntmsk = 0x2,
Packit 577717
    .code   = 0x2,
Packit 577717
  },
Packit 577717
  { .name   = "RAPL_ENERGY_DRAM",
Packit 577717
    .desc   = "Number of Joules consumed by the DRAM. Unit is 2^-32 Joules",
Packit 577717
    .cntmsk = 0x4,
Packit 577717
    .code   = 0x3,
Packit 577717
  },
Packit 577717
};
Packit 577717
Packit 577717
static int
Packit 577717
pfm_rapl_detect(void *this)
Packit 577717
{
Packit 577717
	int ret;
Packit 577717
Packit 577717
	ret = pfm_intel_x86_detect();
Packit 577717
	if (ret != PFM_SUCCESS)
Packit 577717
		return ret;
Packit 577717
Packit 577717
	if (pfm_intel_x86_cfg.family != 6)
Packit 577717
		return PFM_ERR_NOTSUPP;
Packit 577717
Packit 577717
	switch(pfm_intel_x86_cfg.model) {
Packit 577717
		case  42: /* Sandy Bridge */
Packit 577717
		case  58: /* Ivy Bridge */
Packit 577717
		case  60: /* Haswell */
Packit 577717
		case  69: /* Haswell */
Packit 577717
		case  70: /* Haswell */
Packit 577717
		case  61: /* Broadwell */
Packit 577717
		case  71: /* Broadwell GT3E */
Packit 577717
		case  92: /* Goldmont */
Packit 577717
			 /* already setup by default */
Packit 577717
			  break;
Packit 577717
		case  45: /* Sandy Bridg-EP  */
Packit 577717
		case  62: /* Ivy Bridge-EP  */
Packit 577717
			intel_rapl_support.pe 	     = intel_rapl_srv_pe;
Packit 577717
			intel_rapl_support.pme_count = LIBPFM_ARRAY_SIZE(intel_rapl_srv_pe);
Packit 577717
			break;
Packit 577717
		case  78: /* Skylake */
Packit 577717
		case  94: /* Skylake H/S */
Packit 577717
		case 142: /* Kabylake */
Packit 577717
		case 158: /* Kabylake */
Packit 577717
			intel_rapl_support.pe 	     = intel_rapl_skl_cln_pe;
Packit 577717
			intel_rapl_support.pme_count = LIBPFM_ARRAY_SIZE(intel_rapl_skl_cln_pe);
Packit 577717
			break;
Packit 577717
		case  63: /* Haswell-EP  */
Packit 577717
		case  79: /* Broadwell-EP */
Packit 577717
		case  86: /* Broadwell D */
Packit 577717
		case  85: /* Skylake X */
Packit 577717
			intel_rapl_support.pe 	     = intel_rapl_hswep_pe;
Packit 577717
			intel_rapl_support.pme_count = LIBPFM_ARRAY_SIZE(intel_rapl_hswep_pe);
Packit 577717
			break;
Packit 577717
		default :
Packit 577717
			return PFM_ERR_NOTSUPP;
Packit 577717
	}
Packit 577717
	return PFM_SUCCESS;
Packit 577717
}
Packit 577717
Packit 577717
static int
Packit 577717
pfm_intel_rapl_get_encoding(void *this, pfmlib_event_desc_t *e)
Packit 577717
Packit 577717
{
Packit 577717
	const intel_x86_entry_t *pe;
Packit 577717
Packit 577717
	pe = this_pe(this);
Packit 577717
Packit 577717
	e->fstr[0] = '\0';
Packit 577717
Packit 577717
	e->codes[0] = pe[e->event].code;
Packit 577717
	e->count = 1;
Packit 577717
	evt_strcat(e->fstr, "%s", pe[e->event].name);
Packit 577717
Packit 577717
	__pfm_vbprintf("[0x%"PRIx64" event=0x%x] %s\n",
Packit 577717
		       e->codes[0],
Packit 577717
		       e->codes[0], e->fstr);
Packit 577717
Packit 577717
	return PFM_SUCCESS;
Packit 577717
}
Packit 577717
Packit 577717
/*
Packit 577717
 * number modifiers for RAPL
Packit 577717
 * define an empty modifier to avoid firing the
Packit 577717
 * sanity pfm_intel_x86_validate_table(). We are
Packit 577717
 * using this function to avoid duplicating code.
Packit 577717
 */
Packit 577717
static const pfmlib_attr_desc_t rapl_mods[]=
Packit 577717
{};
Packit 577717
Packit 577717
pfmlib_pmu_t intel_rapl_support={
Packit 577717
	.desc			= "Intel RAPL",
Packit 577717
	.name			= "rapl",
Packit 577717
	.perf_name		= "power",
Packit 577717
	.pmu			= PFM_PMU_INTEL_RAPL,
Packit 577717
	.pme_count		= LIBPFM_ARRAY_SIZE(intel_rapl_cln_pe),
Packit 577717
	.type			= PFM_PMU_TYPE_UNCORE,
Packit 577717
	.num_cntrs		= 0,
Packit 577717
	.num_fixed_cntrs	= 3,
Packit 577717
	.max_encoding		= 1,
Packit 577717
	.pe			= intel_rapl_cln_pe, /* default, maybe updated */
Packit 577717
	.pmu_detect		= pfm_rapl_detect,
Packit 577717
	.atdesc			= rapl_mods,
Packit 577717
Packit 577717
	.get_event_encoding[PFM_OS_NONE] = pfm_intel_rapl_get_encoding,
Packit 577717
	 PFMLIB_ENCODE_PERF(pfm_intel_x86_get_perf_encoding),
Packit 577717
	 PFMLIB_OS_DETECT(pfm_intel_x86_perf_detect), \
Packit 577717
	.get_event_first	= pfm_intel_x86_get_event_first,
Packit 577717
	.get_event_next		= pfm_intel_x86_get_event_next,
Packit 577717
	.event_is_valid		= pfm_intel_x86_event_is_valid,
Packit 577717
	.validate_table		= pfm_intel_x86_validate_table,
Packit 577717
	.get_event_info		= pfm_intel_x86_get_event_info,
Packit 577717
	.get_event_attr_info	= pfm_intel_x86_get_event_attr_info,
Packit 577717
	 PFMLIB_VALID_PERF_PATTRS(pfm_intel_x86_perf_validate_pattrs),
Packit 577717
	.get_event_nattrs	= pfm_intel_x86_get_event_nattrs,
Packit 577717
};