-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings-start.sh
executable file
·203 lines (174 loc) · 6.58 KB
/
settings-start.sh
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/sh
# There's no need to run as root, so don't allow it, for security reasons
if [ "$USER" = "root" ]; then
echo "Please switch to a non-root user before running this script."
exit
fi
# Validate Java is installed and the minimum version is available
MIN_JAVA_VER='11'
if command -v java > /dev/null 2>&1; then
# Extract Java version
version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ "$version" == 1.* ]]; then
# Java version 1.x (Java 8 or earlier)
version_major=$(echo "$version" | awk -F '.' '{print $2}')
else
# Java version 9 or higher
version_major=$(echo "$version" | awk -F '.' '{print $1}')
fi
if [ "$version_major" -ge "$MIN_JAVA_VER" ]; then
echo "Passed Java version check (version $version)"
else
echo "Please upgrade your Java to version ${MIN_JAVA_VER} or greater"
exit 1
fi
else
echo "Java is not available, please install Java ${MIN_JAVA_VER} or greater"
exit 1
fi
# No qortal.jar but we have a Maven built one?
# Be helpful and copy across to correct location
if [ ! -e qortal.jar ] && [ -f target/qortal*.jar ]; then
echo "Copying Maven-built Qortal JAR to correct pathname"
cp target/qortal*.jar qortal.jar
fi
# Detect total RAM in MB
RAM_MB=$(awk '/MemTotal/ { printf "%.0f", $2/1024 }' /proc/meminfo)
echo "Detected total RAM: ${RAM_MB} MB"
# Set default JVM parameters based on RAM
if [ "$RAM_MB" -lt 2048 ]; then
# Less than 2 GB RAM
DEFAULT_XMS="512m"
DEFAULT_XMX="1g"
DEFAULT_GC="-XX:+UseSerialGC"
elif [ "$RAM_MB" -lt 4096 ]; then
# 2 GB to 4 GB RAM
DEFAULT_XMS="1g"
DEFAULT_XMX="2g"
DEFAULT_GC="-XX:+UseSerialGC"
elif [ "$RAM_MB" -lt 8192 ]; then
# 4 GB to 8 GB RAM
DEFAULT_XMS="2g"
DEFAULT_XMX="4g"
DEFAULT_GC="-XX:+UseG1GC"
elif [ "$RAM_MB" -lt 16384 ]; then
# 8 GB to 16 GB RAM
DEFAULT_XMS="4g"
DEFAULT_XMX="6g"
DEFAULT_GC="-XX:+UseG1GC"
else
# More than 16 GB RAM
if [ "$version_major" -ge 15 ]; then
# Java 15 or higher, use ZGC
DEFAULT_XMS="6g"
DEFAULT_XMX="8g"
DEFAULT_GC="-XX:+UseZGC"
else
# Java version less than 15, use G1GC
DEFAULT_XMS="6g"
DEFAULT_XMX="8g"
DEFAULT_GC="-XX:+UseG1GC"
fi
fi
# Set Metaspace size
DEFAULT_METASPACE_SIZE="256m"
DEFAULT_MAX_METASPACE_SIZE="512m"
# Set Young Generation size
DEFAULT_NEW_SIZE=""
DEFAULT_MAX_NEW_SIZE=""
# GC Logging parameters
GC_LOGGING_PARAMS="-Xlog:gc*:file=./logs/gc.log:time,uptime:filecount=10,filesize=50M"
# OutOfMemoryError handling
OOM_ERROR_PARAMS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./logs/heap_dump.hprof"
# Additional parameter, not working?
# -XX:OnOutOfMemoryError=\"kill -9 %p\""
# String deduplication and optimization
STRING_OPTS="-XX:+UseStringDeduplication -XX:+OptimizeStringConcat"
# Additional JVM parameters
ADDITIONAL_JVM_ARGS="-XX:+UseCompressedOops -Djava.net.preferIPv4Stack=false"
# Combine default JVM arguments
DEFAULT_JVM_ARGS="-Xms${DEFAULT_XMS} -Xmx${DEFAULT_XMX} ${DEFAULT_GC} -XX:MetaspaceSize=${DEFAULT_METASPACE_SIZE} -XX:MaxMetaspaceSize=${DEFAULT_MAX_METASPACE_SIZE} ${GC_LOGGING_PARAMS} ${OOM_ERROR_PARAMS} ${STRING_OPTS} ${ADDITIONAL_JVM_ARGS}"
echo "Default JVM settings:"
echo "${DEFAULT_JVM_ARGS}"
# Prompt user to accept default or input custom settings
read -p "Do you want to use these JVM settings? [Y/n]: " RESPONSE
if [ "$RESPONSE" = "n" ] || [ "$RESPONSE" = "N" ]; then
# Prompt for custom heap sizes
read -p "Enter initial heap size (e.g., 2g for 2 GB) [Default: ${DEFAULT_XMS}]: " CUSTOM_XMS
read -p "Enter maximum heap size (e.g., 4g for 4 GB) [Default: ${DEFAULT_XMX}]: " CUSTOM_XMX
CUSTOM_XMS=${CUSTOM_XMS:-$DEFAULT_XMS}
CUSTOM_XMX=${CUSTOM_XMX:-$DEFAULT_XMX}
# Prompt for Garbage Collector
echo "Select Garbage Collector:"
echo "1) Serial GC (-XX:+UseSerialGC)"
echo "2) Parallel GC (-XX:+UseParallelGC)"
echo "3) G1 GC (-XX:+UseG1GC)"
if [ "$version_major" -ge 15 ]; then
echo "4) ZGC (-XX:+UseZGC)"
fi
read -p "Enter choice [Default: ${DEFAULT_GC}]: " GC_CHOICE
case $GC_CHOICE in
1)
CUSTOM_GC="-XX:+UseSerialGC"
;;
2)
CUSTOM_GC="-XX:+UseParallelGC"
;;
3)
CUSTOM_GC="-XX:+UseG1GC"
;;
4)
if [ "$version_major" -ge 15 ]; then
CUSTOM_GC="-XX:+UseZGC"
else
echo "ZGC is not available for your Java version. Using default GC."
CUSTOM_GC="${DEFAULT_GC}"
fi
;;
*)
CUSTOM_GC="${DEFAULT_GC}"
;;
esac
# Prompt for Metaspace sizes
read -p "Enter Metaspace size [Default: ${DEFAULT_METASPACE_SIZE}]: " CUSTOM_METASPACE_SIZE
read -p "Enter Max Metaspace size [Default: ${DEFAULT_MAX_METASPACE_SIZE}]: " CUSTOM_MAX_METASPACE_SIZE
CUSTOM_METASPACE_SIZE=${CUSTOM_METASPACE_SIZE:-$DEFAULT_METASPACE_SIZE}
CUSTOM_MAX_METASPACE_SIZE=${CUSTOM_MAX_METASPACE_SIZE:-$DEFAULT_MAX_METASPACE_SIZE}
# Prompt for GC Logging
read -p "Enable GC Logging? [Y/n, Default: Enabled]: " GC_LOGGING_RESPONSE
if [ "$GC_LOGGING_RESPONSE" = "n" ] || [ "$GC_LOGGING_RESPONSE" = "N" ]; then
CUSTOM_GC_LOGGING_PARAMS=""
else
CUSTOM_GC_LOGGING_PARAMS="${GC_LOGGING_PARAMS}"
fi
# Prompt for OutOfMemoryError handling
read -p "Enable Heap Dump on OutOfMemoryError? [Y/n, Default: Enabled]: " OOM_RESPONSE
if [ "$OOM_RESPONSE" = "n" ] || [ "$OOM_RESPONSE" = "N" ]; then
CUSTOM_OOM_ERROR_PARAMS=""
else
CUSTOM_OOM_ERROR_PARAMS="${OOM_ERROR_PARAMS}"
fi
# Prompt for String Optimization
read -p "Enable String Deduplication and Optimization? [Y/n, Default: Enabled]: " STRING_OPT_RESPONSE
if [ "$STRING_OPT_RESPONSE" = "n" ] || [ "$STRING_OPT_RESPONSE" = "N" ]; then
CUSTOM_STRING_OPTS=""
else
CUSTOM_STRING_OPTS="${STRING_OPTS}"
fi
# Combine custom JVM arguments
JVM_ARGS="-Xms${CUSTOM_XMS} -Xmx${CUSTOM_XMX} ${CUSTOM_GC} -XX:MetaspaceSize=${CUSTOM_METASPACE_SIZE} -XX:MaxMetaspaceSize=${CUSTOM_MAX_METASPACE_SIZE} ${CUSTOM_GC_LOGGING_PARAMS} ${CUSTOM_OOM_ERROR_PARAMS} ${CUSTOM_STRING_OPTS} ${ADDITIONAL_JVM_ARGS}"
echo "User-defined JVM settings:"
echo "${JVM_ARGS}"
else
JVM_ARGS="${DEFAULT_JVM_ARGS}"
fi
# Create logs directory if not exists
mkdir -p logs
# Start the Qortal Core application
nohup nice -n 20 java \
${JVM_ARGS} \
-jar qortal.jar \
1>logs/run.log 2>&1 &
# Save backgrounded process's PID
echo $! > run.pid
echo "Qortal is running as PID $!"