28
Android OS 100060010 許許許 100060017 許許許 100060025 許許許 1

Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

Embed Size (px)

Citation preview

Page 1: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

1

Android OS 100060010 許軒中

100060017 李紀萱

100060025 鄧國盛

Page 2: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

2Outline

Overview of Android OS

Multiprogramming and Multitasking

Scheduling employed

Threading supported

Programming languages and IDEs supported

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 3: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

java

C

3

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Android Architecture

Page 4: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

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.

4

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Android Architecture(Cont)

Page 5: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

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 .class .dex .apk

compile convert package

5

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Android Architecture(Cont)

Dalvik

VM

app

Dalvik

VM

app

Dalvik

VM

app

DalvikVM

app app app

Page 6: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

6

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 7: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

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.

KernelThe 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.

7

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Android Architecture(Cont)

Page 8: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

8Anatomy of an Android Application

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 ServicesContent

ProvidersBroadcast receivers

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 9: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

9Activities

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 3Activity 2

Show list of new

emails

Compose emails

Reading emails

Email Application

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 10: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

10Services

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.

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 鄧國盛

Page 11: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

11A unique aspect

Any application can start another application’s component.

Camera Application

Photo Editing Application

Component 1

Component 2

Component 3

Android OS

IntentComponent 1

Component 2

Component 3

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 12: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

12MultitaskingThe 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.

Foreground Process

Visible Process

Service Process

Background Process

Empty Process

Importance

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 13: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

13Foreground Process

Visible Process

It hosts an Activity that the user is interacting with.

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.

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.

Visible Process

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 14: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

14Service Process

Empty Process

They are generally doing things that the user cares about (such as playing music in the background or downloading data on the network),

Background Process

A process holding an activity that's not currently visible to the user

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.

Service Process

Background Process

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 15: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

15When 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.

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 鄧國盛

Page 16: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

16Scheduling

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”

Since Android is based on the Linux kernel, it shares many aspects with Linux, including how processes and threads are scheduled.

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 17: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

17CFS SchedulerScheduler 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 1Nice Value 0Weight 1024

Thread 2Nice Value 5Weight 335

1024/(1024+335)335/(1024+335

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 18: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

18VruntimeTo pick out the next process to run, Linux picks the process with the lowest vruntime (virtual run time)

Thread 1Nice Value 0Weight 1024

Thread 2Nice Value 5Weight 335

Run for 1 nsVruntime += 1

Run for 1 nsVruntime += 3

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 19: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

19Time 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 鄧國盛

Owner
Page 20: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

20

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 21: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

21

Background Cgroup

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?

95% of all CPU cycles5% of all CPU cycles

But…….

Default/Foreground Cgroup

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 22: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

22Linux CFS Nice Values

-20

-19

-18

-17

-16

-15

-14

-13

-12

-11

-10

-9

-8

-7

-6

-5

-4

-3

-2

1 0 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

Nicer to the CPUHard on the CPU

Background Cgroup – 5%Default Cgroup – 95%

TH

REA

D_P

RIO

RIT

Y_U

RG

EN

T_A

UD

IO

TH

REA

D_P

RIO

RIT

Y_LO

WEST

TH

REA

D_P

RIO

RIT

Y_D

EFA

ULT

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 23: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

23Threading in Android

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 鄧國盛

Page 24: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

24

Android also supports java.lang.thread.

There are basically two main ways of having a Thread execute application code.

Create a new class that extends Thread and overrideits run() method.

MyThread t = new MyThread();t.start();

public class MyThread extends Thread{@Override

public void run() {super.run();}//run

}//class100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 25: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

25Create a new Runnable instance passing to it a Thread object.

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 鄧國盛

Page 26: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

26Programming LanguageAndroid 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.langJava.mathJavax.sqlJavax.xml.parsersJava.textJava.security

http://developer.android.com/reference/packages.html

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 27: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

27Android SDK

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛

Page 28: Android OS 100060010 許軒中 100060017 李紀萱 100060025 鄧國盛 1

28Android SDK

100060010 許軒中 100060017 李紀萱 100060025 鄧國盛