2 This file is part of the NoBug debugging library.
5 2007, 2008, 2009, 2010, Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, contact Christian Thaeter <ct@pipapo.org>.
27 #define NOBUG_LIBNOBUG_C
30 #define NOBUG_RINGBUFFER_MAX_MSG 4094
35 pthread_mutex_t nobug_ringbuffer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
37 LLIST_AUTO(nobug_ringbuffer_registry
);
40 //ringbuffer Ringbuffer
41 //ringbuffer ----------
43 //ringbuffer Implementation Details
44 //ringbuffer ~~~~~~~~~~~~~~~~~~~~~~
46 //ringbuffer This Ringbuffer implementation uses mmaped guard pages to implement fast wraparond
47 //ringbuffer by utilizing the MMU hardware. The guard pages mirror the beginning and end of the
48 //ringbuffer ringbuffers memory after respecive before it. By that one can write over the end
49 //ringbuffer without checking for wraparounds in one go, up to the size of the guard areas.
51 //ringbuffer guard before | ringbuffer memory | guard after
52 //ringbuffer aaaa|bbbbb aaaaa|bbbbb
54 //ringbuffer In the simpliest/smallest case this means that one page of memory is just mirrored
55 //ringbuffer 3 times after each other.
58 //ringbuffer In Memory Format
59 //ringbuffer ~~~~~~~~~~~~~~~~
61 //ringbuffer This implementation is meant for string data only, each stored string is delimited
62 //ringbuffer by a '\0' from the next one. There is only a write position, not a read position like
63 //ringbuffer in a queue. The write position is indicated by '\0xff' as sentinel value (for
64 //ringbuffer implementing persistent writebuffers). Even with only a write position, it is possible
65 //ringbuffer to iterate over existing entries, pop the last written entry, append to it and so on.
66 //ringbuffer But all this operations must not interleave with writes which may run over and invalidate
67 //ringbuffer any data queried from it.
71 struct nobug_ringbuffer
*
72 nobug_ringbuffer_init (struct nobug_ringbuffer
* self
, size_t size
, const char * name
, int flags
)
74 size_t pagesz
= sysconf (_SC_PAGESIZE
);
76 size
= (size
+ pagesz
-1) & ~(pagesz
-1);
77 self
->maxmsg
= (NOBUG_RINGBUFFER_MAX_MSG
+2 + pagesz
-1) & ~(pagesz
-1);
79 self
->name
[255] = '\0';
82 strcpy(self
->name
, "/tmp/nobug_ringbufferXXXXXX");
86 strncpy(self
->name
, name
, 255);
89 int fd
= mkstemp(self
->name
);
91 int oflags
= O_RDWR
|O_CREAT
;
92 if (!(flags
& NOBUG_RINGBUFFER_APPEND
))
95 if (fd
== -1 && errno
== EINVAL
)
96 fd
= open(self
->name
, oflags
, 0600);
100 /* still error, exit here */
101 fprintf(stderr
, "nobug_ringbuffer: Failed to open nobug_ringbuffer %s: %s\n", self
->name
, strerror(errno
));
105 if (!name
|| flags
& NOBUG_RINGBUFFER_TEMP
)
108 if (!name
|| !(flags
& NOBUG_RINGBUFFER_KEEP
))
110 /* just in case we need the name later, store the first character at the end */
111 self
->name
[255] = self
->name
[0];
115 if (ftruncate(fd
, size
))
117 fprintf(stderr
, "nobug_ringbuffer: Failed to truncate ringbuffer to %zd: %s\n",
118 size
, strerror(errno
));
123 /* get contigous address range */
124 char * start
= mmap (0, size
+2*self
->maxmsg
, PROT_NONE
, MAP_SHARED
|MAP_ANONYMOUS
|MAP_NORESERVE
, -1, 0);
127 fprintf(stderr
, "nobug_ringbuffer: Failed to reserve %zd bytes of address space: %s\n",
128 size
+2*self
->maxmsg
, strerror(errno
));
132 /* map the backing file */
133 self
->start
= mmap (start
+self
->maxmsg
, size
, PROT_READ
|PROT_WRITE
, MAP_FIXED
|MAP_SHARED
, fd
, 0);
136 fprintf(stderr
, "nobug_ringbuffer: Failed to mmap backing file, %s\n", strerror(errno
));
139 /* map beginning after the end */
140 if (!mmap (start
+self
->maxmsg
+size
, self
->maxmsg
, PROT_READ
|PROT_WRITE
, MAP_FIXED
|MAP_SHARED
, fd
, 0))
142 fprintf(stderr
, "nobug_ringbuffer: Failed to mmap trailing guard page, %s\n", strerror(errno
));
145 /* map end before beginning */
146 if (!mmap (start
, self
->maxmsg
, PROT_READ
|PROT_WRITE
, MAP_FIXED
|MAP_SHARED
, fd
, size
-self
->maxmsg
))
148 fprintf(stderr
, "nobug_ringbuffer: Failed to mmap leading guard page, %s\n", strerror(errno
));
154 if (flags
& NOBUG_RINGBUFFER_APPEND
)
156 start
= memchr (self
->start
, ~0, size
);
158 /* new file, can't append */
161 if (self
->pos
< self
->start
)
167 /* set pos to the end of the file, then new writes will start at the beginning */
168 self
->pos
= self
->start
+size
-1;
169 /* ~0 is used as marker for the turnaround point */
173 #if NOBUG_USE_PTHREAD
174 pthread_mutex_lock (&nobug_ringbuffer_mutex
);
175 llist_insert_tail (&nobug_ringbuffer_registry
, llist_init (&self
->node
));
176 pthread_mutex_unlock (&nobug_ringbuffer_mutex
);
178 llist_insert_tail (&nobug_ringbuffer_registry
, llist_init (&self
->node
));
185 struct nobug_ringbuffer
*
186 nobug_ringbuffer_new (size_t size
, const char * name
, int flags
)
188 struct nobug_ringbuffer
* self
= malloc (sizeof (struct nobug_ringbuffer
));
191 return nobug_ringbuffer_init (self
, size
, name
, flags
);
195 struct nobug_ringbuffer
*
196 nobug_ringbuffer_destroy (struct nobug_ringbuffer
* self
)
198 #if NOBUG_USE_PTHREAD
199 pthread_mutex_lock (&nobug_ringbuffer_mutex
);
200 llist_unlink (&self
->node
);
201 pthread_mutex_unlock (&nobug_ringbuffer_mutex
);
203 llist_unlink (&self
->node
);
208 munmap(self
->start
-self
->maxmsg
, self
->size
+ 2 * self
->maxmsg
);
214 nobug_ringbuffer_delete (struct nobug_ringbuffer
* self
)
216 free (nobug_ringbuffer_destroy (self
));
221 nobug_ringbuffer_sync (struct nobug_ringbuffer
* self
)
223 msync (self
->start
, self
->size
, MS_SYNC
);
228 nobug_ringbuffer_allsync (void)
230 #if NOBUG_USE_PTHREAD
231 pthread_mutex_lock (&nobug_ringbuffer_mutex
);
232 LLIST_FOREACH(&nobug_ringbuffer_registry
, n
)
233 nobug_ringbuffer_sync ((struct nobug_ringbuffer
*) n
);
234 pthread_mutex_unlock (&nobug_ringbuffer_mutex
);
236 LLIST_FOREACH(&nobug_ringbuffer_registry
, n
)
237 nobug_ringbuffer_sync ((struct nobug_ringbuffer
*) n
);
243 nobug_ringbuffer_vprintf (struct nobug_ringbuffer
* self
, const char* fmt
, va_list ap
)
245 int written
= vsnprintf (self
->pos
+ 1, self
->maxmsg
-2, fmt
, ap
);
246 self
->pos
+= (written
+ 1);
248 if (self
->pos
> self
->start
+self
->size
)
249 self
->pos
-= self
->size
;
258 nobug_ringbuffer_printf (struct nobug_ringbuffer
* self
, const char* fmt
, ...)
262 int written
= nobug_ringbuffer_vprintf (self
, fmt
, ap
);
269 nobug_ringbuffer_append (struct nobug_ringbuffer
* self
)
271 if (self
->pos
[-1] != 0)
279 resizes the current (last printed) message, the extended space filled with 'fill' characters unless
280 fill == 0, then the extended space is left untouched and expected to be properly set up by the client
281 returns 0 on failure (newsize would overflow the guard area)
284 nobug_ringbuffer_extend (struct nobug_ringbuffer
* self
, size_t newsize
, const char fill
)
286 char* prev
= nobug_ringbuffer_prev (self
, NULL
);
288 if (prev
>= self
->start
+self
->size
)
291 if (prev
+newsize
+2 > self
->start
+ self
->size
+ self
->maxmsg
)
295 memset(self
->pos
, fill
, prev
+newsize
-self
->pos
);
297 self
->pos
= prev
+newsize
;
306 nobug_ringbuffer_prev (struct nobug_ringbuffer
* self
, char* pos
)
315 if (pos
< self
->start
)
318 if (pos
[1] == (char)~0)
325 nobug_ringbuffer_next (struct nobug_ringbuffer
* self
, char* pos
)
332 if (pos
> self
->start
+self
->size
)
335 if (*pos
== (char)~0)
342 nobug_ringbuffer_save (struct nobug_ringbuffer
* self
, FILE* out
)
348 for (next
= nobug_ringbuffer_next (self
, NULL
); next
; next
= nobug_ringbuffer_next(self
, next
))
350 cnt
= fprintf (out
,"%s\n", next
);
360 nobug_ringbuffer_load (struct nobug_ringbuffer
* self
, FILE* in
)
363 char buf
[NOBUG_RINGBUFFER_MAX_MSG
];
365 while (fgets(buf
, self
->maxmsg
, in
))
367 size_t l
= strlen(buf
);
368 if (buf
[l
-1] == '\n')
370 ret
+= nobug_ringbuffer_printf (self
, "%s", buf
);
376 nobug_ringbuffer_pos (struct nobug_ringbuffer
* self
)
382 nobug_ringbuffer_pop (struct nobug_ringbuffer
* self
)
384 self
->pos
[0] = '\n'; /* clear the \0 */
385 self
->pos
[1] = ' '; /* clear the ~0 */
387 self
->pos
= strrchr(self
->pos
, 0);
388 if (self
->pos
< self
->start
)
389 self
->pos
+= self
->size
;