Skip to content

Commit e35f96d

Browse files
authored
Merge pull request #628 from astrorama/feature/max_files_option
Added option to specify max nb of simultaneous files
2 parents 16f3e29 + 42b0bd5 commit e35f96d

File tree

5 files changed

+130
-6
lines changed

5 files changed

+130
-6
lines changed

SEFramework/SEFramework/FITS/FitsImageSource.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ using Euclid::FilePool::FileHandler;
4646
class FitsImageSource : public ImageSource, public std::enable_shared_from_this<ImageSource> {
4747
public:
4848

49+
static std::shared_ptr<FileManager> getFileManager(unsigned int max_open_files = 500);
4950

5051
/**
5152
* Constructor
@@ -57,14 +58,14 @@ class FitsImageSource : public ImageSource, public std::enable_shared_from_this<
5758
*/
5859
explicit FitsImageSource(const std::string& filename, int hdu_number = 0,
5960
ImageTile::ImageType image_type = ImageTile::AutoType,
60-
std::shared_ptr<FileManager> manager = FileManager::getDefault());
61+
std::shared_ptr<FileManager> manager = getFileManager());
6162

6263
FitsImageSource(const std::string& filename, int width, int height,
6364
ImageTile::ImageType image_type,
6465
const std::shared_ptr<CoordinateSystem> coord_system = nullptr,
6566
bool append = false,
6667
bool empty_primary = false,
67-
std::shared_ptr<FileManager> manager = FileManager::getDefault());
68+
std::shared_ptr<FileManager> manager = getFileManager());
6869

6970
virtual ~FitsImageSource() = default;
7071

@@ -137,6 +138,8 @@ class FitsImageSource : public ImageSource, public std::enable_shared_from_this<
137138
ImageTile::ImageType m_image_type;
138139

139140
int m_current_layer;
141+
142+
static std::shared_ptr<FileManager> s_file_manager;
140143
};
141144

142145
}

SEFramework/src/lib/FITS/FitsImageSource.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
*/
2424

2525
#include "SEFramework/FITS/FitsImageSource.h"
26+
2627
#include "SEFramework/FITS/FitsFile.h"
2728
#include "SEUtils/VariantCast.h"
2829
#include <AlexandriaKernel/memory_tools.h>
30+
#include <FilePool/LRUFileManager.h>
2931
#include <ElementsKernel/Exception.h>
32+
#include "ElementsKernel/Logging.h"
3033
#include <boost/algorithm/string/case_conv.hpp>
3134
#include <boost/algorithm/string/trim.hpp>
3235
#include <boost/filesystem/operations.hpp>
@@ -39,6 +42,9 @@
3942

