X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=doc%2Fhtml%2Ftep106.html;h=8f19af5726fb16de41bc8ad3c7b4fb31e0deb69f;hb=843be811b125fd0bb60a470c2687dce7e8398471;hp=b7f835374ce9afdced22e7e2d4fcfc84ec02a422;hpb=d56750cc1c9423ffd51150040b12d64b6d2cc0d0;p=tinyos-2.x.git diff --git a/doc/html/tep106.html b/doc/html/tep106.html index b7f83537..8f19af57 100644 --- a/doc/html/tep106.html +++ b/doc/html/tep106.html @@ -3,7 +3,7 @@ - + Schedulers and Tasks +

Schedulers and Tasks

@@ -302,7 +298,6 @@ ul.auto-toc {
Philip Levis and Cory Sharp
-

Note

This memo documents a part of TinyOS for the TinyOS Community, and @@ -310,13 +305,13 @@ requests discussion and suggestions for improvements. Distribution of this memo is unlimited. This memo is in full compliance with TEP 1.

-
-

Abstract

+
+

Abstract

This memo documents the structure and implementation of tasks and task schedulers in TinyOS 2.x.

-
-

1. Introduction

+
+

1. Introduction

TinyOS has two basic computational abstractions: asynchronous events and tasks. Early versions of TinyOS provided a single type of task -- parameter free -- and only a FIFO scheduling policy. While changing @@ -328,8 +323,8 @@ available. TinyOS 2.0 takes both approaches, and this memo documents the structure of how it does so as well as a simple mechanism that greatly increases system dependability.

-
-

2. Tasks and the Scheduler in TinyOS 1.x

+
+

2. Tasks and the Scheduler in TinyOS 1.x

Tasks in TinyOS are a form of deferred procedure call (DPC) [1], which enable a program to defer a computation or operation until a later time. TinyOS tasks run to completion and do not pre-empt one @@ -340,8 +335,8 @@ are atomic with respect to other tasks task declarations and post expressions:

 task void computeTask() {
-  // Code here  
-}  
+  // Code here
+}
 

and:

@@ -398,28 +393,28 @@ application component does not try to send another packet until it
 knows the one it is sending completes (so it can re-use the
 buffer). As the sendDone() event was lost, this does not occur,
 and the application stops sending network traffic.

-

The solution to this particular problem in TinyOS 1.x is to signal -sendDone() in the radio send complete interrupt if the post fails: -this violates the sync/async boundary, but the justification is that -a possible rare race condition is better than certain failure. +

The solution to this particular problem in TinyOS 1.x is to signal +sendDone() in the radio send complete interrupt if the post fails: +this violates the sync/async boundary, but the justification is that +a possible rare race condition is better than certain failure. Another solution would be to use an interrupt source to periodically retry posting the task; while this does not break the sync/async -boundary, until the post succeeds the system cannot send packets. +boundary, until the post succeeds the system cannot send packets. The TinyOS 1.x model prevents it from doing any better.

-
-

3. Tasks in TinyOS 2.x

+
+

3. Tasks in TinyOS 2.x

The semantics of tasks in TinyOS 2.x are different than those in 1.x. This change is based on experiences with the limitations and run time errors that the 1.x model introduces. In TinyOS 2.x, a basic post will -only fail if and only if the task has already been posted and has not -started execution. A task can always run, but can only have one +only fail if and only if the task has already been posted and has not +started execution. A task can always run, but can only have one outstanding post at any time.

2.x achieves these semantics by allocating one byte of state per task (the assumption is that there will be fewer than 255 -tasks in the system). While a very large number of tasks could make +tasks in the system). While a very large number of tasks could make this overhead noticable, it is not significant in practice. -If a component needs to post a task several times, then the end of +If a component needs to post a task several times, then the end of the task logic can repost itself as need be.

For example, one can do this:

@@ -429,7 +424,7 @@ task void processTask() {
   // do work
   if (moreToProcess) {
     post processTask();
-  } 
+  }
 }
 

These semantics prevent several problems, such as the inability to @@ -447,15 +442,15 @@ and an event, run. Th up to the interface. For example, a task interface that allows a task to take an integer parameter could look like this:

