Add CPU use to display.c
diff --git a/display.c b/display.c
index e1b2005..f36397f 100644
--- a/display.c
+++ b/display.c
@@ -26,11 +26,11 @@
#include "common.h"
#include "display.h"
+#include <inttypes.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
-#include <inttypes.h>
#include "log.h"
#include "util.h"
@@ -66,27 +66,55 @@
}
}
+static double getCpuUse()
+{
+ static uint64_t prevUserT = 0UL;
+ static uint64_t prevSystemT = 0UL;
+
+ FILE *f = fopen("/proc/stat", "r");
+ if (f == NULL) {
+
+ }
+ defer {
+ fclose(f);
+ };
+ uint64_t userT, niceT, systemT, idleT;
+ if (fscanf(f, "cpu %" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64, &userT, &niceT, &systemT, &idleT) != 4) {
+ LOG_W("fscanf('/proc/stat') != 4");
+ return 0U;
+ }
+
+ if (prevUserT == 0UL || prevSystemT == 0UL) {
+ prevUserT = userT;
+ prevSystemT = systemT;
+ return 0U;
+ }
+
+ double cpuUse = (double)(((systemT - prevSystemT) + (userT - prevUserT))) / sysconf(_SC_CLK_TCK);
+ prevUserT = userT;
+ prevSystemT = systemT;
+ return cpuUse * 100;
+}
+
static void display_displayLocked(honggfuzz_t * hfuzz)
{
unsigned long elapsed_second = (unsigned long)(time(NULL) - hfuzz->timeStart);
-
unsigned int day, hour, min, second;
char time_elapsed_str[64];
-
if (elapsed_second < 24 * 3600) {
hour = elapsed_second / 3600;
min = (elapsed_second - 3600 * hour) / 60;
second = elapsed_second - hour * 3600 - min * 60;
- snprintf(time_elapsed_str, sizeof(time_elapsed_str), "%u hrs %u min %u sec", hour, min,
- second);
+ snprintf(time_elapsed_str, sizeof(time_elapsed_str), "%u hrs %u min %u sec", hour,
+ min, second);
} else {
day = elapsed_second / 24 / 3600;
elapsed_second = elapsed_second - day * 24 * 3600;
hour = elapsed_second / 3600;
min = (elapsed_second - 3600 * hour) / 60;
second = elapsed_second - hour * 3600 - min * 60;
- snprintf(time_elapsed_str, sizeof(time_elapsed_str), "%u days %u hrs %u min %u sec", day,
- hour, min, second);
+ snprintf(time_elapsed_str, sizeof(time_elapsed_str),
+ "%u days %u hrs %u min %u sec", day, hour, min, second);
}
size_t curr_exec_cnt = ATOMIC_GET(hfuzz->mutationsCnt);
@@ -107,37 +135,37 @@
static size_t prev_exec_cnt = 0UL;
uintptr_t exec_per_sec = curr_exec_cnt - prev_exec_cnt;
prev_exec_cnt = curr_exec_cnt;
-
MX_SCOPED_LOCK(logMutexGet());
-
display_put("%s", ESC_CLEAR);
display_put("==================================== STAT ====================================\n");
-
display_put("Iterations: " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET, curr_exec_cnt);
display_printKMG(curr_exec_cnt);
if (hfuzz->mutationsMax) {
- display_put(" (out of: " ESC_BOLD "%zu" ESC_RESET " [" ESC_BOLD "%.2f%%" ESC_RESET "])",
- hfuzz->mutationsMax, exeProgress);
+ display_put(" (out of: " ESC_BOLD "%zu" ESC_RESET " [" ESC_BOLD "%.2f%%" ESC_RESET
+ "])", hfuzz->mutationsMax, exeProgress);
}
display_put("\n");
-
char start_time_str[128];
util_getLocalTime("%F %T", start_time_str, sizeof(start_time_str), hfuzz->timeStart);
- display_put("Run time: " ESC_BOLD "%s" ESC_RESET " (since: " ESC_BOLD "%s" ESC_RESET ")\n",
- time_elapsed_str, start_time_str);
-
+ display_put("Run time: " ESC_BOLD "%s" ESC_RESET " (since: " ESC_BOLD "%s" ESC_RESET
+ ")\n", time_elapsed_str, start_time_str);
display_put("Input file/dir: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->inputFile);
display_put("Fuzzed cmd: '" ESC_BOLD "%s" ESC_RESET "'\n", hfuzz->cmdline_txt);
if (hfuzz->linux.pid > 0) {
- display_put("Remote cmd [" ESC_BOLD "%d" ESC_RESET "]: '" ESC_BOLD "%s" ESC_RESET "'\n",
- hfuzz->linux.pid, hfuzz->linux.pidCmd);
+ display_put("Remote cmd [" ESC_BOLD "%d" ESC_RESET "]: '" ESC_BOLD "%s" ESC_RESET
+ "'\n", hfuzz->linux.pid, hfuzz->linux.pidCmd);
}
- display_put("Fuzzing threads: " ESC_BOLD "%zu" ESC_RESET "\n", hfuzz->threadsMax);
- display_put("%s per second: " ESC_BOLD "% " _HF_MONETARY_MOD "zu" ESC_RESET " (avg: " ESC_BOLD
- "%" _HF_MONETARY_MOD "zu" ESC_RESET ")\n", hfuzz->persistent ? "Rounds" : "Execs",
- exec_per_sec, elapsed_second ? (curr_exec_cnt / elapsed_second) : 0);
+ display_put("Fuzzing threads: " ESC_BOLD "%zu" ESC_RESET, hfuzz->threadsMax);
+ double cpuUse = getCpuUse();
+ if (cpuUse != 0.0l) {
+ display_put(" (CPU: " ESC_BOLD "%.1lf%%" ESC_RESET ")", cpuUse);
+ }
+ display_put("\n%s per second: " ESC_BOLD "% " _HF_MONETARY_MOD "zu" ESC_RESET
+ " (avg: " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET ")\n",
+ hfuzz->persistent ? "Rounds" : "Execs", exec_per_sec,
+ elapsed_second ? (curr_exec_cnt / elapsed_second) : 0);
/* If dry run, print also the input file count */
if (hfuzz->origFlipRate == 0.0L && hfuzz->useVerifier) {
display_put("Input Files: '" ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET "'\n",
@@ -146,15 +174,15 @@
uint64_t crashesCnt = ATOMIC_GET(hfuzz->crashesCnt);
/* colored the crash count as red when exist crash */
- display_put("Crashes: " ESC_BOLD "%s" "%zu" ESC_RESET " (unique: %s" ESC_BOLD "%zu" ESC_RESET
- ", blacklist: " ESC_BOLD "%zu" ESC_RESET ", verified: " ESC_BOLD "%zu" ESC_RESET
- ")\n", crashesCnt > 0 ? ESC_RED : "", hfuzz->crashesCnt,
- crashesCnt > 0 ? ESC_RED : "", ATOMIC_GET(hfuzz->uniqueCrashesCnt),
- ATOMIC_GET(hfuzz->blCrashesCnt), ATOMIC_GET(hfuzz->verifiedCrashesCnt));
-
- display_put("Timeouts (%" _HF_MONETARY_MOD "zu sec): " ESC_BOLD "%" _HF_MONETARY_MOD "zu"
- ESC_RESET "\n", hfuzz->tmOut, ATOMIC_GET(hfuzz->timeoutedCnt));
-
+ display_put("Crashes: " ESC_BOLD "%s" "%zu" ESC_RESET " (unique: %s" ESC_BOLD "%zu"
+ ESC_RESET ", blacklist: " ESC_BOLD "%zu" ESC_RESET ", verified: "
+ ESC_BOLD "%zu" ESC_RESET ")\n", crashesCnt > 0 ? ESC_RED : "",
+ hfuzz->crashesCnt, crashesCnt > 0 ? ESC_RED : "",
+ ATOMIC_GET(hfuzz->uniqueCrashesCnt), ATOMIC_GET(hfuzz->blCrashesCnt),
+ ATOMIC_GET(hfuzz->verifiedCrashesCnt));
+ display_put("Timeouts (%" _HF_MONETARY_MOD "zu sec): " ESC_BOLD "%"
+ _HF_MONETARY_MOD "zu" ESC_RESET "\n", hfuzz->tmOut,
+ ATOMIC_GET(hfuzz->timeoutedCnt));
/* Feedback data sources are enabled. Start with common headers. */
if (hfuzz->dynFileMethod != _HF_DYNFILE_NONE || hfuzz->useSanCov) {
display_put("File corpus size: " ESC_BOLD "%" _HF_MONETARY_MOD "zu" ESC_RESET "\n",
@@ -164,37 +192,37 @@
/* HW perf specific counters */
if (hfuzz->dynFileMethod & _HF_DYNFILE_INSTR_COUNT) {
- display_put(" - instructions: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->linux.hwCnts.cpuInstrCnt));
+ display_put(" - instructions: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->linux.hwCnts.cpuInstrCnt));
}
if (hfuzz->dynFileMethod & _HF_DYNFILE_BRANCH_COUNT) {
- display_put(" - branches: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->linux.hwCnts.cpuBranchCnt));
+ display_put(" - branches: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->linux.hwCnts.cpuBranchCnt));
}
if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_BLOCK) {
- display_put(" - BTS blocks: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
+ display_put(" - BTS blocks: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
}
if (hfuzz->dynFileMethod & _HF_DYNFILE_BTS_EDGE) {
- display_put(" - BTS edges: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
+ display_put(" - BTS edges: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
}
if (hfuzz->dynFileMethod & _HF_DYNFILE_IPT_BLOCK) {
- display_put(" - PT blocks: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
+ display_put(" - PT blocks: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->linux.hwCnts.bbCnt));
}
if (hfuzz->dynFileMethod & _HF_DYNFILE_CUSTOM) {
- display_put(" - custom counter: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->linux.hwCnts.customCnt));
+ display_put(" - custom counter: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->linux.hwCnts.customCnt));
}
if (hfuzz->dynFileMethod & _HF_DYNFILE_SOFT) {
uint64_t softCnt = ATOMIC_GET(hfuzz->linux.hwCnts.softCnt);
uint64_t softCntMax = ATOMIC_GET(hfuzz->linux.hwCnts.softCntMax);
double softCntFrac = (softCntMax != 0) ? ((double)softCnt / softCntMax) : 0.0l;
- display_put(" - functions seen: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET " (max: "
- ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET " - %.2lf%%)\n", softCnt,
- softCntMax, softCntFrac);
+ display_put(" - functions seen: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ " (max: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ " - %.2lf%%)\n", softCnt, softCntMax, softCntFrac);
}
/* Sanitizer coverage specific counters */
@@ -208,8 +236,8 @@
" (instrumented only)\n", ATOMIC_GET(hfuzz->sanCovCnts.iDsoCnt));
display_put(" - discovered #bb: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
" (new from input seed)\n", ATOMIC_GET(hfuzz->sanCovCnts.newBBCnt));
- display_put(" - crashes: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET "\n",
- ATOMIC_GET(hfuzz->sanCovCnts.crashesCnt));
+ display_put(" - crashes: " ESC_BOLD "%" _HF_MONETARY_MOD PRIu64 ESC_RESET
+ "\n", ATOMIC_GET(hfuzz->sanCovCnts.crashesCnt));
}
display_put("==================================== LOGS ====================================\n");
}