Skip to content

Commit

Permalink
Set max.workers for Gradle to fit for the selected workspace class
Browse files Browse the repository at this point in the history
  • Loading branch information
meysholdt committed Jun 3, 2024
1 parent 34a7fc3 commit f5c5e26
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions chunks/lang-java/99-gradle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# This script calculates the optimal number of Gradle workers for a Gitpod workspace.
# It uses `gp top` to detect the number of available virtual CPUs, as Gitpod workspaces run in containers
# and only a subset of the CPU cores known to the Linux kernel are usable.

# Gitpod Cloud:
# If a workspace is described as "up to X cores", dynamic CPU limiting is active.
# The script takes the number of cores from the description and divides it by two, since .resources.cpu.limit
# represents the current limit and depends on CPU utilization.

# Gitpod Enterprise:
# The script uses the value of .resources.cpu.limit, which represents the static CPU limit.

# In any case, the number of workers is set to (core - 2) to leave some capacity for IDE backend processes.

json=$(gp top --json)
description=$(echo "$json" | jq -r '.workspace_class.description')
upto=$(echo "$description" | grep -oP '(?<=Up to )\d+(?= cores)' || echo "")
limit=$(echo "$json" | jq '.resources.cpu.limit / 1000')

if [[ -z "$upto" ]]; then
vcpus=$((limit))
else
vcpus=$((upto / 2))
fi

# Leave 2 vCPUs for other processes, such as IDE backends
workers=$((vcpus - 2))

# Ensure at least one worker, even in small workspaces
workers=$((workers < 1 ? 1 : workers))

# Set an environment variable to configure Gradle
export GRADLE_OPTS="-Dorg.gradle.workers.max=$workers"

# Output the configuration details
# printf "upto: $upto\nlimit: $limit\nvcpu: $vcpus\nworkers: $workers\nGRADLE_OPTS=$GRADLE_OPTS\n"
1 change: 1 addition & 0 deletions chunks/lang-java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ RUN curl -fsSL "https://get.sdkman.io" | bash \
&& echo '[[ -s \"/home/gitpod/.sdkman/bin/sdkman-init.sh\" ]] && source \"/home/gitpod/.sdkman/bin/sdkman-init.sh\"' >> /home/gitpod/.bashrc.d/99-java"
# above, we are adding the sdkman init to .bashrc (executing sdkman-init.sh does that), because one is executed on interactive shells, the other for non-interactive shells (e.g. plugin-host)
ENV GRADLE_USER_HOME=/workspace/.gradle/
COPY --chown=gitpod:gitpod 99-gradle.sh /home/gitpod/.bashrc.d/99-gradle.sh

0 comments on commit f5c5e26

Please sign in to comment.