diff options
author | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-16 03:24:54 +0100 |
---|---|---|
committer | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-16 03:24:54 +0100 |
commit | fe3e3c7e3be29d29b3b7efc22f4933c64e883776 (patch) | |
tree | 39857350c5a8206a96f4f7ff8278d9ab880c02fc | |
parent | fe9833bae838296ec2e17161fe666faba769b101 (diff) | |
download | cpulimit-fe3e3c7e3be29d29b3b7efc22f4933c64e883776.tar.gz |
written two unit tests for process_iterator. tests pass
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | src/process_iterator.c | 40 | ||||
-rw-r--r-- | src/process_iterator.h | 2 | ||||
-rwxr-xr-x | tests/process_iterator_test | bin | 24672 -> 0 bytes | |||
-rw-r--r-- | tests/process_iterator_test.c | 80 |
5 files changed, 107 insertions, 17 deletions
@@ -2,4 +2,4 @@ *~ src/cpulimit busy -test/process_iterator_test +process_iterator_test diff --git a/src/process_iterator.c b/src/process_iterator.c index a9e66ed..eb34d62 100644 --- a/src/process_iterator.c +++ b/src/process_iterator.c @@ -2,13 +2,33 @@ #include <stdlib.h> #include <string.h> #include <sys/procfs.h> - +#include <time.h> #include "process_iterator.h" //See this link to port to other systems: http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC85 #ifdef __linux__ +static int get_boot_time() +{ + int uptime; + FILE *fp = fopen ("/proc/uptime", "r"); + if (fp != NULL) + { + char buf[BUFSIZ]; + char *b = fgets(buf, BUFSIZ, fp); + if (b == buf) + { + char *end_ptr; + double upsecs = strtod(buf, &end_ptr); + uptime = (int)upsecs; + } + fclose (fp); + } + time_t now = time(NULL); + return now - uptime; +} + int init_process_iterator(struct process_iterator *it, struct process_filter *filter) { //open a directory stream to /proc directory @@ -18,6 +38,7 @@ int init_process_iterator(struct process_iterator *it, struct process_filter *fi return -1; } it->filter = filter; + it->boot_time = get_boot_time(); return 0; } @@ -89,10 +110,17 @@ static int is_child_of(pid_t child_pid, pid_t parent_pid) int read_next_process(struct process_iterator *it, struct process *p) { + if (it->dip == NULL) + { + //end of processes + return -1; + } if (it->filter->pid > 0 && !it->filter->include_children) { read_process_info(it->filter->pid, p); - return -1; + p->starttime += it->boot_time; + it->dip = NULL; + return 0; } struct dirent *dit; //read in from /proc and seek for process dirs @@ -102,6 +130,7 @@ int read_next_process(struct process_iterator *it, struct process *p) p->pid = atoi(dit->d_name); if (it->filter->pid > 0 && it->filter->pid != p->pid && !is_child_of(p->pid, it->filter->pid)) continue; read_process_info(p->pid, p); + p->starttime += it->boot_time; break; } if (dit == NULL) @@ -148,11 +177,6 @@ int init_process_iterator(struct process_iterator *it, struct process_filter *fi return -1; } kvm_close(kd); - static int request[2] = { CTL_KERN, KERN_BOOTTIME }; - struct timeval result; - size_t result_len = sizeof result; - if (sysctl (request, 2, &result, &result_len, NULL, 0) < 0) return -1; - it->boot_time = result.tv_sec; it->filter = filter; return 0; } @@ -162,7 +186,7 @@ int read_next_process(struct process_iterator *it, struct process *p) { p->pid = it->procs[it->i].ki_pid; p->ppid = it->procs[it->i].ki_ppid; p->cputime = it->procs[it->i].ki_runtime / 1000; - p->starttime = it->procs[it->i].ki_start.tv_sec - it->boot_time; + p->starttime = it->procs[it->i].ki_start.tv_sec; it->i++; if (it->i == it->count) return -1; return 0; diff --git a/src/process_iterator.h b/src/process_iterator.h index 23af7ed..711cf3b 100644 --- a/src/process_iterator.h +++ b/src/process_iterator.h @@ -76,8 +76,8 @@ struct process_filter { struct process_iterator { #ifdef __linux__ DIR *dip; -#elif defined __FreeBSD__ int boot_time; +#elif defined __FreeBSD__ struct kinfo_proc *procs; int count; int i; diff --git a/tests/process_iterator_test b/tests/process_iterator_test Binary files differdeleted file mode 100755 index f0204fd..0000000 --- a/tests/process_iterator_test +++ /dev/null diff --git a/tests/process_iterator_test.c b/tests/process_iterator_test.c index b8aec52..1822449 100644 --- a/tests/process_iterator_test.c +++ b/tests/process_iterator_test.c @@ -1,22 +1,88 @@ #include <stdio.h> #include <process_iterator.h> +#include <sys/types.h> +#include <unistd.h> +#include <assert.h> +#include <time.h> -int main() +int test_single_process() +{ + struct process_iterator it; + struct process process; + struct process_filter filter; + int count; + time_t now; + //don't iterate children + filter.pid = getpid(); + filter.include_children = 0; + count = 0; + now = time(NULL); + init_process_iterator(&it, &filter); + while (read_next_process(&it, &process) == 0) + { + assert(process.pid == getpid()); + assert(process.ppid == getppid()); + assert(process.cputime < 100); + assert(process.starttime == now || process.starttime == now - 1); + + count++; + } + assert(count == 1); + close_process_iterator(&it); + //iterate children + filter.pid = getpid(); + filter.include_children = 1; + count = 0; + now = time(NULL); + init_process_iterator(&it, &filter); + while (read_next_process(&it, &process) == 0) + { + assert(process.pid == getpid()); + assert(process.ppid == getppid()); + assert(process.cputime < 100); + assert(process.starttime == now || process.starttime == now - 1); + count++; + } + assert(count == 1); + close_process_iterator(&it); + return 0; +} + +int test_multiple_process() { struct process_iterator it; struct process process; struct process_filter filter; - filter.pid = 2981; + int child = fork(); + if (child == 0) + { + //child code + sleep(1); + return 0; + } + filter.pid = getpid(); filter.include_children = 1; init_process_iterator(&it, &filter); + int count = 0; + time_t now = time(NULL); while (read_next_process(&it, &process) == 0) { - printf("Read process %d\n", process.pid); - printf("Parent %d\n", process.ppid); - printf("Starttime %d\n", process.starttime); - printf("CPU time %d\n", process.cputime); - printf("\n"); + if (process.pid == getpid()) assert(process.ppid == getppid()); + else if (process.pid == child) assert(process.ppid == getpid()); + else assert(0); + assert(process.cputime < 100); + assert(process.starttime == now || process.starttime == now - 1); + count++; } + assert(count == 2); close_process_iterator(&it); return 0; } + +int main() +{ + printf("Current PID %d\n", getpid()); + test_single_process(); + test_multiple_process(); + return 0; +}
\ No newline at end of file |