-interface TaskParameter {  
-  async error_t command postTask(uint16_t param);  
-  event void runTask(uint16_t param);  
-}  
+interface TaskParameter {
+  async error_t command postTask(uint16_t param);
+  event void runTask(uint16_t param);
+}
 

Using this task interface, a component could post a task with a uint16_t parameter. When the scheduler runs the task, it will signal the runTask event with the passed parameter, which contains -the task's logic. Note, however, that this does not save any RAM: +the task's logic. Note, however, that this does not save any RAM: the scheduler must have RAM allocated for the parameter. Furthermore, as there can only be one copy of a task outstanding at any time, it is just as simple to store the variable in the component. E.g., @@ -475,7 +470,7 @@ uint16_t param; post parameterTask(); ... task void parameterTask() { - // use param + // use param }

The principal difference between the simplest code for these @@ -490,8 +485,8 @@ if (post myTask() == SUCCESS) { }

-
-

4. The Scheduler in TinyOS 2.x

+
+

4. The Scheduler in TinyOS 2.x

In TinyOS 2.x, the scheduler is a TinyOS component. Every scheduler MUST support nesC tasks. It MAY also support any number of additional task interfaces. The scheduler component is resonsible for @@ -507,29 +502,29 @@ function to obtain a unique identifier, which the scheduler uses to dispatch tasks.

For example, the standard TinyOS scheduler has this signature:

-module SchedulerBasicP {  
-  provides interface Scheduler;  
-  provides interface TaskBasic[uint8_t taskID];  
-  uses interface McuSleep;  
-}  
+module SchedulerBasicP {
+  provides interface Scheduler;
+  provides interface TaskBasic[uint8_t taskID];
+  uses interface McuSleep;
+}
 

A scheduler MUST provide a parameterized TaskBasic interface. If a call to TaskBasic.postTask() returns SUCCESS, the scheduler MUST run it -eventually, so that starvation is not a concern. The scheduler MUST +eventually, so that starvation is not a concern. The scheduler MUST return SUCCESS to a TaskBasic.postTask() operation unless it is not the first call to TaskBasic.postTask() since -that task's TaskBasic.runTask() event has been signaled. The +that task's TaskBasic.runTask() event has been signaled. The McuSleep interface is used for microcontroller power management; its workings are explained in TEP 112 [3].

-

A scheduler MUST provide the Scheduler interface. +

A scheduler MUST provide the Scheduler interface. The Scheduler interface has commands for initialization and running tasks, and is used by TinyOS to execute tasks:

-interface Scheduler {  
-  command void init();  
-  command bool runNextTask(bool sleep);  
-  command void taskLoop();  
-}  
+interface Scheduler {
+  command void init();
+  command bool runNextTask(bool sleep);
+  command void taskLoop();
+}
 

The init() command initializes the task queue and scheduler data structures. runNextTask() MUST run to completion whatever task the @@ -549,17 +544,17 @@ within the scheduler improves the efficiency of the task loop, in terms of the assembly generated by the TinyOS toolchain.

This is the TaskBasic interface:

-interface TaskBasic {  
-  async command error_t postTask();  
-  void event runTask();  
-}  
+interface TaskBasic {
+  async command error_t postTask();
+  void event runTask();
+}
 

When a component declares a task with the task keyword in nesC, it is implicitly declaring that it uses an instance of the TaskBasic interface: the task body is the runTask event. When a component uses the post keyword, it calls the postTask command. Each TaskBasic MUST be -wired to the scheduler with a unique identifier as its parameter. -The parameter MUST be obtained with the unique function in nesC, +wired to the scheduler with a unique identifier as its parameter. +The parameter MUST be obtained with the unique function in nesC, with a key of "TinySchedulerC.TaskBasic". The nesC compiler automatically does this wiring when the task and post keywords are used.

@@ -572,8 +567,8 @@ components MUST NOT assume a FIFO policy. If two tasks must run in a particular temporal order, this order should be enforced by the earlier task posting the later task.

-
-

5. Replacing the Scheduler

+
+

5. Replacing the Scheduler

The TinyOS scheduler is presented as a component named TinySchedulerC. The default TinyOS scheduler implementation is a module named SchedulerBasicP; the default scheduler component is a configuration @@ -586,41 +581,41 @@ scheduler implementations MUST provide a parameterize TaskBasic interface, as SchedulerBasicP does; this supports nesC post statements and task declarations and enables TinyOS core systems to operate properly. Generally, TinyOS core code needs to be able to run unchanged -with new scheduler implementations. All scheduler +with new scheduler implementations. All scheduler implementations MUST provide the Scheduler interface.

For example, imagine a hypothetical scheduler that provides earliest deadline first tasks, which are provided through the TaskEdf interface:

-interface TaskEdf {  
-  async command error_t postTask(uint16_t deadlineMs);  
-  event void runTask();  
-}  
+interface TaskEdf {
+  async command error_t postTask(uint16_t deadlineMs);
+  event void runTask();
+}
 

The scheduler implementation is named SchedulerEdfP, and provides both TaskBasic and TaskEdf interfaces:

-module SchedulerEdfP {  
-  provides interface Scheduler;  
-  provides interface TaskBasic[uint8_t taskID];  
-  provides interface TaskEdf[uint8_t taskID];  
-}  
+module SchedulerEdfP {
+  provides interface Scheduler;
+  provides interface TaskBasic[uint8_t taskID];
+  provides interface TaskEdf[uint8_t taskID];
+}
 

An application that wants to use SchedulerEdfP instead of SchedulerBasicP includes a configuration named TinySchedulerC, which exports all of SchedulerEdfP's interfaces:

-configuration TinySchedulerC {  
-  provides interface Scheduler;  
-  provides interface TaskBasic[uint8_t taskID];  
-  provides interface TaskEdf[uint8_t taskID];  
-}  
-implementation {  
-  components SchedulerEdfP;  
-  Scheduler = SchedulerEdf;  
-  TaskBasic = SchedulerEdfP;  
-  TaskEDF   = SchedulerEdfP;  
-}  
+configuration TinySchedulerC {
+  provides interface Scheduler;
+  provides interface TaskBasic[uint8_t taskID];
+  provides interface TaskEdf[uint8_t taskID];
+}
+implementation {
+  components SchedulerEdfP;
+  Scheduler = SchedulerEdf;
+  TaskBasic = SchedulerEdfP;
+  TaskEDF   = SchedulerEdfP;
+}
 

For a module to have an earliest deadline first task, it uses the TaskEdf interface. Its configuration SHOULD wire it to TinySchedulerC. @@ -634,14 +629,14 @@ is to #define it. For example, TaskEdf.nc might include:

In this example, the module SomethingP requires two EDF tasks:

-configuration SomethingC {  
-  ...  
-}  
-implementation {  
-  components SomethingP, TinySchedulerC;  
-  SomethingP.SendTask -> TinySchedulerC.TaskEdf[unique(UQ_TASK_EDF)];  
-  SomethingP.SenseTask -> TinySchedulerC.TaskEdf[unique(UQ_TASK_EDF)];  
-}  
+configuration SomethingC {
+  ...
+}
+implementation {
+  components SomethingP, TinySchedulerC;
+  SomethingP.SendTask -> TinySchedulerC.TaskEdf[unique(UQ_TASK_EDF)];
+  SomethingP.SenseTask -> TinySchedulerC.TaskEdf[unique(UQ_TASK_EDF)];
+}
 

The module SomethingP also has a basic task. The nesC compiler automatically transforms task keywords into BasicTask interfaces and @@ -651,55 +646,55 @@ interface. A component SHOULD use the keywords whenever possible, and it MUST NOT mix the two syntaxes for a given task. This is an example implementation of SomethingP that uses keywords for basic tasks:

-module SomethingP {  
-  uses interface TaskEdf as SendTask  
-  uses interface TaskEdf as SenseTask  
-}  
-implementation {  
-  // The TaskBasic, written with keywords  
-  task void cleanupTask() { ... some logic ... }  
-  event void SendTask.runTask() { ... some logic ... }  
-  event void SenseTask.runTask() { ... some logic ... }  
-
-  void internal_function() {  
-    call SenseTask.postTask(20);  
-    call SendTask.postTask(100);  
-    post cleanupTask();  
-  }  
-}  
+module SomethingP {
+  uses interface TaskEdf as SendTask
+  uses interface TaskEdf as SenseTask
+}
+implementation {
+  // The TaskBasic, written with keywords
+  task void cleanupTask() { ... some logic ... }
+  event void SendTask.runTask() { ... some logic ... }
+  event void SenseTask.runTask() { ... some logic ... }
+
+  void internal_function() {
+    call SenseTask.postTask(20);
+    call SendTask.postTask(100);
+    post cleanupTask();
+  }
+}
 

The requirement that basic tasks not be subject to starvation requires that a scheduler supporting EDF tasks must ensure that basic tasks run eventually even if there is an unending stream of short deadline tasks to run. Quantifying "eventually" is difficult, -but a 1% share of the MCU cycles (or invocations) is a reasonable +but a 1% share of the MCU cycles (or invocations) is a reasonable approximation.

If the scheduler provides two instances of the same task interface, -their unique keys are based on the name of the interface as the +their unique keys are based on the name of the interface as the scheduler presents it (the "as" keyword). For example, imagine a scheduler which provides two instances of TaskBasic: standard tasks and high-priority tasks. The scheduler usually selects a task for the high priority queue before the standard queue:

-configuration TinySchedulerC {  
-  provides interface Scheduler;  
-  provides interface TaskBasic[uint8_t taskID];  
-  provides interface TaskBasic[uint8_t taskID] as TaskHighPriority;  
-}  
+configuration TinySchedulerC {
+  provides interface Scheduler;
+  provides interface TaskBasic[uint8_t taskID];
+  provides interface TaskBasic[uint8_t taskID] as TaskHighPriority;
+}
 

It cannot always select a high priority task because that could -starve basic tasks. A component that uses a high priority task would +starve basic tasks. A component that uses a high priority task would wire to TaskHighPriority with the key "TinySchedulerC.TaskHighPriority":

-configuration SomethingElseC {}  
-implementation {  
-  components TinySchedulerC as Sched, SomethingElseP;  
-  SomethingElseP.RetransmitTask -> Sched.TaskHighPriority[unique("TinySchedulerC.TaskHighPriority")];  
-}  
+configuration SomethingElseC {}
+implementation {
+  components TinySchedulerC as Sched, SomethingElseP;
+  SomethingElseP.RetransmitTask -> Sched.TaskHighPriority[unique("TinySchedulerC.TaskHighPriority")];
+}
 
-
-

6. Implementation

+
+

6. Implementation

The following files in tinyos-2.x/tos/system contain the reference implementations of the scheduler:

@@ -713,15 +708,15 @@ that wires SchedulerBasicP to McuSleepC http://csl.stanford.edu/~pal/tinyos/edf-sched.tgz.

-
-

7. Author's Address

+
+

7. Author's Address

Philip Levis
358 Gates Hall
Stanford University
Stanford, CA 94305

-
phone - +1 650 725 9046
+
phone - +1 650 725 9046

Cory Sharp
@@ -732,12 +727,12 @@ at the URL http://csl.stanford.ed
-
-

8. Citations

+
+

8. Citations

- @@ -759,8 +754,8 @@ Programming Language Design and Implementation (PLDI).
[1]Erik Cota-Robles and James P. Held. "A Comparison of Windows +
[1]Erik Cota-Robles and James P. Held. "A Comparison of Windows Driver Model Latency Performance on Windows NT and Windows 98." In Proceedings of the Third Symposium on Operating System Design and Implementation (OSDI).
-
-

Appendix A: Changing the Scheduler

+
+

Appendix A: Changing the Scheduler

The nesC compiler transforms the post and task keywords into nesC interfaces, wirings, and calls. By default, the statement:

@@ -771,7 +766,7 @@ implementation {
   task x() {
     ...
     post x();
-  } 
+  }
 
 }