BLCR针对 Android zygote 镜像快照,第一次启动Checkpoint一次,再次启动直接恢复之前的checkpoint 即可,以实现快速启动的目的。
1. 准备BCLR
blcr-0.8.5,截止目前最新的版本是这个
A machine that is running a supported architecture (x86 and x86_64 are fully supported and PPC/PPC64 and ARM are "Experimental") and Linux kernel 2.6.x or 3.x.y.
2. 编译BLCR ,移植,编译成KO
ifeq ($(BLCR),true)
LOCAL_PATH := $(call my-dir)
vm_linux_path = $(word 1, $(subst /vm_linux/,/vm_linux /, $(shell pwd)))
$(shell (mkdir -p $(vm_linux_path)/android/blcr-0.8.5/include/linux))
$(shell (ln -sf $(vm_linux_path)/chiling/kernel/linux-3.0/include/linux/kmalloc_sizes.h $(vm_linux_path)/android/blcr-0.8.5/include/linux/))
ifndef my_local_flags
export my_local_flags = -DHAVE_FTB=0 -DCRI_DEBUG=0 -DCR_KERNEL_TRACING=0 -DCR_RESTORE_IDS=0 -DLIBCR_TRACING=0 -DCR_STACK_GROWTH=-1
endif
#libcr_omit.so
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := libcutils \
libdl \
libc
LOCAL_SRC_FILES := libcr/cr_omit.c
LOCAL_C_INCLUDES := \
$(KERNEL_HEADERS) \
$(LOCAL_PATH)/include \
$(LOCAL_PATH) \
$(LOCAL_PATH)/libcr/ \
$(LOCAL_PATH)/libcr/arch/arm/ \
$(LOCAL_PATH)/../frameworks/base/include/utils
LOCAL_CFLAGS := -DLIYI $(my_local_flags)
LOCAL_MODULE := libcr_omit
LOCAL_MODULE_TAGS := eng
include $(BUILD_SHARED_LIBRARY)
#libcr_run.so
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := libcutils \
libdl \
libc
LOCAL_SRC_FILES:= libcr/cr_run.c
LOCAL_C_INCLUDES := \
$(KERNEL_HEADERS) \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/ \
$(LOCAL_PATH)/libcr/ \
$(LOCAL_PATH)/libcr/arch/arm/ \
$(LOCAL_PATH)/../frameworks/base/include/utils
LOCAL_CFLAGS := -DLIYI $(my_local_flags)
LOCAL_MODULE := libcr_run
LOCAL_MODULE_TAGS := eng
include $(BUILD_SHARED_LIBRARY)
#libcr.so
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_LDLIBS := -lpthread
LOCAL_SHARED_LIBRARIES := libcutils \
libdl \
libc
LOCAL_SRC_FILES:= libcr/cr_async.c \
libcr/cr_trace.c \
libcr/cr_core.c \
libcr/cr_sig_sync.c \
libcr/cr_cs.c \
libcr/cr_pthread.c \
libcr/cr_strerror.c \
libcr/cr_request.c \
libcr/cr_syscall.c \
libcr/cr_omit.c \
libcr/cr_run.c \
libcr/crut_util.c \
libcr/crut_util_libcr.c \
libcr/crut_util_pth.c
LOCAL_C_INCLUDES := \
$(KERNEL_HEADERS) \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/ \
$(LOCAL_PATH)/libcr/ \
$(LOCAL_PATH)/libcr/arch/arm/ \
$(LOCAL_PATH)/../frameworks/base/include/utils
LOCAL_CFLAGS := -DLIYI -g $(my_local_flags)
LOCAL_MODULE := libcr
LOCAL_MODULE_TAGS := eng
include $(BUILD_SHARED_LIBRARY)
# cr_checkpoint
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libcutils
LOCAL_SHARED_LIBRARIES := libdl \
libc \
libcr_run \
libcr
LOCAL_SRC_FILES := util/cr_checkpoint/cr_checkpoint.c
LOCAL_C_INCLUDES := \
$(KERNEL_HEADERS) \
$(LOCAL_PATH)/include
LOCAL_MODULE := cr_checkpoint
LOCAL_MODULE_TAGS := eng
include $(BUILD_EXECUTABLE)
$(call dist-for-goals,droid,$(LOCAL_BUILT_MODULE))
# cr_restart
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libcutils
LOCAL_SHARED_LIBRARIES := libdl \
libc \
libcr_run \
libcr
LOCAL_SRC_FILES := util/cr_restart/cr_restart.c
LOCAL_C_INCLUDES := \
$(KERNEL_HEADERS) \
$(LOCAL_PATH)/include
LOCAL_MODULE := cr_restart
LOCAL_MODULE_TAGS := eng
include $(BUILD_EXECUTABLE)
$(call dist-for-goals,droid,$(LOCAL_BUILT_MODULE))
endif
3. frameworks添加checkpoint
ZygoteInit.java:
...
...
import android.blcr.CheckPoint;
/**
* Startup class for the zygote process.
*
* Pre-initializes some classes, and then waits for commands on a UNIX domain
* socket. Based on these commands, forks off child processes that inherit
* the initial state of the VM.
*
* Please see {@link ZygoteConnection.Arguments} for documentation on the
* client protocol.
*
* @hide
*/
public class ZygoteInit {
private static final String TAG = "Zygote";
...
...
public static void main(String argv[]) {
CheckPoint cp = new CheckPoint();
try {
// Start profiling the zygote initialization.
SamplingProfilerIntegration.start();
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
SystemClock.uptimeMillis());
preload();
cp.checkPoint("/data/zygote.ss");
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
SystemClock.uptimeMillis());
registerZygoteSocket();
// Finish profiling the zygote initialization.
SamplingProfilerIntegration.writeZygoteSnapshot();
// Do an initial gc to clean up after startup
gc();
...
...
添加CheckPoint.java 接口:
frameworks/base/core/java/android/blcr/CheckPoint.java
package android.blcr;
/**
* @hide
*/
public class CheckPoint
{
public CheckPoint()
{
}
/**
* @hide
*/
public native void checkPoint(String name);
}
4. 修改init.rc 启动服务,安装ko
5. 增加BLCR脚本
#!/sbin/sh
ec()
{
/system/bin/busybox echo $* > /dev/console;
}
if [ -f /data/zygote.ss ]; then
ec "*********************load saved zygote*******************"
/system/bin/cr_restart --no-restore-pid -f /data/zygote.ss
rm /data/zygote.ss*
sync
else
ec "optimised by blcr"
ec "*********************srart a new zygote******************"
/system/bin/app_process $*
fi
6. 修改device/ .mk文件增加编译拷贝打包
ifeq ($(BLCR),true)
PRODUCT_COPY_FILES += \
device/mtk/configs/zygote-blcr.rc:root/zygote.rc \
device/mtk/configs/quickboot.sh:system/bin/quickboot.sh \
device/mtk/OUT/blcr.ko:root/blcr.ko
else
PRODUCT_COPY_FILES += \
device/mtk/configs/zygote-normal.rc:root/zygote.rc
endif
另外还涉及JNI等等。