nuwa创建新进程源码分析

本文深入探讨了Nuwa在B2G操作系统中创建新进程的源码实现,涉及NuwaParent与NuwaChild的交互、fork过程、线程连接以及进程间通信(Ipc)机制。详细阐述了ContentProcess、ContentParent、ContentChild类的角色,以及Nuwa如何通过socketpair、messageloop和IPDL机制与B2G主线程和ioloop线程建立通道。同时解释了Nuwa与plugin-container的关系,Nuwa旨在提升系统速度并逐步替代plugin-container的角色。

nuwa.cpp的作用,fork 对线程进行相关冻结设置复制等。线程级别的设置等。。。。

https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/Security/B2G_IPC_internals

https://blog.youkuaiyun.com/hunter___/article/details/82912266

 

具体介绍如下内容:

通道的建立,通道的连接(b2g (parent )如何连接到通道,nuwa如何连接到通道),fork,进程的孵化,

b2g中主线程和ioloop线程之间进行了通道连接?

Nuwa中也一样,它的主线程与ioloop线程也进行连接?

 gecko/mozglue/build/Nuwa.h

。。。

extern "C" {

/**
 * The following 3 methods are used to synchronously spawn the Nuwa process.
 * The typical usage is:
 *
 *   The IPC thread                                The main thread
 *   1. Receives the request.
 *   2. NuwaSpawnPrepare().
 *   3. Request main thread to
 *      spawn. --------------------------------------+
 *   4. NuwaSpawnWait().                             V
 *                                                 5. NuwaSpawn() to clone all
 *                                                    the threads in the Nuwa
 *                                                    process (including main
 *                                                    & IPC threads).
 *                                                 6. NuwaSpawn() finishes and
 *            +-------------------------------------- signals NuwaSpawnWait().
 *            V
 *   7. NuwaSpawnWait() returns.
 */

/**
 * Prepare for spawning a new process. The calling thread (typically the IPC
 * thread) will block further requests to spawn a new process.
 */
MFBT_API void NuwaSpawnPrepare();

/**
 * Called on the thread that receives the request. Used to wait until
 * NuwaSpawn() finishes and maintain the stack of the calling thread.
 */
MFBT_API void NuwaSpawnWait();

/**
 * Spawn a new content process, called in the Nuwa process. This has to run on
 * the main thread.
 *
 * @return The process ID of the new process.
 */
MFBT_API pid_t NuwaSpawn();

/**
 * The following methods are for marking threads created in the Nuwa process so
 * they can be frozen and then recreated in the spawned process. All threads
 * except the main thread must be marked by one of the following 4 methods;
 * otherwise, we have a thread that is created but unknown to us. The Nuwa
 * process will never become ready and this needs to be fixed.
 */

/**
 * Mark the current thread as supporting Nuwa. The thread will implicitly freeze
 * by calling a blocking method such as pthread_mutex_lock() or epoll_wait().
 *
 * @param recreate The custom function that will be called in the spawned
 *                 process, after the thread is recreated. Can be nullptr if no
 *                 custom function to be called after the thread is recreated.
 *                 Note that this function is called duing thread recreation
 *                 while other threads are frozen. It must not perform any
 *                 action (e.g. acquiring a mutex) that might depend on another
 *                 thread that is still blocked.
 * @param arg The argument passed to the custom function. Can be nullptr.
 */
MFBT_API void NuwaMarkCurrentThread(void (*recreate)(void *), void *arg);

/**
 * Mark the current thread as not supporting Nuwa. The calling thread will not
 * be automatically cloned from the Nuwa process to spawned process. If this
 * thread needs to be created in the spawned process, use NuwaAddConstructor()
 * or NuwaAddFinalConstructor() to do it.
 */
MFBT_API void NuwaSkipCurrentThread();

/**
 * Force the current thread to freeze explicitly at the current point.
 */
MFBT_API void NuwaFreezeCurrentThread();

/**
 * Helper functions for the NuwaCheckpointCurrentThread() macro.
 */
MFBT_API jmp_buf* NuwaCheckpointCurrentThread1();

MFBT_API bool NuwaCheckpointCurrentThread2(int setjmpCond);

/**
 * Set the point to recover after thread recreation. The calling thread is not
 * blocked. This is used for the thread that cannot freeze (i.e. the IPC
 * thread).
 */
#define NuwaCheckpointCurrentThread() \
  NuwaCheckpointCurrentThread2(setjmp(*NuwaCheckpointCurrentThread1()))

/**
 * The following methods are called in the initialization stage of the Nuwa
 * process.
 */

/**
 * Prepare for making the Nuwa process.
 */
MFBT_API void PrepareNuwaProcess();

/**
 * Make the current process a Nuwa process. Start to freeze the threads.
 */
MFBT_API void MakeNuwaProcess();

/**
 * Register a method to be invoked after a new process is spawned. The method
 * will be invoked on the main thread *before* recreating the other threads.
 * The registered method must not perform any action (e.g. acquiring a mutex)
 * that might depend on another thread that has not yet been recreated.
 *
 * @param construct The method to be invoked.
 * @param arg The argument passed to the method.
 */
MFBT_API void NuwaAddConstructor(void (*construct)(void *), void *arg);


/**
 * Register a method to be invoked after a new process is spawned and threads
 * are recreated. The method will be invoked on the main thread *after*
 * the other threads are recreated and fully functional.
 *
 * @param construct The method to be invoked.
 * @param arg The argument passed to the method.
 */
MFBT_API void NuwaAddFinalConstructor(void (*construct)(void *), void *arg);


/**
 * Register a method to be invoked after the current thread is recreated in the
 * spawned process. Note that this function is called while other threads are
 * frozen. It must not perform any action (e.g. acquiring a mutex) that might
 * depend on another thread that is still blocked.
 *
 * @param construct The method to be invoked.
 * @param arg The argument passed to the method.
 */
MFBT_API void NuwaAddThreadConstructor(void (*construct)(void *), void *arg);

/**
 * The methods to query the Nuwa-related states.
 */

/**
 * @return If the current process is the Nuwa process.
 */
MFBT_API bool IsNuwaProcess();

/**
 * @return If the Nuwa process is ready for spawning new processes.
 */
MFBT_API bool IsNuwaReady();

#if defined(DEBUG) || defined(ENABLE_TESTS)
/**
 * Asserts that aThread is not frozen.
 */
MFBT_API void NuwaAssertNotFrozen(unsigned int aThread,
                                  const char* aThreadName);
#else
#define NuwaAssertNotFrozen(aThread, aThreadName) do {} while(0);
#endif
};

#endif /* __NUWA_H_ */

 

 

 

gecko/dom/ipc/

gecko/dom/ipc/ .cpp .h .ipdl 总体来讲就是dom 中的进程间通信(Ipc)模块

NuwaParent.cpp

 

dom/ipc/PreallocatedProcessManager.cpp:68:  void NuwaFork()决定何时需要创建新进程,在NuwaChild中被调,往上继续可以追溯到APP请求?

 

PContentParent.h 在b2g main thread 中,SendNuwaFork() 发送fork新进程的消息,

 

PContentChild.h 在nuwa main thread中,会RecvNuwaFork(),并开始去做真正的fork工作。fork完之后会发消息给b2g让它sendAddNewProcess  。

ioloop 线程专门用于接收进程间的消息。ioloop线程与main thread 建立通道。

socketpair+messageloop+ipdl机制

 

Nuwa从b2gloader这一线

gecko/b2g/app/B2GLoader.cpp:307, Fuc:RunProcesses-->

ipc/glue/ProcessUtils_linux.cpp:692, Fuc:XRE_ProcLoaderServiceRun-->

gecko/toolkit/xre/nsAppRunner.cpp:4595, Fuc:XRE_InitCommandLine-->

gecko/ipc/glue/ProcessUtils_linux.cpp:626, Fuc:ProcLoaderServiceRun  call -- process = new ContentProcess(aPeerPid:180)--->

gecko/ipc/glue/ProcessChild.cpp:56, Fuc:ProcessChild--->

gecko/dom/ipc/ContentChild.cpp:637, Fuc:ContentChild-->

 

 

 

ContentChild和ContentParent都及大,总体作用是什么,

class ContentProcess : public mozilla::ipc::ProcessChild

 

 

 

如下关系,

contentprocess

contentparent

contentchild

 

nsI这块到底是谁在用?

nsIContentChild

nsIcontentparent

 

nuwaparent

nuwachild

 

牢记nuwa在ProcLoaderServiceRun上面运行,

../ipc/glue/ProcessUtils_linux.cpp:22:#include "mozilla/ipc/PProcLoaderChild.h"

../ipc/glue/ProcessUtils_linux.cpp:21:#include "mozilla/ipc/PProcLoaderParent.h" Load的细节,不管了。

ProcessUtils_linux.cpp用于处理接收进程间的load 请求等

 

nuwa与plugin-container的关系,开始时plugin是必须的,后加入Nuwa以加快速度。所以,目前正常状态,plugin-container只在第一次fork出进程时被nuwa使用,nuwa成熟之后,所有工作就不劳烦plungin,而是全权由nuwa来,nuwa的目的就在于提升速度横插进来的模块。setprocessasnuwa也就可以理解了。

明白了,第一步是b2g请求plugin-container Load nuwa ,此时此进程还不是nuwa进程,只是成长中的nuwa process,本质是plugin-container过渡态进程,直到plugin-container Load 完nuwa,并将此Plungin 进程设置为Nuwa进程。ok,现在nuwa出来了。后面所有进程的创建都丢给nuwa吧。于是后面系统起来之后,分别启动了3个进程,先后应该如下:homescreen,keyboard,preallocated

没错。

 

 

 

 

 

 

这个是完整源码 python实现 Flask,Vue 【python毕业设计】基于Python的Flask+Vue物业管理系统 源码+论文+sql脚本 完整版 数据库是mysql 本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值