Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread contention in Cache #145

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion cpp/include/cucim/cache/image_cache_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,26 @@ constexpr uint64_t kOneMiB = 1024UL * 1024;
constexpr std::string_view kDefaultCacheTypeStr = "nocache";
constexpr CacheType kDefaultCacheType = cucim::cache::CacheType::kNoCache;
constexpr uint64_t kDefaultCacheMemoryCapacity = 1024UL;
constexpr uint32_t kDefaultCacheMutexPoolCapacity = 11117;
/**
* @brief Mutex Pool size
*
* >>> from functools import reduce
* >>> def calc(pool_size, thread_size):
* >>> a = reduce(lambda x,y: x*y, range(pool_size, pool_size - thread_size, -1))
* >>> print(1 - (a / (pool_size**thread_size)))
*
* >>> calc(100003, 128)
* 0.07809410393222294
* >>> calc(100003, 256)
* 0.2786772006302005
*
* See https://godbolt.org/z/Tvx8179xK
* Creating a pool of 100000 mutexes takes only about 4 MB which is not big.
* I believe that making the mutex size biggger enough helps to the reduce the thread contention.
* For systems with more than 256 threads, the pool size should be larger.
* Choose a prime number for the pool size (https://primes.utm.edu/lists/small/100000.txt).
*/
constexpr uint32_t kDefaultCacheMutexPoolCapacity = 100003;
constexpr uint32_t kDefaultCacheListPadding = 10000;
constexpr uint32_t kDefaultCacheExtraSharedMemorySize = 100;
constexpr bool kDefaultCacheRecordStat = false;
Expand Down
16 changes: 10 additions & 6 deletions cpp/plugins/cucim.kit.cuslide/src/cuslide/tiff/ifd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ bool IFD::read_region_tiles(const TIFF* tiff,
uint32_t dest_pixel_index_x = 0;

uint32_t index = index_y + offset_sx;
// Calculate a simple hash value for the tile index
uint64_t index_hash = ifd_hash_value ^ (static_cast<uint64_t>(index) | (static_cast<uint64_t>(index) << 32));
for (uint32_t offset_x = offset_sx; offset_x <= offset_ex; ++offset_x, ++index)
{
auto tiledata_offset = static_cast<uint64_t>(ifd->image_piece_offsets_[index]);
Expand All @@ -519,11 +521,11 @@ bool IFD::read_region_tiles(const TIFF* tiff,
if (tiledata_size > 0)
{
auto key = image_cache.create_key(ifd_hash_value, index);
image_cache.lock(index);
image_cache.lock(index_hash);
auto value = image_cache.find(key);
if (value)
{
image_cache.unlock(index);
image_cache.unlock(index_hash);
tile_data = static_cast<uint8_t*>(value->data);
}
else
Expand Down Expand Up @@ -577,7 +579,7 @@ bool IFD::read_region_tiles(const TIFF* tiff,

value = image_cache.create_value(tile_data, tile_raster_nbytes);
image_cache.insert(key, value);
image_cache.unlock(index);
image_cache.unlock(index_hash);
}

for (uint32_t ty = tile_pixel_offset_sy; ty <= tile_pixel_offset_ey;
Expand Down Expand Up @@ -743,6 +745,8 @@ bool IFD::read_region_tiles_boundary(const TIFF* tiff,
uint32_t dest_pixel_index_x = 0;

int64_t index = index_y + offset_sx;
// Calculate a simple hash value for the tile index
uint64_t index_hash = ifd_hash_value ^ (static_cast<uint64_t>(index) | (static_cast<uint64_t>(index) << 32));
for (int64_t offset_x = offset_sx; offset_x <= offset_ex; ++offset_x, ++index)
{
uint64_t tiledata_offset = 0;
Expand Down Expand Up @@ -798,11 +802,11 @@ bool IFD::read_region_tiles_boundary(const TIFF* tiff,
uint8_t* tile_data = tile_raster;

auto key = image_cache.create_key(ifd_hash_value, index);
image_cache.lock(index);
image_cache.lock(index_hash);
auto value = image_cache.find(key);
if (value)
{
image_cache.unlock(index);
image_cache.unlock(index_hash);
tile_data = static_cast<uint8_t*>(value->data);
}
else
Expand Down Expand Up @@ -855,7 +859,7 @@ bool IFD::read_region_tiles_boundary(const TIFF* tiff,
}
value = image_cache.create_value(tile_data, tile_raster_nbytes);
image_cache.insert(key, value);
image_cache.unlock(index);
image_cache.unlock(index_hash);
}
if (copy_partial)
{
Expand Down