int main (int dont_care_argc, char *argv[])
{
......
for (;;) {
......
frame_make(); //刷新cpu使用率等信息;
......
select(0, NULL, NULL, NULL, &tv); //定时,默认超时时间为3s;
......
}
}
static void frame_make (void)
{
......
summary_show(); //显示概要信息;
......
}
static proc_t **summary_show (void)
{
......
smpcpu = cpus_refresh(smpcpu); //获取cpu信息;
......
summaryhlp(&smpcpu[Cpu_tot], "Cpu(s):"); //输出cpu信息;
......
}
static CPU_t *cpus_refresh (CPU_t *cpus)
{
......
/* by opening this file once, we'll avoid the hit on minor page faults
(sorry Linux, but you'll have to close it for us) */
if (!fp) {
fp = fopen("/proc/stat", "r");
...
cpus = alloc_c((1 + Cpu_tot) * sizeof(CPU_t));
}
rewind(fp);
fflush(fp);
// first value the last slot with the cpu summary line
if (!fgets(buf, sizeof(buf), fp)) std_err("failed /proc/stat read");
cpus[Cpu_tot].x = 0; // FIXME: can't tell by kernel version number
cpus[Cpu_tot].y = 0; // FIXME: can't tell by kernel version number
cpus[Cpu_tot].z = 0; // FIXME: can't tell by kernel version number
num = sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&cpus[Cpu_tot].u,
&cpus[Cpu_tot].n,
&cpus[Cpu_tot].s,
&cpus[Cpu_tot].i,
&cpus[Cpu_tot].w,
&cpus[Cpu_tot].x,
&cpus[Cpu_tot].y,
&cpus[Cpu_tot].z
);
......
return cpus;
}
static void summaryhlp (CPU_t *cpu, const char *pfx)
{
......
u_frme = cpu->u - cpu->u_sav;
s_frme = cpu->s - cpu->s_sav;
n_frme = cpu->n - cpu->n_sav;
i_frme = TRIMz(cpu->i - cpu->i_sav);
w_frme = cpu->w - cpu->w_sav;
x_frme = cpu->x - cpu->x_sav;
y_frme = cpu->y - cpu->y_sav;
z_frme = cpu->z - cpu->z_sav;
tot_frme = u_frme + s_frme + n_frme + i_frme + w_frme + x_frme + y_frme + z_frme;
if (tot_frme < 1) tot_frme = 1;
scale = 100.0 / (float)tot_frme;
// display some kinda' cpu state percentages
// (who or what is explained by the passed prefix)
show_special(
0,
fmtmk(
States_fmts,
pfx,
(float)u_frme * scale,
(float)s_frme * scale,
(float)n_frme * scale,
(float)i_frme * scale,
(float)w_frme * scale,
(float)x_frme * scale,
(float)y_frme * scale,
(float)z_frme * scale
)
);
......
// remember for next time around
cpu->u_sav = cpu->u;
cpu->s_sav = cpu->s;
cpu->n_sav = cpu->n;
cpu->i_sav = cpu->i;
cpu->w_sav = cpu->w;
cpu->x_sav = cpu->x;
cpu->y_sav = cpu->y;
cpu->z_sav = cpu->z;
}