diff --git a/Makefile b/Makefile index 677630a..17789e6 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CXX=gcc -CXXFLAGS=-g +CXXFLAGS=-g -Wall -Werror -fstack-protector-all -pedantic -Wno-unused SOURCE=main.c standart.c extended.c cpuid.c udev.c printer.c HEADERS=standart.h extended.h cpuid.h udev.h printer.h ascii.h diff --git a/main.c b/main.c index 6588040..445f478 100644 --- a/main.c +++ b/main.c @@ -25,12 +25,12 @@ Peak FLOPS: 512 GFLOP/s(in simple precision) ***/ int main() { - struct cpuInfo* cpu = getCPUInfo(cpu); + struct cpuInfo* cpu = getCPUInfo(); if(cpu == NULL) return EXIT_FAILURE; - struct cache* cach = new_cache(cach); - struct frequency* freq = new_frequency(freq); + struct cache* cach = new_cache(); + struct frequency* freq = new_frequency(); struct ascii* ascii = set_ascii(getCPUVendorInternal(cpu)); char* cpuName = getString_CPUName(); diff --git a/standart.c b/standart.c index f18c9cb..2a80da4 100644 --- a/standart.c +++ b/standart.c @@ -2,9 +2,11 @@ #include #include #include +#include #include "standart.h" #include "cpuid.h" +#include "udev.h" #define BOOLEAN_TRUE 1 #define BOOLEAN_FALSE 0 @@ -99,6 +101,7 @@ VENDOR getCPUVendor(unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) { struct cpuInfo* getCPUInfo() { struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo)); + memset(cpu,0,sizeof(struct cpuInfo)); initializeCpuInfo(cpu); unsigned eax = 0; @@ -222,7 +225,15 @@ char* getPeakPerformance(struct cpuInfo* cpu, long freq) { //7 for GFLOP/s and 6 for digits,eg 412.14 int size = 7+6+1+1; + assert(strlen(STRING_UNKNOWN)+1 <= size); char* string = malloc(sizeof(char)*size); + + //First check we have consistent data + if(freq == UNKNOWN) { + snprintf(string,strlen(STRING_UNKNOWN)+1,STRING_UNKNOWN); + return string; + } + float flops = (cpu->nThreads/cpu->HT)*freq*2; if(cpu->FMA3 || cpu->FMA4) diff --git a/udev.c b/udev.c index 388508e..c114a02 100644 --- a/udev.c +++ b/udev.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "udev.h" @@ -29,13 +30,13 @@ int getSize(char* buf, int size) { char* end = strstr (buf,"K"); if(end == NULL) { printf("ERROR in getSize(strstr)\n"); - return NO_CACHE; + return UNKNOWN; } *end = 0; int cachsize = atoi(buf); if(cachsize == 0) { printf("ERROR in getSize(atoi)\n"); - return NO_CACHE; + return UNKNOWN; } return cachsize; } @@ -43,7 +44,7 @@ int getSize(char* buf, int size) { /*** Returns size(in bytes) of cache described by path or -NO_CACHE if the cache doest no exists +UNKNOWN if the cache doest no exists ***/ @@ -54,7 +55,7 @@ int getCache(char* path) { if(file == NULL) { //Doest not exist - return NO_CACHE; + return UNKNOWN; } //File exists, read it @@ -88,7 +89,7 @@ long getFrequencyFromFile(char* path) { if(file == NULL) { //Doest not exist - return NO_CACHE; + return UNKNOWN; } //File exists, read it @@ -107,7 +108,7 @@ long getFrequencyFromFile(char* path) { free(buf); if(ret == 0) { printf("error in getFrequencyFromFile\n"); - return NO_CACHE; + return UNKNOWN; } fclose(file); return (long)ret*1000; @@ -129,7 +130,7 @@ char* getString_L1(struct cache* cach) { } char* getString_L2(struct cache* cach) { - if(cach->L2 == NO_CACHE) { + if(cach->L2 == UNKNOWN) { char* string = malloc(sizeof(char)*5); snprintf(string,5,STRING_NONE); return string; @@ -144,7 +145,7 @@ char* getString_L2(struct cache* cach) { } char* getString_L3(struct cache* cach) { - if(cach->L3 == NO_CACHE) { + if(cach->L3 == UNKNOWN) { char* string = malloc(sizeof(char)*5); snprintf(string,5,STRING_NONE); return string; @@ -161,8 +162,11 @@ char* getString_L3(struct cache* cach) { char* getString_MaxFrequency(struct frequency* freq) { //Max 3 digits and 3 for '(M/G)Hz' plus 1 for '\0' int size = (4+3+1); + assert(strlen(STRING_UNKNOWN)+1 <= size); char* string = malloc(sizeof(char)*size); - if(freq->max >= 1000000000) + if(freq->max == UNKNOWN) + snprintf(string,strlen(STRING_UNKNOWN)+1,STRING_UNKNOWN); + else if(freq->max >= 1000000000) snprintf(string,size,"%.2f"STRING_GIGAHERZ,(float)(freq->max)/1000000000); else snprintf(string,size,"%.2f"STRING_MEGAHERZ,(float)(freq->max)/1000000); @@ -171,8 +175,8 @@ char* getString_MaxFrequency(struct frequency* freq) { /*** CREATES AND FREES ***/ -struct cache* new_cache(struct cache* cach) { - cach = malloc(sizeof(struct cache)); +struct cache* new_cache() { + struct cache* cach = malloc(sizeof(struct cache)); cach->L1i = getCache(_PATH_CACHE_L1i); cach->L1d = getCache(_PATH_CACHE_L1d); cach->L2 = getCache(_PATH_CACHE_L2); @@ -180,8 +184,8 @@ struct cache* new_cache(struct cache* cach) { return cach; } -struct frequency* new_frequency(struct frequency* freq) { - freq = malloc(sizeof(struct frequency)); +struct frequency* new_frequency() { + struct frequency* freq = malloc(sizeof(struct frequency)); freq->max = getFrequencyFromFile(_PATH_FREQUENCY_MAX); freq->min = getFrequencyFromFile(_PATH_FREQUENCY_MIN); return freq; @@ -205,6 +209,6 @@ void debugCache(struct cache* cach) { } void debugFrequency(struct frequency* freq) { - printf("max f=%dMhz\n",freq->max); - printf("min f=%dMhz\n",freq->min); + printf("max f=%ldMhz\n",freq->max); + printf("min f=%ldMhz\n",freq->min); } diff --git a/udev.h b/udev.h index 5692959..27641c0 100644 --- a/udev.h +++ b/udev.h @@ -19,15 +19,16 @@ /*** CONSTANTS ***/ -#define NO_CACHE -1 +#define UNKNOWN -1 #define DEFAULT_FILE_SIZE 4096 #define DEFAULT_BLOCK_SIZE 128 /*** STRINGS ***/ -#define STRING_NONE "None" -#define STRING_MEGAHERZ "MHz" -#define STRING_GIGAHERZ "GHz" -#define STRING_KILOBYTES "KB" +#define STRING_UNKNOWN "Unknown" +#define STRING_NONE "None" +#define STRING_MEGAHERZ "MHz" +#define STRING_GIGAHERZ "GHz" +#define STRING_KILOBYTES "KB" /*** STRUCTS ***/ @@ -36,14 +37,14 @@ struct frequency; /*** FUNCTIONS ***/ -struct cache* new_cache(struct cache* cach); +struct cache* new_cache(); void debugCache(struct cache* cach); void freeCache(struct cache* cach); char* getString_L1(struct cache* cach); char* getString_L2(struct cache* cach); char* getString_L3(struct cache* cach); -struct frequency* new_frequency(struct frequency* freq); +struct frequency* new_frequency(); void debugFrequency(struct frequency* freq); void freeFrequency(struct frequency* freq); char* getString_MaxFrequency(struct frequency* freq);