All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
searchTimer.h
Go to the documentation of this file.
1 /* hasTimer.h
2  */
3 #ifndef OSL_SEARCHTIMER_H
4 #define OSL_SEARCHTIMER_H
6 #include "osl/misc/lightMutex.h"
7 #include "osl/stl/vector.h"
8 #include "osl/oslConfig.h"
9 #include <boost/shared_ptr.hpp>
10 #include <boost/cstdint.hpp>
11 #include <limits>
12 
13 namespace osl
14 {
15  namespace search
16  {
17  struct TimeAssigned
18  {
19  MilliSeconds::Interval standard, max;
21  : standard(MilliSeconds::Interval::infinity()),
22  max(MilliSeconds::Interval::infinity())
23  {
24  }
25  explicit TimeAssigned(MilliSeconds::Interval assign)
26  : standard(assign), max(assign)
27  {
28  }
29  TimeAssigned(MilliSeconds::Interval s, MilliSeconds::Interval m)
30  : standard(s), max(m)
31  {
32  }
33  };
34  class SearchMonitor;
36  {
39  MilliSeconds start_time;
43  volatile double next_iteration_coefficient;
44  volatile bool stop_all;
47  volatile int last_memory_use1000;
48 
49  // work area
50  MilliSeconds last_tested;
51  uint64_t next_node_count;
52  double nps;
53  volatile bool stable;
54  vector<boost::shared_ptr<SearchMonitor> > monitors;
55  typedef LightMutex Mutex;
56  mutable Mutex mutex;
57 
59  : start_time(MilliSeconds::now()),
61  node_count_hard_limit(std::numeric_limits<uint64_t>::max()),
62  last_memory_use1000(0), stable(true)
63  {
64  }
65  };
67  {
68  boost::shared_ptr<SearchTimerCommon> shared_timer;
70  public:
73  virtual ~SearchTimer();
74  void setTimeAssign(const TimeAssigned& a) {
75  SCOPED_LOCK(lk,shared_timer->mutex);
76  shared_timer->assigned = a;
77  }
78  void setStartTime(MilliSeconds start) {
79  SCOPED_LOCK(lk,shared_timer->mutex);
80  shared_timer->start_time = start;
81  shared_timer->next_node_count = 0;
82  shared_timer->nps = 0.0;
83  shared_timer->last_tested = start;
84  shared_timer->stop_all = false;
85  }
86  void setStable(bool new_stable) { shared_timer->stable = new_stable; }
87  bool isStableNow() const { return shared_timer->stable; }
88  bool hasSchedule() const {
89  SCOPED_LOCK(lk,shared_timer->mutex);
90  return ! shared_timer->assigned.standard.isInfinity();
91  }
92  const TimeAssigned& timeAssigned() const
93  {
94  SCOPED_LOCK(lk,shared_timer->mutex);
95  return shared_timer->assigned;
96  }
97  const MilliSeconds startTime() const {
98  SCOPED_LOCK(lk,shared_timer->mutex);
99  return shared_timer->start_time;
100  }
101  double elapsed(MilliSeconds now) const
102  {
103  return (now - shared_timer->start_time).toSeconds();
104  }
105  double elapsed() const { return elapsed(MilliSeconds::now()); }
106 
107  void setNextIterationCoefficient(double new_value) {
108  SCOPED_LOCK(lk,shared_timer->mutex);
109  shared_timer->next_iteration_coefficient = new_value;
110  }
111  void setNodeCountHardLimit(uint64_t new_value) {
112  SCOPED_LOCK(lk,shared_timer->mutex);
113  shared_timer->node_count_hard_limit = new_value;
114  }
115  double nextIterationCoefficient() const {
116  SCOPED_LOCK(lk,shared_timer->mutex);
117  return shared_timer->next_iteration_coefficient;
118  }
119 
120  bool stopping() const { return shared_timer->stop_all; }
121  void stopNow()
122  {
124  shared_timer->stop_all = true;
125  }
127  void throwIfNoMoreTime(uint64_t node_count)
128  {
129  SearchTimerCommon& shared = *shared_timer;
130  if (! shared.stop_all) {
131  uint64_t next_node_count;
132  {
133 #ifdef OSL_USE_RACE_DETECTOR
134  SCOPED_LOCK(lk,shared_timer->mutex);
135 #endif
136  next_node_count = shared.next_node_count;
137  }
138  if (next_node_count > node_count || ! hasSchedule())
139  return;
140  }
141  testAndUpdateNextTimeTest(node_count);
142  }
143  int nodeAffordable() const
144  {
145  const MilliSeconds now = MilliSeconds::now();
146 #ifdef OSL_USE_RACE_DETECTOR
147  SCOPED_LOCK(lk,shared_timer->mutex);
148 #endif
149  const double nps = shared_timer->nps;
150  const double left
151  = (shared_timer->start_time + shared_timer->assigned.max - now).toSeconds();
152  return std::max(0, static_cast<int>(nps * left));
153  }
155  static void adjustMemoryUseLimit(double scale=0.9);
156  void addMonitor(const boost::shared_ptr<SearchMonitor>&);
157  bool hasMonitor() const
158  {
159  return ! shared_timer->monitors.empty();
160  }
161  const vector<boost::shared_ptr<SearchMonitor> >& monitors() const
162  {
163  return shared_timer->monitors;
164  }
166  {
167  return shared_timer->last_memory_use1000;
168  }
169  private:
170  void testAndUpdateNextTimeTest(uint64_t node_count);
171  void throwStop();
172  };
173  } // namespace search
174 } // namespace osl
175 
176 
177 #endif /* OSL_SEARCHTIMER_H */
178 // ;;; Local Variables:
179 // ;;; mode:c++
180 // ;;; c-basic-offset:2
181 // ;;; End: