2 * stats.c - various routines to do stats on the key graph
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
19 * initcolour - Clear the key graph ready for use.
20 * @parent: Do we want to clear the parent pointers too?
22 * Clears the parent and colour information on all elements in the key
25 void initcolour(bool parent)
31 * Init the colour/parent values. We get each entry list from the hash
32 * table and walk along it, zeroing the values.
34 for (loop = 0; loop < HASHSIZE; loop++) {
35 curkey = gethashtableentry(loop);
36 while (curkey != NULL) {
37 ((struct stats_key *)curkey->object)->colour = 0;
39 ((struct stats_key *)curkey->object)->parent =
42 curkey = curkey->next;
48 * findpath - Given 2 keys finds a path between them.
49 * @have: The key we have.
50 * @want: The key we want to get to.
52 * This does a breadth first search on the key tree, starting with the
53 * key we have. It returns as soon as a path is found or when we run out
54 * of keys; whichever comes sooner.
56 unsigned long findpath(struct stats_key *have, struct stats_key *want)
58 struct ll *keys = NULL;
59 struct ll *oldkeys = NULL;
60 struct ll *sigs = NULL;
61 struct ll *nextkeys = NULL;
66 keys = lladd(NULL, want);
69 while (keys != NULL && have->colour == 0) {
70 sigs = hash_getkeysigs(((struct stats_key *)
71 keys->object)->keyid);
72 while (sigs != NULL && have->colour == 0) {
74 * Check if we've seen this key before and if not mark
75 * it and add its sigs to the list we want to look at.
77 if (((struct stats_key *)sigs->object)->colour == 0) {
79 ((struct stats_key *)sigs->object)->colour =
81 ((struct stats_key *)sigs->object)->parent =
84 nextkeys = lladd(nextkeys, sigs->object);
91 llfree(oldkeys, NULL);
97 if (oldkeys != NULL) {
98 llfree(oldkeys, NULL);
101 if (nextkeys != NULL) {
102 llfree(nextkeys, NULL);
110 * dofindpath - Given 2 keys displays a path between them.
111 * @have: The key we have.
112 * @want: The key we want to get to.
113 * @html: Should we output in html.
115 * This does a breadth first search on the key tree, starting with the
116 * key we have. It returns as soon as a path is found or when we run out
117 * of keys; whichever comes sooner.
119 void dofindpath(uint64_t have, uint64_t want, bool html)
121 struct stats_key *keyinfoa, *keyinfob, *curkey;
122 uint64_t fullhave, fullwant;
126 fullhave = getfullkeyid(have);
127 fullwant = getfullkeyid(want);
130 * Make sure the keys we have and want are in the cache.
132 hash_getkeysigs(fullhave);
133 hash_getkeysigs(fullwant);
135 if ((keyinfoa = findinhash(fullhave)) == NULL) {
136 printf("Couldn't find key 0x%llX.\n", have);
139 if ((keyinfob = findinhash(fullwant)) == NULL) {
140 printf("Couldn't find key 0x%llX.\n", want);
145 * Fill the tree info up.
148 rec = findpath(keyinfoa, keyinfob);
149 keyinfob->parent = 0;
151 printf("%d nodes examined. %ld elements in the hash%s\n", rec,
154 if (keyinfoa->colour == 0) {
155 printf("Can't find a link from 0x%08llX to 0x%08llX%s\n",
160 printf("%d steps from 0x%08llX to 0x%08llX%s\n",
161 keyinfoa->colour, have & 0xFFFFFFFF,
165 while (curkey != NULL && curkey->keyid != 0) {
166 uid = keyid2uid(curkey->keyid);
167 if (html && uid == NULL) {
168 printf("<a href=\"lookup?op=get&search=%llX\">"
169 "0x%08llX</a> ([User id not found])%s"
171 curkey->keyid & 0xFFFFFFFF,
172 curkey->keyid & 0xFFFFFFFF,
173 (curkey->keyid == want) ? "" :
175 } else if (html && uid != NULL) {
176 printf("<a href=\"lookup?op=get&search=%llX\">"
178 " (<a href=\"lookup?op=vindex"
179 "&search=0x%llX\">%s</a>)%s<BR>\n",
180 curkey->keyid & 0xFFFFFFFF,
181 curkey->keyid & 0xFFFFFFFF,
182 curkey->keyid & 0xFFFFFFFF,
184 (curkey->keyid == want) ? "" :
187 printf("0x%08llX (%s)%s\n",
188 curkey->keyid & 0xFFFFFFFF,
189 (uid == NULL) ? "[User id not found]" :
191 (curkey->keyid == want) ? "" :
198 curkey = findinhash(curkey->parent);
201 puts("<P>List of key ids in path:</P>");
203 puts("List of key ids in path:");
206 while (curkey != NULL && curkey->keyid != 0) {
207 printf("0x%08llX ", curkey->keyid & 0xFFFFFFFF);
208 curkey = findinhash(curkey->parent);
216 struct stats_key *furthestkey(struct stats_key *have)
218 unsigned long count = 0;
219 unsigned long curdegree = 0;
220 struct ll *curll, *nextll, *tmp;
221 struct ll *sigs = NULL;
222 struct stats_key *max;
232 curll = lladd(NULL, have);
234 while (curll != NULL) {
235 sigs = hash_getkeysigs(((struct stats_key *)
236 curll->object)->keyid);
237 while (sigs != NULL) {
238 if (((struct stats_key *) sigs->object)->colour == 0) {
240 * We've never seen it. Count it, mark it and
241 * explore its subtree.
244 max = (struct stats_key *)sigs->object;
245 ((struct stats_key *)sigs->object)->colour =
247 ((struct stats_key *)sigs->object)->parent =
248 ((struct stats_key *)
249 curll->object)->keyid;
251 nextll=lladd(nextll, sigs->object);