aboutsummaryrefslogtreecommitdiffstats
path: root/cpulist.c
blob: eb219b58aabee97e5ccbdf08a467f221c63fd912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <stdlib.h>
#include "debug.h"
#include "sysfs.h"
#include "cputemp2maxfreq.h"

extern struct s_config config;
extern struct s_cpudata cpudata;

// Add a CPU to the list
void cpulist_add(char *cpu)
{
 int newlen;

 newlen=cpudata.cpulist_len+1;
 cpudata.cpulist=reallocarray(cpudata.cpulist,newlen,sizeof(char *));

 cpudata.cpulist[cpudata.cpulist_len]=malloc(strlen(cpu)+1);
 strcpy(cpudata.cpulist[cpudata.cpulist_len],cpu);
 cpudata.cpulist[cpudata.cpulist_len][strlen(cpu)]=0;
 cpudata.cpulist_len=newlen;
}

// Validate if we found a file named cpu[0-9][0-9][0-9]
int cpulist_is_cpu(char *name)
{
 if (strncmp(name,"cpu",3)!=0) return 0;
 if ((name[3]<'0') || (name[3]>'9')) return 0;
 if (name[4]==0) return 1;

 if ((name[4]<'0') || (name[4]>'9')) return 0;
 if (name[5]==0) return 1;

 if ((name[5]<'0') || (name[5]>'9')) return 0;
 if (name[6]==0) return 1;

 return 0;
}

// Set a cpufreq parameter to a value
int cpulist_find_cpus()
{
 DIR *cpudir;
 struct dirent *cpu_dirent;
 char sysfs_file[309];
 int package;

 DEBUG1_CPULIST("Started\n")

 if (cpudata.cpulist!=NULL)
 {
  DEBUG1_CPULIST("cpulist already set, free list first\n");
  return -1;
 } 
 cpudata.cpulist_len=0;

// Open the CPU directory in sysfs
 cpudir=opendir("/sys/devices/system/cpu");
 if (cpudir==NULL)
 {
  DEBUG1_CPULIST("Unable to find CPU's\n");
  return -1;
 }

 cpu_dirent=readdir(cpudir);
 while(cpu_dirent!=NULL)
 {

// Check if it's a CPU and it belongs to the correct physical package
  if (cpulist_is_cpu(cpu_dirent->d_name))
  {
   DEBUG2_CPULIST("Found CPU %s\n",cpu_dirent->d_name);
   snprintf(sysfs_file,309,"/sys/devices/system/cpu/%s/topology/physical_package_id",cpu_dirent->d_name);
   package=sysfs_read_long_int(sysfs_file);
   DEBUG2_CPULIST("CPU Belongs to package id %d\n",package);
   if ((package==config.cpu) || (config.cpu<0))
   {
    DEBUG2_CPULIST("Add to list\n");
    cpulist_add(cpu_dirent->d_name);
   } else {
    DEBUG2_CPULIST("Skip this CPU\n");
   }
  }

  cpu_dirent=readdir(cpudir);
 }
 closedir(cpudir);

 DEBUG1_CPULIST("Found %d CPU's\n",config.cpulist_len);
 if (cpudata.cpulist_len==0)
 {
   config.logger("Warning: Found no CPU's beloning to physical CPU %d",config.cpu);
 }

 return 0;
}

void cpulist_free()
{
 int count;
 if (cpudata.cpulist==NULL) return;
 int len=cpudata.cpulist_len;
 cpudata.cpulist_len=0;

 for(count=0;count<len;count++) free(cpudata.cpulist[count]);
 free(cpudata.cpulist);
 cpudata.cpulist=NULL;
}