Using ThreadX and IAR EWRX - Renesas E-learning

Using ThreadX® and IAR Embedded
Workbench on the Renesas RX Processor
John Carbone, VP Marketing, Express Logic, Inc.
Sean Prestridge, Field Applications Engineer, IAR
Class ID: 9C08I
Renesas Electronics America Inc.
© 2012 Renesas Electronics America Inc. All rights reserved.
Agenda
 Part I - Key Concepts
 ThreadX RTOS
– Threads and Priorities
– Context Switching
– Types of Scheduling
– Multithreading
– Preemptive Scheduling
– Preemption-Threshold™ Scheduling
 IAR Embedded Workbench IDE
– Compiler
– Debugger
– RTOS Awareness
– Macros
– External Tools
2
© 2012 Renesas Electronics America Inc. All rights reserved.
Agenda (continued)
 Part II – An Example
 Structure of the Example
– 4 Threads
– Message-Passing
 Setting Priorities
– Equal
• Round-Robin Scheduling
– Unique
• Preemptive Scheduling
• Preemption-Threshold Scheduling
 Assessing The Results Using TraceX
 Summary and Conclusion
 Q/A
 For More Information
3
© 2012 Renesas Electronics America Inc. All rights reserved.
Presenter: John A. Carbone
 VP, Marketing, Express Logic, Inc.
 Responsible for product and corporate
marketing, Renesas partner relationship,
technical articles, and technical training.
 Presenter at Renesas DevCon 2008, 2010,
and various industry conferences
 Authored technical papers on real-time multithreading,
certification, and measurement of real-time performance
PREVIOUS EXPERIENCE:
 VP, Marketing at Green Hills Software
 Embedded developer and FAE
 Member of the IEEE
 BA, Mathematics, Boston College
4
© 2012 Renesas Electronics America Inc. All rights reserved.
Shawn A. Prestridge
 Senior Field Applications Engineer
 Responsible for Embedded Workbench training
 Develops applications for many different boards
 Develops applications for middleware such as:
– RTOS
– GUI
– Projects as examples and for customers
PREVIOUS EXPERIENCE:
 Embedded hardware/software engineer for Texas
instruments.
 Contractual-based development for Ministry of Software
 Degree work from Southern Methodist University: BSEE, BS
Mathematics, MSEE, MS Software Engineering, PhD in EE
5
© 2012 Renesas Electronics America Inc. All rights reserved.
ThreadX
 ThreadX is Express Logic’s small, fast, RTOS for hard real-time
applications
 Small: Under 3KB for RX
 Fast: Under 1.0us context switch time @ 96MHz
 Easy-to-Use: Intuitive API, Textbook, User Guide, Training and
Commercial Support
 Full source code, Royalty-free
 ThreadX Services
 Scheduling
 Interrupt Servicing
 Threads
 Timers
 Queues
 Semaphores
 Mutexes
 Event Flag Groups
 Block/Byte Memory Pools
6
© 2012 Renesas Electronics America Inc. All rights reserved.
IAR Embedded Workbench for RX
 Support for all RX
devices
 Hardware FPU support
 ROPI (position
independent code/data)
support
 ThreadX RTOS awerness
 Renesas ABI (Application Binary Interface) compliant
Link compatible
Renesas libraries can be linked with EW
RPDL compliant (Renesas Peripheral Driver Library)
7
© 2012 Renesas Electronics America Inc. All rights reserved.
IAR Embedded Workbench for RX
 Ready-made example projects demonstrating the different peripheral’s of
the MCU
 Most popular boards supported
 Kickstart version of EW included in Renesas Starterkits
 Best optimized RX compiler!
8
© 2012 Renesas Electronics America Inc. All rights reserved.
Using ThreadX and IAR EWRX
 ThreadX is Integrated for use with IAR Embedded Workbench for
RX (EWRX)
 Delivered as EWRX Projects (Library Build and Demo App)
 EWRX RTOS Awareness for ThreadX
 EWRX ThreadX Execution Profiling
 EWRX Launch of TraceX Analysis Tool
 Build Library
 Set compile options
– Optimization Level
– Debug symbols
– Event Trace (see TraceX later)
– Execution Profiling
 Link with Application
 Any C/C++ code
 Also link initialization code (provided with ThreadX)




9
Download
Run
Debug
Optimize
© 2012 Renesas Electronics America Inc. All rights reserved.
Threads and Priorities
 Threads
 What is a thread?
– Semi-independent program
segment
– Share same memory space
– Run “concurrently”
Process memory space
Process
 How are threads used?
– Modularize a program
– Minimize stalls
 Thread Services
– Create, Suspend, Relinquish,
Terminate, Exit, Prioritize
Thread
Thread
 Thread States
– READY, RUNNING, SUSPENDED, TERMINATED
 Thread Priorities
 Often 0-n, with 0 highest
 Dynamic or Static
 Equal priorities
– Multiple threads at same priority
 Unique priorities
