libmsr  0.3.0
A friendlier interface to accessing MSRs on Intel platforms
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
Macros | Functions
msr_core.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <linux/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "msr_core.h"
#include "memhdlr.h"
#include "msr_counters.h"
#include "cpuid.h"
#include "libmsr_error.h"
#include "libmsr_debug.h"
Include dependency graph for msr_core.c:

Macros

#define _XOPEN_SOURCE   500
 

Functions

uint64_t num_cores (void)
 Retrieve the number of cores existing on the platform.
 
uint64_t num_sockets (void)
 Retrieve the number of sockets existing on the platform.
 
uint64_t num_devs (void)
 Retrieve total number of logical processors existing on the platform.
 
uint64_t cores_per_socket (void)
 Retrieve number of cores per socket existing on the platform.
 
int allocate_batch (int batchnum, size_t bsize)
 Allocate space for new batch operation.
 
int free_batch (int batchnum)
 Deallocate memory for specific set of batch operations.
 
int create_batch_op (off_t msr, uint64_t cpu, uint64_t **dest, const int batchnum)
 Create new batch operation.
 
void core_config (uint64_t *coresPerSocket, uint64_t *threadsPerCore, uint64_t *sysSockets, int *HTenabled)
 Detect platform configuration.
 
int sockets_assert (const unsigned *socket, const int location, const char *file)
 Validate specific socket exists in the platform configuration.
 
int threads_assert (const unsigned *thread, const int location, const char *file)
 Validate specific thread exists in the platform configuration.
 
int cores_assert (const unsigned *core, const int location, const char *file)
 Validate specific core exists in the platform configuration.
 
int stat_module (char *filename, int *kerneltype, int *dev_idx)
 Check status of a file.
 
int init_msr (void)
 Open the MSR module file descriptors exposed in the /dev filesystem.
 
int finalize_msr (void)
 Close the MSR module file descriptors exposed in the /dev filesystem.
 
int write_msr_by_coord (unsigned socket, unsigned core, unsigned thread, off_t msr, uint64_t val)
 Write a new value to an MSR based on the coordinates of a core or thread.
 
int read_msr_by_coord (unsigned socket, unsigned core, unsigned thread, off_t msr, uint64_t *val)
 Read current value of an MSR based on the coordinates of a core or thread.
 
int read_msr_by_coord_batch (unsigned socket, unsigned core, unsigned thread, off_t msr, uint64_t **val, int batchnum)
 Perform batch read of multiple MSR based on the coordinates of a core or thread.
 
int read_batch (const int batchnum)
 Do batch read operation.
 
int write_batch (const int batchnum)
 Do batch write operation.
 
int load_socket_batch (off_t msr, uint64_t **val, int batchnum)
 Load batch operations for a socket.
 
int load_core_batch (off_t msr, uint64_t **val, int batchnum)
 Load batch operations for a core.
 
int load_thread_batch (off_t msr, uint64_t **val, int batchnum)
 Load batch operations for a thread.
 
int read_msr_by_idx (int dev_idx, off_t msr, uint64_t *val)
 Read current value of an MSR based on the index of a core or thread.
 
int write_msr_by_idx (int dev_idx, off_t msr, uint64_t val)
 Write new value to an MSR based on the index of a core or thread.
 
int write_msr_by_idx_and_verify (int dev_idx, off_t msr, uint64_t val)
 Verify successful MSR write by following operation immediately with a read.
 

Macro Definition Documentation

#define _XOPEN_SOURCE   500

Function Documentation

int allocate_batch ( int  batchnum,
size_t  bsize 
)

Allocate space for new batch operation.

Parameters
[in]batchnumlibmsr_data_type_e data type of batch operation.
[in]bsizeSize of batch operation.
Returns
0 if allocation was a success, else -1 if batch_storage() fails.
void core_config ( uint64_t *  coresPerSocket,
uint64_t *  threadsPerCore,
uint64_t *  sockets,
int *  HTenabled 
)

Detect platform configuration.

Parameters
[out]coresPerSocketNumber of cores per socket.
[out]threadsPerCoreNumber of threads per core.
[out]socketsNumber of sockets.
[out]HTenabledIndicates if hyperthreading is enabled/disabled.
int cores_assert ( const unsigned *  core,
const int  location,
const char *  file 
)

