Add method for getting value of cgroup v2 interface file
@@ -59,6 +59,52 @@ getconfig() {
eval "$cmds"
}
+# Print the value of the cgroups v2 interface filename $1 for current process,
+# returning 0 on success. $1 is e.g. memory.max.
+vespa_cg2get() {
+ local filename="$1"
+
+ # Verify cgroups v2
+ if ! [ -e /sys/fs/cgroup/cgroup.controllers ]; then
+ return 1
+ fi
+
+ local cgroup_content
+ if ! cgroup_content=$(< /proc/self/cgroup); then
+ echo "No such file: /proc/self/cgroup" >& 2
+ return 1
+ fi
+
+ local slice
+ while read -r; do
+ if [ -n "$slice" ]; then
+ echo "More than one line in /proc/self/cgroup" >&2
+ return 1
+ fi
+ # Ignore prefix of line up to and including the right-most ':'.
+ # Example line: "0::/user.slice/user-1002.slice/session-29.scope"
+ slice="${REPLY##*:}"
+ done <<< "$cgroup_content"
+
+ local root_dir=/sys/fs/cgroup
+ local leaf_dir="$root_dir$slice"
+ local current_dir="$leaf_dir"
+
+ while true; do
+ local path="$current_dir"/"$filename"
+ if [ -r "$path" ]; then
+ cat "$path"
+ return 0
+ fi
+
+ current_dir="${current_dir%/*}"
+ if (( ${#current_dir} < ${#root_dir} )); then
+ echo "No such filename was found at $leaf_dir: $filename" >&2
+ return 1
+ fi
+ done
+}
+
configure_memory() {
consider_fallback jvm_minHeapsize 1536
consider_fallback jvm_heapsize 1536
