Module: check_mk
Branch: master
Commit: aa5a17a28c4a4ca1dde543fe64a6ab76a0c07e15
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=aa5a17a28c4a4c…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Jul 27 13:16:08 2018 +0200
Fix time calculation bug and some clean ups on the way
* Removed the special case of current_phase. All elements of the phase
stack are now in the stack list and the last element of the list is
always the current phase.
* Fix an accounting bug which lead to invalid time calculations. This
issue made the Check_MK service report wrong run time information.
Change-Id: I9e1caa27f266c3e54c3f7ab5295d0e5d36edb6f8
---
cmk/cpu_tracking.py | 43 +++++++++++++++----------------------------
1 file changed, 15 insertions(+), 28 deletions(-)
diff --git a/cmk/cpu_tracking.py b/cmk/cpu_tracking.py
index e14018a..76fad2a 100644
--- a/cmk/cpu_tracking.py
+++ b/cmk/cpu_tracking.py
@@ -41,42 +41,38 @@ except NameError:
times = {}
last_time_snapshot = []
phase_stack = []
-current_phase = None
def start(initial_phase):
- global times, last_time_snapshot, current_phase, phase_stack
- times = {}
+ global times, last_time_snapshot
console.vverbose("[cpu_tracking] Start with phase '%s'\n" %
initial_phase)
+ times = {}
last_time_snapshot = _time_snapshot()
- current_phase = initial_phase
- phase_stack = []
- _set_phase(initial_phase)
+
+ del phase_stack[:]
+ phase_stack.append(initial_phase)
def end():
console.vverbose("[cpu_tracking] End\n")
- _set_phase(None)
+ _add_times_to_phase()
+ del phase_stack[:]
def push_phase(phase):
if _is_not_tracking():
return
- console.vverbose("[cpu_tracking] Push phase (Stack: %r)\n" % phase_stack)
- phase_stack.append(current_phase)
- _set_phase(phase)
+ console.vverbose("[cpu_tracking] Push phase '%s' (Stack: %r)\n" %
(phase, phase_stack))
+ _add_times_to_phase()
+ phase_stack.append(phase)
def pop_phase():
if _is_not_tracking():
return
- console.vverbose("[cpu_tracking] Pop current phase (Stack: %r)\n" %
phase_stack)
- if len(phase_stack) == 1:
- _set_phase(None)
- else:
- _set_phase(phase_stack[-1])
-
+ console.vverbose("[cpu_tracking] Pop phase '%s' (Stack: %r)\n" %
(phase_stack[-1], phase_stack))
+ _add_times_to_phase()
del phase_stack[-1]
@@ -85,23 +81,13 @@ def get_times():
def _is_not_tracking():
- return current_phase is None
-
-
-def _set_phase(phase):
- global current_phase
- if _is_not_tracking():
- return
-
- console.vverbose("[cpu_tracking] Set phase: %s (previous %s)\n" % (phase,
current_phase))
- _add_times_to_phase()
- current_phase = phase
+ return not bool(phase_stack)
def _add_times_to_phase():
global last_time_snapshot
new_time_snapshot = _time_snapshot()
- for phase in current_phase, "TOTAL":
+ for phase in phase_stack[-1], "TOTAL":
phase_times = times.get(phase, [ 0.0 ] * len(new_time_snapshot))
times[phase] = [
phase_times[i] + new_time_snapshot[i] - last_time_snapshot[i]
@@ -110,4 +96,5 @@ def _add_times_to_phase():
def _time_snapshot():
+ # TODO: Create a better structure for this data
return list(os.times()[:4]) + [ time.time() ]