00001 #define TRACE_NAME "FragmentReceiverManager"
00002
00003 #include <chrono>
00004
00005 #include "artdaq/DAQrate/FragmentReceiverManager.hh"
00006 #include "artdaq/DAQdata/Globals.hh"
00007 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
00008 #include "cetlib_except/exception.h"
00009
00010 artdaq::FragmentReceiverManager::FragmentReceiverManager(const fhicl::ParameterSet& pset)
00011 : stop_requested_(false)
00012 , source_threads_()
00013 , source_plugins_()
00014 , source_metric_data_()
00015 , source_metric_send_time_()
00016 , enabled_sources_()
00017 , fragment_store_()
00018 , recv_frag_count_()
00019 , recv_frag_size_()
00020 , recv_seq_count_()
00021 , suppress_noisy_senders_(pset.get<bool>("auto_suppression_enabled", true))
00022 , suppression_threshold_(pset.get<size_t>("max_receive_difference", 50))
00023 , receive_timeout_(pset.get<size_t>("receive_timeout_usec", 100000))
00024 , last_source_(-1)
00025 {
00026 TLOG(TLVL_DEBUG) << "Constructor" ;
00027 auto enabled_srcs = pset.get<std::vector<int>>("enabled_sources", std::vector<int>());
00028 auto enabled_srcs_empty = enabled_srcs.size() == 0;
00029 if (enabled_srcs_empty)
00030 {
00031 TLOG(TLVL_INFO) << "enabled_sources not specified, assuming all sources enabled." ;
00032 }
00033 else
00034 {
00035 for (auto& s : enabled_srcs)
00036 {
00037 enabled_sources_[s] = true;
00038 }
00039 }
00040
00041 auto srcs = pset.get<fhicl::ParameterSet>("sources", fhicl::ParameterSet());
00042 for (auto& s : srcs.get_pset_names())
00043 {
00044 try
00045 {
00046 auto transfer = std::unique_ptr<TransferInterface>(MakeTransferPlugin(srcs, s,
00047 TransferInterface::Role::kReceive));
00048 auto source_rank = transfer->source_rank();
00049 if (enabled_srcs_empty) enabled_sources_[source_rank] = true;
00050 else if (!enabled_sources_.count(source_rank)) enabled_sources_[source_rank] = false;
00051 running_sources_[source_rank] = false;
00052 source_plugins_[source_rank] = std::move(transfer);
00053 fragment_store_[source_rank];
00054 source_metric_send_time_[source_rank] = std::chrono::steady_clock::now();
00055 source_metric_data_[source_rank] = std::pair<size_t, double>();
00056 }
00057 catch (cet::exception ex)
00058 {
00059 TLOG(TLVL_WARNING) << "cet::exception caught while setting up source " << s << ": " << ex.what() ;
00060 }
00061 catch (std::exception ex)
00062 {
00063 TLOG(TLVL_WARNING) << "std::exception caught while setting up source " << s << ": " << ex.what() ;
00064 }
00065 catch (...)
00066 {
00067 TLOG(TLVL_WARNING) << "Non-cet exception caught while setting up source " << s << "." ;
00068 }
00069 }
00070 if (srcs.get_pset_names().size() == 0)
00071 {
00072 TLOG(TLVL_ERROR) << "No sources configured!" ;
00073 }
00074 }
00075
00076 artdaq::FragmentReceiverManager::~FragmentReceiverManager()
00077 {
00078 TLOG(TLVL_DEBUG) << "Destructor" ;
00079 TLOG(5) << "~FragmentReceiverManager: BEGIN: Setting stop_requested to true, frags=" << count() << ", bytes=" << byteCount() ;
00080 stop_requested_ = true;
00081
00082 TLOG(5) << "~FragmentReceiverManager: Notifying all threads" ;
00083 output_cv_.notify_all();
00084
00085 TLOG(5) << "~FragmentReceiverManager: Joining all threads" ;
00086 for (auto& s : source_threads_)
00087 {
00088 auto& thread = s.second;
00089 if (thread.joinable()) thread.join();
00090 }
00091 TLOG(5) << "~FragmentReceiverManager: DONE" ;
00092 }
00093
00094 bool artdaq::FragmentReceiverManager::fragments_ready_() const
00095 {
00096 for (auto& it : fragment_store_)
00097 {
00098 if (!enabled_sources_.count(it.first)) continue;
00099 if (!it.second.empty()) { return true; }
00100 }
00101 return false;
00102 }
00103
00104 int artdaq::FragmentReceiverManager::get_next_source_() const
00105 {
00106
00107 std::set<int> ready_sources;
00108 for (auto& it : fragment_store_)
00109 {
00110 if (!enabled_sources_.count(it.first)) continue;
00111 if (!it.second.empty()) {
00112 ready_sources.insert(it.first);
00113 }
00114 }
00115
00116 if (ready_sources.size()) {
00117 auto iter = ready_sources.find(last_source_);
00118 if (iter == ready_sources.end() || ++iter == ready_sources.end()) {
00119 TLOG(10) << "get_next_source returning " << *ready_sources.begin();
00120 last_source_ = *ready_sources.begin();
00121 return *ready_sources.begin();
00122 }
00123
00124 TLOG(10) << "get_next_source returning " << *iter;
00125 last_source_ = *iter;
00126 return *iter;
00127 }
00128
00129 TLOG(10) << "get_next_source returning -1";
00130 return -1;
00131 }
00132
00133 void artdaq::FragmentReceiverManager::start_threads()
00134 {
00135 for (auto& source : source_plugins_)
00136 {
00137 auto& rank = source.first;
00138 if (enabled_sources_.count(rank))
00139 {
00140 running_sources_[rank] = true;
00141 source_threads_[rank] = boost::thread(&FragmentReceiverManager::runReceiver_, this, rank);
00142 }
00143 }
00144 }
00145
00146 artdaq::FragmentPtr artdaq::FragmentReceiverManager::recvFragment(int& rank, size_t timeout_usec)
00147 {
00148 TLOG(5) <<"recvFragment entered tmo=" << timeout_usec << " us" ;
00149
00150 if (timeout_usec == 0) timeout_usec = 1000000;
00151
00152 auto ready = fragments_ready_();
00153 size_t waited = 0;
00154 auto wait_amount = timeout_usec / 1000 > 1000 ? timeout_usec / 1000 : 1000;
00155 TLOG(5) << "recvFragment fragment_ready_=" << ready << " before wait" ;
00156 while (!ready && waited < timeout_usec)
00157 {
00158 {
00159 std::unique_lock<std::mutex> lck(input_cv_mutex_);
00160 input_cv_.wait_for(lck, std::chrono::microseconds(wait_amount));
00161 }
00162 waited += wait_amount;
00163 ready = fragments_ready_();
00164 if (running_sources().size() == 0) break;
00165 }
00166 TLOG(5) << "recvFragment fragment_ready_=" << ready << " after waited=" << waited ;
00167 if (!ready)
00168 {
00169 TLOG(5) << "recvFragment: No fragments ready, returning empty" ;
00170 rank = TransferInterface::RECV_TIMEOUT;
00171 return std::unique_ptr<Fragment>{};
00172 }
00173
00174 int current_source = get_next_source_();
00175 FragmentPtr current_fragment = fragment_store_[current_source].front();
00176 output_cv_.notify_all();
00177 rank = current_source;
00178
00179 if (current_fragment != nullptr)
00180 TLOG(5) << "recvFragment: Done rank="<< rank <<", fragment size="<<std::to_string(current_fragment->size()) << " words, seqId=" << current_fragment->sequenceID() ;
00181 return current_fragment;
00182 }
00183
00184 std::set<int> artdaq::FragmentReceiverManager::running_sources() const
00185 {
00186 std::set<int> output;
00187 for (auto& src : running_sources_)
00188 {
00189 if (src.second) output.insert(src.first);
00190 }
00191 return output;
00192 }
00193
00194 std::set<int> artdaq::FragmentReceiverManager::enabled_sources() const
00195 {
00196 std::set<int> output;
00197 for (auto& src : enabled_sources_)
00198 {
00199 if (src.second) output.insert(src.first);
00200 }
00201 return output;
00202 }
00203
00204 void artdaq::FragmentReceiverManager::runReceiver_(int source_rank)
00205 {
00206 while (!stop_requested_ && enabled_sources_.count(source_rank))
00207 {
00208 TLOG(16) << "runReceiver_ "<< source_rank << ": Begin loop" ;
00209 auto is_suppressed = suppress_noisy_senders_ && recv_seq_count_.slotCount(source_rank) > suppression_threshold_ + recv_seq_count_.minCount();
00210 while (!stop_requested_ && is_suppressed)
00211 {
00212 TLOG(6) << "runReceiver_: Suppressing receiver rank " << source_rank ;
00213 if (!is_suppressed) input_cv_.notify_all();
00214 else
00215 {
00216 std::unique_lock<std::mutex> lck(output_cv_mutex_);
00217 output_cv_.wait_for(lck, std::chrono::seconds(1));
00218 }
00219 is_suppressed = suppress_noisy_senders_ && recv_seq_count_.slotCount(source_rank) > suppression_threshold_ + recv_seq_count_.minCount();
00220 }
00221 if (stop_requested_)
00222 {
00223 running_sources_[source_rank] = false;
00224 return;
00225 }
00226
00227 if (fragment_store_[source_rank].GetEndOfData() <= recv_frag_count_.slotCount(source_rank) && !source_plugins_[source_rank]->isRunning())
00228 {
00229 TLOG(TLVL_DEBUG) << "runReceiver_: EndOfData conditions satisfied, ending receive loop";
00230 running_sources_[source_rank] = false;
00231 return;
00232 }
00233
00234 auto start_time = std::chrono::steady_clock::now();
00235 TLOG(16) << "runReceiver_: Calling receiveFragment" ;
00236 auto fragment = std::unique_ptr<Fragment>(new Fragment());
00237 #if 0
00238 auto ret = source_plugins_[source_rank]->receiveFragment(*fragment, receive_timeout_);
00239 TLOG(16) << "runReceiver_: Done with receiveFragment, ret=" << ret << " (should be " << source_rank << ")" ;
00240 if (ret != source_rank) continue;
00241 #else
00242 artdaq::detail::RawFragmentHeader hdr;
00243 auto ret1 = source_plugins_[source_rank]->receiveFragmentHeader(hdr, receive_timeout_);
00244 TLOG(16) << "runReceiver_: Done with receiveFragmentHeader, ret1=" << ret1 << " (should be " << source_rank << ")" ;
00245
00246 if (ret1 != source_rank) continue;
00247
00248 fragment->resize(hdr.word_count - hdr.num_words());
00249 memcpy(fragment->headerAddress(), &hdr, hdr.num_words() * sizeof(artdaq::RawDataType));
00250 auto ret2 = source_plugins_[source_rank]->receiveFragmentData(fragment->headerAddress() + hdr.num_words(), hdr.word_count - hdr.num_words());
00251 if (ret2 != ret1)
00252 {
00253 TLOG(TLVL_ERROR) << "ReceiveFragmentHeader returned " << ret1 << ", but ReceiveFragmentData returned " << ret2 ;
00254 continue;
00255 }
00256 #endif
00257
00258
00259 if (fragment->type() == artdaq::Fragment::EndOfDataFragmentType)
00260 {
00261 TLOG(TLVL_TRACE) << "runReceiver_: EndOfData Fragment received!";
00262 fragment_store_[source_rank].SetEndOfData(*reinterpret_cast<size_t*>(fragment->dataBegin()));
00263 }
00264 else if(fragment->type() == artdaq::Fragment::DataFragmentType || fragment->type() == artdaq::Fragment::ContainerFragmentType || fragment->isUserFragmentType(fragment->type()))
00265 {
00266 TLOG(TLVL_TRACE) << "runReceiver_: Data Fragment received!";
00267 recv_frag_count_.incSlot(source_rank);
00268 recv_frag_size_.incSlot(source_rank, fragment->size() * sizeof(RawDataType));
00269 recv_seq_count_.setSlot(source_rank, fragment->sequenceID());
00270 }
00271 else
00272 {
00273 continue;
00274 }
00275
00276 auto delta_t = std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - start_time).count();
00277 source_metric_data_[source_rank].first += fragment->size() * sizeof(RawDataType);
00278 source_metric_data_[source_rank].second += delta_t;
00279
00280 if (metricMan && TimeUtils::GetElapsedTime(source_metric_send_time_[source_rank]) > 1)
00281 {
00282 TLOG(6) << "runReceiver_: Sending receive stats" ;
00283 metricMan->sendMetric("Data Receive Time From Rank " + std::to_string(source_rank), source_metric_data_[source_rank].second, "s", 1, MetricMode::Accumulate);
00284 metricMan->sendMetric("Data Receive Size From Rank " + std::to_string(source_rank), static_cast<unsigned long>(source_metric_data_[source_rank].first), "B", 1, MetricMode::Accumulate);
00285 metricMan->sendMetric("Data Receive Rate From Rank " + std::to_string(source_rank), source_metric_data_[source_rank].first / source_metric_data_[source_rank].second, "B/s", 1, MetricMode::Average);
00286
00287 source_metric_send_time_[source_rank] = std::chrono::steady_clock::now();
00288 source_metric_data_[source_rank].first = 0;
00289 source_metric_data_[source_rank].second = 0.0;
00290 }
00291
00292
00293 fragment_store_[source_rank].emplace_back(std::move(fragment));
00294 TLOG(TLVL_TRACE) << "runReceiver_: There are now " << fragment_store_[source_rank].size() << " Fragments stored from this source";
00295 input_cv_.notify_all();
00296
00297 }
00298
00299 running_sources_[source_rank] = false;
00300 }