Validate specific core exists in the platform configuration.

Parameters
[in]coreUnique core identifier.
[in]locationLine number in source file where error occurred (use standard predefined macro LINE).
[in]fileName of source file where error occurred (use standard predefined macro FILE).
Returns
0 if successful, else -1 if core requested is greater than number of cores per socket in the platform.
uint64_t cores_per_socket ( void  )

Retrieve number of cores per socket existing on the platform.

Returns
Number of cores per socket.
int create_batch_op ( off_t  msr,
uint64_t  cpu,
uint64_t **  dest,
const int  batchnum 
)

Create new batch operation.

Parameters
[in]msrAddress of MSR for which operation will take place.
[in]cpuCPU where batch operation will take place.
[in]destStores data resulting from rdmsr or necessary for wrmsr.
[in]batchnumlibmsr_data_type_e data type of batch operation.
Returns
0 if creation was a success, else -1 if batch_storage() fails or if the number of batch operations exceeds the allocated size.
int finalize_msr ( void  )

Close the MSR module file descriptors exposed in the /dev filesystem.

Returns
0 if finalization was a success, else -1 if could not close file descriptors.
int free_batch ( int  batchnum)

Deallocate memory for specific set of batch operations.

Parameters
[in]batchnumlibmsr_data_type_e data type of batch operation.
Returns
0 if successful, else -1 if batch_storage() fails.
int init_msr ( void  )

Open the MSR module file descriptors exposed in the /dev filesystem.

Returns
0 if initialization was a success, else -1 if could not stat file descriptors or open any msr module.
int load_core_batch ( off_t  msr,
uint64_t **  val,
const int  batchnum 
)

Load batch operations for a core.

Parameters
[in]msrAddress of register to load.
[in]valPointer to batch storage array.
[in]batchnumlibmsr_data_type_e data type of batch operation.
Returns
0 if successful, else -1 if batch storage array is uninitialized.
Todo:
dev_idx++?
int load_socket_batch ( off_t  msr,
uint64_t **  val,
const int  batchnum 
)

Load batch operations for a socket.

Parameters
[in]msrAddress of register to load.
[in]valPointer to batch storage array.
[in]batchnumlibmsr_data_type_e data type of batch operation.
Returns
0 if successful, else -1 if batch storage array is uninitialized.
int load_thread_batch ( off_t  msr,
uint64_t **  val,
const int  batchnum 
)

Load batch operations for a thread.

Parameters
[in]msrAddress of register to load.
[in]valPointer to batch storage array.
[in]batchnumlibmsr_data_type_e data type of batch operation.
Returns
0 if successful, else -1 if batch storage array is uninitialized.
uint64_t num_cores ( void  )

Retrieve the number of cores existing on the platform.

Returns
Number of cores.
uint64_t num_devs ( void  )

Retrieve total number of logical processors existing on the platform.

Returns
Total number of logical processors.
uint64_t num_sockets ( void  )

Retrieve the number of sockets existing on the platform.

Returns
Number of sockets.
int read_batch ( const int  batchnum)

Do batch read operation.

Parameters
[in]batchnumlibmsr_data_type_e data type of batch operation.
int read_msr_by_coord ( unsigned  socket,
unsigned  core,
unsigned  thread,
off_t  msr,
uint64_t *  val 
)

Read current value of an MSR based on the coordinates of a core or thread.

A user can request to read from core 4 on socket 1, instead of having to map this core to a continuous value based on the the number of cores on socket 0. For a dual socket system with 8 cores, socket 0 would have cores 0-7, core 0 on socket 1 would be mapped to index 8.

Parameters
[in]socketUnique socket/package identifier.
[in]coreUnique core identifier.
[in]threadUnique thread identifier.
[in]msrAddress of register to read.
[out]valValue read from MSR.
Returns
0 if read_msr_by_idx() was a success, else -1 if the file descriptor was NULL or if the number of bytes read was not the size of uint64_t.
int read_msr_by_coord_batch ( unsigned  socket,
unsigned  core,
unsigned  thread,
off_t  msr,
uint64_t **  val,
int  batchnum 
)

