File: cache.c

package info (click to toggle)
bind9 1%3A9.21.18-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 39,888 kB
  • sloc: ansic: 297,806; sh: 42,003; python: 27,121; perl: 2,833; makefile: 86
file content (740 lines) | stat: -rw-r--r-- 18,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*! \file */

#include <inttypes.h>
#include <stdbool.h>

#include <isc/log.h>
#include <isc/loop.h>
#include <isc/mem.h>
#include <isc/refcount.h>
#include <isc/result.h>
#include <isc/stats.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/timer.h>
#include <isc/util.h>

#include <dns/cache.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/masterdump.h>
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include <dns/stats.h>

#ifdef HAVE_JSON_C
#include <json_object.h>
#endif /* HAVE_JSON_C */

#ifdef HAVE_LIBXML2
#include <libxml/xmlwriter.h>
#define ISC_XMLCHAR (const xmlChar *)
#endif /* HAVE_LIBXML2 */

#define CACHE_MAGIC	   ISC_MAGIC('$', '$', '$', '$')
#define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)

/*
 * DNS_CACHE_MINSIZE is how many bytes is the floor for
 * dns_cache_setcachesize().
 */
#define DNS_CACHE_MINSIZE 2097152U /*%< Bytes.  2097152 = 2 MB */

/***
 ***	Types
 ***/

/*%
 * The actual cache object.
 */

struct dns_cache {
	/* Unlocked. */
	unsigned int magic;
	isc_mutex_t lock;
	isc_mem_t *mctx;  /* Memory context for the dns_cache object */
	isc_mem_t *hmctx; /* Heap memory */
	isc_mem_t *tmctx; /* Tree memory */
	char *name;
	isc_refcount_t references;

	/* Locked by 'lock'. */
	dns_rdataclass_t rdclass;
	dns_db_t *db;
	size_t size;
	dns_ttl_t serve_stale_ttl;
	dns_ttl_t serve_stale_refresh;
	isc_stats_t *stats;
	uint32_t maxrrperset;
	uint32_t maxtypepername;
};

/***
 ***	Functions
 ***/

static isc_result_t
cache_create_db(dns_cache_t *cache, dns_db_t **dbp, isc_mem_t **tmctxp,
		isc_mem_t **hmctxp) {
	isc_result_t result;
	char *argv[1] = { 0 };
	dns_db_t *db = NULL;
	isc_mem_t *tmctx = NULL, *hmctx = NULL;

	/*
	 * This will be the cache memory context, which is subject
	 * to cleaning when the configured memory limits are exceeded.
	 */
	isc_mem_create("cache", &tmctx);

	/*
	 * This will be passed to RBTDB to use for heaps. This is separate
	 * from the main cache memory because it can grow quite large under
	 * heavy load and could otherwise cause the cache to be cleaned too
	 * aggressively.
	 */
	isc_mem_create("cache_heap", &hmctx);

	/*
	 * For databases of type "qpcache" or "rbt" (which are the
	 * only cache implementations currently in existence) we pass
	 * hmctx to dns_db_create() via argv[0].
	 */
	argv[0] = (char *)hmctx;
	result = dns_db_create(tmctx, CACHEDB_DEFAULT, dns_rootname,
			       dns_dbtype_cache, cache->rdclass, 1, argv, &db);
	if (result != ISC_R_SUCCESS) {
		goto cleanup_mctx;
	}
	result = dns_db_setcachestats(db, cache->stats);
	if (result != ISC_R_SUCCESS) {
		goto cleanup_db;
	}

	dns_db_setservestalettl(db, cache->serve_stale_ttl);
	dns_db_setservestalerefresh(db, cache->serve_stale_refresh);
	dns_db_setmaxrrperset(db, cache->maxrrperset);
	dns_db_setmaxtypepername(db, cache->maxtypepername);

	*dbp = db;
	*hmctxp = hmctx;
	*tmctxp = tmctx;

	return ISC_R_SUCCESS;

cleanup_db:
	dns_db_detach(&db);
cleanup_mctx:
	isc_mem_detach(&hmctx);
	isc_mem_detach(&tmctx);

	return result;
}

static void
cache_destroy(dns_cache_t *cache) {
	isc_stats_detach(&cache->stats);
	isc_mutex_destroy(&cache->lock);
	isc_mem_free(cache->mctx, cache->name);
	if (cache->hmctx != NULL) {
		isc_mem_detach(&cache->hmctx);
	}
	if (cache->tmctx != NULL) {
		isc_mem_detach(&cache->tmctx);
	}
	isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
}

