3
3
package fs
4
4
5
5
import (
6
+ "errors"
6
7
"fmt"
7
8
"io/ioutil"
8
9
"os"
@@ -17,7 +18,12 @@ import (
17
18
const cgroupKernelMemoryLimit = "memory.kmem.limit_in_bytes"
18
19
19
20
func EnableKernelMemoryAccounting (path string ) error {
20
- // Check if kernel memory is enabled
21
+ // Ensure that kernel memory is available in this kernel build. If it
22
+ // isn't, we just ignore it because EnableKernelMemoryAccounting is
23
+ // automatically called for all memory limits.
24
+ if ! cgroups .PathExists (filepath .Join (path , cgroupKernelMemoryLimit )) {
25
+ return nil
26
+ }
21
27
// We have to limit the kernel memory here as it won't be accounted at all
22
28
// until a limit is set on the cgroup and limit cannot be set once the
23
29
// cgroup has children, or if there are already tasks in the cgroup.
@@ -34,8 +40,9 @@ func setKernelMemory(path string, kernelMemoryLimit int64) error {
34
40
return fmt .Errorf ("no such directory for %s" , cgroupKernelMemoryLimit )
35
41
}
36
42
if ! cgroups .PathExists (filepath .Join (path , cgroupKernelMemoryLimit )) {
37
- // kernel memory is not enabled on the system so we should do nothing
38
- return nil
43
+ // We have specifically been asked to set a kmem limit. If the kernel
44
+ // doesn't support it we *must* error out.
45
+ return errors .New ("kernel memory accounting not supported by this kernel" )
39
46
}
40
47
if err := ioutil .WriteFile (filepath .Join (path , cgroupKernelMemoryLimit ), []byte (strconv .FormatInt (kernelMemoryLimit , 10 )), 0700 ); err != nil {
41
48
// Check if the error number returned by the syscall is "EBUSY"
0 commit comments