From c660159a1893ffade4ae88a4cc7014375b1ba635 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Mon, 22 Jul 2019 09:25:30 +0100 Subject: [PATCH 1/3] added mutex locking / unlocking messages --- src/cache.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/cache.c b/src/cache.c index 9e415dd..636b4cb 100644 --- a/src/cache.c +++ b/src/cache.c @@ -756,8 +756,18 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length, void Cache_close(Cache *cf) { /* Must wait for the background download thread to stop */ + fprintf(stderr, "Cache_read(): locking bgt_lock;\n"); + fflush(stderr); pthread_mutex_lock(&cf->bgt_lock); + fprintf(stderr, "Cache_read(): unlocking bgt_lock;\n"); + fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); + fprintf(stderr, "Cache_read(): locking rw_lock;\n"); + fflush(stderr); + pthread_mutex_lock(&cf->rw_lock); + fprintf(stderr, "Cache_read(): unlocking rw_lock;\n"); + fflush(stderr); + pthread_mutex_unlock(&cf->rw_lock); if (Meta_write(cf)) { fprintf(stderr, "Cache_close(): Meta_write() error."); @@ -807,6 +817,8 @@ static void Seg_set(Cache *cf, off_t offset, int i) static void *Cache_bgdl(void *arg) { Cache *cf = (Cache *) arg; + fprintf(stderr, "Cache_bgdl(): locking rw_lock;\n"); + fflush(stderr); pthread_mutex_lock(&cf->rw_lock); uint8_t *recv_buf = calloc(cf->blksz, sizeof(uint8_t)); fprintf(stderr, "Cache_bgdl(): "); @@ -823,7 +835,11 @@ static void *Cache_bgdl(void *arg) cf->next_offset); } free(recv_buf); + fprintf(stderr, "Cache_bgdl(): unlocking bgt_lock;\n"); + fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); + fprintf(stderr, "Cache_bgdl(): unlocking rw_lock;\n"); + fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); pthread_exit(NULL); } @@ -852,14 +868,23 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) send = Data_read(cf, (uint8_t *) output_buf, len, offset); goto bgdl; } else { - /* Wait for the other I/O threads to finish, then lock */ + /* Wait for any other download thread to finish, then lock */ + fprintf(stderr, "Cache_read(): locking rw_lock;\n"); + fflush(stderr); pthread_mutex_lock(&cf->rw_lock); /* Wait for the background download thread to finish */ + fprintf(stderr, "Cache_read(): locking bgt_lock;\n"); + fflush(stderr); pthread_mutex_lock(&cf->bgt_lock); + fprintf(stderr, "Cache_read(): unlocking bgt_lock;\n"); + fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); if (Seg_exist(cf, offset)) { - /* The segment already exists, send it off the unlock the I/O */ + /* The segment already exists - it was downloaded by the background + * download thread. Send it off and unlock the I/O */ send = Data_read(cf, (uint8_t *) output_buf, len, offset); + fprintf(stderr, "Cache_read(): unlocking rw_lock;\n"); + fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); goto bgdl; } @@ -893,6 +918,8 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) recv); } free(recv_buf); + fprintf(stderr, "Cache_read(): unlocking rw_lock;\n"); + fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); /* -----------Download the next segment in background -------------------*/ @@ -903,6 +930,8 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) cf->next_offset < cf->content_length ){ /* Stop the spawning of multiple background pthreads */ if(!pthread_mutex_trylock(&cf->bgt_lock)) { + fprintf(stderr, "Cache_read(): trylocked bgt_lock;\n"); + fflush(stderr); if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) { fprintf(stderr, "Cache_read(): Error creating background download thread\n" From 64cd6ca2c6f36f41f91b4f1ed018dbabb6855791 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Wed, 24 Jul 2019 17:55:32 +0100 Subject: [PATCH 2/3] added more debug messages --- src/cache.c | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/cache.c b/src/cache.c index 636b4cb..cb7d95b 100644 --- a/src/cache.c +++ b/src/cache.c @@ -756,16 +756,16 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length, void Cache_close(Cache *cf) { /* Must wait for the background download thread to stop */ - fprintf(stderr, "Cache_read(): locking bgt_lock;\n"); + fprintf(stderr, "Cache_close(): locking bgt_lock;\n"); fflush(stderr); pthread_mutex_lock(&cf->bgt_lock); - fprintf(stderr, "Cache_read(): unlocking bgt_lock;\n"); + fprintf(stderr, "Cache_close(): unlocking bgt_lock;\n"); fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); - fprintf(stderr, "Cache_read(): locking rw_lock;\n"); + fprintf(stderr, "Cache_close(): locking rw_lock;\n"); fflush(stderr); pthread_mutex_lock(&cf->rw_lock); - fprintf(stderr, "Cache_read(): unlocking rw_lock;\n"); + fprintf(stderr, "Cache_close(): unlocking rw_lock;\n"); fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); @@ -817,11 +817,12 @@ static void Seg_set(Cache *cf, off_t offset, int i) static void *Cache_bgdl(void *arg) { Cache *cf = (Cache *) arg; - fprintf(stderr, "Cache_bgdl(): locking rw_lock;\n"); + fprintf(stderr, "Cache_bgdl(): thread %lu: locking rw_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_lock(&cf->rw_lock); uint8_t *recv_buf = calloc(cf->blksz, sizeof(uint8_t)); - fprintf(stderr, "Cache_bgdl(): "); + fprintf(stderr, "Cache_bgdl(): thread %lu:", pthread_self()); long recv = path_download(cf->path, (char *) recv_buf, cf->blksz, cf->next_offset); if ( (recv == cf->blksz) || @@ -835,10 +836,12 @@ static void *Cache_bgdl(void *arg) cf->next_offset); } free(recv_buf); - fprintf(stderr, "Cache_bgdl(): unlocking bgt_lock;\n"); + fprintf(stderr, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); - fprintf(stderr, "Cache_bgdl(): unlocking rw_lock;\n"); + fprintf(stderr, "Cache_bgdl(): thread %lu: unlocking rw_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); pthread_exit(NULL); @@ -846,11 +849,12 @@ cf->next_offset); long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) { -// size_t start = offset; -// size_t end = start + len; -// char range_str[64]; -// snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end); -// fprintf(stderr, "Cache_read(%s, %s);\n", cf->path, range_str); + size_t start = offset; + size_t end = start + len; + char range_str[64]; + snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end); + fprintf(stderr, "Cache_read(): thread %lu: %s, %s;\n", pthread_self(), + cf->path, range_str); /* SIGFPE prevention, although this shouldn't happen in the first place! */ if (!cf->blksz) { @@ -869,21 +873,25 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) goto bgdl; } else { /* Wait for any other download thread to finish, then lock */ - fprintf(stderr, "Cache_read(): locking rw_lock;\n"); + fprintf(stderr, "Cache_read(): thread %lu: locking rw_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_lock(&cf->rw_lock); /* Wait for the background download thread to finish */ - fprintf(stderr, "Cache_read(): locking bgt_lock;\n"); + fprintf(stderr, "Cache_read(): thread %lu: locking bgt_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_lock(&cf->bgt_lock); - fprintf(stderr, "Cache_read(): unlocking bgt_lock;\n"); + fprintf(stderr, "Cache_read(): thread %lu: unlocking bgt_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); if (Seg_exist(cf, offset)) { /* The segment already exists - it was downloaded by the background * download thread. Send it off and unlock the I/O */ send = Data_read(cf, (uint8_t *) output_buf, len, offset); - fprintf(stderr, "Cache_read(): unlocking rw_lock;\n"); + fprintf(stderr, "Cache_read(): thread %lu: unlocking rw_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); goto bgdl; @@ -918,7 +926,8 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) recv); } free(recv_buf); - fprintf(stderr, "Cache_read(): unlocking rw_lock;\n"); + fprintf(stderr, "Cache_read(): thread %lu: unlocking rw_lock;\n", + pthread_self()); fflush(stderr); pthread_mutex_unlock(&cf->rw_lock); @@ -930,7 +939,8 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) cf->next_offset < cf->content_length ){ /* Stop the spawning of multiple background pthreads */ if(!pthread_mutex_trylock(&cf->bgt_lock)) { - fprintf(stderr, "Cache_read(): trylocked bgt_lock;\n"); + fprintf(stderr, "Cache_read(): thread %lu: trylocked bgt_lock;\n", + pthread_self()); fflush(stderr); if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) { fprintf(stderr, From 91af975ff8701f9338a23bc1372ee1e7f00ea116 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Fri, 26 Jul 2019 00:06:36 +0100 Subject: [PATCH 3/3] changed default segment size, moved the locking sequence of bgt_lock and rw_lock --- src/cache.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/cache.c b/src/cache.c index cb7d95b..27a762f 100644 --- a/src/cache.c +++ b/src/cache.c @@ -13,18 +13,18 @@ /** * \brief Data file block size - * \details We set it to 1024*1024*8 = 8MiB + * \details We set it to 2*1024*1024 = 8MiB */ -#define DEFAULT_DATA_BLK_SZ 8*1024*1024 +#define DEFAULT_DATA_BLK_SZ 2*1024*1024 /** * \brief Maximum segment block count - * \details This is set to 128*1024 blocks, which uses 128KB. By default, - * this allows the user to store (128*1024)*(8*1024*1024) = 1TB of data + * \details This is set to 512*1024 blocks, which uses 128KB. By default, + * this allows the user to store (512*1024)*(2*1024*1024) = 1TB of data */ -#define DEFAULT_MAX_SEGBC 128*1024 +#define DEFAULT_MAX_SEGBC 512*1024 /** * \brief error associated with metadata @@ -872,11 +872,6 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) send = Data_read(cf, (uint8_t *) output_buf, len, offset); goto bgdl; } else { - /* Wait for any other download thread to finish, then lock */ - fprintf(stderr, "Cache_read(): thread %lu: locking rw_lock;\n", - pthread_self()); - fflush(stderr); - pthread_mutex_lock(&cf->rw_lock); /* Wait for the background download thread to finish */ fprintf(stderr, "Cache_read(): thread %lu: locking bgt_lock;\n", pthread_self()); @@ -886,8 +881,13 @@ long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset) pthread_self()); fflush(stderr); pthread_mutex_unlock(&cf->bgt_lock); + /* Wait for any other download thread to finish*/ + fprintf(stderr, "Cache_read(): thread %lu: locking rw_lock;\n", + pthread_self()); + fflush(stderr); + pthread_mutex_lock(&cf->rw_lock); if (Seg_exist(cf, offset)) { - /* The segment already exists - it was downloaded by the background + /* The segment already exists - it was downloaded by other * download thread. Send it off and unlock the I/O */ send = Data_read(cf, (uint8_t *) output_buf, len, offset); fprintf(stderr, "Cache_read(): thread %lu: unlocking rw_lock;\n",