summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Constants.java
blob: ee1da84e320ca942b9016af0c04c11ee9f9db6c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.openslx.bwlp.sat.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openslx.filetransfer.util.FileChunk;

public class Constants {

	private static final Logger LOGGER = LogManager.getLogger(Constants.class);

	public static final String INCOMPLETE_UPLOAD_SUFFIX = ".upload.partial";
	public static final int MAX_UPLOADS;
	public static final int MAX_UPLOADS_PER_USER;
	public static final int MAX_DOWNLOADS;
	public static final int MAX_CONNECTIONS_PER_TRANSFER;
	public static final int MAX_MASTER_UPLOADS = 2;
	public static final int MAX_MASTER_DOWNLOADS = 3;
	public static final int TRANSFER_TIMEOUT = 15 * 1000; // 15s

	public static final int HASHCHECK_QUEUE_LEN;

	static {
		long maxMem = Runtime.getRuntime().maxMemory();
		if (maxMem == Long.MAX_VALUE) {
			// Apparently the JVM was started without a memory limit (no -Xmx cmdline),
			// so we try a dirty little trick by assuming this is linux and reading it
			// from the /proc file system. If that fails too, assume a default of 512MB
			try (BufferedReader br = new BufferedReader(new FileReader("/proc/meminfo"))) {
				for (String line; (line = br.readLine()) != null;) {
					if (line.startsWith("MemTotal:") && line.endsWith("kB")) {
						String string = line.replaceAll("[^0-9]", "");
						try {
							maxMem = (Long.parseLong(string) / 2l) * 1024l;
							LOGGER.debug("Guessing usable JVM memory via /proc/meminfo");
						} catch (Exception e) {
						}
						break;
					}
				}
			} catch (IOException e) {
			}
			if (maxMem == Long.MAX_VALUE) {
				maxMem = 512l * 1024l * 1024l;
			}
		}
		maxMem /= 1024l * 1024l;
		// Now maxMem is the amount of memory in MiB
		LOGGER.debug("Maximum JVM memory: " + maxMem + "MiB");

		int cpuCount = Runtime.getRuntime().availableProcessors();
		int hashQueueLen = (int) (maxMem / 100);
		if (hashQueueLen < 1) {
			hashQueueLen = 1;
		} else if (hashQueueLen > 6) {
			hashQueueLen = 6;
		}
		int maxPerTransfer = (int) Math.max(1, (maxMem - 400) / (FileChunk.CHUNK_SIZE_MIB * 8));
		if (maxPerTransfer > 4) {
			maxPerTransfer = 4;
		}
		if (maxPerTransfer > cpuCount) {
			maxPerTransfer = cpuCount;
		}
		int maxUploads = (int) Math.max(1, (maxMem - 64) / (FileChunk.CHUNK_SIZE_MIB * (hashQueueLen + 1)));
		if (maxUploads > cpuCount * 4) {
			maxUploads = cpuCount * 4;
		}

		MAX_CONNECTIONS_PER_TRANSFER = maxPerTransfer;
		MAX_UPLOADS = maxUploads;
		MAX_DOWNLOADS = MAX_UPLOADS * 2;
		MAX_UPLOADS_PER_USER = Math.min(MAX_UPLOADS / 2, 4);
		HASHCHECK_QUEUE_LEN = hashQueueLen;
	}
}