Advertisement
renix1

cpufreq

Aug 31st, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. int read_command(char command[]) {
  7.     int current_data = 0;
  8.     FILE *fp;
  9.     fp = popen(command, "r");
  10.     if (fp == NULL) {
  11.         printf("Comando não executado\n");
  12.         exit(1);
  13.     } else {
  14.         fscanf(fp, "%d", &current_data);
  15.     }
  16.     fclose(fp);
  17.     return current_data;
  18. }
  19.  
  20. short int cpu_cores () {
  21.     short int cores = 0;
  22.     cores = read_command("nproc");
  23.     return cores;
  24. }
  25.  
  26. int check_max_freq () {
  27.     return read_command("cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")/1000;
  28. }
  29.  
  30. int set_performance (int freq, short int cores) {
  31.     char command[50];
  32.     for (short int i=0; i<cores; i++) {
  33.         sprintf(command, "cpufreq-set -d %d -c %d -g performance", freq, i);
  34.         system(command);
  35.     }
  36. }
  37.  
  38. int main (void) {
  39.     int maxFrequency = 0;
  40.     short int cpuCores = 0;
  41.     cpuCores = cpu_cores();
  42.     maxFrequency = check_max_freq(cpuCores);
  43.     set_performance(maxFrequency, cpuCores);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement