Lumiera  0.pre.03
»edityourfreedom«
backend.c
Go to the documentation of this file.
1 /*
2  Backend - common lumiera backend facilities
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
29 #include "include/logging.h"
30 #include "lib/safeclib.h"
31 #include "lib/tmpbuf.h"
32 #include "lib/mpool.h"
33 
34 #include "backend/backend.h"
35 #include "common/config.h"
37 #include "backend/filedescriptor.h"
39 #include "backend/mmapcache.h"
40 #include "backend/threadpool.h"
42 
43 #include <unistd.h>
44 #include <sys/resource.h>
45 
46 
47 
48 
49 static enum lumiera_resource_try
50 lumiera_backend_mpool_purge (enum lumiera_resource_try itr, void* data, void* context);
51 
52 static void
54 
55 static void
57 
58 
60 
61 
62 int
64 {
65 
66  TRACE (backend_dbg);
67  lumiera_mutex_init (&lumiera_filecreate_mutex, "fileaccess", &NOBUG_FLAG (mutex_dbg), NOBUG_CONTEXT);
68 
70 
71  /* hook the resourcecollector into the mpool*/
76 
77  /* hook the resourcecollector into the safeclib allocation functions */
79 
80  PLANNED("The resourcecollector aborts by default when there is no final strategy for recovery, TODO: initiate sane shutdown");
81 
83  PLANNED ("hook threadpool into the resourcecollector (maybe in threadpool_init() instead here");
84 
86 
87  lumiera_backend_pagesize = sysconf(_SC_PAGESIZE);
88 
90 
91 
92  const char* filehandles = lumiera_tmpbuf_snprintf (SIZE_MAX,
93  "backend.file.max_handles = %d",
94  /* roughly 2/3 of all available filehandles are managed by the backend */
95  (sysconf (_SC_OPEN_MAX)-10)*2/3);
96 
97  lumiera_config_setdefault (filehandles);
98 
99  long long max_entries;
100  lumiera_config_number_get ("backend.file.max_handles", &max_entries);
101  lumiera_filehandlecache_new (max_entries);
102 
103 #if SIZE_MAX <= 4294967295UL
104  lumiera_config_setdefault ("backend.mmap.as_limit = 3221225469");
105 #else
106  lumiera_config_setdefault ("backend.mmap.as_limit = 211106232532992");
107 #endif
108 
109  struct rlimit as_rlimit;
110  getrlimit (RLIMIT_AS, &as_rlimit);
111 
112  long long as_limit = (long long)as_rlimit.rlim_cur;
113  if (as_rlimit.rlim_cur == RLIM_INFINITY)
114  {
115  lumiera_config_number_get ("backend.mmap.as_limit", &as_limit);
116  }
117  else
118  {
119  INFO (backend, "address space limited to %luMiB", as_rlimit.rlim_cur/1024/1024);
120  }
121 
122  lumiera_mmapcache_new (as_limit);
123 
124  return 0;
125 }
126 
127 
128 void
130 {
131  TRACE (backend_dbg);
132 
137 
139 
142  mpool_malloc_hook = malloc;
143  mpool_free_hook = free;
144 
146 
147  lumiera_mutex_destroy (&lumiera_filecreate_mutex, &NOBUG_FLAG (mutex_dbg), NOBUG_CONTEXT);
148 }
149 
150 
151 static enum lumiera_resource_try
152 lumiera_backend_mpool_purge (enum lumiera_resource_try itr, void* data, void* context)
153 {
154  (void) context;
155  (void) data;
156  (void) itr;
158  return LUMIERA_RESOURCE_NONE;
159 }
160 
161 static void
163 {
164  self->udata =
166 }
167 
168 static void
170 {
172 }
173 
174 
175 
176 /*
177 // Local Variables:
178 // mode: C
179 // c-file-style: "gnu"
180 // indent-tabs-mode: nil
181 // End:
182 */
void lumiera_threadpool_init(void)
Initialise the thread pool.
Definition: threadpool.c:47
Portable and safe wrappers around some C-Lib functions.
No op, returned by a handler when it did nothing.
static void lumiera_backend_resourcecollector_unregister_mpool(MPool self)
Definition: backend.c:169
void lumiera_resourcehandler_unregister(LumieraResourcehandler self)
Unregisters a collector handle Removes the handler from the registry and calls it once with LUMIERA_R...
void lumiera_safeclib_set_resourcecollector(void *hook)
Install the resourcecollector run hook.
Definition: safeclib.c:57
int data
Definition: test-priqueue.c:84
memory blocks, context is a pointer to the size_t required
void lumiera_filehandlecache_delete(void)
Delete the filehandle cache.
void lumiera_threadpool_destroy(void)
Definition: threadpool.c:66
File descriptors are the underlying working horse in accessing files.
void lumiera_mmapcache_new(size_t limit)
Initialises the mmapcache.
Definition: mmapcache.c:41
void lumiera_resourcecollector_destroy(void)
Destroy the resource collector registry.
void(* mpool_free_hook)(void *ptr)
Definition: mpool.c:49
void lumiera_filedescriptorregistry_init(void)
Initialise the global file descriptor registry.
Interface for a lumiera configuration system (draft).
int lumiera_resourcecollector_run(enum lumiera_resource which, enum lumiera_resource_try *iteration, void *context)
Try to free resources.
Registry for used file descriptors.
Lumiera&#39;s Mmapcache stores a MRU cache of all established mmaped memory regions which are currently n...
This header is for including and configuring NoBug.
Round robin temporary buffers.
static void lumiera_free(void *mem)
Free previously allocated memory.
Definition: safeclib.h:82
static void lumiera_backend_resourcecollector_register_mpool(MPool self)
Definition: backend.c:162
lumiera_resourcehandler * LumieraResourcehandler
size_t lumiera_backend_pagesize
Definition: backend.c:59
LumieraResourcehandler lumiera_resourcecollector_register_handler(enum lumiera_resource resource, lumiera_resource_handler_fn handler, void *data)
Registers a new collector handler.
return NULL
Definition: llist.h:596
void(* mpool_init_hook)(MPool self)
called after a mpool got initialised
Definition: mpool.c:52
LumieraConfigitem lumiera_config_setdefault(const char *line)
Installs a default value for a config key.
Definition: config.c:274
void lumiera_filedescriptorregistry_destroy(void)
Destroy and free the global file descriptor registry.
void lumiera_resourcecollector_init(void)
Initialise the Resourcecollector.
lumiera_mutex lumiera_filecreate_mutex
Protect lookup and creation of files.
void * lumiera_malloc(size_t size)
Allocate memory.
Definition: safeclib.c:67
Pooled memory allocator for constant sized objects Memory Pools are implemented as clusters of fixed ...
TRACE(test, "inserted %d", data)
const char * lumiera_config_number_get(const char *key, long long *value)
Number signed integer numbers, in different formats (decimal, hex, oct, binary(for masks)) ...
Definition: config-typed.c:69
void lumiera_filehandlecache_new(int max_entries)
Initialises the filehandle cache.
LumieraMutex lumiera_mutex_init(LumieraMutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
Initialise a mutex variable This initialises a &#39;fast&#39; default mutex which must not be locked recursiv...
Definition: mutex.c:30
static enum lumiera_resource_try lumiera_backend_mpool_purge(enum lumiera_resource_try itr, void *data, void *context)
Definition: backend.c:152
void lumiera_backend_destroy(void)
Definition: backend.c:129
mpool * MPool
Definition: mpool.h:57
void(* mpool_destroy_hook)(MPool self)
called before a mpool gets destroyed
Definition: mpool.c:54
int lumiera_backend_init(void)
Definition: backend.c:63
lumiera_resource_try
Iteration indicator Resource collection works iteratively freeing more and more resources.
Caching and management of filehandles.
Lumiera Backend: global initialisation and definitions.
A global manager for scarce system resources.
LumieraMutex lumiera_mutex_destroy(LumieraMutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a mutex variable.
Definition: mutex.c:48
void *(* mpool_malloc_hook)(size_t size)
Definition: mpool.c:48
void lumiera_mmapcache_delete(void)
Delete the mmap cache.
Definition: mmapcache.c:57
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition: tmpbuf.c:125