Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion postgres-appliance/scripts/configure_spilo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import subprocess
import sys
import pwd
import math

from copy import deepcopy
from six.moves.urllib_parse import urlparse
Expand Down Expand Up @@ -522,6 +523,33 @@ def has_dual_stack():
info.sort(key=lambda x: x[0] == socket.AF_INET, reverse=not has_dual_stack())
return info[0][4][0]

def get_cpu_count() -> int:
limit = None
# Try Cgroup v2
try:
with open("/sys/fs/cgroup/cpu.max", "r") as f:
quota, period = f.read().split()
if quota != "max":
limit = float(quota) / float(period)
except Exception:
pass
# Try Cgroup v1 (if v2 failed)
if limit is None:
try:
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r") as f_q:
with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r") as f_p:
q, p = int(f_q.read()), int(f_p.read())
if q > 0: limit = q / p
except Exception:
pass
# Fallback: Process Affinity or Physical Count
if limit is None:
try:
limit = len(os.sched_getaffinity(0))
except (AttributeError, NotImplementedError):
limit = os.cpu_count() or 1

return max(1, math.ceil(limit))

def get_placeholders(provider):
placeholders = {}
Expand Down Expand Up @@ -581,7 +609,7 @@ def get_placeholders(provider):
# the env dir path is still called "wal-e.d" for backwards compatibility: many existing deployments, scripts,
# or manifests expect this path, even though wal-e itself is not used (wal-g reads env vars from here too)
placeholders.setdefault('WALG_ENV_DIR', os.path.join(placeholders['RW_DIR'], 'etc', 'wal-e.d', 'env'))
cpu_count = str(min(psutil.cpu_count(), 10))
cpu_count = str(min(get_cpu_count(), 10))
placeholders.setdefault('WALG_DOWNLOAD_CONCURRENCY', cpu_count)
placeholders.setdefault('WALG_UPLOAD_CONCURRENCY', cpu_count)
placeholders.setdefault('PAM_OAUTH2', '')
Expand Down