- 经过前面几篇文章得分析,可以大致知晓从native层往下是如何进行SM的获取,那么这篇文章将着手从JAVA层开始梳理service服务端如何添加服务到SM中的
addService()整体流程简介
-
照例先给出整个流程的时序图 -
addService整个流程的示意图如下:
AMS启动
-
本篇以AMS的addService为例进行梳理分析,其他的服务也都是大同小异,这里简单说一下AMS服务的启动 -
AMS是由SystemServer进行开启的,我们从AMS开启入手。 代码路径:android/framework/base/services/java/com/android/server/SystemServer.java public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
....
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
try {
t.traceBegin("StartServices");
startBootstrapServices(t);
startCoreServices(t);
startOtherServices(t);
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
t.traceEnd();
}
....
}
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
...
t.traceBegin("StartActivityManager");
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
t.traceEnd();
...
t.traceBegin("SetSystemProcess");
mActivityManagerService.setSystemProcess();
t.traceEnd();
....
}
到这里,AMS就已经启动了,接下来就是进行服务的注册了,AMS向SM中注册都是在setSystemProcess() 函数中完成的,接下来进一步进行分析
AMS的注册
1. ActivityManagerService.setSystemProcess()
代码路径: android/framework/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this), false,
DUMP_FLAG_PRIORITY_HIGH);
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this),
false, DUMP_FLAG_PRIORITY_CRITICAL);
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ServiceManager.addService("cacheinfo", new CacheBinder(this));
...
}
查看setSystemProcess() 方法,可以看到通过ServiceManager.addService() 方法将AMS相关信息都注册到了SM中,但是这里就有了疑问,SM是在哪里进行获取的呢?带着这个疑问继续分析。
2. ServiceManager.addService()
代码路径:android/framework/base/services/core/java/com/android/os/ServiceManager.java
public static void addService(String name, IBinder service, boolean allowIsolated,
int dumpPriority) {
try {
getIServiceManager().addService(name, service, allowIsolated, dumpPriority);
} catch (RemoteException e) {
Log.e(TAG, "error in addService", e);
}
}
-
看到此处,针对上面的SM是在哪里获取的问题大致有了一个猜测,应该就是通过getIServiceManager() 方法去获取的,那么究竟对不对呢,展开分析一下getIServiceManager() 代码路径:android/framework/base/services/core/java/com/android/os/ServiceManager.java private static IServiceManager getIServiceManager() {
if (sServiceManager != null) {
return sServiceManager;
}
sServiceManager = ServiceManagerNative.
asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));
return sServiceManager;
}
-
确实如上面的猜测,getIServiceManager() 就是返回了一个IServiceManager对象,并且保证了单例,如果已存在IServiceManager对象,那么就直接返回,否则新建后返回,接下来看一下IServiceManager对象是如何新建的,同样将创建的代码进行拆分:
-
BinderInternal.getContextObject() 代码路径:android/frameworks/base/core/java/com/android/internal/os/BinderInternal.java
public static final native IBinder getContextObject();
发现getContextObject() 是一个native方法,会通过JNI层再往下调用,如何调用到JNI,调用哪个JNI方法,可以看前面的<binder JNI注册>文章,这里不做赘述 代码路径:android/frameworks/base/core/jni/android_util_Binder.cpp static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)
{
sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
return javaObjectForIBinder(env, b);
}
-
获取BpBinder对象的方法和defaultServiceManager() 方法里获取BpBinder的方式是完全一样的,这里就不展开了,接下来就着重看一下javaObjectForIBinder() 方法 代码路径:android/frameworks/base/core/jni/android_util_Binder.cpp
jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val)
{
if (val == NULL) return NULL;
if (val->checkSubclass(&gBinderOffsets)) {
jobject object = static_cast<JavaBBinder*>(val.get())->object();
LOGDEATH("objectForBinder %p: it's our own %p!\n", val.get(), object);
return object;
}
BinderProxyNativeData* nativeData = new BinderProxyNativeData();
nativeData->mOrgue = new DeathRecipientList;
nativeData->mObject = val;
jobject object = env->CallStaticObjectMethod(gBinderProxyOffsets.mClass,
gBinderProxyOffsets.mGetInstance, (jlong) nativeData, (jlong) val.get());
if (env->ExceptionCheck()) {
return NULL;
}
BinderProxyNativeData* actualNativeData = getBPNativeData(env, object);
if (actualNativeData == nativeData) {
uint32_t numProxies = gNumProxies.fetch_add(1, std::memory_order_relaxed);
uint32_t numLastWarned = gProxiesWarned.load(std::memory_order_relaxed);
if (numProxies >= numLastWarned + PROXY_WARN_INTERVAL) {
if (gProxiesWarned.compare_exchange_strong(numLastWarned,
numLastWarned + PROXY_WARN_INTERVAL, std::memory_order_relaxed)) {
ALOGW("Unexpectedly many live BinderProxies: %d\n", numProxies);
}
}
} else {
delete nativeData;
}
return object;
}
-
有几个点需要再次说明一下
-
gBinderProxyOffsets结构体的初始化,是在int_register_android_os_BinderProxy() 中完成的,在JNI注册的时候就会调用 -
gBinderOffsets结构体的初始化,是在int_register_android_os_Binder() 中完成的,同样是在JNI注册的时候调用的 -
如果val->checkSubclass(&gBinderOffsets) 返回为TRUE,为什么说返回的是BBinder对象? 首先,object是通过方法:static_cast<JavaBBinder*>(val.get())->object() 获取的,那么同样拆分后进行分析
- 先看
val.get() 代码路径:android/frameworks/base/core/jni/android_util_Binder.cpp sp<JavaBBinder> get(JNIEnv* env, jobject obj)
{
AutoMutex _l(mLock);
sp<JavaBBinder> b = mBinder.promote();
if (b == NULL) {
b = new JavaBBinder(env, obj);
mBinder = b;
ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n",
b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());
}
return b;
}
发现会在get() 方法中进行新建JavaBBinder JavaBBinder(JNIEnv* env, jobject object) : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object))
- 这里的object是BBinder,也就是android.os.binder在native层的JNI对象
JavaBBinder->object() jobject object() const{
最终BinderInternal.getContextObject() 在addService()时返回的是BinderProxy对象,也就是BpBinder -
ServiceManagerNative.asInterface(new BinderProxy()) 代码路径:android/framework/base/core/java/android/os/ServiceManagerNative.java static public IServiceManager asInterface(IBinder obj){ if (obj == null) { return null; }
-
如注释,因为obj是BinderProxy对象,所以对应queryLocalInterface() 返回的就是NULL,接下去就会新建ServiceManagerProxy对象 public ServiceManagerProxy(IBinder remote) {
JNI和JAVA层各个Binder类的关系图
- 至此Binder的整体解析,包含ServiceManager的整个结构就告一段落了,整一个部分,关于客户端和服务端各个Binder对象的关系再进行梳理一下,如下图所示:
- JavaBBinder继承自本地框架的BBinder,代表binder service服务端实体,而JavaBBinderHolder保存JavaBBinder指针,Java层Binder的mObject保存的是JavaBBinderHolder指针的值(android_os_Binder_init函数的env->SetLongField(obj, gBinderOffsets.mObject, (jlong)jbh);),故这里用聚合关系表示。JavaBBinder中的mObject保存的是android.os.Binder在native层的JNI对象,BinderProxy的mObject保存的是BpBinder对象指针的值,故此这里用聚合关系表示
?
|