nidas  v1.2-1520
SampleSorter.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2005, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_CORE_SAMPLESORTER_H
28 #define NIDAS_CORE_SAMPLESORTER_H
29 
30 #include "SampleThread.h"
31 #include "SampleSourceSupport.h"
32 #include "SortedSampleSet.h"
33 
34 namespace nidas { namespace core {
35 
51 class SampleSorter : public SampleThread
52 {
53 public:
54 
62  SampleSorter(const std::string& name,bool raw);
63 
64  virtual ~SampleSorter();
65 
66  void setKeepStats(bool val)
67  {
68  _source.setKeepStats(val);
69  }
70 
71  bool getKeepStats() const
72  {
73  return _source.getKeepStats();
74  }
75 
77  {
78  return _source.getRawSampleSource();
79  }
80 
82  {
84  }
85 
90  void addSampleTag(const SampleTag* tag) throw()
91  {
92  _source.addSampleTag(tag);
93  }
94 
95  void removeSampleTag(const SampleTag* tag) throw()
96  {
98  }
99 
103  std::list<const SampleTag*> getSampleTags() const
104  {
105  return _source.getSampleTags();
106  }
107 
112  {
113  return _source.getSampleTagIterator();
114  }
115 
119  void addSampleClient(SampleClient* client) throw()
120  {
121  _source.addSampleClient(client);
122  }
123 
124  void removeSampleClient(SampleClient* client) throw()
125  {
126  _source.removeSampleClient(client);
127  }
128 
133  void addSampleClientForTag(SampleClient* client,const SampleTag* tag) throw()
134  {
135  _source.addSampleClientForTag(client,tag);
136  }
137 
138  void removeSampleClientForTag(SampleClient* client,const SampleTag* tag) throw()
139  {
140  _source.removeSampleClientForTag(client,tag);
141  }
142 
143  int getClientCount() const throw()
144  {
145  return _source.getClientCount();
146  }
147 
149  {
150  return _source.getSampleStats();
151  }
152 
158  void flush() throw();
159 
163  void interrupt();
164 
168  bool receive(const Sample *s) throw();
169 
177  size_t size() const { return _samples.size(); }
178 
179  void setLengthSecs(float val)
180  {
181  _sorterLengthUsec = (unsigned int)((double)val * USECS_PER_SEC);
182  }
183 
184  float getLengthSecs() const
185  {
186  return (double)_sorterLengthUsec / USECS_PER_SEC;
187  }
188 
193  void setHeapMax(size_t val) { _heapMax = val; }
194 
195  size_t getHeapMax() const { return _heapMax; }
196 
200  size_t getHeapSize() const { return _heapSize; }
201 
209  void setHeapBlock(bool val) { _heapBlock = val; }
210 
211  bool getHeapBlock() const { return _heapBlock; }
212 
217  size_t getNumDiscardedSamples() const
218  {
219  return _discardedSamples;
220  }
221 
226  size_t getNumFutureSamples() const
227  {
228  return _realTimeFutureSamples;
229  }
230 
234  size_t getNumEarlySamples() const
235  {
236  return _earlySamples;
237  }
243  void setRealTime(bool val)
244  {
245  _realTime = val;
246  }
247 
248  bool getRealTime() const
249  {
250  return _realTime;
251  }
252 
263  void setLateSampleCacheSize(unsigned int val)
264  {
265  _lateSampleCacheSize = val;
266  }
267 
268  unsigned int getLateSampleCacheSize() const
269  {
270  return _lateSampleCacheSize;
271  }
272 
273 private:
274 
276 
280  int run() throw(nidas::util::Exception);
281 
285  unsigned int _sorterLengthUsec;
286 
288 
294  void heapDecrement(size_t bytes);
295 
296  nidas::util::Cond _sampleSetCond;
297 
298  nidas::util::Cond _flushCond;
299 
304  size_t _heapMax;
305 
309  size_t _heapSize;
310 
323 
324  nidas::util::Cond _heapCond;
325 
327 
332  unsigned int _discardedSamples;
333 
339 
347  unsigned int _earlySamples;
348 
353 
358 
359  bool _doFlush;
360 
361  bool _flushed;
362 
364 
370  bool _realTime;
371 
373 
374  unsigned int _lateSampleCacheSize;
375 
379  SampleSorter(const SampleSorter&);
380 
384  SampleSorter& operator=(const SampleSorter&);
385 
386 };
387 
388 }} // namespace nidas namespace core
389 
390 #endif
void setKeepStats(bool val)
Definition: SampleSourceSupport.h:154
size_t getNumEarlySamples() const
Number of early samples, which may not be sorted.
Definition: SampleSorter.h:234
SampleT< char > _dummy
Definition: SampleSorter.h:363
unsigned int _earlySamples
Samples which are earlier than the current latest sample in the sorter minus the sorter length...
Definition: SampleSorter.h:347
void setLateSampleCacheSize(unsigned int val)
When aging-off samples, cache this number of samples with the latest time tags.
Definition: SampleSorter.h:263
bool _flushed
Definition: SampleSorter.h:361
int _earlyWarningCount
How often to log warnings about early samples.
Definition: SampleSorter.h:357
bool _heapBlock
_heapBlock controls what happens when the number of bytes in _samples exceeds _heapMax.
Definition: SampleSorter.h:322
Interface for a Thread for buffering samples.
Definition: SampleThread.h:52
SampleSource * getProcessedSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleSorter.h:81
unsigned int _lateSampleCacheSize
Definition: SampleSorter.h:374
size_t _heapSize
Current heap size, in bytes.
Definition: SampleSorter.h:309
void setRealTime(bool val)
Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against t...
Definition: SampleSorter.h:243
const SampleStats & getSampleStats() const
Definition: SampleSorter.h:148
std::list< const SampleTag * > getSampleTags() const
Implementation of SampleSource::getSampleTags().
Definition: SampleSorter.h:103
void removeSampleTag(const SampleTag *tag)
Definition: SampleSourceSupport.cc:67
SampleTagIterator getSampleTagIterator() const
Implementation of SampleSource::getSampleTagIterator().
Definition: SampleSorter.h:111
size_t getNumFutureSamples() const
Number of samples discarded because their timetags were in the future.
Definition: SampleSorter.h:226
SortedSampleSet _samples
Definition: SampleSorter.h:287
virtual ~SampleSorter()
Definition: SampleSorter.cc:106
unsigned int _realTimeFutureSamples
Number of samples discarded because getRealTime() is true and the samples have timetags later than th...
Definition: SampleSorter.h:338
Pure virtual interface for a source of Samples.
Definition: SampleSource.h:48
SampleTagIterator getSampleTagIterator() const
Definition: SampleSourceSupport.cc:75
size_t getHeapSize() const
Get the current amount of heap being used for sorting.
Definition: SampleSorter.h:200
void interrupt()
Interrupt sorting thread.
Definition: SampleSorter.cc:367
SampleSorter(const std::string &name, bool raw)
Constructor.
Definition: SampleSorter.cc:82
void removeSampleTag(const SampleTag *tag)
Definition: SampleSorter.h:95
nidas::util::Cond _flushCond
Definition: SampleSorter.h:298
A source of samples.
Definition: SampleSourceSupport.h:47
unsigned int _discardedSamples
Number of samples discarded because of _heapSize &gt; _heapMax and heapBlock == true.
Definition: SampleSorter.h:332
void removeSampleClient(SampleClient *c)
Remove a SampleClient from this SampleSource This will also remove a SampleClient if it has been adde...
Definition: SampleSourceSupport.cc:88
bool getKeepStats() const
Definition: SampleSorter.h:71
bool getKeepStats() const
Definition: SampleSourceSupport.h:159
bool _heapExceeded
Definition: SampleSorter.h:326
void addSampleTag(const SampleTag *tag)
How to tell this SampleSorter what sample tags it will be sorting.
Definition: SampleSorter.h:90
void removeSampleClientForTag(SampleClient *c, const SampleTag *)
Add a SampleClient to this SampleSource.
Definition: SampleSourceSupport.cc:121
std::multiset< const Sample *, SampleTimetagComparator > SortedSampleSet
A multiset for storing samples sorted by timetag.
Definition: SortedSampleSet.h:107
void addSampleClient(SampleClient *c)
Add a SampleClient to this SampleSource.
Definition: SampleSourceSupport.cc:80
Pure virtual interface of a client of Samples.
Definition: SampleClient.h:38
A SampleClient that sorts its received samples, using an STL multiset, and then sends the sorted samp...
Definition: SampleSorter.h:51
SampleSource * getRawSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleSorter.h:76
bool getRealTime() const
Definition: SampleSorter.h:248
void setHeapBlock(bool val)
Definition: SampleSorter.h:209
unsigned int _sorterLengthUsec
Length of SampleSorter, in micro-seconds.
Definition: SampleSorter.h:285
const SampleStats & getSampleStats() const
Definition: SampleSourceSupport.h:149
int _discardWarningCount
How often to log warnings about discardedSamples.
Definition: SampleSorter.h:352
int getClientCount() const
How many SampleClients are currently in my list.
Definition: SampleSorter.h:143
void setKeepStats(bool val)
Definition: SampleSorter.h:66
long long _maxSorterLengthUsec
Definition: SampleSorter.h:372
void heapDecrement(size_t bytes)
Utility function to decrement the heap size after writing one or more samples.
Definition: SampleSorter.cc:406
A typed Sample, with data of type DataT.
Definition: Sample.h:393
Interface to a data sample.
Definition: Sample.h:189
SampleSource * getRawSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleSourceSupport.h:60
Class for iterating over the SampleTags of a Project, Site, DSMConfig, or a SampleSource.
Definition: NidsIterators.h:217
void setHeapMax(size_t val)
Set the maximum amount of heap memory to use for sorting samples.
Definition: SampleSorter.h:193
bool receive(const Sample *s)
Implementation of SampleClient::receive().
Definition: SampleSorter.cc:496
Class describing a group of variables that are sampled and handled together.
Definition: SampleTag.h:87
void addSampleClientForTag(SampleClient *c, const SampleTag *)
Add a SampleClient to this SampleSource.
Definition: SampleSourceSupport.cc:101
void removeSampleClientForTag(SampleClient *client, const SampleTag *tag)
Remove a SampleClient for a given SampleTag from this SampleSource.
Definition: SampleSorter.h:138
void removeSampleClient(SampleClient *client)
Remove a SampleClient from this SampleSource.
Definition: SampleSorter.h:124
void setLengthSecs(float val)
Definition: SampleSorter.h:179
A source of samples.
Definition: SampleStats.h:41
#define USECS_PER_SEC
Definition: ublox.cc:59
void addSampleClient(SampleClient *client)
Implementation of SampleSource::addSampleClient().
Definition: SampleSorter.h:119
unsigned int getLateSampleCacheSize() const
Definition: SampleSorter.h:268
bool _doFlush
Definition: SampleSorter.h:359
size_t size() const
Current number of samples in the sorter.
Definition: SampleSorter.h:177
bool _realTime
Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against t...
Definition: SampleSorter.h:370
SampleSourceSupport _source
Definition: SampleSorter.h:275
float getLengthSecs() const
Definition: SampleSorter.h:184
int getClientCount() const
How many SampleClients are currently in my list.
Definition: SampleSourceSupport.cc:136
size_t getNumDiscardedSamples() const
Number of samples discarded because of _heapSize &gt; _heapMax and heapBlock == true.
Definition: SampleSorter.h:217
void flush()
Implementation of SampleSource::flush().
Definition: SampleSorter.cc:434
std::list< const SampleTag * > getSampleTags() const
What SampleTags am I a SampleSource for?
Definition: SampleSourceSupport.cc:54
size_t getHeapMax() const
Definition: SampleSorter.h:195
bool getHeapBlock() const
Definition: SampleSorter.h:211
size_t _heapMax
Limit on the maximum size of memory to use while buffering samples.
Definition: SampleSorter.h:304
nidas::util::Cond _sampleSetCond
Definition: SampleSorter.h:296
int run()
Thread run function.
Definition: SampleSorter.cc:144
SampleSource * getProcessedSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleSourceSupport.h:66
nidas::util::Cond _heapCond
Definition: SampleSorter.h:324
void addSampleClientForTag(SampleClient *client, const SampleTag *tag)
Add a Client for a given SampleTag.
Definition: SampleSorter.h:133
void addSampleTag(const SampleTag *tag)
Add a SampleTag to this SampleSource.
Definition: SampleSourceSupport.cc:60