Debian Codegraph @

current/shared/KillSwitch.java

raw symbols SCIP
1 package shared;
2
3 import java.lang.management.ManagementFactory;
4 import java.lang.management.OperatingSystemMXBean;
5 import java.lang.reflect.Array;
6 import java.util.Arrays;
7 //import com.sun.management.OperatingSystemMXBean;
8 import java.util.concurrent.atomic.AtomicBoolean;
9 import java.util.concurrent.atomic.AtomicIntegerArray;
10
11 /**
12 * Monitors CPU utilization to determine if the program has crashed.
13 * Also performs VM forced shutdowns and safe memory allocation.
14 * @author Brian Bushnell
15 * @date Feb 25, 2015
16 *
17 */
18 public final class KillSwitch extends Thread {
19
20 public static void main(String[] args){
21 double seconds=Double.parseDouble(args[0]);
22 double load=Double.parseDouble(args[1]);
23 launch(seconds, load);
24 if(args.length>2){
25
26 }
27 }
28
29 private KillSwitch(double seconds, double load) {
30 maxSeconds=seconds;
31 minLoad=load;
32 }
33
34 public static boolean launch(){
35 return launch(600);
36 }
37
38 public static boolean launch(double seconds){
39 return launch(seconds, 0.002);
40 }
41
42 public static synchronized boolean launch(double seconds, double load){
43 if(count>0){return false;}
44 ks=new KillSwitch(seconds, load);
45 ks.start();
46 return true;
47 }
48
49 @Override
50 public void run(){
51
52 boolean success=monitor();
53 // System.err.println("success: "+success);
54 if(!success || killFlag.get()){
55 if(!suppressMessages){
56 System.err.println("Process has decided it has crashed, and will abort.\n" +
57 "If this decision was incorrect, please re-run with the flag 'monitor=f'");
58 }
59 kill0();
60 }
61 }
62
63 private boolean monitor(){
64
65 final OperatingSystemMXBean bean=ManagementFactory.getOperatingSystemMXBean();
66 if(bean.getSystemLoadAverage()<0){
67 System.err.println("This OS does not support monitor, so monitoring was disabled.");
68 return true;
69 }
70
71 final long start=System.currentTimeMillis();
72 final long buffer=(long)(1+maxSeconds*1000);
73 long stop=start+buffer;
74 // System.err.println("start="+start+", stop="+stop+", buffer="+buffer);
75 // System.err.println("shutdownFlag.get()="+shutdownFlag.get());
76 while(!shutdownFlag.get()){
77 try {
78 sleep(500);
79 } catch (InterruptedException e) {
80 // TODO Auto-generated catch block
81 e.printStackTrace();
82 }
83 final double load=bean.getSystemLoadAverage();
84 final long time=System.currentTimeMillis();
85 if(load>minLoad){stop=time+buffer;}
86 if(time>stop){return false;}
87 // System.err.println("stop-time="+(stop-time)+", load="+load);
88 }
89 // System.err.println("shutdownFlag.get()="+shutdownFlag.get());
90 return true;
91 }
92
93 /*--------------------------------------------------------------*/
94
95 public static synchronized void kill(String s){
96 ballast=null;
97 Exception e=new Exception(s);
98 e.printStackTrace();
99 kill0();
100 }
101
102 public static synchronized void killTraceless(String s){
103 ballast=null;
104 System.err.println(s);
105 kill0();
106 }
107
108 // public static void kill(Throwable e){
109 // e.printStackTrace();
110 // kill0();
111 // }
112
113 public static synchronized void kill(){
114 ballast=null;
115 Exception e=new Exception("Aborting.");
116 e.printStackTrace();
117 kill0();
118 }
119
120 public static synchronized void killSilent(){
121 ballast=null;
122 kill0();
123 }
124
125 private static void kill0(){
126 ballast=null;
127 Runtime.getRuntime().halt(1);
128 }
129
130 public static void shutdown(){
131 shutdownFlag.set(true);
132 }
133
134 public static void setKillFlag(){
135 killFlag.set(true);
136 }
137
138 /*--------------------------------------------------------------*/
139
140 public static final void throwableKill(Throwable e){
141 ballast=null;
142 synchronized(MemKillMessage){
143 e.printStackTrace();
144 kill0();
145 }
146 }
147
148 public static final void exceptionKill(Exception e){
149 ballast=null;
150 synchronized(MemKillMessage){
151 e.printStackTrace();
152 kill0();
153 }
154 }
155
156 public static final void memKill(OutOfMemoryError e){
157 ballast=null;
158 synchronized(MemKillMessage){
159 e.printStackTrace();
160 System.err.println(MemKillMessage);
161 // Shared.printMemory();
162 // killSilent();
163 kill0();
164 }
165 }
166
167 public static final void assertionKill(AssertionError e){
168 ballast=null;
169 synchronized(MemKillMessage){
170 // System.err.println(e);
171 e.printStackTrace();
172 kill0();
173 }
174 }
175
176
177 /*--------------------------------------------------------------*/
178
179 public static final AtomicIntegerArray allocAtomicInt(int len){
180 AtomicIntegerArray ret=null;
181 try {
182 ret=new AtomicIntegerArray(len);
183 } catch (OutOfMemoryError e) {
184 memKill(e);
185 }
186 return ret;
187 }
188
189 public static final long[] allocLong1D(int len){
190 long[] ret=null;
191 try {
192 ret=new long[len];
193 } catch (OutOfMemoryError e) {
194 memKill(e);
195 }
196 return ret;
197 }
198
199 @SuppressWarnings("unchecked")
200 public static final <K> K[] allocObject1D(int len, Class<K> kc){
201 K[] ret=null;
202 try {
203 ret=(K[]) Array.newInstance(kc, len);
204 } catch (OutOfMemoryError e) {
205 memKill(e);
206 }
207 return ret;
208 }
209
210 public static final int[] allocInt1D(int len){
211 int[] ret=null;
212 try {
213 ret=new int[len];
214 } catch (OutOfMemoryError e) {
215 memKill(e);
216 }
217 return ret;
218 }
219
220 public static float[] allocFloat1D(int len) {
221 float[] ret=null;
222 try {
223 ret=new float[len];
224 } catch (OutOfMemoryError e) {
225 memKill(e);
226 }
227 return ret;
228 }
229
230 public static double[] allocDouble1D(int len) {
231 double[] ret=null;
232 try {
233 ret=new double[len];
234 } catch (OutOfMemoryError e) {
235 memKill(e);
236 }
237 return ret;
238 }
239
240 public static final byte[] allocByte1D(int len){
241 byte[] ret=null;
242 try {
243 ret=new byte[len];
244 } catch (OutOfMemoryError e) {
245 memKill(e);
246 }
247 return ret;
248 }
249
250 public static final char[] allocChar1D(int len){
251 char[] ret=null;
252 try {
253 ret=new char[len];
254 } catch (OutOfMemoryError e) {
255 memKill(e);
256 }
257 return ret;
258 }
259
260 /*--------------------------------------------------------------*/
261
262 public static final int[][] allocInt2D(int x){
263 int[][] ret=null;
264 try {
265 ret=new int[x][];
266 } catch (OutOfMemoryError e) {
267 memKill(e);
268 }
269 return ret;
270 }
271
272 public static final int[][] allocInt2D(int x, int y){
273 int[][] ret=null;
274 try {
275 ret=new int[x][y];
276 } catch (OutOfMemoryError e) {
277 ballast2=null;
278 System.err.print(x+","+y);
279 memKill(e);
280 }
281 return ret;
282 }
283
284 public static final float[][] allocFloat2D(int x){
285 float[][] ret=null;
286 try {
287 ret=new float[x][];
288 } catch (OutOfMemoryError e) {
289 memKill(e);
290 }
291 return ret;
292 }
293
294 public static final float[][] allocFloat2D(int x, int y){
295 float[][] ret=null;
296 try {
297 ret=new float[x][y];
298 } catch (OutOfMemoryError e) {
299 ballast2=null;
300 System.err.print(x+","+y);
301 memKill(e);
302 }
303 return ret;
304 }
305
306 public static final long[][] allocLong2D(int x){
307 long[][] ret=null;
308 try {
309 ret=new long[x][];
310 } catch (OutOfMemoryError e) {
311 memKill(e);
312 }
313 return ret;
314 }
315
316 public static final long[][] allocLong2D(int x, int y){
317 long[][] ret=null;
318 try {
319 ret=new long[x][y];
320 } catch (OutOfMemoryError e) {
321 memKill(e);
322 }
323 return ret;
324 }
325
326 /*--------------------------------------------------------------*/
327
328 public static int[][][] allocInt3D(int x) {
329 int[][][] ret=null;
330 try {
331 ret=new int[x][][];
332 } catch (OutOfMemoryError e) {
333 memKill(e);
334 }
335 return ret;
336 }
337
338 public static int[][][] allocInt3D(int x, int y) {
339 int[][][] ret=null;
340 try {
341 ret=new int[x][y][];
342 } catch (OutOfMemoryError e) {
343 memKill(e);
344 }
345 return ret;
346 }
347
348 public static int[][][] allocInt3D(int x, int y, int z) {
349 int[][][] ret=null;
350 try {
351 ret=new int[x][y][z];
352 } catch (OutOfMemoryError e) {
353 memKill(e);
354 }
355 return ret;
356 }
357
358 /*--------------------------------------------------------------*/
359
360 public static byte[] copyOf(byte[] buffer, long newLength) {
361 final int len=buffer.length;
362 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
363 if(newLength>len2 && len2<=len){
364 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
365 }
366 byte[] copy=null;
367 try {
368 copy=Arrays.copyOf(buffer, len2);
369 } catch (OutOfMemoryError e) {
370 memKill(e);
371 }
372 return copy;
373 }
374
375 public static float[] copyOf(float[] buffer, long newLength) {
376 final int len=buffer.length;
377 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
378 if(newLength>len2 && len2<=len){
379 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
380 }
381 float[] copy=null;
382 try {
383 copy=Arrays.copyOf(buffer, len2);
384 } catch (OutOfMemoryError e) {
385 memKill(e);
386 }
387 return copy;
388 }
389
390 public static double[] copyOf(double[] buffer, long newLength) {
391 final int len=buffer.length;
392 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
393 if(newLength>len2 && len2<=len){
394 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
395 }
396 double[] copy=null;
397 try {
398 copy=Arrays.copyOf(buffer, len2);
399 } catch (OutOfMemoryError e) {
400 memKill(e);
401 }
402 return copy;
403 }
404
405 /**
406 * Copy the buffer into an array of size newLength.
407 * Fill extra cells with fillValue.
408 * @param buffer Old array
409 * @param newLength Length of new array
410 * @param fillValue Value to insert in extra cells
411 * @return New array
412 */
413 public static int[] copyAndFill(int[] buffer, long newLength, int fillValue) {
414 final int[] copy=copyOf(buffer, newLength);
415 Arrays.fill(copy, buffer.length, copy.length, fillValue);
416 return copy;
417 }
418
419 public static int[] copyOf(int[] buffer, long newLength) {
420 final int len=buffer.length;
421 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
422 if(newLength>len2 && len2<=len){
423 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
424 }
425 int[] copy=null;
426 try {
427 copy=Arrays.copyOf(buffer, len2);
428 } catch (OutOfMemoryError e) {
429 memKill(e);
430 }
431 return copy;
432 }
433
434 public static int[][] copyOf(int[][] buffer, long newLength) {
435 final int len=buffer.length;
436 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
437 if(newLength>len2 && len2<=len){
438 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
439 }
440 int[][] copy=null;
441 try {
442 copy=Arrays.copyOf(buffer, len2);
443 } catch (OutOfMemoryError e) {
444 memKill(e);
445 }
446 return copy;
447 }
448
449 public static long[] copyOf(long[] buffer, long newLength) {
450 final int len=buffer.length;
451 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
452 if(newLength>len2 && len2<=len){
453 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
454 }
455 long[] copy=null;
456 try {
457 copy=Arrays.copyOf(buffer, len2);
458 } catch (OutOfMemoryError e) {
459 memKill(e);
460 }
461 return copy;
462 }
463
464 public static long[] copyAndFill(long[] buffer, long newLength, int fillValue) {
465 final long[] copy=copyOf(buffer, newLength);
466 Arrays.fill(copy, buffer.length, copy.length, fillValue);
467 return copy;
468 }
469
470 public static long[][] copyOf(long[][] buffer, long newLength) {
471 final int len=buffer.length;
472 final int len2=(int)Tools.min(newLength, Shared.MAX_ARRAY_LEN);
473 if(newLength>len2 && len2<=len){
474 exceptionKill(new Exception("Tried to create an array above length limit: "+len+"," +newLength));
475 }
476 long[][] copy=null;
477 try {
478 copy=Arrays.copyOf(buffer, len2);
479 } catch (OutOfMemoryError e) {
480 memKill(e);
481 }
482 return copy;
483 }
484
485 /*--------------------------------------------------------------*/
486
487 public static byte[] copyOfRange(byte[] buffer, int from, int to) {
488 byte[] copy=null;
489 try {
490 copy=Arrays.copyOfRange(buffer, from, to);
491 } catch (OutOfMemoryError e) {
492 memKill(e);
493 }
494 return copy;
495 }
496
497 public static int[] copyOfRange(int[] buffer, int start, int limit) {
498 int[] copy=null;
499 try {
500 copy=Arrays.copyOfRange(buffer, start, limit);
501 } catch (OutOfMemoryError e) {
502 memKill(e);
503 }
504 return copy;
505 }
506
507 public static long[] copyOfRange(long[] buffer, int start, int limit) {
508 long[] copy=null;
509 try {
510 copy=Arrays.copyOfRange(buffer, start, limit);
511 } catch (OutOfMemoryError e) {
512 memKill(e);
513 }
514 return copy;
515 }
516
517 /*--------------------------------------------------------------*/
518
519 private final double maxSeconds;
520 private final double minLoad;
521
522 private static AtomicBoolean shutdownFlag=new AtomicBoolean(false);
523 private static AtomicBoolean killFlag=new AtomicBoolean(false);
524 private static int count=0;
525 private static KillSwitch ks;
526 private static boolean suppressMessages=false;
527
528 /*--------------------------------------------------------------*/
529
530 private static final String MemKillMessage=new String("\nThis program ran out of memory.\n"
531 + "Try increasing the -Xmx flag and using tool-specific memory-related parameters.");
532
533 public static void addBallast() {
534 synchronized(KillSwitch.class){
535 ballast=new int[20000];
536 ballast2=new int[20000];
537 ballast[0]=1;
538 assert(ballast[0]==1);
539 }
540 }
541
542 private static int[] ballast;
543 private static int[] ballast2;
544
545 }