1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
From f4d1c5b4b3b4623394e09f0feb4fce9150b772c7 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Wed, 18 Mar 2015 12:25:26 +0100
Subject: [PATCH gnumach 4/7] ipc: replace reverse hash table with a radix tree
Currently, there is a hash table mapping (space, object) tuples to
`ipc_entry' objects. This hash table is intertwined with the IPC
tables. There is one hash table per IPC space, but it is only for the
entries in the IPC table. This hash table is called `local' in the
source.
All IPC entries being spilled into the splay tree are instead mapped
by a global hash table.
Replace the local (i.e. per IPC space) reverse hash table with a radix
tree.
* ipc/ipc_entry.c (ipc_entry_grow_table): Adjust accordingly.
* ipc/ipc_entry.h (struct ipc_entry): Adjust comment.
* ipc/ipc_hash.c: Adjust comment explaining the local lookup table.
(IPC_LOCAL_HASH_INVARIANT): New macro.
(ipc_hash_local_lookup): Use the new `ipc_reverse_lookup' function.
(ipc_hash_local_insert): Use the new `ipc_reverse_insert' function.
(ipc_hash_local_delete): Use the new `ipc_reverse_remove' function.
* ipc/ipc_space.c (ipc_space_create): Initialize radix tree.
(ipc_space_destroy): Free radix tree.
* ipc/ipc_space.h (struct ipc_space): Add radix tree.
(ipc_reverse_insert): New function.
(ipc_reverse_remove): Likewise.
(ipc_reverse_remove_all): Likewise.
(ipc_reverse_lookup): Likewise.
---
ipc/ipc_entry.c | 5 +-
ipc/ipc_entry.h | 5 --
ipc/ipc_hash.c | 184 ++++++++------------------------------------------------
ipc/ipc_space.c | 3 +
ipc/ipc_space.h | 57 ++++++++++++++++++
5 files changed, 87 insertions(+), 167 deletions(-)
diff --git a/ipc/ipc_entry.c b/ipc/ipc_entry.c
index e78f74e..5b9fd98 100644
--- a/ipc/ipc_entry.c
+++ b/ipc/ipc_entry.c
@@ -618,6 +618,7 @@ ipc_entry_grow_table(ipc_space_t space)
is_write_unlock(space);
thread_wakeup((event_t) space);
it_entries_free(its, table);
+ ipc_reverse_remove_all(space);
is_write_lock(space);
return KERN_SUCCESS;
}
@@ -641,9 +642,6 @@ ipc_entry_grow_table(ipc_space_t space)
memcpy(table, otable,
osize * sizeof(struct ipc_entry));
- for (i = 0; i < osize; i++)
- table[i].ie_index = 0;
-
(void) memset((void *) (table + osize), 0,
(size - osize) * sizeof(struct ipc_entry));
@@ -651,6 +649,7 @@ ipc_entry_grow_table(ipc_space_t space)
* Put old entries into the reverse hash table.
*/
+ ipc_reverse_remove_all(space);
for (i = 0; i < osize; i++) {
ipc_entry_t entry = &table[i];
diff --git a/ipc/ipc_entry.h b/ipc/ipc_entry.h
index cb6d3f9..0caa70b 100644
--- a/ipc/ipc_entry.h
+++ b/ipc/ipc_entry.h
@@ -54,11 +54,6 @@
* those entries. The cutoff point between the table and the tree
* is adjusted dynamically to minimize memory consumption.
*
- * The ie_index field of entries in the table implements
- * a ordered hash table with open addressing and linear probing.
- * This hash table converts (space, object) -> name.
- * It is used independently of the other fields.
- *
* Free (unallocated) entries in the table have null ie_object
* fields. The ie_bits field is zero except for IE_BITS_GEN.
* The ie_next (ie_request) field links free entries into a free list.
diff --git a/ipc/ipc_hash.c b/ipc/ipc_hash.c
index c2c6d6e..87952a7 100644
--- a/ipc/ipc_hash.c
+++ b/ipc/ipc_hash.c
@@ -296,37 +296,19 @@ ipc_hash_global_delete(
}
/*
- * Each space has a local reverse hash table, which holds
- * entries from the space's table. In fact, the hash table
- * just uses a field (ie_index) in the table itself.
- *
- * The local hash table is an open-addressing hash table,
- * which means that when a collision occurs, instead of
- * throwing the entry into a bucket, the entry is rehashed
- * to another position in the table. In this case the rehash
- * is very simple: linear probing (ie, just increment the position).
- * This simple rehash makes deletions tractable (they're still a pain),
- * but it means that collisions tend to build up into clumps.
- *
- * Because at least one entry in the table (index 0) is always unused,
- * there will always be room in the reverse hash table. If a table
- * with n slots gets completely full, the reverse hash table will
- * have one giant clump of n-1 slots and one free slot somewhere.
- * Because entries are only entered into the reverse table if they
- * are pure send rights (not receive, send-once, port-set,
- * or dead-name rights), and free entries of course aren't entered,
- * I expect the reverse hash table won't get unreasonably full.
- *
- * Ordered hash tables (Amble & Knuth, Computer Journal, v. 17, no. 2,
- * pp. 135-142.) may be desirable here. They can dramatically help
- * unsuccessful lookups. But unsuccessful lookups are almost always
- * followed by insertions, and those slow down somewhat. They
- * also can help deletions somewhat. Successful lookups aren't affected.
- * So possibly a small win; probably nothing significant.
+ * Each space has a local reverse mapping from objects to entries
+ * from the space's table. This used to be a hash table.
*/
-#define IH_LOCAL_HASH(obj, size) \
- ((((mach_port_index_t) (vm_offset_t) (obj)) >> 6) & (size - 1))
+#define IPC_LOCAL_HASH_INVARIANT(S, O, N, E) \
+ MACRO_BEGIN \
+ assert(IE_BITS_TYPE((E)->ie_bits) == MACH_PORT_TYPE_SEND || \
+ IE_BITS_TYPE((E)->ie_bits) == MACH_PORT_TYPE_SEND_RECEIVE || \
+ IE_BITS_TYPE((E)->ie_bits) == MACH_PORT_TYPE_NONE); \
+ assert((E)->ie_object == (O)); \
+ assert((E)->ie_index == (N)); \
+ assert(&(S)->is_table[N] == (E)); \
+ MACRO_END
/*
* Routine: ipc_hash_local_lookup
@@ -345,37 +327,15 @@ ipc_hash_local_lookup(
mach_port_t *namep,
ipc_entry_t *entryp)
{
- ipc_entry_t table;
- ipc_entry_num_t size;
- mach_port_index_t hindex, index;
-
assert(space != IS_NULL);
assert(obj != IO_NULL);
- table = space->is_table;
- size = space->is_table_size;
- hindex = IH_LOCAL_HASH(obj, size);
-
- /*
- * Ideally, table[hindex].ie_index is the name we want.
- * However, must check ie_object to verify this,
- * because collisions can happen. In case of a collision,
- * search farther along in the clump.
- */
-
- while ((index = table[hindex].ie_index) != 0) {
- ipc_entry_t entry = &table[index];
-
- if (entry->ie_object == obj) {
- *namep = MACH_PORT_MAKEB(index, entry->ie_bits);
- *entryp = entry;
- return TRUE;
- }
-
- if (++hindex == size)
- hindex = 0;
+ *entryp = ipc_reverse_lookup(space, obj);
+ if (*entryp != IE_NULL) {
+ *namep = (*entryp)->ie_index;
+ IPC_LOCAL_HASH_INVARIANT(space, obj, *namep, *entryp);
+ return TRUE;
}
-
return FALSE;
}
@@ -394,33 +354,15 @@ ipc_hash_local_insert(
mach_port_index_t index,
ipc_entry_t entry)
{
- ipc_entry_t table;
- ipc_entry_num_t size;
- mach_port_index_t hindex;
-
+ kern_return_t kr;
assert(index != 0);
assert(space != IS_NULL);
assert(obj != IO_NULL);
- table = space->is_table;
- size = space->is_table_size;
- hindex = IH_LOCAL_HASH(obj, size);
-
- assert(entry == &table[index]);
- assert(entry->ie_object == obj);
-
- /*
- * We want to insert at hindex, but there may be collisions.
- * If a collision occurs, search for the end of the clump
- * and insert there.
- */
-
- while (table[hindex].ie_index != 0) {
- if (++hindex == size)
- hindex = 0;
- }
-
- table[hindex].ie_index = index;
+ entry->ie_index = index;
+ IPC_LOCAL_HASH_INVARIANT(space, obj, index, entry);
+ kr = ipc_reverse_insert(space, obj, entry);
+ assert(kr == 0);
}
/*
@@ -438,90 +380,14 @@ ipc_hash_local_delete(
mach_port_index_t index,
ipc_entry_t entry)
{
- ipc_entry_t table;
- ipc_entry_num_t size;
- mach_port_index_t hindex, dindex;
-
+ ipc_entry_t removed;
assert(index != MACH_PORT_NULL);
assert(space != IS_NULL);
assert(obj != IO_NULL);
- table = space->is_table;
- size = space->is_table_size;
- hindex = IH_LOCAL_HASH(obj, size);
-
- assert(entry == &table[index]);
- assert(entry->ie_object == obj);
-
- /*
- * First check we have the right hindex for this index.
- * In case of collision, we have to search farther
- * along in this clump.
- */
-
- while (table[hindex].ie_index != index) {
- if (table[hindex].ie_index == 0)
- {
- static int gak = 0;
- if (gak == 0)
- {
- printf("gak! entry wasn't in hash table!\n");
- gak = 1;
- }
- return;
- }
- if (++hindex == size)
- hindex = 0;
- }
-
- /*
- * Now we want to set table[hindex].ie_index = 0.
- * But if we aren't the last index in a clump,
- * this might cause problems for lookups of objects
- * farther along in the clump that are displaced
- * due to collisions. Searches for them would fail
- * at hindex instead of succeeding.
- *
- * So we must check the clump after hindex for objects
- * that are so displaced, and move one up to the new hole.
- *
- * hindex - index of new hole in the clump
- * dindex - index we are checking for a displaced object
- *
- * When we move a displaced object up into the hole,
- * it creates a new hole, and we have to repeat the process
- * until we get to the end of the clump.
- */
-
- for (dindex = hindex; index != 0; hindex = dindex) {
- for (;;) {
- mach_port_index_t tindex;
- ipc_object_t tobj;
-
- if (++dindex == size)
- dindex = 0;
- assert(dindex != hindex);
-
- /* are we at the end of the clump? */
-
- index = table[dindex].ie_index;
- if (index == 0)
- break;
-
- /* is this a displaced object? */
-
- tobj = table[index].ie_object;
- assert(tobj != IO_NULL);
- tindex = IH_LOCAL_HASH(tobj, size);
-
- if ((dindex < hindex) ?
- ((dindex < tindex) && (tindex <= hindex)) :
- ((dindex < tindex) || (tindex <= hindex)))
- break;
- }
-
- table[hindex].ie_index = index;
- }
+ IPC_LOCAL_HASH_INVARIANT(space, obj, index, entry);
+ removed = ipc_reverse_remove(space, obj);
+ assert(removed == entry);
}
/*
diff --git a/ipc/ipc_space.c b/ipc/ipc_space.c
index ab55e83..dcc96b3 100644
--- a/ipc/ipc_space.c
+++ b/ipc/ipc_space.c
@@ -148,6 +148,7 @@ ipc_space_create(
space->is_tree_total = 0;
space->is_tree_small = 0;
space->is_tree_hash = 0;
+ rdxtree_init(&space->is_reverse_map);
*spacep = space;
return KERN_SUCCESS;
@@ -271,6 +272,8 @@ ipc_space_destroy(
}
ipc_splay_traverse_finish(&space->is_tree);
+ rdxtree_remove_all(&space->is_reverse_map);
+
/*
* Because the space is now dead,
* we must release the "active" reference for it.
diff --git a/ipc/ipc_space.h b/ipc/ipc_space.h
index c4683d2..51c093b 100644
--- a/ipc/ipc_space.h
+++ b/ipc/ipc_space.h
@@ -44,6 +44,7 @@
#include <mach/mach_types.h>
#include <kern/macro_help.h>
#include <kern/lock.h>
+#include <kern/rdxtree.h>
#include <kern/slab.h>
#include <ipc/ipc_splay.h>
#include <ipc/ipc_types.h>
@@ -79,6 +80,8 @@ struct ipc_space {
ipc_entry_num_t is_tree_total; /* number of entries in the tree */
ipc_entry_num_t is_tree_small; /* # of small entries in the tree */
ipc_entry_num_t is_tree_hash; /* # of hashed entries in the tree */
+ struct rdxtree is_reverse_map; /* maps objects to entries */
+
};
#define IS_NULL ((ipc_space_t) 0)
@@ -135,4 +138,58 @@ kern_return_t ipc_space_create(ipc_table_size_t, ipc_space_t *);
kern_return_t ipc_space_create_special(struct ipc_space **);
void ipc_space_destroy(struct ipc_space *);
+/* Reverse lookups. */
+
+/* Cast a pointer to a suitable key. */
+#define KEY(X) ((unsigned long long) (unsigned long) (X))
+
+/* Insert (OBJ, ENTRY) pair into the reverse mapping. SPACE must
+ be write-locked. */
+static inline kern_return_t
+ipc_reverse_insert(ipc_space_t space,
+ ipc_object_t obj,
+ ipc_entry_t entry)
+{
+ assert(space != IS_NULL);
+ assert(obj != IO_NULL);
+ return (kern_return_t) rdxtree_insert(&space->is_reverse_map,
+ KEY(obj), entry);
+}
+
+/* Remove OBJ from the reverse mapping. SPACE must be
+ write-locked. */
+static inline ipc_entry_t
+ipc_reverse_remove(ipc_space_t space,
+ ipc_object_t obj)
+{
+ assert(space != IS_NULL);
+ assert(obj != IO_NULL);
+ return rdxtree_remove(&space->is_reverse_map, KEY(obj));
+}
+
+/* Remove all entries from the reverse mapping. SPACE must be
+ write-locked. */
+static inline void
+ipc_reverse_remove_all(ipc_space_t space)
+{
+ assert(space != IS_NULL);
+ rdxtree_remove_all(&space->is_reverse_map);
+ assert(space->is_reverse_map.height == 0);
+ assert(space->is_reverse_map.root == NULL);
+}
+
+/* Return ENTRY related to OBJ, or NULL if no such entry is found in
+ the reverse mapping. SPACE must be read-locked or
+ write-locked. */
+static inline ipc_entry_t
+ipc_reverse_lookup(ipc_space_t space,
+ ipc_object_t obj)
+{
+ assert(space != IS_NULL);
+ assert(obj != IO_NULL);
+ return rdxtree_lookup(&space->is_reverse_map, KEY(obj));
+}
+
+#undef KEY
+
#endif /* _IPC_IPC_SPACE_H_ */
--
2.1.4
|