cache.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (c) 2018 Anton Valentinov Kirilov
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. associated documentation files (the "Software"), to deal in the Software without restriction,
  5. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies or
  9. substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  11. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  14. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. */
  16. #include <assert.h>
  17. #include <pthread.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <h2o/cache.h>
  22. #include "cache.h"
  23. #include "error.h"
  24. #include "utility.h"
  25. // Increasing the number of caches by the following factor reduces contention; must be a power of 2.
  26. #define CONCURRENCY_FACTOR 4
  27. static size_t get_index(size_t n, h2o_cache_hashcode_t keyhash)
  28. {
  29. assert(is_power_of_2(n));
  30. return keyhash & (n - 1);
  31. }
  32. int cache_create(size_t concurrency,
  33. size_t capacity,
  34. uint64_t duration,
  35. void (*destroy_cb)(h2o_iovec_t value),
  36. cache_t *cache)
  37. {
  38. memset(cache, 0, sizeof(*cache));
  39. assert(is_power_of_2(CONCURRENCY_FACTOR));
  40. // Rounding up to a power of 2 simplifies the calculations a little bit, and, as any increase in
  41. // the number of caches, potentially reduces thread contention.
  42. cache->cache_num = CONCURRENCY_FACTOR * round_up_to_power_of_2(concurrency);
  43. cache->cache_num = MAX(cache->cache_num, 1);
  44. capacity = (capacity + cache->cache_num - 1) / cache->cache_num;
  45. pthread_mutexattr_t attr;
  46. if (pthread_mutexattr_init(&attr))
  47. return 1;
  48. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP))
  49. goto error;
  50. cache->cache = malloc(cache->cache_num * sizeof(*cache->cache));
  51. if (!cache->cache)
  52. goto error;
  53. cache->cache_lock = malloc(cache->cache_num * sizeof(*cache->cache_lock));
  54. if (!cache->cache_lock)
  55. goto error_malloc;
  56. for (size_t i = 0; i < cache->cache_num; i++) {
  57. cache->cache[i] = h2o_cache_create(0, capacity, duration, destroy_cb);
  58. if (!cache->cache[i] || pthread_mutex_init(cache->cache_lock + i, &attr)) {
  59. if (cache->cache[i])
  60. h2o_cache_destroy(cache->cache[i]);
  61. cache->cache_num = i;
  62. cache_destroy(cache);
  63. goto error;
  64. }
  65. }
  66. pthread_mutexattr_destroy(&attr);
  67. return 0;
  68. error_malloc:
  69. free(cache->cache);
  70. error:
  71. pthread_mutexattr_destroy(&attr);
  72. return 1;
  73. }
  74. void cache_destroy(cache_t *cache)
  75. {
  76. if (cache->cache) {
  77. assert(cache->cache_lock);
  78. for (size_t i = 0; i < cache->cache_num; i++) {
  79. h2o_cache_destroy(cache->cache[i]);
  80. pthread_mutex_destroy(cache->cache_lock + i);
  81. }
  82. free(cache->cache);
  83. free(cache->cache_lock);
  84. cache->cache = NULL;
  85. cache->cache_lock = NULL;
  86. }
  87. else
  88. assert(!cache->cache_lock);
  89. }
  90. h2o_cache_ref_t *cache_fetch(cache_t *cache,
  91. uint64_t now,
  92. h2o_iovec_t key,
  93. h2o_cache_hashcode_t keyhash)
  94. {
  95. if (!keyhash)
  96. keyhash = h2o_cache_calchash(key.base, key.len);
  97. const size_t idx = get_index(cache->cache_num, keyhash);
  98. pthread_mutex_t * const mutex = cache->cache_lock + idx;
  99. CHECK_ERROR(pthread_mutex_lock, mutex);
  100. h2o_cache_ref_t * const ret = h2o_cache_fetch(cache->cache[idx], now, key, keyhash);
  101. CHECK_ERROR(pthread_mutex_unlock, mutex);
  102. return ret;
  103. }
  104. void cache_release(cache_t *cache, h2o_cache_ref_t *ref, h2o_cache_hashcode_t keyhash)
  105. {
  106. if (!keyhash)
  107. keyhash = h2o_cache_calchash(ref->key.base, ref->key.len);
  108. const size_t idx = get_index(cache->cache_num, keyhash);
  109. h2o_cache_release(cache->cache[idx], ref);
  110. }
  111. int cache_set(uint64_t now,
  112. h2o_iovec_t key,
  113. h2o_cache_hashcode_t keyhash,
  114. h2o_iovec_t value,
  115. cache_t *cache)
  116. {
  117. if (!keyhash)
  118. keyhash = h2o_cache_calchash(key.base, key.len);
  119. const size_t idx = get_index(cache->cache_num, keyhash);
  120. pthread_mutex_t * const mutex = cache->cache_lock + idx;
  121. CHECK_ERROR(pthread_mutex_lock, mutex);
  122. const int ret = h2o_cache_set(cache->cache[idx], now, key, keyhash, value);
  123. CHECK_ERROR(pthread_mutex_unlock, mutex);
  124. return ret;
  125. }