isc_result_t
dns_cache_create(dns_rdataclass_t rdclass, const char *cachename,
		 isc_mem_t *mctx, dns_cache_t **cachep) {
	isc_result_t result;
	dns_cache_t *cache = NULL;

	REQUIRE(cachename != NULL);
	REQUIRE(cachep != NULL && *cachep == NULL);

	cache = isc_mem_get(mctx, sizeof(*cache));
	*cache = (dns_cache_t){
		.rdclass = rdclass,
		.name = isc_mem_strdup(mctx, cachename),
		.references = ISC_REFCOUNT_INITIALIZER(1),
		.magic = CACHE_MAGIC,
	};

	isc_mutex_init(&cache->lock);
	isc_mem_attach(mctx, &cache->mctx);

	isc_stats_create(mctx, &cache->stats, dns_cachestatscounter_max);

	/*
	 * Create the database
	 */
	CHECK(cache_create_db(cache, &cache->db, &cache->tmctx, &cache->hmctx));

	*cachep = cache;
	return ISC_R_SUCCESS;

cleanup:
	cache_destroy(cache);
	return result;
}

static void
cache_cleanup(dns_cache_t *cache) {
	REQUIRE(VALID_CACHE(cache));

	isc_refcount_destroy(&cache->references);
	cache->magic = 0;

	isc_mem_clearwater(cache->tmctx);
	dns_db_detach(&cache->db);

	cache_destroy(cache);
}

#if DNS_CACHE_TRACE
ISC_REFCOUNT_TRACE_IMPL(dns_cache, cache_cleanup);
#else
ISC_REFCOUNT_IMPL(dns_cache, cache_cleanup);
#endif

void
dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
	REQUIRE(VALID_CACHE(cache));
	REQUIRE(dbp != NULL && *dbp == NULL);
	REQUIRE(cache->db != NULL);

	LOCK(&cache->lock);
	dns_db_attach(cache->db, dbp);
	UNLOCK(&cache->lock);
}

const char *
dns_cache_getname(dns_cache_t *cache) {
	REQUIRE(VALID_CACHE(cache));

	return cache->name;
}

static void
updatewater(dns_cache_t *cache) {
	size_t hi = cache->size - (cache->size >> 3); /* ~ 7/8ths. */
	size_t lo = cache->size - (cache->size >> 2); /* ~ 3/4ths. */
	if (cache->size == 0U || hi == 0U || lo == 0U) {
		isc_mem_clearwater(cache->tmctx);
	} else {
		isc_mem_setwater(cache->tmctx, hi, lo);
	}
}

void
dns_cache_setcachesize(dns_cache_t *cache, size_t size) {
	REQUIRE(VALID_CACHE(cache));

	/*
	 * Impose a minimum cache size; pathological things happen if there
	 * is too little room.
	 */
	if (size != 0U && size < DNS_CACHE_MINSIZE) {
		size = DNS_CACHE_MINSIZE;
	}

	LOCK(&cache->lock);
	cache->size = size;
	updatewater(cache);
	UNLOCK(&cache->lock);
}

size_t
dns_cache_getcachesize(dns_cache_t *cache) {
	size_t size;

	REQUIRE(VALID_CACHE(cache));

	LOCK(&cache->lock);
	size = cache->size;
	UNLOCK(&cache->lock);

	return size;
}

void
dns_cache_setservestalettl(dns_cache_t *cache, dns_ttl_t ttl) {
	REQUIRE(VALID_CACHE(cache));

	LOCK(&cache->lock);
	cache->serve_stale_ttl = ttl;
	UNLOCK(&cache->lock);

	(void)dns_db_setservestalettl(cache->db, ttl);
}

dns_ttl_t
dns_cache_getservestalettl(dns_cache_t *cache) {
	dns_ttl_t ttl;
	isc_result_t result;

	REQUIRE(VALID_CACHE(cache));

	/*
	 * Could get it straight from the dns_cache_t, but use db
	 * to confirm the value that the db is really using.
	 */
	result = dns_db_getservestalettl(cache->db, &ttl);
	return result == ISC_R_SUCCESS ? ttl : 0;
}

void
dns_cache_setservestalerefresh(dns_cache_t *cache, dns_ttl_t interval) {
	REQUIRE(VALID_CACHE(cache));

	LOCK(&cache->lock);
	cache->serve_stale_refresh = interval;
	UNLOCK(&cache->lock);

	(void)dns_db_setservestalerefresh(cache->db, interval);
}

dns_ttl_t
dns_cache_getservestalerefresh(dns_cache_t *cache) {
	isc_result_t result;
	dns_ttl_t interval;

	REQUIRE(VALID_CACHE(cache));

	result = dns_db_getservestalerefresh(cache->db, &interval);
	return result == ISC_R_SUCCESS ? interval : 0;
}

isc_result_t
dns_cache_flush(dns_cache_t *cache) {
	dns_db_t *db = NULL, *olddb = NULL;
	isc_mem_t *tmctx = NULL, *oldtmctx = NULL;
	isc_mem_t *hmctx = NULL, *oldhmctx = NULL;

	RETERR(cache_create_db(cache, &db, &tmctx, &hmctx));

	LOCK(&cache->lock);
	isc_mem_clearwater(cache->tmctx);
	oldhmctx = cache->hmctx;
	cache->hmctx = hmctx;
	oldtmctx = cache->tmctx;
	cache->tmctx = tmctx;
	updatewater(cache);
	olddb = cache->db;
	cache->db = db;
	UNLOCK(&cache->lock);

	dns_db_detach(&olddb);
	isc_mem_detach(&oldhmctx);
	isc_mem_detach(&oldtmctx);

	return ISC_R_SUCCESS;
}

static isc_result_t
clearnode(dns_db_t *db, dns_dbnode_t *node) {
	dns_rdatasetiter_t *iter = NULL;

	RETERR(dns_db_allrdatasets(db, node, NULL, DNS_DB_STALEOK,
				   (isc_stdtime_t)0, &iter));

	DNS_RDATASETITER_FOREACH(iter) {
		isc_result_t result;
		dns_rdataset_t rdataset = DNS_RDATASET_INIT;

		dns_rdatasetiter_current(iter, &rdataset);
		result = dns_db_deleterdataset(db, node, NULL, rdataset.type,
					       rdataset.covers);
		dns_rdataset_disassociate(&rdataset);
		if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED) {
			break;
		}
	}

	dns_rdatasetiter_destroy(&iter);
	return ISC_R_SUCCESS;
}

static isc_result_t
cleartree(dns_db_t *db, const dns_name_t *name) {
	isc_result_t result, answer = ISC_R_SUCCESS;
	dns_dbiterator_t *iter = NULL;
	dns_dbnode_t *node = NULL, *top = NULL;
	dns_fixedname_t fnodename;
	dns_name_t *nodename;

	/*
	 * Create the node if it doesn't exist so dns_dbiterator_seek()
	 * can find it.  We will continue even if this fails.
	 */
	(void)dns_db_findnode(db, name, true, &top);

	nodename = dns_fixedname_initname(&fnodename);

	CHECK(dns_db_createiterator(db, 0, &iter));

	result = dns_dbiterator_seek(iter, name);
	if (result == DNS_R_PARTIALMATCH) {
		result = dns_dbiterator_next(iter);
	}
	if (result != ISC_R_SUCCESS) {
		goto cleanup;
	}

	while (result == ISC_R_SUCCESS) {
		result = dns_dbiterator_current(iter, &node, nodename);
		if (result == DNS_R_NEWORIGIN) {
			result = ISC_R_SUCCESS;
		}
		if (result != ISC_R_SUCCESS) {
			goto cleanup;
		}
		/*
		 * Are we done?
		 */
		if (!dns_name_issubdomain(nodename, name)) {
			goto cleanup;
		}

		/*
		 * If clearnode fails record and move onto the next node.
		 */
		result = clearnode(db, node);
		if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS) {
			answer = result;
		}
		dns_db_detachnode(&node);
		result = dns_dbiterator_next(iter);
	}

cleanup:
	if (result == ISC_R_NOMORE || result == ISC_R_NOTFOUND) {
		result = ISC_R_SUCCESS;
	}
	if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS) {
		answer = result;
	}
	if (node != NULL) {
		dns_db_detachnode(&node);
	}
	if (iter != NULL) {
		dns_dbiterator_destroy(&iter);
	}
	if (top != NULL) {
		dns_db_detachnode(&top);
	}

	return answer;
}

isc_result_t
dns_cache_flushname(dns_cache_t *cache, const dns_name_t *name) {
	return dns_cache_flushnode(cache, name, false);
}

