Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose cpu quota #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 0 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,13 @@ git remote add upstream https://github.com/uber-go/automaxprocs.git
git fetch upstream
```

Install the test dependencies:

```
make dependencies
```

Make sure that the tests and the linters pass:

```
make test
make lint
```

If you're not using the minor version of Go specified in the Makefile's
`LINTABLE_MINOR_VERSIONS` variable, `make lint` doesn't do anything. This is
fine, but it means that you'll only discover lint failures after you open your
pull request.

## Making Changes

Start by creating a new branch for your changes:
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ $(STATICCHECK):

.PHONY: lint
lint: $(GOLINT) $(STATICCHECK)
# avoid `go vet` output going to lint.log on fresh clone
@go build ./...
@go test ./...
@rm -rf lint.log
@echo "Checking gofmt"
@gofmt -d -s $(GO_FILES) 2>&1 | tee lint.log
Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# automaxprocs [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov]

Automatically set `GOMAXPROCS` to match Linux container CPU quota.
# automaxprocs

## Installation

`go get -u go.uber.org/automaxprocs`
Importing the base package will automatically set `GOMAXPROCS` to match Linux container
CPU quota.

## Quick Start

To automatically set `GOMAXPROCS`:
```go
import _ "go.uber.org/automaxprocs"

Expand All @@ -16,6 +16,22 @@ func main() {
}
```

To get the current CPU quota
```go
import "go.uber.org/automaxprocs/cpuquota"

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

fmt.Println("quota:", quota)
```

## Development Status: Stable

All APIs are finalized, and no breaking changes will be made in the 1.x series
Expand All @@ -42,5 +58,3 @@ Released under the [MIT License](LICENSE).
[ci]: https://travis-ci.com/uber-go/automaxprocs
[cov-img]: https://codecov.io/gh/uber-go/automaxprocs/branch/master/graph/badge.svg
[cov]: https://codecov.io/gh/uber-go/automaxprocs


36 changes: 36 additions & 0 deletions cpuquota/cpuquota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cpuquota

import "go.uber.org/automaxprocs/internal/cgroups"

// NewCGroupsForCurrentProcess returns a new *CGroups instance for the current
// process.
func NewCGroupsForCurrentProcess() (cgroups.CGroups, error) {
return cgroups.NewCGroupsForCurrentProcess()
}

// CPUQuota returns the CPU quota applied with the CPU cgroup controller.
// It is a result of `cpu.cfs_quota_us / cpu.cfs_period_us`. If the value of
// `cpu.cfs_quota_us` was not set (-1), the method returns `(-1, nil)`.
func CPUQuota(cg cgroups.CGroups) (float64, bool, error) {
return cg.CPUQuota()
}