summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/Constants.java
blob: 55a87b5fd9815814befc27a54bdc1672796c6b88 (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
package org.openslx.bwlp.sat.util;

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

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

public class Constants {

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

	public static final String INCOMPLETE_UPLOAD_SUFFIX = ".part";
	public static final int MAX_UPLOADS;
	public static final int MAX_DOWNLOADS;
	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

	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");

		MAX_UPLOADS = (int) Math.max(1, (maxMem - 64) / (FileChunk.CHUNK_SIZE_MIB * 2));
		MAX_DOWNLOADS = MAX_UPLOADS * 2;
	}
}