1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef INCLUDED_VCL_TASK_STOPWATCH_HXX
11 #define INCLUDED_VCL_TASK_STOPWATCH_HXX
13 #include <tools/time.hxx>
14 #include <vcl/dllapi.h>
15 #include <vcl/inputtypes.hxx>
16 #include <vcl/svapp.hxx>
19 * Helper class primary used to track time of long running iterating tasks.
21 * Normally it should be sufficient to instantiate the watch object before
22 * starting the iteration and query continueIter() at the end of each.
24 * Called Stopwatch, because there is already a Timer class in the Scheduler.
26 * TODO: merge into the general Scheduler, so this can also be used to track
27 * Task runtimes in a more general way.
28 * TODO: handle fast iterations, where continueIter is called multiple times
29 * per tick, by counting the iterations per tick and use that for approximation.
31 class VCL_DLLPUBLIC TaskStopwatch
33 static constexpr VclInputFlags eDefaultInputStop
= VCL_INPUT_ANY
& ~VclInputFlags::TIMER
;
34 static constexpr unsigned int nDefaultTimeSlice
= 50;
35 static unsigned int m_nTimeSlice
;
37 sal_uInt64 m_nStartTicks
;
38 sal_uInt64 m_nIterStartTicks
;
39 bool m_bConsiderLastIterTime
;
40 VclInputFlags m_eInputStop
;
44 sal_uInt64 nCurTicks
= tools::Time::GetSystemTicks();
45 // handle system ticks wrap as exceeded time slice
46 if (nCurTicks
< m_nStartTicks
)
49 if (m_bConsiderLastIterTime
)
51 // based on the last iter runtime, we don't expect to finish in time
52 // m_nTimeSlice < (nCurTicks - m_nStartTicks) + (nCurTicks - m_nIterStartTicks)
53 if (m_nTimeSlice
< 2 * nCurTicks
- m_nIterStartTicks
- m_nStartTicks
)
56 // time slice exceeded
57 else if (m_nTimeSlice
< nCurTicks
- m_nStartTicks
)
60 m_nIterStartTicks
= nCurTicks
;
62 return !Application::AnyInput(m_eInputStop
);
67 * Per default the watch considers the last iter time when asking for an
68 * other iteration, so considers Scheduler::acceptableTaskTime as a
71 * If you already know your iter time vary in a large range, consider
72 * setting bConciderLastIterTime to false, so Scheduler::acceptableTaskTime
73 * will be used as a minimum time slot.
75 TaskStopwatch(bool bConciderLastIterTime
= true)
76 : m_nStartTicks(tools::Time::GetSystemTicks())
77 , m_nIterStartTicks(m_nStartTicks
)
78 , m_bConsiderLastIterTime(bConciderLastIterTime
)
79 , m_eInputStop(eDefaultInputStop
)
84 * Returns true, if another iteration will probably pass in the time slot
86 bool continueIter() { return nextIter(); }
93 m_nStartTicks
= tools::Time::GetSystemTicks();
94 m_nIterStartTicks
= m_nStartTicks
;
98 * Sets the input events, which should also "exceed" the stopwatch.
100 * Per default this ignores the VclInputFlags::TIMER.
102 void setInputStop(VclInputFlags eInputStop
= eDefaultInputStop
) { m_eInputStop
= eInputStop
; }
103 VclInputFlags
inputStop() const { return m_eInputStop
; }
106 * Sets the time considered the acceptable maximum for a task to run
108 * This is an orientation for long time background jobs to yield to
109 * the scheduler, so Idle task don't starve each other too much.
111 static unsigned int timeSlice() { return m_nTimeSlice
; }
112 static void setTimeSlice(unsigned int nTimeSlice
) { m_nTimeSlice
= nTimeSlice
; }
115 #endif // INCLUDED_VCL_TASK_STOPWATCH_HXX
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */