2 * stats.c - various routines to do stats on the key graph
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
8 * $Id: stats.c,v 1.11 2003/06/04 22:32:56 noodles Exp $
21 * initcolour - Clear the key graph ready for use.
22 * @parent: Do we want to clear the parent pointers too?
24 * Clears the parent and colour information on all elements in the key
27 void initcolour(bool parent)
33 * Init the colour/parent values. We get each entry list from the hash
34 * table and walk along it, zeroing the values.
36 for (loop = 0; loop < HASHSIZE; loop++) {
37 curkey = gethashtableentry(loop);
38 while (curkey != NULL) {
39 ((struct stats_key *)curkey->object)->colour = 0;
41 ((struct stats_key *)curkey->object)->parent =
44 curkey = curkey->next;
50 * findpath - Given 2 keys finds a path between them.
51 * @have: The key we have.
52 * @want: The key we want to get to.
54 * This does a breadth first search on the key tree, starting with the
55 * key we have. It returns as soon as a path is found or when we run out
56 * of keys; whichever comes sooner.
58 unsigned long findpath(struct stats_key *have, struct stats_key *want)
60 struct ll *keys = NULL;
61 struct ll *oldkeys = NULL;
62 struct ll *sigs = NULL;
63 struct ll *nextkeys = NULL;
68 keys = lladd(NULL, want);
71 while (keys != NULL && have->colour == 0) {
72 sigs = cached_getkeysigs(((struct stats_key *)
73 keys->object)->keyid);
74 while (sigs != NULL && have->colour == 0) {
76 * Check if we've seen this key before and if not mark
77 * it and add its sigs to the list we want to look at.
79 if (!((struct stats_key *)sigs->object)->disabled &&
80 ((struct stats_key *)sigs->object)->colour == 0) {
82 ((struct stats_key *)sigs->object)->colour =
84 ((struct stats_key *)sigs->object)->parent =
87 nextkeys = lladd(nextkeys, sigs->object);
94 llfree(oldkeys, NULL);
100 if (oldkeys != NULL) {
101 llfree(oldkeys, NULL);
104 if (nextkeys != NULL) {
105 llfree(nextkeys, NULL);
113 * dofindpath - Given 2 keys displays a path between them.
114 * @have: The key we have.
115 * @want: The key we want to get to.
116 * @html: Should we output in html.
117 * @count: How many paths we should look for.
119 * This does a breadth first search on the key tree, starting with the
120 * key we have. It returns as soon as a path is found or when we run out
121 * of keys; whichever comes sooner.
123 void dofindpath(uint64_t have, uint64_t want, bool html, int count)
125 struct stats_key *keyinfoa, *keyinfob, *curkey;
126 uint64_t fullhave, fullwant;
131 fullhave = getfullkeyid(have);
132 fullwant = getfullkeyid(want);
135 * Make sure the keys we have and want are in the cache.
137 cached_getkeysigs(fullhave);
138 cached_getkeysigs(fullwant);
140 if ((keyinfoa = findinhash(fullhave)) == NULL) {
141 printf("Couldn't find key 0x%llX.\n", have);
144 if ((keyinfob = findinhash(fullwant)) == NULL) {
145 printf("Couldn't find key 0x%llX.\n", want);
151 while (pathnum < count) {
153 * Fill the tree info up.
156 rec = findpath(keyinfoa, keyinfob);
157 keyinfob->parent = 0;
159 printf("%s%d nodes examined. %ld elements in the hash%s\n",
164 if (keyinfoa->colour == 0) {
166 printf("Can't find a link from 0x%08llX to "
172 printf("Can't find any further paths%s\n",
177 printf("%d steps from 0x%08llX to 0x%08llX%s\n",
178 keyinfoa->colour, have & 0xFFFFFFFF,
182 while (curkey != NULL && curkey->keyid != 0) {
183 uid = keyid2uid(curkey->keyid);
184 if (html && uid == NULL) {
185 printf("<a href=\"lookup?op=get&search="
186 "0x%08llX\">0x%08llX</a> (["
187 "User id not found])%s<BR>\n",
188 curkey->keyid & 0xFFFFFFFF,
189 curkey->keyid & 0xFFFFFFFF,
190 (curkey->keyid == fullwant) ?
192 } else if (html && uid != NULL) {
193 printf("<a href=\"lookup?op=get&search="
194 "0x%08llX\">0x%08llX</a>"
195 " (<a href=\"lookup?op=vindex&"
196 "search=0x%08llX\">%s</a>)%s"
198 curkey->keyid & 0xFFFFFFFF,
199 curkey->keyid & 0xFFFFFFFF,
200 curkey->keyid & 0xFFFFFFFF,
202 (curkey->keyid == fullwant) ?
205 printf("0x%08llX (%s)%s\n",
206 curkey->keyid & 0xFFFFFFFF,
208 "[User id not found]" :
210 (curkey->keyid == fullwant) ?
217 if (curkey != keyinfoa && curkey != keyinfob) {
218 curkey->disabled = true;
220 curkey = findinhash(curkey->parent);
223 puts("<P>List of key ids in path:</P>");
225 puts("List of key ids in path:");
228 while (curkey != NULL && curkey->keyid != 0) {
229 printf("0x%08llX ", curkey->keyid & 0xFFFFFFFF);
230 curkey = findinhash(curkey->parent);
240 struct stats_key *furthestkey(struct stats_key *have)
242 unsigned long count = 0;
243 unsigned long curdegree = 0;
244 struct ll *curll, *nextll, *tmp;
245 struct ll *sigs = NULL;
246 struct stats_key *max;
256 curll = lladd(NULL, have);
258 while (curll != NULL) {
259 sigs = cached_getkeysigs(((struct stats_key *)
260 curll->object)->keyid);
261 while (sigs != NULL) {
262 if (((struct stats_key *) sigs->object)->colour == 0) {
264 * We've never seen it. Count it, mark it and
265 * explore its subtree.
268 max = (struct stats_key *)sigs->object;
269 ((struct stats_key *)sigs->object)->colour =
271 ((struct stats_key *)sigs->object)->parent =
272 ((struct stats_key *)
273 curll->object)->keyid;
275 nextll=lladd(nextll, sigs->object);