mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 07:50:40 +01:00
Big refactoring
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
#include <string.h>
|
||||
#include "extended.h"
|
||||
|
||||
char* getString_CPUName() {
|
||||
unsigned eax = 0;
|
||||
unsigned ebx = 0;
|
||||
unsigned ecx = 0;
|
||||
unsigned edx = 0;
|
||||
char* get_str_cpu_name() {
|
||||
unsigned int eax = 0;
|
||||
unsigned int ebx = 0;
|
||||
unsigned int ecx = 0;
|
||||
unsigned int edx = 0;
|
||||
|
||||
char name[64];
|
||||
memset(name,0,64);
|
||||
|
||||
@@ -5,6 +5,6 @@
|
||||
#include "cpuid.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
char* getString_CPUName();
|
||||
char* get_str_cpu_name();
|
||||
|
||||
#endif
|
||||
|
||||
70
src/main.c
70
src/main.c
@@ -25,21 +25,21 @@ 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[]) {
|
||||
printf("Usage: %s [--version] [--help] [--style STYLE]\n\
|
||||
Options: \n\
|
||||
--style Set logo style color\n\
|
||||
default: Default style color\n\
|
||||
dark: Dark style color\n\
|
||||
--help Print this help and exit\n\
|
||||
--version Print cpufetch version and exit\n",
|
||||
argv[0]);
|
||||
void print_help(int argc, char *argv[]) {
|
||||
printf("Usage: %s [--version] [--help] [--style STYLE]\n\
|
||||
Options: \n\
|
||||
--style Set logo style color\n\
|
||||
default: Default style color\n\
|
||||
dark: Dark style color\n\
|
||||
--help Print this help and exit\n\
|
||||
--version Print cpufetch version and exit\n",
|
||||
argv[0]);
|
||||
}
|
||||
|
||||
void version() {
|
||||
printf("cpufetch v%s\n",VERSION);
|
||||
void print_version() {
|
||||
printf("cpufetch v%s\n",VERSION);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
@@ -47,37 +47,37 @@ int main(int argc, char* argv[]) {
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if(showHelp()) {
|
||||
help(argc,argv);
|
||||
print_help(argc, argv);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if(showVersion()) {
|
||||
version();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if(showVersion()) {
|
||||
print_version();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
struct cpuInfo* cpu = getCPUInfo();
|
||||
struct cpuInfo* cpu = get_cpu_info();
|
||||
if(cpu == NULL)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
struct cache* cach = new_cache();
|
||||
struct frequency* freq = new_frequency();
|
||||
struct ascii* art = set_ascii(getCPUVendorInternal(cpu),getStyle());
|
||||
struct cache* cach = get_cache_info();
|
||||
struct frequency* freq = get_frequency_info();
|
||||
struct ascii* art = set_ascii(get_cpu_vendor(cpu),getStyle());
|
||||
if(art == NULL)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
char* cpuName = getString_CPUName();
|
||||
char* maxFrequency = getString_MaxFrequency(freq);
|
||||
char* nCores = getString_NumberCores(cpu);
|
||||
char* avx = getString_AVX(cpu);
|
||||
char* sse = getString_SSE(cpu);
|
||||
char* fma = getString_FMA(cpu);
|
||||
char* aes = getString_AES(cpu);
|
||||
char* sha = getString_SHA(cpu);
|
||||
char* l1 = getString_L1(cach);
|
||||
char* l2 = getString_L2(cach);
|
||||
char* l3 = getString_L3(cach);
|
||||
char* pp = getPeakPerformance(cpu,getFrequency(freq));
|
||||
char* cpuName = get_str_cpu_name();
|
||||
char* maxFrequency = get_str_freq(freq);
|
||||
char* nCores = get_str_ncores(cpu);
|
||||
char* avx = get_str_avx(cpu);
|
||||
char* sse = get_str_sse(cpu);
|
||||
char* fma = get_str_fma(cpu);
|
||||
char* aes = get_str_aes(cpu);
|
||||
char* sha = get_str_sha(cpu);
|
||||
char* l1 = get_str_l1(cach);
|
||||
char* l2 = get_str_l2(cach);
|
||||
char* l3 = get_str_l3(cach);
|
||||
char* pp = get_str_peak_performance(cpu,get_freq(freq));
|
||||
|
||||
setAttribute(art,ATTRIBUTE_NAME,cpuName);
|
||||
setAttribute(art,ATTRIBUTE_FREQUENCY,maxFrequency);
|
||||
@@ -109,8 +109,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
free(cpu);
|
||||
free(art);
|
||||
freeCache(cach);
|
||||
freeFrequency(freq);
|
||||
free_cache_struct(cach);
|
||||
free_freq_struct(freq);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
173
src/standart.c
173
src/standart.c
@@ -3,76 +3,68 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "standart.h"
|
||||
#include "cpuid.h"
|
||||
#include "udev.h"
|
||||
|
||||
#define BOOLEAN_TRUE 1
|
||||
#define BOOLEAN_FALSE 0
|
||||
#define STRING_YES "Yes"
|
||||
#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 {
|
||||
/*** BOOLEAN VALUES ***/
|
||||
|
||||
/* (256 bits) */
|
||||
int AVX;
|
||||
int AVX2;
|
||||
|
||||
/* (512 bits) */
|
||||
int AVX512;
|
||||
|
||||
/* (128 bits) */
|
||||
int SSE;
|
||||
int SSE2;
|
||||
int SSE3;
|
||||
int SSSE3;
|
||||
int SSE4a;
|
||||
int SSE4_1;
|
||||
int SSE4_2;
|
||||
|
||||
int FMA3;
|
||||
int FMA4;
|
||||
|
||||
int AES;
|
||||
int SHA;
|
||||
bool AVX;
|
||||
bool AVX2;
|
||||
bool AVX512;
|
||||
bool SSE;
|
||||
bool SSE2;
|
||||
bool SSE3;
|
||||
bool SSSE3;
|
||||
bool SSE4a;
|
||||
bool SSE4_1;
|
||||
bool SSE4_2;
|
||||
bool FMA3;
|
||||
bool FMA4;
|
||||
bool AES;
|
||||
bool SHA;
|
||||
|
||||
VENDOR cpu_vendor;
|
||||
/*** Number of threads ***/
|
||||
|
||||
int nThreads;
|
||||
/*** Threads per core(Intel HyperThreading) ***/
|
||||
// Threads per core
|
||||
int HT;
|
||||
/*** Max CPUIDs levels ***/
|
||||
unsigned maxLevels;
|
||||
/*** Max CPUIDs extended levels ***/
|
||||
unsigned maxExtendedLevels;
|
||||
// Max cpuids levels
|
||||
unsigned int maxLevels;
|
||||
// Max cpuids extended levels
|
||||
unsigned int maxExtendedLevels;
|
||||
};
|
||||
|
||||
void initializeCpuInfo(struct cpuInfo* cpu) {
|
||||
cpu->AVX = BOOLEAN_FALSE;
|
||||
cpu->AVX2 = BOOLEAN_FALSE;
|
||||
cpu->AVX512 = BOOLEAN_FALSE;
|
||||
cpu->SSE = BOOLEAN_FALSE;
|
||||
cpu->SSE2 = BOOLEAN_FALSE;
|
||||
cpu->SSE3 = BOOLEAN_FALSE;
|
||||
cpu->SSSE3 = BOOLEAN_FALSE;
|
||||
cpu->SSE4a = BOOLEAN_FALSE;
|
||||
cpu->SSE4_1 = BOOLEAN_FALSE;
|
||||
cpu->SSE4_2 = BOOLEAN_FALSE;
|
||||
cpu->FMA3 = BOOLEAN_FALSE;
|
||||
cpu->FMA4 = BOOLEAN_FALSE;
|
||||
cpu->AES = BOOLEAN_FALSE;
|
||||
cpu->SHA = BOOLEAN_FALSE;
|
||||
void init_cpu_info(struct cpuInfo* cpu) {
|
||||
cpu->AVX = false;
|
||||
cpu->AVX2 = false;
|
||||
cpu->AVX512 = false;
|
||||
cpu->SSE = false;
|
||||
cpu->SSE2 = false;
|
||||
cpu->SSE3 = false;
|
||||
cpu->SSSE3 = false;
|
||||
cpu->SSE4a = false;
|
||||
cpu->SSE4_1 = false;
|
||||
cpu->SSE4_2 = false;
|
||||
cpu->FMA3 = false;
|
||||
cpu->FMA4 = false;
|
||||
cpu->AES = false;
|
||||
cpu->SHA = false;
|
||||
}
|
||||
|
||||
#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];
|
||||
memset(name,0,13);
|
||||
name[__COUNTER__] = ebx & MASK;
|
||||
@@ -99,10 +91,9 @@ VENDOR getCPUVendor(unsigned eax,unsigned ebx,unsigned ecx,unsigned edx) {
|
||||
return VENDOR_INVALID;
|
||||
}
|
||||
|
||||
struct cpuInfo* getCPUInfo() {
|
||||
struct cpuInfo* get_cpu_info() {
|
||||
struct cpuInfo* cpu = malloc(sizeof(struct cpuInfo));
|
||||
memset(cpu,0,sizeof(struct cpuInfo));
|
||||
initializeCpuInfo(cpu);
|
||||
init_cpu_info(cpu);
|
||||
|
||||
unsigned eax = 0;
|
||||
unsigned ebx = 0;
|
||||
@@ -115,7 +106,7 @@ struct cpuInfo* getCPUInfo() {
|
||||
cpu->maxLevels = eax;
|
||||
|
||||
//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) {
|
||||
printf("ERROR: CPU vendor is neither AMD nor INTEL\n");
|
||||
return NULL;
|
||||
@@ -141,7 +132,7 @@ struct cpuInfo* getCPUInfo() {
|
||||
}
|
||||
}
|
||||
else {
|
||||
//We can afford this check, assume 1
|
||||
//We cant afford this check, assume 1
|
||||
cpu->HT = 1;
|
||||
}
|
||||
|
||||
@@ -154,8 +145,8 @@ struct cpuInfo* getCPUInfo() {
|
||||
cpu->SSE3 = (ecx & ((int)1 << 0)) != 0;
|
||||
|
||||
cpu->SSSE3 = (ecx & ((int)1 << 9)) != 0;
|
||||
cpu->SSE4_1 = (ecx & ((int)1 << 19)) != 0;
|
||||
cpu->SSE4_2 = (ecx & ((int)1 << 20)) != 0;
|
||||
cpu->SSE4_1 = (ecx & ((int)1 << 19)) != 0;
|
||||
cpu->SSE4_2 = (ecx & ((int)1 << 20)) != 0;
|
||||
|
||||
cpu->AES = (ecx & ((int)1 << 25)) != 0;
|
||||
|
||||
@@ -169,12 +160,12 @@ struct cpuInfo* getCPUInfo() {
|
||||
cpu->AVX2 = (ebx & ((int)1 << 5)) != 0;
|
||||
cpu->SHA = (ebx & ((int)1 << 29)) != 0;
|
||||
cpu->AVX512 = (((ebx & ((int)1 << 16)) != 0) ||
|
||||
((ebx & ((int)1 << 28)) != 0) ||
|
||||
((ebx & ((int)1 << 26)) != 0) ||
|
||||
((ebx & ((int)1 << 27)) != 0) ||
|
||||
((ebx & ((int)1 << 31)) != 0) ||
|
||||
((ebx & ((int)1 << 30)) != 0) ||
|
||||
((ebx & ((int)1 << 17)) != 0) ||
|
||||
((ebx & ((int)1 << 28)) != 0) ||
|
||||
((ebx & ((int)1 << 26)) != 0) ||
|
||||
((ebx & ((int)1 << 27)) != 0) ||
|
||||
((ebx & ((int)1 << 31)) != 0) ||
|
||||
((ebx & ((int)1 << 30)) != 0) ||
|
||||
((ebx & ((int)1 << 17)) != 0) ||
|
||||
((ebx & ((int)1 << 21)) != 0));
|
||||
}
|
||||
if (cpu->maxExtendedLevels >= 0x80000001){
|
||||
@@ -187,7 +178,11 @@ struct cpuInfo* getCPUInfo() {
|
||||
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("AVX2=%s\n", cpu->AVX2 ? "true" : "false");
|
||||
printf("AVX512=%s\n\n", cpu->AVX512 ? "true" : "false");
|
||||
@@ -209,7 +204,7 @@ void debugCpuInfo(struct cpuInfo* cpu) {
|
||||
|
||||
/*** STRING FUNCTIONS ***/
|
||||
|
||||
char* getPeakPerformance(struct cpuInfo* cpu, long freq) {
|
||||
char* get_str_peak_performance(struct cpuInfo* cpu, long freq) {
|
||||
/***
|
||||
PP = PeakPerformance
|
||||
SP = SinglePrecision
|
||||
@@ -255,11 +250,7 @@ char* getPeakPerformance(struct cpuInfo* cpu, long freq) {
|
||||
return string;
|
||||
}
|
||||
|
||||
VENDOR getCPUVendorInternal(struct cpuInfo* cpu) {
|
||||
return cpu->cpu_vendor;
|
||||
}
|
||||
|
||||
char* getString_NumberCores(struct cpuInfo* cpu) {
|
||||
char* get_str_ncores(struct cpuInfo* cpu) {
|
||||
if(cpu->HT > 1) {
|
||||
//2(N.Cores)7(' cores(')3(N.Threads)9(' threads)')
|
||||
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
|
||||
char* string = malloc(sizeof(char)*15+1);
|
||||
if(cpu->AVX == BOOLEAN_FALSE)
|
||||
if(!cpu->AVX)
|
||||
snprintf(string,2+1,"No");
|
||||
else if(cpu->AVX2 == BOOLEAN_FALSE)
|
||||
else if(!cpu->AVX2)
|
||||
snprintf(string,3+1,"AVX");
|
||||
else if(cpu->AVX512 == BOOLEAN_FALSE)
|
||||
else if(!cpu->AVX512)
|
||||
snprintf(string,8+1,"AVX,AVX2");
|
||||
else
|
||||
snprintf(string,15+1,"AVX,AVX2,AVX512");
|
||||
@@ -290,7 +281,7 @@ char* getString_AVX(struct cpuInfo* cpu) {
|
||||
return string;
|
||||
}
|
||||
|
||||
char* getString_SSE(struct cpuInfo* cpu) {
|
||||
char* get_str_sse(struct cpuInfo* cpu) {
|
||||
int last = 0;
|
||||
int SSE_sl = 4;
|
||||
int SSE2_sl = 5;
|
||||
@@ -301,31 +292,31 @@ char* getString_SSE(struct cpuInfo* cpu) {
|
||||
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);
|
||||
|
||||
if(cpu->SSE == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSE) {
|
||||
snprintf(string+last,SSE_sl+1,"SSE,");
|
||||
last+=SSE_sl;
|
||||
}
|
||||
if(cpu->SSE2 == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSE2) {
|
||||
snprintf(string+last,SSE2_sl+1,"SSE2,");
|
||||
last+=SSE2_sl;
|
||||
}
|
||||
if(cpu->SSE3 == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSE3) {
|
||||
snprintf(string+last,SSE3_sl+1,"SSE3,");
|
||||
last+=SSE3_sl;
|
||||
}
|
||||
if(cpu->SSSE3 == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSSE3) {
|
||||
snprintf(string+last,SSSE3_sl+1,"SSSE3,");
|
||||
last+=SSSE3_sl;
|
||||
}
|
||||
if(cpu->SSE4a == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSE4a) {
|
||||
snprintf(string+last,SSE4a_sl+1,"SSE4a,");
|
||||
last+=SSE4a_sl;
|
||||
}
|
||||
if(cpu->SSE4_1 == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSE4_1) {
|
||||
snprintf(string+last,SSE4_1_sl+1,"SSE4_1,");
|
||||
last+=SSE4_1_sl;
|
||||
}
|
||||
if(cpu->SSE4_2 == BOOLEAN_TRUE) {
|
||||
if(!cpu->SSE4_2) {
|
||||
snprintf(string+last,SSE4_2_sl+1,"SSE4_2,");
|
||||
last+=SSE4_2_sl;
|
||||
}
|
||||
@@ -335,11 +326,11 @@ char* getString_SSE(struct cpuInfo* cpu) {
|
||||
return string;
|
||||
}
|
||||
|
||||
char* getString_FMA(struct cpuInfo* cpu) {
|
||||
char* get_str_fma(struct cpuInfo* cpu) {
|
||||
char* string = malloc(sizeof(char)*9+1);
|
||||
if(cpu->FMA3 == BOOLEAN_FALSE)
|
||||
if(!cpu->FMA3)
|
||||
snprintf(string,2+1,"No");
|
||||
else if(cpu->FMA4 == BOOLEAN_FALSE)
|
||||
else if(!cpu->FMA4)
|
||||
snprintf(string,4+1,"FMA3");
|
||||
else
|
||||
snprintf(string,9+1,"FMA3,FMA4");
|
||||
@@ -347,18 +338,18 @@ char* getString_FMA(struct cpuInfo* cpu) {
|
||||
return string;
|
||||
}
|
||||
|
||||
char* getString_AES(struct cpuInfo* cpu) {
|
||||
char* get_str_aes(struct cpuInfo* cpu) {
|
||||
char* string = malloc(sizeof(char)*3+1);
|
||||
if(cpu->AES == BOOLEAN_TRUE)
|
||||
if(cpu->AES)
|
||||
snprintf(string,3+1,STRING_YES);
|
||||
else
|
||||
snprintf(string,2+1,STRING_NO);
|
||||
return string;
|
||||
}
|
||||
|
||||
char* getString_SHA(struct cpuInfo* cpu) {
|
||||
char* get_str_sha(struct cpuInfo* cpu) {
|
||||
char* string = malloc(sizeof(char)*3+1);
|
||||
if(cpu->SHA == BOOLEAN_TRUE)
|
||||
if(cpu->SHA)
|
||||
snprintf(string,3+1,STRING_YES);
|
||||
else
|
||||
snprintf(string,2+1,STRING_NO);
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
#ifndef __01h__
|
||||
#define __01h__
|
||||
|
||||
#define STRING_YES "Yes"
|
||||
#define STRING_NO "No"
|
||||
|
||||
#define VENDOR_EMPTY 0
|
||||
#define VENDOR_INTEL 1
|
||||
#define VENDOR_AMD 2
|
||||
#define VENDOR_INVALID 3
|
||||
#define VENDOR_INTEL_STRING "GenuineIntel"
|
||||
#define VENDOR_AMD_STRING "AuthenticAMD"
|
||||
|
||||
typedef int VENDOR;
|
||||
|
||||
struct cpuInfo;
|
||||
struct cpuInfo* get_cpu_info();
|
||||
VENDOR get_cpu_vendor(struct cpuInfo* cpu);
|
||||
|
||||
struct cpuInfo* getCPUInfo();
|
||||
void debugCpuInfo(struct cpuInfo* cpu);
|
||||
char* get_str_peak_performance(struct cpuInfo* cpu, long freq);
|
||||
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);
|
||||
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);
|
||||
void debug_cpu_info(struct cpuInfo* cpu);
|
||||
|
||||
#endif
|
||||
|
||||
90
src/udev.c
90
src/udev.c
@@ -6,6 +6,20 @@
|
||||
|
||||
#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 {
|
||||
int L1i;
|
||||
int L1d;
|
||||
@@ -18,14 +32,8 @@ struct frequency {
|
||||
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) {
|
||||
char* end = strstr (buf,"K");
|
||||
if(end == NULL) {
|
||||
@@ -41,22 +49,15 @@ int getSize(char* buf, int size) {
|
||||
return cachsize;
|
||||
}
|
||||
|
||||
/***
|
||||
|
||||
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
|
||||
// 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
|
||||
int getCache(char* path) {
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
if(file == NULL) {
|
||||
if(file == NULL) {
|
||||
//Doest not exist
|
||||
return UNKNOWN;
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
//File exists, read it
|
||||
int fd = fileno(file);
|
||||
@@ -64,7 +65,7 @@ int getCache(char* path) {
|
||||
int offset = 0;
|
||||
int block = DEFAULT_BLOCK_SIZE;
|
||||
char* buf = malloc(sizeof(char)*DEFAULT_FILE_SIZE);
|
||||
memset(buf, 0, sizeof(char)*DEFAULT_FILE_SIZE);
|
||||
memset(buf, 0, sizeof(char)*DEFAULT_FILE_SIZE);
|
||||
|
||||
do {
|
||||
bytes_read = read(fd, buf+offset, block);
|
||||
@@ -73,18 +74,14 @@ int getCache(char* path) {
|
||||
|
||||
//Move size from kb to bytes
|
||||
int ret = getSize(buf,offset)*1024;
|
||||
free(buf);
|
||||
free(buf);
|
||||
fclose(file);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/***
|
||||
|
||||
Returns CPU frequency in Hz
|
||||
|
||||
***/
|
||||
|
||||
long getFrequencyFromFile(char* path) {
|
||||
// Returns CPU frequency in Hz
|
||||
long get_freq_from_file(char* path) {
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
if(file == NULL) {
|
||||
@@ -107,20 +104,19 @@ long getFrequencyFromFile(char* path) {
|
||||
int ret = atoi(buf);
|
||||
free(buf);
|
||||
if(ret == 0) {
|
||||
printf("error in getFrequencyFromFile\n");
|
||||
printf("error in get_freq_from_file\n");
|
||||
return UNKNOWN;
|
||||
}
|
||||
fclose(file);
|
||||
return (long)ret*1000;
|
||||
}
|
||||
|
||||
long getFrequency(struct frequency* freq) {
|
||||
long get_freq(struct frequency* freq) {
|
||||
return freq->max;
|
||||
}
|
||||
|
||||
/*** GET_STRING ***/
|
||||
|
||||
char* getString_L1(struct cache* cach) {
|
||||
// String functions
|
||||
char* get_str_l1(struct cache* cach) {
|
||||
//Max 2 digits,2 for 'KB',6 for '(Data)'
|
||||
//and 14 for '(Instructions)'
|
||||
int size = (2*(2+2)+6+14+1);
|
||||
@@ -129,7 +125,7 @@ char* getString_L1(struct cache* cach) {
|
||||
return string;
|
||||
}
|
||||
|
||||
char* getString_L2(struct cache* cach) {
|
||||
char* get_str_l2(struct cache* cach) {
|
||||
if(cach->L2 == UNKNOWN) {
|
||||
char* string = malloc(sizeof(char)*5);
|
||||
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) {
|
||||
char* string = malloc(sizeof(char)*5);
|
||||
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'
|
||||
unsigned int size = (4+3+1);
|
||||
assert(strlen(STRING_UNKNOWN)+1 <= size);
|
||||
@@ -173,34 +169,34 @@ char* getString_MaxFrequency(struct frequency* freq) {
|
||||
return string;
|
||||
}
|
||||
|
||||
/*** CREATES AND FREES ***/
|
||||
|
||||
struct cache* new_cache() {
|
||||
struct cache* get_cache_info() {
|
||||
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);
|
||||
cach->L3 = getCache(_PATH_CACHE_L3);
|
||||
|
||||
return cach;
|
||||
}
|
||||
|
||||
struct frequency* new_frequency() {
|
||||
struct frequency* get_frequency_info() {
|
||||
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;
|
||||
}
|
||||
|
||||
void freeCache(struct cache* cach) {
|
||||
void free_cache_struct(struct cache* cach) {
|
||||
free(cach);
|
||||
}
|
||||
|
||||
void freeFrequency(struct frequency* freq) {
|
||||
void free_freq_struct(struct frequency* freq) {
|
||||
free(freq);
|
||||
}
|
||||
|
||||
/*** DEBUGING ***/
|
||||
|
||||
void debugCache(struct cache* cach) {
|
||||
printf("L1i=%dB\n",cach->L1i);
|
||||
printf("L1d=%dB\n",cach->L1d);
|
||||
|
||||
42
src/udev.h
42
src/udev.h
@@ -1,53 +1,31 @@
|
||||
#ifndef __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 DEFAULT_FILE_SIZE 4096
|
||||
#define DEFAULT_BLOCK_SIZE 128
|
||||
|
||||
/*** STRINGS ***/
|
||||
#define STRING_UNKNOWN "Unknown"
|
||||
#define STRING_NONE "None"
|
||||
#define STRING_MEGAHERZ "MHz"
|
||||
#define STRING_GIGAHERZ "GHz"
|
||||
#define STRING_KILOBYTES "KB"
|
||||
|
||||
/*** STRUCTS ***/
|
||||
|
||||
struct cache;
|
||||
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();
|
||||
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);
|
||||
char* get_str_l1(struct cache* cach);
|
||||
char* get_str_l2(struct cache* cach);
|
||||
char* get_str_l3(struct cache* cach);
|
||||
char* get_str_freq(struct frequency* freq);
|
||||
long get_freq(struct frequency* freq);
|
||||
|
||||
struct frequency* new_frequency();
|
||||
void debugFrequency(struct frequency* freq);
|
||||
void freeFrequency(struct frequency* freq);
|
||||
char* getString_MaxFrequency(struct frequency* freq);
|
||||
long getFrequency(struct frequency* freq);
|
||||
void debugCache(struct cache* cach);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user