– Each thread has unique priority
10
© 2012 Renesas Electronics America Inc. All rights reserved.
Priority
0
Highest
1
2
…
n
Lowest
…
Thread
Context Switch
 Thread Context
 Information critical to thread’s operation
 Register Contents, Program Counter, Stack
Pointer
PC
Registers
SP
 Saved when thread is preempted
 Restored when thread is resumed
 Context Switch
 Interrupt running thread
and do something else
 Result of preemption,
interrupt, or cooperative
service
 What’s involved in a
context switch?
 See
11
© 2012 Renesas Electronics America Inc. All rights reserved.
Step
Operation
Cycles
1
Save the current thread’s context (ie: GP and FP register
values and PC) on the stack.
2
Save the current stack pointer in the thread's control block.
2 - 20
3
Switch to the system stack pointer.
2 - 20
4
Return to the scheduler.
2 - 20
5
Find the highest priority thread that is ready to run.
2 - 50
6
Switch to the new thread's stack.
2 - 50
7
Recover the new thread's context.
20 - 100
8
Return to the new thread at its previous PC.
2 - 40
9
Other processing
0 - 100
TOTAL
50 - 500
20 - 100
Multithreading
 Enabling an activity to use the CPU while other activities don’t
need it – I/O Delay
Time
Thread A
I/O
Start
Thread A
Waits
I/O
Finish
Thread A
Thread B
Thread A has
to wait for
I/O
12
While Thread A
is waiting for
I/O, Thread B
can use CPU
© 2012 Renesas Electronics America Inc. All rights reserved.
When I/O is
done, Thread
A can continue
Preemptive Scheduling
 Preemption
 Interruption for higherpriority activity
– Interrupt
– Thread
 Always run highest
priority thread that is
READY to run
– Maximum
responsiveness
– No Polling, so more
efficient
– Always results in a
context switch
13
© 2012 Renesas Electronics America Inc. All rights reserved.
Priority
 Preemptive Scheduling
Thread-2
Runs
Context
Switch
Thread-1
Begins
Time
Context
Switch
Thread-1
Resumes
Round-Robin Scheduling
 Among threads of a given priority
 Threads of different priorities use either preemptive or
preemption-threshold scheduling
 Threads take turns
 Run to Completion
 Or, relinquish (suspend)
 Or, until blocked (suspend)
 Or, until time slice is exhausted (suspend)
14
© 2012 Renesas Electronics America Inc. All rights reserved.
Preemption-Threshold Scheduling
 A technique to reduce context switches
 Preemption-Threshold Scheduling establishes a
priority for preemption. Preemption requires a priority
higher (lower number) than the preemption-threshold.
 For example, assume a
thread’s priority is 20,
Priority
Comment
and its preemption
0
Preemption allowed for threads with priorities
threshold is set to 15
:
from 0 to 14 (inclusive)
14
 Threads with priority
15
lower than (larger
Thread is assigned Preemption-Threshold = 15 [this has the
effect of disabling preemption for threads with priority values
:
from 15 to 19 (inclusive)]
number) 14 , even if
19
higher than (smaller
number) the running
20
Thread is assigned Priority = 20
thread’s priority (20),
:
will not preempt the
31
running thread
15
© 2012 Renesas Electronics America Inc. All rights reserved.
Part-II: An example to show how various
scheduling techniques affect performance
16
© 2012 Renesas Electronics America Inc. All rights reserved.
The Application
 Fully Preemptive, Round-Robin, or Preemption-Threshold
Scheduling:
 What are the consequences of each with respect to context
switching?
 Construct a system to run and observe
 Producer-consumer application
– One producer, three consumers
– 9 messages sent, 9 received
– Continuous operation
 Log events
 Use EWRX and TraceX to debug
 Count context switches
 Measure throughput
 Draw conclusions
17
© 2012 Renesas Electronics America Inc. All rights reserved.
Producer
Consumer
Consumer
Consumer
An Example To See The Impact
Cycle 1
Cycle 2
Thread A
Thread B
Thread C
Thread D
Send a
Message to
Threads A,
B, C
Send a
Message to
Threads A,
B, C
Send a
Message to
Threads A,
B, C
Time
18
© 2012 Renesas Electronics America Inc. All rights reserved.
Receive
A’s
Messages
Receive
A’s
Messages
Receive
B’s
Messages
Receive
B’s
Messages
Receive
C’s
Messages
Receive
C’s
Messages
Send a
Message to
Threads A,
B, C
Send a
Message to
Threads A,
B, C
Send a
Message to
Threads A,
B, C
Priority Assignments
 Case-1
 Preemptive Scheduling
 Thread A = 1
 Thread B = 2
 Thread C = 3
Case-1
Case-2
Case-3
Thread A
Thread A
Thread A
Thread B
Thread A
1
 Thread D = 4
Thread C
 Case-2
 Thread A = 1
 Thread B = 1
 Thread C = 1
Thread D
Priority
 Round-Robin Scheduling
Thread D
Thread B
Thread B
2
Thread B
 Thread D = 1
 Case-3
Thread C
 Preemption-Threshold
Scheduling
3
Thread C
Thread C
 Thread A = 1, PT=1
 Thread B = 2, PT=2
 Thread C = 3, PT=3
 Thread D = 4, PT=1
