Skip to content

Commit

Permalink
Merge pull request #525 from dkimitsa/fix/ramdisk-tmpfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Ski authored Nov 21, 2020
2 parents afb92a6 + 818ce14 commit d915b8b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.TreeMap;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -233,6 +222,7 @@ public enum TreeShakerMode {
* the builder() method skip them.
*/

private transient final UUID buildUuid;
private transient List<Plugin> plugins = new ArrayList<>();
private transient Target target = null;
private transient File osArchDepLibDir;
Expand All @@ -248,7 +238,10 @@ public enum TreeShakerMode {
private transient Arch sliceArch;
private transient StripArchivesBuilder stripArchivesBuilder;

protected Config() {
protected Config(UUID uuid) {
// save session uuid
this.buildUuid = uuid;

// Add standard plugins
this.plugins.addAll(0, Arrays.asList(
new InterfaceBuilderClassesPlugin(),
Expand All @@ -271,6 +264,10 @@ public Builder builder() throws IOException {
return new Builder(clone(configBeforeBuild));
}

public UUID getBuildUuid() {
return buildUuid;
}

public Home getHome() {
return home;
}
Expand Down Expand Up @@ -816,7 +813,7 @@ private void mergeConfigsFromClasspath() throws IOException {
// classpath. Last the config from this object is added.

// First merge all configs on the classpath to an empty Config
Config config = new Config();
Config config = new Config(this.buildUuid);
for (Path path : clazzes.getPaths()) {
for (String dir : dirs) {
if (path.contains(dir + "/robovm.xml")) {
Expand Down Expand Up @@ -864,7 +861,7 @@ private void loadPluginsFromClassPath() {
}

private static Config clone(Config config) throws IOException {
Config clone = new Config();
Config clone = new Config(config.buildUuid);
for (Field f : Config.class.getDeclaredFields()) {
if (!Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) {
f.setAccessible(true);
Expand Down Expand Up @@ -1214,7 +1211,7 @@ public static class Builder {
}

public Builder() {
this.config = new Config();
this.config = new Config(UUID.randomUUID());
}

public Builder os(OS os) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,21 @@ public void setupRamDisk(Config config, File cacheDir, File tmpDir) {
config.getLogger().info("Couldn't create cache directory on RAM disk, using hard drive");
return;
}

// manage every build in own tmp folder -- allows to clear up not required tmp data
File newTmpDir = new File(volume, "tmp");
newTmpDir = new File(newTmpDir, config.getBuildUuid().toString());
if (tmpDir.getAbsolutePath().startsWith(newTmpDir.getAbsolutePath())) {
// tmpDir already build on top of RamDisk, don't add it
// happens when building slice config from main one
newTmpDir = tmpDir;
} else {
newTmpDir = new File(newTmpDir, tmpDir.getAbsolutePath());
}
if (!newTmpDir.exists() && !newTmpDir.mkdirs()) {
config.getLogger().info("Couldn't create tmp directory on RAM disk, using hard drive");
return;
}
newTmpDir = new File(newTmpDir, tmpDir.getAbsolutePath());
config.getLogger().info("Using RAM disk at %s for cache and tmp directory", ROBOVM_RAM_DISK_PATH);
this.newCacheDir = newCacheDir;
this.newTmpDir = newTmpDir;
Expand All @@ -127,10 +136,15 @@ public void setupRamDisk(Config config, File cacheDir, File tmpDir) {
}

private void cleanRamDisk(FileStore store, File volume, Config config) {
// clean the cache/ and tmp/ dirs
// FIXME be smarter as per the issue report
// clean tmp/ dir
try {
cleanTmp(volume, config);
} catch (IOException e) {
// nothing to do here
}

// clean the cache/
try {
// FileUtils.deleteDirectory(new File(volume, "tmp"));
// only clean the cache if killing the tmp dir didn't work
if (store.getUsableSpace() < MIN_FREE_SPACE) {
cleanCache(store, volume, config);
Expand All @@ -140,6 +154,24 @@ private void cleanRamDisk(FileStore store, File volume, Config config) {
}
}

private void cleanTmp(File volume, Config config) throws IOException {
// clean up all files/folders inside tmp but not current build ones
File tmpDir = new File(volume, "tmp");
if (tmpDir.exists() && tmpDir.isDirectory()) {
File[] builds = tmpDir.listFiles();
if (builds != null && builds.length > 0) {
String currentBuild = config.getBuildUuid().toString();
for (File b : builds) {
if (!currentBuild.equals(b.getName()))
if (b.isDirectory())
FileUtils.deleteDirectory(b);
else
b.delete();
}
}
}
}

private void cleanCache(FileStore store, File volume, Config config) throws IOException {
OS currOs = config.getOs();
Arch currArch = config.getArch();
Expand All @@ -152,7 +184,7 @@ private void cleanCache(FileStore store, File volume, Config config) throws IOEx
for (Arch arch : Arch.values()) {
for (boolean isDebug : new boolean[] { false, true }) {
CacheDir cacheDir = constructCacheDir(volume, os, arch, isDebug);
if (cacheDir != null && !cacheDir.directory.equals(currCacheDir.directory)) {
if (cacheDir != null && (currCacheDir == null || !cacheDir.directory.equals(currCacheDir.directory))) {
cacheDirs.add(cacheDir);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import org.junit.Test;
import static org.junit.Assert.*;
import org.robovm.compiler.clazz.Clazz;
Expand Down Expand Up @@ -132,7 +128,7 @@ public void allStreamsAreClosedInCaseOfFailure() throws Exception {
}

private static Clazzes createClazzes(final Path... paths) throws Exception {
Config cfg = new Config() {
Config cfg = new Config(UUID.randomUUID()) {
};
Clazzes clazzes = new Clazzes(
cfg,
Expand Down

0 comments on commit d915b8b

Please sign in to comment.