Docsity

SR-sequences in Message-Passing Programs: Object-Based and Thread-Based Approaches - Prof., Study notes of Computer Science

Toggle sidebar
bg1
5.5 Tracing, Testing, and Replay for Message Passing Programs
Outline:
SYN-sequences for channel-based programs
Tracing, replaying, and checking the feasibility of SYN-sequences.
Reachability testing for programs that use the channel classes.
5.5.1 SR-sequences
Object-based SYN-sequence: there is one SYN-sequence for each synchronization object
in the program.
Thread-based SYN-sequence: one SYN-sequence for each thread.
5.5.1.1 Object-Based SR-sequences.
Let CP be a concurrent program that uses channels. Assume for now that CP has no
selective wait statements.
The synchronization objects in CP are its channels and the synchronization events in CP
are executions of send() and receive() operations on these channels.
A SYN-sequence for a channel is a sequence of send and receive events (or “SR-
events”) of the following types:
SRsynchronization: a synchronization between send and receive operations on a
synchronous channel
asynchronousSendArrival: an arrival of an asynchronous send operation whose
message is eventually received
asynchronousReceive: a receive operation that eventually receives a message on an
asynchronous channel
unacceptedSend: a synchronous send operation whose message is never received
unacceptedAsynchSend: an asynchronous send operation whose message is never
received
unacceptedReceive: a receive on an asynchronous or synchronous channel that never
receives a message
sendException: a send operation on an asynchronous or synchronous channel that
causes an exception to be thrown
receiveException: a receive operation on an asynchronous or synchronous channel that
causes an exception to be thrown
startEntry: the start of a rendezvous on an entry
endEntry: the end of a rendezvous on an entry
Notice that for asynchronous messages, there are arrival events but no send events. This
is because it is the order in which messages arrive that determines the result of an
execution, not the order in which messages are sent.
An object-based SR-event for channel C is denoted by
(S, R, NS, NR, eventType)
where:
S is the sending thread of a send operation or the calling thread of an entry call.
R is the receiving thread of a receive operation or the accepting thread of an entry call.
NS is the sender’s order number, which gives the relative order of this event among all
of the sending/calling thread’s events.
NR is the receiver’s order number, which gives the relative order of this event among
all of the receiving/accepting thread’s events.
eventType is one of the send-receive event types, which are listed above.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download SR-sequences in Message-Passing Programs: Object-Based and Thread-Based Approaches - Prof. and more Study notes Computer Science in PDF only on Docsity!

5.5 Tracing, Testing, and Replay for Message Passing Programs Outline:^

SYN-sequences for channel-based programs

Tracing, replaying, and checking the feasibility of SYN-sequences.

Reachability testing for programs that use the

channel

classes.

5.5.1 SR-sequences Object-based SYN-sequence: there is one SYN-sequence for each synchronization objectin the program.Thread-based SYN-sequence: one SYN-sequence for each thread. 5.5.1.1 Object-Based SR-sequences

Let CP be a concurrent program that uses

channels

. Assume for now that CP has no

selective wait statements.The synchronization objects in CP are its channels and the synchronization events in CPare executions of

send()

and

receive()

operations on these channels.

A SYN-sequence for a channel is a sequence of send and receive events (or “SR-events”) of the following types:^

SRsynchronization: a synchronization between send and receive operations on asynchronous channel

asynchronousSendArrival: an arrival of an asynchronous send operation whosemessage is eventually received

asynchronousReceive: a receive operation that eventually receives a message on anasynchronous channel

unacceptedSend: a synchronous send operation whose message is never received

unacceptedAsynchSend: an asynchronous send operation whose message is neverreceived

unacceptedReceive: a receive on an asynchronous or synchronous channel that neverreceives a message

sendException: a send operation on an asynchronous or synchronous channel thatcauses an exception to be thrown

receiveException: a receive operation on an asynchronous or synchronous channel thatcauses an exception to be thrown^

startEntry: the start of a rendezvous on an entry

