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 
28 namespace armarx
29 {
30  CycleUtil::CycleUtil(IceUtil::Time const& cycleDuration, bool forceSystemTime) :
31  cycleDuration(cycleDuration), forceSystemTime(forceSystemTime), busyWaitShare(0.0F)
32  {
33  reset();
34  }
35 
36  CycleUtil::CycleUtil(const std::int64_t& cycleDurationMs, bool forceSystemTime) :
37  cycleDuration(IceUtil::Time::milliSeconds(cycleDurationMs)),
38  forceSystemTime(forceSystemTime)
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 
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 
74  {
75  auto now = armarx::CycleUtil::now();
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 
92  {
93  return startTime;
94  }
95 
96  std::int64_t
98  {
99  return cycleCount;
100  }
101 
104  {
105  if (cycleCount > 0)
106  {
107  return IceUtil::Time::microSeconds((lastCycleTime - startTime).toMicroSeconds() /
108  cycleCount);
109  }
110  return now();
111  }
112 
115  {
116  return cycleMinDuration;
117  }
118 
121  {
122  return cycleMaxDuration;
123  }
124 
125  float
127  {
128  return busyWaitShare;
129  }
130 
131  void
133  {
135  }
136 
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 
147  {
148  return lastCycleTime;
149  }
150 } // namespace armarx
armarx::CycleUtil::getCycleCount
std::int64_t getCycleCount() const
Count of how many cycles were executed, i.e.
Definition: CycleUtil.cpp:97
armarx::CycleUtil::getMinimumDuration
IceUtil::Time getMinimumDuration() const
Definition: CycleUtil.cpp:114
armarx::CycleUtil::getLastCycleTime
IceUtil::Time getLastCycleTime() const
Time when the last cycle finished or start time.
Definition: CycleUtil.cpp:146
armarx::CycleUtil::cycleCount
std::int64_t cycleCount
Definition: CycleUtil.h:86
armarx::CycleUtil::cycleMaxDuration
IceUtil::Time cycleMaxDuration
Definition: CycleUtil.h:85
armarx::CycleUtil::setBusyWaitShare
void setBusyWaitShare(float value)
Definition: CycleUtil.cpp:132
IceUtil
Definition: Instance.h:21
armarx::CycleUtil::getMaximumDuration
IceUtil::Time getMaximumDuration() const
Definition: CycleUtil.cpp:120
armarx::CycleUtil::cycleDuration
IceUtil::Time cycleDuration
Definition: CycleUtil.h:83
armarx::CycleUtil::getBusyWaitShare
float getBusyWaitShare() const
Definition: CycleUtil.cpp:126
armarx::CycleUtil::getAverageDuration
IceUtil::Time getAverageDuration() const
Definition: CycleUtil.cpp:103
armarx::CycleUtil::lastCycleTime
IceUtil::Time lastCycleTime
Definition: CycleUtil.h:82
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::CycleUtil::reset
void reset()
resets startTime, lastCycleTime and cycleCount.
Definition: CycleUtil.cpp:44
armarx::CycleUtil::waitForCycleDuration
IceUtil::Time waitForCycleDuration()
This function will wait (virtual or system time) until the cycle time is reached.
Definition: CycleUtil.cpp:53
armarx::CycleUtil::cycleMinDuration
IceUtil::Time cycleMinDuration
Definition: CycleUtil.h:84
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::TimeUtil::Sleep
static void Sleep(IceUtil::Time duration)
lock the calling thread for a given duration (like usleep(...) but using Timeserver time)
Definition: TimeUtil.cpp:76
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::CycleUtil::update
IceUtil::Time update()
Definition: CycleUtil.cpp:73
CycleUtil.h
armarx::CycleUtil::getStartTime
IceUtil::Time getStartTime() const
Time when object was constructed or reset was last called.
Definition: CycleUtil.cpp:91
TimeUtil.h
F
Definition: ExportDialogControllerTest.cpp:16
armarx::CycleUtil::CycleUtil
CycleUtil(const IceUtil::Time &cycleDuration, bool forceSystemTime=false)
Definition: CycleUtil.cpp:30
armarx::CycleUtil::now
static IceUtil::Time now()
Definition: CycleUtil.cpp:138
armarx::CycleUtil::startTime
IceUtil::Time startTime
Definition: CycleUtil.h:81
armarx::CycleUtil::busyWaitShare
float busyWaitShare
Definition: CycleUtil.h:88
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28