From 666d2535484c841b17f316986fbcaa4d727ac0f8 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Fri, 21 Jul 2017 15:59:00 -0500 Subject: [PATCH 01/19] debug --- cmd/traffic_cache_tool/CacheTool.cc | 8 ++++++++ iocore/cache/CacheDisk.cc | 1 + 2 files changed, 9 insertions(+) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 5986edea9b3..48b6b2a8f92 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -1214,6 +1214,12 @@ Clear_Spans(int argc, char *argv[]) return zret; } +Errata +Find_Stripe(int argc, char* argv[]) +{ + +} + int main(int argc, char *argv[]) { @@ -1248,6 +1254,8 @@ main(int argc, char *argv[]) Commands.add(std::string("volumes"), std::string("Volumes"), &Simulate_Span_Allocation); Commands.add(std::string("alloc"), std::string("Storage allocation")) .subCommand(std::string("free"), std::string("Allocate storage on free (empty) spans"), &Cmd_Allocate_Empty_Spans); + Commands.add(std::string("find"), std::string("Find Stripe Assignment")) + .subCommand(std::string("url"), std::string("URL"),&Find_Stripe); Commands.setArgIndex(optind); diff --git a/iocore/cache/CacheDisk.cc b/iocore/cache/CacheDisk.cc index 45432f089d4..a45a32d504f 100644 --- a/iocore/cache/CacheDisk.cc +++ b/iocore/cache/CacheDisk.cc @@ -218,6 +218,7 @@ CacheDisk::openDone(int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */) int CacheDisk::sync() { + Debug("span %s offset %" PRId64"========>>>>>>>>",path,skip); io.aiocb.aio_offset = skip; io.aiocb.aio_buf = header; io.aiocb.aio_nbytes = header_len; From c9d8e07e4345d9c2511ca7216c83e99199f698d3 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Fri, 21 Jul 2017 16:51:22 -0500 Subject: [PATCH 02/19] print --- cmd/traffic_cache_tool/CacheTool.cc | 1 + iocore/cache/CacheDisk.cc | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 48b6b2a8f92..6e809d6ca9d 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -497,6 +497,7 @@ Errata Cache::allocStripe(Span *span, int vol_idx, CacheStripeBlocks len) { auto rv = span->allocStripe(vol_idx, len); + std::cout<_path<<":"<>>>>>>>",path,skip); + Debug("cache_sync","span %s offset %" PRId64"========>>>>>>>>",path,skip); io.aiocb.aio_offset = skip; io.aiocb.aio_buf = header; io.aiocb.aio_nbytes = header_len; From 46e89316bc525316e14ad31ae562cbc2408c1072 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Mon, 24 Jul 2017 17:19:42 -0500 Subject: [PATCH 03/19] hqsh --- cmd/traffic_cache_tool/CacheTool.cc | 14 ++++++++++++-- cmd/traffic_cache_tool/Makefile.am | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 6e809d6ca9d..57b8b659308 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -37,6 +37,10 @@ #include "File.h" #include "CacheDefs.h" #include "Command.h" +#include "ts/ink_code.h" +#include "ts/INK_MD5.h" +#include +#include using ts::Bytes; using ts::Megabytes; @@ -1218,7 +1222,13 @@ Clear_Spans(int argc, char *argv[]) Errata Find_Stripe(int argc, char* argv[]) { - + Errata zret; + INK_MD5 hash; + char hashStr[33]; + char* host="http://s.yimg.com"; + ink_code_md5((unsigned char *)host,strlen(host),(unsigned char*)&hash); + printf("%d hash of %s is %s\n",argc,host,ink_code_to_hex_str(hashStr,(unsigned char*)&hash)); + return zret; } int @@ -1256,7 +1266,7 @@ main(int argc, char *argv[]) Commands.add(std::string("alloc"), std::string("Storage allocation")) .subCommand(std::string("free"), std::string("Allocate storage on free (empty) spans"), &Cmd_Allocate_Empty_Spans); Commands.add(std::string("find"), std::string("Find Stripe Assignment")) - .subCommand(std::string("url"), std::string("URL"),&Find_Stripe); + .subCommand(std::string("url"), std::string("URL"),[](int argc, char* argv[]){ return Find_Stripe(argc,argv); }); Commands.setArgIndex(optind); diff --git a/cmd/traffic_cache_tool/Makefile.am b/cmd/traffic_cache_tool/Makefile.am index de07a5cd84d..fdb20b489b1 100644 --- a/cmd/traffic_cache_tool/Makefile.am +++ b/cmd/traffic_cache_tool/Makefile.am @@ -29,6 +29,8 @@ traffic_cache_tool_LDADD = \ $(top_builddir)/lib/ts/.libs/ink_error.o \ $(top_builddir)/lib/ts/.libs/ink_string.o \ $(top_builddir)/lib/ts/.libs/ink_assert.o \ - $(top_builddir)/lib/tsconfig/.libs/Errata.o + $(top_builddir)/lib/ts/.libs/ink_code.o \ + $(top_builddir)/lib/tsconfig/.libs/Errata.o \ + @OPENSSL_LIBS@ all-am: Makefile $(PROGRAMS) From 2c31795bb81ddbd54b68364b442726f3da36c414 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Tue, 25 Jul 2017 17:05:33 -0500 Subject: [PATCH 04/19] half done build_hash_table --- cmd/traffic_cache_tool/CacheTool.cc | 108 +++++++++++++++++++++++++++- iocore/cache/Cache.cc | 2 + 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 57b8b659308..ee12d5b3598 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -52,7 +52,8 @@ using ts::Errata; using ts::FilePath; using ts::MemView; using ts::CacheDirEntry; - +#define VOL_HASH_TABLE_SIZE 32707 +#define VOL_HASH_ALLOC_SIZE (8 * 1024 * 1024) // one chance per this unit const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL; @@ -489,7 +490,7 @@ struct Cache { enum class SpanDumpDepth { SPAN, STRIPE, DIRECTORY }; void dumpSpans(SpanDumpDepth depth); void dumpVolumes(); - + void build_vol_hash_table(); // ts::CacheStripeBlocks calcTotalSpanPhysicalSize(); ts::CacheStripeBlocks calcTotalSpanConfiguredSize(); @@ -830,6 +831,109 @@ Cache::dumpSpans(SpanDumpDepth depth) } } +void +Cache::build_vol_hash_table() +{ + int num_vols = this->_volumes.size(); + +//TODO: Error handling; for now I am assuming all disks are good +/* + uint64_t total = 0; + int bad_vols = 0; + int map = 0; + uint64_t used = 0; + // initialize number of elements per vol + for (int i = 0; i < num_vols; i++) { + if (DISK_BAD(cp->vols[i]->disk)) { + bad_vols++; + continue; + } + mapping[map] = i; + p[map++] = cp->vols[i]; + total += (cp->vols[i]->len >> STORE_BLOCK_SHIFT); + } + + num_vols -= bad_vols; + + if (!num_vols || !total) { + // all the disks are corrupt, + if (cp->vol_hash_table) { + new_Freer(cp->vol_hash_table, CACHE_MEM_FREE_TIMEOUT); + } + cp->vol_hash_table = nullptr; + ats_free(mapping); + ats_free(p); + return; + } +*/ + unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); + unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); + unsigned int *rnd = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); + unsigned short *ttable = (unsigned short *)ats_malloc(sizeof(unsigned short) * VOL_HASH_TABLE_SIZE); + unsigned short *old_table; + unsigned int *rtable_entries = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); + unsigned int rtable_size = 0; + int i=0; + // estimate allocation + for(auto &elt:_volumes) + { + rtable_entries[i] = elt.second._size / VOL_HASH_ALLOC_SIZE; + rtable_size += rtable_entries[i]; + i++; + } + /* + // seed random number generator + for (int i = 0; i < num_vols; i++) { + uint64_t x = p[i]->hash_id.fold(); + rnd[i] = (unsigned int)x; + } + // initialize table to "empty" + for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { + ttable[i] = VOL_HASH_EMPTY; + } + // generate random numbers proportaion to allocation + rtable_pair *rtable = (rtable_pair *)ats_malloc(sizeof(rtable_pair) * rtable_size); + int rindex = 0; + for (int i = 0; i < num_vols; i++) { + for (int j = 0; j < (int)rtable_entries[i]; j++) { + rtable[rindex].rval = next_rand(&rnd[i]); + rtable[rindex].idx = i; + rindex++; + } + } + ink_assert(rindex == (int)rtable_size); + // sort (rand #, vol $ pairs) + qsort(rtable, rtable_size, sizeof(rtable_pair), cmprtable); + + unsigned int width = (1LL << 32) / VOL_HASH_TABLE_SIZE; + unsigned int pos; // target position to allocate + // select vol with closest random number for each bucket + int i = 0; // index moving through the random numbers + for (int j = 0; j < VOL_HASH_TABLE_SIZE; j++) { + pos = width / 2 + j * width; // position to select closest to + while (pos > rtable[i].rval && i < (int)rtable_size - 1) { + i++; + } + ttable[j] = rtable[i].idx; + Debug("cache_init","hash table %d====================>>>>>>>>>>>>>>>>>>..",ttable[j]); + gotvol[rtable[i].idx]++; + } + for (int i = 0; i < num_vols; i++) { + Debug("cache_init", "build_vol_hash_table index %d mapped to %d requested %d got %d", i, i, forvol[i], gotvol[i]); + } + // install new table + if (nullptr != (old_table = ink_atomic_swap(&(cp->vol_hash_table), ttable))) { + new_Freer(old_table, CACHE_MEM_FREE_TIMEOUT); + } + ats_free(p); + ats_free(forvol); + ats_free(gotvol); + ats_free(rnd); + ats_free(rtable_entries); + ats_free(rtable); + */ +} + void Cache::dumpVolumes() { diff --git a/iocore/cache/Cache.cc b/iocore/cache/Cache.cc index daa6ea9f05d..980d0aac64d 100644 --- a/iocore/cache/Cache.cc +++ b/iocore/cache/Cache.cc @@ -1275,6 +1275,7 @@ Vol::init(char *s, off_t blocks, off_t dir_skip, bool clear) ink_strlcpy(hash_text, seed_str, hash_text_size); snprintf(hash_text + hash_seed_size, (hash_text_size - hash_seed_size), " %" PRIu64 ":%" PRIu64 "", (uint64_t)dir_skip, (uint64_t)blocks); + Debug("cache_init","**** hash string %s*****",hash_text.get()); MD5Context().hash_immediate(hash_id, hash_text, strlen(hash_text)); dir_skip = ROUND_TO_STORE_BLOCK((dir_skip < START_POS ? START_POS : dir_skip)); @@ -1912,6 +1913,7 @@ build_vol_hash_table(CacheHostRecord *cp) i++; } ttable[j] = mapping[rtable[i].idx]; + Debug("cache_init","hash table %d====================>>>>>>>>>>>>>>>>>>..",ttable[j]); gotvol[rtable[i].idx]++; } for (int i = 0; i < num_vols; i++) { From d5d928a680ff5bf7f3a19e125239f7e7ba5a005e Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Wed, 26 Jul 2017 15:59:07 -0500 Subject: [PATCH 05/19] build_stripes_hashtable --- cmd/traffic_cache_tool/CacheTool.cc | 301 ++++++++++++++++++---------- 1 file changed, 197 insertions(+), 104 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index ee12d5b3598..bf501866359 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -54,6 +54,8 @@ using ts::MemView; using ts::CacheDirEntry; #define VOL_HASH_TABLE_SIZE 32707 #define VOL_HASH_ALLOC_SIZE (8 * 1024 * 1024) // one chance per this unit +#define VOL_HASH_EMPTY 0xFFFF + const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL; @@ -84,6 +86,8 @@ struct Span { /// This is broken and needs to be cleaned up. void clearPermanently(); + void build_stripe_hash_table(); + ts::Rv allocStripe(int vol_idx, CacheStripeBlocks len); Errata updateHeader(); ///< Update serialized header and write to disk. @@ -101,6 +105,7 @@ struct Span { /// Live information about stripes. /// Seeded from @a _header and potentially agumented with direct probing. std::list _stripes; + unsigned short* stripes_hash_table; }; /* --------------------------------------------------------------------------------------- */ struct Stripe { @@ -150,6 +155,7 @@ struct Stripe { void updateLiveData(enum Copy c); Span *_span; ///< Hosting span. + INK_MD5 hash_id; ///hash_id Bytes _start; ///< Offset of first byte of stripe. Bytes _content; ///< Start of content. CacheStoreBlocks _len; ///< Length of stripe. @@ -187,6 +193,13 @@ Stripe::Chunk::clear() Stripe::Stripe(Span *span, Bytes start, CacheStoreBlocks len) : _span(span), _start(start), _len(len) { + const char* diskPath = span->_path.path(); + const size_t hash_seed_size = strlen(diskPath); + const size_t hash_text_size = hash_seed_size + 32; + char* hash_text = static_cast(ats_malloc(hash_text_size)); + snprintf(hash_text + hash_seed_size, (hash_text_size - hash_seed_size), " %" PRIu64 ":%" PRIu64 "", (uint64_t)_start, + (uint64_t)_len); + ink_code_md5((unsigned char *)hash_text,strlen(hash_text),(unsigned char*)&hash_id); } bool @@ -490,7 +503,6 @@ struct Cache { enum class SpanDumpDepth { SPAN, STRIPE, DIRECTORY }; void dumpSpans(SpanDumpDepth depth); void dumpVolumes(); - void build_vol_hash_table(); // ts::CacheStripeBlocks calcTotalSpanPhysicalSize(); ts::CacheStripeBlocks calcTotalSpanConfiguredSize(); @@ -831,109 +843,6 @@ Cache::dumpSpans(SpanDumpDepth depth) } } -void -Cache::build_vol_hash_table() -{ - int num_vols = this->_volumes.size(); - -//TODO: Error handling; for now I am assuming all disks are good -/* - uint64_t total = 0; - int bad_vols = 0; - int map = 0; - uint64_t used = 0; - // initialize number of elements per vol - for (int i = 0; i < num_vols; i++) { - if (DISK_BAD(cp->vols[i]->disk)) { - bad_vols++; - continue; - } - mapping[map] = i; - p[map++] = cp->vols[i]; - total += (cp->vols[i]->len >> STORE_BLOCK_SHIFT); - } - - num_vols -= bad_vols; - - if (!num_vols || !total) { - // all the disks are corrupt, - if (cp->vol_hash_table) { - new_Freer(cp->vol_hash_table, CACHE_MEM_FREE_TIMEOUT); - } - cp->vol_hash_table = nullptr; - ats_free(mapping); - ats_free(p); - return; - } -*/ - unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); - unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); - unsigned int *rnd = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); - unsigned short *ttable = (unsigned short *)ats_malloc(sizeof(unsigned short) * VOL_HASH_TABLE_SIZE); - unsigned short *old_table; - unsigned int *rtable_entries = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_vols); - unsigned int rtable_size = 0; - int i=0; - // estimate allocation - for(auto &elt:_volumes) - { - rtable_entries[i] = elt.second._size / VOL_HASH_ALLOC_SIZE; - rtable_size += rtable_entries[i]; - i++; - } - /* - // seed random number generator - for (int i = 0; i < num_vols; i++) { - uint64_t x = p[i]->hash_id.fold(); - rnd[i] = (unsigned int)x; - } - // initialize table to "empty" - for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { - ttable[i] = VOL_HASH_EMPTY; - } - // generate random numbers proportaion to allocation - rtable_pair *rtable = (rtable_pair *)ats_malloc(sizeof(rtable_pair) * rtable_size); - int rindex = 0; - for (int i = 0; i < num_vols; i++) { - for (int j = 0; j < (int)rtable_entries[i]; j++) { - rtable[rindex].rval = next_rand(&rnd[i]); - rtable[rindex].idx = i; - rindex++; - } - } - ink_assert(rindex == (int)rtable_size); - // sort (rand #, vol $ pairs) - qsort(rtable, rtable_size, sizeof(rtable_pair), cmprtable); - - unsigned int width = (1LL << 32) / VOL_HASH_TABLE_SIZE; - unsigned int pos; // target position to allocate - // select vol with closest random number for each bucket - int i = 0; // index moving through the random numbers - for (int j = 0; j < VOL_HASH_TABLE_SIZE; j++) { - pos = width / 2 + j * width; // position to select closest to - while (pos > rtable[i].rval && i < (int)rtable_size - 1) { - i++; - } - ttable[j] = rtable[i].idx; - Debug("cache_init","hash table %d====================>>>>>>>>>>>>>>>>>>..",ttable[j]); - gotvol[rtable[i].idx]++; - } - for (int i = 0; i < num_vols; i++) { - Debug("cache_init", "build_vol_hash_table index %d mapped to %d requested %d got %d", i, i, forvol[i], gotvol[i]); - } - // install new table - if (nullptr != (old_table = ink_atomic_swap(&(cp->vol_hash_table), ttable))) { - new_Freer(old_table, CACHE_MEM_FREE_TIMEOUT); - } - ats_free(p); - ats_free(forvol); - ats_free(gotvol); - ats_free(rnd); - ats_free(rtable_entries); - ats_free(rtable); - */ -} - void Cache::dumpVolumes() { @@ -1173,6 +1082,184 @@ Span::clearPermanently() std::cout << "Clearing " << _path << " not performed, write not enabled" << std::endl; } } + +// explicit pair for random table in build_vol_hash_table +struct rtable_pair { + unsigned int rval; ///< relative value, used to sort. + unsigned int idx; ///< volume mapping table index. +}; + +// comparison operator for random table in build_vol_hash_table +// sorts based on the randomly assigned rval +static int +cmprtable(const void *aa, const void *bb) +{ + rtable_pair *a = (rtable_pair *)aa; + rtable_pair *b = (rtable_pair *)bb; + if (a->rval < b->rval) { + return -1; + } + if (a->rval > b->rval) { + return 1; + } + return 0; +} + +unsigned int +next_rand(unsigned int *p) +{ + unsigned int seed = *p; + seed = 1103515145 * seed + 12345; + *p = seed; + return seed; +} + +void +Span::build_stripe_hash_table() +{ + int num_stripes = this->_stripes.size(); + +//TODO: Error handling; for now I am assuming all disks are good +/* + uint64_t total = 0; + int bad_vols = 0; + int map = 0; + uint64_t used = 0; + // initialize number of elements per vol + for (int i = 0; i < num_vols; i++) { + if (DISK_BAD(cp->vols[i]->disk)) { + bad_vols++; + continue; + } + mapping[map] = i; + p[map++] = cp->vols[i]; + total += (cp->vols[i]->len >> STORE_BLOCK_SHIFT); + } + + num_vols -= bad_vols; + + if (!num_vols || !total) { + // all the disks are corrupt, + if (cp->vol_hash_table) { + new_Freer(cp->vol_hash_table, CACHE_MEM_FREE_TIMEOUT); + } + cp->vol_hash_table = nullptr; + ats_free(mapping); + ats_free(p); + return; + } +*/ + unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned int *rnd = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned short *ttable = (unsigned short *)ats_malloc(sizeof(unsigned short) * VOL_HASH_TABLE_SIZE); + unsigned short *old_table; + unsigned int *rtable_entries = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned int rtable_size = 0; + int i=0; + // estimate allocation + for(auto &elt:_stripes) + { + rtable_entries[i] = elt->_len / VOL_HASH_ALLOC_SIZE; + rtable_size += rtable_entries[i]; + uint64_t x = elt->hash_id.fold(); + // seed random number generator + rnd[i] = (unsigned int)x; + i++; + } + // initialize table to "empty" + for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { + ttable[i] = VOL_HASH_EMPTY; + } + + // generate random numbers proportaion to allocation + rtable_pair *rtable = (rtable_pair *)ats_malloc(sizeof(rtable_pair) * rtable_size); + int rindex = 0; + for (int i = 0; i < num_stripes; i++) { + for (int j = 0; j < (int)rtable_entries[i]; j++) { + rtable[rindex].rval = next_rand(&rnd[i]); + rtable[rindex].idx = i; + rindex++; + } + } + // ink_assert(rindex == (int)rtable_size); + // sort (rand #, vol $ pairs) + qsort(rtable, rtable_size, sizeof(rtable_pair), cmprtable); + unsigned int width = (1LL << 32) / VOL_HASH_TABLE_SIZE; + unsigned int pos; // target position to allocate + // select vol with closest random number for each bucket + i = 0; // index moving through the random numbers + for (int j = 0; j < VOL_HASH_TABLE_SIZE; j++) { + pos = width / 2 + j * width; // position to select closest to + while (pos > rtable[i].rval && i < (int)rtable_size - 1) { + i++; + } + ttable[j] = rtable[i].idx; + gotvol[rtable[i].idx]++; + } + for (int i = 0; i < num_stripes; i++) { + printf("build_vol_hash_table index %d mapped to %d requested %d got %d", i, i, forvol[i], gotvol[i]); + } + stripes_hash_table = ttable; + + ats_free(forvol); + ats_free(gotvol); + ats_free(rnd); + ats_free(rtable_entries); + ats_free(rtable); + /* + // seed random number generator + for (int i = 0; i < num_vols; i++) { + uint64_t x = p[i]->hash_id.fold(); + rnd[i] = (unsigned int)x; + } + // initialize table to "empty" + for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { + ttable[i] = VOL_HASH_EMPTY; + } + // generate random numbers proportaion to allocation + rtable_pair *rtable = (rtable_pair *)ats_malloc(sizeof(rtable_pair) * rtable_size); + int rindex = 0; + for (int i = 0; i < num_vols; i++) { + for (int j = 0; j < (int)rtable_entries[i]; j++) { + rtable[rindex].rval = next_rand(&rnd[i]); + rtable[rindex].idx = i; + rindex++; + } + } + ink_assert(rindex == (int)rtable_size); + // sort (rand #, vol $ pairs) + qsort(rtable, rtable_size, sizeof(rtable_pair), cmprtable); + + unsigned int width = (1LL << 32) / VOL_HASH_TABLE_SIZE; + unsigned int pos; // target position to allocate + // select vol with closest random number for each bucket + int i = 0; // index moving through the random numbers + for (int j = 0; j < VOL_HASH_TABLE_SIZE; j++) { + pos = width / 2 + j * width; // position to select closest to + while (pos > rtable[i].rval && i < (int)rtable_size - 1) { + i++; + } + ttable[j] = rtable[i].idx; + Debug("cache_init","hash table %d====================>>>>>>>>>>>>>>>>>>..",ttable[j]); + gotvol[rtable[i].idx]++; + } + for (int i = 0; i < num_vols; i++) { + Debug("cache_init", "build_vol_hash_table index %d mapped to %d requested %d got %d", i, i, forvol[i], gotvol[i]); + } + // install new table + if (nullptr != (old_table = ink_atomic_swap(&(cp->vol_hash_table), ttable))) { + new_Freer(old_table, CACHE_MEM_FREE_TIMEOUT); + } + ats_free(p); + ats_free(forvol); + ats_free(gotvol); + ats_free(rnd); + ats_free(rtable_entries); + ats_free(rtable); + */ +} + /* --------------------------------------------------------------------------------------- */ Errata VolumeConfig::load(FilePath const &path) @@ -1327,6 +1414,12 @@ Errata Find_Stripe(int argc, char* argv[]) { Errata zret; + Cache cache; + + if ((zret = cache.loadSpan(SpanFile))) + { + printf("yohoo"); + } INK_MD5 hash; char hashStr[33]; char* host="http://s.yimg.com"; From 4a8d68d942e83304398ff48d111c8253869181d7 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Wed, 26 Jul 2017 16:14:46 -0500 Subject: [PATCH 06/19] call build_stripe_hash_table --- cmd/traffic_cache_tool/CacheTool.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index bf501866359..2a0e16d34d6 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -757,6 +757,7 @@ Cache::loadSpanDirect(FilePath const &path, int vol_idx, Bytes size) } else { span->clear(); } + span->build_stripe_hash_table(); _spans.push_back(span.release()); } return zret; From 4239119b2e46e9bce2effbb6f8931c6abc386f75 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Wed, 26 Jul 2017 16:49:30 -0500 Subject: [PATCH 07/19] fix --- cmd/traffic_cache_tool/CacheTool.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 2a0e16d34d6..9c9238d96c2 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -55,6 +55,7 @@ using ts::CacheDirEntry; #define VOL_HASH_TABLE_SIZE 32707 #define VOL_HASH_ALLOC_SIZE (8 * 1024 * 1024) // one chance per this unit #define VOL_HASH_EMPTY 0xFFFF +#define STORE_BLOCK_SHIFT 13 const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; @@ -1119,7 +1120,7 @@ void Span::build_stripe_hash_table() { int num_stripes = this->_stripes.size(); - + int total = 0; //TODO: Error handling; for now I am assuming all disks are good /* uint64_t total = 0; @@ -1166,8 +1167,14 @@ Span::build_stripe_hash_table() uint64_t x = elt->hash_id.fold(); // seed random number generator rnd[i] = (unsigned int)x; + total += (elt->_len >> STORE_BLOCK_SHIFT); i++; } + + for(auto &elt:_stripes) + { + forvol[i] = (VOL_HASH_TABLE_SIZE * (elt->_len >> STORE_BLOCK_SHIFT)) / total; + } // initialize table to "empty" for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { ttable[i] = VOL_HASH_EMPTY; From 59fe9e450fa754ae64774076b8c9d7f3235fa235 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 27 Jul 2017 10:39:52 -0500 Subject: [PATCH 08/19] global stripe table --- cmd/traffic_cache_tool/CacheTool.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 9c9238d96c2..a1ecba3b901 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -72,7 +72,8 @@ ts::CommandTable Commands; int OPEN_RW_FLAG = O_RDONLY; struct Stripe; - +std::list globalList_stripe; +unsigned short* stripes_hash_table; struct Span { Span(FilePath const &path) : _path(path) {} Errata load(); @@ -87,7 +88,6 @@ struct Span { /// This is broken and needs to be cleaned up. void clearPermanently(); - void build_stripe_hash_table(); ts::Rv allocStripe(int vol_idx, CacheStripeBlocks len); Errata updateHeader(); ///< Update serialized header and write to disk. @@ -106,7 +106,7 @@ struct Span { /// Live information about stripes. /// Seeded from @a _header and potentially agumented with direct probing. std::list _stripes; - unsigned short* stripes_hash_table; + }; /* --------------------------------------------------------------------------------------- */ struct Stripe { @@ -753,12 +753,12 @@ Cache::loadSpanDirect(FilePath const &path, int vol_idx, Bytes size) span->_free_space += stripe->_len; } span->_stripes.push_back(stripe); + globalList_stripe.push_back(stripe); } span->_vol_idx = vol_idx; } else { span->clear(); } - span->build_stripe_hash_table(); _spans.push_back(span.release()); } return zret; @@ -1117,9 +1117,9 @@ next_rand(unsigned int *p) } void -Span::build_stripe_hash_table() +build_stripe_hash_table() { - int num_stripes = this->_stripes.size(); + int num_stripes = globalList_stripe.size(); int total = 0; //TODO: Error handling; for now I am assuming all disks are good /* @@ -1160,7 +1160,7 @@ Span::build_stripe_hash_table() unsigned int rtable_size = 0; int i=0; // estimate allocation - for(auto &elt:_stripes) + for(auto &elt:globalList_stripe) { rtable_entries[i] = elt->_len / VOL_HASH_ALLOC_SIZE; rtable_size += rtable_entries[i]; @@ -1171,7 +1171,7 @@ Span::build_stripe_hash_table() i++; } - for(auto &elt:_stripes) + for(auto &elt:globalList_stripe) { forvol[i] = (VOL_HASH_TABLE_SIZE * (elt->_len >> STORE_BLOCK_SHIFT)) / total; } @@ -1426,6 +1426,8 @@ Find_Stripe(int argc, char* argv[]) if ((zret = cache.loadSpan(SpanFile))) { + + build_stripe_hash_table(); printf("yohoo"); } INK_MD5 hash; From 4fc2d65c4191afc78d0c218d572822aed211963f Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 27 Jul 2017 10:44:22 -0500 Subject: [PATCH 09/19] debug --- cmd/traffic_cache_tool/CacheTool.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index a1ecba3b901..2207a5ec16b 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -1162,6 +1162,7 @@ build_stripe_hash_table() // estimate allocation for(auto &elt:globalList_stripe) { + printf("stripe length %d\n",elt->_len); rtable_entries[i] = elt->_len / VOL_HASH_ALLOC_SIZE; rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); @@ -1206,7 +1207,7 @@ build_stripe_hash_table() gotvol[rtable[i].idx]++; } for (int i = 0; i < num_stripes; i++) { - printf("build_vol_hash_table index %d mapped to %d requested %d got %d", i, i, forvol[i], gotvol[i]); + printf("build_vol_hash_table index %d mapped to %d requested %d got %d\n", i, i, forvol[i], gotvol[i]); } stripes_hash_table = ttable; From 40f9f0c375226423b544f7e371988e36a62b444e Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 27 Jul 2017 10:49:51 -0500 Subject: [PATCH 10/19] fix --- iocore/cache/Cache.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iocore/cache/Cache.cc b/iocore/cache/Cache.cc index 980d0aac64d..bbd85cb764e 100644 --- a/iocore/cache/Cache.cc +++ b/iocore/cache/Cache.cc @@ -1870,6 +1870,7 @@ build_vol_hash_table(CacheHostRecord *cp) // estimate allocation for (int i = 0; i < num_vols; i++) { + printf("stripe len %d\n",p[i]->len); forvol[i] = (VOL_HASH_TABLE_SIZE * (p[i]->len >> STORE_BLOCK_SHIFT)) / total; used += forvol[i]; rtable_entries[i] = p[i]->len / VOL_HASH_ALLOC_SIZE; @@ -1913,7 +1914,6 @@ build_vol_hash_table(CacheHostRecord *cp) i++; } ttable[j] = mapping[rtable[i].idx]; - Debug("cache_init","hash table %d====================>>>>>>>>>>>>>>>>>>..",ttable[j]); gotvol[rtable[i].idx]++; } for (int i = 0; i < num_vols; i++) { From 097e9326ce801bfb76770cd8447e8b83d6eab177 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 27 Jul 2017 11:08:30 -0500 Subject: [PATCH 11/19] p --- iocore/cache/Cache.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iocore/cache/Cache.cc b/iocore/cache/Cache.cc index bbd85cb764e..a1ee9a26e3d 100644 --- a/iocore/cache/Cache.cc +++ b/iocore/cache/Cache.cc @@ -1870,7 +1870,7 @@ build_vol_hash_table(CacheHostRecord *cp) // estimate allocation for (int i = 0; i < num_vols; i++) { - printf("stripe len %d\n",p[i]->len); + printf("stripe len %"PRId64"\n",p[i]->len); forvol[i] = (VOL_HASH_TABLE_SIZE * (p[i]->len >> STORE_BLOCK_SHIFT)) / total; used += forvol[i]; rtable_entries[i] = p[i]->len / VOL_HASH_ALLOC_SIZE; From 6047084d667b379e476cfada41f6d7153c72ea77 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 27 Jul 2017 13:48:56 -0500 Subject: [PATCH 12/19] meh --- cmd/traffic_cache_tool/CacheTool.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 2207a5ec16b..ebc839cb231 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -56,7 +56,7 @@ using ts::CacheDirEntry; #define VOL_HASH_ALLOC_SIZE (8 * 1024 * 1024) // one chance per this unit #define VOL_HASH_EMPTY 0xFFFF #define STORE_BLOCK_SHIFT 13 - +#define STORE_BLOCK_SIZE 8192 const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL; @@ -1162,7 +1162,7 @@ build_stripe_hash_table() // estimate allocation for(auto &elt:globalList_stripe) { - printf("stripe length %d\n",elt->_len); + printf("stripe length %"PRId64"\n",elt->_len*STORE_BLOCK_SIZE); rtable_entries[i] = elt->_len / VOL_HASH_ALLOC_SIZE; rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); From 209def211dd838f4efae8f89b453bb56258b022b Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 27 Jul 2017 13:55:52 -0500 Subject: [PATCH 13/19] fix --- cmd/traffic_cache_tool/CacheTool.cc | 246 +++++++++++++--------------- iocore/cache/Cache.cc | 4 +- iocore/cache/CacheDisk.cc | 2 +- 3 files changed, 115 insertions(+), 137 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index ebc839cb231..de5b3a6ecd8 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -41,6 +41,8 @@ #include "ts/INK_MD5.h" #include #include +#include +#include using ts::Bytes; using ts::Megabytes; @@ -57,6 +59,7 @@ using ts::CacheDirEntry; #define VOL_HASH_EMPTY 0xFFFF #define STORE_BLOCK_SHIFT 13 #define STORE_BLOCK_SIZE 8192 +#define DIR_TAG_WIDTH 12 const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL; @@ -72,8 +75,9 @@ ts::CommandTable Commands; int OPEN_RW_FLAG = O_RDONLY; struct Stripe; -std::list globalList_stripe; -unsigned short* stripes_hash_table; +std::vector globalVec_stripe; +std::unordered_set URLset; +unsigned short *stripes_hash_table; struct Span { Span(FilePath const &path) : _path(path) {} Errata load(); @@ -88,7 +92,6 @@ struct Span { /// This is broken and needs to be cleaned up. void clearPermanently(); - ts::Rv allocStripe(int vol_idx, CacheStripeBlocks len); Errata updateHeader(); ///< Update serialized header and write to disk. @@ -106,7 +109,6 @@ struct Span { /// Live information about stripes. /// Seeded from @a _header and potentially agumented with direct probing. std::list _stripes; - }; /* --------------------------------------------------------------------------------------- */ struct Stripe { @@ -156,7 +158,7 @@ struct Stripe { void updateLiveData(enum Copy c); Span *_span; ///< Hosting span. - INK_MD5 hash_id; ///hash_id + INK_MD5 hash_id; /// hash_id Bytes _start; ///< Offset of first byte of stripe. Bytes _content; ///< Start of content. CacheStoreBlocks _len; ///< Length of stripe. @@ -166,6 +168,7 @@ struct Stripe { int64_t _buckets; ///< Number of buckets per segment. int64_t _segments; ///< Number of segments. + const char *hashText = nullptr; /// Meta copies, indexed by A/B then HEAD/FOOT. StripeMeta _meta[2][2]; @@ -194,13 +197,16 @@ Stripe::Chunk::clear() Stripe::Stripe(Span *span, Bytes start, CacheStoreBlocks len) : _span(span), _start(start), _len(len) { - const char* diskPath = span->_path.path(); - const size_t hash_seed_size = strlen(diskPath); - const size_t hash_text_size = hash_seed_size + 32; - char* hash_text = static_cast(ats_malloc(hash_text_size)); - snprintf(hash_text + hash_seed_size, (hash_text_size - hash_seed_size), " %" PRIu64 ":%" PRIu64 "", (uint64_t)_start, - (uint64_t)_len); - ink_code_md5((unsigned char *)hash_text,strlen(hash_text),(unsigned char*)&hash_id); + const char *diskPath = span->_path.path(); + const size_t hash_seed_size = strlen(diskPath); + const size_t hash_text_size = hash_seed_size + 32; + char *hash_text = static_cast(ats_malloc(hash_text_size)); + strncpy(hash_text, diskPath, hash_text_size); + snprintf(hash_text + hash_seed_size, (hash_text_size - hash_seed_size), " %" PRIu64 ":%" PRIu64 "", (uint64_t)_start, + (uint64_t)_len.count() * STORE_BLOCK_SIZE); + printf("hash id of stripe is hash of %s\n", hash_text); + ink_code_md5((unsigned char *)hash_text, strlen(hash_text), (unsigned char *)&hash_id); + hashText = hash_text; } bool @@ -493,6 +499,7 @@ struct Cache { Errata loadSpan(FilePath const &path); Errata loadSpanConfig(FilePath const &path); Errata loadSpanDirect(FilePath const &path, int vol_idx = -1, Bytes size = Bytes(-1)); + Errata loadURLs(FilePath const &path); Errata allocStripe(Span *span, int vol_idx, CacheStripeBlocks len); @@ -515,7 +522,7 @@ Errata Cache::allocStripe(Span *span, int vol_idx, CacheStripeBlocks len) { auto rv = span->allocStripe(vol_idx, len); - std::cout<_path<<":"<_path << ":" << vol_idx << std::endl; if (rv.isOK()) { _volumes[vol_idx]._stripes.push_back(rv); } @@ -565,8 +572,8 @@ class VolumeAllocator typedef std::vector AV; AV _av; ///< Working vector of volume data. - Cache _cache; ///< Current state. - VolumeConfig _vols; ///< Configuration state. + Cache _cache; ///< Current state. + VolumeConfig _vols; ///< Configuration state. public: VolumeAllocator(); @@ -753,7 +760,7 @@ Cache::loadSpanDirect(FilePath const &path, int vol_idx, Bytes size) span->_free_space += stripe->_len; } span->_stripes.push_back(stripe); - globalList_stripe.push_back(stripe); + globalVec_stripe.push_back(stripe); } span->_vol_idx = vol_idx; } else { @@ -808,6 +815,34 @@ Cache::loadSpanConfig(FilePath const &path) return zret; } +Errata +Cache::loadURLs(FilePath const &path) +{ + static const ts::StringView TAG_VOL("url"); + + Errata zret; + + ts::BulkFile cfile(path); + if (0 == cfile.load()) { + ts::StringView content = cfile.content(); + + while (content) { + ts::StringView blob = content.splitPrefix('\n'); + + ts::StringView tag(blob.splitPrefix('=')); + if (!tag) { + } else if (0 == strcasecmp(tag, TAG_VOL)) { + std::string url; + url.assign(blob.begin(), blob.size()); + URLset.insert(url); + } + } + } else { + zret = Errata::Message(0, EBADF, "Unable to load ", path); + } + return zret; +} + void Cache::dumpSpans(SpanDumpDepth depth) { @@ -1119,63 +1154,41 @@ next_rand(unsigned int *p) void build_stripe_hash_table() { - int num_stripes = globalList_stripe.size(); - int total = 0; -//TODO: Error handling; for now I am assuming all disks are good -/* - uint64_t total = 0; - int bad_vols = 0; - int map = 0; - uint64_t used = 0; - // initialize number of elements per vol - for (int i = 0; i < num_vols; i++) { - if (DISK_BAD(cp->vols[i]->disk)) { - bad_vols++; - continue; - } - mapping[map] = i; - p[map++] = cp->vols[i]; - total += (cp->vols[i]->len >> STORE_BLOCK_SHIFT); - } - - num_vols -= bad_vols; - - if (!num_vols || !total) { - // all the disks are corrupt, - if (cp->vol_hash_table) { - new_Freer(cp->vol_hash_table, CACHE_MEM_FREE_TIMEOUT); - } - cp->vol_hash_table = nullptr; - ats_free(mapping); - ats_free(p); - return; - } -*/ - unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); - unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); - unsigned int *rnd = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); - unsigned short *ttable = (unsigned short *)ats_malloc(sizeof(unsigned short) * VOL_HASH_TABLE_SIZE); - unsigned short *old_table; + int num_stripes = globalVec_stripe.size(); + uint64_t total = 0; + unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned int *rnd = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); + unsigned short *ttable = (unsigned short *)ats_malloc(sizeof(unsigned short) * VOL_HASH_TABLE_SIZE); unsigned int *rtable_entries = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); unsigned int rtable_size = 0; - int i=0; + int i = 0; + uint64_t used = 0; // estimate allocation - for(auto &elt:globalList_stripe) - { - printf("stripe length %"PRId64"\n",elt->_len*STORE_BLOCK_SIZE); - rtable_entries[i] = elt->_len / VOL_HASH_ALLOC_SIZE; + for (auto &elt : globalVec_stripe) { + printf("stripe length %" PRId64 "\n", elt->_len.count() * STORE_BLOCK_SIZE); + rtable_entries[i] = elt->_len.count() * STORE_BLOCK_SIZE / VOL_HASH_ALLOC_SIZE; rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); // seed random number generator - rnd[i] = (unsigned int)x; - total += (elt->_len >> STORE_BLOCK_SHIFT); + rnd[i] = (unsigned int)x; + total += ((elt->_len.count() * STORE_BLOCK_SIZE) >> STORE_BLOCK_SHIFT); + i++; + } + i = 0; + for (auto &elt : globalVec_stripe) { + forvol[i] = (VOL_HASH_TABLE_SIZE * ((elt->_len.count() * STORE_BLOCK_SIZE) >> STORE_BLOCK_SHIFT)) / total; + used += forvol[i]; + gotvol[i] = 0; i++; } - for(auto &elt:globalList_stripe) - { - forvol[i] = (VOL_HASH_TABLE_SIZE * (elt->_len >> STORE_BLOCK_SHIFT)) / total; + // spread around the excess + int extra = VOL_HASH_TABLE_SIZE - used; + for (int i = 0; i < extra; i++) { + forvol[i % num_stripes]++; } + // initialize table to "empty" for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { ttable[i] = VOL_HASH_EMPTY; @@ -1191,7 +1204,7 @@ build_stripe_hash_table() rindex++; } } - // ink_assert(rindex == (int)rtable_size); + assert(rindex == (int)rtable_size); // sort (rand #, vol $ pairs) qsort(rtable, rtable_size, sizeof(rtable_pair), cmprtable); unsigned int width = (1LL << 32) / VOL_HASH_TABLE_SIZE; @@ -1216,57 +1229,13 @@ build_stripe_hash_table() ats_free(rnd); ats_free(rtable_entries); ats_free(rtable); - /* - // seed random number generator - for (int i = 0; i < num_vols; i++) { - uint64_t x = p[i]->hash_id.fold(); - rnd[i] = (unsigned int)x; - } - // initialize table to "empty" - for (int i = 0; i < VOL_HASH_TABLE_SIZE; i++) { - ttable[i] = VOL_HASH_EMPTY; - } - // generate random numbers proportaion to allocation - rtable_pair *rtable = (rtable_pair *)ats_malloc(sizeof(rtable_pair) * rtable_size); - int rindex = 0; - for (int i = 0; i < num_vols; i++) { - for (int j = 0; j < (int)rtable_entries[i]; j++) { - rtable[rindex].rval = next_rand(&rnd[i]); - rtable[rindex].idx = i; - rindex++; - } - } - ink_assert(rindex == (int)rtable_size); - // sort (rand #, vol $ pairs) - qsort(rtable, rtable_size, sizeof(rtable_pair), cmprtable); +} - unsigned int width = (1LL << 32) / VOL_HASH_TABLE_SIZE; - unsigned int pos; // target position to allocate - // select vol with closest random number for each bucket - int i = 0; // index moving through the random numbers - for (int j = 0; j < VOL_HASH_TABLE_SIZE; j++) { - pos = width / 2 + j * width; // position to select closest to - while (pos > rtable[i].rval && i < (int)rtable_size - 1) { - i++; - } - ttable[j] = rtable[i].idx; - Debug("cache_init","hash table %d====================>>>>>>>>>>>>>>>>>>..",ttable[j]); - gotvol[rtable[i].idx]++; - } - for (int i = 0; i < num_vols; i++) { - Debug("cache_init", "build_vol_hash_table index %d mapped to %d requested %d got %d", i, i, forvol[i], gotvol[i]); - } - // install new table - if (nullptr != (old_table = ink_atomic_swap(&(cp->vol_hash_table), ttable))) { - new_Freer(old_table, CACHE_MEM_FREE_TIMEOUT); - } - ats_free(p); - ats_free(forvol); - ats_free(gotvol); - ats_free(rnd); - ats_free(rtable_entries); - ats_free(rtable); - */ +Stripe * +key_to_stripe(INK_MD5 *key, const char *hostname, int host_len) +{ + uint32_t h = (key->slice32(2) >> DIR_TAG_WIDTH) % VOL_HASH_TABLE_SIZE; + return globalVec_stripe[stripes_hash_table[h]]; } /* --------------------------------------------------------------------------------------- */ @@ -1348,11 +1317,8 @@ VolumeConfig::load(FilePath const &path) return zret; } /* --------------------------------------------------------------------------------------- */ -struct option Options[] = {{"help", 0, nullptr, 'h'}, - {"spans", 1, nullptr, 's'}, - {"volumes", 1, nullptr, 'v'}, - {"write", 0, nullptr, 'w'}, - {nullptr, 0, nullptr, 0}}; +struct option Options[] = {{"help", 0, nullptr, 'h'}, {"spans", 1, nullptr, 's'}, {"volumes", 1, nullptr, 'v'}, + {"write", 0, nullptr, 'w'}, {"input", 1, nullptr, 'i'}, {nullptr, 0, nullptr, 0}}; } Errata @@ -1418,25 +1384,33 @@ Clear_Spans(int argc, char *argv[]) return zret; } +void +findAssignment(char *url) +{ +} Errata -Find_Stripe(int argc, char* argv[]) +Find_Stripe(FilePath const &input_file_path) { - Errata zret; - Cache cache; - - if ((zret = cache.loadSpan(SpanFile))) - { - + Errata zret; + Cache cache; + if (input_file_path) + printf("passed argv %s\n", input_file_path.path()); + cache.loadURLs(input_file_path); + if ((zret = cache.loadSpan(SpanFile))) { + cache.dumpSpans(Cache::SpanDumpDepth::SPAN); build_stripe_hash_table(); - printf("yohoo"); + for (auto host : URLset) { + INK_MD5 hash; + char hashStr[33]; + ink_code_md5((unsigned char *)host.data(), host.size(), (unsigned char *)&hash); + Stripe *stripe_ = key_to_stripe(&hash, host.data(), host.size()); + printf("hash of %.*s is %s: Stripe %s \n", (int)host.size(), host.data(), + ink_code_to_hex_str(hashStr, (unsigned char *)&hash), stripe_->hashText); } - INK_MD5 hash; - char hashStr[33]; - char* host="http://s.yimg.com"; - ink_code_md5((unsigned char *)host,strlen(host),(unsigned char*)&hash); - printf("%d hash of %s is %s\n",argc,host,ink_code_to_hex_str(hashStr,(unsigned char*)&hash)); - return zret; + } + + return zret; } int @@ -1445,6 +1419,7 @@ main(int argc, char *argv[]) int opt_idx = 0; int opt_val; bool help = false; + FilePath input_url_file; while (-1 != (opt_val = getopt_long(argc, argv, "h", Options, &opt_idx))) { switch (opt_val) { case 'h': @@ -1461,6 +1436,9 @@ main(int argc, char *argv[]) OPEN_RW_FLAG = O_RDWR; std::cout << "NOTE: Writing to physical devices enabled" << std::endl; break; + case 'i': + input_url_file = optarg; + break; } } @@ -1474,7 +1452,7 @@ main(int argc, char *argv[]) Commands.add(std::string("alloc"), std::string("Storage allocation")) .subCommand(std::string("free"), std::string("Allocate storage on free (empty) spans"), &Cmd_Allocate_Empty_Spans); Commands.add(std::string("find"), std::string("Find Stripe Assignment")) - .subCommand(std::string("url"), std::string("URL"),[](int argc, char* argv[]){ return Find_Stripe(argc,argv); }); + .subCommand(std::string("url"), std::string("URL"), [&](int, char *argv[]) { return Find_Stripe(input_url_file); }); Commands.setArgIndex(optind); diff --git a/iocore/cache/Cache.cc b/iocore/cache/Cache.cc index a1ee9a26e3d..b84f4634e1f 100644 --- a/iocore/cache/Cache.cc +++ b/iocore/cache/Cache.cc @@ -1275,7 +1275,7 @@ Vol::init(char *s, off_t blocks, off_t dir_skip, bool clear) ink_strlcpy(hash_text, seed_str, hash_text_size); snprintf(hash_text + hash_seed_size, (hash_text_size - hash_seed_size), " %" PRIu64 ":%" PRIu64 "", (uint64_t)dir_skip, (uint64_t)blocks); - Debug("cache_init","**** hash string %s*****",hash_text.get()); + Debug("cache_init", "**** hash string %s*****", hash_text.get()); MD5Context().hash_immediate(hash_id, hash_text, strlen(hash_text)); dir_skip = ROUND_TO_STORE_BLOCK((dir_skip < START_POS ? START_POS : dir_skip)); @@ -1870,7 +1870,7 @@ build_vol_hash_table(CacheHostRecord *cp) // estimate allocation for (int i = 0; i < num_vols; i++) { - printf("stripe len %"PRId64"\n",p[i]->len); + printf("stripe len %" PRId64 "\n", p[i]->len); forvol[i] = (VOL_HASH_TABLE_SIZE * (p[i]->len >> STORE_BLOCK_SHIFT)) / total; used += forvol[i]; rtable_entries[i] = p[i]->len / VOL_HASH_ALLOC_SIZE; diff --git a/iocore/cache/CacheDisk.cc b/iocore/cache/CacheDisk.cc index 766c5b639ba..3caac466851 100644 --- a/iocore/cache/CacheDisk.cc +++ b/iocore/cache/CacheDisk.cc @@ -218,7 +218,7 @@ CacheDisk::openDone(int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */) int CacheDisk::sync() { - Debug("cache_sync","span %s offset %" PRId64"========>>>>>>>>",path,skip); + Debug("cache_sync", "span %s offset %" PRId64 "========>>>>>>>>", path, skip); io.aiocb.aio_offset = skip; io.aiocb.aio_buf = header; io.aiocb.aio_nbytes = header_len; From efb3067e49a4a44cba65f12b46ce6cb23cc7b69c Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 3 Aug 2017 14:57:37 -0500 Subject: [PATCH 14/19] total to scalar --- cmd/traffic_cache_tool/CacheTool.cc | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index de5b3a6ecd8..d5ff2a33680 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -1155,7 +1155,7 @@ void build_stripe_hash_table() { int num_stripes = globalVec_stripe.size(); - uint64_t total = 0; + CacheStoreBlocks total; unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); unsigned int *rnd = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); @@ -1172,12 +1172,13 @@ build_stripe_hash_table() uint64_t x = elt->hash_id.fold(); // seed random number generator rnd[i] = (unsigned int)x; - total += ((elt->_len.count() * STORE_BLOCK_SIZE) >> STORE_BLOCK_SHIFT); + total += elt->_len; i++; } i = 0; for (auto &elt : globalVec_stripe) { - forvol[i] = (VOL_HASH_TABLE_SIZE * ((elt->_len.count() * STORE_BLOCK_SIZE) >> STORE_BLOCK_SHIFT)) / total; + //forvol[i] = (VOL_HASH_TABLE_SIZE * elt->_len) / total; + forvol[i] =static_cast(VOL_HASH_TABLE_SIZE * elt->_len) / total; used += forvol[i]; gotvol[i] = 0; i++; @@ -1384,10 +1385,7 @@ Clear_Spans(int argc, char *argv[]) return zret; } -void -findAssignment(char *url) -{ -} + Errata Find_Stripe(FilePath const &input_file_path) From 713c724bc10e100dc985097176d2ace4e88e6294 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 3 Aug 2017 15:50:48 -0500 Subject: [PATCH 15/19] rtable_entries to scalar --- cmd/traffic_cache_tool/CacheTool.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index d5ff2a33680..b0667b09b38 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -1167,7 +1167,7 @@ build_stripe_hash_table() // estimate allocation for (auto &elt : globalVec_stripe) { printf("stripe length %" PRId64 "\n", elt->_len.count() * STORE_BLOCK_SIZE); - rtable_entries[i] = elt->_len.count() * STORE_BLOCK_SIZE / VOL_HASH_ALLOC_SIZE; + rtable_entries[i] = elt->_len / 1024; rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); // seed random number generator @@ -1177,7 +1177,6 @@ build_stripe_hash_table() } i = 0; for (auto &elt : globalVec_stripe) { - //forvol[i] = (VOL_HASH_TABLE_SIZE * elt->_len) / total; forvol[i] =static_cast(VOL_HASH_TABLE_SIZE * elt->_len) / total; used += forvol[i]; gotvol[i] = 0; From 197eab6f15f0e06cf7128dfd7c8c2744e27cc1fd Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 3 Aug 2017 16:17:52 -0500 Subject: [PATCH 16/19] rtable to scalar --- cmd/traffic_cache_tool/CacheTool.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index b0667b09b38..a47d10e0027 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -1167,7 +1167,7 @@ build_stripe_hash_table() // estimate allocation for (auto &elt : globalVec_stripe) { printf("stripe length %" PRId64 "\n", elt->_len.count() * STORE_BLOCK_SIZE); - rtable_entries[i] = elt->_len / 1024; + rtable_entries[i] = static_cast(elt->_len / 1024); rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); // seed random number generator From 5bb515360758ed7e085f5c7dee87168a4d142a8a Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Thu, 3 Aug 2017 16:51:42 -0500 Subject: [PATCH 17/19] rtable to scalar --- cmd/traffic_cache_tool/CacheTool.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index a47d10e0027..a7e5f939057 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -56,6 +56,7 @@ using ts::MemView; using ts::CacheDirEntry; #define VOL_HASH_TABLE_SIZE 32707 #define VOL_HASH_ALLOC_SIZE (8 * 1024 * 1024) // one chance per this unit +CacheStoreBlocks Vol_hash_alloc_size(1024); #define VOL_HASH_EMPTY 0xFFFF #define STORE_BLOCK_SHIFT 13 #define STORE_BLOCK_SIZE 8192 @@ -1167,7 +1168,7 @@ build_stripe_hash_table() // estimate allocation for (auto &elt : globalVec_stripe) { printf("stripe length %" PRId64 "\n", elt->_len.count() * STORE_BLOCK_SIZE); - rtable_entries[i] = static_cast(elt->_len / 1024); + rtable_entries[i] = static_cast(elt->_len) / Vol_hash_alloc_size; rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); // seed random number generator From 1b295647f9f78d85627c0c3f4504a90e4f0aa03d Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Fri, 4 Aug 2017 10:12:04 -0500 Subject: [PATCH 18/19] replace define with constexpr --- cmd/traffic_cache_tool/CacheTool.cc | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index a7e5f939057..01246a0173d 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -54,13 +54,11 @@ using ts::Errata; using ts::FilePath; using ts::MemView; using ts::CacheDirEntry; -#define VOL_HASH_TABLE_SIZE 32707 -#define VOL_HASH_ALLOC_SIZE (8 * 1024 * 1024) // one chance per this unit + +constexpr int VOL_HASH_TABLE_SIZE = 32707; CacheStoreBlocks Vol_hash_alloc_size(1024); -#define VOL_HASH_EMPTY 0xFFFF -#define STORE_BLOCK_SHIFT 13 -#define STORE_BLOCK_SIZE 8192 -#define DIR_TAG_WIDTH 12 +constexpr unsigned short VOL_HASH_EMPTY=65535; +constexpr int DIR_TAG_WIDTH=12; const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL; @@ -204,7 +202,7 @@ Stripe::Stripe(Span *span, Bytes start, CacheStoreBlocks len) : _span(span), _st char *hash_text = static_cast(ats_malloc(hash_text_size)); strncpy(hash_text, diskPath, hash_text_size); snprintf(hash_text + hash_seed_size, (hash_text_size - hash_seed_size), " %" PRIu64 ":%" PRIu64 "", (uint64_t)_start, - (uint64_t)_len.count() * STORE_BLOCK_SIZE); + (uint64_t)_len.count()); printf("hash id of stripe is hash of %s\n", hash_text); ink_code_md5((unsigned char *)hash_text, strlen(hash_text), (unsigned char *)&hash_id); hashText = hash_text; @@ -1155,7 +1153,7 @@ next_rand(unsigned int *p) void build_stripe_hash_table() { - int num_stripes = globalVec_stripe.size(); + int num_stripes = globalVec_stripe.size(); CacheStoreBlocks total; unsigned int *forvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); unsigned int *gotvol = (unsigned int *)ats_malloc(sizeof(unsigned int) * num_stripes); @@ -1167,7 +1165,7 @@ build_stripe_hash_table() uint64_t used = 0; // estimate allocation for (auto &elt : globalVec_stripe) { - printf("stripe length %" PRId64 "\n", elt->_len.count() * STORE_BLOCK_SIZE); + // printf("stripe length %" PRId64 "\n", elt->_len.count()); rtable_entries[i] = static_cast(elt->_len) / Vol_hash_alloc_size; rtable_size += rtable_entries[i]; uint64_t x = elt->hash_id.fold(); @@ -1178,7 +1176,7 @@ build_stripe_hash_table() } i = 0; for (auto &elt : globalVec_stripe) { - forvol[i] =static_cast(VOL_HASH_TABLE_SIZE * elt->_len) / total; + forvol[i] = static_cast(VOL_HASH_TABLE_SIZE * elt->_len) / total; used += forvol[i]; gotvol[i] = 0; i++; @@ -1386,7 +1384,6 @@ Clear_Spans(int argc, char *argv[]) return zret; } - Errata Find_Stripe(FilePath const &input_file_path) { From 962ee45df8638e2868b08bab5afb917bb014e948 Mon Sep 17 00:00:00 2001 From: Persia Aziz Date: Fri, 4 Aug 2017 10:30:12 -0500 Subject: [PATCH 19/19] move methods under struct cache --- cmd/traffic_cache_tool/CacheTool.cc | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/traffic_cache_tool/CacheTool.cc b/cmd/traffic_cache_tool/CacheTool.cc index 01246a0173d..febd4ed323e 100644 --- a/cmd/traffic_cache_tool/CacheTool.cc +++ b/cmd/traffic_cache_tool/CacheTool.cc @@ -57,8 +57,8 @@ using ts::CacheDirEntry; constexpr int VOL_HASH_TABLE_SIZE = 32707; CacheStoreBlocks Vol_hash_alloc_size(1024); -constexpr unsigned short VOL_HASH_EMPTY=65535; -constexpr int DIR_TAG_WIDTH=12; +constexpr unsigned short VOL_HASH_EMPTY = 65535; +constexpr int DIR_TAG_WIDTH = 12; const Bytes ts::CacheSpan::OFFSET{CacheStoreBlocks{1}}; enum { SILENT = 0, NORMAL, VERBOSE } Verbosity = NORMAL; @@ -74,9 +74,7 @@ ts::CommandTable Commands; int OPEN_RW_FLAG = O_RDONLY; struct Stripe; -std::vector globalVec_stripe; -std::unordered_set URLset; -unsigned short *stripes_hash_table; + struct Span { Span(FilePath const &path) : _path(path) {} Errata load(); @@ -510,11 +508,16 @@ struct Cache { enum class SpanDumpDepth { SPAN, STRIPE, DIRECTORY }; void dumpSpans(SpanDumpDepth depth); void dumpVolumes(); + void build_stripe_hash_table(); + Stripe *key_to_stripe(INK_MD5 *key, const char *hostname, int host_len); // ts::CacheStripeBlocks calcTotalSpanPhysicalSize(); ts::CacheStripeBlocks calcTotalSpanConfiguredSize(); std::list _spans; std::map _volumes; + std::vector globalVec_stripe; + std::unordered_set URLset; + unsigned short *stripes_hash_table; }; Errata @@ -1151,7 +1154,7 @@ next_rand(unsigned int *p) } void -build_stripe_hash_table() +Cache::build_stripe_hash_table() { int num_stripes = globalVec_stripe.size(); CacheStoreBlocks total; @@ -1231,7 +1234,7 @@ build_stripe_hash_table() } Stripe * -key_to_stripe(INK_MD5 *key, const char *hostname, int host_len) +Cache::key_to_stripe(INK_MD5 *key, const char *hostname, int host_len) { uint32_t h = (key->slice32(2) >> DIR_TAG_WIDTH) % VOL_HASH_TABLE_SIZE; return globalVec_stripe[stripes_hash_table[h]]; @@ -1394,12 +1397,12 @@ Find_Stripe(FilePath const &input_file_path) cache.loadURLs(input_file_path); if ((zret = cache.loadSpan(SpanFile))) { cache.dumpSpans(Cache::SpanDumpDepth::SPAN); - build_stripe_hash_table(); - for (auto host : URLset) { + cache.build_stripe_hash_table(); + for (auto host : cache.URLset) { INK_MD5 hash; char hashStr[33]; ink_code_md5((unsigned char *)host.data(), host.size(), (unsigned char *)&hash); - Stripe *stripe_ = key_to_stripe(&hash, host.data(), host.size()); + Stripe *stripe_ = cache.key_to_stripe(&hash, host.data(), host.size()); printf("hash of %.*s is %s: Stripe %s \n", (int)host.size(), host.data(), ink_code_to_hex_str(hashStr, (unsigned char *)&hash), stripe_->hashText); }