LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
ChunkManagerImpl.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
33 #ifndef LSST_AP_CHUNK_MANAGER_IMPL_H
34 #define LSST_AP_CHUNK_MANAGER_IMPL_H
35 
36 #include <climits>
37 #include <iosfwd>
38 
39 #include "boost/noncopyable.hpp"
40 #include "boost/static_assert.hpp"
41 //#include "boost/type_traits/has_nothrow_constructor.hpp"
42 //#include "boost/type_traits/has_trivial_destructor.hpp"
43 
44 #include "Common.h"
45 #include "Bitset.h"
46 #include "Mutex.h"
47 #include "Condition.h"
48 #include "Chunk.h"
49 #include "DataTraits.h"
50 #include "Time.h"
51 
52 
53 namespace lsst { namespace ap { namespace detail {
54 
71 template <typename EntryT, int NumEntries>
72 class HashedSet : private boost::noncopyable {
73  BOOST_STATIC_ASSERT(NumEntries > 0 && (NumEntries & (NumEntries - 1)) == 0);
74  BOOST_STATIC_ASSERT(NumEntries < INT_MAX);
75 // BOOST_STATIC_ASSERT(boost::has_nothrow_constructor<EntryT>::value);
76 // BOOST_STATIC_ASSERT(boost::has_trivial_destructor<EntryT>::value);
77 
78 public :
79  HashedSet();
80 
82  EntryT * find(int const id) {
83  return const_cast<EntryT *>(doFind(id));
84  }
85 
87  EntryT const * find(int const id) const {
88  return doFind(id);
89  }
90 
91  EntryT * insert(int const id);
92 
93  std::pair<EntryT *, bool> findOrInsert(int const id);
94 
95  bool erase(int const id);
96 
98  int size() const {
99  return _size;
100  }
101 
103  int space() const {
104  return NumEntries - _size;
105  }
106 
112  EntryT * begin() {
113  return _entries;
114  }
115  EntryT const * begin() const {
116  return _entries;
117  }
118 
120  EntryT * end() {
121  return _entries + NumEntries;
122  }
123  EntryT const * end() const {
124  return _entries + NumEntries;
125  }
126 
127 private :
128  int _hashTable[2*NumEntries];
129  int _free;
130  int _size;
131  EntryT _entries[NumEntries];
132 
133  EntryT const * doFind(int const id) const;
134 };
135 
136 
148 template <typename MutexT, typename DataT, typename TraitsT = DataTraits<DataT> >
149 class BlockAllocator : private boost::noncopyable {
150 public :
151  BlockAllocator(unsigned char const * const ref, std::size_t const offset);
152 
153  std::size_t allocate();
154  void allocate(std::size_t * const blockOffsets, int const n);
155  void free(std::size_t const * const blockOffsets, int const n);
156 
157 private :
159 
160  BOOST_STATIC_ASSERT(TraitsT::ENTRIES_PER_BLOCK_LOG2 >= 9);
161 
162  static std::size_t const BLOCK_SIZE =
163  (sizeof(DataT) + sizeof(ChunkEntryFlag)) << TraitsT::ENTRIES_PER_BLOCK_LOG2;
164 
165  MutexT _mutex;
167  std::size_t const _offset;
168 };
169 
170 
172 class Visit {
173 public :
174  Visit() : _id(-1), _next(-1), _failed(false) {}
175 
176  bool failed() const {
177  return _failed;
178  }
179  void setFailed() {
180  _failed = true;
181  }
182 
183  // HashedSet requirements
184  int getId() const {
185  return _id;
186  }
187  int getNextInChain() const {
188  return _next;
189  }
190  void setId(int const id) {
191  _id = id;
192  }
193  void setNextInChain(int const id) {
194  _next = id;
195  }
196 
197 private :
198  int _id;
199  int _next;
200  bool _failed;
201 };
202 
203 
205 class VisitTracker : public HashedSet<Visit, MAX_VISITS_IN_FLIGHT> {
206 public :
207  bool isValid(int const visitId) const;
208  void print(std::ostream & os) const;
209  void print(int const visitId, std::ostream & os) const;
210 };
211 
212 
218 template <typename MutexT, typename DataT, typename TraitsT = DataTraits<DataT> >
219 class SubManager : private boost::noncopyable {
220 public :
222  typedef ChunkRef<Allocator, DataT, TraitsT> Chunk;
224 
225  // -- fields ----------------
226 
227  static int const NUM_CHUNKS = TraitsT::MAX_CHUNKS_PER_FOV * MAX_VISITS_IN_FLIGHT;
228 
231 
232  // -- methods ----------------
233 
234  SubManager(unsigned char const * const ref, std::size_t const offset) : _allocator(ref, offset) {}
235 
237  int size() const { return _chunks.size(); }
239  int space() const { return _chunks.space(); }
240 
242  std::vector<Chunk> & toRead,
243  std::vector<Chunk> & toWaitFor,
244  int const visitId,
245  std::vector<int> const & chunkIds
246  );
247 
248  bool checkForOwnership(
249  std::vector<Chunk> & toRead,
250  std::vector<Chunk> & toWaitFor,
251  int const visitId
252  );
253 
254  void getChunks(
255  std::vector<Chunk> & chunks,
256  std::vector<int> const & chunkIds
257  );
258 
259  bool relinquishOwnership(
260  int const visitId,
261  bool const rollback,
262  VisitTracker const & tracker
263  );
264 
265  void print(std::ostream & os) const;
266  void print(int const chunkId, std::ostream & os) const;
267  void printVisit(int const visitId, std::ostream & os) const;
268 };
269 
270 
287 template <
288  typename MutexT,
289  typename DataT,
290  typename TraitsT = DataTraits<DataT>
291 >
292 class ChunkManagerImpl : private boost::noncopyable {
293 public :
295  typedef typename Manager::Chunk Chunk;
296 
297 private :
299  static std::size_t blocks() {
300  return (sizeof(ChunkManagerImpl) + 511) & ~static_cast<size_t>(511);
301  }
302 
303 public :
308  static std::size_t size() {
309  return blocks() + Chunk::BLOCK_SIZE * TraitsT::NUM_BLOCKS;
310  }
311 
313 
314  bool isVisitInFlight(int const visitId);
315  void failVisit(int const visitId);
316  void registerVisit(int const visitId);
317 
318  void startVisit(
319  std::vector<Chunk> & toRead,
320  std::vector<Chunk> & toWaitFor,
321  int const visitId,
322  std::vector<int> const & chunkIds
323  );
324 
325  void waitForOwnership(
326  std::vector<Chunk> & toRead,
327  std::vector<Chunk> & toWaitFor,
328  int const visitId,
329  TimeSpec const & deadline
330  );
331 
332  void getChunks(
333  std::vector<Chunk> & chunks,
334  std::vector<int> const & chunkIds
335  );
336 
337  bool endVisit(int const visitId, bool const rollback);
338 
339  void printVisits(std::ostream & os) const;
340  void printChunks(std::ostream & os) const;
341  void printVisit(int const visitId, std::ostream & os) const;
342  void printChunk(int const chunkId, std::ostream & os) const;
343 
344 private :
345  void rollbackAllExcept(int const visitId);
346 
347  mutable MutexT _mutex;
351 };
352 
353 
354 }}} // end of namespace lsst::ap::detail
355 
356 #endif // LSST_AP_CHUNK_MANAGER_IMPL_H
A class for manipulating a fixed set of bits at the individual bit level.
A manager for a set of chunks of a single type.
BlockAllocator< MutexT, DataT, TraitsT > Allocator
Wraps the C library timespec struct.
Definition: Time.h:48
void rollbackAllExcept(int const visitId)
void printVisit(int const visitId, std::ostream &os) const
Provides basic chunk parameters at compile time.
Definition: DataTraits.h:62
bool checkForOwnership(std::vector< Chunk > &toRead, std::vector< Chunk > &toWaitFor, int const visitId)
Helper class for managing chunks of a particular type.
Tracks a set of visits.
Encapsulates a POSIX condition variable.
Definition: Condition.h:50
bool relinquishOwnership(int const visitId, bool const rollback, VisitTracker const &tracker)
unsigned char ChunkEntryFlag
Definition: Chunk.h:142
void waitForOwnership(std::vector< Chunk > &toRead, std::vector< Chunk > &toWaitFor, int const visitId, TimeSpec const &deadline)
void createOrRegisterInterest(std::vector< Chunk > &toRead, std::vector< Chunk > &toWaitFor, int const visitId, std::vector< int > const &chunkIds)
void free(std::size_t const *const blockOffsets, int const n)
BOOST_STATIC_ASSERT(TraitsT::ENTRIES_PER_BLOCK_LOG2 >=9)
ChunkDescriptor< TraitsT::MAX_BLOCKS_PER_CHUNK > Descriptor
int size() const
Returns the number of chunks under management.
void printChunk(int const chunkId, std::ostream &os) const
A thread-safe memory block allocator that uses a Bitset to track which blocks (out of a fixed size po...
static std::size_t const BLOCK_SIZE
bool isValid(int const visitId) const
int space() const
Returns the number of additional chunks that could be handled by this manager.
bool endVisit(int const visitId, bool const rollback)
EntryT const * find(int const id) const
Returns a pointer to the entry with the given identifier, or null if there is no such entry...
std::pair< EntryT *, bool > findOrInsert(int const id)
Classes for holding spatial chunks of things in memory.
bool isVisitInFlight(int const visitId)
Convenience wrapper for the C library timespec struct and a simple profiling class.
EntryT const * doFind(int const id) const
EntryT * find(int const id)
Returns a pointer to the entry with the given identifier, or null if there is no such entry...
ChunkRef< Allocator, DataT, TraitsT > Chunk
void printChunks(std::ostream &os) const
void printVisit(int const visitId, std::ostream &os) const
HashedSet< Descriptor, NUM_CHUNKS > _chunks
Master header file for the association pipeline.
A generic descriptor containing state for different kinds of chunks.
Definition: Chunk.h:85
void getChunks(std::vector< Chunk > &chunks, std::vector< int > const &chunkIds)
BlockAllocator(unsigned char const *const ref, std::size_t const offset)
Compile time constants related to data types to be stored in chunks.
Simple wrappers for POSIX mutual exclusion locks.
EntryT * insert(int const id)
void setId(int const id)
void setNextInChain(int const id)
void printVisits(std::ostream &os) const
static std::size_t blocks()
Returns the offset of the first data block (relative to its manager).
BOOST_STATIC_ASSERT(NumEntries< INT_MAX)
int id
Definition: CR.cc:151
SubManager(unsigned char const *const ref, std::size_t const offset)
int _hashTable[2 *NumEntries]
State for a single visit to a field of view.
void getChunks(std::vector< Chunk > &chunks, std::vector< int > const &chunkIds)
SubManager< MutexT, DataT, TraitsT > Manager
void print(std::ostream &os) const
A set of up to NumEntries elements of type EntryT, hashed by an integer identifier.
void startVisit(std::vector< Chunk > &toRead, std::vector< Chunk > &toWaitFor, int const visitId, std::vector< int > const &chunkIds)
void registerVisit(int const visitId)
EntryT const * end() const
Bitset< boost::uint64_t, TraitsT::NUM_BLOCKS > Allocator
EntryT const * begin() const
Simple wrapper class for POSIX condition variables.
void print(std::ostream &os) const