From fbfddeaf4e2c9fe19f2cecdeb55c4434c110d682 Mon Sep 17 00:00:00 2001 From: min Date: Fri, 10 Jun 2022 15:26:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug#120511,120580,=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96cpu=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hardware/libkycpu.c | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/hardware/libkycpu.c b/src/hardware/libkycpu.c index fda56e9..e146c19 100644 --- a/src/hardware/libkycpu.c +++ b/src/hardware/libkycpu.c @@ -9,6 +9,7 @@ #ifdef __linux__ #include #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;