Skip to main content

Determine the actual number of CPUs available based on systemd resource limits - more accurate than nproc in shared systems with CPU quotas

Project description

CPU Quotas (soma login nodes)

The soma login nodes are shared resources. The total CPU resources are soft-limited in order to support multiple users. This is done using systemd quotas.

Unfortunately, Python libraries and command line utilities like nproc don't know about the soft limits and provide inaccurate user CPU core values, leading to oversubscribe processes when running custom parallel scripts.

Workaround

In this repository you will find some useful scripts that get the user CPU qouta values for soma login nodes:

1. python (get_available_cpus.py)

This script supports multiple usage modes and options:

Features:

  • Reads systemd CPU quota from /run/systemd/system/user-$UID.slice.d/50-CPUQuota.conf
  • Handles both percentage format (e.g., "200%") and decimal format (e.g., "2.0")
  • Provides a conservative mode that reserves CPU for system overhead
  • Can be imported as a module in other Python scripts
  • Includes a quiet mode ideal for scripting

Usage:

# Make executable
chmod +x get_available_cpus.py

# Basic usage
./get_available_cpus.py

# Conservative mode (reserves some CPU for system)
./get_available_cpus.py --conservative

# Quiet mode (just the number)
./get_available_cpus.py --quiet

In python scripts:

from get_available_cpus import get_parallel_workers

# Use in multiprocessing
import multiprocessing as mp
num_workers = get_parallel_workers()
with mp.Pool(num_workers) as pool:
    # your parallel code here

2. bash (get_available_cpus.sh)

This is a bash version of the script above for command-line usage:

Usage:

# Make executable
chmod +x get_available_cpus.sh

# Basic usage
./get_available_cpus.sh

# Use in other scripts
NUM_CPUS=$(./get_available_cpus.sh --quiet)

3. pip installing cpu_quota

How to install:

python -m pip install cpu_quota

Usage:

from cpu_quota import get_parallel_workers
num_workers = get_parallel_workers()

Features:

  • Systemd Integration: Reads actual CPU quota limits rather than just hardware CPU count
  • Fallback: Falls back to nproc if no systemd limits are found
  • Conservative Mode: Optionally reserves CPU capacity for system overhead
  • Error Handling: Gracefully handles missing files or parsing errors
  • Flexible Output: Quiet mode for scripting, verbose mode for debugging

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpu_quota-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cpu_quota-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file cpu_quota-0.1.0.tar.gz.

File metadata

  • Download URL: cpu_quota-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cpu_quota-0.1.0.tar.gz
Algorithm Hash digest
SHA256 affe53ccc78f65974dbc1f3bc90711b89d5f343e790996e345af88254fe732ef
MD5 d133fdb13cd3a9b633d8eee98c6267d8
BLAKE2b-256 f23575899b4012cef115cfbc9e1ec3b977b204ad2d2284ec4c72577c13285c1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_quota-0.1.0.tar.gz:

Publisher: publish-package.yml on mpinb/howto-cpu-quota

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_quota-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cpu_quota-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cpu_quota-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbe5e7ae165fb8b9ad1f8d0923a41b1e285eaec13a0138fbb717cc3774a80734
MD5 b976d741b982cffcad86619800c304e3
BLAKE2b-256 ec5423e9d5c801a52d82afeb66a0c232cd01861f190aadb6c70c811f5ca1c8e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_quota-0.1.0-py3-none-any.whl:

Publisher: publish-package.yml on mpinb/howto-cpu-quota

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page