annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/shared/KillSwitch.java @ 68:5028fdace37b

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