summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a8f1c7e)
raw | patch | inline | side by side (parent: a8f1c7e)
author | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Fri, 1 Jun 2007 17:34:15 +0000 (17:34 +0000) | ||
committer | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Fri, 1 Jun 2007 17:34:15 +0000 (17:34 +0000) |
- implement resize, growing part. truncation is to be done.
git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk@1097 a5681a0c-68f1-0310-ab6d-d61299d08faa
git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk@1097 a5681a0c-68f1-0310-ab6d-d61299d08faa
program/src/rrd_open.c | patch | blob | history | |
program/src/rrd_resize.c | patch | blob | history |
diff --git a/program/src/rrd_open.c b/program/src/rrd_open.c
index 746044edc0cadb9380b53038e29e2db43c71166e..7812da183b5dd5e61a7da9e34bdeab0dc3bc8291 100644 (file)
--- a/program/src/rrd_open.c
+++ b/program/src/rrd_open.c
off_t offset = 0;
struct stat statb;
rrd_file_t *rrd_file = NULL;
+ off_t newfile_size = 0;
+ if (rdwr & RRD_CREAT)
+ newfile_size = (off_t) rrd->stat_head;
rrd_init(rrd);
rrd_file = malloc(sizeof(rrd_file_t));
if (rrd_file == NULL) {
mm_flags |= MAP_POPULATE; /* populate ptes and data */
#endif
#if defined MAP_NONBLOCK
+// if (!(rdwr & RRD_COPY))
mm_flags |= MAP_NONBLOCK; /* just populate ptes */
#endif
#ifdef USE_DIRECT_IO
/* Better try to avoid seeks as much as possible. stat may be heavy but
* many concurrent seeks are even worse. */
- if ((fstat(rrd_file->fd, &statb)) < 0) {
+ if (newfile_size == 0 && ((fstat(rrd_file->fd, &statb)) < 0)) {
rrd_set_error("fstat '%s': %s", file_name, rrd_strerror(errno));
goto out_close;
}
- rrd_file->file_len = statb.st_size;
-
+ if (newfile_size == 0) {
+ rrd_file->file_len = statb.st_size;
+ } else {
+ rrd_file->file_len = newfile_size;
+ lseek(rrd_file->fd, newfile_size - 1, SEEK_SET);
+ write(rrd_file->fd, "\0", 1); /* poke */
+ lseek(rrd_file->fd, 0, SEEK_SET);
+ }
#ifdef HAVE_POSIX_FADVISE
/* In general we need no read-ahead when dealing with rrd_files.
When we stop reading, it is highly unlikely that we start up again.
goto out_close;
}
rrd_file->file_start = data;
+ if (rdwr & RRD_CREAT) {
+ goto out_done;
+ }
#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;
- }
+ } else {
# ifndef ONE_PAGE
- /* We do not need to read anything in for the moment */
- _madvise(data, rrd_file->file_len, MADV_DONTNEED);
+ /* We do not need to read anything in for the moment */
+ _madvise(data, rrd_file->file_len, MADV_DONTNEED);
+ /* the stat_head will be needed soonish, so hint accordingly */
+ _madvise(data + PAGE_ALIGN_DOWN(offset),
+ PAGE_ALIGN(sizeof(stat_head_t)),
+ MADV_WILLNEED | MADV_RANDOM);
+
# else
/* alternatively: keep 1 page 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);
+ _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 */
- _madvise(data + PAGE_ALIGN_DOWN(offset), PAGE_ALIGN(sizeof(stat_head_t)),
- MADV_WILLNEED | MADV_RANDOM);
-
+ }
#endif
__rrd_read(rrd->stat_head, stat_head_t,
__rrd_read(rrd->rra_ptr, rra_ptr_t,
rrd->stat_head->rra_cnt);
+ rrd_file->header_len = offset;
+ rrd_file->pos = offset;
#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;
/* read count bytes into buffer buf, starting at rrd_file->pos.
- * Returns the number of bytes read. */
+ * Returns the number of bytes read or <0 on error. */
inline ssize_t rrd_read(
rrd_file_t *rrd_file,
size_t count)
{
#ifdef HAVE_MMAP
- buf = memcpy(buf, rrd_file->file_start + rrd_file->pos, count);
- rrd_file->pos += count; /* mimmic read() semantics */
- return count;
+ size_t _cnt = count;
+ ssize_t _surplus = rrd_file->pos + _cnt - rrd_file->file_len;
+ if (_surplus > 0) { /* short read */
+ _cnt -= _surplus;
+ }
+ if (_cnt == 0)
+ return 0; /* EOF */
+ buf = memcpy(buf, rrd_file->file_start + rrd_file->pos, _cnt);
+
+ rrd_file->pos += _cnt; /* mimmic read() semantics */
+ return _cnt;
#else
ssize_t ret;
ret = read(rrd_file->fd, buf, count);
- //XXX: eventually add generic rrd_set_error(""); here
- rrd_file->pos += count; /* mimmic read() semantics */
+ if (ret > 0)
+ rrd_file->pos += ret; /* mimmic read() semantics */
return ret;
#endif
}
rrd_file->pos += count;
return count; /* mimmic write() semantics */
#else
- ssize_t _sz = write(rrd_file->fd, buf, count);
+ ssize_t _sz = write(rrd_file->fd, buf, count);
+
if (_sz > 0)
rrd_file->pos += _sz;
return _sz;
index 52c208b58f6c98869bafee934ba059b94f1cac12..55ced9ec5c35f0c9f51f613872daca4294d5fe4d 100644 (file)
--- a/program/src/rrd_resize.c
+++ b/program/src/rrd_resize.c
return (-1);
}
- if (modify < 0)
+ if (modify < 0) {
if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
rrd_set_error("This RRA is not that big");
rrd_free(&rrdold);
rrd_close(rrd_file);
return (-1);
}
-
+ } else {
+ /* the size of the new file */
+ rrdnew.stat_head = rrd_file->file_len +
+ (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify);
+ }
rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT);
if (rrd_out_file == NULL) {
rrd_set_error("Can't create '%s': %s", outfilename,