Debian Codegraph @

tests/bench/qplookups.c

raw
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/time.h>
22 #include <isc/util.h>
23
24 #include <dns/fixedname.h>
25 #include <dns/qp.h>
26 #include <dns/rbt.h>
27 #include <dns/types.h>
28
29 #include <tests/dns.h>
30 #include <tests/qp.h>
31
32 static inline size_t
33 smallname_length(void *pval, uint32_t ival) {
34 UNUSED(pval);
35 return ival & 0xff;
36 }
37
38 static inline size_t
39 smallname_labels(void *pval, uint32_t ival) {
40 UNUSED(pval);
41 return ival >> 8;
42 }
43
44 static inline isc_refcount_t *
45 smallname_refcount(void *pval, uint32_t ival) {
46 UNUSED(ival);
47 return pval;
48 }
49
50 static inline uint8_t *
51 smallname_ndata(void *pval, uint32_t ival) {
52 return (uint8_t *)(smallname_refcount(pval, ival) + 1);
53 }
54
55 static inline uint8_t *
56 smallname_offsets(void *pval, uint32_t ival) {
57 return smallname_ndata(pval, ival) + smallname_length(pval, ival);
58 }
59
60 static void
61 smallname_from_name(const dns_name_t *name, void **valp, uint32_t *ctxp) {
62 size_t size = sizeof(isc_refcount_t) + name->length + name->labels;
63 *valp = isc_mem_get(mctx, size);
64 *ctxp = name->labels << 8 | name->length;
65 isc_refcount_init(smallname_refcount(*valp, *ctxp), 0);
66 memmove(smallname_ndata(*valp, *ctxp), name->ndata, name->length);
67 memmove(smallname_offsets(*valp, *ctxp), name->offsets, name->labels);
68 }
69
70 static void
71 smallname_free(void *pval, uint32_t ival) {
72 size_t size = sizeof(isc_refcount_t);
73 size += smallname_length(pval, ival) + smallname_labels(pval, ival);
74 isc_mem_put(mctx, pval, size);
75 }
76
77 static void
78 name_from_smallname(dns_name_t *name, void *pval, uint32_t ival) {
79 dns_name_reset(name);
80 name->ndata = smallname_ndata(pval, ival);
81 name->length = smallname_length(pval, ival);
82 name->labels = smallname_labels(pval, ival);
83 name->offsets = smallname_offsets(pval, ival);
84 name->attributes.readonly = true;
85 if (name->ndata[name->offsets[name->labels - 1]] == '\0') {
86 name->attributes.absolute = true;
87 }
88 }
89
90 static size_t
91 qpkey_from_smallname(dns_qpkey_t key, void *ctx, void *pval, uint32_t ival) {
92 UNUSED(ctx);
93 dns_name_t name = DNS_NAME_INITEMPTY;
94 name_from_smallname(&name, pval, ival);
95 return dns_qpkey_fromname(key, &name);
96 }
97
98 static void
99 smallname_attach(void *ctx, void *pval, uint32_t ival) {
100 UNUSED(ctx);
101 isc_refcount_increment0(smallname_refcount(pval, ival));
102 }
103
104 static void
105 smallname_detach(void *ctx, void *pval, uint32_t ival) {
106 if (isc_refcount_decrement(smallname_refcount(pval, ival)) == 1) {
107 isc_mem_free(ctx, pval);
108 }
109 }
110
111 static void
112 testname(void *ctx, char *buf, size_t size) {
113 REQUIRE(ctx == NULL);
114 strlcpy(buf, "test", size);
115 }
116
117 const dns_qpmethods_t methods = {
118 smallname_attach,
119 smallname_detach,
120 qpkey_from_smallname,
121 testname,
122 };
123
124 static void
125 usage(void) {
126 fprintf(stderr, "usage: lookups <filename>\n");
127 exit(EXIT_FAILURE);
128 }
129
130 static size_t
131 load_qp(dns_qp_t *qp, const char *filename) {
132 isc_result_t result;
133 char *filetext = NULL;
134 size_t filesize, names = 0;
135 char *pos = NULL, *file_end = NULL;
136 off_t fileoff;
137 FILE *fp = NULL;
138
139 result = isc_file_getsize(filename, &fileoff);
140 if (result != ISC_R_SUCCESS) {
141 fprintf(stderr, "stat(%s): %s\n", filename,
142 isc_result_totext(result));
143 exit(EXIT_FAILURE);
144 }
145
146 filesize = (size_t)fileoff;
147 filetext = isc_mem_get(mctx, filesize + 1);
148 fp = fopen(filename, "r");
149 if (fp == NULL || fread(filetext, 1, filesize, fp) < filesize) {
150 fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
151 exit(EXIT_FAILURE);
152 }
153 fclose(fp);
154 filetext[filesize] = '\0';
155
156 pos = filetext;
157 file_end = pos + filesize;
158 while (pos < file_end) {
159 void *pval = NULL;
160 uint32_t ival = 0;
161 dns_fixedname_t fixed;
162 dns_name_t *name = dns_fixedname_initname(&fixed);
163 isc_buffer_t buffer;
164 char *newline = NULL, *domain = pos;
165 size_t len;
166
167 pos += strcspn(pos, "\r\n");
168 newline = pos;
169 pos += strspn(pos, "\r\n");
170
171 len = newline - domain;
172 domain[len] = '\0';
173
174 isc_buffer_init(&buffer, domain, len);
175 isc_buffer_add(&buffer, len);
176 result = dns_name_fromtext(name, &buffer, dns_rootname, 0,
177 NULL);
178 if (result == ISC_R_SUCCESS) {
179 smallname_from_name(name, &pval, &ival);
180 result = dns_qp_insert(qp, pval, ival);
181 }
182 if (result == ISC_R_EXISTS && pval != NULL) {
183 smallname_free(pval, ival);
184 continue;
185 }
186 if (result != ISC_R_SUCCESS) {
187 fprintf(stderr, "%s:%zu: %s %s\n", filename, names,
188 domain, isc_result_totext(result));
189 exit(EXIT_FAILURE);
190 }
191
192 names++;
193 }
194
195 return names;
196 }
197
198 int
199 main(int argc, char **argv) {
200 dns_qp_t *qp = NULL;
201 isc_nanosecs_t start, stop;
202 dns_fixedname_t *items = NULL;
203 dns_qpiter_t it = { 0 };
204 dns_name_t *name = NULL;
205 size_t i = 0, n = 0;
206 char buf[BUFSIZ];
207
208 if (argc != 2) {
209 usage();
210 }
211
212 isc_mem_create(&mctx);
213
214 dns_qp_create(mctx, &methods, NULL, &qp);
215
216 start = isc_time_monotonic();
217 n = load_qp(qp, argv[1]);
218 dns_qp_compact(qp, DNS_QPGC_ALL);
219 stop = isc_time_monotonic();
220
221 snprintf(buf, sizeof(buf), "load %zd names:", n);
222 printf("%-57s%7.3fsec\n", buf, (stop - start) / (double)NS_PER_SEC);
223
224 items = isc_mem_cget(mctx, n, sizeof(dns_fixedname_t));
225 dns_qpiter_init(qp, &it);
226
227 start = isc_time_monotonic();
228 for (i = 0;; i++) {
229 name = dns_fixedname_initname(&items[i]);
230 if (dns_qpiter_next(&it, name, NULL, NULL) != ISC_R_SUCCESS) {
231 break;
232 }
233 }
234 stop = isc_time_monotonic();
235
236 snprintf(buf, sizeof(buf), "iterate %zd names:", n);
237 printf("%-57s%7.3fsec\n", buf, (stop - start) / (double)NS_PER_SEC);
238
239 n = i;
240 start = isc_time_monotonic();
241 for (i = 0; i < n; i++) {
242 name = dns_fixedname_name(&items[i]);
243 dns_qp_getname(qp, name, NULL, NULL);
244 }
245 stop = isc_time_monotonic();
246
247 snprintf(buf, sizeof(buf), "look up %zd names (dns_qp_getname):", n);
248 printf("%-57s%7.3fsec\n", buf, (stop - start) / (double)NS_PER_SEC);
249
250 start = isc_time_monotonic();
251 for (i = 0; i < n; i++) {
252 name = dns_fixedname_name(&items[i]);
253 dns_qp_lookup(qp, name, 0, NULL, NULL, NULL, NULL);
254 }
255 stop = isc_time_monotonic();
256
257 snprintf(buf, sizeof(buf), "look up %zd names (dns_qp_lookup):", n);
258 printf("%-57s%7.3fsec\n", buf, (stop - start) / (double)NS_PER_SEC);
259
260 start = isc_time_monotonic();
261 for (i = 0; i < n; i++) {
262 /*
263 * copy the name, and modify the first letter before
264 * searching; that way it probably won't be found in
265 * the QP trie. (though it might, if for example the trie
266 * contains both "x." and "y.". for best results,
267 * use input data where this isn't an issue.)
268 */
269 dns_fixedname_t sf;
270 dns_name_t *search = dns_fixedname_initname(&sf);
271
272 name = dns_fixedname_name(&items[i]);
273 dns_name_copy(name, search);
274 if (search->ndata[1] != 0) {
275 ++search->ndata[1];
276 }
277
278 dns_qp_lookup(qp, search, 0, NULL, NULL, NULL, NULL);
279 }
280 stop = isc_time_monotonic();
281
282 snprintf(buf, sizeof(buf),
283 "look up %zd wrong names (dns_qp_lookup):", n);
284 printf("%-57s%7.3fsec\n", buf, (stop - start) / (double)NS_PER_SEC);
285
286 isc_mem_cput(mctx, items, n, sizeof(dns_fixedname_t));
287 return 0;
288 }