File: map_runtime.h

package info (click to toggle)
systemtap 2.6-0.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,220 kB
  • ctags: 10,944
  • sloc: cpp: 53,239; ansic: 50,615; exp: 33,694; sh: 9,906; xml: 7,665; perl: 2,089; python: 1,534; tcl: 1,236; makefile: 797; java: 148; lisp: 104; awk: 94; asm: 91; sed: 16
file content (240 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download
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
/* -*- linux-c -*- 
 * Map Runtime Functions
 * Copyright (C) 2012 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#ifndef _STAPDYN_MAP_RUNTIME_H_
#define _STAPDYN_MAP_RUNTIME_H_

#include <pthread.h>

#ifdef NEED_MAP_LOCKS
#define MAP_LOCK(m)	pthread_mutex_lock(&(m)->lock)
#define MAP_UNLOCK(m)	pthread_mutex_unlock(&(m)->lock)
#else
#define MAP_LOCK(sd)	do {} while (0)
#define MAP_UNLOCK(sd)	do {} while (0)
#endif

/* Note that pthread_mutex_trylock()'s return value is opposite of the
 * kernel's spin_trylock(), so we invert the return value of
 * pthread_mutex_trylock(). */
#define MAP_TRYLOCK(m)	(!pthread_mutex_trylock(&(m)->lock))

#define MAP_GET_CPU()	STAT_GET_CPU()
#define MAP_PUT_CPU()	STAT_PUT_CPU()

static int _stp_map_initialize_lock(MAP m)
{
#ifdef NEED_MAP_LOCKS
	int rc;

	if ((rc = stp_pthread_mutex_init_shared(&m->lock)) != 0) {
		_stp_error("Couldn't initialize map mutex: %d\n", rc);
		return rc;
	}
#endif
	return 0;
}

static void _stp_map_destroy_lock(MAP m)
{
#ifdef NEED_MAP_LOCKS
	(void)pthread_mutex_destroy(&m->lock);
#endif
}

struct pmap {
	offptr_t oagg;    /* aggregation map */
	offptr_t omap[];  /* per-cpu maps */
};

static inline MAP _stp_pmap_get_agg(PMAP p)
{
	return offptr_get(&p->oagg);
}

static inline void _stp_pmap_set_agg(PMAP p, MAP agg)
{
	offptr_set(&p->oagg, agg);
}

static inline MAP _stp_pmap_get_map(PMAP p, unsigned cpu)
{
	if (cpu >= _stp_runtime_num_contexts)
		cpu = 0;
	return offptr_get(&p->omap[cpu]);
}

static inline void _stp_pmap_set_map(PMAP p, MAP m, unsigned cpu)
{
	if (cpu >= _stp_runtime_num_contexts)
		cpu = 0;
	offptr_set(&p->omap[cpu], m);
}


static void __stp_map_del(MAP map)
{
	if (map == NULL)
		return;

	/* The lock is the only thing to clean up.  */
	_stp_map_destroy_lock(map);
}


/** Deletes a map.
 * Deletes a map, freeing all memory in all elements.
 * Normally done only when the module exits.
 * @param map
 */

static void _stp_map_del(MAP map)
{
	if (map == NULL)
		return;

	__stp_map_del(map);
	_stp_shm_free(map);
}

static void _stp_pmap_del(PMAP pmap)
{
	int i;

	if (pmap == NULL)
		return;

	/* The pmap is one giant allocation, so do only
	 * the basic cleanup for each map.  */
	for_each_possible_cpu(i)
		__stp_map_del(_stp_pmap_get_map (pmap, i));
	__stp_map_del(_stp_pmap_get_agg(pmap));
	_stp_shm_free(pmap);
}


static int
_stp_map_init(MAP m, unsigned max_entries, int wrap, int node_size)
{
	unsigned i;

	/* The node memory is allocated right after the map itself.  */
	void *node_mem = m + 1;

	INIT_MLIST_HEAD(&m->pool);
	INIT_MLIST_HEAD(&m->head);
	for (i = 0; i < HASH_TABLE_SIZE; i++)
		INIT_MHLIST_HEAD(&m->hashes[i]);

	m->maxnum = max_entries;
	m->wrap = wrap;

	for (i = 0; i < max_entries; i++) {
		struct map_node *node = node_mem + i * node_size;
		mlist_add(&node->lnode, &m->pool);
		INIT_MHLIST_NODE(&node->hnode);
	}

	if (_stp_map_initialize_lock(m) != 0)
		return -1;

	return 0;
}


/** Create a new map.
 * Maps must be created at module initialization time.
 * @param max_entries The maximum number of entries allowed. Currently that
 * number will be preallocated.If more entries are required, the oldest ones
 * will be deleted. This makes it effectively a circular buffer.
 * @return A MAP on success or NULL on failure.
 * @ingroup map_create
 */

static MAP
_stp_map_new(unsigned max_entries, int wrap, int node_size,
		int cpu __attribute((unused)))
{
	MAP m;

	/* NB: Allocate the map in one big chuck.
	 * (See _stp_pmap_new for more explanation) */
	size_t map_size = sizeof(struct map_root) + node_size * max_entries;
	m = _stp_shm_zalloc(map_size);
	if (m == NULL)
		return NULL;

	if (_stp_map_init(m, max_entries, wrap, node_size)) {
		_stp_map_del(m);
		return NULL;
	}
	return m;
}

static PMAP
_stp_pmap_new(unsigned max_entries, int wrap, int node_size)
{
	int i;
	MAP m;
	PMAP pmap;
	void *map_mem;

	/* Allocate the pmap in one big chuck.
	 *
	 * The reason for this is that we're allocating in the shared memory
	 * mmap, which may have to move locations in order to grow.  If some
	 * smaller unit of the pmap allocation were to cause the whole thing to
	 * move, then we'd lose track of the prior allocations.
	 *
	 * Once returned from here, we'll always access the pmap via the global
	 * shared memory base.  So if other map/pmap/stat/etc. allocations
	 * cause it to move later, that's ok.
	 */

	size_t map_size = sizeof(struct map_root) + node_size * max_entries;

	size_t pmap_size = sizeof(struct pmap) +
		sizeof(offptr_t) * _stp_runtime_num_contexts;

	size_t total_size = pmap_size +
		map_size * (_stp_runtime_num_contexts + 1);

	map_mem = pmap = _stp_shm_zalloc(total_size);
	if (pmap == NULL)
		return NULL;
	map_mem += pmap_size;

	for_each_possible_cpu(i)
                _stp_pmap_set_map(pmap, NULL, i);
        _stp_pmap_set_agg(pmap, NULL);

	/* Initialize the per-cpu maps.  */
	for_each_possible_cpu(i) {
		m = map_mem;
		if (_stp_map_init(m, max_entries, wrap, node_size) != 0)
			goto err;
                _stp_pmap_set_map(pmap, m, i);
		map_mem += map_size;
	}

	/* Initialize the aggregate map.  */
	m = map_mem;
	if (_stp_map_init(m, max_entries, wrap, node_size) != 0)
		goto err;
        _stp_pmap_set_agg(pmap, m);

	return pmap;

err:
	_stp_pmap_del(pmap);
	return NULL;
}

#endif /* _STAPDYN_MAP_RUNTIME_H_ */