int clocks_storage(uint64_t *** aperf_val, uint64_t *** mperf_val, uint64_t *** tsc_val) { static int init = 1; static uint64_t ** aperf = NULL, ** mperf = NULL, ** tsc = NULL; static uint64_t totalThreads = 0; if (init) { totalThreads = num_devs(); aperf = (uint64_t **) libmsr_malloc(totalThreads * sizeof(uint64_t *)); mperf = (uint64_t **) libmsr_malloc(totalThreads * sizeof(uint64_t *)); tsc = (uint64_t **) libmsr_malloc(totalThreads * sizeof(uint64_t *)); allocate_batch(CLOCKS_DATA, 3UL * num_devs()); load_thread_batch(MSR_IA32_APERF, aperf, CLOCKS_DATA); load_thread_batch(MSR_IA32_MPERF, mperf, CLOCKS_DATA); load_thread_batch(IA32_TIME_STAMP_COUNTER, tsc, CLOCKS_DATA); init = 0; } if (aperf_val) { *aperf_val = aperf; } if (mperf_val) { *mperf_val = mperf; } if (tsc_val) { *tsc_val = tsc; } return 0; }
int finalize_msr(void) { int dev_idx; int rc; int *fileDescriptor = NULL; uint64_t numDevs = num_devs(); #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: finalize_msr\n"); #endif /* Close the file descriptors. */ for (dev_idx = 0; dev_idx < numDevs; dev_idx++) { fileDescriptor = core_fd(dev_idx); if (fileDescriptor != NULL) { rc = close(*fileDescriptor); if (rc != 0) { libmsr_error_handler("finalize_msr(): Could not close file", LIBMSR_ERROR_MSR_CLOSE, getenv("HOSTNAME"), __FILE__, __LINE__); return -1; } else { *fileDescriptor = 0; } } } memhdlr_finalize(); return 0; }
void dump_clocks_terse_label(FILE *writeFile){ int thread_idx; static uint64_t totalThreads = 0; if (!totalThreads) { totalThreads = num_devs(); } for(thread_idx=0; thread_idx<totalThreads; thread_idx++){ fprintf(writeFile, "aperf%02d mperf%02d tsc%02d ", thread_idx, thread_idx, thread_idx); } }
void dump_clocks_terse(FILE *writeFile){ static uint64_t totalThreads = 0; static uint64_t ** aperf_val = NULL, ** mperf_val = NULL, ** tsc_val = NULL; if (!totalThreads) { totalThreads = num_devs(); clocks_storage(&aperf_val, &mperf_val, &tsc_val); } int thread_idx; read_batch(CLOCKS_DATA); for(thread_idx=0; thread_idx<totalThreads; thread_idx++){ fprintf(writeFile, "%20lu %20lu %20lu ", *aperf_val[thread_idx], *mperf_val[thread_idx], *tsc_val[thread_idx]); } }
/// @brief Retrieve file descriptor per logical processor. /// /// @param [in] dev_idx Unique logical processor identifier. /// /// @return Unique file descriptor, else NULL. static int *core_fd(const int dev_idx) { static int init = 0; static int *file_descriptors = NULL; static uint64_t devices = 0; if (!init) { init = 1; uint64_t numDevs = num_devs();; devices = numDevs; file_descriptors = (int *) libmsr_malloc(devices * sizeof(int)); } if (dev_idx < devices) { return &(file_descriptors[dev_idx]); } libmsr_error_handler("core_fd(): Array reference out of bounds", LIBMSR_ERROR_ARRAY_BOUNDS, getenv("HOSTNAME"), __FILE__, __LINE__); return NULL; }
void dump_clocks_readable(FILE * writeFile) { static uint64_t totalThreads = 0; static uint64_t ** aperf_val = NULL, ** mperf_val = NULL, ** tsc_val = NULL; if (!totalThreads) { totalThreads = num_devs(); clocks_storage(&aperf_val, &mperf_val, &tsc_val); } #ifdef LIBMSR_DEBUG fprintf(stderr, "DEBUG: (clocks_readable) totalThreads is %lu\n", totalThreads); #endif int thread_idx; read_batch(CLOCKS_DATA); for(thread_idx=0; thread_idx<totalThreads; thread_idx++){ fprintf(writeFile, "aperf%02d:%20lu mperf%02d:%20lu tsc%02d:%20lu\n", thread_idx, *aperf_val[thread_idx], thread_idx, *mperf_val[thread_idx], thread_idx, *tsc_val[thread_idx]); } }
int init_msr(void) { int dev_idx; int ret; int *fileDescriptor = NULL; char filename[FILENAME_SIZE]; static int init = 0; int kerneltype = 3; // 0 is msr_safe, 1 is msr ret = find_cpu_top(); if (ret < 0) { return ret; } uint64_t numDevs = num_devs(); #ifdef LIBMSR_DEBUG fprintf(stderr, "%s Initializing %lu device(s).\n", getenv("HOSTNAME"), (numDevs)); #endif if (init) { return 0; } snprintf(filename, FILENAME_SIZE, "/dev/cpu/msr_whitelist"); stat_module(filename, &kerneltype, 0); /* Open the file descriptor for each device's msr interface. */ for (dev_idx = 0; dev_idx < numDevs; dev_idx++) { /* Use the msr_safe module, or default to the msr module. */ if (kerneltype) { snprintf(filename, FILENAME_SIZE, "/dev/cpu/%d/msr", dev_idx); } else { snprintf(filename, FILENAME_SIZE, "/dev/cpu/%d/msr_safe", dev_idx); } if (stat_module(filename, &kerneltype, &dev_idx) < 0) { return -1; } if (dev_idx < 0) { continue; } /* Open the msr module, else return the appropriate error message. */ fileDescriptor = core_fd(dev_idx); *fileDescriptor = open(filename, O_RDWR); if (*fileDescriptor == -1) { libmsr_error_handler("init_msr(): Could not open file", LIBMSR_ERROR_RAPL_INIT, getenv("HOSTNAME"), __FILE__, __LINE__); if (kerneltype) { libmsr_error_handler("init_msr(): Could not open any valid MSR module", LIBMSR_ERROR_RAPL_INIT, getenv("HOSTNAME"), __FILE__, __LINE__); /* Could not open any msr module, so exit. */ return -1; } kerneltype = 1; dev_idx = -1; } } init = 1; return 0; }