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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* An async worker thread to handle certain heap operations that
* need to be done in a separate thread to avoid synchronization
* problems. HeapWorkers and reference clearing/enqueuing are
* handled by this thread.
*/
#include "Dalvik.h"
#include "HeapInternal.h"
#include <sys/time.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h> // for ETIMEDOUT, etc.
static void* heapWorkerThreadStart(void* arg);
/*
* Initialize any HeapWorker state that Heap.c
* cares about. This lets the GC start before the
* HeapWorker thread is initialized.
*/
void dvmInitializeHeapWorkerState()
{
assert(!gDvm.heapWorkerInitialized);
dvmInitMutex(&gDvm.heapWorkerLock);
pthread_cond_init(&gDvm.heapWorkerCond, NULL);
pthread_cond_init(&gDvm.heapWorkerIdleCond, NULL);
gDvm.heapWorkerInitialized = true;
}
/*
* Crank up the heap worker thread.
*
* Does not return until the thread is ready for business.
*/
bool dvmHeapWorkerStartup(void)
{
assert(!gDvm.haltHeapWorker);
assert(!gDvm.heapWorkerReady);
assert(gDvm.heapWorkerHandle == 0);
assert(gDvm.heapWorkerInitialized);
/* use heapWorkerLock/heapWorkerCond to communicate readiness */
dvmLockMutex(&gDvm.heapWorkerLock);
//BUG: If a GC happens in here or in the new thread while we hold the lock,
// the GC will deadlock when trying to acquire heapWorkerLock.
if (!dvmCreateInternalThread(&gDvm.heapWorkerHandle,
"HeapWorker", heapWorkerThreadStart, NULL))
{
dvmUnlockMutex(&gDvm.heapWorkerLock);
return false;
}
/*
* Wait for the heap worker to come up. We know the thread was created,
* so this should not get stuck.
*/
while (!gDvm.heapWorkerReady) {
int cc = pthread_cond_wait(&gDvm.heapWorkerCond, &gDvm.heapWorkerLock);
assert(cc == 0);
}
dvmUnlockMutex(&gDvm.heapWorkerLock);
return true;
}
/*
* Shut down the heap worker thread if it was started.
*/
void dvmHeapWorkerShutdown(void)
{
void* threadReturn;
/* note: assuming that (pthread_t)0 is not a valid thread handle */
if (gDvm.heapWorkerHandle != 0) {
gDvm.haltHeapWorker = true;
dvmSignalHeapWorker(true);
/*
* We may not want to wait for the heapWorkers to complete. It's
* a good idea to do so, in case they're holding some sort of OS
* resource that doesn't get reclaimed when the process exits
* (e.g. an open temp file).
*/
if (pthread_join(gDvm.heapWorkerHandle, &threadReturn) != 0)
LOGW("HeapWorker thread join failed\n");
else
LOGD("HeapWorker thread has shut down\n");
gDvm.heapWorkerReady = false;
}
}
/* Make sure that the HeapWorker thread hasn't spent an inordinate
* amount of time inside a finalizer.
*
* Aborts the VM if the thread appears to be wedged.
*
* The caller must hold the heapWorkerLock to guarantee an atomic
* read of the watchdog values.
*/
void dvmAssertHeapWorkerThreadRunning()
{
if (gDvm.gcHeap->heapWorkerCurrentObject != NULL) {
static const u8 HEAP_WORKER_WATCHDOG_TIMEOUT = 10*1000*1000LL; // 10sec
u8 heapWorkerInterpStartTime = gDvm.gcHeap->heapWorkerInterpStartTime;
u8 now = dvmGetRelativeTimeUsec();
u8 delta = now - heapWorkerInterpStartTime;
u8 heapWorkerInterpCpuStartTime =
gDvm.gcHeap->heapWorkerInterpCpuStartTime;
u8 nowCpu = dvmGetOtherThreadCpuTimeUsec(gDvm.heapWorkerHandle);
u8 deltaCpu = nowCpu - heapWorkerInterpCpuStartTime;
if (delta > HEAP_WORKER_WATCHDOG_TIMEOUT &&
(gDvm.debuggerActive || gDvm.nativeDebuggerActive))
{
/*
* Debugger suspension can block the thread indefinitely. For
* best results we should reset this explicitly whenever the
* HeapWorker thread is resumed. Unfortunately this is also
* affected by native debuggers, and we have no visibility
* into how they're manipulating us. So, we ignore the
* watchdog and just reset the timer.
*/
LOGI("Debugger is attached -- suppressing HeapWorker watchdog\n");
heapWorkerInterpStartTime = now; /* reset timer */
} else if (delta > HEAP_WORKER_WATCHDOG_TIMEOUT) {
char* desc = dexProtoCopyMethodDescriptor(
&gDvm.gcHeap->heapWorkerCurrentMethod->prototype);
LOGE("HeapWorker is wedged: %lldms spent inside %s.%s%s\n",
delta / 1000,
gDvm.gcHeap->heapWorkerCurrentObject->clazz->descriptor,
gDvm.gcHeap->heapWorkerCurrentMethod->name, desc);
free(desc);
dvmDumpAllThreads(true);
/* abort the VM */
dvmAbort();
} else if (delta > HEAP_WORKER_WATCHDOG_TIMEOUT / 2) {
char* desc = dexProtoCopyMethodDescriptor(
&gDvm.gcHeap->heapWorkerCurrentMethod->prototype);
LOGW("HeapWorker may be wedged: %lldms spent inside %s.%s%s\n",
delta / 1000,
gDvm.gcHeap->heapWorkerCurrentObject->clazz->descriptor,
gDvm.gcHeap->heapWorkerCurrentMethod->name, desc);
free(desc);
}
}
}
static void callMethod(Thread *self, Object *obj, Method *method)
{
JValue unused;
/* Keep track of the method we're about to call and
* the current time so that other threads can detect
* when this thread wedges and provide useful information.
*/
gDvm.gcHeap->heapWorkerInterpStartTime = dvmGetRelativeTimeUsec();
gDvm.gcHeap->heapWorkerInterpCpuStartTime = dvmGetThreadCpuTimeUsec();
gDvm.gcHeap->heapWorkerCurrentMethod = method;
gDvm.gcHeap->heapWorkerCurrentObject = obj;
/* Call the method.
*
* Don't hold the lock when executing interpreted
* code. It may suspend, and the GC needs to grab
* heapWorkerLock.
*/
dvmUnlockMutex(&gDvm.heapWorkerLock);
if (false) {
/* Log entry/exit; this will likely flood the log enough to
* cause "logcat" to drop entries.
*/
char tmpTag[16];
sprintf(tmpTag, "HW%d", self->systemTid);
LOG(LOG_DEBUG, tmpTag, "Call %s\n", method->clazz->descriptor);
dvmCallMethod(self, method, obj, &unused);
LOG(LOG_DEBUG, tmpTag, " done\n");
} else {
dvmCallMethod(self, method, obj, &unused);
}
dvmLockMutex(&gDvm.heapWorkerLock);
gDvm.gcHeap->heapWorkerCurrentObject = NULL;
gDvm.gcHeap->heapWorkerCurrentMethod = NULL;
gDvm.gcHeap->heapWorkerInterpStartTime = 0LL;
/* Exceptions thrown during these calls interrupt
* the method, but are otherwise ignored.
*/
if (dvmCheckException(self)) {
#if DVM_SHOW_EXCEPTION >= 1
LOGI("Uncaught exception thrown by finalizer (will be discarded):\n");
dvmLogExceptionStackTrace();
#endif
dvmClearException(self);
}
}
/* Process all enqueued heap work, including finalizers and reference
* clearing/enqueueing.
*
* Caller must hold gDvm.heapWorkerLock.
*/
static void doHeapWork(Thread *self)
{
Object *obj;
HeapWorkerOperation op;
int numFinalizersCalled, numReferencesEnqueued;
#if FANCY_REFERENCE_SUBCLASS
int numReferencesCleared = 0;
#endif
assert(gDvm.voffJavaLangObject_finalize >= 0);
#if FANCY_REFERENCE_SUBCLASS
assert(gDvm.voffJavaLangRefReference_clear >= 0);
assert(gDvm.voffJavaLangRefReference_enqueue >= 0);
#else
assert(gDvm.methJavaLangRefReference_enqueueInternal != NULL);
#endif
numFinalizersCalled = 0;
numReferencesEnqueued = 0;
while ((obj = dvmGetNextHeapWorkerObject(&op)) != NULL) {
Method *method = NULL;
/* Make sure the object hasn't been collected since
* being scheduled.
*/
assert(dvmIsValidObject(obj));
/* Call the appropriate method(s).
*/
if (op == WORKER_FINALIZE) {
numFinalizersCalled++;
method = obj->clazz->vtable[gDvm.voffJavaLangObject_finalize];
assert(dvmCompareNameDescriptorAndMethod("finalize", "()V",
method) == 0);
assert(method->clazz != gDvm.classJavaLangObject);
callMethod(self, obj, method);
} else {
#if FANCY_REFERENCE_SUBCLASS
/* clear() *must* happen before enqueue(), otherwise
* a non-clear reference could appear on a reference
* queue.
*/
if (op & WORKER_CLEAR) {
numReferencesCleared++;
method = obj->clazz->vtable[
gDvm.voffJavaLangRefReference_clear];
assert(dvmCompareNameDescriptorAndMethod("clear", "()V",
method) == 0);
assert(method->clazz != gDvm.classJavaLangRefReference);
callMethod(self, obj, method);
}
if (op & WORKER_ENQUEUE) {
numReferencesEnqueued++;
method = obj->clazz->vtable[
gDvm.voffJavaLangRefReference_enqueue];
assert(dvmCompareNameDescriptorAndMethod("enqueue", "()Z",
method) == 0);
/* We call enqueue() even when it isn't overridden,
* so don't assert(!classJavaLangRefReference) here.
*/
callMethod(self, obj, method);
}
#else
assert((op & WORKER_CLEAR) == 0);
if (op & WORKER_ENQUEUE) {
numReferencesEnqueued++;
callMethod(self, obj,
gDvm.methJavaLangRefReference_enqueueInternal);
}
#endif
}
/* Let the GC collect the object.
*/
dvmReleaseTrackedAlloc(obj, self);
}
LOGV("Called %d finalizers\n", numFinalizersCalled);
LOGV("Enqueued %d references\n", numReferencesEnqueued);
#if FANCY_REFERENCE_SUBCLASS
LOGV("Cleared %d overridden references\n", numReferencesCleared);
#endif
}
/*
* The heap worker thread sits quietly until the GC tells it there's work
* to do.
*/
static void* heapWorkerThreadStart(void* arg)
{
Thread *self = dvmThreadSelf();
int cc;
UNUSED_PARAMETER(arg);
LOGV("HeapWorker thread started (threadid=%d)\n", self->threadId);
/* tell the main thread that we're ready */
dvmLockMutex(&gDvm.heapWorkerLock);
gDvm.heapWorkerReady = true;
cc = pthread_cond_signal(&gDvm.heapWorkerCond);
assert(cc == 0);
dvmUnlockMutex(&gDvm.heapWorkerLock);
dvmLockMutex(&gDvm.heapWorkerLock);
while (!gDvm.haltHeapWorker) {
struct timespec trimtime;
bool timedwait = false;
/* We're done running interpreted code for now. */
dvmChangeStatus(NULL, THREAD_VMWAIT);
/* Signal anyone who wants to know when we're done. */
cc = pthread_cond_broadcast(&gDvm.heapWorkerIdleCond);
assert(cc == 0);
/* Trim the heap if we were asked to. */
trimtime = gDvm.gcHeap->heapWorkerNextTrim;
if (trimtime.tv_sec != 0 && trimtime.tv_nsec != 0) {
struct timespec now;
#ifdef HAVE_TIMEDWAIT_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &now); // relative time
#else
struct timeval tvnow;
gettimeofday(&tvnow, NULL); // absolute time
now.tv_sec = tvnow.tv_sec;
now.tv_nsec = tvnow.tv_usec * 1000;
#endif
if (trimtime.tv_sec < now.tv_sec ||
(trimtime.tv_sec == now.tv_sec &&
trimtime.tv_nsec <= now.tv_nsec))
{
size_t madvisedSizes[HEAP_SOURCE_MAX_HEAP_COUNT];
/* The heap must be locked before the HeapWorker;
* unroll and re-order the locks. dvmLockHeap()
* will put us in VMWAIT if necessary. Once it
* returns, there shouldn't be any contention on
* heapWorkerLock.
*/
dvmUnlockMutex(&gDvm.heapWorkerLock);
dvmLockHeap();
dvmLockMutex(&gDvm.heapWorkerLock);
memset(madvisedSizes, 0, sizeof(madvisedSizes));
dvmHeapSourceTrim(madvisedSizes, HEAP_SOURCE_MAX_HEAP_COUNT);
dvmLogMadviseStats(madvisedSizes, HEAP_SOURCE_MAX_HEAP_COUNT);
dvmUnlockHeap();
trimtime.tv_sec = 0;
trimtime.tv_nsec = 0;
gDvm.gcHeap->heapWorkerNextTrim = trimtime;
} else {
timedwait = true;
}
}
/* sleep until signaled */
if (timedwait) {
#ifdef HAVE_TIMEDWAIT_MONOTONIC
cc = pthread_cond_timedwait_monotonic(&gDvm.heapWorkerCond,
&gDvm.heapWorkerLock, &trimtime);
#else
cc = pthread_cond_timedwait(&gDvm.heapWorkerCond,
&gDvm.heapWorkerLock, &trimtime);
#endif
assert(cc == 0 || cc == ETIMEDOUT || cc == EINTR);
} else {
cc = pthread_cond_wait(&gDvm.heapWorkerCond, &gDvm.heapWorkerLock);
assert(cc == 0);
}
/* dvmChangeStatus() may block; don't hold heapWorkerLock.
*/
dvmUnlockMutex(&gDvm.heapWorkerLock);
dvmChangeStatus(NULL, THREAD_RUNNING);
dvmLockMutex(&gDvm.heapWorkerLock);
LOGV("HeapWorker is awake\n");
/* Process any events in the queue.
*/
doHeapWork(self);
}
dvmUnlockMutex(&gDvm.heapWorkerLock);
LOGD("HeapWorker thread shutting down\n");
return NULL;
}
/*
* Wake up the heap worker to let it know that there's work to be done.
*/
void dvmSignalHeapWorker(bool shouldLock)
{
int cc;
if (shouldLock) {
dvmLockMutex(&gDvm.heapWorkerLock);
}
cc = pthread_cond_signal(&gDvm.heapWorkerCond);
assert(cc == 0);
if (shouldLock) {
dvmUnlockMutex(&gDvm.heapWorkerLock);
}
}
/*
* Block until all pending heap worker work has finished.
*/
void dvmWaitForHeapWorkerIdle()
{
int cc;
assert(gDvm.heapWorkerReady);
dvmChangeStatus(NULL, THREAD_VMWAIT);
dvmLockMutex(&gDvm.heapWorkerLock);
/* Wake up the heap worker and wait for it to finish. */
//TODO(http://b/issue?id=699704): This will deadlock if
// called from finalize(), enqueue(), or clear(). We
// need to detect when this is called from the HeapWorker
// context and just give up.
dvmSignalHeapWorker(false);
cc = pthread_cond_wait(&gDvm.heapWorkerIdleCond, &gDvm.heapWorkerLock);
assert(cc == 0);
dvmUnlockMutex(&gDvm.heapWorkerLock);
dvmChangeStatus(NULL, THREAD_RUNNING);
}
/*
* Do not return until any pending heap work has finished. This may
* or may not happen in the context of the calling thread.
* No exceptions will escape.
*/
void dvmRunFinalizationSync()
{
if (gDvm.zygote) {
assert(!gDvm.heapWorkerReady);
/* When in zygote mode, there is no heap worker.
* Do the work in the current thread.
*/
dvmLockMutex(&gDvm.heapWorkerLock);
doHeapWork(dvmThreadSelf());
dvmUnlockMutex(&gDvm.heapWorkerLock);
} else {
/* Outside of zygote mode, we can just ask the
* heap worker thread to do the work.
*/
dvmWaitForHeapWorkerIdle();
}
}
/*
* Requests that dvmHeapSourceTrim() be called no sooner
* than timeoutSec seconds from now. If timeoutSec
* is zero, any pending trim is cancelled.
*
* Caller must hold heapWorkerLock.
*/
void dvmScheduleHeapSourceTrim(size_t timeoutSec)
{
GcHeap *gcHeap = gDvm.gcHeap;
struct timespec timeout;
if (timeoutSec == 0) {
timeout.tv_sec = 0;
timeout.tv_nsec = 0;
/* Don't wake up the thread just to tell it to cancel.
* If it wakes up naturally, we can avoid the extra
* context switch.
*/
} else {
struct timeval now;
#ifdef HAVE_TIMEDWAIT_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &timeout);
timeout.tv_sec += timeoutSec;
#else
gettimeofday(&now, NULL);
timeout.tv_sec = now.tv_sec + timeoutSec;
timeout.tv_nsec = now.tv_usec * 1000;
#endif
dvmSignalHeapWorker(false);
}
gcHeap->heapWorkerNextTrim = timeout;
}
|