diff options
author | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-07 02:24:56 +0100 |
---|---|---|
committer | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-07 02:24:56 +0100 |
commit | d72e395ac6107e89b92e1c931b4b84ca42f60c2b (patch) | |
tree | 03ccefb6560690517fc49077a67b36a225b3c783 | |
parent | 3c10d8dc5820fde90bd293d95d82f5e4302d1c7d (diff) | |
download | cpulimit-d72e395ac6107e89b92e1c931b4b84ca42f60c2b.tar.gz |
started process iterator for FreeBSD. still unstable.
-rwxr-xr-x[-rw-r--r--] | src/Makefile | 2 | ||||
-rw-r--r-- | src/process.c | 2 | ||||
-rw-r--r-- | src/procutils.c | 21 | ||||
-rw-r--r-- | src/procutils.h | 2 | ||||
-rwxr-xr-x | tests/busy | bin | 5771 -> 5220 bytes | |||
-rwxr-xr-x[-rw-r--r--] | tests/busy.c | 5 |
6 files changed, 26 insertions, 6 deletions
diff --git a/src/Makefile b/src/Makefile index 6deb69b..6e7a89e 100644..100755 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CC?=gcc CFLAGS?=-Wall -O2 -D_GNU_SOURCE TARGETS=cpulimit -LIBS=process.o procutils.o list.o +LIBS=process.o procutils.o list.o -lkvm all:: $(TARGETS) diff --git a/src/process.c b/src/process.c index 4f7403b..73c5883 100644 --- a/src/process.c +++ b/src/process.c @@ -170,7 +170,7 @@ static int get_jiffies(struct process *proc) { return -1; } - process_data = kvm_getprocs(my_kernel, KERN_PROC_PID, pid, &processes); + process_data = kvm_getprocs(my_kernel, KERN_PROC_PID, proc->pid, &processes); if ( (process_data) && (processes >= 1) ) my_jiffies = process_data->ki_runtime; diff --git a/src/procutils.c b/src/procutils.c index fb7aaae..7cf0e71 100644 --- a/src/procutils.c +++ b/src/procutils.c @@ -29,6 +29,7 @@ #include <sys/utsname.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/sysctl.h> #include "procutils.h" @@ -37,6 +38,10 @@ #include <errno.h> #endif +#ifdef __FreeBSD__ +#include <kvm.h> +#endif + /* PROCESS STATISTICS FUNCTIONS */ //deprecated @@ -190,6 +195,22 @@ int init_process_iterator(struct process_iterator *i) { i->procList = result; i->count = err == 0 ? length / sizeof *result : 0; i->c = 0; +#elif defined __FreeBSD__ + kvm_t *kd; + struct kinfo_proc *procs = NULL; + char errbuf[_POSIX2_LINE_MAX]; + int count; + /* Open the kvm interface, get a descriptor */ + if ((kd = kvm_open(NULL, NULL, NULL, 0, errbuf)) == NULL) { + /* fprintf(stderr, "kvm_open: %s\n", errbuf); */ + fprintf(stderr, "kvm_open: ", errbuf); + } + /* Get the list of processes. */ + if ((procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count)) == NULL) { + kvm_close(kd); + /* fprintf(stderr, "kvm_getprocs: %s\n", kvm_geterr(kd)); */ + fprintf(stderr, "kvm_getprocs: ", kvm_geterr(kd)); + } #endif i->current = (struct process*)calloc(1, sizeof(struct process)); diff --git a/src/procutils.h b/src/procutils.h index b5a91a3..c39a239 100644 --- a/src/procutils.h +++ b/src/procutils.h @@ -62,7 +62,7 @@ struct process_iterator { struct kinfo_proc *procList; int count; int c; -#elif defined __hpux +#elif defined __FreeBSD__ int count; #endif struct process *current; Binary files differdiff --git a/tests/busy.c b/tests/busy.c index 4839033..b3afb7c 100644..100755 --- a/tests/busy.c +++ b/tests/busy.c @@ -13,7 +13,7 @@ int main(int argc, char **argv) { int i = 0; int num_threads = 1; if (argc == 2) num_threads = atoi(argv[1]); - for (i=0; i<num_threads; i++) + for (i=0; i<num_threads-1; i++) { pthread_t thread; int ret; @@ -23,8 +23,7 @@ int main(int argc, char **argv) { exit(1); } } - printf("Press ENTER to exit..."); - getchar(); + loop(); return 0; } |