diff --git a/cmd/server-rlimit-nix.go b/cmd/server-rlimit-nix.go deleted file mode 100644 index b58720cbe..000000000 --- a/cmd/server-rlimit-nix.go +++ /dev/null @@ -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 -} diff --git a/cmd/server-rlimit-openbsd.go b/cmd/server-rlimit-openbsd.go deleted file mode 100644 index 79cd7c8cb..000000000 --- a/cmd/server-rlimit-openbsd.go +++ /dev/null @@ -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 -} diff --git a/cmd/server-rlimit-win.go b/cmd/server-rlimit-win.go deleted file mode 100644 index b153392be..000000000 --- a/cmd/server-rlimit-win.go +++ /dev/null @@ -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 -} diff --git a/cmd/server-rlimit.go b/cmd/server-rlimit.go new file mode 100644 index 000000000..2daf92a8b --- /dev/null +++ b/cmd/server-rlimit.go @@ -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 +} diff --git a/cmd/server-rlimit_test.go b/cmd/server-rlimit_test.go new file mode 100644 index 000000000..57b9d83e4 --- /dev/null +++ b/cmd/server-rlimit_test.go @@ -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) + } + } +} diff --git a/pkg/sys/rlimit-file_bsd.go b/pkg/sys/rlimit-file_bsd.go new file mode 100644 index 000000000..ca0c8b28f --- /dev/null +++ b/pkg/sys/rlimit-file_bsd.go @@ -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) +} diff --git a/pkg/sys/rlimit-file_nix.go b/pkg/sys/rlimit-file_nix.go new file mode 100644 index 000000000..f6940c66c --- /dev/null +++ b/pkg/sys/rlimit-file_nix.go @@ -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) +} diff --git a/pkg/sys/rlimit-file_test.go b/pkg/sys/rlimit-file_test.go new file mode 100644 index 000000000..e26cd9c24 --- /dev/null +++ b/pkg/sys/rlimit-file_test.go @@ -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) + } +} diff --git a/pkg/sys/rlimit-file_windows.go b/pkg/sys/rlimit-file_windows.go new file mode 100644 index 000000000..ed50f42d7 --- /dev/null +++ b/pkg/sys/rlimit-file_windows.go @@ -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 +} diff --git a/pkg/sys/rlimit-memory_bsd.go b/pkg/sys/rlimit-memory_bsd.go new file mode 100644 index 000000000..078577b4a --- /dev/null +++ b/pkg/sys/rlimit-memory_bsd.go @@ -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) +} diff --git a/pkg/sys/rlimit-memory_nix.go b/pkg/sys/rlimit-memory_nix.go new file mode 100644 index 000000000..797b4e187 --- /dev/null +++ b/pkg/sys/rlimit-memory_nix.go @@ -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) +} diff --git a/pkg/sys/rlimit-memory_openbsd.go b/pkg/sys/rlimit-memory_openbsd.go new file mode 100644 index 000000000..83e8d858e --- /dev/null +++ b/pkg/sys/rlimit-memory_openbsd.go @@ -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) +} diff --git a/pkg/sys/rlimit-memory_test.go b/pkg/sys/rlimit-memory_test.go new file mode 100644 index 000000000..b01385004 --- /dev/null +++ b/pkg/sys/rlimit-memory_test.go @@ -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) + } +} diff --git a/pkg/sys/rlimit-memory_windows.go b/pkg/sys/rlimit-memory_windows.go new file mode 100644 index 000000000..0a900d636 --- /dev/null +++ b/pkg/sys/rlimit-memory_windows.go @@ -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 +} diff --git a/pkg/sys/stats.go b/pkg/sys/stats.go index ea89c7ec9..87aaabad9 100644 --- a/pkg/sys/stats.go +++ b/pkg/sys/stats.go @@ -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, diff --git a/pkg/sys/stats_bsd.go b/pkg/sys/stats_bsd.go index 276d0c01b..fa6488971 100644 --- a/pkg/sys/stats_bsd.go +++ b/pkg/sys/stats_bsd.go @@ -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. +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) { - return Stats{}, ErrNotImplemented + stats.TotalRAM, err = getHwPhysmem() + return stats, err } diff --git a/pkg/sys/stats_darwin.go b/pkg/sys/stats_darwin.go index 98d9dcc31..01fae14a0 100644 --- a/pkg/sys/stats_darwin.go +++ b/pkg/sys/stats_darwin.go @@ -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 } diff --git a/pkg/sys/stats_linux.go b/pkg/sys/stats_linux.go index 090235bc4..6cd5c6002 100644 --- a/pkg/sys/stats_linux.go +++ b/pkg/sys/stats_linux.go @@ -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 } diff --git a/pkg/sys/stats_test.go b/pkg/sys/stats_test.go index 1a713aed8..e2666639e 100644 --- a/pkg/sys/stats_test.go +++ b/pkg/sys/stats_test.go @@ -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 diff --git a/pkg/sys/stats_windows.go b/pkg/sys/stats_windows.go index 7ae7b9437..08d168428 100644 --- a/pkg/sys/stats_windows.go +++ b/pkg/sys/stats_windows.go @@ -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 }