static u32 measure_preemption(u32 target_ms) { static volatile u32 v1, v2; u64 st_t, en_t, st_c, en_c, real_delta, slice_delta; s32 loop_repeats = 0; st_t = get_cur_time_us(); st_c = get_cpu_usage_us(); repeat_loop: v1 = CTEST_BUSY_CYCLES; while (v1--) v2++; sched_yield(); en_t = get_cur_time_us(); if (en_t - st_t < target_ms * 1000) { loop_repeats++; goto repeat_loop; } /* Let's see what percentage of this time we actually had a chance to run, and how much time was spent in the penalty box. */ en_c = get_cpu_usage_us(); real_delta = (en_t - st_t) / 1000; slice_delta = (en_c - st_c) / 1000; return real_delta * 100 / slice_delta; }
int main(int argc, char** argv) { static volatile u32 v1, v2; s32 loop_repeats = 0, util_perc; u64 st_t, en_t, st_c, en_c, real_delta, slice_delta; SAYF(cCYA "afl-gotcpu " cBRI VERSION cRST " (" __DATE__ " " __TIME__ ") by <*****@*****.**>\n"); /* Run a busy loop for CTEST_TARGET_MS. */ ACTF("Measuring preemption rate (this will take %0.02f sec)...", ((double)CTEST_TARGET_MS) / 1000); st_t = get_cur_time_us(); st_c = get_cpu_usage_us(); repeat_loop: v1 = CTEST_BUSY_CYCLES; while (v1--) v2++; sched_yield(); en_t = get_cur_time_us(); if (en_t - st_t < CTEST_TARGET_MS * 1000) { loop_repeats++; goto repeat_loop; } /* Let's see what percentage of this time we actually had a chance to run, and how much time was spent in the penalty box. */ en_c = get_cpu_usage_us(); real_delta = (en_t - st_t) / 1000; slice_delta = (en_c - st_c) / 1000; OKF("Busy loop hit %u times, real = %llu ms, slice = %llu ms.", loop_repeats, real_delta, slice_delta); util_perc = real_delta * 100 / slice_delta; /* Deliver the final verdict. */ SAYF(cGRA "\n>>> "); if (util_perc < 105) { SAYF(cLGN "PASS: "******"You can probably run additional processes."); } else if (util_perc < 130) { SAYF(cYEL "CAUTION: " cRST "Your CPU may be somewhat overbooked (%u%%).", util_perc); } else { SAYF(cLRD "FAIL: " cRST "Your CPU is overbooked (%u%%).", util_perc); } SAYF(cGRA " <<<" cRST "\n\n"); return (util_perc > 105) + (util_perc > 130); }