📄 dpmd_cpu.c
字号:
#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define KVER_26 0#define KVER_24 1struct cpu_interval { int min; int max; int nice_scale;};static unsigned int c_user, c_nice, c_sys, c_time;static unsigned int c_user_old, c_nice_old, c_sys_old, c_time_old;static unsigned int delta_time;static unsigned int kernel_version = KVER_24;int cpu_parse(const char *ev, void **obj){ struct cpu_interval *ret; ret = malloc(sizeof(struct cpu_interval)); if (ret == NULL) { printf("Unable to make room for a cpu interval (%s)\n", strerror(errno)); return -1; } ret->min = ret->max = 0; ret->nice_scale = 3;#if 0 printf("cpu interval: %s\n", ev);#endif sscanf(ev, "%d-%d,%d", &(ret->min), &(ret->max), &(ret->nice_scale));#if 0 printf("read MIN:%d MAX:%d SCALE:%d\n", ret->min, ret->max, ret->nice_scale);#endif if (ret->nice_scale == 0) { printf("nice_scale value out of range(%d), resetting to default value(3).\n", ret->nice_scale); ret->nice_scale = 3; } if (ret->min > ret->max) { printf("Min higher than Max?\n"); free(ret); return -1; } *obj = ret; return 0;}int cpu_evaluate(const void *s) { int cpu_percent = 0; unsigned int weighted_activity_old = 0; unsigned long int delta_activity = 0, weighted_activity = 0; const struct cpu_interval *c = (const struct cpu_interval *) s; weighted_activity = c_user + c_nice / c->nice_scale + c_sys; weighted_activity_old = c_user_old + c_nice_old / c->nice_scale + c_sys_old; delta_activity = weighted_activity - weighted_activity_old;#if 0 printf("CPU delta_activity=%ld delta_time=%d weighted_activity=%ld.\n", delta_activity, delta_time, weighted_activity);#endif if ( delta_activity > delta_time || delta_time <= 0) { cpu_percent = 100; } else { cpu_percent = delta_activity * 100 / delta_time; }#if 0 printf("CPU usage = %d.\n", cpu_percent); printf("called with min=%d max=%d\n", c->min, c->max);#endif return (cpu_percent >= c->min && cpu_percent <= c->max) ? 1 : 0;}int get_cpu(void) { FILE* fp; int f; unsigned long int c_idle=0, c_iowait=0, c_irq=0, c_softirq=0; /* for linux 2.6 only */ c_user_old = c_user; c_nice_old = c_nice; c_sys_old = c_sys; c_time_old = c_time; /* read raw jiffies... */ fp = fopen ("/proc/stat", "r"); if (!fp) { printf("/proc/stat: %s\n", strerror(errno)); return -1; } do { f = fscanf (fp, "cpu %u %u %u %lu %lu %lu %lu", &c_user, &c_nice, &c_sys, &c_idle, &c_iowait, &c_irq, &c_softirq); } while ((f!=4 && kernel_version==KVER_24) || (f!=7 && kernel_version==KVER_26)); fclose(fp);#if 0 printf("CPU c_user=%d c_nice=%d c_sys=%d c_idle=%ld " "c_iowait=%ld c_irq=%ld c_softirq=%ld.\n", c_user, c_nice, c_sys, c_idle, c_iowait, c_irq, c_softirq);#endif /* calculate total jiffies */ c_sys += c_irq + c_softirq; c_idle += c_iowait; c_time = c_user + c_nice + c_sys + c_idle; /* calculate delta time */ delta_time = c_time - c_time_old; /* weighted_activity = c_user + c_nice / 3 + c_sys; delta_activity = weighted_activity - old_weighted_activity; old_weighted_activity = weighted_activity; */ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -