JobScheduler调度器过程(JobSchedulerService的启动过程)
JobSchedulerService启动过程,最主要工作是从jobs.xml文件收集所有的jobs,放入到JobStore的成员变量mJobSet,转成jobinfo。
JobScheduler服务启动
2.1 startOtherServices
[-> SystemServer.java]
private void startOtherServices() {
...
mSystemServiceManager.startService(JobSchedulerService.class);
...
}
该方法先初始化JSS,然后再调用其onStart()方法。
2.2 JobSchedulerService
[-> JobSchedulerService.java]
JobSchedulerService {
List<StateController> mControllers;
final JobHandler mHandler;
final JobSchedulerStub mJobSchedulerStub;
final JobStore mJobs;
...
public JobSchedulerService(Context context) {
super(context);
mControllers = new ArrayList<StateController>();
mControllers.add(ConnectivityController.get(this));
mControllers.add(TimeController.get(this));
mControllers.add(IdleController.get(this));
mControllers.add(BatteryController.get(this));
mControllers.add(AppIdleController.get(this));
//创建主线程的looper[见小节2.3]
mHandler = new JobHandler(context.getMainLooper());
//创建binder服务端[见小节2.4]
mJobSchedulerStub = new JobSchedulerStub();
//[见小节2.5]
mJobs = JobStore.initAndGet(this);
}
public void onStart() {
publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub);
}
}
创建了5个不同的StateController,分别添加到mControllers。
类型 | 说明 |
---|---|
ConnectivityController | 注册监听网络连接状态的广播 |
TimeController | 注册监听job时间到期的广播 |
IdleController | 注册监听屏幕亮/灭,dream进入/退出,状态改变的广播 |
BatteryController | 注册监听电池是否充电,电量状态的广播 |
AppIdleController | 监听app是否空闲 |
接下来,以ConnectivityController为例,说一说相应Controller的创建过程, 其他Controller也基本类似.
2.2.1 ConnectivityController
[-> ConnectivityController.java]
public class ConnectivityController extends StateController implements ConnectivityManager.OnNetworkActiveListener {
public static ConnectivityController get(JobSchedulerService jms) {
synchronized (sCreationLock) {
if (mSingleton == null) {
//单例模式
mSingleton = new ConnectivityController(jms, jms.getContext());
}
return mSingleton;
}
}
private ConnectivityController(StateChangedListener stateChangedListener, Context context) {
super(stateChangedListener, context);
//注册监听网络连接状态的广播,且采用BackgroundThread线程
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mContext.registerReceiverAsUser(
mConnectivityChangedReceiver, UserHandle.ALL, intentFilter, null,
BackgroundThread.getHandler());
ConnectivityService cs =
(ConnectivityService)ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
if (cs != null) {
if (cs.getActiveNetworkInfo() != null) {
mNetworkConnected = cs.getActiveNetworkInfo().isConnected();
}
mNetworkUnmetered = mNetworkConnected && !cs.isActiveNetworkMetered();
}
}
}
当监听到CONNECTIVITY_ACTION广播,onReceive方法的执行位于“android.bg”线程。
2.3 JSS.JobHandler
[-> JobSchedulerService.java ::JobHandler]
public class JobSchedulerService extends com.android.server.SystemService implements StateChangedListener, JobCompletedListener {
private class JobHandler extends Handler {
public JobHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message message) {
synchronized (mJobs) {
//当系统启动到phase 600,则mReadyToRock=true.
if (!mReadyToRock) {
return;
}
}
switch (message.what) {
case MSG_JOB_EXPIRED: ...
case MSG_CHECK_JOB: ...
}
maybeRunPendingJobsH();
removeMessages(MSG_CHECK_JOB);
}
JobHandler采用的是system_server进程的主线程Looper,也就是该过程运行在主线程。
2.4 JobSchedulerStub
[-> JobSchedulerService.java ::JobSchedulerStub]
final class JobSchedulerStub extends IJobScheduler.Stub {
...
}
JobSchedulerStub作为实现接口IJobScheduler的binder服务端。
2.5 JS.initAndGet
[-> JobStore.java]
static JobStore initAndGet(JobSchedulerService jobManagerService) {
synchronized (sSingletonLock) {
if (sSingleton == null) {
//[见小节2.6]
sSingleton = new JobStore(jobManagerService.getContext(),
Environment.getDataDirectory());
}
return sSingleton;
}
}
2.6 创建JobStore
[-> JobStore.java]
public class JobStore {
final ArraySet<JobStatus> mJobSet;
private final Handler mIoHandler = IoThread.getHandler();
...
private JobStore(Context context, File dataDir) {
mContext = context;
mDirtyOperations = 0;
File systemDir = new File(dataDir, "system");
File jobDir = new File(systemDir, "job");
jobDir.mkdirs();
// 创建/data/system/job/jobs.xml
mJobsFile = new AtomicFile(new File(jobDir, "jobs.xml"));
mJobSet = new ArraySet<JobStatus>();
//[见小节2.7.1]
readJobMapFromDisk(mJobSet);
}
}
该方法会创建job目录以及jobs.xml文件, 以及从文件中读取所有的JobStatus。
2.7 xml解析
2.7.1 ReadJobMapFromDiskRunnable
[-> JobStore.java]
private class ReadJobMapFromDiskRunnable implements Runnable {
private final ArraySet<JobStatus> jobSet;
ReadJobMapFromDiskRunnable(ArraySet<JobStatus> jobSet) {
this.jobSet = jobSet;
}
public void run() {
List<JobStatus> jobs;
FileInputStream fis = mJobsFile.openRead();
synchronized (JobStore.this) {
jobs = readJobMapImpl(fis); //[见小节2.7.2]
if (jobs != null) {
for (int i=0; i<jobs.size(); i++) {
this.jobSet.add(jobs.get(i));
}
}
}
fis.close();
}
}
此处mJobsFile便是/data/system/job/jobs.xml。
2.7.2 readJobMapImpl
[-> JobStore.java]
private List<JobStatus> readJobMapImpl(FileInputStream fis)
throws XmlPullParserException, IOException {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(fis, StandardCharsets.UTF_8.name());
...
String tagName = parser.getName();
if ("job-info".equals(tagName)) {
final List<JobStatus> jobs = new ArrayList<JobStatus>();
...
eventType = parser.next();
do {
//读取每一个 <job/>
if (eventType == XmlPullParser.START_TAG) {
tagName = parser.getName();
if ("job".equals(tagName)) {
//[见小节2.7.3]
JobStatus persistedJob = restoreJobFromXml(parser);
if (persistedJob != null) {
jobs.add(persistedJob);
}
}
}
eventType = parser.next();
} while (eventType != XmlPullParser.END_DOCUMENT);
return jobs;
}
return null;
}
从文件jobs.xml中读取并创建JobStatus,然后添加到mJobSet.
2.7.3 restoreJobFromXml
[-> JobStore.java]
private JobStatus restoreJobFromXml(XmlPullParser parser) throws XmlPullParserException, IOException {
JobInfo.Builder jobBuilder;
int uid;
//创建用于获取jobInfo的Builder[见小节2.7.4]
jobBuilder = buildBuilderFromXml(parser);
jobBuilder.setPersisted(true);
uid = Integer.valueOf(parser.getAttributeValue(null, "uid"));
...
buildConstraintsFromXml(jobBuilder, parser); //读取常量
//读取job执行的两个时间点:delay和deadline
Pair<Long, Long> elapsedRuntimes = buildExecutionTimesFromXml(parser);
...
//[见小节2.8]
return new JobStatus(jobBuilder.build(), uid,
elapsedRuntimes.first, elapsedRuntimes.second);
}
2.7.4 buildBuilderFromXml
[-> JobStore.java]
private JobInfo.Builder buildBuilderFromXml(XmlPullParser parser) throws NumberFormatException {
int jobId = Integer.valueOf(parser.getAttributeValue(null, "jobid"));
String packageName = parser.getAttributeValue(null, "package");
String className = parser.getAttributeValue(null, "class");
ComponentName cname = new ComponentName(packageName, className);
//[见小节2.7.5]
return new JobInfo.Builder(jobId, cname);
}
创建的JobInfo对象,记录着任务的jobid, package, class。
2.7.5 创建JobInfo
[-> JobInfo.java]
public class JobInfo implements Parcelable {
public static final class Builder {
public Builder(int jobId, ComponentName jobService) {
mJobService = jobService;
mJobId = jobId;
}
public JobInfo build() {
mExtras = new PersistableBundle(mExtras);
return new JobInfo(this); //创建JobInfo
}
}
}
2.8 创建JobStatus
[-> JobStatus.java]
public JobStatus(JobInfo job, int uId, long earliestRunTimeElapsedMillis, long latestRunTimeElapsedMillis) {
this(job, uId, 0);
this.earliestRunTimeElapsedMillis = earliestRunTimeElapsedMillis;
this.latestRunTimeElapsedMillis = latestRunTimeElapsedMillis;
}
private JobStatus(JobInfo job, int uId, int numFailures) {
this.job = job;
this.uId = uId;
this.name = job.getService().flattenToShortString();
this.tag = "*job*/" + this.name;
this.numFailures = numFailures;
}
JobStatus对象记录着任务的jobId, ComponentName, uid以及标签和失败次数信息。
2.9 JSS.onBootPhase
public void onBootPhase(int phase) {
if (PHASE_SYSTEM_SERVICES_READY == phase) {
//阶段500,则开始注册package和use移除的广播监听
final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package");
getContext().registerReceiverAsUser(
mBroadcastReceiver, UserHandle.ALL, filter, null, null);
final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
userFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
getContext().registerReceiverAsUser(
mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
} else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
synchronized (mJobs) {
mReadyToRock = true;
mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
BatteryStats.SERVICE_NAME));
for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
//创建JobServiceContext对象
mActiveServices.add(
new JobServiceContext(this, mBatteryStats,
getContext().getMainLooper()));
}
ArraySet<JobStatus> jobs = mJobs.getJobs();
for (int i=0; i<jobs.size(); i++) {
JobStatus job = jobs.valueAt(i);
for (int controller=0; controller<mControllers.size(); controller++) {
mControllers.get(controller).deviceIdleModeChanged(mDeviceIdleMode);
mControllers.get(controller).maybeStartTrackingJob(job);
}
}
//[见小节3.8]
mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
}
}
}
对于低内存的设备,则只创建一个创建JobServiceContext对象;否则创建3个该对象。
2.9.1 创建JobServiceContext
[-> JobServiceContext.java]
JobServiceContext(JobSchedulerService service, IBatteryStats batteryStats, Looper looper) {
this(service.getContext(), batteryStats, service, looper);
}
JobServiceContext(Context context, IBatteryStats batteryStats,
JobCompletedListener completedListener, Looper looper) {
mContext = context;
mBatteryStats = batteryStats;
mCallbackHandler = new JobServiceHandler(looper);
mCompletedListener = completedListener;
mAvailable = true;
}
此处的JobServiceHandler采用的是system_server进程的主线程。
2.10 小结
- JSS.JobHandler运行在system_server进程的主线程;
- JobServiceContext.JobServiceHandler运行在system_server进程的主线程;
- JobSchedulerStub作为实现接口IJobScheduler的binder服务端;
- JobStore:其成员变量mIoHandler运行在”android.io”线程;
- JobStatus:从/data/system/job/jobs.xml文件中读取每个JobInfo,再解析成JobStatus对象,添加到mJobSet。
JobScheduler调度器过程(JobSchedulerService的启动过程)的更多相关文章
- 从Linux启动过程到android启动过程
Linux启动过程: 1.首先开机给系统供电,此时硬件电路会产生一个确定的复位时序,保证cpu是最后一个被复位的器件.为什么cpu要最后被复位呢?因为 如果cpu第一个被复位,则当cpu复位后开始运行 ...
- Linux 启动过程详解
目录 1. Linux启动过程 2. 启动过程概述 3. 引导加载阶段 4. 内核阶段 4.1 内核加载阶段 4.2 内核启动阶段 5. 早期的用户空间 6. 初始化过程 6.1 SysV init ...
- 作业三:LINUX内核的启动过程
作业三:LINUX内核的启动过程 一.使用GDB跟踪内核从start_kernel到init进程启动(附实验截图) (一)使用自己的Linux系统环境搭建MenuOS的过程 下载内核源代码编译内核 c ...
- 分布式事务_02_2PC框架raincat源码解析-启动过程
一.前言 上一节已经将raincat demo工程运行起来了,这一节来分析下raincat启动过程的源码 主要包括: 事务协调者启动过程 事务参与者启动过程 二.协调者启动过程 主要就是在启动类中通过 ...
- Linux 开机引导和启动过程详解
你是否曾经对操作系统为何能够执行应用程序而感到疑惑?那么本文将为你揭开操作系统引导与启动的面纱. 理解操作系统开机引导和启动过程对于配置操作系统和解决相关启动问题是至关重要的.该文章陈述了 GRUB2 ...
- uboot启动过程理解
对于2440而言,启动的方式不多.一般就是外界一个NAND FLASH ,2440内部有个NAND FLASH Controller,会自动把NAND FLASH的前4K拷贝到2440的片内SRAM. ...
- Oracle的启动过程
在Windows操作系统平台下,可以使用SQL*Plus.OEM和系统服务管理等方式进行数据库的启动与关闭操作.数据库启动分为3个步骤:创建并启动数据库实例.装载数据库和打开数据库.数据库的关闭过程与 ...
- PMP--3. 项目启动过程组
####################################################### 从第三章开始,我正式进入项目过程,启动.规划.执行.监控.收尾五大过程组的具体在之后依次 ...
- Django 源码小剖: URL 调度器(URL dispatcher)
在刚开始接触 django 的时候, 我们尝试着从各种入门文档中创建一个自己的 django 项目, 需要在 mysite.urls.py 中配置 URL. 这是 django url 匹配处理机制的 ...
随机推荐
- (14)Why some people find exercise harder than others
https://www.ted.com/talks/emily_balcetis_why_some_people_find_exercise_harder_than_others/transcript ...
- Educational Codeforces Round 60 C 思维 + 二分
https://codeforces.com/contest/1117/problem/C 题意 在一个二维坐标轴上给你一个起点一个终点(x,y<=1e9),然后给你一串字符串代表每一秒的风向, ...
- bootstrap-glyph-customization
http://www.runoob.com/try/demo_source/bootstrap-glyph-customization.htm http://fontawesome.io/icons/
- ELK简单部署
系统环境: IP:192.168.0.156 ruby环境准备 yum -y install ruby-devel ruby-libs ruby-rdoc ruby-ri ruby-static ru ...
- C语言的问题,头文件:keil也许有漏洞
2018-06-15 16:52:03 ------------------------------------------------------------------------------ ...
- 第08章:MongoDB-CRUD操作--文档--删除
①语法 remove() [2.6以后方法过时] deleteOne() [2.6以后官方推荐] deleteMany() [2.6以后官方推荐] db.collection.remove( < ...
- location位置操作
使用location对象可以通过很多方式来改变浏览器的位置. location.assign('http://www.klkx.com') 传入一个URL地址 这样可以立即打开一个新的URL并在浏览器 ...
- mysql碰到unknown variable 'xxxx' 的解决方法
在使用mysqlbinlog查看日志的时候碰到了一个问题, 错误提示如下: /usr/local/mysql/bin/mysqlbinlog: unknown variable 'default-ch ...
- python psutil简单示例
python psutil简单示例 利用psutil编写简单的检测小脚本 0.安装psutil模块 ...
- java基础-day17
第06天 集合 今日内容介绍 u 集合&迭代器 u 增强for & 泛型 u 常见数据结构 u List子体系 第1章 集合&迭代器 1.1 集合体系结构 1.1 ...