Chapter 5 Program Design and Analysis

120
Chapter 5 Program Design and Analysis 金金金金金 金金金金金金金金金金 (Slides are taken from the textbook slides)

description

Chapter 5 Program Design and Analysis. 金仲達教授 清華大學資訊工程學系 (Slides are taken from the textbook slides). Outline. Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing - PowerPoint PPT Presentation

Transcript of Chapter 5 Program Design and Analysis

Page 1: Chapter 5 Program Design and Analysis

Chapter 5

Program Design and Analysis

金仲達教授清華大學資訊工程學系

(Slides are taken from the textbook slides)

Page 2: Chapter 5 Program Design and Analysis

Program Design-2

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing Design example: software modem

Page 3: Chapter 5 Program Design and Analysis

Program Design-3

Software components Need to break the design up into pieces to

be able to write the code. Some component designs come up often. A design pattern is a generic description of

a component that can be customized and used in different circumstances. Design pattern: generalized description of the

design of a certain type of program. Designer fills in details to customize the

pattern to a particular programming problem.

Page 4: Chapter 5 Program Design and Analysis

Program Design-4

Pattern: state machine style State machine keeps internal state as a

variable, changes state based on inputs. State machine is useful in many contexts:

parsing user input responding to complex stimuli controlling sequential outputs for control-dominated code, reactive systems

Page 5: Chapter 5 Program Design and Analysis

Program Design-5

State machine example

idle

buzzer seated

belted

no seat/-

seat/timer onno belt and no timer/-

no belt/timer on

belt/-belt/

buzzer off

Belt/buzzer on

no seat/-

no seat/buzzer off

State machine

state

output step(input)

design pattern

Page 6: Chapter 5 Program Design and Analysis

Program Design-6

C code structure Current state is kept in a variable. State table is implemented as a switch.

Cases define states. States can test inputs.

while (TRUE) {switch (state) {

case state1: …}

}

Switch is repeatedly evaluated in a while loop.

Page 7: Chapter 5 Program Design and Analysis

Program Design-7

C implementation#define IDLE 0#define SEATED 1#define BELTED 2#define BUZZER 3switch (state) {case IDLE: if (seat)

{ state = SEATED; timer_on = TRUE; }break;

case SEATED: if (belt) state = BELTED;else if (timer) state = BUZZER;

break;…

}

Page 8: Chapter 5 Program Design and Analysis

Program Design-8

Another example

A B

C D

in1=1/x=a

in1=0/x=b

r=0/out2=1

r=1/out1=0

s=1/out1=1

s=0/out1=0

Page 9: Chapter 5 Program Design and Analysis

Program Design-9

