Root/src/cpu.cpp

1#include <errno.h>
2#include <fcntl.h>
3#include <stdio.h>
4#include <string.h>
5#include <unistd.h>
6
7#include <iostream>
8
9#include "cpu.h"
10#include "debug.h"
11
12#define SYSFS_CPUFREQ_DIR "/sys/devices/system/cpu/cpu0/cpufreq"
13#define SYSFS_CPUFREQ_MAX SYSFS_CPUFREQ_DIR "/scaling_max_freq"
14#define SYSFS_CPUFREQ_SET SYSFS_CPUFREQ_DIR "/scaling_setspeed"
15
16void writeStringToFile(const char *path, const char *content)
17{
18    int fd = open(path, O_RDWR);
19    if (fd == -1) {
20        WARNING("Failed to open '%s': %s\n", path, strerror(errno));
21    } else {
22        ssize_t written = write(fd, content, strlen(content));
23        if (written == -1) {
24            WARNING("Error writing '%s': %s\n", path, strerror(errno));
25        }
26        close(fd);
27    }
28}
29
30void jz_cpuspeed(unsigned clockspeed)
31{
32    char freq[10];
33    sprintf(freq, "%d", clockspeed * 1000);
34    writeStringToFile(SYSFS_CPUFREQ_MAX, freq);
35    writeStringToFile(SYSFS_CPUFREQ_SET, freq);
36}
37

Archive Download this file



interactive