endEntry: the end of a rendezvous on an entry

Notice that for asynchronous messages, there are

arrival

events but no

send

events. This

is because it is the order in which messages arrive that determines the result of anexecution, not the order in which messages are sent.An object-based SR-event for channel C is denoted by

(S, R, N

, NS

, eventType)R

where:^

S is the sending thread of a send operation or the calling thread of an entry call.

R is the receiving thread of a receive operation or the accepting thread of an entry call.

N

S^

is the sender’s order number, which gives the relative order of this event among all of the sending/calling thread’s events.

N

R^

is the receiver’s order number, which gives the relative order of this event among all of the receiving/accepting thread’s events.

eventType is one of the send-receive event types, which are listed above.

In addition:^

The order number of an event executed by thread

T

gives the relative order of this

event among all the events exercised by

T

. For example, an event

T

with order number

2 is the second event exercised by

T

For

asynchronousSendArrival, unacceptedSend

and

sendException

events,

R

and

N

R

are not applicable (NA) since no receiving thread is involved with these events.

For

unacceptedReceive

and

receiveException

events,

S

and

N

S^

are not applicable since

there is no sending thread.

The order number of an

asynchronousSendArrival

event is for the send operation of

the thread that generated it. The program in Listing 5.15 shows why order numbers are needed in SR-events.

mailbox C1, C2; // synchronous mailboxes

Thread

Thread

Thread

C1.send();

C1.receive();

C2.receive();

C2.send();

Listing 5.15 Demonstrating the need for order numbers.

Assume that order numbers are not specified.^

The only feasible SR-sequence for channel

C

(without order numbers) contains the

single event:

(Thread1, Thread2, SRsynchronization).

The only feasible SR-sequence for

C

also contains a single event:

(Thread1, Thread3, SRsynchronization).

Suppose we reverse the order of the send operations in

Thread

Thread

Thread

Thread

C2.send()

;^

C1.receive();

C2.receive();

C1.send()

Then the feasible SR-sequences (without order numbers) for channels

C

and

C

in the

new program are the same as in the original program, even though the two programs aredifferent in an obvious and important way.Adding order numbers to the SR-events distinguishes between these two differentprograms. The SR-sequence for

C

in the first program becomes

st^

event of Thread1 and 1

st^

event of Thread

(Thread1, Thread2, 1, 1, SRsynchronization). and the SR-sequence for

C

in the second program becomes

nd

event of Thread1 and 1

st^

event of Thread

(Thread1, Thread2, 2, 1, SRsynchronization). When order numbers are specified, an SR-sequence of

C

that is feasible for the first

program will be infeasible for the second, and vice versa.Given the above format for an object-based SR-event, an SR-sequence of channel

C

is

denoted as

