android 完整启动流程概括 (二)

android 应用程序进程的启动过程

Ams 在启动应用程序进程时检查这个应用程序需要的应用程序进程是否存在,不存在就会请求Zygote进程启动需要的应用程序进程;
由上节可知,Zygote的java层会创建一个Server端的Socket,用来等待AMS请求Zygote来创建新的应用程序进程;
Zygote进程Fork自身创建应用程序进程;

应用程序进程启动过程

  • AMS发送启动应用程序进程请求

    • AMS通过调用startProcessLocked方法向Zygote进程发送请求,最终通过Process.start(...)启动;
    • start方法调用ZygoteProcess.start(…),通过startViaZygote方法创建args参数;
    • 使用openZygoteSocketIfNeeded方法尝试与Zygote进程ServerSocket建立Socket连接,Zygote(runSelectLoop)中fork了一个新的应用程序进程;
    • 在通过zygoteSendArgsAndGetResult方法将args传给Zygote,Zygote通过args启动对应的入口ActivityThread;
  • Zygote接受请求并创建应用程序进程

    • zygoteConnection 通过socket通信取出Argument参数; 见 ZygoteConnection.java 中processOneCommand方法启动子进程;

	-----------------------ActivityThread.java-----------------------
	public static void main(String[] args) {
	        ...
	        Looper.prepareMainLooper();
	        ActivityThread thread = new ActivityThread();
	        thread.attach(false);
	        ...
	        Looper.loop();
	        throw new RuntimeException("Main thread loop unexpectedly exited");
	    }

private void attach(boolean system) {
        ...
        //activityThread main 函数进入;
        if (!system) {
           ...
            final IActivityManager mgr = ActivityManager.getService();
            try {
            	//AMS 的 attachApplication方法;
                mgr.attachApplication(mAppThread);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
            ...
        } else {
           ...
            try {
            	//创建上下文;
                mInstrumentation = new Instrumentation();
                ContextImpl context = ContextImpl.createAppContext(
                        this, getSystemContext().mPackageInfo);
                mInitialApplication = context.mPackageInfo.makeApplication(true, null);
                mInitialApplication.onCreate();
            } catch (Exception e) {
                throw new RuntimeException(
                        "Unable to instantiate Application():" + e.toString(), e);
            }
        }
        ...
    }

	-----------------------ActivityManagerService.java-----------------------
	private final void startProcessLocked(ProcessRecord app, String hostingType,
            String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
        ...

        try {
            try {
                final int userId = UserHandle.getUserId(app.uid);
                AppGlobals.getPackageManager().checkPackageStartable(app.info.packageName, userId);
            } catch (RemoteException e) {
                throw e.rethrowAsRuntimeException();
            }
			//获取要创建的应用程序进程的用户Id;
            int uid = app.uid;
            int[] gids = null;
            int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
            if (!app.isolated) {
                ...

                /*
					gids的创建赋值;
                 * Add shared application and profile GIDs so applications can share some
                 * resources like shared libraries and access user-wide resources
                 */
                if (ArrayUtils.isEmpty(permGids)) {
                    gids = new int[3];
                } else {
                    gids = new int[permGids.length + 3];
                    System.arraycopy(permGids, 0, gids, 3, permGids.length);
                }
                gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid));
                gids[1] = UserHandle.getCacheAppGid(UserHandle.getAppId(uid));
                gids[2] = UserHandle.getUserGid(UserHandle.getUserId(uid));
            }
            ...
			//设置用于启动的类,即应用程序入口;
            if (entryPoint == null) entryPoint = "android.app.ActivityThread";
            Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Start proc: " +
                    app.processName);
            checkTime(startTime, "startProcess: asking zygote to start proc");
            ProcessStartResult startResult;
            if (hostingType.equals("webview_service")) {
                startResult = startWebView(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, null, entryPointArgs);
            } else {
                startResult = Process.start(entryPoint,
                        app.processName, uid, uid, gids, debugFlags, mountExternal,
                        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,
                        app.info.dataDir, invokeWith, entryPointArgs);
            }
            ...
        } catch (RuntimeException e) {
            ...
        }
    }

	-----------------Process.java--------------------

	/**
     * Start a new process.
     * 
     * <p>If processes are enabled, a new process is created and the
     * static main() function of a <var>processClass</var> is executed there.
     * The process will continue running after this function returns.
     * 
     * <p>If processes are not enabled, a new thread in the caller's
     * process is created and main() of <var>processClass</var> called there.
     * 
     * <p>The niceName parameter, if not an empty string, is a custom name to
     * give to the process instead of using processClass.  This allows you to
     * make easily identifyable processes even if you are using the same base
     * <var>processClass</var> to start them.
     * 
     * When invokeWith is not null, the process will be started as a fresh app
     * and not a zygote fork. Note that this is only allowed for uid 0 or when
     * debugFlags contains DEBUG_ENABLE_DEBUGGER.
     *
     * @param processClass The class to use as the process's main entry
     *                     point.
     * @param niceName A more readable name to use for the process.
     * @param uid The user-id under which the process will run.
     * @param gid The group-id under which the process will run.
     * @param gids Additional group-ids associated with the process.
     * @param debugFlags Additional flags.
     * @param targetSdkVersion The target SDK version for the app.
     * @param seInfo null-ok SELinux information for the new process.
     * @param abi non-null the ABI this app should be started with.
     * @param instructionSet null-ok the instruction set to use.
     * @param appDataDir null-ok the data directory of the app.
     * @param invokeWith null-ok the command to invoke with.
     * @param zygoteArgs Additional arguments to supply to the zygote process.
     * 
     * @return An object that describes the result of the attempt to start the process.
     * @throws RuntimeException on fatal start failure
     * 
     * {@hide}
     */
    public static final ProcessStartResult start(final String processClass,
                                  final String niceName,
                                  int uid, int gid, int[] gids,
                                  int debugFlags, int mountExternal,
                                  int targetSdkVersion,
                                  String seInfo,
                                  String abi,
                                  String instructionSet,
                                  String appDataDir,
                                  String invokeWith,
                                  String[] zygoteArgs) {
        return zygoteProcess.start(processClass, niceName, uid, gid, gids,
                    debugFlags, mountExternal, targetSdkVersion, seInfo,
                    abi, instructionSet, appDataDir, invokeWith, zygoteArgs);
    }

	
	-------------ZygoteProcess.java----------------

	/**
     * Tries to open socket to Zygote process if not already open. If
     * already open, does nothing.  May block and retry.  Requires that mLock be held.
     * zygote的启动脚本有4种;
     */
    @GuardedBy("mLock")
    private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
        Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held");

        if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
            try {
				//与Zygote进程建立Socket连接
                primaryZygoteState = ZygoteState.connect(mSocket);
            } catch (IOException ioe) {
                throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
            }
        }
		
		//连接Zygote主模式返回的ZygoteState是否与启动应用程序进程所需要的ABI匹配
        if (primaryZygoteState.matches(abi)) {
            return primaryZygoteState;
        }

        // The primary zygote didn't match. Try the secondary.
        if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) {
            try {
				//尝试zygote辅模式;
                secondaryZygoteState = ZygoteState.connect(mSecondarySocket);
            } catch (IOException ioe) {
                throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe);
            }
        }
		//连接zygote辅模式返回的ZygoteState是否与启动应用程序进程所需要的ABI匹配;
        if (secondaryZygoteState.matches(abi)) {
            return secondaryZygoteState;
        }

        throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi);
    }

	/**
     * Sends an argument list to the zygote process, which starts a new child
     * and returns the child's pid. Please note: the present implementation
     * replaces newlines in the argument list with spaces.
     *
     * @throws ZygoteStartFailedEx if process start failed for any reason
     */
    @GuardedBy("mLock")
    private static Process.ProcessStartResult zygoteSendArgsAndGetResult(
            ZygoteState zygoteState, ArrayList<String> args)
            throws ZygoteStartFailedEx {
        try {
            // Throw early if any of the arguments are malformed. This means we can
            // avoid writing a partial response to the zygote.
            int sz = args.size();
            for (int i = 0; i < sz; i++) {
                if (args.get(i).indexOf('\n') >= 0) {
                    throw new ZygoteStartFailedEx("embedded newlines not allowed");
                }
            }

            /**
             * See com.android.internal.os.SystemZygoteInit.readArgumentList()
             * Presently the wire format to the zygote process is:
             * a) a count of arguments (argc, in essence)
             * b) a number of newline-separated argument strings equal to count
             *
             * After the zygote process reads these it will write the pid of
             * the child or -1 on failure, followed by boolean to
             * indicate whether a wrapper process was used.
             */
            final BufferedWriter writer = zygoteState.writer;
            final DataInputStream inputStream = zygoteState.inputStream;

            writer.write(Integer.toString(args.size()));
            writer.newLine();

            for (int i = 0; i < sz; i++) {
                String arg = args.get(i);
                writer.write(arg);
                writer.newLine();
            }

            writer.flush();

            // Should there be a timeout on this?
            Process.ProcessStartResult result = new Process.ProcessStartResult();

            // Always read the entire result from the input stream to avoid leaving
            // bytes in the stream for future process starts to accidentally stumble
            // upon.
            result.pid = inputStream.readInt();
            result.usingWrapper = inputStream.readBoolean();

            if (result.pid < 0) {
                throw new ZygoteStartFailedEx("fork() failed");
            }
            return result;
        } catch (IOException ex) {
            zygoteState.close();
            throw new ZygoteStartFailedEx(ex);
        }
    }

Binder线程池启动过程

  • 调用ZygoteInit.java的zygoteInit方法启动入口,通过ZygoteInit.nativeZygoteInit()方法启动Binder线程池;
  • 此jni方法对应 AndroidRuntime.cppregister_com_android_internal_os_ZygoteInit_nativeZygoteInit native函数; 调用AndroidRuntime的 onZygoteInit方法, 而AppRuntime是androidRuntime的子类,从而调用了app_main.cpp中AppRuntime的 onZygoteInit方法;
  • onZygoteInit方法将当前线程注册到Binder驱动程序中,这样创建的线程就加入到binder线程池中;这样新创建的应用程序进程就支持Binder进程间通信,我们只需创建当前进程的Binder对象,注册到ServiceManager中就实现了Binder进程间通信;

我是这样理解的:

binder线程池


	----------------------app_main.cpp------------------------------

	class AppRuntime : public AndroidRuntime
	{
	public:						
    AppRuntime(char* argBlockStart, const size_t argBlockLength)
        : AndroidRuntime(argBlockStart, argBlockLength)
        , mClass(NULL)
    {
    }
    ...

    virtual void onZygoteInit()
    {
        sp<ProcessState> proc = ProcessState::self();
        ALOGV("App process: starting thread pool.\n");
        proc->startThreadPool();
    }

    ...
	};


	------------------------ProcessState.cpp---------------------------

	void ProcessState::startThreadPool()
	{
	    AutoMutex _l(mLock);
	    if (!mThreadPoolStarted) {
	        mThreadPoolStarted = true;
	        spawnPooledThread(true);
	    }
	}

	void ProcessState::spawnPooledThread(bool isMain)
	{
	    if (mThreadPoolStarted) {
	        String8 name = makeBinderThreadName();
	        ALOGV("Spawning new pooled thread, name=%s\n", name.string());
	        sp<Thread> t = new PoolThread(isMain);
	        t->run(name.string());
	    }
	}	

	class PoolThread : public Thread
	{
	public:
	    explicit PoolThread(bool isMain)
	        : mIsMain(isMain)
	    {
	    }
	    
	protected:
	    virtual bool threadLoop()
	    {
			//将当前线程注册到binder驱动程序中,创建的线程加入到Binder线程池中;新创建的应用程序进程就支持Binder进程间通信;
			//我们只需创建当前进程的Binder对象,并注册到ServiceManager中就可以实现Binder进程间通信;
	        IPCThreadState::self()->joinThreadPool(mIsMain);
	        return false;
	    }
	    
	    const bool mIsMain;
	};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值