uos

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCPUs

func GetCPUs() int

GetCPUs calculates and returns the number of CPU cores available to the application. This function is designed to provide a more accurate count of available CPUs, especially when running in a containerized environment such as Docker or Kubernetes on Linux, where CPU resources can be limited.

The function operates as follows:

  1. Checks the operating system where the application is running. If it's Linux, it proceeds to check the cgroup settings that are commonly used in containerized environments to limit CPU resources.

  2. For Linux: a. Attempts to read the CPU allocation from the cgroup settings, specifically the cpu.cfs_quota_us and cpu.cfs_period_us files, which define the CPU quota and period for the container. b. If the cgroup CPU quota and period can be successfully read, it calculates the number of CPUs as the quotient of the quota and period. This gives a decimal representation of the number of CPU cores allocated to the container. This value is then returned as an integer. c. If the cgroup files are not accessible or an error occurs while reading them (which might happen if the application is not running in a containerized environment), the error is ignored, and the function falls back to the next step.

  3. If the operating system is not Linux or if the cgroup CPU information could not be obtained, the function falls back to using runtime.NumCPU(). This standard Go library function returns the number of logical CPUs available to the current process, as seen by the Go runtime. This count reflects the total number of CPU cores available to the process, which may be the total number of cores on the machine or limited by the OS scheduler, depending on the environment and OS settings.

Return value:

  • The function returns an integer representing the number of CPU cores that the application should consider available. This number is either derived from the cgroup settings (in containerized Linux environments) or from the runtime information provided by the Go runtime.NumCPU() function (in non-containerized environments or non-Linux operating systems).

Note:

  • This function is particularly useful in containerized environments where CPU resources might be limited and different from the physical host's total CPU resources. By accounting for these limits, applications can make more informed decisions about resource allocation, concurrency, and parallel processing.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL