From e486eb1edeff097c4ac9c23ab10025ccb3597b51 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 2 Jul 2007 20:39:56 +0000 Subject: [PATCH] Attempt to implement buffer freeing. Something is wrong somewhere with the moving, but I couldn't figure out what. Disabled the caching in case it is interfering. Added a function to graphically display the status of the ring buffer. More details later, I have to go. git-svn-id: svn://jdgordon.mine.nu/mob@20 9862a28c-4e93-4879-ac26-10afcf840a8f --- buffering.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 101 insertions(+), 11 deletions(-) diff --git a/buffering.c b/buffering.c index f21a0e1..97808bb 100644 --- a/buffering.c +++ b/buffering.c @@ -164,9 +164,10 @@ static struct memory_handle *find_handle(int handle_id) struct memory_handle *m = first_handle; /* simple caching because most of the time the requested handle will either be the same as the last, or the one after the last */ +#if 0 if (last_m) { - if (last_handle_id == handle_id) + if (last_handle_id == handle_id && last_handle_id == last_m->id) return last_m; else if (last_m->next && (last_m->next->id == handle_id)) { @@ -178,6 +179,7 @@ static struct memory_handle *find_handle(int handle_id) return last_m; } } +#endif while (m && m->id != handle_id) { m = m->next; } @@ -217,6 +219,8 @@ static int buffer_handle(int handle_id) int ret = 0; while (h->filerem > 0) { + //printf("h: %d\n", (void *)h - (void *)buffer); + //printf("buf_widx: %ld\n", (long)buf_widx); /* max amount to copy */ size_t copy_n = MIN(conf_filechunk, buffer_len - buf_widx); @@ -228,7 +232,8 @@ static int buffer_handle(int handle_id) if (rc < 0) { - printf("File ended %ldB early", (long)h->filerem); + printf("failed on fd %d at buf_widx = %ld for handle %d\n", h->fd, (long)buf_widx, h->id); + printf("File ended %ld bytes early\n", (long)h->filerem); h->filesize -= h->filerem; h->filerem = 0; break; @@ -251,6 +256,19 @@ static int buffer_handle(int handle_id) return ret; } +/* this seems wrong */ +static void free_buffer(void) +{ + size_t delta = RINGBUF_SUB(first_handle->buf_idx, first_handle->data); + size_t dest = RINGBUF_ADD((void *)first_handle - (void *)buffer, delta); + printf("buf_ridx: %ld, buf_widx: %ld\n", (long)buf_ridx, (long)buf_widx); + printf("dest: %ld, delta: %ld\n", (long)dest, (long)delta); + first_handle = move_handle(dest, first_handle); + first_handle->data = RINGBUF_ADD(first_handle->data, delta); + first_handle->buf_idx = first_handle->data; + buf_ridx = dest; +} + /* Request a file be buffered filename: name of the file t open offset: starting offset to buffer from the file @@ -296,12 +314,17 @@ int bufclose(int handle_id) if (!h) return -1; - /* when we close the first handle, we can reclaim the space from its buffer */ - if (h == first_handle) { - buf_ridx = RINGBUF_ADD(h->buf_idx, h->available); + if (num_handles > 1) { + rm_handle(h); + buf_ridx = (size_t)first_handle - (size_t)buffer; + } else { + printf("closing the first and last handle\n"); + /* num_handles == 1, therefore h == first_handle */ + buf_ridx = buf_widx; + rm_handle(h); } - return rm_handle(h) ? 0 : -1; + return 0; } /* Seek in a handle. Return 0 for success and < 0 for failure */ @@ -334,6 +357,7 @@ int bufread(int handle_id, size_t size, char *dest) memcpy(dest+read, buffer, buffered_data - read); } else memcpy(dest, &buffer[h->buf_idx], buffered_data); + h->buf_idx = RINGBUF_ADD(h->buf_idx, buffered_data); h->available -= buffered_data; return buffered_data; @@ -394,14 +418,18 @@ bool test_ll(void) if (cur_handle != m4 || first_handle != m1) return false; + int m2id = m2->id; m2 = move_handle(m2->data + 1024*1024, m2); - if (cur_handle != m4 || first_handle != m1 || m1->next != m2 || m2->next != m3) + if (cur_handle != m4 || first_handle != m1 || m1->next != m2 || m2->next != m3 || + find_handle(m2id) != m2) return false; + int m1id = m1->id; m1 = move_handle(m1->data + 1024*1024*3, m1); - if (cur_handle != m4 || first_handle != m1 || m1->next != m2) + if (cur_handle != m4 || first_handle != m1 || m1->next != m2 || + find_handle(m1id) != m1) return false; rm_handle(m1); @@ -424,6 +452,52 @@ static void list_handles(void) } } +/* display a nice graphical view of the ringbuffer. */ +static void graph_view(int width) +{ + int i, r_pos, w_pos; + r_pos = buf_ridx * width / buffer_len; + w_pos = buf_widx * width / buffer_len; + + printf("|"); + for (i=0; i <= width; i++) + { + if (i != r_pos && i != w_pos) + { + if (buf_ridx <= buf_widx) + { + if (i > r_pos && i < w_pos) + printf(">"); + else + printf("-"); + } + else + { + if (i > r_pos || i < w_pos) + printf(">"); + else + printf("-"); + } + } + else + { + if (i == r_pos && i == w_pos) + { + if (buf_ridx <= buf_widx) + printf("RW"); + else + printf("WR"); + } + else if (i == r_pos) + printf("R"); + else if (i == w_pos) + printf("W"); + } + } + printf("|"); + printf("\n"); +} + void buffer_init(void) { buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + GUARD_SIZE)); @@ -485,10 +559,12 @@ int main(int argc, char *argv[]) while (done == false) { + printf("buffer usage: %d handles_used: %d\n", BUF_USED,num_handles); + graph_view(100); + /* "Buffering thread" section */ if ((next_file <= argc) && need_rebuffer()) { - printf("buffer usage: %d handles_used: %d\n", BUF_USED,num_handles); if ( !m || ((m->filerem == 0) && (m->next == NULL))) { int h = bufopen(argv[next_file++], 0); @@ -514,8 +590,10 @@ int main(int argc, char *argv[]) { printf("reading handle: %d\n", handle_order[reading_handle]); long read; + long total = 0; char file[MAX_PATH]; - if (reading_handle >= last_handle) + if (reading_handle >= last_handle + && !handle_has_data(handle_order[reading_handle])) done = true; if (fd < 0) @@ -532,8 +610,12 @@ int main(int argc, char *argv[]) { read = bufread(handle_order[reading_handle], GUARD_SIZE,read_buffer); write(fd, read_buffer, read); + total += read; } while (read > 0); + + printf("read %ld bytes from handle %d\n", total, handle_order[reading_handle]); + /* close the fd/handle if there is no more data or an error */ if (read < 0 || handle_has_data(handle_order[reading_handle]) == false) { @@ -543,9 +625,17 @@ int main(int argc, char *argv[]) reading_handle++; fd = -1; } + else + { + printf("there is data left to buffer for %d\n", handle_order[reading_handle]); + free_buffer(); + } } } - + + printf("buffer usage: %d handles_used: %d\n", BUF_USED,num_handles); + graph_view(100); + free(buffer); return 0; } -- 2.11.4.GIT