mirror of
https://github.com/minio/minio.git
synced 2025-01-11 15:03:22 -05:00
Move rlimit functions into sys package. (#3824)
This patch addresses below * go build works for bsd family * probe total RAM size for bsd family * make unit testable functions
This commit is contained in:
parent
09e9fd745c
commit
480ea826dc
@ -1,93 +0,0 @@
|
||||
// +build !windows,!plan9,!openbsd
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/minio/minio/pkg/sys"
|
||||
)
|
||||
|
||||
// For all unixes we need to bump allowed number of open files to a
|
||||
// higher value than its usual default of '1024'. The reasoning is
|
||||
// that this value is too small for a server.
|
||||
func setMaxOpenFiles() error {
|
||||
var rLimit syscall.Rlimit
|
||||
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the current limit to Max, it is usually around 4096.
|
||||
// TO increase this limit further user has to manually edit
|
||||
// `/etc/security/limits.conf`
|
||||
rLimit.Cur = rLimit.Max
|
||||
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
||||
}
|
||||
|
||||
// Set max memory used by minio as a process, this value is usually
|
||||
// set to 'unlimited' but we need to validate additionally to verify
|
||||
// if any hard limit is set by the user, in such a scenario would need
|
||||
// to reset the global max cache size to be 80% of the hardlimit set
|
||||
// by the user. This is done to honor the system limits and not crash.
|
||||
func setMaxMemory() error {
|
||||
var rLimit syscall.Rlimit
|
||||
err := syscall.Getrlimit(syscall.RLIMIT_AS, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the current limit to Max, it is default 'unlimited'.
|
||||
// TO decrease this limit further user has to manually edit
|
||||
// `/etc/security/limits.conf`
|
||||
rLimit.Cur = rLimit.Max
|
||||
err = syscall.Setrlimit(syscall.RLIMIT_AS, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = syscall.Getrlimit(syscall.RLIMIT_AS, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// If current rlimit is less than minRAMSize, do not set globalMaxCacheSize.
|
||||
if rLimit.Cur < minRAMSize {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get total RAM.
|
||||
stats, err := sys.GetStats()
|
||||
if err != nil {
|
||||
// Ignore sys.ErrNotImplemented error.
|
||||
if err == sys.ErrNotImplemented {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Set 50% of current rlimit or total RAM to globalMaxCacheSize.
|
||||
if stats.TotalRAM >= minRAMSize {
|
||||
if rLimit.Cur < stats.TotalRAM {
|
||||
globalMaxCacheSize = rLimit.Cur / 2
|
||||
} else {
|
||||
globalMaxCacheSize = stats.TotalRAM / 2
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
// +build openbsd
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"github.com/minio/minio/pkg/sys"
|
||||
)
|
||||
|
||||
// For all unixes we need to bump allowed number of open files to a
|
||||
// higher value than its usual default of '1024'. The reasoning is
|
||||
// that this value is too small for a server.
|
||||
func setMaxOpenFiles() error {
|
||||
var rLimit syscall.Rlimit
|
||||
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the current limit to Max, it is usually around 4096.
|
||||
// TO increase this limit further user has to manually edit
|
||||
// `/etc/security/limits.conf`
|
||||
rLimit.Cur = rLimit.Max
|
||||
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
||||
}
|
||||
|
||||
// Set max memory used by minio as a process, this value is usually
|
||||
// set to 'unlimited' but we need to validate additionally to verify
|
||||
// if any hard limit is set by the user, in such a scenario would need
|
||||
// to reset the global max cache size to be 80% of the hardlimit set
|
||||
// by the user. This is done to honor the system limits and not crash.
|
||||
func setMaxMemory() error {
|
||||
var rLimit syscall.Rlimit
|
||||
err := syscall.Getrlimit(syscall.RLIMIT_DATA, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the current limit to Max, it is default 'unlimited'.
|
||||
// TO decrease this limit further user has to manually edit
|
||||
// `/etc/security/limits.conf`
|
||||
rLimit.Cur = rLimit.Max
|
||||
err = syscall.Setrlimit(syscall.RLIMIT_DATA, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = syscall.Getrlimit(syscall.RLIMIT_DATA, &rLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// If current rlimit is less than minRAMSize, do not set globalMaxCacheSize.
|
||||
if rLimit.Cur < minRAMSize {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get total RAM.
|
||||
stats, err := sys.GetStats()
|
||||
if err != nil {
|
||||
// Ignore sys.ErrNotImplemented error.
|
||||
if err == sys.ErrNotImplemented {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Set 50% of current rlimit or total RAM to globalMaxCacheSize.
|
||||
if stats.TotalRAM >= minRAMSize {
|
||||
if rLimit.Cur < stats.TotalRAM {
|
||||
globalMaxCacheSize = rLimit.Cur / 2
|
||||
} else {
|
||||
globalMaxCacheSize = stats.TotalRAM / 2
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import "github.com/minio/minio/pkg/sys"
|
||||
|
||||
func setMaxOpenFiles() error {
|
||||
// Golang uses Win32 file API (CreateFile, WriteFile, ReadFile,
|
||||
// CloseHandle, etc.), then you don't have a limit on open files
|
||||
// (well, you do but it is based on your resources like memory).
|
||||
return nil
|
||||
}
|
||||
|
||||
func setMaxMemory() error {
|
||||
// Get total RAM.
|
||||
stats, err := sys.GetStats()
|
||||
if err != nil {
|
||||
// Ignore sys.ErrNotImplemented error.
|
||||
if err == sys.ErrNotImplemented {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Set 50% of total RAM to globalMaxCacheSize.
|
||||
if stats.TotalRAM >= minRAMSize {
|
||||
globalMaxCacheSize = stats.TotalRAM / 2
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
73
cmd/server-rlimit.go
Normal file
73
cmd/server-rlimit.go
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/minio/minio/pkg/sys"
|
||||
)
|
||||
|
||||
func setMaxOpenFiles() error {
|
||||
_, maxLimit, err := sys.GetMaxOpenFileLimit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sys.SetMaxOpenFileLimit(maxLimit, maxLimit)
|
||||
}
|
||||
|
||||
func getMaxCacheSize(curLimit, totalRAM uint64) (cacheSize uint64) {
|
||||
// Return zero if current limit or totalTAM is less than minRAMSize.
|
||||
if curLimit < minRAMSize || totalRAM < minRAMSize {
|
||||
return cacheSize
|
||||
}
|
||||
|
||||
// Return 50% of current rlimit or total RAM as cache size.
|
||||
if curLimit < totalRAM {
|
||||
cacheSize = curLimit / 2
|
||||
} else {
|
||||
cacheSize = totalRAM / 2
|
||||
}
|
||||
|
||||
return cacheSize
|
||||
}
|
||||
|
||||
func setMaxMemory() error {
|
||||
// Get max memory limit
|
||||
_, maxLimit, err := sys.GetMaxMemoryLimit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set max memory limit as current memory limit.
|
||||
if err = sys.SetMaxMemoryLimit(maxLimit, maxLimit); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get total RAM.
|
||||
stats, err := sys.GetStats()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// In some OS like windows, maxLimit is zero. Set total RAM as maxLimit.
|
||||
if maxLimit == 0 {
|
||||
maxLimit = stats.TotalRAM
|
||||
}
|
||||
|
||||
globalMaxCacheSize = getMaxCacheSize(maxLimit, stats.TotalRAM)
|
||||
return nil
|
||||
}
|
43
cmd/server-rlimit_test.go
Normal file
43
cmd/server-rlimit_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetMaxCacheSize(t *testing.T) {
|
||||
testCases := []struct {
|
||||
curLimit uint64
|
||||
totalRAM uint64
|
||||
expectedResult uint64
|
||||
}{
|
||||
{uint64(0), uint64(0), uint64(0)},
|
||||
{minRAMSize, uint64(0), uint64(0)},
|
||||
{uint64(0), minRAMSize, uint64(0)},
|
||||
{uint64(18446744073709551615), uint64(8115998720), uint64(0)},
|
||||
{uint64(8115998720), uint64(16115998720), uint64(0)},
|
||||
{minRAMSize, minRAMSize, uint64(4294967296)},
|
||||
{minRAMSize, uint64(16115998720), uint64(4294967296)},
|
||||
{uint64(18446744073709551615), uint64(10115998720), uint64(5057999360)},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
cacheSize := getMaxCacheSize(testCase.curLimit, testCase.totalRAM)
|
||||
if testCase.expectedResult != cacheSize {
|
||||
t.Fatalf("expected: %v, got: %v", testCase.expectedResult, cacheSize)
|
||||
}
|
||||
}
|
||||
}
|
40
pkg/sys/rlimit-file_bsd.go
Normal file
40
pkg/sys/rlimit-file_bsd.go
Normal file
@ -0,0 +1,40 @@
|
||||
// +build freebsd dragonfly
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// GetMaxOpenFileLimit - returns maximum file descriptor number that can be opened by this process.
|
||||
func GetMaxOpenFileLimit() (curLimit, maxLimit uint64, err error) {
|
||||
var rlimit syscall.Rlimit
|
||||
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err == nil {
|
||||
curLimit = uint64(rlimit.Cur)
|
||||
maxLimit = uint64(rlimit.Max)
|
||||
}
|
||||
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxOpenFileLimit - sets maximum file descriptor number that can be opened by this process.
|
||||
func SetMaxOpenFileLimit(curLimit, maxLimit uint64) error {
|
||||
rlimit := syscall.Rlimit{Cur: int64(curLimit), Max: int64(curLimit)}
|
||||
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
|
||||
}
|
38
pkg/sys/rlimit-file_nix.go
Normal file
38
pkg/sys/rlimit-file_nix.go
Normal file
@ -0,0 +1,38 @@
|
||||
// +build linux darwin openbsd netbsd
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import "syscall"
|
||||
|
||||
// GetMaxOpenFileLimit - returns maximum file descriptor number that can be opened by this process.
|
||||
func GetMaxOpenFileLimit() (curLimit, maxLimit uint64, err error) {
|
||||
var rlimit syscall.Rlimit
|
||||
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err == nil {
|
||||
curLimit = rlimit.Cur
|
||||
maxLimit = rlimit.Max
|
||||
}
|
||||
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxOpenFileLimit - sets maximum file descriptor number that can be opened by this process.
|
||||
func SetMaxOpenFileLimit(curLimit, maxLimit uint64) error {
|
||||
rlimit := syscall.Rlimit{Cur: curLimit, Max: maxLimit}
|
||||
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
|
||||
}
|
40
pkg/sys/rlimit-file_test.go
Normal file
40
pkg/sys/rlimit-file_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import "testing"
|
||||
|
||||
// Test get max open file limit.
|
||||
func TestGetMaxOpenFileLimit(t *testing.T) {
|
||||
_, _, err := GetMaxOpenFileLimit()
|
||||
if err != nil {
|
||||
t.Errorf("expected: nil, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Test set open file limit
|
||||
func TestSetMaxOpenFileLimit(t *testing.T) {
|
||||
curLimit, maxLimit, err := GetMaxOpenFileLimit()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to get max open file limit. %v", err)
|
||||
}
|
||||
|
||||
err = SetMaxOpenFileLimit(curLimit, maxLimit)
|
||||
if err != nil {
|
||||
t.Errorf("expected: nil, got: %v", err)
|
||||
}
|
||||
}
|
31
pkg/sys/rlimit-file_windows.go
Normal file
31
pkg/sys/rlimit-file_windows.go
Normal file
@ -0,0 +1,31 @@
|
||||
// +build windows
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
// GetMaxOpenFileLimit - returns maximum file descriptor number that can be opened by this process.
|
||||
func GetMaxOpenFileLimit() (curLimit, maxLimit uint64, err error) {
|
||||
// Nothing to do for windows.
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxOpenFileLimit - sets maximum file descriptor number that can be opened by this process.
|
||||
func SetMaxOpenFileLimit(curLimit, maxLimit uint64) error {
|
||||
// Nothing to do for windows.
|
||||
return nil
|
||||
}
|
38
pkg/sys/rlimit-memory_bsd.go
Normal file
38
pkg/sys/rlimit-memory_bsd.go
Normal file
@ -0,0 +1,38 @@
|
||||
// +build freebsd dragonfly
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import "syscall"
|
||||
|
||||
// GetMaxMemoryLimit - returns the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func GetMaxMemoryLimit() (curLimit, maxLimit uint64, err error) {
|
||||
var rlimit syscall.Rlimit
|
||||
if err = syscall.Getrlimit(syscall.RLIMIT_DATA, &rlimit); err == nil {
|
||||
curLimit = uint64(rlimit.Cur)
|
||||
maxLimit = uint64(rlimit.Max)
|
||||
}
|
||||
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxMemoryLimit - sets the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func SetMaxMemoryLimit(curLimit, maxLimit uint64) error {
|
||||
rlimit := syscall.Rlimit{Cur: int64(curLimit), Max: int64(maxLimit)}
|
||||
return syscall.Setrlimit(syscall.RLIMIT_DATA, &rlimit)
|
||||
}
|
38
pkg/sys/rlimit-memory_nix.go
Normal file
38
pkg/sys/rlimit-memory_nix.go
Normal file
@ -0,0 +1,38 @@
|
||||
// +build linux darwin netbsd
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import "syscall"
|
||||
|
||||
// GetMaxMemoryLimit - returns the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func GetMaxMemoryLimit() (curLimit, maxLimit uint64, err error) {
|
||||
var rlimit syscall.Rlimit
|
||||
if err = syscall.Getrlimit(syscall.RLIMIT_AS, &rlimit); err == nil {
|
||||
curLimit = rlimit.Cur
|
||||
maxLimit = rlimit.Max
|
||||
}
|
||||
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxMemoryLimit - sets the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func SetMaxMemoryLimit(curLimit, maxLimit uint64) error {
|
||||
rlimit := syscall.Rlimit{Cur: curLimit, Max: maxLimit}
|
||||
return syscall.Setrlimit(syscall.RLIMIT_AS, &rlimit)
|
||||
}
|
38
pkg/sys/rlimit-memory_openbsd.go
Normal file
38
pkg/sys/rlimit-memory_openbsd.go
Normal file
@ -0,0 +1,38 @@
|
||||
// +build openbsd
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import "syscall"
|
||||
|
||||
// GetMaxMemoryLimit - returns the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func GetMaxMemoryLimit() (curLimit, maxLimit uint64, err error) {
|
||||
var rlimit syscall.Rlimit
|
||||
if err = syscall.Getrlimit(syscall.RLIMIT_DATA, &rlimit); err == nil {
|
||||
curLimit = rlimit.Cur
|
||||
maxLimit = rlimit.Max
|
||||
}
|
||||
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxMemoryLimit - sets the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func SetMaxMemoryLimit(curLimit, maxLimit uint64) error {
|
||||
rlimit := syscall.Rlimit{Cur: curLimit, Max: maxLimit}
|
||||
return syscall.Setrlimit(syscall.RLIMIT_DATA, &rlimit)
|
||||
}
|
40
pkg/sys/rlimit-memory_test.go
Normal file
40
pkg/sys/rlimit-memory_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
import "testing"
|
||||
|
||||
// Test get max memory limit.
|
||||
func TestGetMaxMemoryLimit(t *testing.T) {
|
||||
_, _, err := GetMaxMemoryLimit()
|
||||
if err != nil {
|
||||
t.Errorf("expected: nil, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Test set memory limit
|
||||
func TestSetMaxMemoryLimit(t *testing.T) {
|
||||
curLimit, maxLimit, err := GetMaxMemoryLimit()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to get max memory limit. %v", err)
|
||||
}
|
||||
|
||||
err = SetMaxMemoryLimit(curLimit, maxLimit)
|
||||
if err != nil {
|
||||
t.Errorf("expected: nil, got: %v", err)
|
||||
}
|
||||
}
|
31
pkg/sys/rlimit-memory_windows.go
Normal file
31
pkg/sys/rlimit-memory_windows.go
Normal file
@ -0,0 +1,31 @@
|
||||
// +build windows
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
// GetMaxMemoryLimit - returns the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func GetMaxMemoryLimit() (curLimit, maxLimit uint64, err error) {
|
||||
// Nothing to do for windows.
|
||||
return curLimit, maxLimit, err
|
||||
}
|
||||
|
||||
// SetMaxMemoryLimit - sets the maximum size of the process's virtual memory (address space) in bytes.
|
||||
func SetMaxMemoryLimit(curLimit, maxLimit uint64) error {
|
||||
// Nothing to do for windows.
|
||||
return nil
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -16,11 +16,6 @@
|
||||
|
||||
package sys
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrNotImplemented - GetStats() is not implemented on bsds.
|
||||
var ErrNotImplemented = errors.New("not implemented")
|
||||
|
||||
// Stats - system statistics.
|
||||
type Stats struct {
|
||||
TotalRAM uint64 // Physical RAM size in bytes,
|
||||
|
@ -1,7 +1,7 @@
|
||||
// +build !linux,!windows,!darwin
|
||||
// +build openbsd netbsd freebsd dragonfly
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
* Minio Cloud Storage, (C) 2016,2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -18,7 +18,28 @@
|
||||
|
||||
package sys
|
||||
|
||||
// GetStats - return system statistics for windows.
|
||||
func GetStats() (stats Stats, err error) {
|
||||
return Stats{}, ErrNotImplemented
|
||||
import (
|
||||
"encoding/binary"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func getHwPhysmem() (uint64, error) {
|
||||
totalString, err := syscall.Sysctl("hw.physmem")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// syscall.sysctl() helpfully assumes the result is a null-terminated string and
|
||||
// removes the last byte of the result if it's 0 :/
|
||||
totalString += "\x00"
|
||||
|
||||
total := uint64(binary.LittleEndian.Uint64([]byte(totalString)))
|
||||
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetStats - return system statistics for bsd.
|
||||
func GetStats() (stats Stats, err error) {
|
||||
stats.TotalRAM, err = getHwPhysmem()
|
||||
return stats, err
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
// +build darwin
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
* Minio Cloud Storage, (C) 2016,2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -38,14 +38,8 @@ func getHwMemsize() (uint64, error) {
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetStats - return system statistics for windows.
|
||||
// GetStats - return system statistics for macOS.
|
||||
func GetStats() (stats Stats, err error) {
|
||||
memSize, err := getHwMemsize()
|
||||
if err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
stats = Stats{
|
||||
TotalRAM: memSize,
|
||||
}
|
||||
return stats, nil
|
||||
stats.TotalRAM, err = getHwMemsize()
|
||||
return stats, err
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
// +build linux
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
* Minio Cloud Storage, (C) 2016,2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -22,13 +22,10 @@ import "syscall"
|
||||
|
||||
// GetStats - return system statistics.
|
||||
func GetStats() (stats Stats, err error) {
|
||||
si := syscall.Sysinfo_t{}
|
||||
err = syscall.Sysinfo(&si)
|
||||
if err != nil {
|
||||
return
|
||||
var si syscall.Sysinfo_t
|
||||
if err = syscall.Sysinfo(&si); err == nil {
|
||||
stats.TotalRAM = uint64(si.Totalram)
|
||||
}
|
||||
stats = Stats{
|
||||
TotalRAM: uint64(si.Totalram),
|
||||
}
|
||||
return stats, nil
|
||||
|
||||
return stats, err
|
||||
}
|
||||
|
@ -1,4 +1,18 @@
|
||||
// +build linux darwin windows
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016,2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sys
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
// +build windows
|
||||
|
||||
/*
|
||||
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||||
* Minio Cloud Storage, (C) 2016,2017 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -44,12 +44,11 @@ type memoryStatusEx struct {
|
||||
func GetStats() (stats Stats, err error) {
|
||||
var memInfo memoryStatusEx
|
||||
memInfo.cbSize = uint32(unsafe.Sizeof(memInfo))
|
||||
mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo)))
|
||||
if mem == 0 {
|
||||
return Stats{}, syscall.GetLastError()
|
||||
if mem, _, _ := procGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&memInfo))); mem == 0 {
|
||||
err = syscall.GetLastError()
|
||||
} else {
|
||||
stats.TotalRAM = memInfo.ullTotalPhys
|
||||
}
|
||||
stats = Stats{
|
||||
TotalRAM: memInfo.ullTotalPhys,
|
||||
}
|
||||
return stats, nil
|
||||
|
||||
return stats, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user