Alexandria  2.19
Please provide a description of the project.
LRUFileManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
21 #include <sys/resource.h>
22 
23 namespace Euclid {
24 namespace FilePool {
25 
26 LRUFileManager::LRUFileManager(unsigned limit) : m_limit(limit) {
27  if (m_limit == 0) {
28  struct rlimit rlim;
29  getrlimit(RLIMIT_NOFILE, &rlim);
30  assert(rlim.rlim_cur > 3);
31  m_limit = rlim.rlim_cur - 3; // Account for stdout, stderr and stdin
32  }
33 }
34 
36  closeAll();
37 }
38 
39 void LRUFileManager::notifyIntentToOpen(bool /*write*/) {
41 
42  while (m_files.size() >= m_limit) {
43  bool closed = false;
44  for (auto& id : m_sorted_ids) {
45  auto& meta = m_files[id];
46  auto close_call = meta->m_request_close;
47  lock.unlock();
48  closed = close_call();
49  lock.lock();
50  // If the file was closed, the iterator on m_sorted_ids has been invalidated!
51  if (closed)
52  break;
53  }
54  if (!closed) {
55  throw Elements::Exception() << "Limit reached and failed to close any existing file descriptor";
56  }
57  }
58 }
59 
64  --m_current_pos[id];
65 }
66 
69  auto iter = m_current_pos[id];
70  m_current_pos.erase(id);
71  m_sorted_ids.erase(iter);
72 }
73 
75  // Update count
76  id->m_last_used = Clock::now();
77  ++id->m_used_count;
78 
79  // Bring it to the back, since it is the last used
81  auto iter = m_current_pos[id];
82  auto ptr = std::move(*iter);
83  m_sorted_ids.erase(iter);
86  --m_current_pos[id];
87 }
88 
89 unsigned int LRUFileManager::getLimit() const {
90  return m_limit;
91 }
92 
93 unsigned int LRUFileManager::getUsed() const {
94  return m_sorted_ids.size();
95 }
96 
97 unsigned int LRUFileManager::getAvailable() const {
98  return m_limit - m_sorted_ids.size();
99 }
100 
101 } // namespace FilePool
102 } // namespace Euclid
Euclid::FilePool::FileManager::FileId
FileMetadata * FileId
Opaque FileId, its concrete type should only be assumed to be copyable and hashable.
Definition: FileManager.h:62
std::move
T move(T... args)
LRUFileManager.h
Euclid::FilePool::LRUFileManager::getLimit
unsigned getLimit() const
Definition: LRUFileManager.cpp:89
std::map::size
T size(T... args)
Euclid::FilePool::LRUFileManager::~LRUFileManager
virtual ~LRUFileManager()
Definition: LRUFileManager.cpp:35
std::lock_guard
STL class.
Euclid::FilePool::LRUFileManager::notifyUsed
void notifyUsed(FileId id) override
Definition: LRUFileManager.cpp:74
Euclid::FilePool::LRUFileManager::getUsed
unsigned getUsed() const
Definition: LRUFileManager.cpp:93
Euclid::FilePool::LRUFileManager::notifyClosedFile
void notifyClosedFile(FileId id) override
Definition: LRUFileManager.cpp:67
Exception.h
Euclid::FilePool::LRUFileManager::getAvailable
unsigned getAvailable() const
Definition: LRUFileManager.cpp:97
std::unique_lock
STL class.
Euclid::FilePool::LRUFileManager::notifyIntentToOpen
void notifyIntentToOpen(bool write) override
Definition: LRUFileManager.cpp:39
Euclid::FilePool::LRUFileManager::notifyOpenedFile
void notifyOpenedFile(FileId id) override
Definition: LRUFileManager.cpp:60
std::map::erase
T erase(T... args)
Elements::Exception
Euclid::FilePool::FileManager::closeAll
void closeAll()
Definition: FileManager.cpp:75
Euclid::FilePool::LRUFileManager::m_sorted_ids
std::list< FileId > m_sorted_ids
Sorted from less to more recent.
Definition: LRUFileManager.h:54
Euclid::FilePool::FileManager::m_mutex
std::mutex m_mutex
Definition: FileManager.h:158
std::list::emplace_back
T emplace_back(T... args)
Euclid::FilePool::LRUFileManager::LRUFileManager
LRUFileManager(unsigned limit=500)
Definition: LRUFileManager.cpp:26
Euclid::FilePool::LRUFileManager::m_current_pos
std::map< FileId, std::list< FileId >::iterator > m_current_pos
Definition: LRUFileManager.h:55
std::list::end
T end(T... args)
Euclid::FilePool::FileManager::m_files
std::map< FileId, std::unique_ptr< FileMetadata > > m_files
Definition: FileManager.h:172
Euclid
Definition: InstOrRefHolder.h:29
Euclid::FilePool::LRUFileManager::m_limit
unsigned m_limit
Definition: LRUFileManager.h:52
std::chrono::steady_clock::now
T now(T... args)