Skip to content

Commit 11d7dd6

Browse files
committed
Better detection of available system resources for decompilation and allow certain tasks to be marked such that nothing else runs concurrently
1 parent c720fb5 commit 11d7dd6

File tree

9 files changed

+94
-8
lines changed

9 files changed

+94
-8
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ dependencies {
254254
}
255255
}
256256
modelApi libs.gson
257+
modelImplementation libs.oshi.core
257258
modelCompileOnlyApi cLibs.jspecify
258259
modelCompileOnly cLibs.bundles.compileonly
259260
modelAnnotationProcessor cLibs.bundles.annotationprocessor
261+
modelApi libs.forkedtaskexecutor
260262

261263
signaturesApi libs.asm
262264
signaturesCompileOnly cLibs.bundles.compileonly
@@ -276,7 +278,6 @@ dependencies {
276278
requireFeature('runner')
277279
}
278280
}
279-
modelApi libs.forkedtaskexecutor
280281

281282
instrumentationImplementation libs.asm
282283
}

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ asm = "9.7"
77
mappingio = "0.7.0"
88
feather = "1.1.0"
99
gson = "2.11.0"
10+
oshi = "6.6.5"
1011

1112
[libraries]
1213

@@ -24,3 +25,5 @@ feather-core = { group = "org.parchmentmc", name = "feather", version.ref = "fea
2425
feather-gson = { group = "org.parchmentmc.feather", name = "io-gson", version.ref = "feather" }
2526

2627
gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
28+
29+
oshi-core = { group = "com.github.oshi", name = "oshi-core", version.ref = "oshi" }

settings.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ dependencyResolutionManagement {
2626
includeGroupAndSubgroups 'org.parchmentmc'
2727
}
2828
}
29+
maven {
30+
name 'Staging'
31+
url 'https://maven.lukebemish.dev/staging/'
32+
}
2933
}
3034

3135
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS

src/main/java/dev/lukebemish/taskgraphrunner/runtime/Task.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,12 @@ private void execute(Context context) {
322322
// Something was not up-to-date -- so we run everything
323323
LOGGER.info("Starting task `{}`.", name);
324324
try {
325-
if (parallelism == null) {
326-
run(context);
327-
} else {
328-
context.lockManager().enforcedParallelism(context, parallelism, () -> run(context));
325+
try (var ignored = LockManager.heavyLightLock(parallelism)) {
326+
if (parallelism == null) {
327+
run(context);
328+
} else {
329+
context.lockManager().enforcedParallelism(context, parallelism, () -> run(context));
330+
}
329331
}
330332
boolean nothingChanged = true;
331333
for (var output : outputTypes().keySet()) {

src/main/java/dev/lukebemish/taskgraphrunner/runtime/util/LockManager.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class LockManager {
3737

3838
private static final Map<String, String> parallelismGroups;
3939

40+
private static final Semaphore heavyLock = new Semaphore(Integer.MAX_VALUE, true);
41+
4042
static {
4143
String groupsProperty = System.getProperty("dev.lukebemish.taskgraphrunner.parallelism.groups", "");
4244
Map<String, String> map = new LinkedHashMap<>();
@@ -53,6 +55,29 @@ public class LockManager {
5355
parallelismGroups = map;
5456
}
5557

58+
public static boolean isHeavy(String key) {
59+
if (parallelismGroups.containsKey(key)) {
60+
if (isHeavy(parallelismGroups.get(key))) {
61+
return true;
62+
}
63+
}
64+
return Boolean.getBoolean("dev.lukebemish.taskgraphrunner.parallelism."+key+".heavy");
65+
}
66+
67+
public static LockLike heavyLightLock(String key) {
68+
try {
69+
if (isHeavy(key)) {
70+
heavyLock.acquire(Integer.MAX_VALUE);
71+
return new HeavyLock(heavyLock);
72+
} else {
73+
heavyLock.acquire();
74+
return new LightLock(heavyLock);
75+
}
76+
} catch (InterruptedException e) {
77+
throw new RuntimeException(e);
78+
}
79+
}
80+
5681
private static int findParallelism(String key) {
5782
if (parallelismGroups.containsKey(key)) {
5883
int maxInt = 0;
@@ -269,6 +294,30 @@ public sealed interface LockLike extends AutoCloseable {
269294
void close();
270295
}
271296

297+
public static final class LightLock implements LockLike {
298+
private final Semaphore semaphore;
299+
private LightLock(Semaphore semaphore) {
300+
this.semaphore = semaphore;
301+
}
302+
303+
@Override
304+
public void close() {
305+
semaphore.release();
306+
}
307+
}
308+
309+
public static final class HeavyLock implements LockLike {
310+
private final Semaphore semaphore;
311+
private HeavyLock(Semaphore semaphore) {
312+
this.semaphore = semaphore;
313+
}
314+
315+
@Override
316+
public void close() {
317+
semaphore.release(Integer.MAX_VALUE);
318+
}
319+
}
320+
272321
public static final class Locks implements LockLike {
273322
private final List<? extends LockLike> locks;
274323

src/model/java/dev/lukebemish/taskgraphrunner/model/conversion/NeoFormGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public static Config convert(Path neoFormConfig, Value selfReference, Options op
339339
args = toolModel.args;
340340
}
341341
if (isVineflower(function) && function.args().stream().noneMatch(s -> s.startsWith("-thr="))) {
342-
args.add(processArgument(downloadInputs, "-thr="+Runtime.getRuntime().availableProcessors(), step, source, listLibrariesName));
342+
args.add(processArgument(downloadInputs, "-thr="+SystemSpecsFinder.recommendedThreads(), step, source, listLibrariesName));
343343
}
344344
for (var arg : function.args()) {
345345
args.add(processArgument(downloadInputs, arg, step, source, listLibrariesName));
@@ -552,6 +552,9 @@ private static Argument processArgument(Map<String, Input> inputs, String arg, N
552552
if (arg.startsWith("-Xmx")) {
553553
var rest = arg.substring(4);
554554
var systemProp = "dev.lukebemish.taskgraphrunner."+step.type()+".maxHeap";
555+
if (isVineflower(function)) {
556+
rest = String.valueOf(SystemSpecsFinder.recommendedMemory());
557+
}
555558
return new Argument.Untracked("-Xmx{}", new InputValue.DirectInput(new Value.SystemPropertyValue(systemProp, rest)));
556559
} else if (arg.startsWith("-Xms")) {
557560
var rest = arg.substring(4);

src/model/java/dev/lukebemish/taskgraphrunner/model/conversion/SingleVersionGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ public static Config convert(String version, Options options) throws IOException
341341
var decompileTask = new TaskModel.Tool(
342342
"decompile",
343343
List.of(
344-
new Argument.Untracked("-Xmx{}", new InputValue.DirectInput(new Value.SystemPropertyValue("dev.lukebemish.taskgraphrunner.decompile.maxHeap", "4G"))),
344+
new Argument.Untracked("-Xmx{}", new InputValue.DirectInput(new Value.SystemPropertyValue("dev.lukebemish.taskgraphrunner.decompile.maxHeap", String.valueOf(SystemSpecsFinder.recommendedMemory())))),
345345
Argument.direct("-jar"),
346346
new Argument.FileInput(null, new Input.DirectInput(Value.tool("vineflower")), PathSensitivity.NONE),
347-
new Argument.Untracked("-thr={}", new InputValue.DirectInput(new Value.SystemPropertyValue("dev.lukebemish.taskgraphrunner.decompile.maxThreads", String.valueOf(Runtime.getRuntime().availableProcessors())))),
347+
new Argument.Untracked("-thr={}", new InputValue.DirectInput(new Value.SystemPropertyValue("dev.lukebemish.taskgraphrunner.decompile.maxThreads", String.valueOf(SystemSpecsFinder.recommendedThreads())))),
348348
Argument.direct("--decompile-inner"),
349349
Argument.direct("--remove-bridge"),
350350
Argument.direct("--decompile-generics"),
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dev.lukebemish.taskgraphrunner.model.conversion;
2+
3+
import oshi.SystemInfo;
4+
5+
class SystemSpecsFinder {
6+
static long recommendedThreads() {
7+
return Runtime.getRuntime().availableProcessors() / 2;
8+
}
9+
10+
static long recommendedMemory() {
11+
return Math.min(TOTAL_MEMORY / 5, GIGABYTE * 4);
12+
}
13+
14+
private static final long TOTAL_MEMORY;
15+
16+
private static final long GIGABYTE = 1024 * 1024 * 1024;
17+
18+
static {
19+
var systemInfo = new SystemInfo();
20+
var hardware = systemInfo.getHardware();
21+
TOTAL_MEMORY = hardware.getMemory().getTotal();
22+
}
23+
}

src/model/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
requires com.google.gson;
33
requires static org.jspecify;
44
requires dev.lukebemish.forkedtaskexecutor;
5+
requires com.github.oshi;
56

67
opens dev.lukebemish.taskgraphrunner.model.conversion to com.google.gson;
78
opens dev.lukebemish.taskgraphrunner.model to com.google.gson;

0 commit comments

Comments
 (0)