Created
April 2, 2012 10:46
-
-
Save inish777/2282501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. | |
Copyright (C) 2012 Andrew Inishev <inish777@gmail.com> | |
Copyright (C) 2011 NumberSeven <nbrseven@gmail.com> | |
*/ | |
#include "core/cpu.h" | |
#define IF_CH(x,y) if(!strncmp(x, y, strlen(y))) | |
#define PRINTF_MACRO(x, y) printf("\t\"%s\":\"%s\",\n", x, y); | |
#define PRINTF_MACRO_END(x,y) printf("\t\"%s\":\"%s\"\n", x, y); | |
typedef struct cpu_info_t | |
{ | |
char* cpu; | |
char* cache_size; | |
char* model_name; | |
char* type; | |
char* speed_in_mhz; | |
char* speed_in_bgmps; | |
char* temp; | |
char* crit_temp; | |
}cpu_info_t; | |
static int alloc_cpy(char** dest, char* src); | |
int cpu_info() | |
{ | |
FILE *cpufp; | |
char ch [MAXLEN]; | |
char line [MAXLEN]; | |
cpu_info_t** cpu_info = (cpu_info_t**)calloc(1, sizeof(cpu_info_t*)); | |
cpu_info[0] = (cpu_info_t*)calloc(1, sizeof(cpu_info_t)); | |
if ((cpufp = fopen("/proc/cpuinfo", "r")) == NULL) | |
{ | |
puts("Error opening /proc/cpuinfo"); | |
exit (-1); | |
} | |
int x = 0; | |
while (fgets(ch, MAXLEN, cpufp)) | |
{ | |
IF_CH(ch, "processor") | |
{ | |
sscanf (ch, "%*s %*s %s", line); | |
if(cpu_info[x]->cpu) | |
{ | |
cpu_info = (cpu_info_t**)realloc(cpu_info, sizeof(cpu_info_t*) * (++x + 1)); | |
cpu_info[x] = (cpu_info_t*) calloc(1, sizeof(cpu_info_t)); | |
} | |
alloc_cpy(&cpu_info[x]->cpu, line); | |
} else IF_CH(ch, "vendor_id") | |
{ | |
sscanf(ch,"%*s %*s %s", line); | |
alloc_cpy(&cpu_info[x]->type, line); | |
} else IF_CH(ch, "model name") | |
{ | |
ch[strlen(ch)-1] = '\0'; | |
alloc_cpy(&cpu_info[x]->model_name, &ch[13]); | |
} else IF_CH(ch, "cpu MHz") | |
{ | |
sscanf(ch, "%*s %*s %*s %s\n", line); | |
alloc_cpy(&cpu_info[x]->speed_in_mhz, line); | |
} else IF_CH(ch, "cache size") | |
{ | |
sscanf(ch, "%*s %*s %*s %s", line); | |
strcat (line, " KB"); | |
alloc_cpy(&cpu_info[x]->cache_size, line); | |
} else IF_CH(ch, "bogomips") | |
{ | |
sscanf (ch, "%*s %*s %s", line); | |
alloc_cpy(&cpu_info[x]->speed_in_bgmps, line); | |
cpu_temperature (cpu_info, x); | |
} | |
} | |
cpu_info = (cpu_info_t**)realloc(cpu_info, sizeof(cpu_info_t*) * (++x + 1)); | |
cpu_info[x] = NULL; | |
x = 0; | |
puts("["); | |
while(cpu_info[x]) | |
{ puts("{"); | |
PRINTF_MACRO("CPU", cpu_info[x]->cpu); | |
PRINTF_MACRO("Processor type", cpu_info[x]->type); | |
PRINTF_MACRO("Model name", cpu_info[x]->model_name); | |
PRINTF_MACRO("Current speed in MHz", cpu_info[x]->speed_in_mhz); | |
PRINTF_MACRO("Cache size", cpu_info[x]->cache_size); | |
PRINTF_MACRO("Current speed in bogomips", cpu_info[x]->speed_in_bgmps); | |
PRINTF_MACRO("Temperature", cpu_info[x]->temp); | |
PRINTF_MACRO_END("Critical temperature", cpu_info[x]->crit_temp); | |
if(cpu_info[x + 1]) | |
puts("},"); | |
else | |
puts("}"); | |
x++; | |
} | |
puts("]"); | |
free(cpu_info); | |
fclose (cpufp); | |
return 0; | |
} | |
/************************************************************ | |
cpu_temperature () use libsensors4 to get CPU temperature. | |
*************************************************************/ | |
void cpu_temperature (cpu_info_t** cpu_info, int x) | |
{ | |
sensors_init (NULL); | |
const sensors_chip_name* chip; | |
const sensors_feature* feature; | |
const sensors_subfeature* subfeature; | |
int nr1 = 0; | |
int nr2 = 0; | |
double input_temp, critical_temp; | |
while ((chip = sensors_get_detected_chips (NULL, &nr1)) != NULL) | |
{ | |
while ((feature = sensors_get_features (chip, &nr2)) != NULL) | |
{ | |
if(feature->type==SENSORS_FEATURE_TEMP) | |
{ | |
if(!(subfeature = sensors_get_subfeature (chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT))) | |
break; | |
sensors_get_value (chip, subfeature->number, &input_temp); | |
if(!(subfeature = sensors_get_subfeature (chip, feature, SENSORS_SUBFEATURE_TEMP_CRIT))) | |
break; | |
sensors_get_value (chip, subfeature->number, &critical_temp); | |
} | |
nr2++; | |
} | |
nr1++; | |
} | |
cpu_info[x]->temp = (char*)calloc(1, MAXLEN); | |
cpu_info[x]->crit_temp = (char*)calloc(1, MAXLEN); | |
sprintf (cpu_info[x]->temp, "%d°C", (int)input_temp); | |
sprintf (cpu_info[x]->crit_temp,"%d°C", (int)critical_temp); | |
sensors_cleanup(); | |
} | |
static int alloc_cpy(char** dest, char* src) | |
{ | |
*dest = (char*) calloc(1, MAXLEN); | |
strcpy(*dest, src); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment