Download Android OS - ShareCourse

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Android OS
100060010 許軒中
100060017 李紀萱
100060025 鄧國盛
Outline

Overview of Android OS

Multiprogramming and Multitasking

Scheduling employed

Threading supported

Programming languages and IDEs supported
2
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android Architecture
3
java
C
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android Architecture(Cont)
4
The applications are at the topmost layer of the Android stack.
An average user of the Android device would mostly interact with
this layer (for basic functions, such as making phone calls, accessing
the Web browser etc.)
Application framework includes the programs that manage the
phone's basic functions. Think of the application framework as a set
of basic tools with which a developer can build much more
complex tool.
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android Architecture(Cont)
5
app
app
app
Dalvik Dalvik Dalvik
VM
VM
VM
Core libraries (Java)
These are different from Java SE and Java ME libraries. However these libraries
provides most of the functionalities defined in the Java SE libraries.
Dalvik VM

It is a type of JVM used in android devices to run apps and is optimized for
low processing power and low memory environments. Dalvik VM is Register
based and each application runs on its own Dalvik VM, that is, It has been
designed to allow multiple VM instances to run at once

Operates on DEX files.
.java
compile
.class
convert
.dex
package
.apk
100060010許軒中 100060017李紀萱 100060025鄧國盛
6
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android Architecture(Cont)
7
Libraries (C/C++)
Android’s native libraries carry a set of instructions to guide the device in handling
different types of data. These libraries are written in c or c++ language and are
specific for a particular hardware.
Kernel
The basic layer is the Linux kernel. The whole Android OS is built on top of the Linux 2.6
Kernel with some further architectural changes made by Google. It is this Linux that
interacts with the hardware and contains all the essential hardware drivers. It also acts
as an abstraction layer between the hardware and other software layers.
100060010許軒中 100060017李紀萱 100060025鄧國盛
Anatomy of an Android
Application
8
Application components are the essential building blocks of an Android application.
Each component is a different point through which the system can enter your
application. Not all components are actual entry points for the user and some depend
on each other, but each one exists as its own entity and plays a specific role—each
one is a unique building block that helps define your application's overall behavior.
Activities
Services
Content
Providers
Broadcast
receivers
100060010許軒中 100060017李紀萱 100060025鄧國盛
Activities
9
An activity represents a single screen with a user interface.
For example, an email application might have one activity that shows a list of new
emails, another activity to compose an email, and another activity for reading
emails. Although the activities work together to form a cohesive user experience in
the email application, each one is independent of the others. As such, a different
application can start any one of these activities (if the email application allows it).
For example, a camera application can start the activity in the email application
that composes new mail, in order for the user to share a picture.
Activity 1
Activity 2
Activity 3
Show list of new
emails
Compose
emails
Reading emails
Email Application
100060010許軒中 100060017李紀萱 100060025鄧國盛
Services
A service is a component that runs in the background to
perform long-running operations or to perform work for
remote processes. A service does not provide a user
interface.
10
Example: Playing music in the background
Content
Providers
Broadcast
receivers
A content provider manages a shared set of application
data. You can store the data in the file system, an SQLite
database, on the web, or any other persistent storage
location your application can access. Through the content
provider, other applications can query or even modify the
data (if the content provider allows it). For example, the
Android system provides a content provider that manages
the user's contact information.
A broadcast receiver is a component that responds to
system-wide broadcast announcements. Many broadcasts
originate from the system—for example, a broadcast
announcing that the screen has turned off, the battery is
low, or a picture was captured. Applications can also
initiate broadcasts—for example, to let other applications
know that some data has been downloaded to the device
and is available for them to use.
100060010許軒中 100060017李紀萱 100060025鄧國盛
A unique aspect
11
Any application can start another application’s
component.
Camera
Application
Component 1
Android
OS
Intent
Photo Editing
Application
Component 1
Component 2
Component 2
Component 3
Component 3
100060010許軒中 100060017李紀萱 100060025鄧國盛
Multitasking
12
The Android system tries to maintain an application
process for as long as possible…….
but eventually needs to remove old processes to reclaim memory for new or
more important processes.
To determine which processes to keep and which to kill, the system places
each process into an "importance hierarchy" based on the components
running in the process and the state of those components.
Importance
Foreground Process
Visible Process
Service Process
Background Process
Empty Process
100060010許軒中 100060017李紀萱 100060025鄧國盛
Foreground Process
It hosts an Activity that the user is interacting with.
13
It hosts a Service that's bound to the activity that the
user is interacting with.
It hosts a BroadcastReceiver that's executing its
onReceive() method.
Visible
Visible
Process
Process
A process that doesn't have any foreground
components, but still can affect what the user sees
on screen.
It hosts a Service that's bound to a visible (or
foreground) activity.
100060010許軒中 100060017李紀萱 100060025鄧國盛
Service
Service
Process
Process
They are generally doing things that the user cares
about (such as playing music in the background or
downloading data on the network),
14
Background
Process
Background
Process
A process holding an activity that's not currently
visible to the user
Empty Process
A process that doesn't hold any active application
components. The only reason to keep this kind of
process alive is for caching purposes, to improve
startup time the next time a component needs to run
in it.
100060010許軒中 100060017李紀萱 100060025鄧國盛
When deciding which processes to kill, the Android
system weighs their relative importance to the user.
For example, it more readily shuts down a process
hosting activities that are no longer visible on screen,
compared to a process hosting visible activities. The
decision whether to terminate a process, therefore,
depends on the state of the components running in
that process.
15
If a user later returns to an application that's been killed, Android
preserves the "all applications are running all of the time"
experience by keeping track of the parts of the application the
user is aware of (the Activities), and re-starting them with
information about the last state they were seen in. Android's
process management can be seen as a form of swap space:
application processes represent a certain amount of in-use
memory; when memory is low, some processes can be killed
(swapped out); when those processes are needed again, they
can be re-started from their last saved state (swapped in).
100060010許軒中 100060017李紀萱 100060025鄧國盛
Scheduling
16
Since Android is based on the Linux kernel, it shares
many aspects with Linux, including how processes
and threads are scheduled.

