diff options
author | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-21 23:34:09 +0100 |
---|---|---|
committer | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-21 23:34:09 +0100 |
commit | eccc7f8368982422fb15287be27bdd83567d0e8c (patch) | |
tree | 8e142dc5c050e0a2463ff55e4d665d3ce9111680 | |
parent | 622bdab1e23bf95d5ed725b5db8ef2cf86d42cb2 (diff) | |
download | cpulimit-eccc7f8368982422fb15287be27bdd83567d0e8c.tar.gz |
fixed arithmetic bug
-rw-r--r-- | src/process_group.c | 48 | ||||
-rw-r--r-- | tests/process_iterator_test.c | 15 |
2 files changed, 15 insertions, 48 deletions
diff --git a/src/process_group.c b/src/process_group.c index 043ee69..318babb 100644 --- a/src/process_group.c +++ b/src/process_group.c @@ -54,41 +54,6 @@ static inline unsigned long timediff(const struct timeval *t1,const struct timev //parameter in range 0-1 #define ALFA 0.08 -int process_monitor(struct process *proc) -{ - // int j = get_jiffies(proc); - // if (j<0) return -1; //error retrieving jiffies count (maybe the process is dead) - // struct timeval now; - // gettimeofday(&now, NULL); - // if (proc->last_jiffies==-1) { - // //store current time - // proc->last_sample = now; - // //store current jiffies - // proc->last_jiffies = j; - // //it's the first sample, cannot figure out the cpu usage - // proc->cpu_usage = -1; - // return 0; - // } - // //time from previous sample (in ns) - // long dt = timediff(&now, &(proc->last_sample)); - // //how many jiffies in dt? - // double max_jiffies = dt * HZ / 1000000.0; - // double sample = (j - proc->last_jiffies) / max_jiffies; - // if (proc->cpu_usage == -1) { - // //initialization - // proc->cpu_usage = sample; - // } - // else { - // //usage adjustment - // proc->cpu_usage = (1-ALFA) * proc->cpu_usage + ALFA * sample; - // } - // //store current time - // proc->last_sample = now; - // //store current jiffies - // proc->last_jiffies = j; - return 0; -} - void update_process_group(struct process_group *pgroup) { struct process_iterator it; @@ -105,10 +70,9 @@ void update_process_group(struct process_group *pgroup) long dt = timediff(&now, &pgroup->last_update) / 1000; while (get_next_process(&it, &tmp_process) != -1) { - // printf("Read process %d\n", tmp_process.pid); - // printf("Parent %d\n", tmp_process.ppid); - // printf("Starttime %d\n", tmp_process.starttime); - // printf("CPU time %d\n", tmp_process.cputime); +// struct timeval t; +// gettimeofday(&t, NULL); +// printf("T=%ld.%ld PID=%d PPID=%d START=%d CPUTIME=%d\n", t.tv_sec, t.tv_usec, tmp_process.pid, tmp_process.ppid, tmp_process.starttime, tmp_process.cputime); int hashkey = pid_hashfn(tmp_process.pid + tmp_process.starttime); if (pgroup->proctable[hashkey] == NULL) { @@ -140,16 +104,16 @@ void update_process_group(struct process_group *pgroup) assert(tmp_process.ppid == p->ppid); assert(tmp_process.starttime == p->starttime); //process exists. update CPU usage - double sample = (tmp_process.cputime - p->cputime) / dt; + double sample = 1.0 * (tmp_process.cputime - p->cputime) / dt; if (p->cpu_usage == -1) { //initialization p->cpu_usage = sample; } else { //usage adjustment - p->cpu_usage = (1-ALFA) * p->cpu_usage + ALFA * sample; + p->cpu_usage = (1.0-ALFA) * p->cpu_usage + ALFA * sample; } - p->cpu_usage = (1-ALFA) * p->cpu_usage + ALFA * sample; + p->cpu_usage = (1.0-ALFA) * p->cpu_usage + ALFA * sample; p->cputime = tmp_process.cputime; add_elem(pgroup->proclist, p); } diff --git a/tests/process_iterator_test.c b/tests/process_iterator_test.c index e4d84fd..487d7ea 100644 --- a/tests/process_iterator_test.c +++ b/tests/process_iterator_test.c @@ -125,7 +125,7 @@ void test_process_group_all() assert(close_process_group(&pgroup) == 0); } -void test_process_group_single() +void test_process_group_single(int include_children) { struct process_group pgroup; child = fork(); @@ -137,9 +137,10 @@ void test_process_group_single() } signal(SIGABRT, &kill_child); signal(SIGTERM, &kill_child); - assert(init_process_group(&pgroup, child, 1) == 0); + assert(init_process_group(&pgroup, child, include_children) == 0); int i; - for (i=0; i<10000; i++) + double tot_usage = 0; + for (i=0; i<100; i++) { update_process_group(&pgroup); struct list_node *node = NULL; @@ -149,15 +150,16 @@ void test_process_group_single() assert(p->pid == child); assert(p->ppid == getpid()); assert(p->cpu_usage <= 1.2); -printf("%f\n", p->cpu_usage); + tot_usage += p->cpu_usage; count++; } assert(count == 1); struct timespec interval; interval.tv_sec = 0; - interval.tv_nsec = 100000000; + interval.tv_nsec = 50000000; nanosleep(&interval, NULL); } + assert(tot_usage / i < 1.1 && tot_usage / i > 0.9); assert(close_process_group(&pgroup) == 0); kill(child, SIGINT); } @@ -169,6 +171,7 @@ int main() test_multiple_process(); test_all_processes(); test_process_group_all(); - test_process_group_single(); + test_process_group_single(0); + test_process_group_single(1); return 0; } |