slice_intern.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "src/core/lib/slice/slice_internal.h"
  19. #include <inttypes.h>
  20. #include <string.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include "src/core/lib/iomgr/iomgr_internal.h" /* for iomgr_abort_on_leaks() */
  24. #include "src/core/lib/profiling/timers.h"
  25. #include "src/core/lib/slice/slice_string_helpers.h"
  26. #include "src/core/lib/support/murmur_hash.h"
  27. #include "src/core/lib/transport/static_metadata.h"
  28. #define LOG2_SHARD_COUNT 5
  29. #define SHARD_COUNT (1 << LOG2_SHARD_COUNT)
  30. #define INITIAL_SHARD_CAPACITY 8
  31. #define TABLE_IDX(hash, capacity) (((hash) >> LOG2_SHARD_COUNT) % (capacity))
  32. #define SHARD_IDX(hash) ((hash) & ((1 << LOG2_SHARD_COUNT) - 1))
  33. typedef struct interned_slice_refcount {
  34. grpc_slice_refcount base;
  35. grpc_slice_refcount sub;
  36. size_t length;
  37. gpr_atm refcnt;
  38. uint32_t hash;
  39. struct interned_slice_refcount* bucket_next;
  40. } interned_slice_refcount;
  41. typedef struct slice_shard {
  42. gpr_mu mu;
  43. interned_slice_refcount** strs;
  44. size_t count;
  45. size_t capacity;
  46. } slice_shard;
  47. /* hash seed: decided at initialization time */
  48. static uint32_t g_hash_seed;
  49. static int g_forced_hash_seed = 0;
  50. static slice_shard g_shards[SHARD_COUNT];
  51. typedef struct {
  52. uint32_t hash;
  53. uint32_t idx;
  54. } static_metadata_hash_ent;
  55. static static_metadata_hash_ent
  56. static_metadata_hash[4 * GRPC_STATIC_MDSTR_COUNT];
  57. static uint32_t max_static_metadata_hash_probe;
  58. static uint32_t static_metadata_hash_values[GRPC_STATIC_MDSTR_COUNT];
  59. static void interned_slice_ref(void* p) {
  60. interned_slice_refcount* s = (interned_slice_refcount*)p;
  61. GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&s->refcnt, 1) > 0);
  62. }
  63. static void interned_slice_destroy(interned_slice_refcount* s) {
  64. slice_shard* shard = &g_shards[SHARD_IDX(s->hash)];
  65. gpr_mu_lock(&shard->mu);
  66. GPR_ASSERT(0 == gpr_atm_no_barrier_load(&s->refcnt));
  67. interned_slice_refcount** prev_next;
  68. interned_slice_refcount* cur;
  69. for (prev_next = &shard->strs[TABLE_IDX(s->hash, shard->capacity)],
  70. cur = *prev_next;
  71. cur != s; prev_next = &cur->bucket_next, cur = cur->bucket_next)
  72. ;
  73. *prev_next = cur->bucket_next;
  74. shard->count--;
  75. gpr_free(s);
  76. gpr_mu_unlock(&shard->mu);
  77. }
  78. static void interned_slice_unref(grpc_exec_ctx* exec_ctx, void* p) {
  79. interned_slice_refcount* s = (interned_slice_refcount*)p;
  80. if (1 == gpr_atm_full_fetch_add(&s->refcnt, -1)) {
  81. interned_slice_destroy(s);
  82. }
  83. }
  84. static void interned_slice_sub_ref(void* p) {
  85. interned_slice_ref(((char*)p) - offsetof(interned_slice_refcount, sub));
  86. }
  87. static void interned_slice_sub_unref(grpc_exec_ctx* exec_ctx, void* p) {
  88. interned_slice_unref(exec_ctx,
  89. ((char*)p) - offsetof(interned_slice_refcount, sub));
  90. }
  91. static uint32_t interned_slice_hash(grpc_slice slice) {
  92. interned_slice_refcount* s = (interned_slice_refcount*)slice.refcount;
  93. return s->hash;
  94. }
  95. static int interned_slice_eq(grpc_slice a, grpc_slice b) {
  96. return a.refcount == b.refcount;
  97. }
  98. static const grpc_slice_refcount_vtable interned_slice_vtable = {
  99. interned_slice_ref, interned_slice_unref, interned_slice_eq,
  100. interned_slice_hash};
  101. static const grpc_slice_refcount_vtable interned_slice_sub_vtable = {
  102. interned_slice_sub_ref, interned_slice_sub_unref,
  103. grpc_slice_default_eq_impl, grpc_slice_default_hash_impl};
  104. static void grow_shard(slice_shard* shard) {
  105. size_t capacity = shard->capacity * 2;
  106. size_t i;
  107. interned_slice_refcount** strtab;
  108. interned_slice_refcount *s, *next;
  109. GPR_TIMER_BEGIN("grow_strtab", 0);
  110. strtab = (interned_slice_refcount**)gpr_zalloc(
  111. sizeof(interned_slice_refcount*) * capacity);
  112. for (i = 0; i < shard->capacity; i++) {
  113. for (s = shard->strs[i]; s; s = next) {
  114. size_t idx = TABLE_IDX(s->hash, capacity);
  115. next = s->bucket_next;
  116. s->bucket_next = strtab[idx];
  117. strtab[idx] = s;
  118. }
  119. }
  120. gpr_free(shard->strs);
  121. shard->strs = strtab;
  122. shard->capacity = capacity;
  123. GPR_TIMER_END("grow_strtab", 0);
  124. }
  125. static grpc_slice materialize(interned_slice_refcount* s) {
  126. grpc_slice slice;
  127. slice.refcount = &s->base;
  128. slice.data.refcounted.bytes = (uint8_t*)(s + 1);
  129. slice.data.refcounted.length = s->length;
  130. return slice;
  131. }
  132. uint32_t grpc_slice_default_hash_impl(grpc_slice s) {
  133. return gpr_murmur_hash3(GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s),
  134. g_hash_seed);
  135. }
  136. uint32_t grpc_static_slice_hash(grpc_slice s) {
  137. return static_metadata_hash_values[GRPC_STATIC_METADATA_INDEX(s)];
  138. }
  139. int grpc_static_slice_eq(grpc_slice a, grpc_slice b) {
  140. return GRPC_STATIC_METADATA_INDEX(a) == GRPC_STATIC_METADATA_INDEX(b);
  141. }
  142. uint32_t grpc_slice_hash(grpc_slice s) {
  143. return s.refcount == nullptr ? grpc_slice_default_hash_impl(s)
  144. : s.refcount->vtable->hash(s);
  145. }
  146. grpc_slice grpc_slice_maybe_static_intern(grpc_slice slice,
  147. bool* returned_slice_is_different) {
  148. if (GRPC_IS_STATIC_METADATA_STRING(slice)) {
  149. return slice;
  150. }
  151. uint32_t hash = grpc_slice_hash(slice);
  152. for (uint32_t i = 0; i <= max_static_metadata_hash_probe; i++) {
  153. static_metadata_hash_ent ent =
  154. static_metadata_hash[(hash + i) % GPR_ARRAY_SIZE(static_metadata_hash)];
  155. if (ent.hash == hash && ent.idx < GRPC_STATIC_MDSTR_COUNT &&
  156. grpc_slice_eq(grpc_static_slice_table[ent.idx], slice)) {
  157. *returned_slice_is_different = true;
  158. return grpc_static_slice_table[ent.idx];
  159. }
  160. }
  161. return slice;
  162. }
  163. bool grpc_slice_is_interned(grpc_slice slice) {
  164. return (slice.refcount && slice.refcount->vtable == &interned_slice_vtable) ||
  165. GRPC_IS_STATIC_METADATA_STRING(slice);
  166. }
  167. grpc_slice grpc_slice_intern(grpc_slice slice) {
  168. GPR_TIMER_BEGIN("grpc_slice_intern", 0);
  169. if (GRPC_IS_STATIC_METADATA_STRING(slice)) {
  170. GPR_TIMER_END("grpc_slice_intern", 0);
  171. return slice;
  172. }
  173. uint32_t hash = grpc_slice_hash(slice);
  174. for (uint32_t i = 0; i <= max_static_metadata_hash_probe; i++) {
  175. static_metadata_hash_ent ent =
  176. static_metadata_hash[(hash + i) % GPR_ARRAY_SIZE(static_metadata_hash)];
  177. if (ent.hash == hash && ent.idx < GRPC_STATIC_MDSTR_COUNT &&
  178. grpc_slice_eq(grpc_static_slice_table[ent.idx], slice)) {
  179. GPR_TIMER_END("grpc_slice_intern", 0);
  180. return grpc_static_slice_table[ent.idx];
  181. }
  182. }
  183. interned_slice_refcount* s;
  184. slice_shard* shard = &g_shards[SHARD_IDX(hash)];
  185. gpr_mu_lock(&shard->mu);
  186. /* search for an existing string */
  187. size_t idx = TABLE_IDX(hash, shard->capacity);
  188. for (s = shard->strs[idx]; s; s = s->bucket_next) {
  189. if (s->hash == hash && grpc_slice_eq(slice, materialize(s))) {
  190. if (gpr_atm_no_barrier_fetch_add(&s->refcnt, 1) == 0) {
  191. /* If we get here, we've added a ref to something that was about to
  192. * die - drop it immediately.
  193. * The *only* possible path here (given the shard mutex) should be to
  194. * drop from one ref back to zero - assert that with a CAS */
  195. GPR_ASSERT(gpr_atm_rel_cas(&s->refcnt, 1, 0));
  196. /* and treat this as if we were never here... sshhh */
  197. } else {
  198. gpr_mu_unlock(&shard->mu);
  199. GPR_TIMER_END("grpc_slice_intern", 0);
  200. return materialize(s);
  201. }
  202. }
  203. }
  204. /* not found: create a new string */
  205. /* string data goes after the internal_string header */
  206. s = (interned_slice_refcount*)gpr_malloc(sizeof(*s) +
  207. GRPC_SLICE_LENGTH(slice));
  208. gpr_atm_rel_store(&s->refcnt, 1);
  209. s->length = GRPC_SLICE_LENGTH(slice);
  210. s->hash = hash;
  211. s->base.vtable = &interned_slice_vtable;
  212. s->base.sub_refcount = &s->sub;
  213. s->sub.vtable = &interned_slice_sub_vtable;
  214. s->sub.sub_refcount = &s->sub;
  215. s->bucket_next = shard->strs[idx];
  216. shard->strs[idx] = s;
  217. memcpy(s + 1, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice));
  218. shard->count++;
  219. if (shard->count > shard->capacity * 2) {
  220. grow_shard(shard);
  221. }
  222. gpr_mu_unlock(&shard->mu);
  223. GPR_TIMER_END("grpc_slice_intern", 0);
  224. return materialize(s);
  225. }
  226. void grpc_test_only_set_slice_hash_seed(uint32_t seed) {
  227. g_hash_seed = seed;
  228. g_forced_hash_seed = 1;
  229. }
  230. void grpc_slice_intern_init(void) {
  231. if (!g_forced_hash_seed) {
  232. g_hash_seed = (uint32_t)gpr_now(GPR_CLOCK_REALTIME).tv_nsec;
  233. }
  234. for (size_t i = 0; i < SHARD_COUNT; i++) {
  235. slice_shard* shard = &g_shards[i];
  236. gpr_mu_init(&shard->mu);
  237. shard->count = 0;
  238. shard->capacity = INITIAL_SHARD_CAPACITY;
  239. shard->strs = (interned_slice_refcount**)gpr_zalloc(sizeof(*shard->strs) *
  240. shard->capacity);
  241. }
  242. for (size_t i = 0; i < GPR_ARRAY_SIZE(static_metadata_hash); i++) {
  243. static_metadata_hash[i].hash = 0;
  244. static_metadata_hash[i].idx = GRPC_STATIC_MDSTR_COUNT;
  245. }
  246. max_static_metadata_hash_probe = 0;
  247. for (size_t i = 0; i < GRPC_STATIC_MDSTR_COUNT; i++) {
  248. static_metadata_hash_values[i] =
  249. grpc_slice_default_hash_impl(grpc_static_slice_table[i]);
  250. for (size_t j = 0; j < GPR_ARRAY_SIZE(static_metadata_hash); j++) {
  251. size_t slot = (static_metadata_hash_values[i] + j) %
  252. GPR_ARRAY_SIZE(static_metadata_hash);
  253. if (static_metadata_hash[slot].idx == GRPC_STATIC_MDSTR_COUNT) {
  254. static_metadata_hash[slot].hash = static_metadata_hash_values[i];
  255. static_metadata_hash[slot].idx = (uint32_t)i;
  256. if (j > max_static_metadata_hash_probe) {
  257. max_static_metadata_hash_probe = (uint32_t)j;
  258. }
  259. break;
  260. }
  261. }
  262. }
  263. }
  264. void grpc_slice_intern_shutdown(void) {
  265. for (size_t i = 0; i < SHARD_COUNT; i++) {
  266. slice_shard* shard = &g_shards[i];
  267. gpr_mu_destroy(&shard->mu);
  268. /* TODO(ctiller): GPR_ASSERT(shard->count == 0); */
  269. if (shard->count != 0) {
  270. gpr_log(GPR_DEBUG, "WARNING: %" PRIuPTR " metadata strings were leaked",
  271. shard->count);
  272. for (size_t j = 0; j < shard->capacity; j++) {
  273. for (interned_slice_refcount* s = shard->strs[j]; s;
  274. s = s->bucket_next) {
  275. char* text =
  276. grpc_dump_slice(materialize(s), GPR_DUMP_HEX | GPR_DUMP_ASCII);
  277. gpr_log(GPR_DEBUG, "LEAKED: %s", text);
  278. gpr_free(text);
  279. }
  280. }
  281. if (grpc_iomgr_abort_on_leaks()) {
  282. abort();
  283. }
  284. }
  285. gpr_free(shard->strs);
  286. }
  287. }