ATLAS Offline Software
VP1Prioritiser.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // //
7 // Implementation of class VP1Prioritiser //
8 // //
9 // Author: Thomas Kittelmann <Thomas.Kittelmann@cern.ch> //
10 // //
11 // Initial version: April 2007 //
12 // //
14 
15 #include "VP1Gui/VP1Prioritiser.h"
17 #include "VP1Base/IVP1System.h"
18 #include <QTime>
19 #include <QElapsedTimer>
20 #include <QQueue>
21 #include <QSet>
22 #include <map>
23 #include <cassert>
24 
28 
29 //____________________________________________________________________
31 public:
32 
33  class SystemInfo{
34  public:
37  ~SystemInfo();
40  void addTimeMeasurement(const double&);
41  double refreshtime() const { return m_timing; }
42  private:
43  double m_timing;
44  double m_priority;
45  const bool m_accumulator;
47  void calcPriority();
48  Imp *m_d;
49  void removeFromPriMap();
50  QQueue<double> m_timemeasurements;
51  };
52 
54  //The following map is for prioritising the order of system refreshes:
55  typedef std::multimap<double,IVP1System*> Pri2SysMap;
56  Pri2SysMap priority2sys;//This map is kept updated automatically by the SystemInfo objects.
57 
58  QSet<IVP1ChannelWidget*> visiblechannels, soonvisiblechannels;
59  double soonvisbonus;
60 
61  QHash<IVP1System*,SystemInfo*> sys2info;
62 
63  QElapsedTimer * stopwatch;
65 
66  void updateSysinfoWithVisibilityState(const QSet<IVP1ChannelWidget*>& channels,
68 
69 
70  std::set<IVP1System *>::const_iterator itsys, itsysE;
71 };
72 
76 
77 //____________________________________________________________________
79  : system(s), m_priority(117117.117),
80  m_accumulator(cw->isAccumulator()),m_vs(vs),m_d(d)
81 {
82  //If another system with the same name exists already, we use that
83  //timing as a reasonable guess. Otherwise we use the average of the
84  //other channels. Failing that we use a default of 500ms.:
85  double tmp(0);
86  bool found(false);
89  if ( it.key()->name() == s->name() ) {
90  m_timing=it.value()->refreshtime();
91  found=true;
92  break;
93  }
94  tmp += m_timing=it.value()->refreshtime();
95  }
96  if (!found&&!m_d->sys2info.empty())
97  m_timing = tmp/m_d->sys2info.count();
98  else
99  m_timing=500;
100 
101  calcPriority();
102 }
103 
104 //____________________________________________________________________
106 {
107  removeFromPriMap();
108 }
109 
110 //____________________________________________________________________
112 {
113  //Fixme: When do we get tabmanager setVisible signals? At startup? When channels are added?
114  //Fixme: Do not emit these during file loading!!
115  if (m_vs==vs)
116  return;
117  m_vs = vs;
118  calcPriority();
119 }
120 
121 //____________________________________________________________________
123 {
124  //Keep the seven latest measurements, and use a truncated mean as
125  //final estimate.
126 
127  m_timemeasurements.enqueue(t);
128  if (m_timemeasurements.count()==8)
129  m_timemeasurements.dequeue();
130 
131  QList<double> tmplist = m_timemeasurements;
132  std::sort(tmplist.begin(), tmplist.end());
133 
134  switch(tmplist.count()) {
135  case 1: m_timing = tmplist.at(0); break;
136  case 2: m_timing = 0.5*(tmplist.at(0)+tmplist.at(1)); break;
137  case 3: m_timing = tmplist.at(1); break;
138  case 4: m_timing = 0.5*(tmplist.at(1)+tmplist.at(2)); break;
139  case 5: m_timing = (tmplist.at(1)+tmplist.at(2)+tmplist.at(3))/3.0; break;
140  case 6: m_timing = 0.5*(tmplist.at(2)+tmplist.at(3)); break;
141  case 7: m_timing = (tmplist.at(2)+tmplist.at(3)+tmplist.at(4))/3.0; break;
142  default: assert(0&&"Should never happen"); break;
143  }
144 
145  calcPriority();
146 }
147 
148 //____________________________________________________________________
150 {
151  //1) Remove from m_d->priority2sys map
152 
153  if (m_priority!=117117.117)
154  removeFromPriMap();
155 
156  //2) Calculate new priority
157 
158  //Priority is based on how long the systems take to refresh (the
159  //fastest gets refreshed first). In order to ensure that visible,
160  //soonvisible, and accumulators gets priority they get a bonus of
161  //30s, XXXms, and 100ms respectively (where XXX depends on the scenario):
162 
163  m_priority = m_timing + (m_vs==VISIBLE?-30000.0:(m_vs==SOONVISIBLE?-m_d->soonvisbonus:0.0)) + (m_accumulator?-100.0:0.0);
164 
165  //3) Add to m_d->priority2sys map
166 
167  std::pair<double,IVP1System*> a(m_priority,system);
168  m_d->priority2sys.insert(a);
169 }
170 
171 //____________________________________________________________________
173 {
174  //Fixme: Cache itp and it (in class Imp even?)!
175  std::pair<Pri2SysMap::iterator, Pri2SysMap::iterator> itp(m_d->priority2sys.equal_range(m_priority));
176  Pri2SysMap::iterator it = itp.first;
177  for (;it!=itp.second;++it) {
178  if (it->second==system) {
179  m_d->priority2sys.erase(it);
180  break;
181  }
182  }
183  assert(it != itp.second);
184 }
185 
189 
190 //____________________________________________________________________
192  : QObject(parent), m_d(new Imp)
193 {
194  m_d->prioritiser=this;
195  m_d->stopwatch = new QElapsedTimer();
197  m_d->soonvisbonus=0;
198 }
199 
200 //____________________________________________________________________
202 {
203  delete m_d->stopwatch;
204  delete m_d; m_d=0;
205 }
206 
207 //___________________________________________________________________
210  for (Imp::Pri2SysMap::iterator it=m_d->priority2sys.begin();it!=itE;++it) {
211  if ( it->second->state()==IVP1System::ERASED &&it->second->activeState()==IVP1System::ON )
212  return it->second;
213  }
214  return 0;
215 }
216 
217 //___________________________________________________________________
219  QList<IVP1System*> tmp;
221  for (Imp::Pri2SysMap::iterator it=m_d->priority2sys.begin();it!=itE;++it) {
222  if ( it->second->state()==IVP1System::REFRESHED )
223  tmp << it->second;
224  }
225  return tmp;
226 }
227 
228 
229 //___________________________________________________________________
231 {
232  double tmp(0);
235  if ( it.key()->state()==IVP1System::ERASED &&it.key()->activeState()==IVP1System::ON )
236  tmp += it.value()->refreshtime();
237  }
238  return tmp;
239 }
240 
241 //___________________________________________________________________
243 {
244  assert(!m_d->currenttimedsystem);
245  m_d->stopwatch->start();
247  assert(m_d->sys2info.contains(s));
248  return m_d->sys2info[s]->refreshtime();
249 }
250 
251 
252 //___________________________________________________________________
254 {
255  return static_cast<double>(m_d->stopwatch->elapsed());
256 }
257 
258 //___________________________________________________________________
260 {
261  assert(m_d->currenttimedsystem);
262  double timing = static_cast<double>(m_d->stopwatch->elapsed());
263  if (m_d->sys2info.contains(m_d->currenttimedsystem)) {//This check, since the corresponding channel might have become uncreated in the meantime.
264  m_d->sys2info[m_d->currenttimedsystem]->addTimeMeasurement(timing);
265  }
267  return timing;
268 }
269 
270 //____________________________________________________________________
272  m_d->itsys = cw->systems().begin();
273  m_d->itsysE = cw->systems().end();
274 }
275 
276 //___________________________________________________________________
278  const SystemInfo::VisibilityState&vs) {
279  for(IVP1ChannelWidget*cw : channels) {
280  for (prioritiser->setupSysItr(cw);itsys!=itsysE;++itsys) {
281  assert(sys2info.contains(*itsys));
282  //if (sys2info.contains(*itsys))//This check since channel deletion might have triggered this update.
283  sys2info[*itsys]->setVisibilityState(vs);
284  }
285  }
286 }
287 
288 //___________________________________________________________________
289 void VP1Prioritiser::visibleChannelsChanged(const QSet<IVP1ChannelWidget*>&newvis,
290  const QSet<IVP1ChannelWidget*>&newsoonvis,
291  const double& soonvisbonus)
292 {
293  if (m_d->visiblechannels==newvis&&m_d->soonvisiblechannels==newsoonvis&&m_d->soonvisbonus==soonvisbonus)
294  return;
299  m_d->visiblechannels = newvis;
300  m_d->soonvisiblechannels = newsoonvis;
301  m_d->soonvisbonus = soonvisbonus;
302 }
303 
304 //___________________________________________________________________
306 {
307  bool visible = m_d->visiblechannels.contains(cw);
308  bool soonvisible = m_d->soonvisiblechannels.contains(cw);
309  assert(!(visible&&soonvisible));
310  for (setupSysItr(cw);m_d->itsys!=m_d->itsysE;++m_d->itsys) {
311  assert(!m_d->sys2info.contains(*(m_d->itsys)));
314  m_d->sys2info.insert(*(m_d->itsys),info);
315  }
316 }
317 
318 //___________________________________________________________________
320 {
321  for (setupSysItr(cw);m_d->itsys!=m_d->itsysE;++m_d->itsys) {
322  assert(m_d->sys2info.contains(*(m_d->itsys)));
323  delete m_d->sys2info[*(m_d->itsys)];
324  m_d->sys2info.remove(*(m_d->itsys));
325  }
326  if (m_d->visiblechannels.contains(cw))
327  m_d->visiblechannels.remove(cw);
328  if (m_d->soonvisiblechannels.contains(cw))
329  m_d->soonvisiblechannels.remove(cw);
330 }
grepfile.info
info
Definition: grepfile.py:38
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TruthTest.itp
itp
Definition: TruthTest.py:46
VP1Prioritiser::nextErasedActiveSystemByPriority
IVP1System * nextErasedActiveSystemByPriority()
Definition: VP1Prioritiser.cxx:208
VP1Prioritiser::Imp::SystemInfo::m_d
Imp * m_d
Definition: VP1Prioritiser.cxx:48
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
VP1Prioritiser::getSystemsToEraseByPriority
QList< IVP1System * > getSystemsToEraseByPriority()
Definition: VP1Prioritiser.cxx:218
VP1Prioritiser::Imp::SystemInfo::VisibilityState
VisibilityState
Definition: VP1Prioritiser.cxx:35
beamspotman.tmplist
tmplist
Definition: beamspotman.py:381
VP1Prioritiser::setupSysItr
void setupSysItr(IVP1ChannelWidget *cw)
Definition: VP1Prioritiser.cxx:271
VP1Prioritiser::Imp::Pri2SysMap
std::multimap< double, IVP1System * > Pri2SysMap
Definition: VP1Prioritiser.cxx:55
hist_file_dump.d
d
Definition: hist_file_dump.py:137
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
skel.it
it
Definition: skel.GENtoEVGEN.py:396
VP1Prioritiser::VP1Prioritiser
VP1Prioritiser(QObject *parent)
Definition: VP1Prioritiser.cxx:191
VP1Prioritiser::Imp::itsys
std::set< IVP1System * >::const_iterator itsys
Definition: VP1Prioritiser.cxx:70
VP1Prioritiser::Imp::stopwatch
QElapsedTimer * stopwatch
Definition: VP1Prioritiser.cxx:63
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
VP1Prioritiser::~VP1Prioritiser
virtual ~VP1Prioritiser()
Definition: VP1Prioritiser.cxx:201
TruthTest.itE
itE
Definition: TruthTest.py:25
dq_defect_copy_defect_database.channels
def channels
Definition: dq_defect_copy_defect_database.py:56
VP1Prioritiser.h
VP1Prioritiser::Imp::SystemInfo::m_timemeasurements
QQueue< double > m_timemeasurements
Definition: VP1Prioritiser.cxx:50
VP1Prioritiser::Imp::visiblechannels
QSet< IVP1ChannelWidget * > visiblechannels
Definition: VP1Prioritiser.cxx:58
VP1Prioritiser::Imp::SystemInfo::refreshtime
double refreshtime() const
Definition: VP1Prioritiser.cxx:41
VP1Prioritiser::Imp::SystemInfo::system
IVP1System * system
Definition: VP1Prioritiser.cxx:38
IVP1System
Definition: IVP1System.h:36
VP1Prioritiser::Imp::soonvisiblechannels
QSet< IVP1ChannelWidget * > soonvisiblechannels
Definition: VP1Prioritiser.cxx:58
VP1Prioritiser::channelCreated
void channelCreated(IVP1ChannelWidget *)
Definition: VP1Prioritiser.cxx:305
trigDumpTimers.timing
def timing(hist)
Definition: trigDumpTimers.py:13
VP1Prioritiser::Imp::priority2sys
Pri2SysMap priority2sys
Definition: VP1Prioritiser.cxx:56
VP1Prioritiser::Imp::SystemInfo
Definition: VP1Prioritiser.cxx:33
VP1Prioritiser
Definition: VP1Prioritiser.h:22
VP1Prioritiser::Imp::SystemInfo::removeFromPriMap
void removeFromPriMap()
Definition: VP1Prioritiser.cxx:172
VP1Prioritiser::Imp::SystemInfo::NOTVISIBLE
@ NOTVISIBLE
Definition: VP1Prioritiser.cxx:35
test_pyathena.parent
parent
Definition: test_pyathena.py:15
VP1Prioritiser::endTiming_Refresh
double endTiming_Refresh()
Definition: VP1Prioritiser.cxx:259
IVP1System::REFRESHED
@ REFRESHED
Definition: IVP1System.h:143
IVP1ChannelWidget::systems
const std::set< IVP1System * > & systems()
Definition: IVP1ChannelWidget.cxx:231
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
VP1Prioritiser::Imp::itsysE
std::set< IVP1System * >::const_iterator itsysE
Definition: VP1Prioritiser.cxx:70
VP1Prioritiser::channelUncreated
void channelUncreated(IVP1ChannelWidget *)
Definition: VP1Prioritiser.cxx:319
VP1Prioritiser::Imp::SystemInfo::VISIBLE
@ VISIBLE
Definition: VP1Prioritiser.cxx:35
VP1Prioritiser::Imp::soonvisbonus
double soonvisbonus
Definition: VP1Prioritiser.cxx:59
VP1Prioritiser::Imp::prioritiser
VP1Prioritiser * prioritiser
Definition: VP1Prioritiser.cxx:53
IVP1ChannelWidget
Definition: IVP1ChannelWidget.h:34
IVP1System::ON
@ ON
Definition: IVP1System.h:144
VP1Prioritiser::Imp::SystemInfo::m_accumulator
const bool m_accumulator
Definition: VP1Prioritiser.cxx:45
VP1Prioritiser::beginTiming_Refresh
double beginTiming_Refresh(IVP1System *)
Definition: VP1Prioritiser.cxx:242
VP1Prioritiser::visibleChannelsChanged
void visibleChannelsChanged(const QSet< IVP1ChannelWidget * > &vis, const QSet< IVP1ChannelWidget * > &soonvis, const double &soonvisbonus)
Definition: VP1Prioritiser.cxx:289
VP1Prioritiser::Imp::SystemInfo::SystemInfo
SystemInfo(IVP1System *, Imp *, IVP1ChannelWidget *, const VisibilityState &)
Definition: VP1Prioritiser.cxx:78
VP1Prioritiser::Imp::SystemInfo::setVisibilityState
void setVisibilityState(const VisibilityState &)
Definition: VP1Prioritiser.cxx:111
VP1Prioritiser::Imp
Definition: VP1Prioritiser.cxx:30
VP1Prioritiser::Imp::SystemInfo::~SystemInfo
~SystemInfo()
Definition: VP1Prioritiser.cxx:105
VP1Prioritiser::Imp::sys2info
QHash< IVP1System *, SystemInfo * > sys2info
Definition: VP1Prioritiser.cxx:61
a
TList * a
Definition: liststreamerinfos.cxx:10
VP1Prioritiser::Imp::SystemInfo::calcPriority
void calcPriority()
Definition: VP1Prioritiser.cxx:149
VP1Prioritiser::elapsedTiming_Refresh
double elapsedTiming_Refresh()
Definition: VP1Prioritiser.cxx:253
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
VP1Prioritiser::estimateRemainingCalcTime
double estimateRemainingCalcTime() const
Definition: VP1Prioritiser.cxx:230
VP1Prioritiser::Imp::currenttimedsystem
IVP1System * currenttimedsystem
Definition: VP1Prioritiser.cxx:64
VP1Prioritiser::Imp::SystemInfo::SOONVISIBLE
@ SOONVISIBLE
Definition: VP1Prioritiser.cxx:35
VP1Prioritiser::m_d
Imp * m_d
Definition: VP1Prioritiser.h:45
VP1Prioritiser::Imp::SystemInfo::m_vs
VisibilityState m_vs
Definition: VP1Prioritiser.cxx:46
VP1Prioritiser::Imp::SystemInfo::addTimeMeasurement
void addTimeMeasurement(const double &)
Definition: VP1Prioritiser.cxx:122
VP1Prioritiser::Imp::updateSysinfoWithVisibilityState
void updateSysinfoWithVisibilityState(const QSet< IVP1ChannelWidget * > &channels, const SystemInfo::VisibilityState &vs)
Definition: VP1Prioritiser.cxx:277
IVP1System.h
VP1Prioritiser::Imp::SystemInfo::m_priority
double m_priority
Definition: VP1Prioritiser.cxx:44
IVP1System::ERASED
@ ERASED
Definition: IVP1System.h:143
VP1Prioritiser::Imp::SystemInfo::m_timing
double m_timing
Definition: VP1Prioritiser.cxx:43
IVP1ChannelWidget.h