Debian Codegraph @

runtime/dyninst/map_runtime.h

raw
1 /* -*- linux-c -*-
2 * Map Runtime Functions
3 * Copyright (C) 2012-2016 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #ifndef _STAPDYN_MAP_RUNTIME_H_
12 #define _STAPDYN_MAP_RUNTIME_H_
13
14 #include <pthread.h>
15
16 #define MAP_GET_CPU() STAT_GET_CPU()
17 #define MAP_PUT_CPU() STAT_PUT_CPU()
18
19 struct pmap {
20 int bit_shift; /* scale factor for integer arithmetic */
21 int stat_ops; /* related statistical operators */
22 offptr_t oagg; /* aggregation map */
23 offptr_t omap[]; /* per-cpu maps */
24 };
25
26 static inline MAP _stp_pmap_get_agg(PMAP p)
27 {
28 return offptr_get(&p->oagg);
29 }
30
31 static inline void _stp_pmap_set_agg(PMAP p, MAP agg)
32 {
33 offptr_set(&p->oagg, agg);
34 }
35
36 static inline MAP _stp_pmap_get_map(PMAP p, unsigned cpu)
37 {
38 if (cpu >= _stp_runtime_num_contexts)
39 cpu = 0;
40 return offptr_get(&p->omap[cpu]);
41 }
42
43 static inline void _stp_pmap_set_map(PMAP p, MAP m, unsigned cpu)
44 {
45 if (cpu >= _stp_runtime_num_contexts)
46 cpu = 0;
47 offptr_set(&p->omap[cpu], m);
48 }
49
50
51 static void __stp_map_del(MAP map)
52 {
53 }
54
55
56 /** Deletes a map.
57 * Deletes a map, freeing all memory in all elements.
58 * Normally done only when the module exits.
59 * @param map
60 */
61
62 static void _stp_map_del(MAP map)
63 {
64 __stp_map_del(map);
65 _stp_shm_free(map);
66 }
67
68 static void _stp_pmap_del(PMAP pmap)
69 {
70 int i;
71
72 /* The pmap is one giant allocation, so do only
73 * the basic cleanup for each map. */
74 for_each_possible_cpu(i)
75 __stp_map_del(_stp_pmap_get_map (pmap, i));
76 __stp_map_del(_stp_pmap_get_agg(pmap));
77 _stp_shm_free(pmap);
78 }
79
80
81 static int
82 _stp_map_init(MAP m, unsigned max_entries, unsigned hash_table_mask, int wrap, int node_size)
83 {
84 unsigned i;
85
86 /* The node memory is allocated right after the map (incl. the hash table). */
87 void *node_mem = (void*)(m + 1) + sizeof(struct mhlist_head)*(hash_table_mask+1);
88
89 INIT_MLIST_HEAD(&m->pool);
90 INIT_MLIST_HEAD(&m->head);
91 m->hash_table_mask = hash_table_mask;
92 for (i = 0; i <= m->hash_table_mask; i++)
93 INIT_MHLIST_HEAD(&m->hashes[i]);
94
95 m->maxnum = max_entries;
96 m->wrap = wrap;
97
98 for (i = 0; i < max_entries; i++) {
99 struct map_node *node = node_mem + i * node_size;
100 mlist_add(&node->lnode, &m->pool);
101 INIT_MHLIST_NODE(&node->hnode);
102 }
103
104 return 0;
105 }
106
107
108 /** Create a new map.
109 * Maps must be created at module initialization time.
110 * @param max_entries The maximum number of entries allowed. Currently that
111 * number will be preallocated.If more entries are required, the oldest ones
112 * will be deleted. This makes it effectively a circular buffer.
113 * @return A MAP on success or NULL on failure.
114 * @ingroup map_create
115 */
116
117 static MAP
118 _stp_map_new(unsigned max_entries, int wrap, int node_size,
119 int cpu __attribute((unused)))
120 {
121 MAP m;
122
123 /* NB: Allocate the map in one big chuck.
124 * (See _stp_pmap_new for more explanation) */
125 unsigned hash_table_mask = HASHTABLESIZE(max_entries)-1; /* usable as bitmask */
126 size_t map_size = sizeof(struct map_root)
127 + sizeof(struct mhlist_head) * (hash_table_mask+1)
128 + node_size * max_entries;
129 m = _stp_shm_zalloc(map_size);
130 if (m == NULL)
131 return NULL;
132
133 if (_stp_map_init(m, max_entries, hash_table_mask, wrap, node_size)) {
134 _stp_map_del(m);
135 return NULL;
136 }
137 return m;
138 }
139
140 static PMAP
141 _stp_pmap_new(unsigned max_entries, int wrap, int node_size)
142 {
143 int i;
144 MAP m;
145 PMAP pmap;
146 void *map_mem;
147 unsigned hash_table_mask = HASHTABLESIZE(max_entries)-1; /* usable as bitmask */
148
149 /* Allocate the pmap in one big chuck.
150 *
151 * The reason for this is that we're allocating in the shared memory
152 * mmap, which may have to move locations in order to grow. If some
153 * smaller unit of the pmap allocation were to cause the whole thing to
154 * move, then we'd lose track of the prior allocations.
155 *
156 * Once returned from here, we'll always access the pmap via the global
157 * shared memory base. So if other map/pmap/stat/etc. allocations
158 * cause it to move later, that's ok.
159 */
160
161 size_t map_size = sizeof(struct map_root)
162 + sizeof(struct mhlist_head) * (hash_table_mask+1)
163 + node_size * max_entries;
164 size_t pmap_size = sizeof(struct pmap) +
165 sizeof(offptr_t) * _stp_runtime_num_contexts;
166 size_t total_size = pmap_size +
167 map_size * (_stp_runtime_num_contexts + 1);
168
169 map_mem = pmap = _stp_shm_zalloc(total_size);
170 if (pmap == NULL)
171 return NULL;
172 map_mem += pmap_size;
173
174 for_each_possible_cpu(i)
175 _stp_pmap_set_map(pmap, NULL, i);
176 _stp_pmap_set_agg(pmap, NULL);
177
178 /* Initialize the per-cpu maps. */
179 for_each_possible_cpu(i) {
180 m = map_mem;
181 if (_stp_map_init(m, max_entries, hash_table_mask, wrap, node_size) != 0)
182 goto err;
183 _stp_pmap_set_map(pmap, m, i);
184 map_mem += map_size;
185 }
186
187 /* Initialize the aggregate map. */
188 m = map_mem;
189 if (_stp_map_init(m, max_entries, hash_table_mask, wrap, node_size) != 0)
190 goto err;
191 _stp_pmap_set_agg(pmap, m);
192
193 return pmap;
194
195 err:
196 _stp_pmap_del(pmap);
197 return NULL;
198 }
199
200 #endif /* _STAPDYN_MAP_RUNTIME_H_ */