修复bug#120511,120580,机器无法获取cpu信息

This commit is contained in:
min 2022-06-10 15:26:52 +08:00
parent 9dc92d8cc8
commit fbfddeaf4e
1 changed files with 61 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#ifdef __linux__ #ifdef __linux__
#include <sys/utsname.h> #include <sys/utsname.h>
#endif #endif
#define MIDSIZE 128
struct _cpuInfo{ struct _cpuInfo{
size_t ncpus; // CPU数量 size_t ncpus; // CPU数量
@ -82,6 +83,23 @@ static int lookup(char *line, char *pattern, char **value)
return 1; return 1;
} }
static int do_shell(char *comm, char *buf)
{
FILE *stream;
stream = popen(comm, "r");
if (stream == NULL) {
return 0;
}
fread(buf, sizeof(char), MIDSIZE, stream);
pclose(stream);
buf[strlen(buf) - 1] = '\0';//去掉结尾转行符
if (strlen(buf) == 0) {
return 0;
}
return 1;
}
static void _get_cpu_info() static void _get_cpu_info()
{ {
// 我知道这里有竞态问题但是不想引入pthread所以算了 // 我知道这里有竞态问题但是不想引入pthread所以算了
@ -127,9 +145,11 @@ static void _get_cpu_info()
else if (lookup(buffer, "model name", &cpuinf->model)); else if (lookup(buffer, "model name", &cpuinf->model));
else if (lookup(buffer, "flags", &cpuinf->flags)); // x86 else if (lookup(buffer, "flags", &cpuinf->flags)); // x86
else if (lookup(buffer, "features", &cpuinf->flags)); // s390 else if (lookup(buffer, "features", &cpuinf->flags)); // s390
else if (lookup(buffer, "Loongson Features", &cpuinf->flags));
else if (lookup(buffer, "Features", &cpuinf->flags)); // aarch64 else if (lookup(buffer, "Features", &cpuinf->flags)); // aarch64
else if (lookup(buffer, "type", &cpuinf->flags)); // sparc64 else if (lookup(buffer, "type", &cpuinf->flags)); // sparc64
else if (lookup(buffer, "cpu MHz", &cpuinf->cur_freq_MHz)); else if (lookup(buffer, "cpu MHz", &cpuinf->cur_freq_MHz));
else if (lookup(buffer, "CPU MHz", &cpuinf->cur_freq_MHz));
else if (lookup(buffer, "processor", &cpu_process)) else if (lookup(buffer, "processor", &cpu_process))
{ {
cpuinf->cpu_process = atoi(cpu_process) + 1; cpuinf->cpu_process = atoi(cpu_process) + 1;
@ -151,6 +171,47 @@ static void _get_cpu_info()
cpuinf->virt = strdup("vmx"); cpuinf->virt = strdup("vmx");
} }
if(cpuinf->vendor == NULL)
{
char ret[MIDSIZE];
do_shell("lscpu | grep \"厂商 ID\"", ret);
if(strcmp(ret, ""))
{
printf("test\n");
char *substr = "";
char *date = strstr(ret, substr);
strcpy(ret, date + 27);
cpuinf->vendor = strdup(ret);
}
}
if(cpuinf->model == NULL)
{
char ret[MIDSIZE];
do_shell("lscpu | grep \"型号名称\"", ret);
if(strcmp(ret, ""))
{
char *substr = "";
char *date = strstr(ret, substr);
strcpy(ret, date + 26);
cpuinf->model = strdup(ret);
}
}
if(cpuinf->corenums == 0)
{
char ret[MIDSIZE];
do_shell("lscpu | grep \"每个座的核数\"", ret);
if(strcmp(ret, ""))
{
char *substr = "";
char *date = strstr(ret, substr);
strcpy(ret, date + 20);
cpuinf->corenums = atoi(ret);
}
}
#endif #endif
return; return;
} }