Big refactoring

This commit is contained in:
Dr-Noob
2020-06-20 11:24:43 +02:00
parent 7cd4cb6dcb
commit e766f38884
7 changed files with 187 additions and 228 deletions

View File

@@ -2,11 +2,11 @@
#include <string.h> #include <string.h>
#include "extended.h" #include "extended.h"
char* getString_CPUName() { char* get_str_cpu_name() {
unsigned eax = 0; unsigned int eax = 0;
unsigned ebx = 0; unsigned int ebx = 0;
unsigned ecx = 0; unsigned int ecx = 0;
unsigned edx = 0; unsigned int edx = 0;
char name[64]; char name[64];
memset(name,0,64); memset(name,0,64);

View File

@@ -5,6 +5,6 @@
#include "cpuid.h" #include "cpuid.h"
#include <stdlib.h> #include <stdlib.h>
char* getString_CPUName(); char* get_str_cpu_name();
#endif #endif

View File

@@ -25,9 +25,9 @@ Peak FLOPS: 512 GFLOP/s(in simple precision)
***/ ***/
static const char* VERSION = "0.33"; static const char* VERSION = "0.34";
void help(int argc, char *argv[]) { void print_help(int argc, char *argv[]) {
printf("Usage: %s [--version] [--help] [--style STYLE]\n\ printf("Usage: %s [--version] [--help] [--style STYLE]\n\
Options: \n\ Options: \n\
--style Set logo style color\n\ --style Set logo style color\n\
@@ -38,7 +38,7 @@ void help(int argc, char *argv[]) {
argv[0]); argv[0]);
} }
void version() { void print_version() {
printf("cpufetch v%s\n",VERSION); printf("cpufetch v%s\n",VERSION);
} }
@@ -47,37 +47,37 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
if(showHelp()) { if(showHelp()) {
help(argc,argv); print_help(argc, argv);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if(showVersion()) { if(showVersion()) {
version(); print_version();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
struct cpuInfo* cpu = getCPUInfo(); struct cpuInfo* cpu = get_cpu_info();
if(cpu == NULL) if(cpu == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
struct cache* cach = new_cache(); struct cache* cach = get_cache_info();
struct frequency* freq = new_frequency(); struct frequency* freq = get_frequency_info();
struct ascii* art = set_ascii(getCPUVendorInternal(cpu),getStyle()); struct ascii* art = set_ascii(get_cpu_vendor(cpu),getStyle());
if(art == NULL) if(art == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
char* cpuName = getString_CPUName(); char* cpuName = get_str_cpu_name();
char* maxFrequency = getString_MaxFrequency(freq); char* maxFrequency = get_str_freq(freq);
char* nCores = getString_NumberCores(cpu); char* nCores = get_str_ncores(cpu);
char* avx = getString_AVX(cpu); char* avx = get_str_avx(cpu);
char* sse = getString_SSE(cpu); char* sse = get_str_sse(cpu);
char* fma = getString_FMA(cpu); char* fma = get_str_fma(cpu);
char* aes = getString_AES(cpu); char* aes = get_str_aes(cpu);
char* sha = getString_SHA(cpu); char* sha = get_str_sha(cpu);
char* l1 = getString_L1(cach); char* l1 = get_str_l1(cach);
char* l2 = getString_L2(cach); char* l2 = get_str_l2(cach);
char* l3 = getString_L3(cach); char* l3 = get_str_l3(cach);
char* pp = getPeakPerformance(cpu,getFrequency(freq)); char* pp = get_str_peak_performance(cpu,get_freq(freq));
setAttribute(art,ATTRIBUTE_NAME,cpuName); setAttribute(art,ATTRIBUTE_NAME,cpuName);
setAttribute(art,ATTRIBUTE_FREQUENCY,maxFrequency); setAttribute(art,ATTRIBUTE_FREQUENCY,maxFrequency);
@@ -109,8 +109,8 @@ int main(int argc, char* argv[]) {
free(cpu); free(cpu);
free(art); free(art);
freeCache(cach); free_cache_struct(cach);
freeFrequency(freq); free_freq_struct(freq);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@@ -3,76 +3,68 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <stdbool.h>
#include "standart.h" #include "standart.h"
#include "cpuid.h" #include "cpuid.h"
#include "udev.h" #include "udev.h"
#define BOOLEAN_TRUE 1 #define STRING_YES "Yes"
#define BOOLEAN_FALSE 0 #define STRING_NO "No"
/*** #define VENDOR_INTEL_STRING "GenuineIntel"
#define VENDOR_AMD_STRING "AuthenticAMD"
MASTER PAGE /*
http://www.sandpile.org/x86/cpuid.htm * cpuid reference: http://www.sandpile.org/x86/cpuid.htm
*/
***/
struct cpuInfo { struct cpuInfo {
/*** BOOLEAN VALUES ***/ bool AVX;
bool AVX2;
/* (256 bits) */ bool AVX512;
int AVX; bool SSE;
int AVX2; bool SSE2;
bool SSE3;
/* (512 bits) */ bool SSSE3;
int AVX512; bool SSE4a;
bool SSE4_1;
/* (128 bits) */ bool SSE4_2;
int SSE; bool FMA3;
int SSE2; bool FMA4;
int SSE3; bool AES;
int SSSE3; bool SHA;
int SSE4a;
int SSE4_1;
int SSE4_2;
int FMA3;
int FMA4;
int AES;
int SHA;
VENDOR cpu_vendor; VENDOR cpu_vendor;
/*** Number of threads ***/
int nThreads; int nThreads;
/*** Threads per core(Intel HyperThreading) ***/ // Threads per core
int HT; int HT;
/*** Max CPUIDs levels ***/ // Max cpuids levels
unsigned maxLevels; unsigned int maxLevels;
/*** Max CPUIDs extended levels ***/ // Max cpuids extended levels
unsigned maxExtendedLevels; unsigned int maxExtendedLevels;
}; };
void initializeCpuInfo(struct cpuInfo* cpu) { void init_cpu_info(struct cpuInfo* cpu) {
cpu->AVX = BOOLEAN_FALSE; cpu->AVX = false;
cpu->AVX2 = BOOLEAN_FALSE; cpu->AVX2 = false;
cpu->AVX512 = BOOLEAN_FALSE; cpu->AVX512 = false;
cpu->SSE = BOOLEAN_FALSE; cpu->SSE = false;
cpu->SSE2 = BOOLEAN_FALSE; cpu->SSE2 = false;
cpu->SSE3 = BOOLEAN_FALSE; cpu->SSE3 = false;
cpu->SSSE3 = BOOLEAN_FALSE; cpu->SSSE3 = false;
cpu->SSE4a = BOOLEAN_FALSE; cpu->SSE4a = false;
cpu->SSE4_1 = BOOLEAN_FALSE; cpu->SSE4_1 = false;
cpu->SSE4_2 = BOOLEAN_FALSE; cpu->SSE4_2 = false;
cpu->FMA3 = BOOLEAN_FALSE; cpu->FMA3 = false;
cpu->FMA4 = BOOLEAN_FALSE; cpu->FMA4 = false;
cpu->AES = BOOLEAN_FALSE; cpu->AES = false;
cpu->SHA = BOOLEAN_FALSE; cpu->SHA = false;
} }
#define MASK 0xFF #define MASK 0xFF
VENDOR getCPUVendor(unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) { VENDOR get_cpu_vendor_internal(unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) {
char name[13]; char name[13];
memset(name,0,13); memset(name,0,13);
name[__COUNTER__] = ebx & MASK; name[__COUNTER__] = ebx & MASK;
@@ -99,10 +91,9 @@ VENDOR getCPUVendor(unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) {
return VENDOR_INVALID; return VENDOR_INVALID;
} }
struct cpuInfo* getCPUInfo() { struct cpuInfo* get_cpu_info() {
struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo)); struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));
memset(cpu,0,sizeof(struct cpuInfo)); init_cpu_info(cpu);
initializeCpuInfo(cpu);
unsigned eax = 0; unsigned eax = 0;
unsigned ebx = 0; unsigned ebx = 0;
@@ -115,7 +106,7 @@ struct cpuInfo* getCPUInfo() {
cpu->maxLevels = eax; cpu->maxLevels = eax;
//Fill vendor //Fill vendor
cpu->cpu_vendor = getCPUVendor(eax,ebx,ecx,edx); cpu->cpu_vendor = get_cpu_vendor_internal(eax,ebx,ecx,edx);
if(cpu->cpu_vendor == VENDOR_INVALID) { if(cpu->cpu_vendor == VENDOR_INVALID) {
printf("ERROR: CPU vendor is neither AMD nor INTEL\n"); printf("ERROR: CPU vendor is neither AMD nor INTEL\n");
return NULL; return NULL;
@@ -141,7 +132,7 @@ struct cpuInfo* getCPUInfo() {
} }
} }
else { else {
//We can afford this check, assume 1 //We cant afford this check, assume 1
cpu->HT = 1; cpu->HT = 1;
} }
@@ -187,7 +178,11 @@ struct cpuInfo* getCPUInfo() {
return cpu; return cpu;
} }
void debugCpuInfo(struct cpuInfo* cpu) { VENDOR get_cpu_vendor(struct cpuInfo* cpu) {
return cpu->cpu_vendor;
}
void debug_cpu_info(struct cpuInfo* cpu) {
printf("AVX=%s\n", cpu->AVX ? "true" : "false"); printf("AVX=%s\n", cpu->AVX ? "true" : "false");
printf("AVX2=%s\n", cpu->AVX2 ? "true" : "false"); printf("AVX2=%s\n", cpu->AVX2 ? "true" : "false");
printf("AVX512=%s\n\n", cpu->AVX512 ? "true" : "false"); printf("AVX512=%s\n\n", cpu->AVX512 ? "true" : "false");
@@ -209,7 +204,7 @@ void debugCpuInfo(struct cpuInfo* cpu) {
/*** STRING FUNCTIONS ***/ /*** STRING FUNCTIONS ***/
char* getPeakPerformance(struct cpuInfo* cpu, long freq) { char* get_str_peak_performance(struct cpuInfo* cpu, long freq) {
/*** /***
PP = PeakPerformance PP = PeakPerformance
SP = SinglePrecision SP = SinglePrecision
@@ -255,11 +250,7 @@ char* getPeakPerformance(struct cpuInfo* cpu, long freq) {
return string; return string;
} }
VENDOR getCPUVendorInternal(struct cpuInfo* cpu) { char* get_str_ncores(struct cpuInfo* cpu) {
return cpu->cpu_vendor;
}
char* getString_NumberCores(struct cpuInfo* cpu) {
if(cpu->HT > 1) { if(cpu->HT > 1) {
//2(N.Cores)7(' cores(')3(N.Threads)9(' threads)') //2(N.Cores)7(' cores(')3(N.Threads)9(' threads)')
int size = 2+7+3+9+1; int size = 2+7+3+9+1;
@@ -275,14 +266,14 @@ char* getString_NumberCores(struct cpuInfo* cpu) {
} }
char* getString_AVX(struct cpuInfo* cpu) { char* get_str_avx(struct cpuInfo* cpu) {
//If all AVX are available, it will use up to 15 //If all AVX are available, it will use up to 15
char* string = malloc(sizeof(char)*15+1); char* string = malloc(sizeof(char)*15+1);
if(cpu->AVX == BOOLEAN_FALSE) if(!cpu->AVX)
snprintf(string,2+1,"No"); snprintf(string,2+1,"No");
else if(cpu->AVX2 == BOOLEAN_FALSE) else if(!cpu->AVX2)
snprintf(string,3+1,"AVX"); snprintf(string,3+1,"AVX");
else if(cpu->AVX512 == BOOLEAN_FALSE) else if(!cpu->AVX512)
snprintf(string,8+1,"AVX,AVX2"); snprintf(string,8+1,"AVX,AVX2");
else else
snprintf(string,15+1,"AVX,AVX2,AVX512"); snprintf(string,15+1,"AVX,AVX2,AVX512");
@@ -290,7 +281,7 @@ char* getString_AVX(struct cpuInfo* cpu) {
return string; return string;
} }
char* getString_SSE(struct cpuInfo* cpu) { char* get_str_sse(struct cpuInfo* cpu) {
int last = 0; int last = 0;
int SSE_sl = 4; int SSE_sl = 4;
int SSE2_sl = 5; int SSE2_sl = 5;
@@ -301,31 +292,31 @@ char* getString_SSE(struct cpuInfo* cpu) {
int SSE4_2_sl = 7; int SSE4_2_sl = 7;
char* string = malloc(sizeof(char)*SSE_sl+SSE2_sl+SSE3_sl+SSSE3_sl+SSE4a_sl+SSE4_1_sl+SSE4_2_sl+1); char* string = malloc(sizeof(char)*SSE_sl+SSE2_sl+SSE3_sl+SSSE3_sl+SSE4a_sl+SSE4_1_sl+SSE4_2_sl+1);
if(cpu->SSE == BOOLEAN_TRUE) { if(!cpu->SSE) {
snprintf(string+last,SSE_sl+1,"SSE,"); snprintf(string+last,SSE_sl+1,"SSE,");
last+=SSE_sl; last+=SSE_sl;
} }
if(cpu->SSE2 == BOOLEAN_TRUE) { if(!cpu->SSE2) {
snprintf(string+last,SSE2_sl+1,"SSE2,"); snprintf(string+last,SSE2_sl+1,"SSE2,");
last+=SSE2_sl; last+=SSE2_sl;
} }
if(cpu->SSE3 == BOOLEAN_TRUE) { if(!cpu->SSE3) {
snprintf(string+last,SSE3_sl+1,"SSE3,"); snprintf(string+last,SSE3_sl+1,"SSE3,");
last+=SSE3_sl; last+=SSE3_sl;
} }
if(cpu->SSSE3 == BOOLEAN_TRUE) { if(!cpu->SSSE3) {
snprintf(string+last,SSSE3_sl+1,"SSSE3,"); snprintf(string+last,SSSE3_sl+1,"SSSE3,");
last+=SSSE3_sl; last+=SSSE3_sl;
} }
if(cpu->SSE4a == BOOLEAN_TRUE) { if(!cpu->SSE4a) {
snprintf(string+last,SSE4a_sl+1,"SSE4a,"); snprintf(string+last,SSE4a_sl+1,"SSE4a,");
last+=SSE4a_sl; last+=SSE4a_sl;
} }
if(cpu->SSE4_1 == BOOLEAN_TRUE) { if(!cpu->SSE4_1) {
snprintf(string+last,SSE4_1_sl+1,"SSE4_1,"); snprintf(string+last,SSE4_1_sl+1,"SSE4_1,");
last+=SSE4_1_sl; last+=SSE4_1_sl;
} }
if(cpu->SSE4_2 == BOOLEAN_TRUE) { if(!cpu->SSE4_2) {
snprintf(string+last,SSE4_2_sl+1,"SSE4_2,"); snprintf(string+last,SSE4_2_sl+1,"SSE4_2,");
last+=SSE4_2_sl; last+=SSE4_2_sl;
} }
@@ -335,11 +326,11 @@ char* getString_SSE(struct cpuInfo* cpu) {
return string; return string;
} }
char* getString_FMA(struct cpuInfo* cpu) { char* get_str_fma(struct cpuInfo* cpu) {
char* string = malloc(sizeof(char)*9+1); char* string = malloc(sizeof(char)*9+1);
if(cpu->FMA3 == BOOLEAN_FALSE) if(!cpu->FMA3)
snprintf(string,2+1,"No"); snprintf(string,2+1,"No");
else if(cpu->FMA4 == BOOLEAN_FALSE) else if(!cpu->FMA4)
snprintf(string,4+1,"FMA3"); snprintf(string,4+1,"FMA3");
else else
snprintf(string,9+1,"FMA3,FMA4"); snprintf(string,9+1,"FMA3,FMA4");
@@ -347,18 +338,18 @@ char* getString_FMA(struct cpuInfo* cpu) {
return string; return string;
} }
char* getString_AES(struct cpuInfo* cpu) { char* get_str_aes(struct cpuInfo* cpu) {
char* string = malloc(sizeof(char)*3+1); char* string = malloc(sizeof(char)*3+1);
if(cpu->AES == BOOLEAN_TRUE) if(cpu->AES)
snprintf(string,3+1,STRING_YES); snprintf(string,3+1,STRING_YES);
else else
snprintf(string,2+1,STRING_NO); snprintf(string,2+1,STRING_NO);
return string; return string;
} }
char* getString_SHA(struct cpuInfo* cpu) { char* get_str_sha(struct cpuInfo* cpu) {
char* string = malloc(sizeof(char)*3+1); char* string = malloc(sizeof(char)*3+1);
if(cpu->SHA == BOOLEAN_TRUE) if(cpu->SHA)
snprintf(string,3+1,STRING_YES); snprintf(string,3+1,STRING_YES);
else else
snprintf(string,2+1,STRING_NO); snprintf(string,2+1,STRING_NO);

View File

@@ -1,30 +1,24 @@
#ifndef __01h__ #ifndef __01h__
#define __01h__ #define __01h__
#define STRING_YES "Yes"
#define STRING_NO "No"
#define VENDOR_EMPTY 0 #define VENDOR_EMPTY 0
#define VENDOR_INTEL 1 #define VENDOR_INTEL 1
#define VENDOR_AMD 2 #define VENDOR_AMD 2
#define VENDOR_INVALID 3 #define VENDOR_INVALID 3
#define VENDOR_INTEL_STRING "GenuineIntel"
#define VENDOR_AMD_STRING "AuthenticAMD"
typedef int VENDOR; typedef int VENDOR;
struct cpuInfo; struct cpuInfo* get_cpu_info();
VENDOR get_cpu_vendor(struct cpuInfo* cpu);
struct cpuInfo* getCPUInfo(); char* get_str_peak_performance(struct cpuInfo* cpu, long freq);
void debugCpuInfo(struct cpuInfo* cpu); char* get_str_ncores(struct cpuInfo* cpu);
char* get_str_avx(struct cpuInfo* cpu);
char* get_str_sse(struct cpuInfo* cpu);
char* get_str_fma(struct cpuInfo* cpu);
char* get_str_aes(struct cpuInfo* cpu);
char* get_str_sha(struct cpuInfo* cpu);
VENDOR getCPUVendorInternal(struct cpuInfo* cpu); void debug_cpu_info(struct cpuInfo* cpu);
char* getPeakPerformance(struct cpuInfo* cpu, long freq);
char* getString_NumberCores(struct cpuInfo* cpu);
char* getString_AVX(struct cpuInfo* cpu);
char* getString_SSE(struct cpuInfo* cpu);
char* getString_FMA(struct cpuInfo* cpu);
char* getString_AES(struct cpuInfo* cpu);
char* getString_SHA(struct cpuInfo* cpu);
#endif #endif

View File

@@ -6,6 +6,20 @@
#include "udev.h" #include "udev.h"
#define _PATH_SYS_SYSTEM "/sys/devices/system"
#define _PATH_SYS_CPU _PATH_SYS_SYSTEM"/cpu"
#define _PATH_ONE_CPU _PATH_SYS_CPU"/cpu0"
#define _PATH_FREQUENCY _PATH_ONE_CPU"/cpufreq"
#define _PATH_FREQUENCY_MAX _PATH_FREQUENCY"/cpuinfo_max_freq"
#define _PATH_FREQUENCY_MIN _PATH_FREQUENCY"/cpuinfo_min_freq"
#define _PATH_CPU_CACHE _PATH_ONE_CPU"/cache"
#define _PATH_CACHE_L1d _PATH_CPU_CACHE"/index0/size"
#define _PATH_CACHE_L1i _PATH_CPU_CACHE"/index1/size"
#define _PATH_CACHE_L2 _PATH_CPU_CACHE"/index2/size"
#define _PATH_CACHE_L3 _PATH_CPU_CACHE"/index3/size"
struct cache { struct cache {
int L1i; int L1i;
int L1d; int L1d;
@@ -18,14 +32,8 @@ struct frequency {
long min; long min;
}; };
/*** // Parses buf which should be expressed in the way: xxxxK where 'x' are numbers and 'K' refers to kilobytes.
// Returns the size as a int in bytes
Parses buf which should be expressed in the way:
xxxxK where 'x' are numbers and 'K' refers to kilobytes.
Returns the size as a int in bytes
***/
int getSize(char* buf, int size) { int getSize(char* buf, int size) {
char* end = strstr (buf,"K"); char* end = strstr (buf,"K");
if(end == NULL) { if(end == NULL) {
@@ -41,15 +49,8 @@ int getSize(char* buf, int size) {
return cachsize; return cachsize;
} }
/*** // Returns size(in bytes) of cache described by path or UNKNOWN if the cache doest no exists
//TODO: We use reads in various places, refactor
Returns size(in bytes) of cache described by path or
UNKNOWN if the cache doest no exists
***/
//Sacar factor comun lectura
//Block pasar de 1 a 1000
int getCache(char* path) { int getCache(char* path) {
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
@@ -75,16 +76,12 @@ int getCache(char* path) {
int ret = getSize(buf,offset)*1024; int ret = getSize(buf,offset)*1024;
free(buf); free(buf);
fclose(file); fclose(file);
return ret; return ret;
} }
/*** // Returns CPU frequency in Hz
long get_freq_from_file(char* path) {
Returns CPU frequency in Hz
***/
long getFrequencyFromFile(char* path) {
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
if(file == NULL) { if(file == NULL) {
@@ -107,20 +104,19 @@ long getFrequencyFromFile(char* path) {
int ret = atoi(buf); int ret = atoi(buf);
free(buf); free(buf);
if(ret == 0) { if(ret == 0) {
printf("error in getFrequencyFromFile\n"); printf("error in get_freq_from_file\n");
return UNKNOWN; return UNKNOWN;
} }
fclose(file); fclose(file);
return (long)ret*1000; return (long)ret*1000;
} }
long getFrequency(struct frequency* freq) { long get_freq(struct frequency* freq) {
return freq->max; return freq->max;
} }
/*** GET_STRING ***/ // String functions
char* get_str_l1(struct cache* cach) {
char* getString_L1(struct cache* cach) {
//Max 2 digits,2 for 'KB',6 for '(Data)' //Max 2 digits,2 for 'KB',6 for '(Data)'
//and 14 for '(Instructions)' //and 14 for '(Instructions)'
int size = (2*(2+2)+6+14+1); int size = (2*(2+2)+6+14+1);
@@ -129,7 +125,7 @@ char* getString_L1(struct cache* cach) {
return string; return string;
} }
char* getString_L2(struct cache* cach) { char* get_str_l2(struct cache* cach) {
if(cach->L2 == UNKNOWN) { if(cach->L2 == UNKNOWN) {
char* string = malloc(sizeof(char)*5); char* string = malloc(sizeof(char)*5);
snprintf(string,5,STRING_NONE); snprintf(string,5,STRING_NONE);
@@ -144,7 +140,7 @@ char* getString_L2(struct cache* cach) {
} }
} }
char* getString_L3(struct cache* cach) { char* get_str_l3(struct cache* cach) {
if(cach->L3 == UNKNOWN) { if(cach->L3 == UNKNOWN) {
char* string = malloc(sizeof(char)*5); char* string = malloc(sizeof(char)*5);
snprintf(string,5,STRING_NONE); snprintf(string,5,STRING_NONE);
@@ -159,7 +155,7 @@ char* getString_L3(struct cache* cach) {
} }
} }
char* getString_MaxFrequency(struct frequency* freq) { char* get_str_freq(struct frequency* freq) {
//Max 3 digits and 3 for '(M/G)Hz' plus 1 for '\0' //Max 3 digits and 3 for '(M/G)Hz' plus 1 for '\0'
unsigned int size = (4+3+1); unsigned int size = (4+3+1);
assert(strlen(STRING_UNKNOWN)+1 <= size); assert(strlen(STRING_UNKNOWN)+1 <= size);
@@ -173,34 +169,34 @@ char* getString_MaxFrequency(struct frequency* freq) {
return string; return string;
} }
/*** CREATES AND FREES ***/ struct cache* get_cache_info() {
struct cache* new_cache() {
struct cache* cach = malloc(sizeof(struct cache)); struct cache* cach = malloc(sizeof(struct cache));
cach->L1i = getCache(_PATH_CACHE_L1i); cach->L1i = getCache(_PATH_CACHE_L1i);
cach->L1d = getCache(_PATH_CACHE_L1d); cach->L1d = getCache(_PATH_CACHE_L1d);
cach->L2 = getCache(_PATH_CACHE_L2); cach->L2 = getCache(_PATH_CACHE_L2);
cach->L3 = getCache(_PATH_CACHE_L3); cach->L3 = getCache(_PATH_CACHE_L3);
return cach; return cach;
} }
struct frequency* new_frequency() { struct frequency* get_frequency_info() {
struct frequency* freq = malloc(sizeof(struct frequency)); struct frequency* freq = malloc(sizeof(struct frequency));
freq->max = getFrequencyFromFile(_PATH_FREQUENCY_MAX);
freq->min = getFrequencyFromFile(_PATH_FREQUENCY_MIN); freq->max = get_freq_from_file(_PATH_FREQUENCY_MAX);
freq->min = get_freq_from_file(_PATH_FREQUENCY_MIN);
return freq; return freq;
} }
void freeCache(struct cache* cach) { void free_cache_struct(struct cache* cach) {
free(cach); free(cach);
} }
void freeFrequency(struct frequency* freq) { void free_freq_struct(struct frequency* freq) {
free(freq); free(freq);
} }
/*** DEBUGING ***/
void debugCache(struct cache* cach) { void debugCache(struct cache* cach) {
printf("L1i=%dB\n",cach->L1i); printf("L1i=%dB\n",cach->L1i);
printf("L1d=%dB\n",cach->L1d); printf("L1d=%dB\n",cach->L1d);

View File

@@ -1,53 +1,31 @@
#ifndef __UDEV__ #ifndef __UDEV__
#define __UDEV__ #define __UDEV__
/*** PATHS ***/
#define _PATH_SYS_SYSTEM "/sys/devices/system"
#define _PATH_SYS_CPU _PATH_SYS_SYSTEM"/cpu"
#define _PATH_ONE_CPU _PATH_SYS_CPU"/cpu0"
#define _PATH_FREQUENCY _PATH_ONE_CPU"/cpufreq"
#define _PATH_FREQUENCY_MAX _PATH_FREQUENCY"/cpuinfo_max_freq"
#define _PATH_FREQUENCY_MIN _PATH_FREQUENCY"/cpuinfo_min_freq"
#define _PATH_CPU_CACHE _PATH_ONE_CPU"/cache"
#define _PATH_CACHE_L1d _PATH_CPU_CACHE"/index0/size"
#define _PATH_CACHE_L1i _PATH_CPU_CACHE"/index1/size"
#define _PATH_CACHE_L2 _PATH_CPU_CACHE"/index2/size"
#define _PATH_CACHE_L3 _PATH_CPU_CACHE"/index3/size"
/*** CONSTANTS ***/
#define UNKNOWN -1 #define UNKNOWN -1
#define DEFAULT_FILE_SIZE 4096 #define DEFAULT_FILE_SIZE 4096
#define DEFAULT_BLOCK_SIZE 128 #define DEFAULT_BLOCK_SIZE 128
/*** STRINGS ***/
#define STRING_UNKNOWN "Unknown" #define STRING_UNKNOWN "Unknown"
#define STRING_NONE "None" #define STRING_NONE "None"
#define STRING_MEGAHERZ "MHz" #define STRING_MEGAHERZ "MHz"
#define STRING_GIGAHERZ "GHz" #define STRING_GIGAHERZ "GHz"
#define STRING_KILOBYTES "KB" #define STRING_KILOBYTES "KB"
/*** STRUCTS ***/
struct cache; struct cache;
struct frequency; struct frequency;
/*** FUNCTIONS ***/ struct cache* get_cache_info();
struct frequency* get_frequency_info();
void free_cache_struct(struct cache* cach);
void free_freq_struct(struct frequency* freq);
struct cache* new_cache(); char* get_str_l1(struct cache* cach);
void debugCache(struct cache* cach); char* get_str_l2(struct cache* cach);
void freeCache(struct cache* cach); char* get_str_l3(struct cache* cach);
char* getString_L1(struct cache* cach); char* get_str_freq(struct frequency* freq);
char* getString_L2(struct cache* cach); long get_freq(struct frequency* freq);
char* getString_L3(struct cache* cach);
struct frequency* new_frequency();
void debugFrequency(struct frequency* freq); void debugFrequency(struct frequency* freq);
void freeFrequency(struct frequency* freq); void debugCache(struct cache* cach);
char* getString_MaxFrequency(struct frequency* freq);
long getFrequency(struct frequency* freq);
#endif #endif