-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathomrtraceapi.cpp
304 lines (263 loc) · 10.5 KB
/
omrtraceapi.cpp
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
/*******************************************************************************
* Copyright IBM Corp. and others 2014
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include <string.h>
#include "omrport.h"
#include "omr.h"
#include "omragent.h"
#include "omrtrace_internal.h"
#include "pool_api.h"
#include "omrutil.h"
extern const char *UT_NO_THREAD_NAME;
#define OMR_TRACE_THREAD_FROM_VMTHREAD_NONNULL(thr) ((thr)->_trace.omrTraceThread)
/* Calls to control the trace engine life cycle, the sequence is ([] are optional)
* Note: These functions will be renamed under task 65134
* 1 - fillInUTInterfaces - Setup function pointers in interfaces via fillInUTInterfaces
* 2 - initializeTrace - Tell trace to intialise via initializeTrace supplying default options.
* (Usually just the location of the .dat file is standard)
* 3 - threadStart - Notify trace about the current thread since it started before trace was available via threadStart
* - Trace should be notified about about threads beginning and ending via calls to threadStart and
* threadStop when they start and end.
* [6] - setOptions - set any additional default options or process any options specified on the command line.
* 8 - Initialize port, thread and any other modules that may have loaded before trace was ready.
* 9 - The real work happens here, hopefully for a long time.
* 10 - utTerminateTrace - Tell the trace engine to shutdown.
* 11 - freeTrace - Tell the trace engine to free its internal data structures.
*
*/
#if defined(OMR_THR_FORK_SUPPORT)
static void postForkCleanupGlobals(OMR_TraceThread *thr);
static void postForkCleanupBuffers(OMR_TraceThread *thr);
static void postForkCleanupThreads(OMR_TraceThread *thr);
#endif /* defined(OMR_THR_FORK_SUPPORT) */
omr_error_t
omr_trc_startup(OMR_VM *omrVM, const OMR_TraceLanguageInterface *languageIntf, const char *datDir, const char **opts)
{
omr_error_t rc = OMR_ERROR_NONE;
OMRTraceEngine *newTrcEngine = NULL;
OMRPORT_ACCESS_FROM_OMRVM(omrVM);
newTrcEngine = (OMRTraceEngine *)omrmem_allocate_memory(sizeof(*newTrcEngine), OMRMEM_CATEGORY_TRACE);
if (NULL == newTrcEngine) {
rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY;
omrtty_printf("omr_trc_startup: Failed to alloc newTrcEngine, rc=%d\n", rc);
goto done;
}
rc = fillInUTInterfaces(&newTrcEngine->utIntf, &newTrcEngine->omrTraceIntfS, &newTrcEngine->utModuleIntfS);
if (OMR_ERROR_NONE != rc) {
omrtty_printf("omr_trc_startup: fillInUTInterfaces failed, rc=%d\n", rc);
goto done;
}
/* Set the path to the .dat file or -Xtrace:print doesn't work. */
rc = initializeTrace(omrVM, datDir, languageIntf);
if (OMR_ERROR_NONE != rc) {
omrtty_printf("omr_trc_startup: initializeTrace failed, rc=%d\n", rc);
goto done;
}
OMR_TRACEGLOBAL(initState) = OMR_TRACE_ENGINE_ENABLED;
if (NULL != opts) {
rc = setOptions(NULL, opts, FALSE);
if (OMR_ERROR_NONE != rc) {
omrtty_printf("omr_trc_startup: failed to set trace options, rc=%d\n", rc);
goto done;
}
}
omrVM->_trcEngine = newTrcEngine;
done:
return rc;
}
omr_error_t
omr_trc_shutdown(OMR_VMThread *currentThread)
{
omr_error_t rc = OMR_ERROR_NONE;
OMR_TraceThread **thrSlot = &OMR_TRACE_THREAD_FROM_VMTHREAD_NONNULL(currentThread);
if (NULL == *thrSlot) {
/* The current thread is not attached to the trace engine. */
rc = OMR_THREAD_NOT_ATTACHED;
} else {
/*
* Shut trace down.
* Because the current thread must be attached to the trace engine, we can
* guarantee that at least one exiting thread will notice the shutdown state
* and free the trace engine data.
*/
rc = utTerminateTrace();
if (OMR_ERROR_NONE == rc) {
rc = threadStop(thrSlot);
}
}
return rc;
}
void
omr_trc_startMultiThreading(OMR_VM *omrVM)
{
if (omrVM->_trcEngine) {
OMR_TRACEGLOBAL(initState) = OMR_TRACE_ENGINE_MT_ENABLED;
}
}
omr_error_t
omr_trc_startThreadTrace(OMR_VMThread *currentThread, const char *threadName)
{
omr_error_t rc = OMR_ERROR_NONE;
if (NULL != omrTraceGlobal) {
if (OMR_TRACE_ENGINE_MT_ENABLED != OMR_TRACEGLOBAL(initState)) {
rc = OMR_ERROR_NOT_AVAILABLE;
} else {
rc = threadStart(&OMR_TRACE_THREAD_FROM_VMTHREAD_NONNULL(currentThread),
((NULL == currentThread->_language_vmthread)? currentThread : currentThread->_language_vmthread),
threadName,
currentThread->_os_thread,
currentThread);
}
}
return rc;
}
omr_error_t
omr_trc_stopThreadTrace(OMR_VMThread *currentThread)
{
omr_error_t rc = OMR_ERROR_NONE;
if (NULL == currentThread) {
rc = OMR_ERROR_ILLEGAL_ARGUMENT;
} else {
OMR_TraceThread **thrSlot = &OMR_TRACE_THREAD_FROM_VMTHREAD_NONNULL(currentThread);
if (NULL == *thrSlot) {
/* Do nothing if the thread is not attached to the trace engine. */
} else {
/* It's safe to read omrTraceGlobal because at least one thread is attached to the trace engine.
* OMRTODO The initState check may not be needed here.
*/
if ((NULL != omrTraceGlobal) && (OMR_TRACEGLOBAL(initState) >= OMR_TRACE_ENGINE_MT_ENABLED)) {
rc = threadStop(thrSlot);
}
}
}
return rc;
}
#if defined(OMR_THR_FORK_SUPPORT)
void
omr_trc_preForkHandler(void)
{
if ((NULL != omrTraceGlobal) && (OMR_TRACE_ENGINE_MT_ENABLED == OMR_TRACEGLOBAL(initState))) {
/* Disable trace points until the post fork handler is called.
* Trace must be disabled while the monitors are held.
*/
OMR_TraceThread *thr = twThreadSelf();
if (NULL != thr) {
incrementRecursionCounter(thr);
}
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: requesting global subscribers lock.\n"));
omrthread_monitor_enter(OMR_TRACEGLOBAL(subscribersLock));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: obtained global subscribers lock.\n"));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: requesting global trace lock.\n"));
omrthread_monitor_enter(OMR_TRACEGLOBAL(traceLock));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: obtained global trace lock.\n"));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: requesting global thread pool lock.\n"));
omrthread_monitor_enter(OMR_TRACEGLOBAL(threadPoolLock));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: obtained global thread pool lock.\n"));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: requesting global buffer pool lock.\n"));
omrthread_monitor_enter(OMR_TRACEGLOBAL(bufferPoolLock));
UT_DBGOUT(1, ("<UT> omr_trc_preForkHandler: obtained global buffer pool lock.\n"));
}
}
void
omr_trc_postForkParentHandler(void)
{
if ((NULL != omrTraceGlobal) && (OMR_TRACE_ENGINE_MT_ENABLED == OMR_TRACEGLOBAL(initState))) {
omrthread_monitor_exit(OMR_TRACEGLOBAL(bufferPoolLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global buffer pool lock.\n"));
omrthread_monitor_exit(OMR_TRACEGLOBAL(threadPoolLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global thread pool lock.\n"));
omrthread_monitor_exit(OMR_TRACEGLOBAL(traceLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global trace lock.\n"));
omrthread_monitor_exit(OMR_TRACEGLOBAL(subscribersLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global subscribers lock.\n"));
/* This is after exiting the monitors to prevent any
* trace points while holding those locks.
*/
OMR_TraceThread *thr = twThreadSelf();
if (NULL != thr) {
decrementRecursionCounter(thr);
}
}
}
void
omr_trc_postForkChildHandler(void)
{
if ((NULL != omrTraceGlobal) && (OMR_TRACE_ENGINE_MT_ENABLED == OMR_TRACEGLOBAL(initState))) {
omrthread_monitor_exit(OMR_TRACEGLOBAL(bufferPoolLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global buffer pool lock.\n"));
omrthread_monitor_exit(OMR_TRACEGLOBAL(threadPoolLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global thread pool lock.\n"));
omrthread_monitor_exit(OMR_TRACEGLOBAL(traceLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkChildHandler: released global trace lock.\n"));
omrthread_monitor_exit(OMR_TRACEGLOBAL(subscribersLock));
UT_DBGOUT(1, ("<UT> omr_trc_postForkParentHandler: released global subscribers lock.\n"));
OMR_TraceThread *thr = twThreadSelf();
postForkCleanupGlobals(thr);
postForkCleanupBuffers(thr);
/* This is after exiting the pool monitors to prevent any
* trace points while holding those locks.
*/
if (NULL != thr) {
decrementRecursionCounter(thr);
}
postForkCleanupThreads(thr);
}
}
void
postForkCleanupGlobals(OMR_TraceThread *thr)
{
/* Reset OMR_TRACEGLOBALs. */
if (NULL != thr) {
OMR_TRACEGLOBAL(threadCount) = 1;
} else {
OMR_TRACEGLOBAL(threadCount) = 0;
}
OMR_TRACEGLOBAL(lastPrint) = NULL;
OMR_TRACEGLOBAL(lostRecords) = 0;
}
void
postForkCleanupBuffers(OMR_TraceThread *thr)
{
/* Clear all buffers in the pool and in freeQueue. */
OMR_TRACEGLOBAL(freeQueue) = NULL;
if (NULL != thr) {
thr->trcBuf = NULL;
}
pool_clear(OMR_TRACEGLOBAL(bufferPool));
}
void
postForkCleanupThreads(OMR_TraceThread *thr)
{
J9PoolState threadPoolState;
OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary));
/* Cleanup and free the name and OMR_TraceThread for all threads which do not exist after fork. */
OMR_TraceThread *iteratingThread = (OMR_TraceThread *)pool_startDo(OMR_TRACEGLOBAL(threadPool), &threadPoolState);
while (NULL != iteratingThread) {
if (thr != iteratingThread) {
if ((NULL != iteratingThread->name) && (UT_NO_THREAD_NAME != iteratingThread->name)) {
omrmem_free_memory((void *)iteratingThread->name);
}
pool_removeElement(OMR_TRACEGLOBAL(threadPool), iteratingThread);
}
iteratingThread = (OMR_TraceThread *)pool_nextDo(&threadPoolState);
}
}
#endif /* defined(OMR_THR_FORK_SUPPORT) */