Debian Codegraph @

runtime/dyninst/runtime_context.h

raw
1 /* -*- linux-c -*-
2 * Context Runtime Functions
3 * Copyright (C) 2014 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_RUNTIME_CONTEXT_H_
12 #define _STAPDYN_RUNTIME_CONTEXT_H_
13
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <sched.h>
17 #include <pthread.h>
18 #include <asm/unistd.h>
19
20 /* Defined after stap_probes[] by translate.cxx */
21 static const char* stp_probe_point(size_t index);
22
23 /* Defined later in common_session_state.h */
24 static inline struct context* stp_session_context(size_t index);
25
26 static int _stp_runtime_num_contexts;
27
28 /* Locally-cached context pointer -- see _stp_runtime_entryfn_get_context()
29 * and _stp_runtime_entryfn_put_context(). */
30 static __thread struct context *tls_context;
31
32 static int _stp_runtime_contexts_init(void)
33 {
34 _stp_runtime_num_contexts = sysconf(_SC_NPROCESSORS_ONLN);
35 if (_stp_runtime_num_contexts < 1)
36 _stp_runtime_num_contexts = 1;
37 return 0;
38 }
39
40 static int _stp_runtime_contexts_alloc(void)
41 {
42 int i;
43
44 /* The allocation was already done in stp_session_init;
45 * we just need to initialize the context data. */
46 for (i = 0; i < _stp_runtime_num_contexts; i++) {
47 int rc;
48 struct context *c = stp_session_context(i);
49 c->data_index = i;
50 rc = stp_pthread_mutex_init_shared(&c->lock);
51 if (rc != 0) {
52 _stp_error("context mutex initialization failed");
53 return rc;
54 }
55 }
56 return 0;
57 }
58
59 /* Free the context resources.
60 *
61 * NB: This should *not* be called by every process which has mmaped the shared
62 * memory. Only the main process which created shm and originally called
63 * _stp_runtime_contexts_alloc should be the one to free it.
64 */
65 static void _stp_runtime_contexts_free(void)
66 {
67 int i;
68
69 /* The context memory is managed elsewhere;
70 * we just need to teardown the context locks. */
71 for (i = 0; i < _stp_runtime_num_contexts; i++) {
72 struct context *c = stp_session_context(i);
73 (void)pthread_mutex_destroy(&c->lock);
74 }
75 }
76
77 static int _stp_runtime_get_data_index(void)
78 {
79 int data_index;
80
81 /* If this thread has already gotten a context structure,
82 * return the data index from it. */
83 if (tls_context != NULL)
84 return tls_context->data_index;
85
86 /* This shouldn't happen. */
87 /* FIXME: assert? */
88 return 0;
89 }
90
91
92 /* Figure out with cpu we're on, which is our default data_index.
93 * Make sure the returned data index number is within the range of
94 * [0.._stp_runtime_num_contexts]. Be sure to handle a sched_getcpu()
95 * failure (it will return -1). */
96 static int _stp_context_index(void)
97 {
98 /* The current cpu is the preferred index, because it will usually be
99 * different for every concurrent thread. (It is possible to be the same
100 * though, if kernel scheduling is unkind to us.) */
101 int index = _stp_sched_getcpu();
102
103 /* Failing cpu#, use the tid as a somewhat-random index. */
104 if (index < 0)
105 index = syscall(SYS_gettid);
106
107 return index % _stp_runtime_num_contexts;
108 }
109
110 static struct context * _stp_runtime_entryfn_get_context(void)
111 {
112 struct context *c;
113 int i, index, rc, data_index;
114
115 /* If 'tls_context' (which is thread-local storage) is already set
116 * for this thread, we are re-entrant, so just quit. */
117 if (tls_context != NULL)
118 return NULL;
119
120 data_index = _stp_context_index();
121 if (unlikely(data_index < 0))
122 data_index = 0;
123
124 /* Try to find a free context structure. */
125 index = data_index;
126 for (i = 0; i < _stp_runtime_num_contexts; i++, index++) {
127 if (index >= _stp_runtime_num_contexts)
128 index = 0;
129 c = stp_session_context(index);
130 if (pthread_mutex_trylock(&c->lock) == 0) {
131 /* We found a free context structure. Now that it is
132 * locked, set the TLS pointer and return the context. */
133 tls_context = c;
134 return tls_context;
135 }
136 }
137
138 /* If we're here, we couldn't find a free context structure. Wait
139 * on one. */
140 c = stp_session_context(data_index);
141 rc = pthread_mutex_lock(&c->lock);
142 if (rc == 0) {
143 tls_context = c;
144 return tls_context;
145 }
146 return NULL;
147 }
148
149 static void _stp_runtime_entryfn_put_context(struct context *c)
150 {
151 if (c && c == tls_context) {
152 tls_context = NULL;
153 pthread_mutex_unlock(&c->lock);
154 }
155 /* else, warn about bad state? */
156 return;
157 }
158
159 static struct context *_stp_runtime_get_context(void)
160 {
161 /* Note we don't call _stp_runtime_entryfn_get_context()
162 * here. This function is called after
163 * _stp_runtime_entryfn_get_context() and has no corresponding
164 * "put" function. */
165 return tls_context;
166 }
167
168 static void _stp_runtime_context_wait(void)
169 {
170 struct timespec hold_start;
171 int hold_index;
172 int holdon;
173
174 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &hold_start);
175 hold_index = -1;
176 do {
177 int i;
178 holdon = 0;
179 struct timespec now, elapsed;
180
181 for (i = 0; i < _stp_runtime_num_contexts; i++) {
182 struct context *c = stp_session_context(i);
183 int ret = pthread_mutex_trylock(&c->lock);
184 if (ret == 0)
185 pthread_mutex_unlock(&c->lock);
186 else if (ret == EBUSY) {
187 holdon = 1;
188
189 /* Just In case things are really stuck, let's print
190 * some diagnostics. */
191 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &now);
192 _stp_timespec_sub(&now, &hold_start, &elapsed);
193
194 /* If its been > 1 second since we started and we
195 * haven't already printed a message for this stuck
196 * context, print one. */
197 if (elapsed.tv_sec > 0 && (i > hold_index)) {
198 /* NB: c->probe_point is local memory, invalid across processes,
199 * so read it indirectly through c->probe_index instead. */
200 const char* pp = stp_probe_point(c->probe_index);
201 if (pp)
202 _stp_error("context[%d] stuck: %s", i, pp);
203 else
204 _stp_error("context[%d] stuck in unknown probe %zu",
205 i, c->probe_index);
206 hold_index = i;
207 }
208 }
209 /* else other pthread error == broken? what then? */
210 }
211
212 #ifdef STAP_OVERRIDE_STUCK_CONTEXT
213 /* In case things are really really stuck, we are going to
214 * pretend/assume/hope everything is OK, and let the cleanup
215 * finish. */
216 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &now);
217 _stp_timespec_sub(&now, &hold_start, &elapsed);
218 if (elapsed.tv_sec > 10) {
219 _stp_warn("overriding stuck context to allow shutdown.");
220 holdon = 0; /* allow loop to exit */
221 }
222 #endif
223
224 if (holdon) {
225 sched_yield();
226 }
227 } while (holdon);
228 }
229
230 #endif /* _STAPDYN_RUNTIME_CONTEXT_H_ */