Skip to content

Commit

Permalink
adds tile cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Plaza committed Aug 29, 2015
1 parent 477cdaf commit 6faa9f5
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ endif()
# Compile libdvidcpp library components
add_library (dvidcpp src/DVIDNodeService.cpp src/DVIDServerService.cpp
src/DVIDConnection.cpp src/DVIDException.cpp src/DVIDGraph.cpp
src/BinaryData.cpp src/DVIDThreadedFetch.cpp src/Algorithms.cpp)
src/BinaryData.cpp src/DVIDThreadedFetch.cpp src/Algorithms.cpp src/DVIDCache.cpp)
target_link_libraries (dvidcpp ${LIBDVID_EXT_LIBS})
if (NOT ${BUILDEM_DIR} STREQUAL "None")
add_dependencies (dvidcpp ${LIBDVID_DEPS})
Expand Down
39 changes: 39 additions & 0 deletions libdvid/DVIDCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* Caches requests from DVID.
*/

#ifndef DVIDCACHE
#define DVIDCACHE

#include <tr1/unordered_map>
#include <boost/thread/mutex.hpp>
#include "BinaryData.h"

namespace libdvid {

class DVIDCache {
public:
static DVIDCache* get_cache();
void set_cache_size(unsigned long long max_size_);
void set(std::string key, BinaryDataPtr data);
BinaryDataPtr get(std::string key);

private:
DVIDCache() : cache_stamp(0), max_size(0), curr_size(0) {}
void delete_old_cache();
void clear_cache();

static boost::mutex mutex;

std::tr1::unordered_map<std::string, BinaryDataPtr> cache;
std::tr1::unordered_map<std::string, unsigned long long> cache_stamps;
unsigned long long cache_stamp;
unsigned long long max_size;
unsigned long long curr_size;

};

}


#endif
66 changes: 64 additions & 2 deletions load_tests/loadtest_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <libdvid/DVIDNodeService.h>
#include <libdvid/DVIDThreadedFetch.h>
#include "ScopeTime.h"
#include "DVIDCache.h"


#include <iostream>

Expand Down Expand Up @@ -43,7 +45,6 @@ int FETCHSIZE = 1024;
*/
int main(int argc, char** argv)
{
cout << "Blah!!" << endl;
// must point to a DVID instance with tiles (with name tile name) and
// starting voxel coordinates
if (argc != 7 && argc != 8) {
Expand All @@ -52,7 +53,8 @@ int main(int argc, char** argv)
}
try {
ScopeTime overall_time;

DVIDCache::get_cache()->set_cache_size(1000000000);

DVIDNodeService dvid_node(argv[1], argv[2]);

// some arithmetic to get tiles aligned to the tile space (indicated
Expand Down Expand Up @@ -148,6 +150,66 @@ int main(int argc, char** argv)
cout << "Frame rate: " << total_read_time / NUM_FETCHES * 1000 << " milliseconds" << endl;


// with cache
ScopeTime tiles_timer2(false);
for (int z = zstart; z < (zstart+NUM_FETCHES); ++z) {
// fetch 9 tiles
ScopeTime tile_timer(false);
tilepos[2] = z;
int bytes_read = 0;


vector<vector<int> > tilepos_array;


tilepos[0] = xstart / TILESIZE;
tilepos[1] = ystart / TILESIZE;
tilepos_array.push_back(tilepos);

tilepos[0] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] -= 2;
tilepos[1] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] -= 2;
tilepos[1] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] += 1;
tilepos_array.push_back(tilepos);

tilepos[0] += 1;
tilepos_array.push_back(tilepos);


vector<BinaryDataPtr> tiles = get_tile_array_binary(dvid_node, tilename, XY, 0, tilepos_array, 0);
for (int i = 0; i < tiles.size(); ++i) {
bytes_read += tiles[i]->length();
}

total_bytes_read += bytes_read;
double read_time = tile_timer.getElapsed();
cout << "Read " << bytes_read << " bytes (9 tiles) in " << read_time << " seconds" << endl;
}
total_read_time = tiles_timer2.getElapsed();
cout << "Read " << total_bytes_read << " bytes (" << NUM_FETCHES << " tile planes) in " << total_read_time << " seconds" << endl;
cout << "Frame rate: " << total_read_time / NUM_FETCHES * 1000 << " milliseconds" << endl;





//exit(0);

// size for gray and label fetch
Expand Down
90 changes: 90 additions & 0 deletions src/DVIDCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "DVIDCache.h"
#include <map>

using std::map; using std::string;
using std::tr1::unordered_map;

namespace libdvid {

boost::mutex DVIDCache::mutex;

DVIDCache* DVIDCache::get_cache()
{
boost::mutex::scoped_lock lock(mutex);
static DVIDCache cache;
return &cache;
}

void DVIDCache::set_cache_size(unsigned long long max_size_)
{
boost::mutex::scoped_lock lock(mutex);
clear_cache();
max_size = max_size_;
}

void DVIDCache::set(std::string key, BinaryDataPtr data)
{
boost::mutex::scoped_lock lock(mutex);

if (max_size > 0) {
BinaryDataPtr ptr = cache[key];

if (ptr) {
curr_size -= ptr->length();
}
curr_size += data->length();
cache[key] = data;
cache_stamps[key] = cache_stamp;
++cache_stamp;

if (curr_size > max_size) {
delete_old_cache();
}
}
}

BinaryDataPtr DVIDCache::get(std::string key)
{
boost::mutex::scoped_lock lock(mutex);
BinaryDataPtr ptr;

if (max_size > 0) {
if (cache.find(key) != cache.end()) {
ptr = cache[key];
cache_stamps[key] = cache_stamp;
++cache_stamp;
}
}
return ptr;
}

void DVIDCache::clear_cache()
{
cache.clear();
cache_stamps.clear();
cache_stamp = 0;
max_size = 0;
curr_size = 0;
}


void DVIDCache::delete_old_cache()
{
map<unsigned long long, string> cache_order;
unsigned long long delsize = 0;

for (unordered_map<string, unsigned long long>::iterator iter = cache_stamps.begin(); iter != cache_stamps.end(); ++iter) {
cache_order[iter->second] = iter->first;
}

for (map<unsigned long long, string>::iterator iter = cache_order.begin(); iter != cache_order.end(); ++iter) {
delsize += cache[iter->second]->length();
cache.erase(iter->second);
cache_stamps.erase(iter->second);
if (delsize > (max_size/2)) {
break;
}
}
}

}
10 changes: 9 additions & 1 deletion src/DVIDNodeService.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "DVIDNodeService.h"
#include "DVIDException.h"
#include "DVIDCache.h"

#include <json/json.h>
#include <set>
Expand Down Expand Up @@ -160,7 +161,14 @@ BinaryDataPtr DVIDNodeService::get_tile_slice_binary(string datatype_instance,
}

string endpoint = sstr.str();
return custom_request(endpoint, BinaryDataPtr(), GET);
BinaryDataPtr cachedata = DVIDCache::get_cache()->get(uuid + "/" + endpoint);
if (cachedata) {
return cachedata;
}

cachedata = custom_request(endpoint, BinaryDataPtr(), GET);
DVIDCache::get_cache()->set(uuid + "/" + endpoint, cachedata);
return cachedata;
}

Grayscale3D DVIDNodeService::get_gray3D(string datatype_instance, Dims_t sizes,
Expand Down

0 comments on commit 6faa9f5

Please sign in to comment.