forked from ashishjohn1908/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldRT.java
More file actions
159 lines (116 loc) · 6.52 KB
/
Copy pathHelloWorldRT.java
File metadata and controls
159 lines (116 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. */
import javax.realtime.*;
import java.io.*;
public class HelloWorldRT {
// Global scope constants
static final int ITERATIONS = 10000; // Number of iterations of the periodic thread
static final int PERIOD = 2; // Period in milliseconds
// A periodic real-time thread class measuring latencies in thread rescheduling times
static class PeriodicThread extends RealtimeThread {
Clock refRTClock;
RelativeTime usedPeriod;
AbsoluteTime expectedStartTime;
StringWriter noticeableLatencyRecordingWriter; // A noticeable latency is at least 100 microseconds
StringWriter highestLatencyWriter;
PeriodicThread(Clock rtClock,
RelativeTime period,
AbsoluteTime startTime,
StringWriter noticeableLatencyWriter,
StringWriter worstLatencyWriter) {
refRTClock = rtClock;
usedPeriod = period;
expectedStartTime = startTime;
noticeableLatencyRecordingWriter = noticeableLatencyWriter;
highestLatencyWriter = worstLatencyWriter;
}
// Refined RealtimeThread.run() method: for each loop,
// from real-time clock we collect first the activation time of the real-time thread,
// compute the expected activation time by adding the period to previous expected time,
// then compute the latency in activation using the difference between those two times.
public void run() {
AbsoluteTime expectedActivationTime =
new AbsoluteTime(expectedStartTime); // For the first loop, expected activation time is start time
AbsoluteTime activeTime = new AbsoluteTime(); // Measured rescheduling time
RelativeTime loopLatency = new RelativeTime(); // Calculated latency of the current loop
RelativeTime worstLatency = new RelativeTime(); // Worst latency observed so far
int nanos = 0;
long millis = 0;
for (int i = 0; i < ITERATIONS; i++) {
try {
if ( !waitForNextPeriod() ) {
throw new Exception(" Deadline missed at loop " + i);
}
// Records entry time in period
refRTClock.getTime(activeTime);
// Calculates expected activation time for this loop
expectedActivationTime.add(usedPeriod, expectedActivationTime);
// Calculates activation time latency for this loop
activeTime.subtract(expectedActivationTime, loopLatency);
// Records unexpected latencies (starting after first loop)
if (i > 0) {
nanos = loopLatency.getNanoseconds();
millis = loopLatency.getMilliseconds();
// If latency higher than 100 microseconds, record it
if ( nanos >= 100000
|| millis > 0) {
noticeableLatencyRecordingWriter.write(
"\n Loop " + (i+1) +
": Rescheduling time = " + activeTime +
"; Noticeable latency = " + loopLatency);
}
// If latency exceeds the worst case so far, record it
if ( millis > worstLatency.getMilliseconds()
|| (millis == worstLatency.getMilliseconds()
&& nanos > worstLatency.getNanoseconds()) ) {
highestLatencyWriter.write("\n New highest latency at loop " + (i+1) + " = " + loopLatency);
worstLatency.set(loopLatency);
}
}
}
catch (Exception e) {
e.printStackTrace();
// Even if we get a deadline miss, we still need to
// update the expected activation time for next loop computation
expectedActivationTime.add(usedPeriod, expectedActivationTime);
}
}
System.out.println("Real-time thread execution finished.\n");
}
}
public static void main(String[] args) {
System.out.println("\nHello Real-Time World !");
// Reminder of basic program parameters
System.out.println("----------------------------------");
System.out.println(" Number of iterations = " + ITERATIONS);
System.out.println(" Period value = " + PERIOD + " milliseconds (ms)");
System.out.println("----------------------------------");
// Utility string writers to record and print out latencies,
// writing to memory and avoiding standard output I/O while in RealTimeThread run()
StringWriter noticeableLatencyWriter = new StringWriter(5000);
StringWriter worstLatencyWriter = new StringWriter(5000);
// Initializes real-time program variables
PriorityScheduler ps = (PriorityScheduler) Scheduler.getDefaultScheduler();
int maxPriority = ps.getMaxPriority();
Clock rtClock = Clock.getRealtimeClock();
RelativeTime period = new RelativeTime(PERIOD, 0);
AbsoluteTime startTime = rtClock.getTime().add(1000,0);
// Initializes and starts the periodic RealTimeThread
PeriodicThread periodicThread =
new PeriodicThread(rtClock, period, startTime, noticeableLatencyWriter, worstLatencyWriter);
periodicThread.setPriority(maxPriority);
periodicThread.setReleaseParameters(new PeriodicParameters(startTime, period));
periodicThread.start();
// Waits for RealTime thread and reports results on standard output
try {
periodicThread.join();
System.out.print("Noticeable latencies (values >= 100 microseconds) in order of occurrence (if any): ");
System.out.println(noticeableLatencyWriter);
System.out.print("\nHighest latency values in increasing order: ");
System.out.println(worstLatencyWriter);
// All done
System.out.println("\n\nBye, Real-Time World ...");
} catch(InterruptedException e) {
throw new Error("Impossible interrupt!");
}
}
}