The Linux kernel uses the CFS, or
the Completely Fair Scheduler
which picks out the next process
dynamically based on vruntime
values and “nice values”
100060010許軒中 100060017李紀萱 100060025鄧國盛
CFS Scheduler
17
Scheduler Latency - the period in which all run queue tasks are scheduled at least once.
Now consider a case where two threads share the CPU, one with niceness 0 and the other with
niceness 5. CFS assigns these niceness levels the weights of 1024 and 335 respectively.
The time that the threads get is therefore proportional to 1024/(1024+335) and 335/(1024+335).
The thread with niceness 0 will receive approximately 75ms out of each 100ms and the thread with
niceness 5 will receive approximately 25ms out of each 100ms
Thread 1
Nice Value 0
Weight 1024
Thread 2
Nice Value 5
Weight 335
1024/(1024+335)
335/(1024+335
100060010許軒中 100060017李紀萱 100060025鄧國盛
Vruntime
18
To pick out the next process to run, Linux picks the process with the
Thread 1
Nice Value 0
Weight 1024
Thread 2
Nice Value 5
Weight 335
lowest vruntime (virtual run time)
Run for 1 ns
Run for 1 ns
Vruntime += 1
Vruntime += 3
100060010許軒中 100060017李紀萱 100060025鄧國盛
19
Time Slice Given to each process =
sched_latency * (process_weight / all weights)
Vruntime added to each process =
Actual Runtime / (process_weight)
Vrun time = sched_latency / all weights
The CPU thinks that each process was given the same run time!
However as we can see, processes with higher priorities (lower nice values)
actually get larger time slices to run!
100060010許軒中 100060017李紀萱 100060025鄧國盛
20
100060010許軒中 100060017李紀萱 100060025鄧國盛
21
But…….
Imagine if we had 10 background threads and 1 foreground thread.
Are nice levels enough to guarantee that the response time of our foreground thread is unaffected?
Background Cgroup
5% of all CPU cycles
Default/Foreground Cgroup
95% of all CPU cycles
100060010許軒中 100060017李紀萱 100060025鄧國盛
Linux CFS Nice Values
Hard on the CPU
22
Nicer to the CPU
- - - - - - - - - - - - - - - - - - - 1 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1 1 9 8 7 6 5 4 3 2
0 1 2 3 4 5 6 7 8 9
0 9 8 7 6 5 4 3 2 1 0
THREAD_PRIORITY_LOWEST
THREAD_PRIORITY_DEFAULT
THREAD_PRIORITY_URGENT_AUDIO
Default Cgroup – 95%
Background Cgroup – 5%
100060010許軒中 100060017李紀萱 100060025鄧國盛
Threading in Android
23
When an Android application is first
started, the runtime system creates a
single thread in which all application
components will run by default. This
thread is generally referred to as the
main thread.
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android also supports java.lang.thread.
There are basically two main ways of having a
Thread execute application code.
24
Create a new class that extends Thread and override
its run() method.
MyThread t = new MyThread();
t.start();
public class MyThread extends Thread{
@Override
public void run() {
super.run();
}//run
}//class
100060010許軒中 100060017李紀萱 100060025鄧國盛
Create a new Runnable instance passing to it a Thread object.
25
Runnable myRunnable1 = new MyRunnableClass();
Thread t1 = new Thread(myRunnable1);
t1.start();
public class MyRunnableClass implements
Runnable {
@Override
public void run() {
}//run
}//class
100060010許軒中 100060017李紀萱 100060025鄧國盛
Programming Language
26
Android development is java-based (most of the times), because a large
portion of Java libraries is supported in Android along with the Android libraries.
Java.lang
Java.math
Javax.sql
Javax.xml.parsers
Java.text
Java.security
http://developer.android.com/reference/packages.html
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android SDK
27
100060010許軒中 100060017李紀萱 100060025鄧國盛
Android SDK
28
100060010許軒中 100060017李紀萱 100060025鄧國盛