4043
namespace SourceXtractor {
4144

45+
static Elements::Logging logger = Elements::Logging::getLogger("FitsFile");
46+
47+
4248
using Euclid::make_unique;
4349

4450
namespace {
@@ -386,7 +392,18 @@ int FitsImageSource::getImageType() const {
386392
return LONGLONG_IMG;
387393
}
388394
}
389-
390395
//FIXME add missing types
391396

397+
398+
std::shared_ptr<FileManager> FitsImageSource::getFileManager(unsigned int max_open_files) {
399+
if (!s_file_manager) {
400+
logger.info() << "Creating shared LRUFileManager with limit of " << max_open_files << " open files.";
401+
s_file_manager = std::make_shared<Euclid::FilePool::LRUFileManager>(max_open_files);
402+
}
403+
return s_file_manager;
392404
}
405+
406+
std::shared_ptr<FileManager> FitsImageSource::s_file_manager = nullptr;
407+
408+
} // namespace SourceXtractor
409+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/** Copyright © 2019-2025 Université de Genève, LMU Munich - Faculty of Physics, IAP-CNRS/Sorbonne Université
2+
*
3+
* This library is free software; you can redistribute it and/or modify it under
4+
* the terms of the GNU Lesser General Public License as published by the Free
5+
* Software Foundation; either version 3.0 of the License, or (at your option)
6+
* any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11+
* details.
12+
*
13+
* You should have received a copy of the GNU Lesser General Public License
14+
* along with this library; if not, write to the Free Software Foundation, Inc.,
15+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
18+
#ifndef SEIMPLEMENTATION_CONFIGURATION_FILEMANAGERCONFIG_H
19+
#define SEIMPLEMENTATION_CONFIGURATION_FILEMANAGERCONFIG_H
20+
21+
#include "Configuration/Configuration.h"
22+
23+
namespace SourceXtractor {
24+
25+
class FileManagerConfig : public Euclid::Configuration::Configuration {
26+
public:
27+
explicit FileManagerConfig(long manager_id);
28+
virtual ~FileManagerConfig() = default;
29+
30+
std::map<std::string, Configuration::OptionDescriptionList> getProgramOptions() override;
31+
void preInitialize(const UserValues& args) override;
32+
void initialize(const UserValues& args) override;
33+
34+
int getMaxSimultaneousFiles() const {
35+
return m_max_simultaneous_files;
36+
}
37+
38+
private:
39+
int m_max_simultaneous_files;
40+
};
41+
42+
} // namespace SourceXtractor
43+
44+
#endif // SEIMPLEMENTATION_CONFIGURATION_FILEMANAGERCONFIG_H
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/** Copyright © 2019-2025 Université de Genève, LMU Munich - Faculty of Physics, IAP-CNRS/Sorbonne Université
2+
*
3+
* This library is free software; you can redistribute it and/or modify it under
4+
* the terms of the GNU Lesser General Public License as published by the Free
5+
* Software Foundation; either version 3.0 of the License, or (at your option)
6+
* any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11+
* details.
12+
*
13+
* You should have received a copy of the GNU Lesser General Public License
14+
* along with this library; if not, write to the Free Software Foundation, Inc.,
15+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
18+
#include "SEImplementation/Configuration/FileManagerConfig.h"
19+
20+
#include <boost/algorithm/string.hpp>
21+
22+
#include "ElementsKernel/Exception.h"
23+
#include "Configuration/ConfigManager.h"
24+
25+
#include "SEFramework/FITS/FitsImageSource.h"
26+
27+
#include <iostream>
28+
29+
using namespace Euclid::Configuration;
30+
namespace po = boost::program_options;
31+
32+
namespace SourceXtractor {
33+
34+
static const std::string MAX_SIMULTANEOUS_FILES {"max-simultaneous-files" };
35+
36+
37+
// Constructor
38+
FileManagerConfig::FileManagerConfig(long manager_id)
39+
: Configuration(manager_id), m_max_simultaneous_files(500) {
40+
}
41+
42+
std::map<std::string, Configuration::OptionDescriptionList> FileManagerConfig::getProgramOptions() {
43+
return { {"File Manager", {
44+
{MAX_SIMULTANEOUS_FILES.c_str(), po::value<int>()->default_value(500),
45+
"Maximum number of simultaneous open files."}
46+
}} };
47+
}
48+
49+
void FileManagerConfig::preInitialize(const UserValues& args) {
50+
// We have to configure the FileManager before any other configuration tries to use it
51+
m_max_simultaneous_files = args.find(MAX_SIMULTANEOUS_FILES)->second.as<int>();
52+
FitsImageSource::getFileManager(m_max_simultaneous_files);
53+
}
54+
55+
void FileManagerConfig::initialize(const UserValues& args) {
56+
}
57+
58+
} // namespace SourceXtractor

SEMain/src/program/SourceXtractor.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "SEImplementation/Configuration/MemoryConfig.h"
7575
#include "SEImplementation/Configuration/OutputConfig.h"
7676
#include "SEImplementation/Configuration/SamplingConfig.h"
77+
#include "SEImplementation/Configuration/FileManagerConfig.h"
7778
#include "SEImplementation/CheckImages/CheckImages.h"
7879
#include "SEImplementation/Prefetcher/Prefetcher.h"
7980

@@ -191,6 +192,7 @@ class SEMain : public Elements::Program {
191192
config_manager.registerConfiguration<BackgroundAnalyzerFactory>();
192193
config_manager.registerConfiguration<SamplingConfig>();
193194
config_manager.registerConfiguration<DetectionFrameConfig>();
195+
config_manager.registerConfiguration<FileManagerConfig>();
194196

195197
CheckImages::getInstance().reportConfigDependencies(config_manager);
196198

@@ -319,15 +321,15 @@ class SEMain : public Elements::Program {
319321
throw Elements::Exception() << "The configuration file '" << cfg_file << "' does not exist";
320322
}
321323
}
322-
324+
323325
// Create the progress listener and printer ASAP
324326
progress_printer_factory.configure(args);
325327
auto progress_mediator = progress_printer_factory.createProgressMediator();
326-
328+
327329
// Initialize the rest of the components
328330
auto& config_manager = ConfigManager::getInstance(config_manager_id);
329331
config_manager.initialize(args);
330-
332+
331333
// Configure TileManager
332334
auto memory_config = config_manager.getConfiguration<MemoryConfig>();
333335
TileManager::getInstance()->setOptions(memory_config.getTileSize(),

0 commit comments

Comments
 (0)