From b3670f8027fc6db1fa4840ad8c83a140ab958abe Mon Sep 17 00:00:00 2001 From: Liam Xu Date: Mon, 18 Mar 2019 15:48:08 -0700 Subject: [PATCH 1/4] Add hour handling in convertCPUTimes function This commit add hour handling in convertCPUTimes function. The time string usually comes from macOS command line: ps -a -o stime,utime -p which could contain hour string. --- process/process_darwin.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 0d153ff..1444906 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -389,12 +389,31 @@ func convertCPUTimes(s string) (ret float64, err error) { var _tmp string if strings.Contains(s, ":") { _t := strings.Split(s, ":") - hour, err := strconv.Atoi(_t[0]) - if err != nil { - return ret, err - } - t += hour * 60 * 100 - _tmp = _t[1] + if len(_t) > 3 { + return ret, err + } else if len(_t) == 3 { + hour, err := strconv.Atoi(_t[0]) + if err != nil { + return ret, err + } + t += hour * 60 * 60 * ClockTicks + + mins, err := strconv.Atoi(_t[1]) + if err != nil { + return ret, err + } + t += mins * 60 * ClockTicks + _tmp = _t[2] + } else if len(_t) == 2 { + mins, err := strconv.Atoi(_t[0]) + if err != nil { + return ret, err + } + t += mins * 60 * ClockTicks + _tmp = _t[1] + } else { + _tmp = s + } } else { _tmp = s } @@ -404,7 +423,7 @@ func convertCPUTimes(s string) (ret float64, err error) { return ret, err } h, err := strconv.Atoi(_t[0]) - t += h * 100 + t += h * ClockTicks h, err = strconv.Atoi(_t[1]) t += h return float64(t) / ClockTicks, nil From 6eb4d73bde6159a59c8c56cb76183844786e0313 Mon Sep 17 00:00:00 2001 From: Xu Lian Date: Mon, 18 Mar 2019 20:36:12 -0700 Subject: [PATCH 2/4] Fix an indentation issues --- process/process_darwin.go | 48 +++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 1444906..ea1a042 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -390,30 +390,30 @@ func convertCPUTimes(s string) (ret float64, err error) { if strings.Contains(s, ":") { _t := strings.Split(s, ":") if len(_t) > 3 { - return ret, err - } else if len(_t) == 3 { - hour, err := strconv.Atoi(_t[0]) - if err != nil { - return ret, err - } - t += hour * 60 * 60 * ClockTicks - - mins, err := strconv.Atoi(_t[1]) - if err != nil { - return ret, err - } - t += mins * 60 * ClockTicks - _tmp = _t[2] - } else if len(_t) == 2 { - mins, err := strconv.Atoi(_t[0]) - if err != nil { - return ret, err - } - t += mins * 60 * ClockTicks - _tmp = _t[1] - } else { - _tmp = s - } + return ret, err + } else if len(_t) == 3 { + hour, err := strconv.Atoi(_t[0]) + if err != nil { + return ret, err + } + t += hour * 60 * 60 * ClockTicks + + mins, err := strconv.Atoi(_t[1]) + if err != nil { + return ret, err + } + t += mins * 60 * ClockTicks + _tmp = _t[2] + } else if len(_t) == 2 { + mins, err := strconv.Atoi(_t[0]) + if err != nil { + return ret, err + } + t += mins * 60 * ClockTicks + _tmp = _t[1] + } else { + _tmp = s + } } else { _tmp = s } From f2f18df9dbc3cd8e06c06e623d61eaf41296bc1e Mon Sep 17 00:00:00 2001 From: Liam Xu Date: Fri, 22 Mar 2019 10:21:01 -0700 Subject: [PATCH 3/4] Use Swith to replace if else Use Swith to replace if else --- process/process_darwin.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index ea1a042..9a2f2e9 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -389,9 +389,8 @@ func convertCPUTimes(s string) (ret float64, err error) { var _tmp string if strings.Contains(s, ":") { _t := strings.Split(s, ":") - if len(_t) > 3 { - return ret, err - } else if len(_t) == 3 { + switch len(_t) { + case 3: hour, err := strconv.Atoi(_t[0]) if err != nil { return ret, err @@ -404,15 +403,17 @@ func convertCPUTimes(s string) (ret float64, err error) { } t += mins * 60 * ClockTicks _tmp = _t[2] - } else if len(_t) == 2 { + case 2: mins, err := strconv.Atoi(_t[0]) if err != nil { return ret, err } t += mins * 60 * ClockTicks _tmp = _t[1] - } else { + case 1, 0: _tmp = s + default: + return ret, err } } else { _tmp = s From 1b525b7c9c8eded05da8f8b719276ec993b9b041 Mon Sep 17 00:00:00 2001 From: Liam Xu Date: Fri, 22 Mar 2019 11:44:52 -0700 Subject: [PATCH 4/4] Return cpu time format error Return cpu time format error --- process/process_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 9a2f2e9..a15af5a 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -413,7 +413,7 @@ func convertCPUTimes(s string) (ret float64, err error) { case 1, 0: _tmp = s default: - return ret, err + return ret, fmt.Errorf("wrong cpu time string") } } else { _tmp = s