mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 16:00:39 +01:00
[v0.98] Use malloc/calloc wrapper that exits when alloc fails, as suggested by #90
This commit is contained in:
@@ -71,7 +71,7 @@ bool show_debug() {
|
||||
}
|
||||
|
||||
bool show_raw() {
|
||||
return args.raw_flag;
|
||||
return args.raw_flag;
|
||||
}
|
||||
|
||||
bool verbose_enabled() {
|
||||
@@ -83,20 +83,20 @@ int max_arg_str_length() {
|
||||
int len = sizeof(args_str) / sizeof(args_str[0]);
|
||||
for(int i=0; i < len; i++) {
|
||||
max_len = max(max_len, (int) strlen(args_str[i]));
|
||||
}
|
||||
}
|
||||
return max_len;
|
||||
}
|
||||
|
||||
STYLE parse_style(char* style) {
|
||||
uint8_t i = 0;
|
||||
uint8_t styles_count = sizeof(SYTLES_STR_LIST) / sizeof(SYTLES_STR_LIST[0]);
|
||||
|
||||
|
||||
while(i != styles_count && (SYTLES_STR_LIST[i] == NULL || strcmp(SYTLES_STR_LIST[i], style) != 0))
|
||||
i++;
|
||||
|
||||
if(i == styles_count)
|
||||
return STYLE_INVALID;
|
||||
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -109,11 +109,11 @@ void free_colors_struct(struct colors* cs) {
|
||||
}
|
||||
|
||||
bool parse_color(char* optarg_str, struct colors** cs) {
|
||||
*cs = malloc(sizeof(struct colors));
|
||||
(*cs)->c1 = malloc(sizeof(struct color));
|
||||
(*cs)->c2 = malloc(sizeof(struct color));
|
||||
(*cs)->c3 = malloc(sizeof(struct color));
|
||||
(*cs)->c4 = malloc(sizeof(struct color));
|
||||
*cs = emalloc(sizeof(struct colors));
|
||||
(*cs)->c1 = emalloc(sizeof(struct color));
|
||||
(*cs)->c2 = emalloc(sizeof(struct color));
|
||||
(*cs)->c3 = emalloc(sizeof(struct color));
|
||||
(*cs)->c4 = emalloc(sizeof(struct color));
|
||||
struct color** c1 = &((*cs)->c1);
|
||||
struct color** c2 = &((*cs)->c2);
|
||||
struct color** c3 = &((*cs)->c3);
|
||||
@@ -121,43 +121,43 @@ bool parse_color(char* optarg_str, struct colors** cs) {
|
||||
int32_t ret;
|
||||
char* str_to_parse = NULL;
|
||||
bool free_ptr;
|
||||
|
||||
|
||||
if(strcmp(optarg_str, COLOR_STR_INTEL) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 46);
|
||||
str_to_parse = emalloc(sizeof(char) * 46);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_INTEL);
|
||||
free_ptr = true;
|
||||
}
|
||||
else if(strcmp(optarg_str, COLOR_STR_AMD) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 44);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_AMD);
|
||||
str_to_parse = emalloc(sizeof(char) * 44);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_AMD);
|
||||
free_ptr = true;
|
||||
}
|
||||
else if(strcmp(optarg_str, COLOR_STR_IBM) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 45);
|
||||
str_to_parse = emalloc(sizeof(char) * 45);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_IBM);
|
||||
free_ptr = true;
|
||||
}
|
||||
else if(strcmp(optarg_str, COLOR_STR_ARM) == 0) {
|
||||
str_to_parse = malloc(sizeof(char) * 46);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_ARM);
|
||||
str_to_parse = emalloc(sizeof(char) * 46);
|
||||
strcpy(str_to_parse, COLOR_DEFAULT_ARM);
|
||||
free_ptr = true;
|
||||
}
|
||||
else {
|
||||
else {
|
||||
str_to_parse = optarg_str;
|
||||
free_ptr = false;
|
||||
}
|
||||
|
||||
ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
|
||||
|
||||
ret = sscanf(str_to_parse, "%d,%d,%d:%d,%d,%d:%d,%d,%d:%d,%d,%d",
|
||||
&(*c1)->R, &(*c1)->G, &(*c1)->B,
|
||||
&(*c2)->R, &(*c2)->G, &(*c2)->B,
|
||||
&(*c3)->R, &(*c3)->G, &(*c3)->B,
|
||||
&(*c4)->R, &(*c4)->G, &(*c4)->B);
|
||||
|
||||
|
||||
if(ret != 12) {
|
||||
printErr("Expected to read 12 values for color but read %d", ret);
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//TODO: Refactor c1->R c2->R ... to c[i]->R
|
||||
if((*c1)->R < 0 || (*c1)->R > 255) {
|
||||
printErr("Red in color 1 is invalid. Must be in range (0, 255)");
|
||||
@@ -182,17 +182,17 @@ bool parse_color(char* optarg_str, struct colors** cs) {
|
||||
if((*c2)->B < 0 || (*c2)->B > 255) {
|
||||
printErr("Blue in color 2 is invalid. Must be in range (0, 255)");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(free_ptr) free (str_to_parse);
|
||||
|
||||
return true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char* build_short_options() {
|
||||
const char *c = args_chr;
|
||||
int len = sizeof(args_chr) / sizeof(args_chr[0]);
|
||||
char* str = (char *) malloc(sizeof(char) * (len*2 + 1));
|
||||
char* str = (char *) emalloc(sizeof(char) * (len*2 + 1));
|
||||
memset(str, 0, sizeof(char) * (len*2 + 1));
|
||||
|
||||
#ifdef ARCH_X86
|
||||
@@ -210,7 +210,7 @@ char* build_short_options() {
|
||||
|
||||
bool parse_args(int argc, char* argv[]) {
|
||||
int opt;
|
||||
int option_index = 0;
|
||||
int option_index = 0;
|
||||
opterr = 0;
|
||||
|
||||
bool color_flag = false;
|
||||
@@ -243,7 +243,7 @@ bool parse_args(int argc, char* argv[]) {
|
||||
printErr("Color option specified more than once");
|
||||
return false;
|
||||
}
|
||||
color_flag = true;
|
||||
color_flag = true;
|
||||
if(!parse_color(optarg, &args.colors)) {
|
||||
printErr("Color parsing failed");
|
||||
return false;
|
||||
@@ -276,7 +276,7 @@ bool parse_args(int argc, char* argv[]) {
|
||||
else if(opt == args_chr[ARG_VERSION]) {
|
||||
args.version_flag = true;
|
||||
}
|
||||
else {
|
||||
else {
|
||||
printWarn("Invalid options");
|
||||
args.help_flag = true;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ char* get_str_cpu_name(struct cpuInfo* cpu) {
|
||||
|
||||
#if defined(ARCH_X86) || defined(ARCH_PPC)
|
||||
char* get_str_sockets(struct topology* topo) {
|
||||
char* string = malloc(sizeof(char) * 2);
|
||||
char* string = emalloc(sizeof(char) * 2);
|
||||
int32_t sanity_ret = snprintf(string, 2, "%d", topo->sockets);
|
||||
if(sanity_ret < 0) {
|
||||
printBug("get_str_sockets: snprintf returned a negative value for input: '%d'", topo->sockets);
|
||||
@@ -56,44 +56,44 @@ uint32_t get_nsockets(struct topology* topo) {
|
||||
|
||||
int32_t get_value_as_smallest_unit(char ** str, uint32_t value) {
|
||||
int32_t sanity_ret;
|
||||
*str = malloc(sizeof(char)* 11); //8 for digits, 2 for units
|
||||
*str = emalloc(sizeof(char)* 11); //8 for digits, 2 for units
|
||||
|
||||
if(value/1024 >= 1024)
|
||||
sanity_ret = snprintf(*str, 10,"%.4g"STRING_MEGABYTES, (double)value/(1<<20));
|
||||
else
|
||||
sanity_ret = snprintf(*str, 10,"%.4g"STRING_KILOBYTES, (double)value/(1<<10));
|
||||
|
||||
sanity_ret = snprintf(*str, 10,"%.4g"STRING_KILOBYTES, (double)value/(1<<10));
|
||||
|
||||
return sanity_ret;
|
||||
}
|
||||
|
||||
// String functions
|
||||
// String functions
|
||||
char* get_str_cache_two(int32_t cache_size, uint32_t physical_cores) {
|
||||
// 4 for digits, 2 for units, 2 for ' (', 3 digits, 2 for units and 7 for ' Total)'
|
||||
uint32_t max_size = 4+2 + 2 + 4+2 + 7 + 1;
|
||||
int32_t sanity_ret;
|
||||
char* string = malloc(sizeof(char) * max_size);
|
||||
char* string = emalloc(sizeof(char) * max_size);
|
||||
char* tmp1;
|
||||
char* tmp2;
|
||||
char* tmp2;
|
||||
int32_t tmp1_len = get_value_as_smallest_unit(&tmp1, cache_size);
|
||||
int32_t tmp2_len = get_value_as_smallest_unit(&tmp2, cache_size * physical_cores);
|
||||
|
||||
|
||||
if(tmp1_len < 0) {
|
||||
printBug("get_value_as_smallest_unit: snprintf returned a negative value for input: %d\n", cache_size);
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
if(tmp2_len < 0) {
|
||||
printBug("get_value_as_smallest_unit: snprintf returned a negative value for input: %d\n", cache_size * physical_cores);
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
uint32_t size = tmp1_len + 2 + tmp2_len + 7 + 1;
|
||||
sanity_ret = snprintf(string, size, "%s (%s Total)", tmp1, tmp2);
|
||||
|
||||
sanity_ret = snprintf(string, size, "%s (%s Total)", tmp1, tmp2);
|
||||
|
||||
if(sanity_ret < 0) {
|
||||
printBug("get_str_cache_two: snprintf returned a negative value for input: '%s' and '%s'\n", tmp1, tmp2);
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
free(tmp1);
|
||||
free(tmp2);
|
||||
return string;
|
||||
@@ -103,21 +103,21 @@ char* get_str_cache_one(int32_t cache_size) {
|
||||
// 4 for digits, 2 for units, 2 for ' (', 3 digits, 2 for units and 7 for ' Total)'
|
||||
uint32_t max_size = 4+2 + 1;
|
||||
int32_t sanity_ret;
|
||||
char* string = malloc(sizeof(char) * max_size);
|
||||
char* string = emalloc(sizeof(char) * max_size);
|
||||
char* tmp;
|
||||
int32_t tmp_len = get_value_as_smallest_unit(&tmp, cache_size);
|
||||
|
||||
|
||||
if(tmp_len < 0) {
|
||||
printBug("get_value_as_smallest_unit: snprintf returned a negative value for input: %d", cache_size);
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
uint32_t size = tmp_len + 1;
|
||||
sanity_ret = snprintf(string, size, "%s", tmp);
|
||||
|
||||
|
||||
if(sanity_ret < 0) {
|
||||
printBug("get_str_cache_one: snprintf returned a negative value for input: '%s'", tmp);
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
free(tmp);
|
||||
return string;
|
||||
@@ -145,7 +145,7 @@ char* get_str_l2(struct cache* cach) {
|
||||
|
||||
char* get_str_l3(struct cache* cach) {
|
||||
if(!cach->L3->exists)
|
||||
return NULL;
|
||||
return NULL;
|
||||
return get_str_cache(cach->L3->size, cach->L3->num_caches);
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ char* get_str_freq(struct frequency* freq) {
|
||||
//Max 3 digits and 3 for '(M/G)Hz' plus 1 for '\0'
|
||||
uint32_t size = (5+1+3+1);
|
||||
assert(strlen(STRING_UNKNOWN)+1 <= size);
|
||||
char* string = malloc(sizeof(char)*size);
|
||||
char* string = emalloc(sizeof(char)*size);
|
||||
memset(string, 0, sizeof(char)*size);
|
||||
|
||||
if(freq->max == UNKNOWN_FREQ || freq->max < 0)
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "global.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -68,3 +72,25 @@ void set_log_level(bool verbose) {
|
||||
int max(int a, int b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
void* emalloc(size_t size) {
|
||||
void* ptr = malloc(size);
|
||||
|
||||
if(ptr == NULL) {
|
||||
printErr("malloc failed: %s", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* ecalloc(size_t nmemb, size_t size) {
|
||||
void* ptr = calloc(nmemb, size);
|
||||
|
||||
if(ptr == NULL) {
|
||||
printErr("calloc failed: %s", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@@ -8,5 +8,7 @@ void printWarn(const char *fmt, ...);
|
||||
void printErr(const char *fmt, ...);
|
||||
void printBug(const char *fmt, ...);
|
||||
int max(int a, int b);
|
||||
void* emalloc(size_t size);
|
||||
void* ecalloc(size_t nmemb, size_t size);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -107,7 +107,7 @@ static const char* ATTRIBUTE_FIELDS [] = {
|
||||
|
||||
struct attribute {
|
||||
int type;
|
||||
char* value;
|
||||
char* value;
|
||||
};
|
||||
|
||||
struct ascii {
|
||||
@@ -129,14 +129,14 @@ void setAttribute(struct ascii* art, int type, char* value) {
|
||||
art->attributes[art->n_attributes_set]->value = value;
|
||||
art->attributes[art->n_attributes_set]->type = type;
|
||||
art->n_attributes_set++;
|
||||
|
||||
|
||||
if(art->n_attributes_set > MAX_ATTRIBUTES) {
|
||||
printBug("Set %d attributes, while max value is %d!", art->n_attributes_set, MAX_ATTRIBUTES);
|
||||
}
|
||||
}
|
||||
|
||||
char* rgb_to_ansi(struct color* c, bool background, bool bold) {
|
||||
char* str = malloc(sizeof(char) * 100);
|
||||
char* str = emalloc(sizeof(char) * 100);
|
||||
if(background) {
|
||||
snprintf(str, 44, "\x1b[48;2;%.3d;%.3d;%.3dm", c->R, c->G, c->B);
|
||||
}
|
||||
@@ -152,19 +152,19 @@ char* rgb_to_ansi(struct color* c, bool background, bool bold) {
|
||||
|
||||
struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) {
|
||||
char *COL_FANCY_1, *COL_FANCY_2, *COL_FANCY_3, *COL_FANCY_4, *COL_RETRO_1, *COL_RETRO_2, *COL_RETRO_3, *COL_RETRO_4;
|
||||
struct ascii* art = malloc(sizeof(struct ascii));
|
||||
struct ascii* art = emalloc(sizeof(struct ascii));
|
||||
art->n_attributes_set = 0;
|
||||
art->additional_spaces = 0;
|
||||
art->vendor = vendor;
|
||||
art->attributes = malloc(sizeof(struct attribute *) * MAX_ATTRIBUTES);
|
||||
art->attributes = emalloc(sizeof(struct attribute *) * MAX_ATTRIBUTES);
|
||||
for(uint32_t i=0; i < MAX_ATTRIBUTES; i++) {
|
||||
art->attributes[i] = malloc(sizeof(struct attribute));
|
||||
art->attributes[i] = emalloc(sizeof(struct attribute));
|
||||
art->attributes[i]->type = 0;
|
||||
art->attributes[i]->value = NULL;
|
||||
}
|
||||
strcpy(art->reset, COLOR_RESET);
|
||||
|
||||
#ifdef ARCH_X86
|
||||
#ifdef ARCH_X86
|
||||
if(art->vendor == CPU_VENDOR_INTEL) {
|
||||
COL_FANCY_1 = COLOR_BG_CYAN;
|
||||
COL_FANCY_2 = COLOR_BG_WHITE;
|
||||
@@ -176,11 +176,11 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) {
|
||||
COL_FANCY_1 = COLOR_BG_WHITE;
|
||||
COL_FANCY_2 = COLOR_BG_GREEN;
|
||||
COL_FANCY_3 = COLOR_FG_WHITE;
|
||||
COL_FANCY_4 = COLOR_FG_GREEN;
|
||||
COL_FANCY_4 = COLOR_FG_GREEN;
|
||||
art->ascii_chars[0] = '@';
|
||||
}
|
||||
else {
|
||||
printBug("Invalid CPU vendor in set_ascii (%d)", art->vendor);
|
||||
printBug("Invalid CPU vendor in set_ascii (%d)", art->vendor);
|
||||
return NULL;
|
||||
}
|
||||
#elif ARCH_PPC
|
||||
@@ -321,7 +321,7 @@ struct ascii* set_ascii(VENDOR vendor, STYLE style, struct colors* cs) {
|
||||
}
|
||||
|
||||
char tmp[NUMBER_OF_LINES * LINE_SIZE + 1];
|
||||
#ifdef ARCH_X86
|
||||
#ifdef ARCH_X86
|
||||
if(art->vendor == CPU_VENDOR_INTEL)
|
||||
strcpy(tmp, INTEL_ASCII);
|
||||
else if(art->vendor == CPU_VENDOR_AMD)
|
||||
@@ -391,7 +391,7 @@ void print_algorithm_intel(struct ascii* art, int n, bool* flag) {
|
||||
|
||||
void print_algorithm_amd(struct ascii* art, int n, bool* flag) {
|
||||
*flag = false; // dummy, just silence compiler error
|
||||
|
||||
|
||||
for(int i=0; i < LINE_SIZE; i++) {
|
||||
if(art->art[n][i] == '@')
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
@@ -402,7 +402,7 @@ void print_algorithm_amd(struct ascii* art, int n, bool* flag) {
|
||||
}
|
||||
}
|
||||
|
||||
void print_ascii_x86(struct ascii* art, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int i, bool* flag)) {
|
||||
void print_ascii_x86(struct ascii* art, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int i, bool* flag)) {
|
||||
int attr_to_print = 0;
|
||||
int attr_type;
|
||||
char* attr_value;
|
||||
@@ -419,7 +419,7 @@ void print_ascii_x86(struct ascii* art, uint32_t la, void (*callback_print_algor
|
||||
attr_type = art->attributes[attr_to_print]->type;
|
||||
attr_value = art->attributes[attr_to_print]->value;
|
||||
attr_to_print++;
|
||||
|
||||
|
||||
space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_type]));
|
||||
printf("%s%s%s%*s%s%s%s\n", art->color1_text, ATTRIBUTE_FIELDS[attr_type], art->reset, space_right, "", art->color2_text, attr_value, art->reset);
|
||||
}
|
||||
@@ -430,7 +430,7 @@ void print_ascii_x86(struct ascii* art, uint32_t la, void (*callback_print_algor
|
||||
|
||||
void print_ascii(struct ascii* art) {
|
||||
uint32_t longest_attribute = longest_attribute_length(art);
|
||||
|
||||
|
||||
if(art->vendor == CPU_VENDOR_INTEL)
|
||||
print_ascii_x86(art, longest_attribute, &print_algorithm_intel);
|
||||
else if(art->vendor == CPU_VENDOR_AMD)
|
||||
@@ -438,14 +438,14 @@ void print_ascii(struct ascii* art) {
|
||||
else {
|
||||
printBug("Invalid CPU vendor: %d\n", art->vendor);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs);
|
||||
if(art == NULL)
|
||||
return false;
|
||||
|
||||
return false;
|
||||
|
||||
char* uarch = get_str_uarch(cpu);
|
||||
char* manufacturing_process = get_str_process(cpu);
|
||||
char* sockets = get_str_sockets(cpu->topo);
|
||||
@@ -480,7 +480,7 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
setAttribute(art,ATTRIBUTE_NCORES,n_cores);
|
||||
}
|
||||
setAttribute(art,ATTRIBUTE_AVX,avx);
|
||||
setAttribute(art,ATTRIBUTE_FMA,fma);
|
||||
setAttribute(art,ATTRIBUTE_FMA,fma);
|
||||
setAttribute(art,ATTRIBUTE_L1i,l1i);
|
||||
setAttribute(art,ATTRIBUTE_L1d,l1d);
|
||||
setAttribute(art,ATTRIBUTE_L2,l2);
|
||||
@@ -488,7 +488,7 @@ bool print_cpufetch_x86(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
setAttribute(art,ATTRIBUTE_L3,l3);
|
||||
}
|
||||
setAttribute(art,ATTRIBUTE_PEAK,pp);
|
||||
|
||||
|
||||
if(art->n_attributes_set > NUMBER_OF_LINES) {
|
||||
printBug("The number of attributes set is bigger than the max that can be displayed");
|
||||
return false;
|
||||
@@ -566,8 +566,8 @@ void print_ascii(struct ascii* art) {
|
||||
bool print_cpufetch_ppc(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
struct ascii* art = set_ascii(get_cpu_vendor(cpu), s, cs);
|
||||
if(art == NULL)
|
||||
return false;
|
||||
|
||||
return false;
|
||||
|
||||
char* uarch = get_str_uarch(cpu);
|
||||
char* manufacturing_process = get_str_process(cpu);
|
||||
char* sockets = get_str_sockets(cpu->topo);
|
||||
@@ -624,25 +624,25 @@ void print_algorithm_snapd_mtk(struct ascii* art, int n) {
|
||||
if(art->art[n][i] == '@')
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
else if(art->art[n][i] == '#')
|
||||
printf("%s%c%s", art->color2_ascii, art->ascii_chars[1], art->reset);
|
||||
printf("%s%c%s", art->color2_ascii, art->ascii_chars[1], art->reset);
|
||||
else
|
||||
printf("%c",art->art[n][i]);
|
||||
printf("%c",art->art[n][i]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_algorithm_samsung(struct ascii* art, int n) {
|
||||
int y_margin = 2;
|
||||
int x_margin = 2 * y_margin;
|
||||
|
||||
|
||||
for(int i=0; i < LINE_SIZE; i++) {
|
||||
if(art->art[n][i] == '#') {
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
}
|
||||
else if((n >= y_margin && n < NUMBER_OF_LINES-y_margin) && (i >= x_margin && i < LINE_SIZE-x_margin)) {
|
||||
if(art->art[n][i] == '#')
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
else
|
||||
printf("%s%c%s","\x1b[48;2;10;10;10m" COLOR_FG_WHITE, art->art[n][i], art->reset);
|
||||
printf("%s%c%s","\x1b[48;2;10;10;10m" COLOR_FG_WHITE, art->art[n][i], art->reset);
|
||||
}
|
||||
else
|
||||
printf("%c", art->art[n][i]);
|
||||
@@ -650,21 +650,21 @@ void print_algorithm_samsung(struct ascii* art, int n) {
|
||||
}
|
||||
|
||||
void print_algorithm_arm(struct ascii* art, int n) {
|
||||
for(int i=0; i < LINE_SIZE; i++) {
|
||||
for(int i=0; i < LINE_SIZE; i++) {
|
||||
if(art->art[n][i] == '#')
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
printf("%s%c%s", art->color1_ascii, art->ascii_chars[0], art->reset);
|
||||
else
|
||||
printf("%c",art->art[n][i]);
|
||||
printf("%c",art->art[n][i]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_ascii_arm(struct ascii* art, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int n)) {
|
||||
void print_ascii_arm(struct ascii* art, uint32_t la, void (*callback_print_algorithm)(struct ascii* art, int n)) {
|
||||
int attr_to_print = 0;
|
||||
int attr_type;
|
||||
char* attr_value;
|
||||
uint32_t limit_up;
|
||||
uint32_t limit_down;
|
||||
|
||||
|
||||
uint32_t space_right;
|
||||
uint32_t space_up = (NUMBER_OF_LINES - art->n_attributes_set)/2;
|
||||
uint32_t space_down = NUMBER_OF_LINES - art->n_attributes_set - space_up;
|
||||
@@ -678,7 +678,7 @@ void print_ascii_arm(struct ascii* art, uint32_t la, void (*callback_print_algor
|
||||
}
|
||||
bool add_space = false;
|
||||
uint32_t len = max(art->n_attributes_set, NUMBER_OF_LINES);
|
||||
|
||||
|
||||
for(uint32_t n=0; n < len; n++) {
|
||||
if(n >= art->additional_spaces && n < NUMBER_OF_LINES + art->additional_spaces)
|
||||
callback_print_algorithm(art, n - art->additional_spaces);
|
||||
@@ -689,14 +689,14 @@ void print_ascii_arm(struct ascii* art, uint32_t la, void (*callback_print_algor
|
||||
attr_type = art->attributes[attr_to_print]->type;
|
||||
attr_value = art->attributes[attr_to_print]->value;
|
||||
attr_to_print++;
|
||||
|
||||
|
||||
if(attr_type == ATTRIBUTE_PEAK) {
|
||||
add_space = false;
|
||||
}
|
||||
if(attr_type == ATTRIBUTE_CPU_NUM) {
|
||||
printf("%s%s%s\n", art->color1_text, attr_value, art->reset);
|
||||
add_space = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(add_space) {
|
||||
space_right = 1 + (la - strlen(ATTRIBUTE_FIELDS[attr_type]));
|
||||
@@ -715,30 +715,30 @@ void print_ascii_arm(struct ascii* art, uint32_t la, void (*callback_print_algor
|
||||
|
||||
void print_ascii(struct ascii* art) {
|
||||
uint32_t longest_attribute = longest_attribute_length(art);
|
||||
|
||||
|
||||
if(art->vendor == SOC_VENDOR_SNAPDRAGON || art->vendor == SOC_VENDOR_MEDIATEK || art->vendor == SOC_VENDOR_KIRIN || art->vendor == SOC_VENDOR_BROADCOM)
|
||||
print_ascii_arm(art, longest_attribute, &print_algorithm_snapd_mtk);
|
||||
print_ascii_arm(art, longest_attribute, &print_algorithm_snapd_mtk);
|
||||
else if(art->vendor == SOC_VENDOR_EXYNOS)
|
||||
print_ascii_arm(art, longest_attribute, &print_algorithm_samsung);
|
||||
print_ascii_arm(art, longest_attribute, &print_algorithm_samsung);
|
||||
else {
|
||||
if(art->vendor != SOC_VENDOR_UNKNOWN)
|
||||
printWarn("Invalid SOC vendor: %d\n", art->vendor);
|
||||
print_ascii_arm(art, longest_attribute, &print_algorithm_arm);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
struct ascii* art = set_ascii(get_soc_vendor(cpu->soc), s, cs);
|
||||
if(art == NULL)
|
||||
return false;
|
||||
|
||||
return false;
|
||||
|
||||
char* manufacturing_process = get_str_process(cpu->soc);
|
||||
char* soc_name = get_soc_name(cpu->soc);
|
||||
char* features = get_str_features(cpu);
|
||||
setAttribute(art,ATTRIBUTE_SOC,soc_name);
|
||||
setAttribute(art,ATTRIBUTE_TECHNOLOGY,manufacturing_process);
|
||||
|
||||
|
||||
if(cpu->num_cpus == 1) {
|
||||
char* uarch = get_str_uarch(cpu);
|
||||
char* max_frequency = get_str_freq(cpu->freq);
|
||||
@@ -752,20 +752,20 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
* Cache functionality may be implemented
|
||||
* in the future
|
||||
*/
|
||||
|
||||
|
||||
setAttribute(art,ATTRIBUTE_UARCH,uarch);
|
||||
setAttribute(art,ATTRIBUTE_FREQUENCY,max_frequency);
|
||||
setAttribute(art,ATTRIBUTE_NCORES,n_cores);
|
||||
if(features != NULL) {
|
||||
setAttribute(art, ATTRIBUTE_FEATURES, features);
|
||||
}
|
||||
setAttribute(art, ATTRIBUTE_FEATURES, features);
|
||||
}
|
||||
}
|
||||
else {
|
||||
struct cpuInfo* ptr = cpu;
|
||||
struct cpuInfo* ptr = cpu;
|
||||
for(int i = 0; i < cpu->num_cpus; ptr = ptr->next_cpu, i++) {
|
||||
char* uarch = get_str_uarch(ptr);
|
||||
char* max_frequency = get_str_freq(ptr->freq);
|
||||
char* n_cores = get_str_topology(ptr, ptr->topo, false);
|
||||
char* n_cores = get_str_topology(ptr, ptr->topo, false);
|
||||
/*
|
||||
* char* l1i = get_str_l1i(cpu->cach);
|
||||
* char* l1d = get_str_l1d(cpu->cach);
|
||||
@@ -775,22 +775,22 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
* Cache functionality may be implemented
|
||||
* in the future
|
||||
*/
|
||||
|
||||
char* cpu_num = malloc(sizeof(char) * 9);
|
||||
|
||||
char* cpu_num = emalloc(sizeof(char) * 9);
|
||||
sprintf(cpu_num, "CPU %d:", i+1);
|
||||
setAttribute(art, ATTRIBUTE_CPU_NUM, cpu_num);
|
||||
setAttribute(art, ATTRIBUTE_UARCH, uarch);
|
||||
setAttribute(art, ATTRIBUTE_FREQUENCY, max_frequency);
|
||||
setAttribute(art, ATTRIBUTE_NCORES, n_cores);
|
||||
if(features != NULL) {
|
||||
setAttribute(art, ATTRIBUTE_FEATURES, features);
|
||||
}
|
||||
setAttribute(art, ATTRIBUTE_FEATURES, features);
|
||||
}
|
||||
}
|
||||
}
|
||||
char* pp = get_str_peak_performance(cpu);
|
||||
setAttribute(art,ATTRIBUTE_PEAK,pp);
|
||||
|
||||
if(art->n_attributes_set > NUMBER_OF_LINES) {
|
||||
|
||||
if(art->n_attributes_set > NUMBER_OF_LINES) {
|
||||
art->additional_spaces = (art->n_attributes_set - NUMBER_OF_LINES) / 2;
|
||||
}
|
||||
if(cpu->hv->present)
|
||||
@@ -798,7 +798,7 @@ bool print_cpufetch_arm(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
|
||||
print_ascii(art);
|
||||
|
||||
free(manufacturing_process);
|
||||
free(manufacturing_process);
|
||||
free(pp);
|
||||
|
||||
free(art->attributes);
|
||||
@@ -823,7 +823,7 @@ bool print_cpufetch(struct cpuInfo* cpu, STYLE s, struct colors* cs) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef ARCH_X86
|
||||
return print_cpufetch_x86(cpu, s, cs);
|
||||
#elif ARCH_PPC
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
char* read_file(char* path, int* len) {
|
||||
int fd = open(path, O_RDONLY);
|
||||
|
||||
|
||||
if(fd == -1) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -13,17 +13,17 @@ char* read_file(char* path, int* len) {
|
||||
int bytes_read = 0;
|
||||
int offset = 0;
|
||||
int block = 128;
|
||||
char* buf = malloc(sizeof(char)*DEFAULT_FILE_SIZE);
|
||||
char* buf = emalloc(sizeof(char)*DEFAULT_FILE_SIZE);
|
||||
memset(buf, 0, sizeof(char)*DEFAULT_FILE_SIZE);
|
||||
|
||||
while ( (bytes_read = read(fd, buf+offset, block)) > 0 ) {
|
||||
offset += bytes_read;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (close(fd) == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
*len = offset;
|
||||
return buf;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ long get_freq_from_file(char* path, bool hv_present) {
|
||||
free(buf);
|
||||
return UNKNOWN_FREQ;
|
||||
}
|
||||
|
||||
|
||||
// We will be getting the frequency in KHz
|
||||
// We consider it is an error if frequency is
|
||||
// greater than 10 GHz or less than 100 MHz
|
||||
@@ -57,9 +57,9 @@ long get_freq_from_file(char* path, bool hv_present) {
|
||||
printBug("Invalid data was read from file '%s': %ld\n", path, ret);
|
||||
return UNKNOWN_FREQ;
|
||||
}
|
||||
|
||||
|
||||
free(buf);
|
||||
|
||||
|
||||
return ret/1000;
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ int get_num_caches_from_files(char** paths, int num_paths) {
|
||||
int SHARED_MAP_MAX_LEN = 8 + 1;
|
||||
int filelen;
|
||||
char* buf;
|
||||
uint32_t* shared_maps = malloc(sizeof(uint32_t *) * num_paths);
|
||||
uint32_t* shared_maps = emalloc(sizeof(uint32_t *) * num_paths);
|
||||
|
||||
// 1. Read cpu_shared_map from every core
|
||||
for(int i=0; i < num_paths; i++) {
|
||||
@@ -158,7 +158,7 @@ int get_num_caches_from_files(char** paths, int num_paths) {
|
||||
// 2. Count number of different masks; this is the number of caches
|
||||
int num_caches = 0;
|
||||
bool found = false;
|
||||
uint32_t* unique_shared_maps = malloc(sizeof(uint32_t *) * num_paths);
|
||||
uint32_t* unique_shared_maps = emalloc(sizeof(uint32_t *) * num_paths);
|
||||
for(int i=0; i < num_paths; i++) unique_shared_maps[i] = 0;
|
||||
|
||||
for(int i=0; i < num_paths; i++) {
|
||||
@@ -176,7 +176,7 @@ int get_num_caches_from_files(char** paths, int num_paths) {
|
||||
}
|
||||
|
||||
int get_num_caches_by_level(struct cpuInfo* cpu, uint32_t level) {
|
||||
char** paths = malloc(sizeof(char *) * cpu->topo->total_cores);
|
||||
char** paths = emalloc(sizeof(char *) * cpu->topo->total_cores);
|
||||
char* cache_path = NULL;
|
||||
|
||||
if(level == 0) cache_path = _PATH_CACHE_L1I;
|
||||
@@ -189,7 +189,7 @@ int get_num_caches_by_level(struct cpuInfo* cpu, uint32_t level) {
|
||||
}
|
||||
|
||||
for(int i=0; i < cpu->topo->total_cores; i++) {
|
||||
paths[i] = malloc(sizeof(char) * _PATH_CACHE_MAX_LEN);
|
||||
paths[i] = emalloc(sizeof(char) * _PATH_CACHE_MAX_LEN);
|
||||
sprintf(paths[i], "%s%s/cpu%d%s%s", _PATH_SYS_SYSTEM, _PATH_SYS_CPU, i, cache_path, _PATH_CACHE_SHARED_MAP);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user