slice_intern.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #include "src/core/lib/slice/slice_internal.h"
  20. #include <inttypes.h>
  21. #include <string.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include "src/core/lib/gpr/murmur_hash.h"
  25. #include "src/core/lib/gprpp/sync.h"
  26. #include "src/core/lib/iomgr/iomgr_internal.h" /* for iomgr_abort_on_leaks() */
  27. #include "src/core/lib/profiling/timers.h"
  28. #include "src/core/lib/slice/slice_string_helpers.h"
  29. #include "src/core/lib/transport/static_metadata.h"
  30. #define LOG2_SHARD_COUNT 5
  31. #define SHARD_COUNT (1 << LOG2_SHARD_COUNT)
  32. #define INITIAL_SHARD_CAPACITY 8
  33. #define TABLE_IDX(hash, capacity) (((hash) >> LOG2_SHARD_COUNT) % (capacity))
  34. #define SHARD_IDX(hash) ((hash) & ((1 << LOG2_SHARD_COUNT) - 1))
  35. using grpc_core::InternedSliceRefcount;
  36. typedef struct slice_shard {
  37. gpr_mu mu;
  38. InternedSliceRefcount** strs;
  39. size_t count;
  40. size_t capacity;
  41. } slice_shard;
  42. /* hash seed: decided at initialization time */
  43. uint32_t g_hash_seed;
  44. static int g_forced_hash_seed = 0;
  45. static slice_shard g_shards[SHARD_COUNT];
  46. typedef struct {
  47. uint32_t hash;
  48. uint32_t idx;
  49. } static_metadata_hash_ent;
  50. static static_metadata_hash_ent
  51. static_metadata_hash[4 * GRPC_STATIC_MDSTR_COUNT];
  52. static uint32_t max_static_metadata_hash_probe;
  53. uint32_t grpc_static_metadata_hash_values[GRPC_STATIC_MDSTR_COUNT];
  54. namespace grpc_core {
  55. InternedSliceRefcount::~InternedSliceRefcount() {
  56. slice_shard* shard = &g_shards[SHARD_IDX(this->hash)];
  57. MutexLock lock(&shard->mu);
  58. InternedSliceRefcount** prev_next;
  59. InternedSliceRefcount* cur;
  60. for (prev_next = &shard->strs[TABLE_IDX(this->hash, shard->capacity)],
  61. cur = *prev_next;
  62. cur != this; prev_next = &cur->bucket_next, cur = cur->bucket_next)
  63. ;
  64. *prev_next = cur->bucket_next;
  65. shard->count--;
  66. }
  67. } // namespace grpc_core
  68. static void grow_shard(slice_shard* shard) {
  69. GPR_TIMER_SCOPE("grow_strtab", 0);
  70. size_t capacity = shard->capacity * 2;
  71. size_t i;
  72. InternedSliceRefcount** strtab;
  73. InternedSliceRefcount *s, *next;
  74. strtab = static_cast<InternedSliceRefcount**>(
  75. gpr_zalloc(sizeof(InternedSliceRefcount*) * capacity));
  76. for (i = 0; i < shard->capacity; i++) {
  77. for (s = shard->strs[i]; s; s = next) {
  78. size_t idx = TABLE_IDX(s->hash, capacity);
  79. next = s->bucket_next;
  80. s->bucket_next = strtab[idx];
  81. strtab[idx] = s;
  82. }
  83. }
  84. gpr_free(shard->strs);
  85. shard->strs = strtab;
  86. shard->capacity = capacity;
  87. }
  88. static grpc_slice materialize(InternedSliceRefcount* s) {
  89. grpc_slice slice;
  90. slice.refcount = &s->base;
  91. slice.data.refcounted.bytes = reinterpret_cast<uint8_t*>(s + 1);
  92. slice.data.refcounted.length = s->length;
  93. return slice;
  94. }
  95. uint32_t grpc_slice_default_hash_impl(grpc_slice s) {
  96. return gpr_murmur_hash3(GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s),
  97. g_hash_seed);
  98. }
  99. uint32_t grpc_static_slice_hash(grpc_slice s) {
  100. return grpc_static_metadata_hash_values[GRPC_STATIC_METADATA_INDEX(s)];
  101. }
  102. int grpc_static_slice_eq(grpc_slice a, grpc_slice b) {
  103. return GRPC_STATIC_METADATA_INDEX(a) == GRPC_STATIC_METADATA_INDEX(b);
  104. }
  105. uint32_t grpc_slice_hash(grpc_slice s) {
  106. return s.refcount == nullptr ? grpc_slice_default_hash_impl(s)
  107. : s.refcount->Hash(s);
  108. }
  109. grpc_slice grpc_slice_maybe_static_intern(grpc_slice slice,
  110. bool* returned_slice_is_different) {
  111. if (GRPC_IS_STATIC_METADATA_STRING(slice)) {
  112. return slice;
  113. }
  114. uint32_t hash = grpc_slice_hash(slice);
  115. for (uint32_t i = 0; i <= max_static_metadata_hash_probe; i++) {
  116. static_metadata_hash_ent ent =
  117. static_metadata_hash[(hash + i) % GPR_ARRAY_SIZE(static_metadata_hash)];
  118. if (ent.hash == hash && ent.idx < GRPC_STATIC_MDSTR_COUNT &&
  119. grpc_slice_eq(grpc_static_slice_table[ent.idx], slice)) {
  120. *returned_slice_is_different = true;
  121. return grpc_static_slice_table[ent.idx];
  122. }
  123. }
  124. return slice;
  125. }
  126. bool grpc_slice_is_interned(const grpc_slice& slice) {
  127. return (slice.refcount &&
  128. (slice.refcount->GetType() == grpc_slice_refcount::Type::INTERNED ||
  129. GRPC_IS_STATIC_METADATA_STRING(slice)));
  130. }
  131. grpc_slice grpc_slice_intern(grpc_slice slice) {
  132. GPR_TIMER_SCOPE("grpc_slice_intern", 0);
  133. if (GRPC_IS_STATIC_METADATA_STRING(slice)) {
  134. return slice;
  135. }
  136. uint32_t hash = grpc_slice_hash(slice);
  137. for (uint32_t i = 0; i <= max_static_metadata_hash_probe; i++) {
  138. static_metadata_hash_ent ent =
  139. static_metadata_hash[(hash + i) % GPR_ARRAY_SIZE(static_metadata_hash)];
  140. if (ent.hash == hash && ent.idx < GRPC_STATIC_MDSTR_COUNT &&
  141. grpc_slice_eq(grpc_static_slice_table[ent.idx], slice)) {
  142. return grpc_static_slice_table[ent.idx];
  143. }
  144. }
  145. InternedSliceRefcount* s;
  146. slice_shard* shard = &g_shards[SHARD_IDX(hash)];
  147. gpr_mu_lock(&shard->mu);
  148. /* search for an existing string */
  149. size_t idx = TABLE_IDX(hash, shard->capacity);
  150. for (s = shard->strs[idx]; s; s = s->bucket_next) {
  151. if (s->hash == hash && grpc_slice_eq(slice, materialize(s))) {
  152. if (s->refcnt.RefIfNonZero()) {
  153. gpr_mu_unlock(&shard->mu);
  154. return materialize(s);
  155. }
  156. }
  157. }
  158. /* not found: create a new string */
  159. /* string data goes after the internal_string header */
  160. s = static_cast<InternedSliceRefcount*>(
  161. gpr_malloc(sizeof(*s) + GRPC_SLICE_LENGTH(slice)));
  162. new (s) grpc_core::InternedSliceRefcount(GRPC_SLICE_LENGTH(slice), hash,
  163. shard->strs[idx]);
  164. memcpy(reinterpret_cast<char*>(s + 1), GRPC_SLICE_START_PTR(slice),
  165. GRPC_SLICE_LENGTH(slice));
  166. shard->strs[idx] = s;
  167. shard->count++;
  168. if (shard->count > shard->capacity * 2) {
  169. grow_shard(shard);
  170. }
  171. gpr_mu_unlock(&shard->mu);
  172. return materialize(s);
  173. }
  174. void grpc_test_only_set_slice_hash_seed(uint32_t seed) {
  175. g_hash_seed = seed;
  176. g_forced_hash_seed = 1;
  177. }
  178. void grpc_slice_intern_init(void) {
  179. if (!g_forced_hash_seed) {
  180. g_hash_seed = static_cast<uint32_t>(gpr_now(GPR_CLOCK_REALTIME).tv_nsec);
  181. }
  182. for (size_t i = 0; i < SHARD_COUNT; i++) {
  183. slice_shard* shard = &g_shards[i];
  184. gpr_mu_init(&shard->mu);
  185. shard->count = 0;
  186. shard->capacity = INITIAL_SHARD_CAPACITY;
  187. shard->strs = static_cast<InternedSliceRefcount**>(
  188. gpr_zalloc(sizeof(*shard->strs) * shard->capacity));
  189. }
  190. for (size_t i = 0; i < GPR_ARRAY_SIZE(static_metadata_hash); i++) {
  191. static_metadata_hash[i].hash = 0;
  192. static_metadata_hash[i].idx = GRPC_STATIC_MDSTR_COUNT;
  193. }
  194. max_static_metadata_hash_probe = 0;
  195. for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
  196. grpc_static_metadata_hash_values[i] =
  197. grpc_slice_default_hash_impl(grpc_static_slice_table[i]);
  198. for (size_t j = 0; j < GPR_ARRAY_SIZE(static_metadata_hash); j++) {
  199. size_t slot = (grpc_static_metadata_hash_values[i] + j) %
  200. GPR_ARRAY_SIZE(static_metadata_hash);
  201. if (static_metadata_hash[slot].idx == GRPC_STATIC_MDSTR_COUNT) {
  202. static_metadata_hash[slot].hash = grpc_static_metadata_hash_values[i];
  203. static_metadata_hash[slot].idx = static_cast<uint32_t>(i);
  204. if (j > max_static_metadata_hash_probe) {
  205. max_static_metadata_hash_probe = static_cast<uint32_t>(j);
  206. }
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. void grpc_slice_intern_shutdown(void) {
  213. for (size_t i = 0; i < SHARD_COUNT; i++) {
  214. slice_shard* shard = &g_shards[i];
  215. gpr_mu_destroy(&shard->mu);
  216. /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
  217. if (shard->count != 0) {
  218. gpr_log(GPR_DEBUG, "WARNING: %" PRIuPTR " metadata strings were leaked",
  219. shard->count);
  220. for (size_t j = 0; j < shard->capacity; j++) {
  221. for (InternedSliceRefcount* s = shard->strs[j]; s; s = s->bucket_next) {
  222. char* text =
  223. grpc_dump_slice(materialize(s), GPR_DUMP_HEX | GPR_DUMP_ASCII);
  224. gpr_log(GPR_DEBUG, "LEAKED: %s", text);
  225. gpr_free(text);
  226. }
  227. }
  228. if (grpc_iomgr_abort_on_leaks()) {
  229. abort();
  230. }
  231. }
  232. gpr_free(shard->strs);
  233. }
  234. }