Skip to content

Commit

Permalink
Auto icr parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrd committed Jan 24, 2023
1 parent 6f00b38 commit 40e30d9
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ This will produce a pyramid with JPG (lossy) tiles.

## 🚨 Memory overflow issues 🚨 ##

NOTE: Starting from the version 1.1.5, if you omit the icr parameter, the tool will try to "safely" guess it based on the input image dimensions and the heap size.

By default PyramidIO is trying to read and cache the entire image into the memory to achieve the best possible performance. In case of large images it might cause memory overflow issues. There are two types of them: `java.lang.OutOfMemoryError: Java heap space` and `java.lang.RuntimeException: Cannot cache region java.awt.Rectangle`
Both are usually fixable by using `-icr` parameter:
```
Expand Down
2 changes: 1 addition & 1 deletion generic-archiver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</parent>
<name>${project.groupId}:${project.artifactId}</name>
<description>Generic library to write archives folders.</description>
Expand Down
2 changes: 1 addition & 1 deletion hdfs-archiver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</parent>
<name>${project.groupId}:${project.artifactId}</name>
<description>Library to read and write archives on HDFS.</description>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
<packaging>pom</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>Parent project of pyramidio, a Java tool to read and write image pyramids. Bio-Formats enhanced version.</description>
Expand Down
2 changes: 1 addition & 1 deletion pyramidio-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</parent>
<name>${project.groupId}:${project.artifactId}</name>
<description>Command line interface to build image pyramids.</description>
Expand Down
61 changes: 59 additions & 2 deletions pyramidio-cli/src/main/java/gov/nist/isg/pyramidio/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import gov.nist.isg.archiver.FilesArchiver;
//@darwinjob import gov.nist.isg.pyramidio.DirectImageReader;
import gov.nist.isg.pyramidio.ScalablePyramidBuilder;
import loci.formats.FormatException;
import loci.formats.ImageReader;
import no.uio.nesys.pyramidio.BioFormatsImageReader;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -126,7 +129,7 @@ public static void main(String[] args) {
= (Number) commandLine.getParsedOptionValue(
inputCacheRatioOption.getOpt());
float cachePercentage = inputCacheRatioNumber == null
? 1
? getCalculatedPercentage(inputFile)
: inputCacheRatioNumber.floatValue();

ScalablePyramidBuilder spb = new ScalablePyramidBuilder(
Expand Down Expand Up @@ -164,7 +167,61 @@ public static void main(String[] args) {

}

private static void printHelp(Options options) {
private static float getCalculatedPercentage(File inputFile) {

try {
ImageReader imageReader = new ImageReader();
imageReader.setId(inputFile.getPath());

long width = imageReader.getSizeX();
long height = imageReader.getSizeY();
long bitsPerPixel = imageReader.getBitsPerPixel();
long channelCount = imageReader.getRGBChannelCount();

logger.info("Input image width: " + width + ", height: " + height + ", channel count: " + channelCount
+ ", bits per pixel: " + bitsPerPixel);
if (channelCount > 3)
logger.warning(
"Channel count > 3. Is alpha channel present? The output pyramid might be corrupted. See https://github.com/darwinjob/pyramidio-bioformats/issues/1");

long imageSize = width * height * bitsPerPixel * channelCount / 8; // bytes
logger.info("Uncompressed image size: " + imageSize / (1024 * 1024) + "MB");

logger.info("Number of processors: " + Runtime.getRuntime().availableProcessors());

// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();
// Get maximum size of heap in bytes. The heap cannot grow beyond this size. Any
// attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();

logger.info("Total memory: " + heapSize / (1024 * 1024) + "MB, max memory: " + heapMaxSize / (1024 * 1024)
+ "MB, free memory: " + heapFreeSize / (1024 * 1024) + "MB");

// TODO ATTN.!: Assuming taking this amount of the heap is safe. Wild guess.
float icr = (heapMaxSize / 5f) / imageSize;
logger.info("Calculated icr: " + icr);

if (icr > 1)
icr = 1;

logger.info("Actual icr: " + icr);

imageReader.close();
return icr;
} catch (FormatException | IOException e) {
logger.info(
"Failed to calculate icr (Input Cache Ratio). Setting icr to 1 (cache entire image). The exception:\n"
+ e);
return 1;
}

}

private static void printHelp(Options options) {
new HelpFormatter().printHelp("pyramidio", options);
}
}
2 changes: 1 addition & 1 deletion pyramidio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</parent>
<name>${project.groupId}:${project.artifactId}</name>
<description>Library to read and write image pyramids.</description>
Expand Down
2 changes: 1 addition & 1 deletion s3-archiver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</parent>
<name>${project.groupId}:${project.artifactId}</name>
<description>Library to read and write archives on Amazon S3.</description>
Expand Down
2 changes: 1 addition & 1 deletion tar-archiver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>gov.nist.isg</groupId>
<artifactId>pyramidio-parent</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
</parent>
<name>${project.groupId}:${project.artifactId}</name>
<description>Library to read and write archives in tar files.</description>
Expand Down

0 comments on commit 40e30d9

Please sign in to comment.