[v1.05] Add support for frequency measurement in ARM (had to move bind_to_cpu to common dir)

This commit is contained in:
Dr-Noob
2024-02-16 23:37:37 +01:00
parent 72eee0e08e
commit d71a461d83
10 changed files with 114 additions and 54 deletions

View File

@@ -2,11 +2,6 @@
#define _GNU_SOURCE
// TODO: This should be under common/
#include "../x86/apic.h"
#include "../x86/freq/freq.h"
#include "global.h"
#include <time.h>
#include <stdio.h>
#include <stdint.h>
@@ -17,6 +12,14 @@
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#ifdef ARCH_X86
#include "../x86/freq/freq.h"
#elif ARCH_ARM
#include "../arm/freq.h"
#endif
#include "global.h"
static long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags) {
@@ -36,10 +39,7 @@ perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
// - measure_frequency uses actual computation while measuring the frequency
// whereas measure_max_frequency uses nop instructions. This makes the former
// x86 dependant whereas the latter is architecture independant.
int64_t measure_max_frequency(void) {
// TODO: Support binding on all cores inside a SoC (x86
// hybrid topologies and ARM SoCs)
int core = 0;
int64_t measure_max_frequency(uint32_t core) {
if (!bind_to_cpu(core)) {
printErr("Failed binding the process to CPU %d", core);
return -1;

View File

@@ -1,6 +1,6 @@
#ifndef __COMMON_FREQ__
#define __COMMON_FREQ__
int64_t measure_max_frequency(void);
int64_t measure_max_frequency(uint32_t core);
#endif

View File

@@ -1,3 +1,14 @@
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#elif defined __linux__
#define _GNU_SOURCE
#include <sched.h>
#elif defined __FreeBSD__
#include <sys/param.h>
#include <sys/cpuset.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -199,6 +210,34 @@ void* erealloc(void *ptr, size_t size) {
return newptr;
}
#ifndef __APPLE__
bool bind_to_cpu(int cpu_id) {
#ifdef _WIN32
HANDLE process = GetCurrentProcess();
DWORD_PTR processAffinityMask = 1 << cpu_id;
return SetProcessAffinityMask(process, processAffinityMask);
#elif defined __linux__
cpu_set_t currentCPU;
CPU_ZERO(&currentCPU);
CPU_SET(cpu_id, &currentCPU);
if (sched_setaffinity (0, sizeof(currentCPU), &currentCPU) == -1) {
printWarn("sched_setaffinity: %s", strerror(errno));
return false;
}
return true;
#elif defined __FreeBSD__
cpuset_t currentCPU;
CPU_ZERO(&currentCPU);
CPU_SET(cpu_id, &currentCPU);
if(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), &currentCPU) == -1) {
printWarn("cpuset_setaffinity: %s", strerror(errno));
return false;
}
return true;
#endif
}
#endif
void print_version(FILE *restrict stream) {
#ifdef GIT_FULL_VERSION
fprintf(stream, "cpufetch %s (%s %s)\n", GIT_FULL_VERSION, OS_STR, ARCH_STR);

View File

@@ -19,6 +19,9 @@ char *strremove(char *str, const char *sub);
void* emalloc(size_t size);
void* ecalloc(size_t nmemb, size_t size);
void* erealloc(void *ptr, size_t size);
#ifndef __APPLE__
bool bind_to_cpu(int cpu_id);
#endif
void print_version(FILE *restrict stream);
#endif