automaxprocs

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: MIT Imports: 2 Imported by: 0

README

libcgroups

This is a fork of github.com/uber-go/automaxprocs that exposes the existing functions necessary to get the current process' cgroups, and the cpu quota allocation from that.

Importing the base package will automatically set GOMAXPROCS to match Linux container CPU quota.

Quick Start

To automatically set GOMAXPROCS:

import _ "github.com/ovesh/automaxprocs"

func main() {
  // Your application logic here.
}

To get the current CPU quota

allCGroups, err := cgroups.NewCGroupsForCurrentProcess()
if err != nil {
	// handle err
}
quota, defined, err := allCGroups.CPUQuota()
if !defined || err != nil {
	// handle err
}

fmt.Println("quota:", quota)

Released under the MIT License.

Documentation

Overview

Package automaxprocs automatically sets GOMAXPROCS to match the Linux container CPU quota, if any.

Example
package main

// Importing automaxprocs automatically adjusts GOMAXPROCS.
import (
	"fmt"

	"github.com/ovesh/automaxprocs/internal/cgroups"

	_ "go.uber.org/automaxprocs"
)

// To render a whole-file example, we need a package-level declaration.
var _ = ""

func main() {}

func getCurrentCPUAllocation() error {
	allCGroups, err := cgroups.NewCGroupsForCurrentProcess()
	if err != nil {
		return err
	}
	fmt.Printf("all allCGroups: %+v\n", allCGroups)
	quota, defined, err := allCGroups.CPUQuota()
	if !defined || err != nil {
		return err
	}

	fmt.Println("quota:", quota)
	return nil
}
Output:

Directories

Path Synopsis
internal
cgroups
Package cgroups provides utilities to access Linux control group (CGroups) parameters (CPU quota, for example) for a given process.
Package cgroups provides utilities to access Linux control group (CGroups) parameters (CPU quota, for example) for a given process.
Package maxprocs lets Go programs easily configure runtime.GOMAXPROCS to match the configured Linux CPU quota.
Package maxprocs lets Go programs easily configure runtime.GOMAXPROCS to match the configured Linux CPU quota.

Jump to

Keyboard shortcuts

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