isc_result_t
dns_cache_flushnode(dns_cache_t *cache, const dns_name_t *name, bool tree) {
	isc_result_t result;
	dns_dbnode_t *node = NULL;
	dns_db_t *db = NULL;

	if (tree && dns_name_equal(name, dns_rootname)) {
		return dns_cache_flush(cache);
	}

	LOCK(&cache->lock);
	if (cache->db != NULL) {
		dns_db_attach(cache->db, &db);
	}
	UNLOCK(&cache->lock);
	if (db == NULL) {
		return ISC_R_SUCCESS;
	}

	if (tree) {
		result = cleartree(cache->db, name);
	} else {
		result = dns_db_findnode(cache->db, name, false, &node);
		if (result == ISC_R_NOTFOUND) {
			result = ISC_R_SUCCESS;
			goto cleanup_db;
		}
		if (result != ISC_R_SUCCESS) {
			goto cleanup_db;
		}
		result = clearnode(cache->db, node);
		dns_db_detachnode(&node);
	}

cleanup_db:
	dns_db_detach(&db);
	return result;
}

isc_stats_t *
dns_cache_getstats(dns_cache_t *cache) {
	REQUIRE(VALID_CACHE(cache));
	return cache->stats;
}

void
dns_cache_updatestats(dns_cache_t *cache, isc_result_t result) {
	REQUIRE(VALID_CACHE(cache));
	if (cache->stats == NULL) {
		return;
	}

	switch (result) {
	case ISC_R_SUCCESS:
	case DNS_R_NCACHENXDOMAIN:
	case DNS_R_NCACHENXRRSET:
	case DNS_R_CNAME:
	case DNS_R_DNAME:
	case DNS_R_GLUE:
	case DNS_R_ZONECUT:
	case DNS_R_COVERINGNSEC:
		isc_stats_increment(cache->stats,
				    dns_cachestatscounter_queryhits);
		break;
	default:
		isc_stats_increment(cache->stats,
				    dns_cachestatscounter_querymisses);
	}
}

void
dns_cache_setmaxrrperset(dns_cache_t *cache, uint32_t value) {
	REQUIRE(VALID_CACHE(cache));

	cache->maxrrperset = value;
	if (cache->db != NULL) {
		dns_db_setmaxrrperset(cache->db, value);
	}
}

void
dns_cache_setmaxtypepername(dns_cache_t *cache, uint32_t value) {
	REQUIRE(VALID_CACHE(cache));

	cache->maxtypepername = value;
	if (cache->db != NULL) {
		dns_db_setmaxtypepername(cache->db, value);
	}
}

/*
 * XXX: Much of the following code has been copied in from statschannel.c.
 * We should refactor this into a generic function in stats.c that can be
 * called from both places.
 */
typedef struct cache_dumparg {
	isc_statsformat_t type;
	void *arg;		 /* type dependent argument */
	int ncounters;		 /* for general statistics */
	int *counterindices;	 /* for general statistics */
	uint64_t *countervalues; /* for general statistics */
	isc_result_t result;
} cache_dumparg_t;

static void
getcounter(isc_statscounter_t counter, uint64_t val, void *arg) {
	cache_dumparg_t *dumparg = arg;

	REQUIRE(counter < dumparg->ncounters);
	dumparg->countervalues[counter] = val;
}

static void
getcounters(isc_stats_t *stats, isc_statsformat_t type, int ncounters,
	    int *indices, uint64_t *values) {
	cache_dumparg_t dumparg;

	memset(values, 0, sizeof(values[0]) * ncounters);

	dumparg.type = type;
	dumparg.ncounters = ncounters;
	dumparg.counterindices = indices;
	dumparg.countervalues = values;

	isc_stats_dump(stats, getcounter, &dumparg, ISC_STATSDUMP_VERBOSE);
}

void
dns_cache_dumpstats(dns_cache_t *cache, FILE *fp) {
	int indices[dns_cachestatscounter_max];
	uint64_t values[dns_cachestatscounter_max];

	REQUIRE(VALID_CACHE(cache));

	getcounters(cache->stats, isc_statsformat_file,
		    dns_cachestatscounter_max, indices, values);

	fprintf(fp, "%20" PRIu64 " %s\n", values[dns_cachestatscounter_hits],
		"cache hits");
	fprintf(fp, "%20" PRIu64 " %s\n", values[dns_cachestatscounter_misses],
		"cache misses");
	fprintf(fp, "%20" PRIu64 " %s\n",
		values[dns_cachestatscounter_queryhits],
		"cache hits (from query)");
	fprintf(fp, "%20" PRIu64 " %s\n",
		values[dns_cachestatscounter_querymisses],
		"cache misses (from query)");
	fprintf(fp, "%20" PRIu64 " %s\n",
		values[dns_cachestatscounter_deletelru],
		"cache records deleted due to memory exhaustion");
	fprintf(fp, "%20" PRIu64 " %s\n",
		values[dns_cachestatscounter_deletettl],
		"cache records deleted due to TTL expiration");
	fprintf(fp, "%20" PRIu64 " %s\n",
		values[dns_cachestatscounter_coveringnsec],
		"covering nsec returned");
	fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db),
		"cache database nodes");

	fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->tmctx),
		"cache tree memory in use");

	fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->hmctx),
		"cache heap memory in use");
}

