4
4
import pexplicit .runtime .STATUS ;
5
5
import pexplicit .runtime .logger .PExplicitLogger ;
6
6
import pexplicit .runtime .logger .StatWriter ;
7
+ import pexplicit .runtime .scheduler .Scheduler ;
7
8
import pexplicit .runtime .scheduler .explicit .ExplicitSearchScheduler ;
8
9
import pexplicit .runtime .scheduler .replay .ReplayScheduler ;
9
10
import pexplicit .utils .exceptions .BugFoundException ;
14
15
15
16
import java .time .Duration ;
16
17
import java .time .Instant ;
18
+ import java .util .ArrayList ;
17
19
import java .util .concurrent .*;
18
20
19
21
/**
20
22
* Represents the runtime executor that executes the analysis engine
21
23
*/
22
24
public class RuntimeExecutor {
23
- private static ExecutorService executor ;
24
- private static Future <Integer > future ;
25
- private static ExplicitSearchScheduler scheduler ;
25
+ private static ThreadPoolExecutor executor ;
26
+ private static ArrayList < Future <Integer >> futures = new ArrayList <>() ;
27
+ private static ArrayList < ExplicitSearchScheduler > schedulers = new ArrayList <>() ;
26
28
27
29
private static void runWithTimeout (long timeLimit )
28
30
throws TimeoutException ,
29
31
InterruptedException ,
30
32
RuntimeException {
31
- try {
33
+ try { // PIN: If thread gets exception, need to kill the other threads.
32
34
if (timeLimit > 0 ) {
33
- future .get (timeLimit , TimeUnit .SECONDS );
35
+
36
+ for (Future <Integer > future : futures ) {
37
+ // Future<Integer> future = futures.get(i);
38
+ future .get (timeLimit , TimeUnit .SECONDS );
39
+ }
40
+
34
41
} else {
35
- future .get ();
42
+
43
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++) {
44
+ Future <Integer > future = futures .get (i );
45
+ future .get ();
46
+ }
36
47
}
37
48
} catch (TimeoutException | BugFoundException e ) {
38
49
throw e ;
@@ -55,7 +66,11 @@ private static void runWithTimeout(long timeLimit)
55
66
56
67
private static void printStats () {
57
68
double searchTime = TimeMonitor .stopInterval ();
58
- scheduler .recordStats ();
69
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++) {
70
+ ExplicitSearchScheduler scheduler = schedulers .get (i );
71
+ scheduler .recordStats ();
72
+ }
73
+
59
74
if (PExplicitGlobal .getResult ().equals ("correct for any depth" )) {
60
75
PExplicitGlobal .setStatus (STATUS .VERIFIED );
61
76
} else if (PExplicitGlobal .getResult ().startsWith ("correct up to step" )) {
@@ -69,9 +84,9 @@ private static void preprocess() {
69
84
PExplicitLogger .logInfo (String .format ("... Checker is using '%s' strategy (seed:%s)" ,
70
85
PExplicitGlobal .getConfig ().getSearchStrategyMode (), PExplicitGlobal .getConfig ().getRandomSeed ()));
71
86
72
- executor = Executors .newSingleThreadExecutor ( );
87
+ executor = ( ThreadPoolExecutor ) Executors .newFixedThreadPool ( PExplicitGlobal . getMaxThreads () );
73
88
74
- PExplicitGlobal .setResult ("error" );
89
+ // PExplicitGlobal.setResult("error"); // TODO: Set Results, need to take care of.
75
90
76
91
double preSearchTime =
77
92
TimeMonitor .findInterval (TimeMonitor .getStart ());
@@ -84,10 +99,29 @@ private static void preprocess() {
84
99
85
100
private static void process (boolean resume ) throws Exception {
86
101
try {
87
- TimedCall timedCall = new TimedCall (scheduler , resume );
88
- future = executor .submit (timedCall );
102
+ // create and add first task through scheduler 0
103
+ schedulers .get (0 ).getSearchStrategy ().createFirstTask ();
104
+
105
+ ArrayList <TimedCall > timedCalls = new ArrayList <>();
106
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++) {
107
+ timedCalls .add (new TimedCall (schedulers .get (i ), resume , i ));
108
+ }
109
+
110
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++) {
111
+ Future <Integer > future = executor .submit (timedCalls .get (i ));
112
+ futures .add (future );
113
+ }
114
+
115
+ Thread .sleep (1000 ); // Sleep for 1 second, so that threads can pick up the task from the executor object
116
+
117
+ // Get the number of pending tasks
118
+ int pendingTasks = executor .getQueue ().size ();
119
+ PExplicitLogger .logInfo ("Number of pending tasks: " + pendingTasks );
120
+
89
121
TimeMonitor .startInterval ();
122
+
90
123
runWithTimeout ((long ) PExplicitGlobal .getConfig ().getTimeLimit ());
124
+
91
125
} catch (TimeoutException e ) {
92
126
PExplicitGlobal .setStatus (STATUS .TIMEOUT );
93
127
throw new Exception ("TIMEOUT" , e );
@@ -96,13 +130,21 @@ private static void process(boolean resume) throws Exception {
96
130
throw new Exception ("MEMOUT" , e );
97
131
} catch (BugFoundException e ) {
98
132
PExplicitGlobal .setStatus (STATUS .BUG_FOUND );
99
- PExplicitGlobal .setResult (String .format ("found cex of length %d" , scheduler .getStepNumber ()));
133
+
134
+
100
135
PExplicitLogger .logStackTrace (e );
101
136
102
- ReplayScheduler replayer = new ReplayScheduler (scheduler .schedule );
103
- PExplicitGlobal .setScheduler (replayer );
137
+ ArrayList <ReplayScheduler > replayers = new ArrayList <>();
138
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++)
139
+ replayers .add (new ReplayScheduler ((schedulers .get (i )).schedule ));
140
+
141
+ ArrayList <Scheduler > localSchedulers = PExplicitGlobal .getSchedulers ();
142
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++)
143
+ localSchedulers .set (i , replayers .get (i ));
144
+
104
145
try {
105
- replayer .run ();
146
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++)
147
+ (replayers .get (i )).run ();
106
148
} catch (NullPointerException | StackOverflowError | ClassCastException replayException ) {
107
149
PExplicitLogger .logStackTrace ((Exception ) replayException );
108
150
throw new BugFoundException (replayException .getMessage (), replayException );
@@ -121,20 +163,34 @@ private static void process(boolean resume) throws Exception {
121
163
PExplicitGlobal .setStatus (STATUS .ERROR );
122
164
throw new Exception ("ERROR" , e );
123
165
} finally {
124
- future .cancel (true );
166
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++) {
167
+ Future <Integer > future = futures .get (i );
168
+ future .cancel (true );
169
+ }
125
170
executor .shutdownNow ();
126
- scheduler .updateResult ();
171
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++)
172
+ (schedulers .get (i )).updateResult ();
127
173
printStats ();
128
- PExplicitLogger .logEndOfRun (scheduler , Duration .between (TimeMonitor .getStart (), Instant .now ()).getSeconds ());
174
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++)
175
+ PExplicitLogger .logEndOfRun (schedulers .get (i ), Duration .between (TimeMonitor .getStart (), Instant .now ()).getSeconds ());
129
176
}
130
177
}
131
178
132
179
public static void run () throws Exception {
133
- scheduler = new ExplicitSearchScheduler ();
134
- PExplicitGlobal .setScheduler (scheduler );
180
+ ArrayList <Scheduler > localSchedulers = PExplicitGlobal .getSchedulers ();
181
+ for (int i = 0 ; i < PExplicitGlobal .getMaxThreads (); i ++) {
182
+ ExplicitSearchScheduler localCopy = new ExplicitSearchScheduler ();
183
+ schedulers .add (localCopy );
184
+ localSchedulers .add (localCopy );
185
+ }
186
+
135
187
136
188
preprocess ();
189
+
190
+
137
191
process (false );
192
+
193
+
138
194
}
139
195
140
196
}
0 commit comments