main
emmm 1 month ago
parent a6704ef8c1
commit fc527fee69

@ -51,142 +51,135 @@
#include "debug.h"
#ifdef __linux__
# define HAVE_AFFINITY 1
# define HAVE_AFFINITY 1 // 如果是Linux系统支持CPU亲和性
#endif /* __linux__ */
/* Get unix time in microseconds. */
/* 获取当前时间,单位:微秒 */
static u64 get_cur_time_us(void) {
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec * 1000000ULL) + tv.tv_usec;
gettimeofday(&tv, &tz); // 获取当前的系统时间
return (tv.tv_sec * 1000000ULL) + tv.tv_usec; // 将秒转换为微秒并返回
}
/* Get CPU usage in microseconds. */
/* 获取当前进程的CPU使用时间单位微秒 */
static u64 get_cpu_usage_us(void) {
struct rusage u;
getrusage(RUSAGE_SELF, &u);
return (u.ru_utime.tv_sec * 1000000ULL) + u.ru_utime.tv_usec +
(u.ru_stime.tv_sec * 1000000ULL) + u.ru_stime.tv_usec;
getrusage(RUSAGE_SELF, &u); // 获取当前进程的资源使用情况
return (u.ru_utime.tv_sec * 1000000ULL) + u.ru_utime.tv_usec + // 用户态CPU时间
(u.ru_stime.tv_sec * 1000000ULL) + u.ru_stime.tv_usec; // 内核态CPU时间
}
/* Measure preemption rate. */
/* 测量预占率target_ms为目标时间单位毫秒 */
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;
u64 st_t, en_t, st_c, en_c, real, slice;
s32 loop_repeats = 0;
st_t = get_cur_time_us();
st_c = get_cpu_usage_us();
st_t = get_cur_time_us(); // 获取开始时间
st_c = get_cpu_usage_us(); // 获取开始时的CPU使用时间
repeat_loop:
v1 = CTEST_BUSY_CYCLES;
v1 = CTEST_BUSY_CYCLES; // 模拟的忙循环次数
while (v1--) v2++;
sched_yield();
while (v1--) v2++; // 执行忙循环
sched_yield(); // 强制当前进程让出CPU
en_t = get_cur_time_us();
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();
en_c = get_cpu_usage_us(); // 获取结束时的CPU使用时间
real_delta = (en_t - st_t) / 1000;
slice_delta = (en_c - st_c) / 1000;
return real_delta * 100 / slice_delta;
// 计算实际经过时间和CPU时间的差值单位毫秒
real = (en_t - st_t) / 1000;
slice = (en_c - st_c) / 1000;
// 返回CPU预占率单位百分比
return real * 100 / slice;
}
/* Do the benchmark thing. */
/* 主程序 */
int main(int argc, char** argv) {
#ifdef HAVE_AFFINITY
#ifdef HAVE_AFFINITY // 如果支持CPU亲和性
u32 cpu_cnt = sysconf(_SC_NPROCESSORS_ONLN),
u32 cpu_cnt = sysconf(_SC_NPROCESSORS_ONLN), // 获取系统中CPU核心数
idle_cpus = 0, maybe_cpus = 0, i;
SAYF(cCYA "afl-gotcpu " cBRI VERSION cRST " by <lcamtuf@google.com>\n");
ACTF("Measuring per-core preemption rate (this will take %0.02f sec)...",
((double)CTEST_CORE_TRG_MS) / 1000);
((double)CTEST_CORE_TRG_MS) / 1000); // 输出提示信息,显示测量时间
for (i = 0; i < cpu_cnt; i++) {
for (i = 0; i < cpu_cnt; i++) { // 遍历每个CPU核心
s32 fr = fork();
s32 fr = fork(); // 创建子进程
if (fr < 0) PFATAL("fork failed");
if (!fr) {
if (!fr) { // 子进程执行以下代码
cpu_set_t c;
u32 util_perc;
CPU_ZERO(&c);
CPU_SET(i, &c);
CPU_ZERO(&c); // 清空CPU集合
CPU_SET(i, &c); // 将当前核心i加入CPU集合
if (sched_setaffinity(0, sizeof(c), &c))
if (sched_setaffinity(0, sizeof(c), &c)) // 设置当前进程的CPU亲和性
PFATAL("sched_setaffinity failed for cpu %d", i);
util_perc = measure_preemption(CTEST_CORE_TRG_MS);
util_perc = measure_preemption(CTEST_CORE_TRG_MS); // 测量当前核心的预占率
// 根据预占率判断当前核心的负载状态
if (util_perc < 110) {
SAYF(" Core #%u: " cLGN "AVAILABLE " cRST "(%u%%)\n", i, util_perc);
exit(0);
exit(0); // 退出子进程
} else if (util_perc < 250) {
SAYF(" Core #%u: " cYEL "CAUTION " cRST "(%u%%)\n", i, util_perc);
exit(1);
SAYF(" Core #%u: " cYEL "CAUTION " cRST "(%u%%)\n", i, util_perc);
exit(1); // 退出子进程
}
SAYF(" Core #%u: " cLRD "OVERBOOKED " cRST "(%u%%)\n" cRST, i,
util_perc);
exit(2);
SAYF(" Core #%u: " cLRD "OVERBOOKED " cRST "(%u%%)\n" cRST, i, util_perc);
exit(2); // 退出子进程
}
}
// 等待所有子进程完成
for (i = 0; i < cpu_cnt; i++) {
int ret;
if (waitpid(-1, &ret, 0) < 0) PFATAL("waitpid failed");
if (WEXITSTATUS(ret) == 0) idle_cpus++;
if (WEXITSTATUS(ret) <= 1) maybe_cpus++;
if (WEXITSTATUS(ret) == 0) idle_cpus++; // 统计空闲的CPU核心
if (WEXITSTATUS(ret) <= 1) maybe_cpus++; // 统计可能可用的CPU核心
}
SAYF(cGRA "\n>>> ");
if (idle_cpus) { // 如果有空闲的CPU核心
if (maybe_c
if (idle_cpus) {
if (maybe_cpus == idle_cpus) {

@ -490,13 +490,10 @@ static void detect_file_args(char** argv) {
}
<<<<<<< HEAD
/* Show banner. */
// 打印工具的名称和版本信息。
=======
>>>>>>> 31b9bf56f4cc249d8b6bb414203bff28d8cf724a
static void show_banner(void) {
SAYF(cCYA "afl-showmap " cBRI VERSION cRST " by <lcamtuf@google.com>\n");

Loading…
Cancel
Save