Lines Matching defs:cache

1 /*	$NetBSD: cache.c,v 1.13 2025/01/26 16:25:22 christos Exp $	*/
31 #include <dns/cache.h>
51 #define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
64 * The actual cache object.
94 cache_create_db(dns_cache_t *cache, dns_db_t **dbp, isc_mem_t **tmctxp,
102 * This will be the cache memory context, which is subject
106 isc_mem_setname(tmctx, "cache");
110 * from the main cache memory because it can grow quite large under
111 * heavy load and could otherwise cause the cache to be cleaned too
119 * only cache implementations currently in existence) we pass
124 dns_dbtype_cache, cache->rdclass, 1, argv, &db);
128 result = dns_db_setcachestats(db, cache->stats);
133 dns_db_setservestalettl(db, cache->serve_stale_ttl);
134 dns_db_setservestalerefresh(db, cache->serve_stale_refresh);
135 dns_db_setmaxrrperset(db, cache->maxrrperset);
136 dns_db_setmaxtypepername(db, cache->maxtypepername);
139 * XXX this is only used by the RBT cache, and can
142 dns_db_setloop(db, isc_loop_main(cache->loopmgr));
160 cache_destroy(dns_cache_t *cache) {
161 isc_stats_detach(&cache->stats);
162 isc_mutex_destroy(&cache->lock);
163 isc_mem_free(cache->mctx, cache->name);
164 if (cache->hmctx != NULL) {
165 isc_mem_detach(&cache->hmctx);
167 if (cache->tmctx != NULL) {
168 isc_mem_detach(&cache->tmctx);
170 isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
177 dns_cache_t *cache = NULL;
183 cache = isc_mem_get(mctx, sizeof(*cache));
184 *cache = (dns_cache_t){
192 isc_mutex_init(&cache->lock);
193 isc_mem_attach(mctx, &cache->mctx);
195 isc_stats_create(mctx, &cache->stats, dns_cachestatscounter_max);
200 result = cache_create_db(cache, &cache->db, &cache->tmctx,
201 &cache->hmctx);
206 *cachep = cache;
210 cache_destroy(cache);
215 cache_cleanup(dns_cache_t *cache) {
216 REQUIRE(VALID_CACHE(cache));
218 isc_refcount_destroy(&cache->references);
219 cache->magic = 0;
221 isc_mem_clearwater(cache->tmctx);
222 dns_db_detach(&cache->db);
224 cache_destroy(cache);
234 dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
235 REQUIRE(VALID_CACHE(cache));
237 REQUIRE(cache->db != NULL);
239 LOCK(&cache->lock);
240 dns_db_attach(cache->db, dbp);
241 UNLOCK(&cache->lock);
245 dns_cache_getname(dns_cache_t *cache) {
246 REQUIRE(VALID_CACHE(cache));
248 return cache->name;
252 updatewater(dns_cache_t *cache) {
253 size_t hi = cache->size - (cache->size >> 3); /* ~ 7/8ths. */
254 size_t lo = cache->size - (cache->size >> 2); /* ~ 3/4ths. */
255 if (cache->size == 0U || hi == 0U || lo == 0U) {
256 isc_mem_clearwater(cache->tmctx);
258 isc_mem_setwater(cache->tmctx, hi, lo);
263 dns_cache_setcachesize(dns_cache_t *cache, size_t size) {
264 REQUIRE(VALID_CACHE(cache));
267 * Impose a minimum cache size; pathological things happen if there
274 LOCK(&cache->lock);
275 cache->size = size;
276 updatewater(cache);
277 UNLOCK(&cache->lock);
281 dns_cache_getcachesize(dns_cache_t *cache) {
284 REQUIRE(VALID_CACHE(cache));
286 LOCK(&cache->lock);
287 size = cache->size;
288 UNLOCK(&cache->lock);
294 dns_cache_setservestalettl(dns_cache_t *cache, dns_ttl_t ttl) {
295 REQUIRE(VALID_CACHE(cache));
297 LOCK(&cache->lock);
298 cache->serve_stale_ttl = ttl;
299 UNLOCK(&cache->lock);
301 (void)dns_db_setservestalettl(cache->db, ttl);
305 dns_cache_getservestalettl(dns_cache_t *cache) {
309 REQUIRE(VALID_CACHE(cache));
315 result = dns_db_getservestalettl(cache->db, &ttl);
320 dns_cache_setservestalerefresh(dns_cache_t *cache, dns_ttl_t interval) {
321 REQUIRE(VALID_CACHE(cache));
323 LOCK(&cache->lock);
324 cache->serve_stale_refresh = interval;
325 UNLOCK(&cache->lock);
327 (void)dns_db_setservestalerefresh(cache->db, interval);
331 dns_cache_getservestalerefresh(dns_cache_t *cache) {
335 REQUIRE(VALID_CACHE(cache));
337 result = dns_db_getservestalerefresh(cache->db, &interval);
342 dns_cache_flush(dns_cache_t *cache) {
348 result = cache_create_db(cache, &db, &tmctx, &hmctx);
353 LOCK(&cache->lock);
354 isc_mem_clearwater(cache->tmctx);
355 oldhmctx = cache->hmctx;
356 cache->hmctx = hmctx;
357 oldtmctx = cache->tmctx;
358 cache->tmctx = tmctx;
359 updatewater(cache);
360 olddb = cache->db;
361 cache->db = db;
362 UNLOCK(&cache->lock);
481 dns_cache_flushname(dns_cache_t *cache, const dns_name_t *name) {
482 return dns_cache_flushnode(cache, name, false);
486 dns_cache_flushnode(dns_cache_t *cache, const dns_name_t *name, bool tree) {
492 return dns_cache_flush(cache);
495 LOCK(&cache->lock);
496 if (cache->db != NULL) {
497 dns_db_attach(cache->db, &db);
499 UNLOCK(&cache->lock);
505 result = cleartree(cache->db, name);
507 result = dns_db_findnode(cache->db, name, false, &node);
515 result = clearnode(cache->db, node);
516 dns_db_detachnode(cache->db, &node);
525 dns_cache_getstats(dns_cache_t *cache) {
526 REQUIRE(VALID_CACHE(cache));
527 return cache->stats;
531 dns_cache_updatestats(dns_cache_t *cache, isc_result_t result) {
532 REQUIRE(VALID_CACHE(cache));
533 if (cache->stats == NULL) {
546 isc_stats_increment(cache->stats,
550 isc_stats_increment(cache->stats,
556 dns_cache_setmaxrrperset(dns_cache_t *cache, uint32_t value) {
557 REQUIRE(VALID_CACHE(cache));
559 cache->maxrrperset = value;
560 if (cache->db != NULL) {
561 dns_db_setmaxrrperset(cache->db, value);
566 dns_cache_setmaxtypepername(dns_cache_t *cache, uint32_t value) {
567 REQUIRE(VALID_CACHE(cache));
569 cache->maxtypepername = value;
570 if (cache->db != NULL) {
571 dns_db_setmaxtypepername(cache->db, value);
613 dns_cache_dumpstats(dns_cache_t *cache, FILE *fp) {
617 REQUIRE(VALID_CACHE(cache));
619 getcounters(cache->stats, isc_statsformat_file,
623 "cache hits");
625 "cache misses");
628 "cache hits (from query)");
631 "cache misses (from query)");
634 "cache records deleted due to memory exhaustion");
637 "cache records deleted due to TTL expiration");
641 fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db, dns_dbtree_main),
642 "cache database nodes");
643 fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db, dns_dbtree_nsec),
644 "cache NSEC auxiliary database nodes");
645 fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)dns_db_hashsize(cache->db),
646 "cache database hash buckets");
648 fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->tmctx),
649 "cache tree memory in use");
651 fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->hmctx),
652 "cache heap memory in use");
677 dns_cache_renderxml(dns_cache_t *cache, void *writer0) {
683 REQUIRE(VALID_CACHE(cache));
685 getcounters(cache->stats, isc_statsformat_file,
703 dns_db_nodecount(cache->db, dns_dbtree_main), writer));
705 dns_db_nodecount(cache->db, dns_dbtree_nsec), writer));
706 TRY0(renderstat("CacheBuckets", dns_db_hashsize(cache->db), writer));
708 TRY0(renderstat("TreeMemInUse", isc_mem_inuse(cache->tmctx), writer));
710 TRY0(renderstat("HeapMemInUse", isc_mem_inuse(cache->hmctx), writer));
726 dns_cache_renderjson(dns_cache_t *cache, void *cstats0) {
733 REQUIRE(VALID_CACHE(cache));
735 getcounters(cache->stats, isc_statsformat_file,
767 dns_db_nodecount(cache->db, dns_dbtree_main));
772 dns_db_nodecount(cache->db, dns_dbtree_nsec));
776 obj = json_object_new_int64(dns_db_hashsize(cache->db));
780 obj = json_object_new_int64(isc_mem_inuse(cache->tmctx));
784 obj = json_object_new_int64(isc_mem_inuse(cache->hmctx));