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 functionsmaster
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 |
||||
} |
@ -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 |
||||
} |
@ -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) |
||||
} |
||||
} |
||||
} |
@ -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) |
||||
} |
@ -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) |
||||
} |
@ -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) |
||||
} |
||||
} |
@ -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 |
||||
} |
@ -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) |
||||
} |
@ -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) |
||||
} |
@ -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) |
||||
} |
@ -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) |
||||
} |
||||
} |
@ -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 |
||||
} |
Loading…
Reference in new issue