Perform batch read of multiple MSR based on the coordinates of a core or thread.

Parameters
[in]socketUnique socket/package identifier.
[in]coreUnique core identifier.
[in]threadUnique thread identifier.
[in]msrAddress of register to read.
[out]valValue read from MSR.
[in]batchnumlibmsr_data_type_e data type of batch operation.
Returns
0 if read_msr_by_idx() was a success, else -1 if the file descriptor was NULL or if the number of bytes read was not the size of uint64_t.
int read_msr_by_idx ( int  dev_idx,
off_t  msr,
uint64_t *  val 
)

Read current value of an MSR based on the index of a core or thread.

A user can request to read from index 8, which is core 0 on socket 1 in a dual socket system with 8 cores per socket. This index is a continuous value based on the the number of cores on socket 0.

Parameters
[in]dev_idxUnique device identifier.
[in]msrAddress of register to read.
[out]valValue read from MSR.
Returns
0 if successful, else -1 if file descriptor was NULL or if the number of bytes read was not the size of uint64_t.
int sockets_assert ( const unsigned *  socket,
const int  location,
const char *  file 
)

Validate specific socket exists in the platform configuration.

Parameters
[in]socketUnique socket/package identifier.
[in]locationLine number in source file where error occurred (use standard predefined macro LINE).
[in]fileName of source file where error occurred (use standard predefined macro FILE).
Returns
0 if successful, else -1 if socket requested is greater than number of sockets in the platform.
int stat_module ( char *  filename,
int *  kerneltype,
int *  dev_idx 
)

Check status of a file.

Parameters
[in]filenameFile to check status of.
[in]kerneltypeOS privilege level (ring 0-ring 3).
[in]dev_idxUnique logical processor index.
Returns
0 if successful, else -1 if can't find any msr module with RW permissions.
int threads_assert ( const unsigned *  thread,
const int  location,
const char *  file 
)

Validate specific thread exists in the platform configuration.

Parameters
[in]threadUnique thread identifier.
[in]locationLine number in source file where error occurred (use standard predefined macro LINE).
[in]fileName of source file where error occurred (use standard predefined macro FILE).
Returns
0 if successful, else -1 if thread requested is greater than number of threads per core in the platform.
int write_batch ( const int  batchnum)

Do batch write operation.

Parameters
[in]batchnumlibmsr_data_type_e data type of batch operation.
int write_msr_by_coord ( unsigned  socket,
unsigned  core,
unsigned  thread,
off_t  msr,
uint64_t  val 
)

Write a new value to an MSR based on the coordinates of a core or thread.

A user can request to write to core 4 on socket 1, instead of having to map this core to a continuous value based on the the number of cores on socket 0. For a dual socket system with 8 cores, socket 0 would have cores 0-7, core 0 on socket 1 would be mapped to index 8.

Parameters
[in]socketUnique socket/package identifier.
[in]coreUnique core identifier.
[in]threadUnique thread identifier.
[in]msrAddress of register to write.
[in]valValue to write to MSR.
Returns
0 if write_msr_by_idx() was a success, else -1 if the file descriptor was NULL or if the number of bytes written was not the size of uint64_t.
int write_msr_by_idx ( int  dev_idx,
off_t  msr,
uint64_t  val 
)

Write new value to an MSR based on the index of a core or thread.

A user can request to read from index 8, which is core 0 on socket 1 in a dual socket system with 8 cores per socket. This index is a continuous value based on the the number of cores on socket 0.

Parameters
[in]dev_idxUnique device identifier.
[in]msrAddress of register to read.
[out]valValue read from MSR.
Returns
0 if successful, else -1 if file descriptor was NULL or if the number of bytes read was not the size of uint64_t.
int write_msr_by_idx_and_verify ( int  dev_idx,
off_t  msr,
uint64_t  val 
)

Verify successful MSR write by following operation immediately with a read.

Parameters
[in]dev_idxUnique device identifier.
[in]msrAddress of register to read.
[out]valValue read from MSR.
Returns
0 if successful, else -1 if file descriptor was NULL, if the number of bytes written was not the size of uint64_t, if the read function failed, or if the value written does not match the read value.