diff options
author | Richard Braun <syn@sceen.net> | 2007-07-10 00:34:37 +0000 |
---|---|---|
committer | Richard Braun <syn@sceen.net> | 2007-07-10 00:34:37 +0000 |
commit | 9f3fea170a6d5a35c949a42cbe4b9e0137d1b3db (patch) | |
tree | c76513ef85999c3cc6a23e1bdde3d90a1920d4c7 | |
parent | 7d5ea3bfbb3d1bae12c4be2fed262b8e044e3588 (diff) |
Implemented a garbage collector for the slab allocator.
-rw-r--r-- | tntd/Makefile | 2 | ||||
-rw-r--r-- | tntd/gc.c | 101 | ||||
-rw-r--r-- | tntd/gc.h | 26 | ||||
-rw-r--r-- | tntd/main.c | 6 |
4 files changed, 133 insertions, 2 deletions
diff --git a/tntd/Makefile b/tntd/Makefile index f40a5e2..196c289 100644 --- a/tntd/Makefile +++ b/tntd/Makefile @@ -4,7 +4,7 @@ CFLAGS = -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes \ LDFLAGS = -L ../libetp -L ../libtnttk LIBS = -letp -ltnttk -lpthread -OBJECTS = main.o tnt.o tntd.o device.o port.o socket.o rpc.o +OBJECTS = main.o tnt.o tntd.o device.o port.o socket.o rpc.o gc.o .PHONY: clean diff --git a/tntd/gc.c b/tntd/gc.c new file mode 100644 index 0000000..7d5ee75 --- /dev/null +++ b/tntd/gc.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2007 Richard Braun <syn@sceen.net> + * Copyright (C) 2007 Stefano Bertinetti <ildoctor@ildoctor.net> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <time.h> +#include <errno.h> +#include <stdlib.h> +#include <pthread.h> +#include <sys/time.h> + +#include <tnttk/cache.h> + +#include "gc.h" +#include "output.h" + +#define GC_INTERVAL 10 + +static pthread_t gc_thread; + +static pthread_mutex_t gc_mutex; + +static pthread_cond_t gc_cond; + +static int gc_stop; + +static void * +gc_run(void *arg) +{ + struct timespec timeout; + struct timeval now; + int error; + + output("garbage collector thread running..."); + + pthread_mutex_lock(&gc_mutex); + + do + { + gettimeofday(&now, NULL); + timeout.tv_sec = now.tv_sec + GC_INTERVAL; + timeout.tv_nsec = now.tv_usec * 1000; + error = 0; + + while (!gc_stop && (error != ETIMEDOUT)) + error = pthread_cond_timedwait(&gc_cond, &gc_mutex, &timeout); + + if (gc_stop) + break; + + slab_reclaim(); + } + while (1); + + pthread_mutex_unlock(&gc_mutex); + + output("garbage collector thread stopping..."); + return NULL; +} + +void +gc_terminate(void) +{ + pthread_mutex_lock(&gc_mutex); + gc_stop = 1; + pthread_cond_signal(&gc_cond); + pthread_mutex_unlock(&gc_mutex); + pthread_join(gc_thread, NULL); +} + +void +gc_init(void) +{ + int error; + + pthread_mutex_init(&gc_mutex, NULL); + pthread_cond_init(&gc_cond, NULL); + gc_stop = 0; + error = pthread_create(&gc_thread, NULL, gc_run, NULL); + + if (error) + { + output_error("unable to start garbage collector thread, error = %d", + error); + exit(1); + } +} diff --git a/tntd/gc.h b/tntd/gc.h new file mode 100644 index 0000000..31e5f31 --- /dev/null +++ b/tntd/gc.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2007 Richard Braun <syn@sceen.net> + * Copyright (C) 2007 Stefano Bertinetti <ildoctor@ildoctor.net> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _GC_H +#define _GC_H + +void gc_terminate(void); +void gc_init(void); + +#endif /* _GC_H */ diff --git a/tntd/main.c b/tntd/main.c index 9b9e42d..3f7b49d 100644 --- a/tntd/main.c +++ b/tntd/main.c @@ -30,6 +30,7 @@ #include <tnttk/cache.h> +#include "gc.h" #include "ip.h" #include "rpc.h" #include "port.h" @@ -74,7 +75,7 @@ main(int argc, char *argv[]) if (raw_sd == -1) { perror("unable to create socket"); - exit(1); + return EXIT_FAILURE; } error = getifaddrs(&ifaddrs); @@ -91,6 +92,7 @@ main(int argc, char *argv[]) port_init(); socket_init(); rpc_init(); + gc_init(); ip_packet = malloc(buffer_size); @@ -144,10 +146,12 @@ main(int argc, char *argv[]) output("daemon shutting down..."); free(ip_packet); + gc_terminate(); rpc_terminate(); socket_terminate(); port_terminate(); device_terminate(); + slab_terminate(); output("threads and caches destroyed, daemon exiting..."); return EXIT_SUCCESS; } |