Skip to content
Snippets Groups Projects
Commit 31f5b61a authored by Emilio Meschi's avatar Emilio Meschi :bicyclist_tone1: Committed by Thomas Owen James
Browse files

implemented asynchronous close and rename of files

parent bf076931
No related branches found
No related tags found
No related merge requests found
#include "OutputFileHandler.h" #include "OutputFileHandler.h"
#include "log.h" #include "log.h"
#include "tools.h"
#include <iomanip> #include <iomanip>
const std::string OutputFileHandler::working_dir_ = "in_progress";
const std::string OutputFileHandler::journal_file_ = "index.journal";
tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>>
OutputFileHandler::files_to_close_ =
tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>>();
bool OutputFileHandler::file_handler_running_ = false;
void OutputFileHandler::enqueue_current_file_for_close_and_move_maybe() {
if (current_file_ != 0)
files_to_close_.push(
std::pair<std::string, FILE *>(current_filename_, current_file_));
}
FILE *OutputFileHandler::getFile(uint32_t run, uint32_t index) {
if (current_run_number_ != run || current_index_ != index) {
enqueue_current_file_for_close_and_move_maybe();
current_run_number_ = run;
current_index_ = index;
open_new_file();
}
return current_file_;
}
void OutputFileHandler::create_output_directory_maybe( void OutputFileHandler::create_output_directory_maybe(
std::string &output_directory) { std::string &output_directory) {
struct stat sb; struct stat sb;
...@@ -26,29 +51,47 @@ void OutputFileHandler::create_output_directory_maybe( ...@@ -26,29 +51,47 @@ void OutputFileHandler::create_output_directory_maybe(
LOG(TRACE) << "Created output directory: " << output_directory << "'."; LOG(TRACE) << "Created output directory: " << output_directory << "'.";
} }
void OutputFileHandler::open_file(uint32_t index, uint32_t run) { void OutputFileHandler::open_new_file() {
// Create a new file // Create a new file
create_output_directory_maybe(working_files_basepath_); create_output_directory_maybe(working_files_base_path_);
std::string filename = working_files_base_path_ + "/" + current_filename_ = format_filename(current_run_number_, current_index_);
format_filename(control.run_number, index); std::string full_filename =
LOG(TRACE) << "opening file with index " << index; working_files_base_path_ + "/" + current_filename_;
current_file_ = fopen( filename.c_str(), "wbx" )); LOG(TRACE) << "opening file with index " << current_index_;
current_file_ = fopen(full_filename.c_str(), "wbx");
if (current_file_ == NULL) { if (current_file_ == NULL) {
std::string err = tools::strerror("ERROR when creating file '" + std::string err =
current_filename_a + "'"); tools::strerror("ERROR when creating file '" + current_filename_ + "'");
LOG(ERROR) << err; LOG(ERROR) << err;
throw std::runtime_error(err); throw std::runtime_error(err);
} }
current_index_ = index;
} }
/* // Create a properly formatted file name
* Create a properly formatted file name
*/
std::string OutputFileHandler::format_filename(uint32_t run_number, std::string OutputFileHandler::format_filename(uint32_t run_number,
uint32_t index) { uint32_t index) {
std::ostringstream ofilename; std::ostringstream ofilename;
ofilename << filename_prefix_ << "_" << std::setfill(0) << std::setw(6) ofilename << filename_prefix_ << "_" << std::setfill('0') << std::setw(6)
<< run_number << "_" << index << ".dat"; << run_number << "_" << index << ".dat";
return ofilename.str(); return ofilename.str();
} }
void OutputFileHandler::close_and_rename::operator()() const {
std::pair<std::string, FILE *> n;
while (file_handler_running_ || (files_to_close_.size() > 0)) {
// std::cout << std::this_thread::get_id() << " try pop" <<
// files_to_close_.size() << std::endl;
files_to_close_.pop(n);
if (fclose(n.second) != 0) {
LOG(ERROR) << tools::strerror("File close failed");
}
std::string from = base_path_ + "/" + working_dir_ + "/" + n.first;
std::string to = base_path_ + "/" + n.first;
if (rename(from.c_str(), to.c_str()) != 0) {
LOG(ERROR) << tools::strerror("File rename failed");
}
// std::cout << std::this_thread::get_id() << " popped " << n.first << "
// size now " << files_to_close_.size() << std::endl;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment