CycleUtil.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "CycleUtil.h"
25
26#include "TimeUtil.h"
27
28namespace armarx
29{
35
36 CycleUtil::CycleUtil(const std::int64_t& cycleDurationMs, bool forceSystemTime) :
37 cycleDuration(IceUtil::Time::milliSeconds(cycleDurationMs)),
39 {
40 reset();
41 }
42
43 void
45 {
46 cycleCount = 0;
48 cycleMaxDuration = IceUtil::Time::microSeconds(0);
49 cycleMinDuration = IceUtil::Time::microSeconds(std::numeric_limits<std::int64_t>::max());
50 }
51
52 IceUtil::Time
54 {
55 auto start = armarx::CycleUtil::now();
56 const auto waitTime = IceUtil::Time::microSeconds(
57 std::max<std::int64_t>(0, (lastCycleTime + cycleDuration - start).toMicroSeconds()));
58 const double waitTimeDouble = waitTime.toMicroSecondsDouble() -
59 (busyWaitShare * cycleDuration.toMicroSecondsDouble());
60 if (busyWaitShare < 1.0)
61 {
62 TimeUtil::Sleep(IceUtil::Time::microSecondsDouble(waitTimeDouble));
63 }
64 while ((now() - lastCycleTime).toMilliSecondsDouble() <
65 cycleDuration.toMilliSecondsDouble())
66 {
67 }
68 update();
69 return armarx::CycleUtil::now() - start;
70 }
71
72 IceUtil::Time
74 {
76 auto cycleTime = now - lastCycleTime;
77 cycleCount++;
78 if (cycleTime > cycleMaxDuration)
79 {
80 cycleMaxDuration = cycleTime;
81 }
82 if (cycleTime < cycleMinDuration)
83 {
84 cycleMinDuration = cycleTime;
85 }
87 return cycleTime;
88 }
89
90 IceUtil::Time
92 {
93 return startTime;
94 }
95
96 std::int64_t
98 {
99 return cycleCount;
100 }
101
102 IceUtil::Time
104 {
105 if (cycleCount > 0)
106 {
107 return IceUtil::Time::microSeconds((lastCycleTime - startTime).toMicroSeconds() /
108 cycleCount);
109 }
110 return now();
111 }
112
113 IceUtil::Time
115 {
116 return cycleMinDuration;
117 }
118
119 IceUtil::Time
121 {
122 return cycleMaxDuration;
123 }
124
125 float
127 {
128 return busyWaitShare;
129 }
130
131 void
133 {
134 busyWaitShare = value;
135 }
136
137 IceUtil::Time
139 {
140 struct timespec ts;
141 clock_gettime(CLOCK_MONOTONIC, &ts);
142 return IceUtil::Time::microSeconds(ts.tv_sec * 1e6 + ts.tv_nsec / 1000);
143 }
144
145 IceUtil::Time
147 {
148 return lastCycleTime;
149 }
150} // namespace armarx
IceUtil::Time getMaximumDuration() const
IceUtil::Time update()
Definition CycleUtil.cpp:73
CycleUtil(const IceUtil::Time &cycleDuration, bool forceSystemTime=false)
Definition CycleUtil.cpp:30
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition CycleUtil.cpp:53
IceUtil::Time cycleDuration
Definition CycleUtil.h:83
IceUtil::Time getStartTime() const
Time when object was constructed or reset was last called.
Definition CycleUtil.cpp:91
float getBusyWaitShare() const
IceUtil::Time cycleMinDuration
Definition CycleUtil.h:84
IceUtil::Time cycleMaxDuration
Definition CycleUtil.h:85
IceUtil::Time getAverageDuration() const
std::int64_t getCycleCount() const
Count of how many cycles were executed, i.e.
Definition CycleUtil.cpp:97
void setBusyWaitShare(float value)
std::int64_t cycleCount
Definition CycleUtil.h:86
IceUtil::Time getMinimumDuration() const
IceUtil::Time getLastCycleTime() const
Time when the last cycle finished or start time.
void reset()
resets startTime, lastCycleTime and cycleCount.
Definition CycleUtil.cpp:44
IceUtil::Time lastCycleTime
Definition CycleUtil.h:82
IceUtil::Time startTime
Definition CycleUtil.h:81
static IceUtil::Time now()
static void Sleep(IceUtil::Time duration)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition TimeUtil.cpp:80
This file offers overloads of toIce() and fromIce() functions for STL container types.