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:
Bala FA
2017-03-02 11:21:57 +05:30
committed by Harshavardhana
parent 09e9fd745c
commit 480ea826dc
20 changed files with 509 additions and 273 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
View 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
View 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)
}
}
}