#include #include #include #include #include "cleanbench.h" #define BUF_SIZ 1024 /****************** ** removeNewLine ** ******************* ** Removes a trailing newline character if present */ static void removeNewLine(char * s) { int len = strlen(s); if(len > 0 && s[len - 1] == '\n') { s[len - 1] = '\0'; } } /*************** ** runCommand ** **************** ** Run the system command through a pipe ** The pointer result must point to a pre-allocated array of at least BUF_SIZ */ static void runCommand (const char *command, char *result) { FILE * pipe; pipe = popen(command, "r"); if(pipe == NULL) { /* command failed */ result[0] = '\0'; } else { if(NULL == fgets(result, BUF_SIZ, pipe)) { /* command failed */ result[0] = '\0'; } pclose(pipe); } removeNewLine(result); } /******************** ** readProcCpuInfo ** ********************* ** Reads and parses /proc/cpuinfo on a Linux system ** The pointers must point to pre-allocated arrays of at least BUF_SIZ */ static void readProcCpuInfo (char *model, char *cache) { char format[32]; proccpuinfo *info = proccpuinfo_read(); if (!info) { return; } switch (info->cpus) { case 1: strcpy(format, "%s %s %.0fMHz"); break; case 2: strcpy(format, "Dual %s %s %.0fMHz"); break; default: strcpy(format, "%d CPUs %s %s %.0fMHz"); break; } snprintf(model, BUF_SIZ, format, info->hardware_platform, info->architecture, info->frequency); snprintf(cache, BUF_SIZ, "%u KB", info->cache); proccpuinfo_free(info); } /************* ** hardware ** ************** ** Runs the system command "uname -s -r" ** Reads /proc/cpuinfo if on a linux system ** Writes output */ void hardware(void) { char os[BUF_SIZ]; char model[BUF_SIZ]; char cache[BUF_SIZ]; const char os_command[] = "uname -s -r"; #ifdef NO_UNAME os[0] = '\0'; #else runCommand(os_command, os); #endif if(NULL != strstr(os, "Linux")) { readProcCpuInfo (model, cache); } else { model[0] = '\0'; cache[0] = '\0'; } printf("CPU : %s\n", model); printf("L2 Cache : %s\n", cache); printf("OS : %s\n", os); }