From c08039365f62abe5f180c9c3c6c3f8fac6feb378 Mon Sep 17 00:00:00 2001 From: Angelo Marletta Date: Wed, 20 Jun 2012 02:48:05 +0100 Subject: added process_group and test. all tests pass --- src/Makefile | 5 ++-- src/process_group.c | 76 ++++++++++++++++++++++++-------------------------- src/process_group.h | 17 +++++------ src/process_iterator.c | 2 +- src/process_iterator.h | 2 +- 5 files changed, 50 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 7e2bc94..e6e819f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,8 @@ CC?=gcc CFLAGS?=-Wall -g -D_GNU_SOURCE TARGETS=cpulimit -LIBS=procutils.o list.o process_iterator.o +#LIBS=procutils.o list.o process_iterator.o +LIBS=list.o process_iterator.o process_group.o UNAME := $(shell uname) @@ -9,7 +10,7 @@ ifeq ($(UNAME), FreeBSD) LIBS+=-lkvm endif -all:: process_iterator.o +all:: $(LIBS) #cpulimit: cpulimit.c $(LIBS) # $(CC) -o cpulimit cpulimit.c $(LIBS) $(CFLAGS) diff --git a/src/process_group.c b/src/process_group.c index b8b1155..2afef71 100644 --- a/src/process_group.c +++ b/src/process_group.c @@ -2,71 +2,67 @@ #include #include #include + +#include "process_iterator.h" #include "process_group.h" #include "list.h" -int init_process_monitor(struct process_monitor *monitor, int target_pid, int ignore_children) +int init_process_group(struct process_group *pgroup, int target_pid, int include_children) { //hashtable initialization - memset(&monitor->proctable, 0, sizeof(monitor->proctable)); - monitor->target_pid = target_pid; - monitor->ignore_children = ignore_children; + memset(&pgroup->proctable, 0, sizeof(pgroup->proctable)); + pgroup->target_pid = target_pid; + pgroup->include_children = include_children; + pgroup->proclist = (struct list*)malloc(sizeof(struct list)); + return 0; +} + +int close_process_group(struct process_group *pgroup) +{ + int i; + int size = sizeof(pgroup->proctable) / sizeof(struct process*); + for (i=0; iproctable[i] != NULL) { + //free() history for each process + destroy_list(pgroup->proctable[i]); + free(pgroup->proctable[i]); + pgroup->proctable[i] = NULL; + } + } + free(pgroup->proclist); + pgroup->proclist = NULL; return 0; } -struct list* get_process_list(struct process_monitor *monitor) +void update_process_group(struct process_group *pgroup) { struct process_iterator it; struct process process; - init_process_iterator(&it); - while (read_next_process(&it, &process) != -1) + struct process_filter filter; + filter.pid = pgroup->target_pid; + filter.include_children = pgroup->include_children; + init_process_iterator(&it, &filter); + while (get_next_process(&it, &process) != -1) { - if (monitor->ignore_children && process.pid != monitor->target_pid) continue; 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); - int hashkey = pid_hashfn(process.pid + process.starttime); - if (monitor->proctable[hashkey] == NULL) + if (pgroup->proctable[hashkey] == NULL) { - - if (is_ancestor(pid, ppid)) - struct process *ancestor = NULL; - while((ancestor=seek_process(f, ppid))==NULL) { - ppid = getppid_of(ppid); - } - - //empty bucket - monitor->proctable[hashkey] = malloc(sizeof(struct list)); + pgroup->proctable[hashkey] = malloc(sizeof(struct list)); struct process *new_process = malloc(sizeof(struct process)); memcpy(new_process, &process, sizeof(struct process)); - init_list(monitor->proctable[hashkey], 4); - add_elem(monitor->proctable[hashkey], new_process); + init_list(pgroup->proctable[hashkey], 4); + add_elem(pgroup->proctable[hashkey], new_process); } else { - + //existing bucket + } } close_process_iterator(&it); - - return NULL; } - -int close_process_monitor(struct process_monitor *monitor) -{ - int i; - int size = sizeof(monitor->proctable) / sizeof(struct process*); - for (i=0; iproctable[i] != NULL) { - //free() history for each process - destroy_list(monitor->proctable[i]); - free(monitor->proctable[i]); - monitor->proctable[i] = NULL; - } - } - monitor->target_pid = 0; - return 0; -} \ No newline at end of file diff --git a/src/process_group.h b/src/process_group.h index eb4c5d8..27c3352 100644 --- a/src/process_group.h +++ b/src/process_group.h @@ -19,9 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef __PROCESS_MONITOR_H +#ifndef __PROCESS_GROUP_H -#define __PROCESS_MONITOR_H +#define __PROCESS_GROUP_H #include "process_iterator.h" @@ -30,18 +30,19 @@ #define PIDHASH_SZ 1024 #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1)) -struct process_monitor +struct process_group { //hashtable with all the processes (array of struct list of struct process) struct list *proctable[PIDHASH_SZ]; + struct list *proclist; pid_t target_pid; - int ignore_children; + int include_children; }; -int init_process_monitor(struct process_monitor *monitor, int target_pid, int ignore_children); +int init_process_group(struct process_group *pgroup, int target_pid, int include_children); -struct list* get_process_list(struct process_monitor *monitor); +void update_process_group(struct process_group *pgroup); -int close_process_monitor(struct process_monitor *monitor); +int close_process_group(struct process_group *pgroup); -#endif \ No newline at end of file +#endif diff --git a/src/process_iterator.c b/src/process_iterator.c index ff13035..e27ed4c 100644 --- a/src/process_iterator.c +++ b/src/process_iterator.c @@ -286,7 +286,7 @@ int close_process_iterator(struct process_iterator *it) { // } // } - // i->procList = result; + // i->proclist = result; // i->count = err == 0 ? length / sizeof *result : 0; // i->c = 0; diff --git a/src/process_iterator.h b/src/process_iterator.h index 1e118f4..1747644 100644 --- a/src/process_iterator.h +++ b/src/process_iterator.h @@ -82,7 +82,7 @@ struct process_iterator { int count; int i; #elif defined __APPLE__ - struct kinfo_proc *procList; + struct kinfo_proc *proclist; #endif struct process_filter *filter; }; -- cgit v1.2.3