#ifdef HAVE_LIBXML2
#define TRY0(a)                     \
	do {                        \
		xmlrc = (a);        \
		if (xmlrc < 0)      \
			goto error; \
	} while (0)
static int
renderstat(const char *name, uint64_t value, xmlTextWriterPtr writer) {
	int xmlrc;

	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
					 ISC_XMLCHAR name));
	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "", value));
	TRY0(xmlTextWriterEndElement(writer)); /* counter */

error:
	return xmlrc;
}

int
dns_cache_renderxml(dns_cache_t *cache, void *writer0) {
	int indices[dns_cachestatscounter_max];
	uint64_t values[dns_cachestatscounter_max];
	int xmlrc;
	xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;

	REQUIRE(VALID_CACHE(cache));

	getcounters(cache->stats, isc_statsformat_file,
		    dns_cachestatscounter_max, indices, values);
	TRY0(renderstat("CacheHits", values[dns_cachestatscounter_hits],
			writer));
	TRY0(renderstat("CacheMisses", values[dns_cachestatscounter_misses],
			writer));
	TRY0(renderstat("QueryHits", values[dns_cachestatscounter_queryhits],
			writer));
	TRY0(renderstat("QueryMisses",
			values[dns_cachestatscounter_querymisses], writer));
	TRY0(renderstat("DeleteLRU", values[dns_cachestatscounter_deletelru],
			writer));
	TRY0(renderstat("DeleteTTL", values[dns_cachestatscounter_deletettl],
			writer));
	TRY0(renderstat("CoveringNSEC",
			values[dns_cachestatscounter_coveringnsec], writer));

	TRY0(renderstat("CacheNodes", dns_db_nodecount(cache->db), writer));

	TRY0(renderstat("TreeMemInUse", isc_mem_inuse(cache->tmctx), writer));

	TRY0(renderstat("HeapMemInUse", isc_mem_inuse(cache->hmctx), writer));
error:
	return xmlrc;
}
#endif /* ifdef HAVE_LIBXML2 */

#ifdef HAVE_JSON_C
#define CHECKMEM(m)                              \
	do {                                     \
		if (m == NULL) {                 \
			result = ISC_R_NOMEMORY; \
			goto error;              \
		}                                \
	} while (0)

isc_result_t
dns_cache_renderjson(dns_cache_t *cache, void *cstats0) {
	isc_result_t result = ISC_R_SUCCESS;
	int indices[dns_cachestatscounter_max];
	uint64_t values[dns_cachestatscounter_max];
	json_object *obj;
	json_object *cstats = (json_object *)cstats0;

	REQUIRE(VALID_CACHE(cache));

	getcounters(cache->stats, isc_statsformat_file,
		    dns_cachestatscounter_max, indices, values);

	obj = json_object_new_int64(values[dns_cachestatscounter_hits]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "CacheHits", obj);

	obj = json_object_new_int64(values[dns_cachestatscounter_misses]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "CacheMisses", obj);

	obj = json_object_new_int64(values[dns_cachestatscounter_queryhits]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "QueryHits", obj);

	obj = json_object_new_int64(values[dns_cachestatscounter_querymisses]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "QueryMisses", obj);

	obj = json_object_new_int64(values[dns_cachestatscounter_deletelru]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "DeleteLRU", obj);

	obj = json_object_new_int64(values[dns_cachestatscounter_deletettl]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "DeleteTTL", obj);

	obj = json_object_new_int64(values[dns_cachestatscounter_coveringnsec]);
	CHECKMEM(obj);
	json_object_object_add(cstats, "CoveringNSEC", obj);

	obj = json_object_new_int64(dns_db_nodecount(cache->db));
	CHECKMEM(obj);
	json_object_object_add(cstats, "CacheNodes", obj);

	obj = json_object_new_int64(isc_mem_inuse(cache->tmctx));
	CHECKMEM(obj);
	json_object_object_add(cstats, "TreeMemInUse", obj);

	obj = json_object_new_int64(isc_mem_inuse(cache->hmctx));
	CHECKMEM(obj);
	json_object_object_add(cstats, "HeapMemInUse", obj);

	result = ISC_R_SUCCESS;
error:
	return result;
}
#endif /* ifdef HAVE_JSON_C */