-
-#define MYFREAD(MYVAR,MYVART,MYCNT) \
- if ((MYVAR = malloc(sizeof(MYVART) * MYCNT)) == NULL) {\
- rrd_set_error("" #MYVAR " malloc"); \
- fclose(*in_file); \
- return (-1); } \
- fread(MYVAR,sizeof(MYVART),MYCNT, *in_file);
-
-
- MYFREAD(rrd->stat_head, stat_head_t, 1)
-
- /* lets do some test if we are on track ... */
- if (strncmp(rrd->stat_head->cookie,RRD_COOKIE,4) != 0){
- rrd_set_error("'%s' is not an RRD file",file_name);
- free(rrd->stat_head);
- fclose(*in_file);
- return(-1);}
-
- if (atoi(rrd->stat_head->version) > atoi(RRD_VERSION)){
- rrd_set_error("can't handle RRD file version %s",
- rrd->stat_head->version);
- free(rrd->stat_head);
- fclose(*in_file);
- return(-1);}
-
- if (rrd->stat_head->float_cookie != FLOAT_COOKIE){
- rrd_set_error("This RRD was created on other architecture");
- free(rrd->stat_head);
- fclose(*in_file);
- return(-1);}
-
- MYFREAD(rrd->ds_def, ds_def_t, rrd->stat_head->ds_cnt)
- MYFREAD(rrd->rra_def, rra_def_t, rrd->stat_head->rra_cnt)
- MYFREAD(rrd->live_head, live_head_t, 1)
- MYFREAD(rrd->pdp_prep, pdp_prep_t, rrd->stat_head->ds_cnt)
- MYFREAD(rrd->cdp_prep, cdp_prep_t, (rrd->stat_head->rra_cnt
- * rrd->stat_head->ds_cnt))
- MYFREAD(rrd->rra_ptr, rra_ptr_t, rrd->stat_head->rra_cnt)
-#undef MYFREAD
-
- return(0);
+#ifdef HAVE_MMAP
+ data = mmap(0, rrd_file->file_len, mm_prot, mm_flags,
+ rrd_file->fd, offset);
+
+ /* lets see if the first read worked */
+ if (data == MAP_FAILED) {
+ rrd_set_error("error mmaping file '%s': %s", file_name,
+ rrd_strerror(errno));
+ goto out_close;
+ }
+ rrd_file->file_start = data;
+#endif
+#ifdef USE_MADVISE
+ if (rdwr & RRD_COPY) {
+ /* We will read everything in a moment (copying) */
+ _madvise(data, rrd_file->file_len, MADV_WILLNEED | MADV_SEQUENTIAL);
+ goto out_done;
+ }
+ /* We do not need to read anything in for the moment */
+#ifndef ONE_PAGE
+ _madvise(data, rrd_file->file_len, MADV_DONTNEED);
+// _madvise(data, rrd_file->file_len, MADV_RANDOM);
+#else
+/* alternatively: keep 2 pages worth of data, likely headers,
+ * don't need the rest. */
+ _madvise(data, _page_size, MADV_WILLNEED | MADV_SEQUENTIAL);
+ _madvise(data + _page_size, (rrd_file->file_len >= _page_size)
+ ? rrd_file->file_len - _page_size : 0, MADV_DONTNEED);
+#endif
+#endif
+
+#if defined USE_MADVISE && !defined ONE_PAGE
+ /* the stat_head will be needed soonish, so hint accordingly */
+// too finegrained to calc the individual sizes, just keep 2 pages worth of hdr
+ _madvise(data + PAGE_ALIGN_DOWN(offset), PAGE_ALIGN(sizeof(stat_head_t)),
+ MADV_WILLNEED);
+
+#endif
+
+ __rrd_read(rrd->stat_head, stat_head_t,
+ 1);
+
+ /* lets do some test if we are on track ... */
+ if (memcmp(rrd->stat_head->cookie, RRD_COOKIE, sizeof(RRD_COOKIE)) != 0) {
+ rrd_set_error("'%s' is not an RRD file", file_name);
+ goto out_nullify_head;
+ }
+
+ if (rrd->stat_head->float_cookie != FLOAT_COOKIE) {
+ rrd_set_error("This RRD was created on other architecture");
+ goto out_nullify_head;
+ }
+
+ version = atoi(rrd->stat_head->version);
+
+ if (version > atoi(RRD_VERSION)) {
+ rrd_set_error("can't handle RRD file version %s",
+ rrd->stat_head->version);
+ goto out_nullify_head;
+ }
+#if defined USE_MADVISE && !defined ONE_PAGE
+ /* the ds_def will be needed soonish, so hint accordingly */
+ _madvise(data + PAGE_ALIGN_DOWN(offset),
+ PAGE_ALIGN(sizeof(ds_def_t) * rrd->stat_head->ds_cnt),
+ MADV_WILLNEED);
+#endif
+ __rrd_read(rrd->ds_def, ds_def_t,
+ rrd->stat_head->ds_cnt);
+
+#if defined USE_MADVISE && !defined ONE_PAGE
+ /* the rra_def will be needed soonish, so hint accordingly */
+ _madvise(data + PAGE_ALIGN_DOWN(offset),
+ PAGE_ALIGN(sizeof(rra_def_t) * rrd->stat_head->rra_cnt),
+ MADV_WILLNEED);
+#endif
+ __rrd_read(rrd->rra_def, rra_def_t,
+ rrd->stat_head->rra_cnt);
+
+ /* handle different format for the live_head */
+ if (version < 3) {
+ rrd->live_head = (live_head_t *) malloc(sizeof(live_head_t));
+ if (rrd->live_head == NULL) {
+ rrd_set_error("live_head_t malloc");
+ goto out_close;
+ }
+#ifdef HAVE_MMAP
+ memmove(&rrd->live_head->last_up, data + offset, sizeof(long));
+ offset += sizeof(long);
+#else
+ offset += read(rrd_file->fd, &rrd->live_head->last_up, sizeof(long));
+#endif
+ rrd->live_head->last_up_usec = 0;
+ } else {
+#if defined USE_MADVISE && !defined ONE_PAGE
+ /* the live_head will be needed soonish, so hint accordingly */
+ _madvise(data + PAGE_ALIGN_DOWN(offset),
+ PAGE_ALIGN(sizeof(live_head_t)), MADV_WILLNEED);
+#endif
+ __rrd_read(rrd->live_head, live_head_t,
+ 1);
+ }
+//XXX: This doesn't look like it needs madvise
+ __rrd_read(rrd->pdp_prep, pdp_prep_t,
+ rrd->stat_head->ds_cnt);
+
+//XXX: This could benefit from madvise()ing
+ __rrd_read(rrd->cdp_prep, cdp_prep_t,
+ rrd->stat_head->rra_cnt * rrd->stat_head->ds_cnt);
+
+//XXX: This could benefit from madvise()ing
+ __rrd_read(rrd->rra_ptr, rra_ptr_t,
+ rrd->stat_head->rra_cnt);
+
+#ifdef USE_MADVISE
+ out_done:
+#endif
+ rrd_file->header_len = offset;
+ rrd_file->pos = offset;
+
+ return (rrd_file);
+ out_nullify_head:
+ rrd->stat_head = NULL;
+ out_close:
+ close(rrd_file->fd);
+ return NULL;