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
;
42 bool nextIter(bool bQueryOnly
)
44 sal_uInt64 nCurTicks
= tools::Time::GetSystemTicks();
45 // handle system ticks wrap as exceeded time slice
46 if (nCurTicks
< m_nStartTicks
)
49 if (!bQueryOnly
&& 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
)
61 m_nIterStartTicks
= nCurTicks
;
63 return !Application::AnyInput(m_eInputStop
);
68 * Per default the watch considers the last iter time when asking for an
69 * other iteration, so considers Scheduler::acceptableTaskTime as a
72 * If you already know your iter time vary in a large range, consider
73 * setting bConciderLastIterTime to false, so Scheduler::acceptableTaskTime
74 * will be used as a minimum time slot.
76 TaskStopwatch(bool bConciderLastIterTime
= true)
77 : m_nStartTicks(tools::Time::GetSystemTicks())
78 , m_nIterStartTicks(m_nStartTicks
)
79 , m_bConsiderLastIterTime(bConciderLastIterTime
)
80 , m_eInputStop(eDefaultInputStop
)
85 * Returns true, if the time slot is already exceeded
87 bool exceededRuntime() { return !nextIter(true); }
90 * Returns true, if another iteration will probably pass in the time slot
92 bool continueIter() { return nextIter(false); }
99 m_nStartTicks
= tools::Time::GetSystemTicks();
100 m_nIterStartTicks
= m_nStartTicks
;
104 * Sets the input events, which should also "exceed" the stopwatch.
106 * Per default this ignores the VclInputFlags::TIMER.
108 void setInputStop(VclInputFlags eInputStop
= eDefaultInputStop
) { m_eInputStop
= eInputStop
; }
109 VclInputFlags
inputStop() const { return m_eInputStop
; }
112 * Sets the time considered the acceptable maximum for a task to run
114 * This is an orientation for long time background jobs to yield to
115 * the scheduler, so Idle task don't starve each other too much.
117 static unsigned int timeSlice() { return m_nTimeSlice
; }
118 static void setTimeSlice(unsigned int nTimeSlice
) { m_nTimeSlice
= nTimeSlice
; }
121 #endif // INCLUDED_VCL_TASK_STOPWATCH_HXX
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */