1 /*****************************************************************************
2 * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007
3 *****************************************************************************
4 * rrd_open.c Open an RRD File
5 *****************************************************************************
7 *****************************************************************************/
13 /* DEBUG 2 prints information obtained via mincore(2) */
15 /* do not calculate exact madvise hints but assume 1 page for headers and
16 * set DONTNEED for the rest, which is assumed to be data */
17 /* Avoid calling madvise on areas that were already hinted. May be benefical if
18 * your syscalls are very slow */
21 /* the cast to void* is there to avoid this warning seen on ia64 with certain
22 versions of gcc: 'cast increases required alignment of target type'
24 #define __rrd_read(dst, dst_t, cnt) \
25 (dst) = (dst_t*)(void*) (data + offset); \
26 offset += sizeof(dst_t) * (cnt)
28 #define __rrd_read(dst, dst_t, cnt) \
29 if ((dst = malloc(sizeof(dst_t)*(cnt))) == NULL) { \
30 rrd_set_error(#dst " malloc"); \
31 goto out_nullify_head; \
33 offset += read (rrd_file->fd, dst, sizeof(dst_t)*(cnt))
36 /* get the address of the start of this page */
38 #define PAGE_START(addr) ((addr)&(~(_page_size-1)))
42 /* Open a database file, return its header and an open filehandle,
43 * positioned to the first cdp in the first rra.
44 * In the error path of rrd_open, only rrd_free(&rrd) has to be called
45 * before returning an error. Do not call rrd_close upon failure of rrd_open.
49 const char *const file_name,
54 mode_t mode = S_IRUSR;
58 ssize_t _page_size = sysconf(_SC_PAGESIZE);
59 int mm_prot = PROT_READ, mm_flags = 0;
64 rrd_file_t *rrd_file = NULL;
65 off_t newfile_size = 0;
67 if (rdwr & RRD_CREAT) {
68 /* yes bad inline signaling alert, we are using the
69 floatcookie to pass the size in ... only used in resize */
70 newfile_size = (off_t) rrd->stat_head->float_cookie;
74 rrd_file = malloc(sizeof(rrd_file_t));
75 if (rrd_file == NULL) {
76 rrd_set_error("allocating rrd_file descriptor for '%s'", file_name);
79 memset(rrd_file, 0, sizeof(rrd_file_t));
82 if ((rdwr & (RRD_READONLY | RRD_READWRITE)) ==
83 (RRD_READONLY | RRD_READWRITE)) {
84 /* Both READONLY and READWRITE were given, which is invalid. */
85 rrd_set_error("in read/write request mask");
89 if (rdwr & RRD_READONLY) {
92 mm_flags = MAP_PRIVATE;
94 mm_flags |= MAP_NORESERVE; /* readonly, so no swap backing needed */
98 if (rdwr & RRD_READWRITE) {
102 mm_flags = MAP_SHARED;
103 mm_prot |= PROT_WRITE;
106 if (rdwr & RRD_CREAT) {
107 flags |= (O_CREAT | O_TRUNC);
110 if (rdwr & RRD_READAHEAD) {
112 mm_flags |= MAP_POPULATE; /* populate ptes and data */
114 #if defined MAP_NONBLOCK
115 mm_flags |= MAP_NONBLOCK; /* just populate ptes */
119 if ((rrd_file->fd = open(file_name, flags, mode)) < 0) {
120 rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));
124 /* Better try to avoid seeks as much as possible. stat may be heavy but
125 * many concurrent seeks are even worse. */
126 if (newfile_size == 0 && ((fstat(rrd_file->fd, &statb)) < 0)) {
127 rrd_set_error("fstat '%s': %s", file_name, rrd_strerror(errno));
130 if (newfile_size == 0) {
131 rrd_file->file_len = statb.st_size;
133 rrd_file->file_len = newfile_size;
134 lseek(rrd_file->fd, newfile_size - 1, SEEK_SET);
135 write(rrd_file->fd, "\0", 1); /* poke */
136 lseek(rrd_file->fd, 0, SEEK_SET);
138 #ifdef HAVE_POSIX_FADVISE
139 /* In general we need no read-ahead when dealing with rrd_files.
140 When we stop reading, it is highly unlikely that we start up again.
141 In this manner we actually save time and diskaccess (and buffer cache).
142 Thanks to Dave Plonka for the Idea of using POSIX_FADV_RANDOM here. */
143 if (0 != posix_fadvise(rrd_file->fd, 0, 0, POSIX_FADV_RANDOM)) {
144 rrd_set_error("setting POSIX_FADV_RANDOM on '%s': %s", file_name,
145 rrd_strerror(errno));
151 if (rdwr & RRD_READWRITE)
153 if (setvbuf((rrd_file->fd),NULL,_IONBF,2)) {
154 rrd_set_error("failed to disable the stream buffer\n");
160 data = mmap(0, rrd_file->file_len, mm_prot, mm_flags,
161 rrd_file->fd, offset);
163 /* lets see if the first read worked */
164 if (data == MAP_FAILED) {
165 rrd_set_error("mmaping file '%s': %s", file_name,
166 rrd_strerror(errno));
169 rrd_file->file_start = data;
170 if (rdwr & RRD_CREAT) {
171 memset(data, DNAN, newfile_size - 1);
175 if (rdwr & RRD_CREAT)
178 if (rdwr & RRD_COPY) {
179 /* We will read everything in a moment (copying) */
180 madvise(data, rrd_file->file_len, MADV_WILLNEED | MADV_SEQUENTIAL);
182 /* We do not need to read anything in for the moment */
183 madvise(data, rrd_file->file_len, MADV_RANDOM);
184 /* the stat_head will be needed soonish, so hint accordingly */
185 madvise(data, sizeof(stat_head_t), MADV_WILLNEED | MADV_RANDOM);
189 __rrd_read(rrd->stat_head, stat_head_t,
192 /* lets do some test if we are on track ... */
193 if (memcmp(rrd->stat_head->cookie, RRD_COOKIE, sizeof(RRD_COOKIE)) != 0) {
194 rrd_set_error("'%s' is not an RRD file", file_name);
195 goto out_nullify_head;
198 if (rrd->stat_head->float_cookie != FLOAT_COOKIE) {
199 rrd_set_error("This RRD was created on another architecture");
200 goto out_nullify_head;
203 version = atoi(rrd->stat_head->version);
205 if (version > atoi(RRD_VERSION)) {
206 rrd_set_error("can't handle RRD file version %s",
207 rrd->stat_head->version);
208 goto out_nullify_head;
210 #if defined USE_MADVISE
211 /* the ds_def will be needed soonish, so hint accordingly */
212 madvise(data + PAGE_START(offset),
213 sizeof(ds_def_t) * rrd->stat_head->ds_cnt, MADV_WILLNEED);
215 __rrd_read(rrd->ds_def, ds_def_t,
216 rrd->stat_head->ds_cnt);
218 #if defined USE_MADVISE
219 /* the rra_def will be needed soonish, so hint accordingly */
220 madvise(data + PAGE_START(offset),
221 sizeof(rra_def_t) * rrd->stat_head->rra_cnt, MADV_WILLNEED);
223 __rrd_read(rrd->rra_def, rra_def_t,
224 rrd->stat_head->rra_cnt);
226 /* handle different format for the live_head */
228 rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
229 if (rrd->live_head == NULL) {
230 rrd_set_error("live_head_t malloc");
234 memmove(&rrd->live_head->last_up, data + offset, sizeof(long));
235 offset += sizeof(long);
237 offset += read(rrd_file->fd, &rrd->live_head->last_up, sizeof(long));
239 rrd->live_head->last_up_usec = 0;
241 #if defined USE_MADVISE
242 /* the live_head will be needed soonish, so hint accordingly */
243 madvise(data + PAGE_START(offset),
244 sizeof(live_head_t), MADV_WILLNEED);
246 __rrd_read(rrd->live_head, live_head_t,
249 //XXX: This doesn't look like it needs madvise
250 __rrd_read(rrd->pdp_prep, pdp_prep_t,
251 rrd->stat_head->ds_cnt);
253 //XXX: This could benefit from madvise()ing
254 __rrd_read(rrd->cdp_prep, cdp_prep_t,
255 rrd->stat_head->rra_cnt * rrd->stat_head->ds_cnt);
257 //XXX: This could benefit from madvise()ing
258 __rrd_read(rrd->rra_ptr, rra_ptr_t,
259 rrd->stat_head->rra_cnt);
261 rrd_file->header_len = offset;
262 rrd_file->pos = offset;
266 rrd->stat_head = NULL;
275 /* Close a reference to an rrd_file. */
278 rrd_file_t *rrd_file,
282 /* pretty print blocks in core */
285 ssize_t _page_size = sysconf(_SC_PAGESIZE);
287 off = rrd_file->file_len +
288 ((rrd_file->file_len + _page_size - 1) / _page_size);
292 if (mincore(rrd_file->file_start, rrd_file->file_len, vec) == 0) {
294 unsigned is_in = 0, was_in = 0;
296 for (off = 0, prev = 0; off < rrd_file->file_len; ++off) {
297 is_in = vec[off] & 1; /* if lsb set then is core resident */
300 if (was_in != is_in) {
301 fprintf(stderr, "%s: %sin core: %p len %ld\n", mark,
302 was_in ? "" : "not ", vec + prev, off - prev);
308 "%s: %sin core: %p len %ld\n", mark,
309 was_in ? "" : "not ", vec + prev, off - prev);
311 fprintf(stderr, "mincore: %s", rrd_strerror(errno));
314 fprintf(stderr, "sorry mincore only works with mmap");
319 /* drop cache except for the header and the active pages */
321 rrd_file_t *rrd_file,
324 unsigned long dontneed_start;
325 unsigned long rra_start;
326 unsigned long active_block;
328 ssize_t _page_size = sysconf(_SC_PAGESIZE);
330 #if defined DEBUG && DEBUG > 1
331 mincore_print(rrd_file, "before");
334 /* ignoring errors from RRDs that are smaller then the file_len+rounding */
335 rra_start = rrd_file->header_len;
336 dontneed_start = PAGE_START(rra_start) + _page_size;
337 for (i = 0; i < rrd->stat_head->rra_cnt; ++i) {
340 + rrd->rra_ptr[i].cur_row
341 * rrd->stat_head->ds_cnt * sizeof(rrd_value_t));
342 if (active_block > dontneed_start) {
344 madvise(rrd_file->file_start + dontneed_start,
345 active_block - dontneed_start - 1, MADV_DONTNEED);
347 /* in linux at least only fadvise DONTNEED seems to purge pages from cache */
348 #ifdef HAVE_POSIX_FADVISE
349 posix_fadvise(rrd_file->fd, dontneed_start,
350 active_block - dontneed_start - 1,
351 POSIX_FADV_DONTNEED);
354 dontneed_start = active_block;
355 /* do not relase 'hot' block if update for this RAA will occure within 10 minutes */
356 if (rrd->stat_head->pdp_step * rrd->rra_def[i].pdp_cnt -
357 rrd->live_head->last_up % (rrd->stat_head->pdp_step *
358 rrd->rra_def[i].pdp_cnt) < 10 * 60) {
359 dontneed_start += _page_size;
362 rrd->rra_def[i].row_cnt * rrd->stat_head->ds_cnt *
366 madvise(rrd_file->file_start + dontneed_start,
367 rrd_file->file_len - dontneed_start, MADV_DONTNEED);
369 #ifdef HAVE_POSIX_FADVISE
370 posix_fadvise(rrd_file->fd, dontneed_start,
371 rrd_file->file_len - dontneed_start, POSIX_FADV_DONTNEED);
373 #if defined DEBUG && DEBUG > 1
374 mincore_print(rrd_file, "after");
379 rrd_file_t *rrd_file)
384 ret = munmap(rrd_file->file_start, rrd_file->file_len);
386 rrd_set_error("munmap rrd_file: %s", rrd_strerror(errno));
388 ret = close(rrd_file->fd);
390 rrd_set_error("closing file: %s", rrd_strerror(errno));
397 /* Set position of rrd_file. */
400 rrd_file_t *rrd_file,
407 if (whence == SEEK_SET)
409 else if (whence == SEEK_CUR)
410 rrd_file->pos += off;
411 else if (whence == SEEK_END)
412 rrd_file->pos = rrd_file->file_len + off;
414 ret = lseek(rrd_file->fd, off, whence);
416 rrd_set_error("lseek: %s", rrd_strerror(errno));
419 //XXX: mimic fseek, which returns 0 upon success
420 return ret == -1; //XXX: or just ret to mimic lseek
424 /* Get current position in rrd_file. */
426 inline off_t rrd_tell(
427 rrd_file_t *rrd_file)
429 return rrd_file->pos;
433 /* read count bytes into buffer buf, starting at rrd_file->pos.
434 * Returns the number of bytes read or <0 on error. */
436 inline ssize_t rrd_read(
437 rrd_file_t *rrd_file,
443 ssize_t _surplus = rrd_file->pos + _cnt - rrd_file->file_len;
445 if (_surplus > 0) { /* short read */
450 buf = memcpy(buf, rrd_file->file_start + rrd_file->pos, _cnt);
452 rrd_file->pos += _cnt; /* mimmic read() semantics */
457 ret = read(rrd_file->fd, buf, count);
459 rrd_file->pos += ret; /* mimmic read() semantics */
465 /* write count bytes from buffer buf to the current position
466 * rrd_file->pos of rrd_file->fd.
467 * Returns the number of bytes written. */
469 inline ssize_t rrd_write(
470 rrd_file_t *rrd_file,
475 memcpy(rrd_file->file_start + rrd_file->pos, buf, count);
476 rrd_file->pos += count;
477 return count; /* mimmic write() semantics */
479 ssize_t _sz = write(rrd_file->fd, buf, count);
482 rrd_file->pos += _sz;
488 /* flush all data pending to be written to FD. */
490 inline void rrd_flush(
491 rrd_file_t *rrd_file)
493 if (fdatasync(rrd_file->fd) != 0) {
494 rrd_set_error("flushing fd %d: %s", rrd_file->fd,
495 rrd_strerror(errno));
500 /* Initialize RRD header. */
505 rrd->stat_head = NULL;
508 rrd->live_head = NULL;
510 rrd->pdp_prep = NULL;
511 rrd->cdp_prep = NULL;
512 rrd->rrd_value = NULL;
516 /* free RRD header data. */
519 inline void rrd_free(
527 free(rrd->live_head);
528 free(rrd->stat_head);
534 free(rrd->rrd_value);
539 /* routine used by external libraries to free memory allocated by
549 /* XXX: FIXME: missing documentation. */
550 /*XXX: FIXME should be renamed to rrd_readfile or _rrd_readfile */
552 int /*_rrd_*/ readfile(
553 const char *file_name,
557 long writecnt = 0, totalcnt = MEMBLK;
562 if ((strcmp("-", file_name) == 0)) {
565 if ((input = fopen(file_name, "rb")) == NULL) {
566 rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno));
574 } while (c != '\n' && !feof(input));
576 if (strcmp("-", file_name)) {
577 fseek(input, 0, SEEK_END);
578 /* have extra space for detecting EOF without realloc */
579 totalcnt = (ftell(input) + 1) / sizeof(char) - offset;
580 if (totalcnt < MEMBLK)
581 totalcnt = MEMBLK; /* sanitize */
582 fseek(input, offset * sizeof(char), SEEK_SET);
584 if (((*buffer) = (char *) malloc((totalcnt + 4) * sizeof(char))) == NULL) {
585 perror("Allocate Buffer:");
590 fread((*buffer) + writecnt, 1,
591 (totalcnt - writecnt) * sizeof(char), input);
592 if (writecnt >= totalcnt) {
595 rrd_realloc((*buffer),
596 (totalcnt + 4) * sizeof(char))) == NULL) {
597 perror("Realloc Buffer:");
601 } while (!feof(input));
602 (*buffer)[writecnt] = '\0';
603 if (strcmp("-", file_name) != 0) {