aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAngelo Marletta <angelo.marletta@gmail.com>2012-06-20 02:48:05 +0100
committerAngelo Marletta <angelo.marletta@gmail.com>2012-06-20 02:48:05 +0100
commitc08039365f62abe5f180c9c3c6c3f8fac6feb378 (patch)
tree8e5e20b979437b78ec0fc5b6e8e6baf79d1aa6a1
parent01a76753a060c27e26ac691c0d69f99b975038e5 (diff)
downloadcpulimit-c08039365f62abe5f180c9c3c6c3f8fac6feb378.tar.gz
added process_group and test. all tests pass
-rw-r--r--src/Makefile5
-rw-r--r--src/process_group.c76
-rw-r--r--src/process_group.h17
-rw-r--r--src/process_iterator.c2
-rw-r--r--src/process_iterator.h2
-rw-r--r--tests/Makefile13
-rw-r--r--tests/process_iterator_test.c26
7 files changed, 75 insertions, 66 deletions
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 <string.h>
#include <stdlib.h>
#include <limits.h>
+
+#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; i<size; i++) {
+ if (pgroup->proctable[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; i<size; i++) {
- if (monitor->proctable[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;
};
diff --git a/tests/Makefile b/tests/Makefile
index d187359..ffc80fa 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,8 +1,9 @@
CC?=gcc
CFLAGS?=-Wall -g
TARGETS=busy process_iterator_test
-LIBS?=-lpthread
-
+SRC=../src
+SYSLIBS?=-lpthread
+LIBS=$(SRC)/list.o $(SRC)/process_iterator.o $(SRC)/process_group.o
UNAME := $(shell uname)
ifeq ($(UNAME), FreeBSD)
@@ -11,11 +12,11 @@ endif
all:: $(TARGETS)
-busy: busy.c $(LIBS)
- $(CC) -o busy busy.c $(LIBS) $(CFLAGS)
+busy: busy.c $(SYSLIBS)
+ $(CC) -o busy busy.c $(SYSLIBS) $(CFLAGS)
-process_iterator_test: process_iterator_test.c $(LIBS) ../src/process_iterator.o
- $(CC) -I../src -o process_iterator_test process_iterator_test.c ../src/process_iterator.o $(LIBS) $(CFLAGS)
+process_iterator_test: process_iterator_test.c $(LIBS)
+ $(CC) -I$(SRC) -o process_iterator_test process_iterator_test.c $(LIBS) $(SYSLIBS) $(CFLAGS)
clean:
rm -f *~ *.o $(TARGETS)
diff --git a/tests/process_iterator_test.c b/tests/process_iterator_test.c
index ac5d542..dcd4ebe 100644
--- a/tests/process_iterator_test.c
+++ b/tests/process_iterator_test.c
@@ -1,11 +1,14 @@
#include <stdio.h>
-#include <process_iterator.h>
+#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
-int test_single_process()
+#include <process_iterator.h>
+#include <process_group.h>
+
+void test_single_process()
{
struct process_iterator it;
struct process process;
@@ -44,10 +47,9 @@ int test_single_process()
}
assert(count == 1);
close_process_iterator(&it);
- return 0;
}
-int test_multiple_process()
+void test_multiple_process()
{
struct process_iterator it;
struct process process;
@@ -57,7 +59,7 @@ int test_multiple_process()
{
//child code
sleep(1);
- return 0;
+ exit(0);
}
filter.pid = getpid();
filter.include_children = 1;
@@ -75,10 +77,9 @@ int test_multiple_process()
}
assert(count == 2);
close_process_iterator(&it);
- return 0;
}
-int test_all_processes()
+void test_all_processes()
{
struct process_iterator it;
struct process process;
@@ -99,7 +100,15 @@ int test_all_processes()
}
assert(count >= 10);
close_process_iterator(&it);
- return 0;
+}
+
+void test_process_list()
+{
+ struct process_group pgroup;
+ pid_t current_pid = getpid();
+ assert(init_process_group(&pgroup, current_pid, 0) == 0);
+ update_process_group(&pgroup);
+ assert(close_process_group(&pgroup) == 0);
}
int main()
@@ -108,5 +117,6 @@ int main()
test_single_process();
test_multiple_process();
test_all_processes();
+ test_process_list();
return 0;
}
Un proyecto texto-plano.xyz