5 #ifndef IBIS_FILEMANAGER_H
6 #define IBIS_FILEMANAGER_H
37 ACCESS_PREFERENCE pref=MMAP_LARGE_FILES);
40 ACCESS_PREFERENCE pref=MMAP_LARGE_FILES);
54 static time_t
iBeat() {
return hbeat++;}
56 const double&
pageCount()
const {
return page_count;}
60 static inline void increaseUse(
size_t inc,
const char* evt);
61 static inline void decreaseUse(
size_t dec,
const char* evt);
68 virtual void operator()()
const = 0;
76 #if defined(HAVE_FILE_MAP)
86 const off_t b,
const off_t e);
127 template <
typename T>
142 size_t size()
const {
return nbuf;}
144 size_t resize(
size_t sz=0);
183 typedef std::map<
const char*,
roFile*,
185 typedef std::set< const cleaner* > cleanerList;
186 typedef std::set< const char*, std::less< const char* > > nameList;
194 cleanerList cleaners;
202 pthread_cond_t readCond;
207 mutable pthread_mutex_t mutex;
210 mutable pthread_cond_t cond;
215 static uint32_t pagesize;
217 int unload(
size_t size);
218 void invokeCleaners()
const;
243 storage(
const char* fname,
const off_t begin,
const off_t end);
244 storage(
const int fdes,
const off_t begin,
const off_t end);
245 storage(
const char* begin,
const char* end);
246 storage(
char* addr,
size_t num);
256 bool empty()
const {
return (m_begin == 0 || m_begin >= m_end);}
259 return (m_begin!=0 && m_begin<m_end ? m_end-m_begin : 0);}
262 return (m_begin!=0 && m_begin<m_end ? m_end-m_begin : 0);}
287 off_t
read(
const char* fname,
const off_t begin,
const off_t end);
288 off_t
read(
const int fdes,
const off_t begin,
const off_t end);
289 void write(
const char* file)
const;
311 virtual void clear();
336 void read(
const char* file);
337 #if defined(HAVE_FILE_MAP)
338 void mapFile(
const char* file);
350 void doRead(
const char* file);
352 void doRead(
const char* file, off_t b, off_t e);
353 #if defined(HAVE_FILE_MAP)
354 void doMap(
const char* file, off_t b, off_t e,
int opt=0);
362 time_t now = time(0);
364 sc =
static_cast<float>(1e-4 *
size() +
nacc);
366 else if (lastUse >= now) {
367 sc =
static_cast<float>(sqrt(5e-6*
size()) + nacc +
371 sc =
static_cast<float>((sqrt(1e-6*
size() + now - opened) +
372 (
static_cast<double>(
nacc) /
373 (now - opened))) / (now - lastUse));
379 virtual void clear();
382 void printBody(std::ostream& out)
const;
392 #if defined(_WIN32) && defined(_MSC_VER)
396 #elif (HAVE_MMAP+0 > 0)
407 #if defined(HAVE_FILE_MAP)
408 class FASTBIT_CXX_DLLSPEC
ibis::fileManager::rofSegment
414 rofSegment(
const char *fn, off_t b, off_t e);
415 virtual ~rofSegment() {};
420 rofSegment(
const rofSegment&);
421 rofSegment& operator=(
const rofSegment&);
423 std::string filename_;
522 if (stop - start > 0) {
523 start = (start / pagesize) * pagesize;
524 page_count += ceil(static_cast<double>((stop - start)) / pagesize);
529 ibis::fileManager::increaseUse(
size_t inc,
const char* evt) {
531 LOGGER(inc > 0 && evt != 0 && *evt != 0 && ibis::gVerbose > 9)
532 << evt <<
" added " << inc <<
" bytes to increase totalBytes to "
537 ibis::fileManager::decreaseUse(
size_t dec,
const char* evt) {
539 LOGGER(dec > 0 && evt != 0 && *evt != 0 && ibis::gVerbose > 9)
540 << evt <<
" removed " << dec <<
" bytes to decrease totalBytes to "
555 {
char* tmp = name; name = rhs.
name; rhs.
name = tmp;}
557 {
char* tmp = m_end; m_end = rhs.
m_end; rhs.
m_end = tmp;}
558 {
unsigned itmp = nacc; nacc = rhs.
nacc; rhs.
nacc = itmp;}
561 #endif // IBIS_FILEMANAGER_H
storage()
Constructor. Allocate no real storage.
Definition: fileManager.cpp:1844
int64_t write(int, const void *, int64_t)
A wrapper over POSIX write function.
Definition: util.cpp:999
void flushFile(const char *name)
Close the file, remove the record about it from the file manager.
Definition: fileManager.cpp:221
virtual void clear()
Freeing the storage allocated.
Definition: fileManager.cpp:2548
~fileManager()
Destructor.
Definition: fileManager.cpp:692
static storage * getFileSegment(const char *name, const int fdes, const off_t b, const off_t e)
Retrieve a portion of a file content.
Definition: fileManager.cpp:1248
A unsigned 64-bit shared integer class.
Definition: util.h:1086
static time_t iBeat()
Returns the value of a simple counter. It is not thread-safe!
Definition: fileManager.h:54
T & operator[](size_t i)
Return the ith value. It does not perform array bounds check!
Definition: fileManager.h:136
const double & pageCount() const
Returns the number of pages accessed by function read from stdlib.h.
Definition: fileManager.h:56
static unsigned int maxOpenFiles
The Maximum number of files that can be kept open at the same time.
Definition: fileManager.h:176
virtual void beginUse()
Record a new active reference to this object.
Definition: fileManager.cpp:2464
size_t size() const
Return the size (bytes) of the object.
Definition: fileManager.h:258
The storage class treats all memory as char*.
Definition: fileManager.h:237
char * begin()
Starting address of the storage object.
Definition: fileManager.h:268
virtual void beginUse()
Start using a file. Increments the active reference.
Definition: fileManager.cpp:2510
const char * end() const
Ending address of the storage object.
Definition: fileManager.h:270
virtual bool isFileMap() const
Is the storage a file map ?
Definition: fileManager.h:284
static ibis::util::sharedInt64 totalBytes
The total number of bytes of all managed objects.
Definition: fileManager.h:172
void swap(buffer< T > &other)
Swap the content of two buffers.
Definition: fileManager.h:146
char * name
Name of the file. NULL (0) if no file is involved.
Definition: fileManager.h:301
The current implementation of FastBit is code named IBIS; most data structures and functions are in t...
Definition: bord.h:16
const char * begin() const
Starting address of the storage object.
Definition: fileManager.h:272
bool empty() const
Is the storage object empty?
Definition: fileManager.h:256
static int adjustCacheSize(uint64_t)
Change the size of memory cache allocated to the file manager.
Definition: fileManager.cpp:373
virtual void clear()
Actually freeing the storage allocated.
Definition: fileManager.cpp:2226
float score() const
The function assigns a score to a file.
Definition: fileManager.h:360
static uint64_t maxBytes
The maximum number of bytes allowed.
Definition: fileManager.h:174
size_t size() const
The number of elements in the buffer. NOT the number of bytes.
Definition: fileManager.h:142
void removeCleaner(const cleaner *cl)
Unregister the cleaner functor.
Definition: fileManager.cpp:499
virtual void * release()
Release the control of the memory to the caller as a raw pointer.
Definition: fileManager.cpp:2261
virtual void * release()
Release the control of the memory to the caller as a raw pointer.
Definition: fileManager.h:380
void unrecordFile(roFile *)
Remove the file name from the list of files tracked.
Definition: fileManager.cpp:780
char * m_end
End of the storage.
Definition: fileManager.h:305
uint64_t getMaxMmapBytes() const
Return the size in bytes of files that are memory mapped.
Definition: fileManager.cpp:399
static uint32_t pageSize()
Returns the page size (in bytes) used by the file system.
Definition: fileManager.h:58
A buffer is intended to be a temporary workspace in memory.
Definition: fileManager.h:128
unsigned pastUse() const
Number of past accesses to this object.
Definition: fileManager.h:281
void swap(storage &rhs)
Swap the content of the storage objects.
Definition: fileManager.h:554
T * address() const
Address of the buffer allocated.
Definition: fileManager.h:140
storage & operator=(const storage &rhs)
Assignment operator.
Definition: fileManager.cpp:2189
This fileManager is intended to allow different objects to share the same open file.
Definition: fileManager.h:23
int getFile(const char *name, array_t< T > &arr, ACCESS_PREFERENCE pref=MMAP_LARGE_FILES)
Given a file name, place the content in an array_t.
Definition: fileManager.cpp:122
void doRead(const char *file)
Read the content of a file into memory.
Definition: fileManager.cpp:2675
Template array_t is a replacement of std::vector.
Definition: array_t.h:24
ACCESS_PREFERENCE
Hint passed to the function getFile.
Definition: fileManager.h:29
ibis::util::sharedInt32 nref
Number of (active) references to this storage.
Definition: fileManager.h:309
Defines minor utility functions and common classes used by FastBit.
virtual void printStatus(std::ostream &out) const
Print information about the storage object to the specified output stream.
Definition: fileManager.cpp:2610
static uint64_t bytesInUse()
Returns the number of bytes currently on records.
Definition: fileManager.h:115
const T & operator[](size_t i) const
Return the ith value. It does not perform array bounds check!
Definition: fileManager.h:138
unsigned nacc
Number of accesses in the past.
Definition: fileManager.h:307
void flushDir(const char *name)
Close all files in the named directory, including subdirectories.
Definition: fileManager.cpp:268
void printStatus(std::ostream &out) const
Prints status information about the file manager.
Definition: fileManager.cpp:182
char operator[](size_t i) const
Unchecked index operator. Returns the character at position i.
Definition: fileManager.h:274
void copy(const storage &rhs)
Copy function. Make an in-meory copy following the copy-and-swap idiom.
Definition: fileManager.cpp:2197
void enlarge(size_t nelm=0)
Enlarge the current storage object.
Definition: fileManager.cpp:2206
void clear()
Close all files and remove all records of them.
Definition: fileManager.cpp:422
void addCleaner(const cleaner *cl)
Register an external cleaner functor.
Definition: fileManager.cpp:489
static fileManager & instance()
Returns a pointer to the one and only file manager.
Definition: fileManager.cpp:507
virtual void endUse()
Record the termination of an active reference.
Definition: fileManager.cpp:2475
void signalMemoryAvailable() const
Signal to the file manager that some memory have been freed.
Definition: fileManager.cpp:1631
size_t bytes() const
Return the number of bytes contained in the object.
Definition: fileManager.h:261
This class manages content of a whole (read-only) file.
Definition: fileManager.h:322
void recordFile(roFile *)
Record a newly allocated storage in the two lists.
Definition: fileManager.cpp:705
virtual void printStatus(std::ostream &out) const
Print information about the storage object to the specified output stream.
Definition: fileManager.cpp:2295
char * m_begin
Beginning of the storage.
Definition: fileManager.h:303
roFile()
Constructor.
Definition: fileManager.cpp:2496
static uint64_t bytesFree()
Return the number of bytes free.
Definition: fileManager.h:460
unsigned int getMaxOpenMmapFiles() const
Return the count of files that are memory mapped.
Definition: fileManager.cpp:394
const char * filename() const
Pointer to the file name supporting this storage object.
Definition: fileManager.h:253
fileManager()
The protected constructor of the ibis::fileManager class.
Definition: fileManager.cpp:538
void recordPages(off_t start, off_t stop)
Given the starting and ending addresses, this function computes the number of pages involved...
Definition: fileManager.h:521
unsigned inUse() const
Number of current accesses to this object.
Definition: fileManager.h:279
virtual void endUse()
Stop using a file. Decrement the active reference count.
Definition: fileManager.cpp:2526
A shared unsigned 32-bit integer class.
Definition: util.h:945
A function object to be used to register external cleaners.
Definition: fileManager.h:66
int tryGetFile(const char *name, array_t< T > &arr, ACCESS_PREFERENCE pref=MMAP_LARGE_FILES)
Given a file name, place the content in an array_t.
Definition: fileManager.cpp:155
off_t read(const char *fname, const off_t begin, const off_t end)
Read a part of a file.
Definition: fileManager.cpp:2315
virtual bool isFileMap() const
Is the storage a file map ?
Definition: fileManager.h:331
static uint64_t currentCacheSize()
A read lock on the file manager. Any object using a file under the.
Definition: fileManager.h:111
int disconnectFile()
Disconnect the storage object from the file.
Definition: fileManager.cpp:2596