| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
package jalview.bin; |
| 24 |
|
|
| 25 |
|
import java.util.Locale; |
| 26 |
|
|
| 27 |
|
import jalview.util.ErrorLog; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@author |
| 38 |
|
|
| 39 |
|
|
| |
|
| 62.2% |
Uncovered Elements: 59 (156) |
Complexity: 50 |
Complexity Density: 0.51 |
|
| 40 |
|
public class MemorySetting |
| 41 |
|
{ |
| 42 |
|
|
| 43 |
|
public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc"; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax"; |
| 47 |
|
|
| 48 |
|
private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; |
| 49 |
|
|
| 50 |
|
private static final long GIGABYTE = 1073741824; |
| 51 |
|
|
| 52 |
|
public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE / 2; |
| 53 |
|
|
| 54 |
|
public static final long APPLICATION_MIN_MEMORY = GIGABYTE / 2; |
| 55 |
|
|
| 56 |
|
private static final long MAX_HEAPSIZE_GB_DEFAULT = 32; |
| 57 |
|
|
| 58 |
|
private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8; |
| 59 |
|
|
| 60 |
|
public static final String NS = "MEMORY"; |
| 61 |
|
|
| 62 |
|
public static final String CUSTOMISED_SETTINGS = NS |
| 63 |
|
+ "_CUSTOMISED_SETTINGS"; |
| 64 |
|
|
| 65 |
|
public static final String MEMORY_JVMMEMPC = NS + "_" |
| 66 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.toUpperCase(Locale.ROOT); |
| 67 |
|
|
| 68 |
|
public static final String MEMORY_JVMMEMMAX = NS + "_" |
| 69 |
|
+ MAX_HEAPSIZE_PROPERTY_NAME.toUpperCase(Locale.ROOT); |
| 70 |
|
|
| 71 |
|
protected static boolean logToClassChecked = false; |
| 72 |
|
|
| 73 |
|
public static String memorySuffixes = "bkmgt"; |
| 74 |
|
|
| 75 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 76 |
1 |
public static long getMemorySetting()... |
| 77 |
|
{ |
| 78 |
1 |
return getMemorySetting(null, null); |
| 79 |
|
} |
| 80 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 81 |
16 |
public static long getMemorySetting(String jvmmemmaxarg,... |
| 82 |
|
String jvmmempcarg) |
| 83 |
|
{ |
| 84 |
16 |
return getMemorySetting(jvmmemmaxarg, jvmmempcarg, true, false); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
@param |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
@param |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
@param |
| 115 |
|
|
| 116 |
|
|
| 117 |
|
@return |
| 118 |
|
|
| |
|
| 70.4% |
Uncovered Elements: 24 (81) |
Complexity: 25 |
Complexity Density: 0.45 |
|
| 119 |
16 |
public static long getMemorySetting(String jvmmemmaxarg,... |
| 120 |
|
String jvmmempcarg, boolean useProps, boolean quiet) |
| 121 |
|
{ |
| 122 |
|
|
| 123 |
16 |
long maxMemLong = -1; |
| 124 |
16 |
clearAdjustmentMessage(); |
| 125 |
|
|
| 126 |
|
|
| 127 |
16 |
long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
| 128 |
16 |
if (jvmmemmaxarg == null && useProps) |
| 129 |
|
{ |
| 130 |
1 |
jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME); |
| 131 |
|
} |
| 132 |
16 |
String jvmmemmax = jvmmemmaxarg; |
| 133 |
16 |
if (jvmmemmax != null && jvmmemmax.length() > 0) |
| 134 |
|
{ |
| 135 |
|
|
| 136 |
15 |
try |
| 137 |
|
{ |
| 138 |
15 |
memmax = memoryStringToLong(jvmmemmax); |
| 139 |
15 |
if (memmax == 0) |
| 140 |
|
{ |
| 141 |
0 |
throw (new NumberFormatException("Not allowing 0")); |
| 142 |
|
} |
| 143 |
|
} catch (NumberFormatException e) |
| 144 |
|
{ |
| 145 |
0 |
memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
| 146 |
0 |
setAdjustmentMessage("MemorySetting Property '" |
| 147 |
|
+ MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg |
| 148 |
|
+ "') badly formatted or 0, using default (" |
| 149 |
|
+ MAX_HEAPSIZE_GB_DEFAULT + "g).", quiet); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
|
| 153 |
15 |
if (memmax < APPLICATION_MIN_MEMORY) |
| 154 |
|
{ |
| 155 |
1 |
memmax = APPLICATION_MIN_MEMORY; |
| 156 |
1 |
setAdjustmentMessage("MemorySetting Property '" |
| 157 |
|
+ MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg |
| 158 |
|
+ ") too small, using minimum (" + APPLICATION_MIN_MEMORY |
| 159 |
|
+ ").", quiet); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
} |
| 163 |
|
else |
| 164 |
|
{ |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
16 |
float percent = MAX_HEAPSIZE_PERCENT_DEFAULT; |
| 173 |
16 |
if (jvmmempcarg == null && useProps) |
| 174 |
|
{ |
| 175 |
1 |
jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME); |
| 176 |
|
} |
| 177 |
16 |
String jvmmempc = jvmmempcarg; |
| 178 |
16 |
long mempc = -1; |
| 179 |
16 |
try |
| 180 |
|
{ |
| 181 |
16 |
if (jvmmempc != null) |
| 182 |
|
{ |
| 183 |
15 |
int trypercent = Integer.parseInt(jvmmempc); |
| 184 |
14 |
if (0 <= trypercent && trypercent <= 100) |
| 185 |
|
{ |
| 186 |
14 |
percent = trypercent; |
| 187 |
|
} |
| 188 |
|
else |
| 189 |
|
{ |
| 190 |
0 |
setAdjustmentMessage("MemorySetting Property '" |
| 191 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME |
| 192 |
|
+ "' should be in range 0..100. Using default " + percent |
| 193 |
|
+ "%", quiet); |
| 194 |
|
} |
| 195 |
|
} |
| 196 |
|
} catch (NumberFormatException e) |
| 197 |
|
{ |
| 198 |
1 |
setAdjustmentMessage("MemorySetting property '" |
| 199 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg |
| 200 |
|
+ ") badly formatted", quiet); |
| 201 |
|
} |
| 202 |
|
|
| 203 |
|
|
| 204 |
16 |
boolean memoryPercentError = false; |
| 205 |
16 |
try |
| 206 |
|
{ |
| 207 |
16 |
long physicalMem = GetMemory.getPhysicalMemory(); |
| 208 |
16 |
if (physicalMem > APPLICATION_MIN_MEMORY) |
| 209 |
|
{ |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| 213 |
16 |
mempc = (long) ((physicalMem / 100F) * percent); |
| 214 |
|
|
| 215 |
|
|
| 216 |
16 |
boolean reducedmempc = false; |
| 217 |
16 |
if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY) |
| 218 |
|
{ |
| 219 |
12 |
mempc = physicalMem - LEAVE_FREE_MIN_MEMORY; |
| 220 |
12 |
reducedmempc = true; |
| 221 |
12 |
setAdjustmentMessage("MemorySetting Property '" |
| 222 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg |
| 223 |
|
+ ") too large. Leaving free space for OS and reducing to (" |
| 224 |
|
+ mempc + ").", quiet); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
16 |
if (mempc < APPLICATION_MIN_MEMORY) |
| 229 |
|
{ |
| 230 |
0 |
if (reducedmempc) |
| 231 |
|
{ |
| 232 |
0 |
setAdjustmentMessage("Reduced MemorySetting (" + mempc |
| 233 |
|
+ ") too small. Increasing to application minimum (" |
| 234 |
|
+ APPLICATION_MIN_MEMORY + ").", quiet); |
| 235 |
|
} |
| 236 |
|
else |
| 237 |
|
{ |
| 238 |
0 |
setAdjustmentMessage("MemorySetting Property '" |
| 239 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" |
| 240 |
|
+ jvmmempcarg + ") too small. Using minimum (" |
| 241 |
|
+ APPLICATION_MIN_MEMORY + ").", quiet); |
| 242 |
|
} |
| 243 |
0 |
mempc = APPLICATION_MIN_MEMORY; |
| 244 |
|
} |
| 245 |
|
} |
| 246 |
|
else |
| 247 |
|
{ |
| 248 |
|
|
| 249 |
0 |
mempc = physicalMem; |
| 250 |
0 |
setAdjustmentMessage( |
| 251 |
|
"Not enough physical memory for application. Ignoring MemorySetting Property '" |
| 252 |
|
+ MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" |
| 253 |
|
+ jvmmempcarg |
| 254 |
|
+ "). Using maximum memory available (" |
| 255 |
|
+ physicalMem + ").", |
| 256 |
|
quiet); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
} catch (Throwable t) |
| 260 |
|
{ |
| 261 |
0 |
memoryPercentError = true; |
| 262 |
0 |
setAdjustmentMessage( |
| 263 |
|
"Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean", |
| 264 |
|
quiet); |
| 265 |
0 |
t.printStackTrace(); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
16 |
if (memoryPercentError && mempc == -1 |
| 272 |
|
&& !(jvmmempcarg == null && jvmmemmaxarg != null) |
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
&& memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE) |
| 280 |
|
{ |
| 281 |
0 |
setAdjustmentMessage( |
| 282 |
|
"Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT |
| 283 |
|
+ "g due to failure to read physical memory size.", |
| 284 |
|
quiet); |
| 285 |
0 |
memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; |
| 286 |
|
} |
| 287 |
|
|
| 288 |
16 |
if (mempc == -1) |
| 289 |
|
{ |
| 290 |
0 |
maxMemLong = memmax; |
| 291 |
|
} |
| 292 |
|
else |
| 293 |
|
{ |
| 294 |
16 |
maxMemLong = Math.min(mempc, memmax); |
| 295 |
|
} |
| 296 |
|
|
| 297 |
16 |
return maxMemLong; |
| 298 |
|
} |
| 299 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 5 (15) |
Complexity: 7 |
Complexity Density: 0.78 |
|
| 300 |
15 |
public static boolean isValidMemoryString(String text)... |
| 301 |
|
{ |
| 302 |
15 |
if (text.length() > 0) |
| 303 |
|
{ |
| 304 |
15 |
char lastChar = text.charAt(text.length() - 1); |
| 305 |
15 |
char[] otherChars = text.substring(0, text.length() - 1) |
| 306 |
|
.toCharArray(); |
| 307 |
15 |
for (char c : otherChars) |
| 308 |
|
{ |
| 309 |
59 |
if (c < '0' || c > '9') |
| 310 |
|
{ |
| 311 |
0 |
return false; |
| 312 |
|
} |
| 313 |
|
} |
| 314 |
15 |
if ((lastChar < '0' || lastChar > '9') && memorySuffixes |
| 315 |
|
.indexOf(Character.toLowerCase(lastChar)) == -1) |
| 316 |
|
{ |
| 317 |
0 |
return false; |
| 318 |
|
} |
| 319 |
|
} |
| 320 |
15 |
return true; |
| 321 |
|
} |
| 322 |
|
|
| |
|
| 64% |
Uncovered Elements: 9 (25) |
Complexity: 7 |
Complexity Density: 0.47 |
|
| 323 |
15 |
public static long memoryStringToLong(String memString)... |
| 324 |
|
throws NumberFormatException |
| 325 |
|
{ |
| 326 |
15 |
if (!isValidMemoryString(memString)) |
| 327 |
|
{ |
| 328 |
0 |
throw (new NumberFormatException("Not a valid memory string")); |
| 329 |
|
} |
| 330 |
15 |
char suffix = Character |
| 331 |
|
.toLowerCase(memString.charAt(memString.length() - 1)); |
| 332 |
15 |
if ('0' <= suffix && suffix <= '9') |
| 333 |
|
{ |
| 334 |
1 |
return Long.valueOf(memString); |
| 335 |
|
} |
| 336 |
14 |
if (memorySuffixes.indexOf(suffix) == -1) |
| 337 |
|
{ |
| 338 |
0 |
return -1; |
| 339 |
|
} |
| 340 |
|
|
| 341 |
14 |
long multiplier = (long) Math.pow(2, |
| 342 |
|
memorySuffixes.indexOf(suffix) * 10); |
| 343 |
|
|
| 344 |
|
|
| 345 |
|
|
| 346 |
14 |
long mem = Long |
| 347 |
|
.parseLong(memString.substring(0, memString.length() - 1)); |
| 348 |
14 |
if (mem == 0) |
| 349 |
|
{ |
| 350 |
0 |
return 0; |
| 351 |
|
} |
| 352 |
|
|
| 353 |
|
|
| 354 |
14 |
if (Long.MAX_VALUE / mem > multiplier) |
| 355 |
|
{ |
| 356 |
14 |
return multiplier * mem; |
| 357 |
|
} |
| 358 |
|
else |
| 359 |
|
{ |
| 360 |
|
|
| 361 |
0 |
ErrorLog.outPrintln("Memory parsing of '" + memString |
| 362 |
|
+ "' produces number too big. Limiting to Long.MAX_VALUE=" |
| 363 |
|
+ Long.MAX_VALUE); |
| 364 |
0 |
return Long.MAX_VALUE; |
| 365 |
|
} |
| 366 |
|
} |
| 367 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 368 |
0 |
public static String memoryLongToString(long mem)... |
| 369 |
|
{ |
| 370 |
0 |
return memoryLongToString(mem, "%.3f"); |
| 371 |
|
} |
| 372 |
|
|
| |
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 373 |
0 |
public static String memoryLongToString(long mem, String format)... |
| 374 |
|
{ |
| 375 |
0 |
int exponent = 0; |
| 376 |
0 |
float num = mem; |
| 377 |
0 |
char suffix = 'b'; |
| 378 |
|
|
| 379 |
0 |
for (int i = 0; i < memorySuffixes.length(); i++) |
| 380 |
|
{ |
| 381 |
0 |
char s = Character.toUpperCase(memorySuffixes.charAt(i)); |
| 382 |
0 |
if (mem < (long) Math.pow(2, exponent + 10) |
| 383 |
|
|| i == memorySuffixes.length() - 1) |
| 384 |
|
{ |
| 385 |
0 |
suffix = s; |
| 386 |
0 |
num = (float) (mem / Math.pow(2, exponent)); |
| 387 |
0 |
break; |
| 388 |
|
} |
| 389 |
0 |
exponent += 10; |
| 390 |
|
} |
| 391 |
|
|
| 392 |
0 |
return String.format(format, num) + suffix; |
| 393 |
|
} |
| 394 |
|
|
| 395 |
|
private static String ADJUSTMENT_MESSAGE = null; |
| 396 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 397 |
14 |
private static void setAdjustmentMessage(String reason, boolean quiet)... |
| 398 |
|
{ |
| 399 |
14 |
ADJUSTMENT_MESSAGE = reason; |
| 400 |
14 |
if (!quiet) |
| 401 |
|
{ |
| 402 |
14 |
ErrorLog.outPrintln(reason); |
| 403 |
|
} |
| 404 |
|
} |
| 405 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 406 |
16 |
public static void clearAdjustmentMessage()... |
| 407 |
|
{ |
| 408 |
16 |
ADJUSTMENT_MESSAGE = null; |
| 409 |
|
} |
| 410 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 411 |
0 |
public static String getAdjustmentMessage()... |
| 412 |
|
{ |
| 413 |
0 |
return ADJUSTMENT_MESSAGE; |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
} |