xref: /titanic_52/usr/src/cmd/fm/modules/common/eversholt/iexpr.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * iexpr.c -- instanced expression cache module
27  *
28  * this module provides a cache of fully instantized expressions.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include "alloc.h"
36 #include "out.h"
37 #include "lut.h"
38 #include "tree.h"
39 #include "ptree.h"
40 #include "itree.h"
41 #include "ipath.h"
42 #include "iexpr.h"
43 #include "stats.h"
44 #include "eval.h"
45 #include "config.h"
46 
47 #define	IEXPRSZ	1024	/* hash table size */
48 
49 static struct stats *Niexpr;
50 
51 /* the cache is a hash table of these structs */
52 static struct iexpr {
53 	struct node *np;
54 	struct iexpr *next;	/* next entry in hash bucket */
55 	int count;
56 } *Cache[IEXPRSZ];
57 
58 /*
59  * iexpr_init -- initialize the iexpr module
60  */
61 void
62 iexpr_init(void)
63 {
64 	Niexpr = stats_new_counter("iexpr.niexpr", "iexpr cache entries", 1);
65 }
66 
67 /*
68  * iexpr_hash -- produce a simple hash from an instanced expression tree
69  */
70 static unsigned
71 iexpr_hash(struct node *np)
72 {
73 	if (np == NULL)
74 		return (1);
75 
76 	switch (np->t) {
77 	case T_GLOBID:
78 		return ((uintptr_t)np->u.globid.s);
79 
80 	case T_ASSIGN:
81 	case T_CONDIF:
82 	case T_CONDELSE:
83 	case T_NE:
84 	case T_EQ:
85 	case T_LT:
86 	case T_LE:
87 	case T_GT:
88 	case T_GE:
89 	case T_BITAND:
90 	case T_BITOR:
91 	case T_BITXOR:
92 	case T_BITNOT:
93 	case T_LSHIFT:
94 	case T_RSHIFT:
95 	case T_LIST:
96 	case T_AND:
97 	case T_OR:
98 	case T_NOT:
99 	case T_ADD:
100 	case T_SUB:
101 	case T_MUL:
102 	case T_DIV:
103 	case T_MOD:
104 		return ((int)np->t *
105 		    (iexpr_hash(np->u.expr.left) +
106 		    iexpr_hash(np->u.expr.right)));
107 
108 	case T_NAME:
109 		return ((uintptr_t)np->u.name.s);
110 
111 	case T_EVENT:
112 		return (iexpr_hash(np->u.event.ename) +
113 		    iexpr_hash(np->u.event.epname));
114 
115 	case T_FUNC:
116 		return ((uintptr_t)np->u.func.s +
117 		    iexpr_hash(np->u.func.arglist));
118 
119 	case T_QUOTE:
120 		return ((uintptr_t)np->u.quote.s);
121 
122 	case T_NUM:
123 		return ((int)np->u.ull);
124 
125 	default:
126 		outfl(O_DIE, np->file, np->line,
127 		    "iexpr_hash: unexpected node type: %s",
128 		    ptree_nodetype2str(np->t));
129 	}
130 	/*NOTREACHED*/
131 	return (1);
132 }
133 
134 /*
135  * iexpr_cmp -- compare two instanced expression trees
136  */
137 static int
138 iexpr_cmp(struct node *np1, struct node *np2)
139 {
140 	int diff;
141 
142 	if (np1 == np2)
143 		return (0);
144 
145 	if (np1 == NULL)
146 		return (1);
147 
148 	if (np2 == NULL)
149 		return (-1);
150 
151 	if (np1->t != np2->t)
152 		return (np2->t - np1->t);
153 
154 	/* types match, need to see additional info matches */
155 	switch (np1->t) {
156 	case T_GLOBID:
157 		return (np2->u.globid.s - np1->u.globid.s);
158 
159 	case T_ASSIGN:
160 	case T_CONDIF:
161 	case T_CONDELSE:
162 	case T_NE:
163 	case T_EQ:
164 	case T_LT:
165 	case T_LE:
166 	case T_GT:
167 	case T_GE:
168 	case T_BITAND:
169 	case T_BITOR:
170 	case T_BITXOR:
171 	case T_BITNOT:
172 	case T_LSHIFT:
173 	case T_RSHIFT:
174 	case T_LIST:
175 	case T_AND:
176 	case T_OR:
177 	case T_NOT:
178 	case T_ADD:
179 	case T_SUB:
180 	case T_MUL:
181 	case T_DIV:
182 	case T_MOD:
183 		diff = iexpr_cmp(np1->u.expr.left, np2->u.expr.left);
184 		if (diff != 0)
185 			return (diff);
186 		return (iexpr_cmp(np1->u.expr.right, np2->u.expr.right));
187 
188 	case T_NAME:
189 		if (np2->u.name.s != np1->u.name.s)
190 			return (np2->u.name.s - np1->u.name.s);
191 		diff = iexpr_cmp(np1->u.name.child, np2->u.name.child);
192 		if (diff != 0)
193 			return (diff);
194 		return (iexpr_cmp(np1->u.name.next, np2->u.name.next));
195 
196 	case T_EVENT:
197 		diff = iexpr_cmp(np1->u.event.ename, np2->u.event.ename);
198 		if (diff != 0)
199 			return (diff);
200 		return (iexpr_cmp(np1->u.event.epname, np2->u.event.epname));
201 
202 	case T_FUNC:
203 		if (np1->u.func.s != np2->u.func.s)
204 			return (np2->u.func.s - np1->u.func.s);
205 		return (iexpr_cmp(np1->u.func.arglist, np2->u.func.arglist));
206 
207 	case T_QUOTE:
208 		return (np2->u.quote.s - np1->u.quote.s);
209 
210 	case T_NUM:
211 		if (np2->u.ull > np1->u.ull)
212 			return (1);
213 		else if (np1->u.ull > np2->u.ull)
214 			return (-1);
215 		else
216 			return (0);
217 
218 	default:
219 		outfl(O_DIE, np1->file, np1->line,
220 		    "iexpr_cmp: unexpected node type: %s",
221 		    ptree_nodetype2str(np1->t));
222 	}
223 	/*NOTREACHED*/
224 	return (0);
225 }
226 
227 /*
228  * iexpr -- find instanced expr in cache, or add it if necessary
229  */
230 struct node *
231 iexpr(struct node *np)
232 {
233 	unsigned idx = iexpr_hash(np) % IEXPRSZ;
234 	struct iexpr *bucketp = Cache[idx];
235 	struct iexpr *cp;
236 
237 	/* search cache */
238 	for (cp = bucketp; cp != NULL; cp = cp->next)
239 		if (iexpr_cmp(cp->np, np) == 0) {
240 			/* found it */
241 			tree_free(np);
242 			cp->count++;
243 			return (cp->np);
244 		}
245 
246 	/* allocate new cache entry */
247 	cp = MALLOC(sizeof (*cp));
248 	cp->np = np;
249 	cp->next = bucketp;
250 	cp->count = 1;
251 	Cache[idx] = cp;
252 
253 	stats_counter_bump(Niexpr);
254 
255 	return (np);
256 }
257 
258 void
259 iexpr_free(struct node *np)
260 {
261 	unsigned idx = iexpr_hash(np) % IEXPRSZ;
262 	struct iexpr *cp;
263 	struct iexpr *prevcp = NULL;
264 
265 	/* search cache */
266 	for (cp = Cache[idx]; cp != NULL; cp = cp->next) {
267 		if (iexpr_cmp(cp->np, np) == 0) {
268 			/* found it */
269 			cp->count--;
270 			if (cp->count == 0) {
271 				tree_free(cp->np);
272 				if (prevcp == NULL)
273 					Cache[idx] = cp->next;
274 				else
275 					prevcp->next = cp->next;
276 				FREE(cp);
277 			}
278 			return;
279 		}
280 		prevcp = cp;
281 	}
282 }
283 
284 /*
285  * iexpr_cached -- return true if np is in the iexpr cache
286  */
287 int
288 iexpr_cached(struct node *np)
289 {
290 	struct iexpr *cp = Cache[iexpr_hash(np) % IEXPRSZ];
291 
292 	/* search cache */
293 	for (; cp != NULL; cp = cp->next)
294 		if (iexpr_cmp(cp->np, np) == 0) {
295 			/* found it */
296 			return (1);
297 		}
298 
299 	return (0);
300 }
301 
302 /*
303  * iexpr_fini -- free the iexpr cache
304  */
305 void
306 iexpr_fini(void)
307 {
308 	int i;
309 
310 	for (i = 0; i < IEXPRSZ; i++) {
311 		struct iexpr *cp;
312 		struct iexpr *ncp;
313 
314 		for (cp = Cache[i]; cp != NULL; cp = ncp) {
315 			tree_free(cp->np);
316 			ncp = cp->next;
317 			FREE(cp);
318 		}
319 		Cache[i] = NULL;
320 	}
321 }
322