aboutsummaryrefslogtreecommitdiffstats
path: root/cpufreq.c
diff options
context:
space:
mode:
authorPA4WDH2023-05-20 17:17:43 +0200
committerPA4WDH2023-05-20 17:17:43 +0200
commit15d9095122c0d4f9a4dab05fb0e84d84c624e1b4 (patch)
treec6edcc0b2efe1dc68577d57de9de74ae4bf8cdd4 /cpufreq.c
parentInitial commit, POC shell script (diff)
downloadcputemp2maxfreq-15d9095122c0d4f9a4dab05fb0e84d84c624e1b4.tar.gz
cputemp2maxfreq-15d9095122c0d4f9a4dab05fb0e84d84c624e1b4.tar.bz2
cputemp2maxfreq-15d9095122c0d4f9a4dab05fb0e84d84c624e1b4.zip
Translate POC to C code0.1
Diffstat (limited to 'cpufreq.c')
-rw-r--r--cpufreq.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/cpufreq.c b/cpufreq.c
new file mode 100644
index 0000000..d9e0e4e
--- /dev/null
+++ b/cpufreq.c
@@ -0,0 +1,82 @@
+#include <fcntl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include "debug.h"
+#include "sysfs.h"
+
+#define DIR_BUF_SIZE 1024
+
+int cpufreq_is_cpu(char *name)
+{
+ if (strncmp(name,"cpu",3)!=0) return 0;
+ if ((name[3]<'0') || (name[3]>'9')) return 0;
+
+ return 1;
+}
+
+int cpufreq_set_str(char *parameter,char *value,int checkdelay)
+{
+ int fd;
+ char sysfs_file[128];
+
+ DEBUG1_CPUFREQ("Set %s to %s\n",parameter,value)
+
+ char buf[DIR_BUF_SIZE];
+ long nread;
+
+ struct linux_dirent {
+ unsigned long d_ino;
+ off_t d_off;
+ unsigned short d_reclen;
+ char d_name[];
+ };
+
+ struct linux_dirent *d;
+ size_t bpos;
+
+ fd=open("/sys/devices/system/cpu",O_RDONLY|O_DIRECTORY);
+ if (fd==-1)
+ {
+ DEBUG1_CPUFREQ("Unable to find CPU's\n");
+ return -1;
+ }
+
+ while(1)
+ {
+ nread=syscall(SYS_getdents,fd,buf,DIR_BUF_SIZE);
+ if (nread==-1)
+ {
+ DEBUG1_CPUFREQ("Unable to find CPU's\n");
+ close(fd);
+ return -1;
+ }
+ if (nread==0) break;
+
+ for(bpos=0;bpos<nread;)
+ {
+ d=(struct linux_dirent *)(buf+bpos);
+ if (cpufreq_is_cpu(d->d_name))
+ {
+ DEBUG2_CPUFREQ("Found CPU %s\n",d->d_name);
+ snprintf(sysfs_file,128,"/sys/devices/system/cpu/%s/cpufreq/%s",d->d_name,parameter);
+ if (sysfs_write_str(sysfs_file,value,checkdelay)!=0)
+ {
+ DEBUG1_CPUFREQ("Failed to set %s\n",d->d_name);
+ return -1;
+ }
+ }
+ bpos+=d->d_reclen;
+ }
+ }
+ close(fd);
+}
+
+int cpufreq_set_long_int(char *parameter,long int value,int checkdelay)
+{
+ char buf[255];
+
+ snprintf(buf,255,"%ld",value);
+ cpufreq_set_str(parameter,buf,checkdelay);
+}