From f7819fcb25aaf5b08202db275847c825e7b09999 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 28 Feb 2017 11:12:06 +0100 Subject: [PATCH] Refactor cpu-realtime file creation to remove duplication Signed-off-by: Vincent Demeester --- daemon/daemon_unix.go | 20 +++++++++++--------- daemon/graphdriver/counter.go | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/daemon/daemon_unix.go b/daemon/daemon_unix.go index 7d3c70263d..6a961bae01 100644 --- a/daemon/daemon_unix.go +++ b/daemon/daemon_unix.go @@ -1276,19 +1276,21 @@ func (daemon *Daemon) initCgroupsPath(path string) error { path = filepath.Join(root, path) sysinfo := sysinfo.New(true) - if sysinfo.CPURealtimePeriod && daemon.configStore.CPURealtimePeriod != 0 { - if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) { - return err - } - if err := ioutil.WriteFile(filepath.Join(path, "cpu.rt_period_us"), []byte(strconv.FormatInt(daemon.configStore.CPURealtimePeriod, 10)), 0700); err != nil { - return err - } + if err := maybeCreateCPURealTimeFile(sysinfo.CPURealtimePeriod, daemon.configStore.CPURealtimePeriod, "cpu.rt_period_us", path); err != nil { + return err } - if sysinfo.CPURealtimeRuntime && daemon.configStore.CPURealtimeRuntime != 0 { + if err := maybeCreateCPURealTimeFile(sysinfo.CPURealtimeRuntime, daemon.configStore.CPURealtimeRuntime, "cpu.rt_runtime_us", path); err != nil { + return err + } + return nil +} + +func maybeCreateCPURealTimeFile(sysinfoPresent bool, configValue int64, file string, path string) error { + if sysinfoPresent && configValue != 0 { if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) { return err } - if err := ioutil.WriteFile(filepath.Join(path, "cpu.rt_runtime_us"), []byte(strconv.FormatInt(daemon.configStore.CPURealtimeRuntime, 10)), 0700); err != nil { + if err := ioutil.WriteFile(filepath.Join(path, file), []byte(strconv.FormatInt(configValue, 10)), 0700); err != nil { return err } } diff --git a/daemon/graphdriver/counter.go b/daemon/graphdriver/counter.go index e72a097228..78046d7453 100644 --- a/daemon/graphdriver/counter.go +++ b/daemon/graphdriver/counter.go @@ -56,4 +56,4 @@ func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) int { count := m.count c.mu.Unlock() return count -} \ No newline at end of file +}