Priority
19
PT
© 2012 Renesas Electronics America Inc. All rights reserved.
4
Thread D
Thread D
Build, Run, Debug the Application
 Open Project
in EWRX
 View Project
Files
 View/Edit
Code
 Build,
Download,
Debug
20
© 2012 Renesas Electronics America Inc. All rights reserved.
Debug View
 Stop at
Breakpoint
 View
Counters
 View
Thread List
 View
Message
Queues,
Memory,
etc.
 Analyze
results
using
TraceX
21
© 2012 Renesas Electronics America Inc. All rights reserved.
TraceX
 Real-time event trace
 Shows RTOS events
 Shows Application
events
 Target trace_buffer
 EWRX uploads to host
at breakpoint via
macro
 Launch from EWRX
22
© 2012 Renesas Electronics America Inc. All rights reserved.
Analyzing Results Using TraceX
 Launch TraceX from the Embedded Workbench IDE
23
© 2012 Renesas Electronics America Inc. All rights reserved.
Examining The Events
 Case-1: Preemptive
Scheduling
 Thread D sends a
message to Thread A
 Thread A preempts
 Thread A reads its
message then
suspends (queue
empty)
 Thread D sends
message to Thread B
 Thread B preempts
 Similarly for Thread C
 9 Messages
 18 Context Switches
24
© 2012 Renesas Electronics America Inc. All rights reserved.
1
1
2
2
3
4
3
5
4
6
7
5
8
6
7
8
9
9 10 11 12 13 14 15 16 17 18
Examining The Events
 Case-2: Round-Robin
Scheduling (Equal
Priorities)
 Thread D sends 3
messages to each
queue
 Thread D then
suspends
 Thread A reads its 3
messages then
suspends (queue
empty)
 Similarly, for Threads
B and C
 Thread D then writes
another set of
messages
 9 Messages
 4 Context Switches
25
© 2012 Renesas Electronics America Inc. All rights reserved.
2
1
3
4
Examining The Events
 Case-3: PreemptionThreshold Scheduling
 Thread D sends 3
messages to each
queue
 Thread D then
suspends
 Thread A reads its 3
messages then
suspends (queue
empty)
2
 Similarly, for Threads
B and C
 Thread D then writes
another set of
messages
 9 Messages
 4 Context Switches
26
© 2012 Renesas Electronics America Inc. All rights reserved.
3
1
4
Compare Context Switches
Case-1
No PTS
1
2 3
Case-2 and Case-3
Round-Robin or PTS
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
2
3
1
Context Switches
27
© 2012 Renesas Electronics America Inc. All rights reserved.
4
Compare Timing
 Case-1 shows 7,531 ticks
in a cycle
 Case-2 and Case-3 show
4,420 ticks in a cycle
28
© 2012 Renesas Electronics America Inc. All rights reserved.
Assessing The Results
 Context Switches
Case
Messages
Context
Switches
Case-1: Unique Priorities
9
18
Case-2/3: RoundRobin/Preemption-Threshold
9
4
 Throughput
29
Measurement
Case-1
(Unique
Priorities)
Case-2/3 (RoundRobin/Preemption
-Threshold)
Ratio
(Case 1 vs Case-2/3)
Context Switches
18
4
450%
Elapsed Time
7,531 ticks
4,420 ticks
170%
Messages Sent
9
9
No Change
Messages Received
9
9
No Change
© 2012 Renesas Electronics America Inc. All rights reserved.
Summary And Conclusions
 Round-Robin or Preemption-Threshold Scheduling can
reduce the number of context switches an application
must perform
 Full Preemption delivers maximum responsiveness, but may
result in overhead that has a negative impact on throughput
 Round-Robin and PTS offer solutions to excessive context
switch overhead and can increase throughput
 Good Tools can make RX development easier
 IAR Embedded Workbench IDE makes building and debugging
easy
 TraceX eases analysis of real-time applications
 Strong Integration Makes Tools easy To Use
 ThreadX Kernel Awareness in EWRX
 TraceX Launch from EWRX
 ThreadX delivered as EWRX Projects
30
© 2012 Renesas Electronics America Inc. All rights reserved.
Q/A
 Q/A
 For further information about ThreadX:
 Contact Express Logic, Inc.
– 1-888-THREADX (1-858-613-6640)
– [email protected]
 Visit: www.rtos.com
 For further information about IAR Embedded Workbench for
RX:
 Contact IAR
– 1-650-287-4250 (Western US)
– 1-508-281-6680 (Eastern US)
– +46 18 16 78 00 (HQ – Sweden)
– [email protected]
 Visit www.iar.com
31
© 2012 Renesas Electronics America Inc. All rights reserved.
Please Provide Your Feedback…
 Please utilize the ‘Guidebook’ application to leave feedback
or
 Ask me for the paper feedback form for you to use…
32
© 2012 Renesas Electronics America Inc. All rights reserved.
Renesas Electronics America Inc.
© 2012 Renesas Electronics America Inc. All rights reserved.