C state tableswitch (state) {case A: if (in1==1) { x = a; state = B; } else { x = b; state = D; }break;

case B: if (r==0) { out2 = 1; state = B; } else { out1 = 0; state = C; }break;

case C: if (s==0) { out1 = 0; state = C; } else { out1 = 1; state = D; }break;

Page 10: Chapter 5 Program Design and Analysis

Program Design-10

Pattern: data stream style Commonly used in signal processing:

new data constantly arrives; each datum has a limited lifetime.

Use a circular buffer to hold the data stream.

x1 x2 x3 x4 x5 x6

t1 t2 t3

Data stream

x1 x2 x3 x4

Circular buffer

x5 x6 x7

Page 11: Chapter 5 Program Design and Analysis

Program Design-11

Circular buffer pattern

Circular buffer

init()add(data)data head()data element(index)

Page 12: Chapter 5 Program Design and Analysis

Program Design-12

Circular buffers Indexes locate currently used data, current

input data:

d1

d2

d3

d4

time t1

use

input d5

d2

d3

d4

time t1+1

use

input

Page 13: Chapter 5 Program Design and Analysis

Program Design-13

Circular buffer implementation: FIR filterint circ_buffer[N], circ_buffer_head = 0;int c[N]; /* coefficients */…int ibuf, ic;for (f=0, ibuff=circ_buff_head, ic=0;ic<N; ibuff=(ibuff==N-1?0:ibuff++), ic++)

f = f + c[ic]*circ_buffer[ibuf];

Page 14: Chapter 5 Program Design and Analysis

Program Design-14

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing Design example: software modem

Page 15: Chapter 5 Program Design and Analysis

Program Design-15

Models of programs Source code is not a good representation

for programs: clumsy; leaves much information implicit.

Compilers derive intermediate representations to manipulate and optimize the program.

Page 16: Chapter 5 Program Design and Analysis

Program Design-16

Data flow graph DFG: data flow graph. Does not represent control. Models basic block: code with one entry

and exit. Describes the minimal ordering

requirements on operations.

Page 17: Chapter 5 Program Design and Analysis

Program Design-17

Single assignment formx = a + b;y = c - d;z = x * y;y = b + d;

original basic block

x = a + b;y = c - d;z = x * y;y1 = b + d;

single assignment form

Page 18: Chapter 5 Program Design and Analysis

Program Design-18

Data flow graphx = a + b;y = c - d;z = x * y;y1 = b + d;

single assignment form

+ -

+*

DFG

a b c d

z

xy

y1

Page 19: Chapter 5 Program Design and Analysis

Program Design-19

DFGs and partial ordersPartial order: a+b, c-d; b+d, x*yCan do pairs of

operations in any order.+ -

+*

a b c d

z

xy

y1

Page 20: Chapter 5 Program Design and Analysis

Program Design-20

Control-data flow graph CDFG: represents control and data. Uses data flow graphs as components. Two types of nodes:

decision; data flow.

Page 21: Chapter 5 Program Design and Analysis

Program Design-21

Data flow nodeEncapsulates a data flow graph:

Write operations in basic block form for simplicity.

x = a + b;y = c + d

Page 22: Chapter 5 Program Design and Analysis

Program Design-22

Control

condT

F

Equivalent forms

valuev1

v2 v3

v4

Page 23: Chapter 5 Program Design and Analysis

Program Design-23

CDFG exampleif (cond1) bb1(); else bb2();bb3();switch (test1) {

case c1: bb4(); break;case c2: bb5(); break;case c3: bb6(); break;

}

cond1 bb1()

bb2()

bb3()

bb4()

test1

bb5() bb6()

T

F

c1

c2

c3

Page 24: Chapter 5 Program Design and Analysis

Program Design-24

for loopfor (i=0; i<N; i++)

loop_body();for loop

i=0;while (i<N) {

loop_body(); i++; }equivalent

i<N

loop_body()

T

F

i=0

Page 25: Chapter 5 Program Design and Analysis

Program Design-25

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing Design example: software modem

Page 26: Chapter 5 Program Design and Analysis

Program Design-26

Assembly and linking Last steps in compilation:

HLL compile assembly assembleHLLHLL assemblyassembly

linkexecutableload

Page 27: Chapter 5 Program Design and Analysis

Program Design-27

Multiple-module programs Programs may be composed from several

files. Addresses become more specific during

processing: relative addresses are measured relative to the

start of a module; absolute addresses are measured relative to

the start of the CPU address space.

Page 28: Chapter 5 Program Design and Analysis

Program Design-28

Assemblers Major tasks:

generate binary for symbolic instructions; translate labels into addresses; handle pseudo-ops (data, etc.).

Generally one-to-one translation. Assembly labels:

ORG 100label1 ADR r4,c

Page 29: Chapter 5 Program Design and Analysis

Program Design-29

Symbol table generation Use program location counter (PLC) to

determine address of each location. Scan program, keeping count of PLC. Addresses are generated at assembly

time, not execution time.

Page 30: Chapter 5 Program Design and Analysis

Program Design-30

Symbol table example

ADD r0,r1,r2

xx ADD r3,r4,r5CMP r0,r3

yy SUB r5,r6,r7

assembly code

xx 0x8

yy 0xa

PLC=0x7

PLC=0x8

PLC=0x9

PLC=0xa

symbol table

Page 31: Chapter 5 Program Design and Analysis

Program Design-31

Two-pass assembly Pass 1:

generate symbol table Pass 2:

generate binary instructions

Page 32: Chapter 5 Program Design and Analysis

Program Design-32

Relative address generation Some label values may not be known at

assembly time. Labels within the module may be kept in

relative form. Must keep track of external labels---can’t

generate full binary for instructions that use external labels.

Page 33: Chapter 5 Program Design and Analysis

Program Design-33

Pseudo-operations Pseudo-ops do not generate instructions:

ORG sets program location. EQU generates symbol table entry without

advancing PLC. Data statements define data blocks.

Page 34: Chapter 5 Program Design and Analysis

Program Design-34

Linking Combines several object modules into a

single executable module. Jobs:

put modules in order; resolve labels across modules.

Page 35: Chapter 5 Program Design and Analysis

Program Design-35

external reference

entry point

Externals and entry points

a ADR r4,yyyADD r3,r4,r5xxx ADD r1,r2,r3

B ayyy %1

Page 36: Chapter 5 Program Design and Analysis

Program Design-36

Module ordering Code modules must be placed in absolute

positions in the memory space. Load map or linker flags control the order

of modules.

module1

module2

module3

Page 37: Chapter 5 Program Design and Analysis

Program Design-37

Dynamic linking Some operating systems link modules

dynamically at run time: shares one copy of library among all executing

programs; allows programs to be updated with new

versions of libraries.

Page 38: Chapter 5 Program Design and Analysis

Program Design-38

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing Design example: software modem

Page 39: Chapter 5 Program Design and Analysis

Program Design-39

Compilation Compilation strategy (Wirth):

compilation = translation + optimization Compiler determines quality of code:

use of CPU resources; memory access scheduling; code size.

Page 40: Chapter 5 Program Design and Analysis

Program Design-40

Basic compilation phases

HLL

parsing, symbol table

machine-independentoptimizations

machine-dependentoptimizations

assembly

Page 41: Chapter 5 Program Design and Analysis

Program Design-41

Statement translation and optimization Source code is translated into

intermediate form such as CDFG. CDFG is transformed/optimized. CDFG is translated into instructions with

optimization decisions. Instructions are further optimized.

Page 42: Chapter 5 Program Design and Analysis

Program Design-42

Arithmetic expressionsa*b + 5*(c-d)

expression

DFG

* -

*

+

a b c d

5

Page 43: Chapter 5 Program Design and Analysis

Program Design-43

2

3

4

1

Arithmetic expressions, cont’d.

ADR r4,aMOV r1,[r4]ADR r4,bMOV r2,[r4]MUL r3,r1,r2

DFG

* -

*

+

a b c d

5ADR r4,cMOV r1,[r4]ADR r4,dMOV r5,[r4]SUB r6,r4,r5

MUL r7,r6,#5

ADD r8,r7,r3

code

Page 44: Chapter 5 Program Design and Analysis

Program Design-44

Control code generationif (a+b > 0)

x = 5;else

x = 7;a+b>0 x=5

x=7

Page 45: Chapter 5 Program Design and Analysis

Program Design-45

3

21

Control code generation, cont’d.

ADR r5,aLDR r1,[r5]ADR r5,bLDR r2,bADD r3,r1,r2BLE label3

a+b>0 x=5

x=7LDR r3,#5ADR r5,xSTR r3,[r5]B stmtent

label3 LDR r3,#7ADR r5,xSTR r3,[r5]

stmtent ...

Page 46: Chapter 5 Program Design and Analysis

Program Design-46

Procedure linkage Need code to:

call and return; pass parameters and results.

Parameters and returns are passed on stack. Procedures with few parameters may use

registers.

Page 47: Chapter 5 Program Design and Analysis

Program Design-47

Procedure stacks

proc1

growth

proc1(int a) {proc2(5);

}

proc2

SPstack pointer

FPframe pointer

5 accessed relative to SP

Page 48: Chapter 5 Program Design and Analysis

Program Design-48

ARM procedure linkage APCS (ARM Procedure Call Standard):

r0-r3 pass parameters into procedure. Extra parameters are put on stack frame.

r0 holds return value. r4-r7 hold register values. r11 is frame pointer, r13 is stack pointer. r10 holds limiting address on stack size to

check for stack overflows.

Page 49: Chapter 5 Program Design and Analysis

Program Design-49

Data structures Different types of data structures use

different data layouts. Some offsets into data structure can be

computed at compile time, others must be computed at run time.

Page 50: Chapter 5 Program Design and Analysis

Program Design-50

One-dimensional arrays C array name points to 0th element:

a[0]

a[1]

a[2]

a

= *(a + 1)

Page 51: Chapter 5 Program Design and Analysis

Program Design-51

Two-dimensional arrays Column-major layout:

a[0,0]

a[0,1]

a[1,0]

a[1,1] = a[i*M+j]

...

M

...

N

Page 52: Chapter 5 Program Design and Analysis

Program Design-52

Structures Fields within structures are static offsets:

field1

field2

aptrstruct { int field1; char field2;} mystruct;

struct mystruct a, *aptr = &a;

4 bytes

*(aptr+4)

Page 53: Chapter 5 Program Design and Analysis

Program Design-53

Expression simplification Constant folding:

8+1 = 9 Algebraic:

a*b + a*c = a*(b+c) Strength reduction:

a*2 = a<<1

Page 54: Chapter 5 Program Design and Analysis

Program Design-54

Dead code elimination Dead code:#define DEBUG 0if (DEBUG) dbg(p1);

Can be eliminated by analysisof control flow, constant folding

0

dbg(p1);

1

0

Page 55: Chapter 5 Program Design and Analysis

Program Design-55

Procedure inlining Eliminates procedure linkage overhead:

int foo(a,b,c) { return a + b - c;}z = foo(w,x,y);z = w + x + y;

May increase code size and extra cache activities

Page 56: Chapter 5 Program Design and Analysis

Program Design-56

Loop transformations Goals:

reduce loop overhead; increase opportunities for pipelining; improve memory system performance.

Page 57: Chapter 5 Program Design and Analysis

Program Design-57

Loop unrolling Reduces loop overhead, enables some

other optimizations.

for (i=0; i<4; i++)a[i] = b[i] * c[i];

for (i=0; i<2; i++) {a[i*2] = b[i*2] * c[i*2];a[i*2+1] = b[i*2+1] * c[i*2+1];

}

Page 58: Chapter 5 Program Design and Analysis

Program Design-58

Loop fusion and distribution Fusion combines two loops into 1:for (i=0; i<N; i++) a[i] = b[i] * 5;for (j=0; j<N; j++) w[j] = c[j] * d[j]; for (i=0; i<N; i++) {a[i] = b[i] * 5;

w[i] = c[i] * d[i];} Distribution breaks one loop into two. Changes optimizations within loop body.

Page 59: Chapter 5 Program Design and Analysis

Program Design-59

Loop tiling Changes order of accesses within array.

Changes cache behavior.

for (i=0; i<N; i++) for (j=0; j<N; j++)

c[i] = a[i,j]*b[i];

for (i=0; i<N; i+=2) for (j=0; j<N; j+=2) for (ii=0;ii<min(i+2,N);ii++) for (jj=0;jj<min(j+2,N);jj++)

c[ii] = a[ii,jj]*b[ii];

Page 60: Chapter 5 Program Design and Analysis

Program Design-60

Code motionfor (i=0; i<N*M; i++)z[i] = a[i] + b[i];

i<N*M

i=0;

z[i] = a[i] + b[i];

i = i+1;

N

Yi<X

i=0; X = N*M

Page 61: Chapter 5 Program Design and Analysis

Program Design-61

Induction variable elimination Induction variable: loop index. Consider loop:

for (i=0; i<N; i++)for (j=0; j<M; j++)

z[i][j] = b[i][j]; Rather than recompute i*M+j for each array in

each iteration, share induction variable between arrays, increment at end of loop body.

Page 62: Chapter 5 Program Design and Analysis

Program Design-62

Array conflicts in cache

a[0][0]

b[0][0]

main memory cache

1024 4099

...

1024

4099

Page 63: Chapter 5 Program Design and Analysis

Program Design-63

Array conflicts, cont’d. Array elements conflict because they are

in the same line, even if not mapped to same location.

Solutions: move one array; pad array.

a[0,0] a[0,1] a[0,2]

a[1,0] a[1,1] a[1,2]

before

a[0,0] a[0,1] a[0,2]

a[1,0] a[1,1] a[1,2]

after

a[0,2]

a[1,2]

Page 64: Chapter 5 Program Design and Analysis

Program Design-64

Register allocation Goals:

choose register to hold each variable; determine lifespan of variable in the register.

Basic case: within basic block.

Page 65: Chapter 5 Program Design and Analysis

Program Design-65

Register lifetime graphw = a + b;x = c + w;y = c + d;

a r0b r1c r2d r0w r3x r0y r3

time

a

b

cdwxy

1 2 3

t=1t=2t=3

c is livein interval

• spilling if not enough registers• graph coloring on conflict graph• operator rescheduling to improve

Page 66: Chapter 5 Program Design and Analysis

Program Design-66

Instruction scheduling Non-pipelined machines do not need instructi

on scheduling: any order of instructions that satisfies data dependencies runs equally fast.

In pipelined machines, execution time of one instruction depends on the nearby instructions: opcode, operands

Key: tracking resource utilization over time

Page 67: Chapter 5 Program Design and Analysis

Program Design-67

Reservation table A reservation table

relates instructions/timeto CPU resources

Time/instr A Binstr1 Xinstr2 X Xinstr3 Xinstr4 X

Page 68: Chapter 5 Program Design and Analysis

Program Design-68

Software pipelining Schedules instructions across loop

iterations. Reduces instruction latency in iteration i

by inserting instructions from iteration i-1. Example on SHARC:for (i=0; i<N; i++)

sum += a[i]*b[i]; Combine three iterations:

Fetch array elements a, b for iteration i. Multiply a, b for iteration i-1. Compute sum for iteration i-2.

Page 69: Chapter 5 Program Design and Analysis

Program Design-69

Software pipelining in SHARC/* first iteration performed outside loop */ai=a[0]; bi=b[0]; p=ai*bi;/* initiate loads used in second iteration; remainin

g loads will be performed inside loop */for (i=2; i<N-2; i++) {

ai=a[i]; bi=b[i]; /* fetch next cycle multiply */p = ai*bi; /* multiply for next iteration’s sum */sum += p; /* sum using p from last iteration */

}sum += p; p=ai*bi; sum +=p;

Page 70: Chapter 5 Program Design and Analysis

Program Design-70

Software pipelining timing

ai=a[i]; bi=b[i];

p = ai*bi;

sum += p;

ai=a[i]; bi=b[i];

p = ai*bi;

sum += p;

ai=a[i]; bi=b[i];

p = ai*bi;

sum += p;

time pipe

iteration i-2 iteration i-1 iteration i

Page 71: Chapter 5 Program Design and Analysis

Program Design-71

Instruction selection May be several ways to implement an

operation or sequence of operations. Template matching: represent operations

as graphs, match possible instruction sequences onto graph (e.g., using dynamic programming)

*

+

expression templates

* +

*

+

MULcost=1

ADDcost=1

MADDcost=1

Page 72: Chapter 5 Program Design and Analysis

Program Design-72

Using your compiler Understand various optimization levels (-

O1, -O2, etc.) Look at mixed compiler/assembler output. Modifying compiler output requires care:

correctness; loss of hand-tweaked code.

Page 73: Chapter 5 Program Design and Analysis

Program Design-73

Interpreters and JIT compilers Interpreter: translates and executes

program statements on-the-fly. JIT compiler: compiles small sections of

code into instructions during program execution. Eliminates some translation overhead. Often requires more memory.

Page 74: Chapter 5 Program Design and Analysis

Program Design-74

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs

for execution time, energy/power, program size Program validation and testing Design example: software modem

Page 75: Chapter 5 Program Design and Analysis

Program Design-75

Motivation Embedded systems must often meet

deadlines. Faster may not be fast enough.

Need to be able to analyze execution time. Worst-case, not typical.

Need techniques for reliably improving execution time.

Page 76: Chapter 5 Program Design and Analysis

Program Design-76

Run times will vary Program execution times depend on

several factors: Input data values. State of the instruction, data caches. Pipelining effects.

Page 77: Chapter 5 Program Design and Analysis

Program Design-77

Measuring program speed CPU simulator.

I/O may be hard. May not be totally accurate.

Hardware timer. Connected to microprocessor bus to measure

timing of code Requires board, instrumented program.

Logic analyzer. Limited logic analyzer memory depth.

Page 78: Chapter 5 Program Design and Analysis

Program Design-78

Program performance metrics Average-case:

For typical data values, whatever they are. Worst-case:

For any possible input set. Best-case:

For any possible input set. What values create worst/average/best

case? analysis; experimentation.

Concerns: operations; program paths.

Page 79: Chapter 5 Program Design and Analysis

Program Design-79

Performance analysis Elements of program performance (Shaw):

execution time = program path + instruction timing

Path depends on data values. Choose which case you are interested in.

Instruction timing depends on pipelining, cache behavior.

Page 80: Chapter 5 Program Design and Analysis

Program Design-80

Track program paths Consider for loop:for (i=0, f=0, i<N; i++)

f = f + c[i]*x[i]; Loop initiation block

executed once. Loop test executed

N+1 times. Loop body and

variable update executed N times.

For nest-if: need to enumerate all paths

i<N

i=0; f=0;

f = f + c[i]*x[i];

i = i+1;

N

Y

Page 81: Chapter 5 Program Design and Analysis

Program Design-81

Measure instruction timing Not all instructions take the same amount

of time. Hard to get execution time data for

instructions. Instruction execution times are not

independent. Execution time may depend on operand

values.

Page 82: Chapter 5 Program Design and Analysis

Program Design-82

Trace-driven performance analysis Trace: a record of the execution path of a

program. Trace gives execution path for

performance analysis. A useful trace:

requires proper input values; is large (gigabytes).

Page 83: Chapter 5 Program Design and Analysis

Program Design-83

Trace generation Hardware capture:

logic analyzer; hardware assist in CPU.

Software: PC sampling. Instrumentation instructions. Simulation.

Page 84: Chapter 5 Program Design and Analysis

Program Design-84

Performance optimization hints Use registers efficiently. Use page mode memory accesses. Analyze cache behavior:

instruction conflicts can be handled by rewriting code, rescheudling;

conflicting scalar data can easily be moved; conflicting array data can be moved, padded.

Page 85: Chapter 5 Program Design and Analysis

Program Design-85

Energy/power optimization Energy: ability to do work.

Most important in battery-powered systems. Power: energy per unit time.

Important even in wall-plug systems---power becomes heat.

Page 86: Chapter 5 Program Design and Analysis

Program Design-86

Measuring energy consumption Execute a small loop, measure current:

while (TRUE)a();

I

CPU

Page 87: Chapter 5 Program Design and Analysis

Program Design-87

Sources of energy consumption Relative energy per operation (Catthoor et al):

memory transfer: 33 external I/O: 10 SRAM write: 9 SRAM read: 4.4 multiply: 3.6 add: 1

Focus on memory for energy reduction

Page 88: Chapter 5 Program Design and Analysis

Program Design-88

Cache behavior is important Cache (SRAM) uses more power than

DRAM Energy consumption has a sweet spot as

cache size changes: cache too small: program thrashes, burning

energy on external memory accesses; cache too large: cache itself burns too much

power. Need to choose a proper size

Page 89: Chapter 5 Program Design and Analysis

Program Design-89

Optimizing programs for energy First-order optimization:

high performance = low energy. Optimize memory access patterns! Use registers efficiently. Identify and eliminate cache conflicts. Moderate loop unrolling eliminates some loop

overhead instructions. Eliminate pipeline stalls (e.g., software pipelin

e). Inlining procedures may help: reduces linkage,

but may increase cache thrashing.

Page 90: Chapter 5 Program Design and Analysis

Program Design-90

Optimizing for program size Goal:

reduce hardware cost of memory; reduce power consumption of memory units.

Reduce data size: Reuse constants, variables, data buffers in different

parts of code. Requires careful verification of correctness.

Generate data using instructions. Reduce code size:

Avoid function inlining. Choose CPU with compact instructions. Use specialized instructions where possible.

Page 91: Chapter 5 Program Design and Analysis

Program Design-91

Code compression Use statistical compression to reduce code

size, decompress on-the-fly:

CPUdeco

mpr

esso

r table

cache

mainmemory

0101101

0101101LDR r0,[r4]

Page 92: Chapter 5 Program Design and Analysis

Program Design-92

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing Design example: software modem

Page 93: Chapter 5 Program Design and Analysis

Program Design-93

Goals Make sure software works as intended.

We will concentrate on functional testing---performance testing is harder.

What tests are required to adequately test the program? What is “adequate”?

Page 94: Chapter 5 Program Design and Analysis

Program Design-94

Testing basics Basic procedure:

Provide the program with inputs. Execute the program. Compare the outputs to expected results.

Types of software testing: Black-box: tests are generated without

knowledge of program internals. Clear-box (white-box): tests are generated

from the program structure.

Page 95: Chapter 5 Program Design and Analysis

Program Design-95

Clear-box testing Generate tests based on the structure of

the program. Is a given block of code executed when we

think it should be executed? Does a variable receive the value we think it

should get?

Page 96: Chapter 5 Program Design and Analysis

Program Design-96

Controllability and observability Controllability: must be able to cause a particu

lar internal condition to occur. Observability: must be able to see the effects o

f a state from the outside.

for (firout = 0.0, j =0; j < N; j++)firout += buff[j] * c[j];

if (firout > 100.0) firout = 100.0;if (firout < -100.0) firout = -100.0;

Controllability: to test range checks for firout, must first load circular buffer with suitable values

Observability: how to observe values of buff, firout?

Page 97: Chapter 5 Program Design and Analysis

Program Design-97

Choosing tests to performPath-based testing: Clear-box testing generally tests selected

program paths: control program to exercise a path; observe program to determine if path was

properly executed. May look at whether location on path was

reached (control), whether variable on path was set (data).

Several ways to look at control coverage, to discussed next ...

Page 98: Chapter 5 Program Design and Analysis

Program Design-98

Example: choosing paths Two possible criteria for selecting a set of

paths: Execute every statement at least once. Execute every direction of a branch at least

once.

Covers allstatements

+/+ Covers allbranches

Page 99: Chapter 5 Program Design and Analysis

Program Design-99

Find basis paths How many distinct paths are in a program? An undirected graph has a basis set of edges:

a linear combination of basis edges (xor together sets of edges) gives any possible subset of edges in the graph.

If we can cover all basis paths, the control flow is considered adequately covered

CDFG is directed, so basis set is approximation

Page 100: Chapter 5 Program Design and Analysis

Program Design-100

Basis set example

a b

c

ed

a b c d ea 0 0 1 0 0b 0 0 1 0 1c 1 1 0 1 0d 0 0 1 0 1e 0 1 0 1 0

incidence matrix

a 1 0 0 0 0b 0 1 0 0 0c 0 0 1 0 0d 0 0 0 1 0e 0 0 0 0 1

basis set

Page 101: Chapter 5 Program Design and Analysis

Program Design-101

Cyclomatic complexity Provides an upper bound on the control compl

exity of a program (size of basis set): e = # edges in control graph; n = # nodes in control graph; p = # graph components.

Cyclomatic complexity: M = e - n + 2p. Structured program: # binary decisions + 1.

Page 102: Chapter 5 Program Design and Analysis

Program Design-102

Branch testing strategy Exercise the elements of a conditional, not just

one true and one false case. Devise a test for every simple condition in a Bo

olean expression. Example: meant to write

if (a || (b >= c)) { printf(“OK\n”); } Actually wrote:

if (a && (b >= c)) { printf(“OK\n”); } Branch testing strategy:

One test for a=F, (b >= c) = T: a=0, b=3, c=2. Produces different answers.

Page 103: Chapter 5 Program Design and Analysis

Program Design-103

Domain testing Concentrates on linear inequalities. Example: j <= i + 1.

Test two cases on boundary, one outside boundary.

correct incorrect

j

i

i=3,j=5

i=4,j=5

i=1,j=2

Page 104: Chapter 5 Program Design and Analysis

Program Design-104

Data flow testing Def-use analysis: match variable definitions (a

ssignments) and uses. Example:

x = 5;…

if (x > 0) ... Does assignment get to the use?

Choose tests that exercise chosen def-use pairs Set value at def and observe use to check the path

(or flow)

def

p-use

Page 105: Chapter 5 Program Design and Analysis

Program Design-105

Loop testing Common, specialized structure---

specialized tests can help. Useful test cases:

skip loop entirely; one iteration; two iterations; mid-range of iterations; n-1, n, n+1 iterations.

Page 106: Chapter 5 Program Design and Analysis

Program Design-106

Black-box testing Black-box tests are made from the

specifications, not the code. Black-box testing complements clear-box.

May test unusual cases better. Types of tests:

Specified inputs/outputs: select inputs from spec, determine required outputs.

Random: generate random tests, determine appropriate output.

Regression: tests used in previous versions of system.

Page 107: Chapter 5 Program Design and Analysis

Program Design-107

Evaluating tests How good are your tests?

Keep track of bugs found, compare to historical trends.

Error injection: add bugs to copy of code, run tests on modified code.

Page 108: Chapter 5 Program Design and Analysis

Program Design-108

Outline Program design Models of programs Assembly and linking Basic compilation techniques Analysis and optimization of programs Program validation and testing Design example: software modem

Page 109: Chapter 5 Program Design and Analysis

Program Design-109

Theory of operation Frequency-shift keying:

separate frequencies for 0 and 1.

time

0 1

Page 110: Chapter 5 Program Design and Analysis

Program Design-110

FSK encoding Generate waveforms based on current bit:

bit-controlledwaveformgenerator

0110101

Page 111: Chapter 5 Program Design and Analysis

Program Design-111

FSK decoding

A/D

con

vert

er zero filter

one filter

detector 0 bit

detector 1 bit

Page 112: Chapter 5 Program Design and Analysis

Program Design-112

Transmission scheme Send data in 8-bit bytes. Arbitrary spacing

between bytes. Byte starts with 0 start bit. Receiver measures length of start bit to

synchronize itself to remaining 8 bits.

start (0) bit 1 bit 2 bit 3 bit 8...

Page 113: Chapter 5 Program Design and Analysis

Program Design-113

RequirementsInputs Analog sound input, reset button.

Outputs Analog sound output, LED bit display.

Functions Transmitter: Sends data from memoryin 8-bit bytes plus start bit.Receiver: Automatically detects bytesand reads bits. Displays current bit onLED.

Performance 1200 baud.

Manufacturing cost Dominated by microprocessor andanalog I/O

Power Powered by AC.

Physicalsize/weight

Small desktop object.

Page 114: Chapter 5 Program Design and Analysis

Program Design-114

Specification

Line-in*

input()

Receiver

sample-in()bit-out()

1 1

Transmitter

bit-in()sample-out()

Line-out*

output()

1 1

Page 115: Chapter 5 Program Design and Analysis

Program Design-115

System architecture Interrupt handlers for samples:

input and output. Transmitter. Receiver.

Page 116: Chapter 5 Program Design and Analysis

Program Design-116

Transmitter Waveform generation by table lookup.

float sine_wave[N_SAMP] = { 0.0, 0.5, 0.866, 1, 0.866, 0.5, 0.0, -0.5, -0.866, -1.0, -0.866, -0.5, 0};

time

Page 117: Chapter 5 Program Design and Analysis

Program Design-117

Receiver Filters (FIR for simplicity) use circular

buffers to hold data. Timer measures bit length. State machine recognizes start bits, data

bits.

Page 118: Chapter 5 Program Design and Analysis

Program Design-118

Hardware platform CPU. A/D converter. D/A converter. Timer.

Page 119: Chapter 5 Program Design and Analysis

Program Design-119

Component design and testing Easy to test transmitter and receiver on

host. Transmitter can be verified with speaker

outputs. Receiver verification tasks:

start bit recognition; data bit recognition.

Page 120: Chapter 5 Program Design and Analysis

Program Design-120

System integration and testing Use loopback mode to test components again

st each other. Loopback in software or by connecting D/A and A/D

converters.