Skip to content

Commit f05c8e7

Browse files
committed
Update scheduler.go (fix issue vpenso#18)
1 parent cef8c26 commit f05c8e7

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

scheduler.go

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
1616
package main
1717

1818
import (
19+
"io/ioutil"
1920
"log"
21+
"os/exec"
22+
"regexp"
2023
"strings"
2124
"strconv"
22-
"io/ioutil"
23-
"os/exec"
2425
"github.com/prometheus/client_golang/prometheus"
2526
)
2627

@@ -53,28 +54,51 @@ func SchedulerData() []byte {
5354
return out
5455
}
5556

56-
// Helper function to split a single line from the sdiag output
57-
func SplitColonValueToFloat(input string) float64 {
58-
// Verify if the string extracted from the sdiag output is empty
59-
if input == "" { return 0 }
60-
str := strings.Split(input,":")
61-
rvalue := strings.TrimSpace(str[1])
62-
flt, _ := strconv.ParseFloat(rvalue,64)
63-
return flt
64-
}
65-
6657
// Extract the relevant metrics from the sdiag output
6758
func ParseSchedulerMetrics(input []byte) *SchedulerMetrics {
68-
lines := strings.Split(string(input),"\n")
6959
var sm SchedulerMetrics
70-
sm.threads = SplitColonValueToFloat(lines[4])
71-
sm.queue_size = SplitColonValueToFloat(lines[5])
72-
sm.last_cycle = SplitColonValueToFloat(lines[14])
73-
sm.mean_cycle = SplitColonValueToFloat(lines[17])
74-
sm.cycle_per_minute = SplitColonValueToFloat(lines[19])
75-
sm.backfill_last_cycle = SplitColonValueToFloat(lines[27])
76-
sm.backfill_mean_cycle = SplitColonValueToFloat(lines[29])
77-
sm.backfill_depth_mean = SplitColonValueToFloat(lines[32])
60+
lines := strings.Split(string(input),"\n")
61+
// Guard variables to check for string repetitions in the output of sdiag
62+
// (two 'Last cycle', two 'Mean cycle')
63+
lc_count := 0
64+
mc_count := 0
65+
for _, line := range lines {
66+
if strings.Contains(line, ":") {
67+
state := strings.Split(line, ":")[0]
68+
st := regexp.MustCompile(`^Server thread`)
69+
qs := regexp.MustCompile(`^Agent queue`)
70+
lc := regexp.MustCompile(`^[\s]+Last cycle$`)
71+
mc := regexp.MustCompile(`^[\s]+Mean cycle$`)
72+
cpm := regexp.MustCompile(`^[\s]+Cycles per`)
73+
dpm := regexp.MustCompile(`^[\s]+Depth Mean$`)
74+
switch {
75+
case st.MatchString(state) == true:
76+
sm.threads, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
77+
case qs.MatchString(state) == true:
78+
sm.queue_size, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
79+
case lc.MatchString(state) == true:
80+
if lc_count == 0 {
81+
sm.last_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
82+
lc_count = 1
83+
}
84+
if lc_count == 1 {
85+
sm.backfill_last_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
86+
}
87+
case mc.MatchString(state) == true:
88+
if mc_count == 0 {
89+
sm.mean_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
90+
mc_count = 1
91+
}
92+
if mc_count == 1 {
93+
sm.backfill_mean_cycle, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
94+
}
95+
case cpm.MatchString(state) == true:
96+
sm.cycle_per_minute, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
97+
case dpm.MatchString(state) == true:
98+
sm.backfill_depth_mean, _ = strconv.ParseFloat(strings.TrimSpace(strings.Split(line, ":")[1]), 64)
99+
}
100+
}
101+
}
78102
return &sm
79103
}
80104

0 commit comments

Comments
 (0)