Skip to content

Commit

Permalink
re-enabled old Math2.ensureMemory and modified shedThisRequest to wor…
Browse files Browse the repository at this point in the history
…k with it

Former-commit-id: ed3f467090d10b0c5c03e70c0fd5f3e665ae7124 [formerly 4e2b109fcdd71a3b8cf8384d5c34f6ad861f603f] [formerly 5aea312c3102cb9f41e25c78544abfe25158e008 [formerly 1e4dbd99a979fbd4066aa34041134e70bb15b0d0]]
Former-commit-id: 02231a242bff5b66940a950cc06a44d0b3693ad1 [formerly da5939ad91a7db66b500f81f3dd23f4a7fd094d7]
Former-commit-id: 6ce42236695422d7a066c399203963b5f7802463
Former-commit-id: c3c2870cd8f5c8e00eb02a5df9d97f919335c03f
  • Loading branch information
Bob Simons committed Sep 28, 2022
1 parent a553a74 commit f56c24d
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 47 deletions.
3 changes: 3 additions & 0 deletions WEB-INF/classes/com/cohort/array/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public ByteArray() {
* @param primitiveArray a primitiveArray of any type
*/
public ByteArray(PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(1L * primitiveArray.size(), "ByteArray");
array = new byte[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -157,6 +158,7 @@ public final static ByteArray fromCSV(String csv) {
*/
public ByteArray(int first, int last) {
size = last - first + 1;
Math2.ensureMemoryAvailable(1L * size, "ByteArray");
array = new byte[size];
for (int i = 0; i < size; i++)
array[i] = (byte)(first + i);
Expand Down Expand Up @@ -1280,6 +1282,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(1L * array.length, "ByteArray");
final byte newArray[] = new byte[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
2 changes: 2 additions & 0 deletions WEB-INF/classes/com/cohort/array/CharArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public final boolean isMissingValue(final int index) {
* @param primitiveArray a primitiveArray of any type
*/
public CharArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(2L * primitiveArray.size(), "CharArray");
array = new char[primitiveArray.size()]; //exact size
maxIsMV = true; //always true for CharArray, so users shouldn't ever need to test it
append(primitiveArray);
Expand Down Expand Up @@ -1229,6 +1230,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(2L * array.length, "CharArray");
final char newArray[] = new char[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
2 changes: 2 additions & 0 deletions WEB-INF/classes/com/cohort/array/DoubleArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public DoubleArray() {
* @param primitiveArray a primitiveArray of any type
*/
public DoubleArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(8L * primitiveArray.size(), "DoubleArray");
array = new double[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -1030,6 +1031,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(8L * array.length, "DoubleArray");
final double newArray[] = new double[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
2 changes: 2 additions & 0 deletions WEB-INF/classes/com/cohort/array/FloatArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public FloatArray() {
* @param primitiveArray a primitiveArray of any type
*/
public FloatArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(4L * primitiveArray.size(), "FloatArray");
array = new float[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -1061,6 +1062,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(4L * array.length, "FloatArray");
final float newArray[] = new float[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
3 changes: 3 additions & 0 deletions WEB-INF/classes/com/cohort/array/IntArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public IntArray() {
* @param primitiveArray a primitiveArray of any type
*/
public IntArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(4L * primitiveArray.size(), "IntArray");
array = new int[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand All @@ -129,6 +130,7 @@ public IntArray(final int capacity, final boolean active) {
*/
public IntArray(final int first, final int last) {
size = last - first + 1;
Math2.ensureMemoryAvailable(4L * size, "IntArray");
array = new int[size];
for (int i = 0; i < size; i++)
array[i] = first + i;
Expand Down Expand Up @@ -1159,6 +1161,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(4L * array.length, "IntArray");
final int newArray[] = new int[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
2 changes: 2 additions & 0 deletions WEB-INF/classes/com/cohort/array/LongArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public LongArray() {
* @param primitiveArray a primitiveArray of any type
*/
public LongArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(8L * primitiveArray.size(), "LongArray");
array = new long[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -1121,6 +1122,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(8L * array.length, "LongArray");
final long newArray[] = new long[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
3 changes: 3 additions & 0 deletions WEB-INF/classes/com/cohort/array/ShortArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public ShortArray() {
* @param primitiveArray a primitiveArray of any type
*/
public ShortArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(2L * primitiveArray.size(), "ShortArray");
array = new short[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand All @@ -128,6 +129,7 @@ public ShortArray(final int capacity, final boolean active) {
*/
public ShortArray(final int first, final int last) {
size = last - first + 1;
Math2.ensureMemoryAvailable(2L * size, "ShortArray");
array = new short[size];
for (int i = 0; i < size; i++)
array[i] = (short)(first + i);
Expand Down Expand Up @@ -1188,6 +1190,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(2L * array.length, "ShortArray");
final short newArray[] = new short[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
2 changes: 2 additions & 0 deletions WEB-INF/classes/com/cohort/array/StringArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public StringArray() {
* @param primitiveArray a primitiveArray of any type
*/
public StringArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(16L * primitiveArray.size(), "StringArray");
array = new StringHolder[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -1539,6 +1540,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(16L * array.length, "StringArray");
StringHolder[] newArray = new StringHolder[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
3 changes: 3 additions & 0 deletions WEB-INF/classes/com/cohort/array/UByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public UByteArray() {
* @param primitiveArray a primitiveArray of any type
*/
public UByteArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(1L * primitiveArray.size(), "UByteArray");
array = new byte[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -199,6 +200,7 @@ public UByteArray(final short[] anArray) {
*/
public UByteArray(final int first, final int last) {
size = last - first + 1;
Math2.ensureMemoryAvailable(1L * size, "UByteArray");
array = new byte[size];
for (int i = 0; i < size; i++)
array[i] = pack(first + i);
Expand Down Expand Up @@ -1379,6 +1381,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(1L * array.length, "UByteArray");
final byte newArray[] = new byte[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
3 changes: 3 additions & 0 deletions WEB-INF/classes/com/cohort/array/UIntArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public UIntArray() {
* @param primitiveArray a primitiveArray of any type
*/
public UIntArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(4L * primitiveArray.size(), "UIntArray");
array = new int[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand All @@ -164,6 +165,7 @@ public UIntArray(final int capacity, final boolean active) {
*/
public UIntArray(final int first, final int last) {
size = last - first + 1;
Math2.ensureMemoryAvailable(4L * size, "UIntArray");
array = new int[size];
for (int i = 0; i < size; i++)
array[i] = pack(first + i);
Expand Down Expand Up @@ -1252,6 +1254,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(4L * array.length, "UIntArray");
final int newArray[] = new int[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
2 changes: 2 additions & 0 deletions WEB-INF/classes/com/cohort/array/ULongArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public ULongArray() {
* @param primitiveArray a primitiveArray of any type
*/
public ULongArray(final PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(8L * primitiveArray.size(), "ULongArray");
array = new long[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand Down Expand Up @@ -1304,6 +1305,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(8L * array.length, "ULongArray");
final long newArray[] = new long[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
4 changes: 4 additions & 0 deletions WEB-INF/classes/com/cohort/array/UShortArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public UShortArray() {
* @param primitiveArray a primitiveArray of any type
*/
public UShortArray(PrimitiveArray primitiveArray) {
Math2.ensureMemoryAvailable(2L * primitiveArray.size(), "UShortArray");
array = new short[primitiveArray.size()]; //exact size
append(primitiveArray);
}
Expand All @@ -170,6 +171,7 @@ public UShortArray(final int capacity, final boolean active) {
*/
public UShortArray(final int first, final int last) {
size = last - first + 1;
Math2.ensureMemoryAvailable(2L * size, "UShortArray");
array = new short[size];
for (int i = 0; i < size; i++)
array[i] = pack(first + i);
Expand Down Expand Up @@ -211,6 +213,7 @@ public UShortArray(final int[] anArray) {
*/
public UShortArray(final char[] charArray) {
size = charArray.length;
Math2.ensureMemoryAvailable(2L * size, "UShortArray");
array = new short[size];
for (int i = 0; i < size; i++)
array[i] = (short)charArray[i];
Expand Down Expand Up @@ -1286,6 +1289,7 @@ public void copy(final int from, final int to) {
public void reorder(final int rank[]) {
final int n = rank.length;
//new length could be n, but I'll keep it the same array.length as before
Math2.ensureMemoryAvailable(2L * array.length, "UShortArray");
final short newArray[] = new short[array.length];
for (int i = 0; i < n; i++)
newArray[i] = array[rank[i]];
Expand Down
38 changes: 8 additions & 30 deletions WEB-INF/classes/com/cohort/util/Math2.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public class Math2 {
public volatile static long maxUsingMemory = 0; //volatile: used by all threads
public static long maxMemory = Runtime.getRuntime().maxMemory();

public static long halfMemory = maxMemory / 2; //time for shedThisRequest to call gc and reject highMemory requests
public static long maxSafeMemory = maxMemory * 3L / 4; //the max any method should consider getting to
public static long halfMemory = maxMemory / 2; //50% time for shedThisRequest to call gc and reject highMemory requests
public static long highMemory = maxMemory * 65L/100; //65% time for shedThisRequest to call gc and reject lowMemory requests
public static long maxSafeMemory = maxMemory * 3L / 4; //75% the max we should consider getting to
public static long dangerousMemory = maxMemory * 9L / 10; //90% this is really bad

public static long alwaysOkayMemoryRequest = maxSafeMemory / 16;
public static long alwaysOkayMemoryRequest = maxSafeMemory / 20;

/**
* These are *not* final so EDStatic can replace them with translated Strings.
Expand Down Expand Up @@ -452,8 +454,7 @@ public static long gc(final long millis) {


/**
* This throws an exception if the requested nBytes are unlikely to be
* available.
* This throws an exception if the requested nBytes leads to memoryInUse&gt;dangerousMemory.
* This isn't perfect, but is better than nothing.
* Future: locks? synchronization? ...?
*
Expand All @@ -463,34 +464,11 @@ public static long gc(final long millis) {
* @throws RuntimeException if the requested nBytes are unlikely to be available.
*/
public static void ensureMemoryAvailable(final long nBytes, final String attributeTo) {
//2022-09-08 Previously, this used the code now in testMemoryAvailable.
//Now this does nothing.
//Some methods previously used ensureMemoryAvailable, but now use testMemoryAvaible.
//Previously, this method could reject any request for lots of memory,
//Danger: this method can reject any request for lots of memory,
// even if it was for ERDDAP management (i.e., shooting myself in the foot).
}


/**
* This throws an exception if the requested nBytes are unlikely to be
* available.
* This isn't perfect, but is better than nothing.
* Future: locks? synchronization? ...?
*
* @param nBytes size of data structure that caller plans to create
* @param attributeTo for a WARNING or ERROR message, this is the string
* to which this not-enough-memory issue should be attributed.
* @throws RuntimeException if the requested nBytes are unlikely to be available.
*/
public static void testMemoryAvailable(final long nBytes, final String attributeTo) {

//2022-09-08 The new system for memory protection in ERDDAP is to shed user requests
//coming into ERDDAP when current memory usage is high (see ERDDAP.shedThisRequest()).
//But some places call this new method (like old ensureMemoryAvailable, but new name).
//Now, I really avoid calling gc -- let memory use appear to be hight so user requests are rejected.

//this is a little risky, but avoids frequent calls to calculate memoryInUse
if (nBytes < alwaysOkayMemoryRequest) //e.g., 8GB -&gt; maxSafe=6GB /8=750MB //2014-09-05 was 10MB!
if (nBytes < alwaysOkayMemoryRequest) //e.g., 8GB -&gt; maxSafe=6GB /20=300MB
return;

//is this single request by itself too big under any circumstances?
Expand Down
30 changes: 30 additions & 0 deletions WEB-INF/classes/gov/noaa/pfel/coastwatch/Projects.java
Original file line number Diff line number Diff line change
Expand Up @@ -10379,5 +10379,35 @@ public static boolean appendOBIS(int nLines, String fileName, String colNamesLin
}
}

/**
* Given an ERDDAP log file, this finds all of the requests which haven't finished.
*
*/
public static void findUnfinishedRequests(String logFileName) throws Exception {
String2.log("* Projects.findUnfinishedRequests(" + logFileName + ")");
ArrayList<String> al = File2.readLinesFromFile(logFileName, "UTF-8", 1);
String lines[] = al.toArray(new String[0]);
al = null;
int nLines = lines.length;
StringBuilder sb = new StringBuilder();

for (int outer = 0; outer < nLines; outer++) {
//is this a startLine?
String line = lines[outer];
if (!line.startsWith("{{{{#"))
continue;

//find the stopLine
int spacePo = line.indexOf(" ", 5);
int number = String2.parseInt(line.substring(5, spacePo));
String stopLine = String2.stringStartsWith(lines, "}}}}#" + number + " ");
if (stopLine == null)
sb.append(line + "\n");
}

String2.log(sb.toString());
}


}

1 change: 1 addition & 0 deletions WEB-INF/classes/gov/noaa/pfel/coastwatch/TestAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public static void main(String args[]) throws Throwable {
// "/u00/satellite/PH53/1981/data/" +
// "19810826023552-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA07_G_1981238_night-v02.0-fv01.0.nc",
// "sea_surface_temperature", 0.01);
// Projects.findUnfinishedRequests("I:/logArchivedAt2022-09-27T12.52.00_shed0.txt");

// *** Daily
// Projects.viirsLatLon(true); //create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,7 @@ public void makeLonPM180(boolean pm180) throws Exception {
//the tData object below can be huge, ensure there is enough memory
Math2.ensureArraySizeOkay(newNLon * (long)nLat, "Grid.makeLonPM180");
long tDataNBytes = newNLon * 8L * nLat; //calculation is done as longs
Math2.testMemoryAvailable(tDataNBytes, "Grid.makeLonPM180"); //throws Exception if trouble
Math2.ensureMemoryAvailable(tDataNBytes, "Grid.makeLonPM180"); //throws Exception if trouble

//for each new tLon value, find which lon value matches it, and move that column's data to tData
double tData[] = new double[newNLon * nLat];
Expand Down
Loading

0 comments on commit f56c24d

Please sign in to comment.