Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
scdaq
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
scouting-demonstrator
scdaq
Commits
31f5b61a
Commit
31f5b61a
authored
2 years ago
by
Emilio Meschi
Committed by
Thomas Owen James
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
implemented asynchronous close and rename of files
parent
bf076931
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/OutputFileHandler.cc
+56
-13
56 additions, 13 deletions
src/OutputFileHandler.cc
with
56 additions
and
13 deletions
src/OutputFileHandler.cc
+
56
−
13
View file @
31f5b61a
#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;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment