Skip to content

Commit ef9bb44

Browse files
committed
Factor out timeout functionality
1 parent 0d6a29c commit ef9bb44

1 file changed

Lines changed: 49 additions & 31 deletions

File tree

src/Series.cpp

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,52 @@ namespace
127127
int padding,
128128
std::string const &postfix,
129129
std::optional<std::string> const &extension);
130+
131+
struct TimeoutLazyParsing
132+
{
133+
using Clock = std::chrono::system_clock;
134+
Clock::time_point start_parsing, time_of_last_warning;
135+
uint64_t timeout;
136+
bool printed_warning_already = false;
137+
138+
TimeoutLazyParsing(uint64_t timeout_in) : timeout(timeout_in)
139+
{
140+
if (timeout > 0)
141+
{
142+
start_parsing = Clock::now();
143+
time_of_last_warning = start_parsing;
144+
}
145+
}
146+
147+
void now(size_t current_iteration_count, size_t total_iteration_count)
148+
{
149+
if (timeout == 0)
150+
{
151+
return;
152+
}
153+
auto current = Clock::now();
154+
auto diff = std::chrono::duration_cast<std::chrono::seconds>(
155+
current - time_of_last_warning);
156+
if (uint64_t(diff.count()) >= timeout)
157+
{
158+
auto total_diff =
159+
std::chrono::duration_cast<std::chrono::seconds>(
160+
current - start_parsing);
161+
if (!printed_warning_already)
162+
{
163+
std::cerr
164+
<< R"(Warning: Parsing Iterations is taking a long time. Consider setting {"defer_iteration_parsing": true} for lazy opening of the Series and then explicitly opening Iterations with Iteration::open(). Refer also to the documentation at https://openpmd-api.readthedocs.io. Suppress this warning by setting {"hint_lazy_parsing_timeout": 0}.)"
165+
<< '\n';
166+
printed_warning_already = true;
167+
}
168+
std::cerr << "Elapsed time: " << total_diff.count()
169+
<< "s, parsed " << current_iteration_count << " of "
170+
<< total_iteration_count << " Iterations."
171+
<< std::endl;
172+
time_of_last_warning = current;
173+
}
174+
}
175+
};
130176
} // namespace
131177

132178
struct Series::ParsedInput
@@ -1739,15 +1785,7 @@ void Series::readFileBased(
17391785
bool atLeastOneIterationSuccessful = false;
17401786
std::optional<error::ReadError> forwardFirstError;
17411787

1742-
using Clock = std::chrono::system_clock;
1743-
Clock::time_point start_parsing, time_of_last_warning;
1744-
auto timeout = series.m_hintLazyParsingAfterTimeout;
1745-
bool printed_warning_already = false;
1746-
if (timeout > 0)
1747-
{
1748-
start_parsing = Clock::now();
1749-
time_of_last_warning = start_parsing;
1750-
}
1788+
TimeoutLazyParsing timeout(series.m_hintLazyParsingAfterTimeout);
17511789

17521790
size_t read_iterations = 0;
17531791
for (auto &iteration : series.iterations)
@@ -1757,29 +1795,9 @@ void Series::readFileBased(
17571795
{
17581796
continue;
17591797
}
1760-
if (timeout > 0 && !read_only_this_single_iteration.has_value())
1798+
if (!read_only_this_single_iteration.has_value())
17611799
{
1762-
auto current = Clock::now();
1763-
auto diff = std::chrono::duration_cast<std::chrono::seconds>(
1764-
current - time_of_last_warning);
1765-
if (uint64_t(diff.count()) >= timeout)
1766-
{
1767-
auto total_diff =
1768-
std::chrono::duration_cast<std::chrono::seconds>(
1769-
current - start_parsing);
1770-
if (!printed_warning_already)
1771-
{
1772-
std::cerr
1773-
<< R"(Warning: Parsing Iterations is taking a long time. Consider setting {"defer_iteration_parsing": true} for lazy opening of the Series and then explicitly opening Iterations with Iteration::open(). Refer also to the documentation at https://openpmd-api.readthedocs.io. Suppress this warning by setting {"hint_lazy_parsing_timeout": 0}.)"
1774-
<< '\n';
1775-
printed_warning_already = true;
1776-
}
1777-
std::cerr << "Elapsed time: " << total_diff.count()
1778-
<< "s, parsed " << read_iterations << " of "
1779-
<< series.iterations.size() << " Iterations."
1780-
<< std::endl;
1781-
time_of_last_warning = current;
1782-
}
1800+
timeout.now(read_iterations, iterations.size());
17831801
}
17841802
if (auto error = readIterationEagerly(iteration.second); error)
17851803
{

0 commit comments

Comments
 (0)