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
|
/*
* Copyright (C) 2006 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.
*/
package dalvik.system;
import java.io.File;
/**
* Provides hooks for the zygote to call back into the runtime to perform
* parent or child specific initialization..
*
* @hide
*/
public final class ZygoteHooks {
private long token;
/**
* Temporary hack: check time since start time and log if over a fixed threshold.
*
*/
private static void checkTime(long startTime, String where) {
long now = System.nanoTime();
long msDuration = (now - startTime) / (1000 * 1000);
if (msDuration > 1000) {
// If we are taking more than a second, log about it.
System.logW("Slow operation: " + msDuration + "ms so far, now at " + where);
}
}
/**
* Called by the zygote prior to every fork. Each call to {@code preFork}
* is followed by a matching call to {@link #postForkChild(int)} on the child
* process and {@link #postForkCommon()} on both the parent and the child
* process. {@code postForkCommon} is called after {@code postForkChild} in
* the child process.
*/
public void preFork() {
long startTime = System.nanoTime();
Daemons.stop();
checkTime(startTime, "ZygoteHooks.Daemons.stop");
waitUntilAllThreadsStopped();
checkTime(startTime, "ZygoteHooks.waituntilallthreadsstopped");
token = nativePreFork();
checkTime(startTime, "ZygoteHooks.Daemons.nativePreFork");
}
/**
* Called by the zygote in the child process after every fork. The debug
* flags from {@code debugFlags} are applied to the child process. The string
* {@code instructionSet} determines whether to use a native bridge.
*/
public void postForkChild(int debugFlags, String instructionSet) {
nativePostForkChild(token, debugFlags, instructionSet);
}
/**
* Called by the zygote in both the parent and child processes after
* every fork. In the child process, this method is called after
* {@code postForkChild}.
*/
public void postForkCommon() {
Daemons.start();
}
private static native long nativePreFork();
private static native void nativePostForkChild(long token, int debugFlags,
String instructionSet);
/**
* We must not fork until we're single-threaded again. Wait until /proc shows we're
* down to just one thread.
*/
private static void waitUntilAllThreadsStopped() {
File tasks = new File("/proc/self/task");
while (tasks.list().length > 1) {
try {
// Experimentally, booting and playing about with a stingray, I never saw us
// go round this loop more than once with a 10ms sleep.
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
}
}
}
|