C: ((S

, R 1

, N 1

S

, N

R

, eventType

), (S 1

, R 2

, N 2

S

, N

R

, eventType

where (S

, Ri

, Ni

Si

, N

Ri

, eventType

) denotes the ith, i>0, SR-event in the SR-sequence ofi

C

An object-based SR-sequence of a program contains an SR-sequence for each channel inthe program.

The list of thread-based SR-event types is the same as that above for object-based events,except that we add an

elseDelay

event type for selective wait statements:

elseDelay: selection of an else or delay alternative. In addition:^

An

SRsynchronization

event representing a synchronization between send and receive

operations on a synchronous channel appears in the SR-sequence for the owningthread (i.e. the receiving thread). There is no corresponding send event in the sequencefor the sending thread.

In Format 2, no order number is required for channel

C

since the SR-sequence of the

owning thread

T

contains all the events for channel

C

and thus the order numbers for

events on

C

are given implicitly (i.e., the

i

th event for

C

implicitly has order number

i .) For Format 1, an SR-sequence of thread

T

is denoted as:

T: ((C

, V 1

C

, eventType

), (C 1

V

C

, eventType

For Format 2, an SR-sequence of thread T is denoted as:

T: (S

, N 1

S

, C

, eventType 1

(S

, N 2

S

, C

, eventType 2

A thread-based SR-sequence of a program contains a thread-based SR-sequence for eachthread in the program.We can easily translate a thread-based SR-sequence into an object-based SR-sequence,and vice versa.

Example: Consider again the simple program in Listing 5.15.

mailbox C1, C2; // synchronous mailboxesThread

Thread

Thread

C1.send(); C1.receive(); C2.receive();C2.send();Listing 5.15 Demonstrating the need for order numbers.

The object-based SR-sequence of this program is

C

: (Thread1, Thread2, 1, 1, SRsynchronization) 1 C

: (Thread1, Thread3, 2, 1, SRsynchronization). 2

The corresponding thread-based SR-sequence using Format 1 is:

Thread

: (C1, 1, send), (C2, 1, send) 1

Thread

: (C1, 1, receive) 2

Thread

: (C2, 1, receive). 3

The thread-based SR-sequence using Format 2 is:

Thread1: (empty)

// send events do not appear in the SR-sequence of the sender

Thread2: (Thread1,

^1

, C1, SRsynchronization)

// 1 is Thread1’s order number for its first

send

operation

Thread3: (Thread1,

^2

, C2, SRsynchronization).

// 2 is Thread1’s order number for its second

send

operation

Thread1’s sequence is empty under Format 2, since Thread1 only executes send eventsand these send events appear as

SRsynchronization

events in Thread2 and Thread3.

5.5.1.3 Totally-Ordered SR-sequences

The object- and thread-based SR-sequences defined above are all partially-ordered --there is a separate sequence for each channel or thread in the program.Ina totally-ordered SR-sequence there is only one SR-sequence for the program and thissequence includes events for all the channels and threads.An SR-event in a totally-ordered SR-sequence is denoted by

(S, R, C, eventType) where:^

S is the sending thread of a send operation or the calling thread of an entry call.

R is the receiving thread of a receive operation or the accepting thread of an entry call.

C is the channel name.

eventType is one of the types listed above for Format 2. A totally-ordered SR-sequence contains no order numbers for channels or threads, sincethis information is specified implicitly by the ordering of events in the sequence.A totally-ordered SR-sequence of program CP is denoted as: ((S

R

C

eventType

(S

, R 1

, C 2

, eventType 2

For example, a totally-ordered SR-sequence for the program in Listing 5.15 is

(^

(Thread

, Thread 1

, C 2

, SRsynchronization), 1

(Thread

, Thread 1

, C 3

, SRsynchronization) ). 2

A totally-ordered SR-sequence can be translated into a partially-ordered sequence, andvice versa^

A totally-ordered sequence is usually easier to understand than a partially-ordered one.

If a program is running on a single CPU, a single controller can easily record a totally-ordered sequence.

However, using a single controller creates a bottleneck during execution.

Since threads and channels are user-level objects in the program, they can beinstrumented to collect either thread-based or object-based sequences. In a distributed program (which involves multiple machines):^

a channel is typically not a user-level object, so object-based sequences may bedifficult to collect.

If multiple controllers (e.g., one controller per machine) are used to collect partially-ordered SR-sequences, then a bottleneck is avoided, but the trace files will be createdon separate machines across the network.

If a single controller is used to collect a totally-ordered SR-sequence from adistributed program, then certain event-ordering issues must be addressed so that theSR-sequence is accurately captured (see Section 6.3).

mailbox C1, C2; // synchronous mailboxesThread

Thread

Thread

C1.send(); C1.receive(); C2.receive();C2.send();Listing 5.15 Demonstrating the need for order numbers.

For the program in Listing 5.15, the object-based, simple SR-sequence of this program is

C1: (Thread1, Thread2)C2: (Thread2, Thread3) The thread-based, simple SR-sequence using Format 1 is

Thread1: (1), (1)Thread2: (1)Thread3: (1) Let Thread2 be the owner of channel

C

and Thread

be the owner of channel

C

, then

the thread-based simple SR-sequence using Format 2 is

Thread2: (Thread1)Thread3: (Thread1). The thread-based, totally-ordered simple SR-sequence is

(Thread1, Thread2), (Thread1, Thread3).

5.5.3 Determining the Feasibility of an SR-sequence Before a thread can perform a send or receive operation, it must request permission froma control module. The controller is responsible for reading an SR-sequence and forcingthe execution to proceed according to this sequence.The

send()

and

receive()

methods in the

channel

classes are modified by adding one or

more calls to the controller. Listing 5.16 shows a sketch of Java class

link

with the

modifications highlighted.In trace mode:^

Methods

send()

and

receive()

notify the controller of any exceptions that occur.

Operation

receive()

notifies the controller when a message is received, which causes

an

SRsynchronization

event to be recorded. The arguments on the calls refer to the

fields of an SR-event, which were defined in the previous section. (sender (S),receiver (R), order number of the sender (N

), order number of the receiver (NS

),R

channel name (C), order number of the channel (N

), and the various event types).C

In test mode,^

Methods

send()

and

receive()

issue a call to the controller to request permission to

exercise send and receive events.

The call to

control.msgReceived()

notifies the controller that the event has occurred,

so that permission can be given for the next event. The controller:^

Inputs an SR-sequence and attempts to force the SR-sequence to be exercised.

If the next event in the SR-sequence indicates that T is expected to request a certainpermit, the controller will wait for T to issue an entry call to the appropriate channel.

If no call arrives before a timeout expires, then the sequence is assumed to beinfeasible.

public class link extends channel {

...public final void send(Object sentMsg) {

if (mode == TEST)

control.requestSendPermit(S,N

,C);S

synchronized(sending) {

// save first Thread to call sendif (sender == null) sender = Thread.currentThread();if (Thread.currentThread() != sender) {

if (mode == TRACE)

control.traceMsg(S, N

, C, VS

, sendException);C

if (mode == TEST) {

control.requestSendExceptionPermit(S,N

, C);S

control.msgReceived(); } throw new InvalidLinkUsage

("Attempted to use link with multiple senders");

} if (sentMsg == null) {

if (mode == TRACE)

control.traceMsg(S, N

, C, VS

, sendException);C

if (mode == TEST) {

control.requestSendExceptionPermit(S, N

, C);S

control.msgReceived(); } throw new NullPointerException("Null message passed to send()"); } message = sentMsg;sent.V();received.P(); } } Listing 5.16 Class l

ink

modified for tracing and feasibility.

public final Object receive() {

Object receivedMessage = null; if (mode == TEST) control.requestReceivePermit(R,N

,C);R^

synchronized (receiving) {

// save first Thread to call receiveif (receiver == null) receiver = Thread.currentThread();f (Thread.currentThread() != receiver) {

if (mode == TRACE)

control.traceMsg(R,N

,C,VR^

,receiveException);C

if (mode == TEST) {

control.requestReceiveExceptionPermit(R, N

, C);R^

control.msgReceived(); } throw new InvalidLinkUsage

("Attempted to use link with multiple receivers");

} sent.P();receivedMessage = message; if (mode == TRACE) control.traceMsg

(S, R, N

, NS

, C, VC, SR_SYNCHRONIZATION);R^

else if (mode == TEST) control.msgReceived(); received.V();return receivedMessage; } } } Listing 5.16 (continued) Class l

ink

modified for tracing and feasibility.

Thread

Thread

send

Thread

receive

send

receive

Thread

Thread

send

Thread

send

receive

(a)

(b)

Figure 5.18 Execution trace and a race variant.

Note that the race variant in Fig. 5.18b contains only one receive event for Thread2. Wecannot include a second receive event for Thread2 since we cannot be sure that Thread2will execute another receive statement after the variant is replayed.Suppose that Thread2 executes the following statements:

x = p2.receive();if (x)

p2.receive();

In the execution trace in Fig. 5.18a, Thread2 executes both receive events. Thus, we cansay with certainty that the value

x

received from Thread2 caused the condition in

Thread2’s if-statement to be evaluated to

true

In the race variant in Fig. 5.18b:^

We changed the sending thread for Thread2’s first receive to Thread3.

The value Thread2 receives from Thread3 may or may not cause the condition inThread2’s if-statement to be evaluated to

true

Since we generate variants without examining the source code of the program, we canno longer be sure what will happen after Thread

’s first receive. 2

To be safe, we remove from the variant all events that happen after the changedreceive event.

The variants of a trace depend on the type of synchronization provided by the channels.Fig. 5.19a shows a program that uses asynchronous ports. An execution trace of thisprogram is given in Fig. 5.19b.

Thread

Thread

Thread

p3.send(m1);

x = p2.receive(); y = p3.receive();

p2.send(m2);

p3.send(x);

z = p3.receive();

Thread

Thread

s

Thread

r

s

r

(a)

s

r

Thread

Thread

s

Thread

r

r2^ s

s

r

(b)

(c)

Figure 5.19 Execution trace and a variant for asynchronous ports with FIFOsynchronization.Assume that the ports used in the program of Fig. 5.19a are the asynchronous portsimplemented in Section 5.1:^

Thread1’s send to Thread3 definitely happens before Thread2’s send to Thread3 sinceThread1 and Thread2 synchronize in between sending their messages to Thread3.

Based on this “happened before” relation, the port implementation used in this chapterguarantees that Thread3 will receive the message sent by Thread1 before it receivesthe message sent by Thread2. In general: if a message m

1

is sent to thread T before another message m

2

is sent to T,

then message m

1

is received by T before m

2

is received.

This type of synchronization is called

causal synchronization

. If causal synchronization is

used, no race variants can be generated for the trace in Fig. 5.19b.Formal definitions of the “happens before” relation and causality are given in Chapter 6.

Causal synchronization is not always guaranteed.

FIFO synchronization

is commonly

used in distributed programs, which send message across a communications network.FIFO synchronization guarantees that messages sent from one thread to another threadare received in the order that they are sent.

Thread

Thread

Thread

p3.send(m1);

x = p2.receive(); y = p3.receive();

p2.send(m2);

p3.send(x);

z = p3.receive();

Thread

Thread

s

Thread

r

s

r

(a)

s

r

Thread

Thread

s

Thread

r

r2^ s

s

r

(b)

(c)

Figure 5.19 Execution trace and a variant for asynchronous ports with FIFOsynchronization.Fig. 5.19c shows a variant of the trace in Fig. 5.19b.^

This variant assumes that FIFO synchronization is used, which allows Thread3 toreceive messages from Thread1 and Thread2 in the reverse order that they were sent.

FIFO synchronization guarantees that Thread3 receives Thread1’s messages in theorder that Thread1 sends them, and that Thread3 receives Thread2’s messages in theorder that Thread2 sends them, but the first message Thread3 receives can be fromThread1 or from Thread2. Distributed programming is discussed in Chapter 6. To prepare for Chapter 6, we haveimplemented a special asynchronous channel port with FIFO synchronization and supportfor reachability testing.^

These FIFO ports make it possible to write programs that behave like distributedprograms but run on a single computer and so are easier to write and easier to debug

Writing such a program is a good first step towards writing a real distributed program.

Table 5.1 shows the results of applying reachability testing to programs that use messagepassing with selective waits.^

Program BB is the bounded buffer program in Listing 5.10.

Program RW is the readers and writers program in Listing 5.11.

Program

Config.

#Seqs.

Program

Config.

#Seqs.

BB-Select

3P+3C+2S

RW-Select

3R+2W

Table 5-1 Reachability testing results for message-passing programs

These programs, which use selective waits, generate far fewer sequences than thecorresponding bounded buffer and reader and writer programs in Chapters 3 and 4 thatused semaphores and monitors.

A simple SR-sequence is replayed by specifying

replay

mode and using the same values

for the

strategy

and

controllers

that were used during tracing:

java –Dmode=replay –Dstrategy=thread –Dcontrollers=single Buffer As in Chapters 3 and 4, reachability testing is performed on

Buffer

by setting the

mode

property to

rt

and executing a driver process named

RTDriver

with “Buffer” as the

command-line argument:

java –Dmode=rt RTDriver Buffer The feasibility of an SR-sequence is determined in

test

mode:

java –Dmode=test –Dstrategy=thread –Dcontrollers=single Buffer If the mode is not specified, the default value for the

mode

property will turn tracing,

replay, and testing off.The default value for the strategy property is

object

and the default value for the

controllers

property is

single

5.5.6.2 Using the C++

channel

Classes

In Windows/DOS, an execution of a C++ version of program

Buffer

in Listing 5.20 is

traced by executing the command:

set MODE=TRACE

// Unix: setenv MODE TRACE

and then specifying whether the tracing strategy should be object-based or thread-based:

set STRATEGY=OBJECT // collect object-based SR-sequences; Unix: setenv STRATEGY OBJECT

set STRATEGY=THREAD // collect thread-based SR-sequences; Unix: setenv STRATEGY THREADand whether there should be a single Controller or multiple Controllers:

set CONTROLLERS=SINGLE

// single Controller thread;

Unix: setenv

CONTROLLERS SINGLE

set CONTROLLERS=MULTIPLE // multiple Controller threads; Unix: setenv CONTROLLERS MULTIPLEUsing a single Controller results in a totally-ordered (object-based or thread-based) SR-sequence.If multiple Controllers are used, the collected SR-sequence will be partially-ordered (oneper thread or object).If the program contains a

selectiveWait

, then the tracing strategy must be

thread-based.

When a thread-based strategy is used, the owner of each link, port, or entry P must beindicated by calling P.setOwner(T), where thread T is the owner of port P. An example ofthis is shown in the

main()

method in listing 5.20.

public final class Buffer {

public static void main (String args[]) {

selectableEntry deposit = new selectableEntry();selectableEntry withdraw = new selectableEntry();boundedBuffer b = new boundedBuffer(deposit,withdraw,3);Producer pl = new Producer (deposit, 1);Consumer cl = new Consumer (withdraw, 1); deposit.setOwner(b); withdraw.setOwner(b); b.setDaemon(true);b.start(); cl.start(); pl.start();try{cl.join(); pl.join();}catch (InterruptedException e) {System.exit(1);} } } Part of Figure 5.20.Tracing an execution with a THREAD strategy and a SINGLE controller creates severaltrace files for

Buffer

file channel-replay.txt contains a totally-ordered simple SR-sequence of the execution

file channel-test.txt, contains a totally-ordered complete SR-sequence. Using MULTIPLE controllers creates files that contain a separate simple SR-sequenceand complete SR-sequence for each of the threads.Random delays can be executed during trace mode using “set RANDOMDELAY=ON.”When ON is specified, random delays are executed before each

send()

and

receive()

. To

turn random delays off, use OFF; this is also the default value. The value of variableRANDOMDELAY is ignored during replay and test modes.To turn deadlock detection on during tracing or reachability testing, execute” setDEADLOCKDETECTION=ON.” To turn deadlock detection off, use OFF; this is alsothe default value.

A simple SR-sequence is replayed by executing “set MODE=REPLAY”, and using thesame values for the STRATEGY and the CONTROLLERS variables that were usedduring tracing.As in Chapter 3, reachability testing is performed on

Buffer

by setting the MODE to RT

and customizing the driver process in file

RTDriver

.cpp. This file is part of the

synchronization library. Directions for customizing the driver process are in the file.The feasibility of an SR-sequence is determined in TEST

mode: set MODE TEST. If the

mode is not specified, the default value for the MODE variable will turn tracing, replay,and testing off.The default value for the STRATEGY variable is OBJECT and the default value for theCONTROLLERS variable is SINGLE.