Processor Counter Monitor
Classes | Macros | Functions | Variables
cpucounters.cpp File Reference

The bulk of PCM implementation. More...

#include <stdio.h>
#include <assert.h>
#include "cpucounters.h"
#include "msr.h"
#include "pci.h"
#include "types.h"
#include "utils.h"
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <string.h>
#include <limits>
#include <map>
#include <algorithm>
#include <thread>
#include <future>
#include <functional>
#include <queue>
#include <condition_variable>
#include <mutex>
#include <atomic>

Classes

class  InstanceLock
 
class  TemporalThreadAffinity
 
union  PCM_CPUID_INFO
 
class  CoreTaskQueue
 

Macros

#define PCM_INSTANCE_LOCK_SEMAPHORE_NAME   "PCM inst lock"
 
#define PCM_NUM_INSTANCES_SEMAPHORE_NAME   "Num PCM insts"
 
#define PCM_PARAM_PROTECT(...)   __VA_ARGS__
 
#define PCM_CSTATE_ARRAY(array_, val)
 
#define SAFE_SYSCTLBYNAME(message, ret_value)
 
#define CPUCNT_INIT_THE_REST_OF_EVTCNT
 
#define PCM_PCICFG_MC_INIT(controller, channel, arch)
 
#define PCM_PCICFG_QPI_INIT(port, arch)
 
#define PCM_PCICFG_EDC_INIT(controller, clock, arch)
 
#define PCM_PCICFG_M2M_INIT(x, arch)
 
#define PCM_MEM_CAPACITY   (1024ULL*1024ULL*64ULL)
 

Functions

uint32 build_bit_ui (uint32 beg, uint32 end)
 
uint32 extract_bits_ui (uint32 myin, uint32 beg, uint32 end)
 
uint64 build_bit (uint32 beg, uint32 end)
 
uint64 extract_bits (uint64 myin, uint32 beg, uint32 end)
 
int32 extractThermalHeadroom (uint64 val)
 
uint64 get_frequency_from_cpuid ()
 
void pcm_cpuid (int leaf, PCM_CPUID_INFO &info)
 
void pcm_cpuid (const unsigned leaf, const unsigned subleaf, PCM_CPUID_INFO &info)
 
uint64 RDTSC ()
 
SystemCounterState getSystemCounterState ()
 Reads the counter state of the system. More...
 
SocketCounterState getSocketCounterState (uint32 socket)
 Reads the counter state of a socket. More...
 
CoreCounterState getCoreCounterState (uint32 core)
 Reads the counter state of a (logical) core. More...
 
void print_mcfg (const char *path)
 
int getBusFromSocket (const uint32 socket)
 

Variables

pthread_mutex_t processIntanceMutex = PTHREAD_MUTEX_INITIALIZER
 

Detailed Description

The bulk of PCM implementation.

Macro Definition Documentation

#define CPUCNT_INIT_THE_REST_OF_EVTCNT
Value:
unc_event_select_reg.fields.occ_ctr_rst = 1; \
unc_event_select_reg.fields.edge = 0; \
unc_event_select_reg.fields.enable_pmi = 0; \
unc_event_select_reg.fields.enable = 1; \
unc_event_select_reg.fields.invert = 0; \
unc_event_select_reg.fields.cmask = 0;
#define PCM_CSTATE_ARRAY (   array_,
  val 
)
Value:
{ \
static uint64 tmp[] = val; \
PCM_COMPILE_ASSERT(sizeof(tmp) / sizeof(uint64) == (static_cast<int>(MAX_C_STATE)+1)); \
array_ = tmp; \
break; \
}
#define PCM_PCICFG_EDC_INIT (   controller,
  clock,
  arch 
)
Value:
EDCRegisterLocation.resize(controller + 1); \
EDCRegisterLocation[controller] = std::make_pair(arch##_EDC##controller##_##clock##_REGISTER_DEV_ADDR, arch##_EDC##controller##_##clock##_REGISTER_FUNC_ADDR);
#define PCM_PCICFG_M2M_INIT (   x,
  arch 
)
Value:
M2MRegisterLocation.resize(x + 1); \
M2MRegisterLocation[x] = std::make_pair(arch##_M2M_##x##_REGISTER_DEV_ADDR, arch##_M2M_##x##_REGISTER_FUNC_ADDR);
#define PCM_PCICFG_MC_INIT (   controller,
  channel,
  arch 
)
Value:
MCRegisterLocation.resize(controller + 1); \
MCRegisterLocation[controller].resize(channel + 1); \
MCRegisterLocation[controller][channel] = \
std::make_pair(arch##_MC##controller##_CH##channel##_REGISTER_DEV_ADDR, arch##_MC##controller##_CH##channel##_REGISTER_FUNC_ADDR);
#define PCM_PCICFG_QPI_INIT (   port,
  arch 
)
Value:
XPIRegisterLocation.resize(port + 1); \
XPIRegisterLocation[port] = std::make_pair(arch##_QPI_PORT##port##_REGISTER_DEV_ADDR, arch##_QPI_PORT##port##_REGISTER_FUNC_ADDR);
#define SAFE_SYSCTLBYNAME (   message,
  ret_value 
)
Value:
{ \
size_t size; \
char *pParam; \
if(0 != sysctlbyname(message, NULL, &size, NULL, 0)) \
{ \
std::cerr << "Unable to determine size of " << message << " sysctl return type." << std::endl; \
return false; \
} \
if(NULL == (pParam = (char *)malloc(size))) \
{ \
std::cerr << "Unable to allocate memory for " << message << std::endl; \
return false; \
} \
if(0 != sysctlbyname(message, (void*)pParam, &size, NULL, 0)) \
{ \
std::cerr << "Unable to get " << message << " from sysctl." << std::endl; \
return false; \
} \
ret_value = convertUnknownToInt(size, pParam); \
free(pParam); \
}

Function Documentation

CoreCounterState getCoreCounterState ( uint32  core)

Reads the counter state of a (logical) core.

Helper function. Uses PCM object to access counters.

Parameters
corecore id
Returns
State of counters in the core

References PCM::getCoreCounterState(), and PCM::getInstance().

SocketCounterState getSocketCounterState ( uint32  socket)

Reads the counter state of a socket.

Helper function. Uses PCM object to access counters.

Parameters
socketsocket id
Returns
State of counters in the socket

References PCM::getInstance(), and PCM::getSocketCounterState().

SystemCounterState getSystemCounterState ( )

Reads the counter state of the system.

Helper function. Uses PCM object to access counters.

System consists of several sockets (CPUs). Socket has a CPU in it. Socket (CPU) consists of several (logical) cores.

Returns
State of counters in the entire system

References PCM::getInstance(), and PCM::getSystemCounterState().