| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <stdlib.h> |
| 16 |
|
| 17 |
#include <isc/commandline.h> |
| 18 |
#include <isc/file.h> |
| 19 |
#include <isc/ht.h> |
| 20 |
#include <isc/rwlock.h> |
| 21 |
#include <isc/util.h> |
| 22 |
|
| 23 |
#include <dns/fixedname.h> |
| 24 |
#include <dns/qp.h> |
| 25 |
#include <dns/rbt.h> |
| 26 |
#include <dns/types.h> |
| 27 |
|
| 28 |
#include <tests/dns.h> |
| 29 |
#include <tests/qp.h> |
| 30 |
|
| 31 |
static inline size_t |
| 32 |
smallname_length(void *pval, uint32_t ival) { |
| 33 |
UNUSED(pval); |
| 34 |
return ival & 0xff; |
| 35 |
} |
| 36 |
|
| 37 |
static inline size_t |
| 38 |
smallname_labels(void *pval, uint32_t ival) { |
| 39 |
UNUSED(pval); |
| 40 |
return ival >> 8; |
| 41 |
} |
| 42 |
|
| 43 |
static inline isc_refcount_t * |
| 44 |
smallname_refcount(void *pval, uint32_t ival) { |
| 45 |
UNUSED(ival); |
| 46 |
return pval; |
| 47 |
} |
| 48 |
|
| 49 |
static inline uint8_t * |
| 50 |
smallname_ndata(void *pval, uint32_t ival) { |
| 51 |
return (uint8_t *)(smallname_refcount(pval, ival) + 1); |
| 52 |
} |
| 53 |
|
| 54 |
static inline uint8_t * |
| 55 |
smallname_offsets(void *pval, uint32_t ival) { |
| 56 |
return smallname_ndata(pval, ival) + smallname_length(pval, ival); |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
smallname_from_name(const dns_name_t *name, void **valp, uint32_t *ctxp) { |
| 61 |
size_t size = sizeof(isc_refcount_t) + name->length + name->labels; |
| 62 |
*valp = isc_mem_get(mctx, size); |
| 63 |
*ctxp = name->labels << 8 | name->length; |
| 64 |
isc_refcount_init(smallname_refcount(*valp, *ctxp), 0); |
| 65 |
memmove(smallname_ndata(*valp, *ctxp), name->ndata, name->length); |
| 66 |
memmove(smallname_offsets(*valp, *ctxp), name->offsets, name->labels); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
smallname_free(void *pval, uint32_t ival) { |
| 71 |
size_t size = sizeof(isc_refcount_t); |
| 72 |
size += smallname_length(pval, ival) + smallname_labels(pval, ival); |
| 73 |
isc_mem_put(mctx, pval, size); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
name_from_smallname(dns_name_t *name, void *pval, uint32_t ival) { |
| 78 |
dns_name_reset(name); |
| 79 |
name->ndata = smallname_ndata(pval, ival); |
| 80 |
name->length = smallname_length(pval, ival); |
| 81 |
name->labels = smallname_labels(pval, ival); |
| 82 |
name->offsets = smallname_offsets(pval, ival); |
| 83 |
name->attributes.readonly = true; |
| 84 |
if (name->ndata[name->offsets[name->labels - 1]] == '\0') { |
| 85 |
name->attributes.absolute = true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
static size_t |
| 90 |
qpkey_from_smallname(dns_qpkey_t key, void *ctx, void *pval, uint32_t ival) { |
| 91 |
UNUSED(ctx); |
| 92 |
dns_name_t name = DNS_NAME_INITEMPTY; |
| 93 |
name_from_smallname(&name, pval, ival); |
| 94 |
return dns_qpkey_fromname(key, &name); |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
smallname_attach(void *ctx, void *pval, uint32_t ival) { |
| 99 |
UNUSED(ctx); |
| 100 |
isc_refcount_increment0(smallname_refcount(pval, ival)); |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
smallname_detach(void *ctx, void *pval, uint32_t ival) { |
| 105 |
if (isc_refcount_decrement(smallname_refcount(pval, ival)) == 1) { |
| 106 |
isc_mem_free(ctx, pval); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
testname(void *ctx, char *buf, size_t size) { |
| 112 |
REQUIRE(ctx == NULL); |
| 113 |
strlcpy(buf, "test", size); |
| 114 |
} |
| 115 |
|
| 116 |
const dns_qpmethods_t methods = { |
| 117 |
smallname_attach, |
| 118 |
smallname_detach, |
| 119 |
qpkey_from_smallname, |
| 120 |
testname, |
| 121 |
}; |
| 122 |
|
| 123 |
static void |
| 124 |
usage(void) { |
| 125 |
fprintf(stderr, |
| 126 |
"usage: qp_dump [-dt] <filename>\n" |
| 127 |
" -d output in graphviz dot format\n" |
| 128 |
" -t output in ad-hoc indented text format\n"); |
| 129 |
} |
| 130 |
|
| 131 |
int |
| 132 |
main(int argc, char *argv[]) { |
| 133 |
isc_result_t result; |
| 134 |
dns_qp_t *qp = NULL; |
| 135 |
const char *filename = NULL; |
| 136 |
char *filetext = NULL; |
| 137 |
size_t filesize; |
| 138 |
off_t fileoff; |
| 139 |
FILE *fp = NULL; |
| 140 |
size_t wirebytes = 0, labels = 0, names = 0; |
| 141 |
char *pos = NULL, *file_end = NULL; |
| 142 |
bool dumpdot = false, dumptxt = false; |
| 143 |
int opt; |
| 144 |
|
| 145 |
while ((opt = isc_commandline_parse(argc, argv, "dt")) != -1) { |
| 146 |
switch (opt) { |
| 147 |
case 'd': |
| 148 |
dumpdot = true; |
| 149 |
continue; |
| 150 |
case 't': |
| 151 |
dumptxt = true; |
| 152 |
continue; |
| 153 |
default: |
| 154 |
usage(); |
| 155 |
exit(EXIT_FAILURE); |
| 156 |
continue; |
| 157 |
} |
| 158 |
} |
| 159 |
argc -= isc_commandline_index; |
| 160 |
argv += isc_commandline_index; |
| 161 |
|
| 162 |
if (argc != 1) { |
| 163 |
/* must exit 0 to appease test runner */ |
| 164 |
usage(); |
| 165 |
exit(EXIT_SUCCESS); |
| 166 |
} |
| 167 |
|
| 168 |
isc_mem_create(&mctx); |
| 169 |
|
| 170 |
filename = argv[0]; |
| 171 |
result = isc_file_getsize(filename, &fileoff); |
| 172 |
if (result != ISC_R_SUCCESS) { |
| 173 |
fprintf(stderr, "stat(%s): %s\n", filename, |
| 174 |
isc_result_totext(result)); |
| 175 |
exit(EXIT_FAILURE); |
| 176 |
} |
| 177 |
|
| 178 |
filesize = (size_t)fileoff; |
| 179 |
filetext = isc_mem_get(mctx, filesize + 1); |
| 180 |
fp = fopen(filename, "r"); |
| 181 |
if (fp == NULL || fread(filetext, 1, filesize, fp) < filesize) { |
| 182 |
fprintf(stderr, "read(%s): %s\n", filename, strerror(errno)); |
| 183 |
exit(EXIT_FAILURE); |
| 184 |
} |
| 185 |
fclose(fp); |
| 186 |
filetext[filesize] = '\0'; |
| 187 |
|
| 188 |
dns_qp_create(mctx, &methods, NULL, &qp); |
| 189 |
|
| 190 |
pos = filetext; |
| 191 |
file_end = pos + filesize; |
| 192 |
while (pos < file_end) { |
| 193 |
void *pval = NULL; |
| 194 |
uint32_t ival = 0; |
| 195 |
dns_fixedname_t fixed; |
| 196 |
dns_name_t *name = dns_fixedname_initname(&fixed); |
| 197 |
isc_buffer_t buffer; |
| 198 |
char *newline = NULL, *domain = pos; |
| 199 |
size_t len; |
| 200 |
|
| 201 |
pos += strcspn(pos, "\r\n"); |
| 202 |
newline = pos; |
| 203 |
pos += strspn(pos, "\r\n"); |
| 204 |
|
| 205 |
len = newline - domain; |
| 206 |
domain[len] = '\0'; |
| 207 |
|
| 208 |
isc_buffer_init(&buffer, domain, len); |
| 209 |
isc_buffer_add(&buffer, len); |
| 210 |
result = dns_name_fromtext(name, &buffer, dns_rootname, 0, |
| 211 |
NULL); |
| 212 |
if (result == ISC_R_SUCCESS) { |
| 213 |
smallname_from_name(name, &pval, &ival); |
| 214 |
result = dns_qp_insert(qp, pval, ival); |
| 215 |
} |
| 216 |
if (result == ISC_R_EXISTS && pval != NULL) { |
| 217 |
smallname_free(pval, ival); |
| 218 |
continue; |
| 219 |
} |
| 220 |
if (result != ISC_R_SUCCESS) { |
| 221 |
fprintf(stderr, "%s:%zu: %s %s\n", filename, names, |
| 222 |
domain, isc_result_totext(result)); |
| 223 |
exit(EXIT_FAILURE); |
| 224 |
} |
| 225 |
|
| 226 |
wirebytes += name->length; |
| 227 |
labels += name->labels; |
| 228 |
names += 1; |
| 229 |
} |
| 230 |
dns_qp_compact(qp, DNS_QPGC_ALL); |
| 231 |
|
| 232 |
#define print_megabytes(label, value) \ |
| 233 |
printf("%6.2f MiB - " label "\n", (double)(value) / 1048576.0) |
| 234 |
|
| 235 |
if (!dumptxt && !dumpdot) { |
| 236 |
size_t smallbytes = wirebytes + labels + |
| 237 |
names * sizeof(isc_refcount_t); |
| 238 |
dns_qp_memusage_t memusage = dns_qp_memusage(qp); |
| 239 |
uint64_t compaction_us, recovery_us, rollback_us; |
| 240 |
dns_qp_gctime(&compaction_us, &recovery_us, &rollback_us); |
| 241 |
|
| 242 |
printf("leaves %zu\n" |
| 243 |
" nodes %zu\n" |
| 244 |
" used %zu\n" |
| 245 |
" free %zu\n" |
| 246 |
" cow %zu\n" |
| 247 |
"chunks %zu\n" |
| 248 |
" bytes %zu\n", |
| 249 |
memusage.leaves, memusage.live, memusage.used, |
| 250 |
memusage.free, memusage.hold, memusage.chunk_count, |
| 251 |
memusage.bytes); |
| 252 |
|
| 253 |
printf("%f compaction\n", (double)compaction_us / 1000000); |
| 254 |
printf("%f recovery\n", (double)recovery_us / 1000000); |
| 255 |
printf("%f rollback\n", (double)rollback_us / 1000000); |
| 256 |
|
| 257 |
size_t bytes = memusage.bytes; |
| 258 |
print_megabytes("file size", filesize); |
| 259 |
print_megabytes("names", wirebytes); |
| 260 |
print_megabytes("labels", labels); |
| 261 |
print_megabytes("names + labels", wirebytes + labels); |
| 262 |
print_megabytes("smallnames", smallbytes); |
| 263 |
print_megabytes("qp-trie", bytes); |
| 264 |
print_megabytes("qp-trie + smallnames", bytes + smallbytes); |
| 265 |
print_megabytes("calculated", bytes + smallbytes + filesize); |
| 266 |
print_megabytes("allocated", isc_mem_inuse(mctx)); |
| 267 |
printf("%6zu - height\n", qp_test_getheight(qp)); |
| 268 |
printf("%6zu - max key len\n", qp_test_maxkeylen(qp)); |
| 269 |
} |
| 270 |
|
| 271 |
if (dumptxt) { |
| 272 |
qp_test_dumptrie(qp); |
| 273 |
} |
| 274 |
if (dumpdot) { |
| 275 |
qp_test_dumpdot(qp); |
| 276 |
} |
| 277 |
|
| 278 |
return 0; |
| 279 |
} |