修复bug#120511,120580,机器无法获取cpu信息
This commit is contained in:
parent
9dc92d8cc8
commit
fbfddeaf4e
|
@ -9,6 +9,7 @@
|
|||
#ifdef __linux__
|
||||
#include <sys/utsname.h>
|
||||
#endif
|
||||
#define MIDSIZE 128
|
||||
|
||||
struct _cpuInfo{
|
||||
size_t ncpus; // CPU数量
|
||||
|
@ -82,6 +83,23 @@ static int lookup(char *line, char *pattern, char **value)
|
|||
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()
|
||||
{
|
||||
// 我知道这里有竞态问题,但是不想引入pthread,所以算了
|
||||
|
@ -127,9 +145,11 @@ static void _get_cpu_info()
|
|||
else if (lookup(buffer, "model name", &cpuinf->model));
|
||||
else if (lookup(buffer, "flags", &cpuinf->flags)); // x86
|
||||
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, "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, "processor", &cpu_process))
|
||||
{
|
||||
cpuinf->cpu_process = atoi(cpu_process) + 1;
|
||||
|
@ -150,6 +170,47 @@ static void _get_cpu_info()
|
|||
else if (strstr(cpuinf->flags, " 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
|
||||
return;
|
||||
|
|
Loading…
Reference in New Issue