10 #define TRACE_NAME "RandomDelayFilter"
12 #include "art/Framework/Core/EDFilter.h"
13 #include "art/Framework/Core/ModuleMacros.h"
14 #include "art/Framework/Principal/Event.h"
15 #include "art/Framework/Principal/Handle.h"
16 #include "art/Framework/Principal/Run.h"
17 #include "art/Framework/Principal/SubRun.h"
18 #include "canvas/Utilities/InputTag.h"
19 #include "fhiclcpp/ParameterSet.h"
21 #include "artdaq/DAQdata/Globals.hh"
28 class RandomDelayFilter;
88 bool filter(art::Event& e)
override;
101 std::mt19937 engine_;
102 std::unique_ptr<std::uniform_real_distribution<double>> uniform_distn_;
103 std::unique_ptr<std::normal_distribution<double>> normal_distn_;
104 std::unique_ptr<std::exponential_distribution<double>> exponential_distn_;
105 std::unique_ptr<std::uniform_int_distribution<int>> pass_distn_;
114 DistType distribution_type_;
119 #if ART_HEX_VERSION >= 0x30200
123 min_ms_(p.get<double>(
"minimum_delay_ms", 0))
124 , max_ms_(p.get<double>(
"maximum_delay_ms", 1000))
125 , mean_ms_(p.get<double>(
"mean_delay_ms", 500))
126 , sigma_ms_(p.get<double>(
"sigma_delay_ms", 100))
127 , pass_factor_(p.get<int>(
"pass_filter_percentage", 100))
128 , load_factor_(p.get<double>(
"cpu_load_ratio", 0.5))
129 , engine_(p.get<int64_t>(
"random_seed", time(0)))
130 , pass_distn_(new std::uniform_int_distribution<int>(0, 100))
133 if (pass_factor_ > 100) pass_factor_ = 100;
134 if (pass_factor_ < 0) pass_factor_ = 0;
135 if (load_factor_ < 0.0) load_factor_ = 0.0;
136 if (load_factor_ > 1.0) load_factor_ = 1.0;
138 if (min_ms_ < 0) min_ms_ = 0;
139 if (min_ms_ > max_ms_) max_ms_ = min_ms_;
140 if (mean_ms_ < 0) mean_ms_ = 0;
141 if (sigma_ms_ < 0) sigma_ms_ = 0;
143 auto type = p.get<std::string>(
"distribution_type",
"Uniform");
144 assert(type.size() >= 1);
149 TLOG(TLVL_INFO) <<
"Generating delay times using Normal distribution with mean " << mean_ms_ <<
" ms, std. dev. " << sigma_ms_ <<
" ms, min " << min_ms_ <<
" ms and max " << max_ms_ <<
"ms.";
150 distribution_type_ = DistType::Normal;
151 if (mean_ms_ < min_ms_)
153 TLOG(TLVL_WARNING) <<
"Mean is smaller than min, setting to min";
157 normal_distn_.reset(
new std::normal_distribution<double>(mean_ms_, sigma_ms_));
161 TLOG(TLVL_INFO) <<
"Generating delay times using Exponential distribution with mean " << mean_ms_ <<
" ms, min " << min_ms_ <<
" ms and max " << max_ms_ <<
" ms.";
162 distribution_type_ = DistType::Exponential;
163 if (mean_ms_ < min_ms_)
165 TLOG(TLVL_WARNING) <<
"Mean is smaller than min, setting to min";
169 if (mean_ms_ == 0) mean_ms_ = 1;
170 exponential_distn_.reset(
new std::exponential_distribution<double>(1 / mean_ms_));
174 TLOG(TLVL_INFO) <<
"Generating delay times using Uniform distribution with min " << min_ms_ <<
" ms and max " << max_ms_ <<
" ms.";
175 distribution_type_ = DistType::Uniform;
176 uniform_distn_.reset(
new std::uniform_real_distribution<double>(min_ms_, max_ms_));
181 TLOG(TLVL_INFO) <<
"Delay time set to " << min_ms_ <<
" ms.";
182 distribution_type_ = DistType::Fixed;
189 double delay = min_ms_;
192 switch (distribution_type_)
194 case DistType::Normal:
195 delay += (*normal_distn_)(engine_);
197 case DistType::Exponential:
198 delay += (*exponential_distn_)(engine_);
200 case DistType::Uniform:
201 delay = (*uniform_distn_)(engine_);
203 case DistType::Fixed:
206 }
while (delay > max_ms_ && delay < min_ms_);
207 TLOG(TLVL_DEBUG) <<
"Simulating processing of event " << e.event() <<
" by delaying " << delay <<
"ms.";
209 usleep(1000 * (1 - load_factor_) * delay);
212 auto now = std::chrono::steady_clock::now();
213 while (TimeUtils::GetElapsedTimeMilliseconds(now) < static_cast<size_t>(delay * load_factor_))
215 i = i + 1 % std::numeric_limits<int>::max();
218 return (*pass_distn_)(engine_) < pass_factor_;
RandomDelayFilter(fhicl::ParameterSet const &p)
RandomDelayFilter Constructor.
bool filter(art::Event &e) override
Filter is a required override of art::EDFilter, and is called for each event.
A filter which delays for a random amount of time, then drops a random fraction of events...
RandomDelayFilter & operator=(RandomDelayFilter const &)=delete
Copy Assignment operator is deleted.