00001 #ifndef ARTDAQ_DAQRATE_SHAREDMEMORYEVENTMANAGER_HH
00002 #define ARTDAQ_DAQRATE_SHAREDMEMORYEVENTMANAGER_HH
00003
00004 #include "artdaq/DAQdata/Globals.hh"
00005 #include "artdaq-core/Core/SharedMemoryManager.hh"
00006 #include "artdaq-core/Data/RawEvent.hh"
00007 #include "artdaq/DAQrate/RequestSender.hh"
00008 #include <set>
00009 #include <deque>
00010 #include <fstream>
00011 #include <iomanip>
00012 #include <sys/stat.h>
00013 #include "fhiclcpp/fwd.h"
00014 #include "artdaq/Application/StatisticsHelper.hh"
00015 #include "artdaq/DAQrate/detail/ArtConfig.hh"
00016 #define ART_SUPPORTS_DUPLICATE_EVENTS 0
00017
00018 namespace artdaq {
00019
00023 class art_config_file
00024 {
00025 public:
00030 art_config_file(fhicl::ParameterSet ps)
00031 : dir_name_("/tmp/partition_" + std::to_string(Globals::GetPartitionNumber()))
00032 , file_name_(dir_name_ + "/artConfig_" + std::to_string(my_rank) + "_" + std::to_string(artdaq::TimeUtils::gettimeofday_us()) + ".fcl")
00033 {
00034 mkdir(dir_name_.c_str(), S_IRWXU);
00035
00036 std::ofstream of(file_name_, std::ofstream::trunc);
00037 of << ps.to_string();
00038
00039
00040
00041
00042
00043
00044
00045 if (!ps.has_key("services.message"))
00046 {
00047 of << " services.message: { " << generateMessageFacilityConfiguration("art") << "} ";
00048 }
00049
00050
00051
00052 of.close();
00053 }
00054 ~art_config_file()
00055 {
00056 remove(file_name_.c_str());
00057 rmdir(dir_name_.c_str());
00058 }
00063 std::string getFileName() const { return file_name_; }
00064 private:
00065 std::string dir_name_;
00066 std::string file_name_;
00067 };
00068
00072 class SharedMemoryEventManager : public SharedMemoryManager
00073 {
00074 public:
00075 typedef RawEvent::run_id_t run_id_t;
00076 typedef RawEvent::subrun_id_t subrun_id_t;
00077 typedef Fragment::sequence_id_t sequence_id_t;
00078 typedef std::map<sequence_id_t, RawEvent_ptr> EventMap;
00079
00080 struct Config
00081 {
00082 fhicl::Atom<size_t> max_event_size_bytes{ fhicl::Name{ "max_event_size_bytes"}, fhicl::Comment{"Maximum event size (all Fragments), in bytes"} };
00083 fhicl::Atom<size_t> stale_buffer_timeout_usec{ fhicl::Name{ "stale_buffer_timeout_usec"}, fhicl::Comment{"Maximum amount of time elapsed before a buffer is marked as abandoned. Time is reset each time an operation is performed on the buffer."}, 5000000 };
00084 fhicl::Atom<bool> overwrite_mode{ fhicl::Name{ "overwrite_mode"}, fhicl::Comment{"Whether buffers are allowed to be overwritten when safe (state == Full or Reading)"}, false };
00085 fhicl::Atom<bool> restart_crashed_art_processes{ fhicl::Name{"restart_crashed_art_processes"}, fhicl::Comment{"Whether to automatically restart art processes that fail for any reason"}, true };
00086 fhicl::Atom<uint32_t> shared_memory_key{ fhicl::Name{ "shared_memory_key"}, fhicl::Comment{"Key to use for shared memory access"}, 0xBEE70000 + getpid() };
00087 fhicl::Atom<size_t> buffer_count{ fhicl::Name{ "buffer_count"}, fhicl::Comment{"Number of events in the Shared Memory (incomplete + pending art)"} };
00088 fhicl::Atom<size_t> max_fragment_size_bytes{ fhicl::Name{ "max_fragment_size_bytes"}, fhicl::Comment{" Maximum Fragment size, in bytes"} };
00089 fhicl::Atom<size_t> event_queue_wait_time{ fhicl::Name{ "event_queue_wait_time"}, fhicl::Comment{"Amount of time (in seconds) an event can exist in shared memory before being released to art. Used as input to default parameter of \"stale_buffer_timeout_usec\"."}, 5 };
00090 fhicl::Atom<bool> broadcast_mode{ fhicl::Name{ "broadcast_mode"}, fhicl::Comment{"When true, buffers are not marked Empty when read, but return to Full state. Buffers are overwritten in order received."}, false };
00091 fhicl::Atom<size_t> art_analyzer_count{ fhicl::Name{ "art_analyzer_count"}, fhicl::Comment{"Number of art procceses to start"}, 1 };
00092 fhicl::Atom<size_t> expected_fragments_per_event{ fhicl::Name{ "expected_fragments_per_event"}, fhicl::Comment{"Number of Fragments to expect per event"} };
00093 fhicl::Atom<bool> update_run_ids_on_new_fragment{ fhicl::Name{ "update_run_ids_on_new_fragment"}, fhicl::Comment{"Whether the run and subrun ID of an event should be updated whenever a Fragment is added."}, true };
00094 fhicl::Atom<bool> send_init_fragments{ fhicl::Name{ "send_init_fragments"}, fhicl::Comment{"Whether Init Fragments are expected to be sent to art. If true, a Warning message is printed when an Init Fragment is requested but none are available."}, true };
00095 fhicl::Atom<int> incomplete_event_report_interval_ms{ fhicl::Name{ "incomplete_event_report_interval_ms"}, fhicl::Comment{"Interval at which an incomplete event report should be written"}, -1 };
00096 fhicl::Atom<int> fragment_broadcast_timeout_ms{ fhicl::Name{ "fragment_broadcast_timeout_ms"}, fhicl::Comment{"Amount of time broadcast fragments should live in the broadcast shared memory segment"}, 3000 };
00097 fhicl::Atom<double> minimum_art_lifetime_s{ fhicl::Name{ "minimum_art_lifetime_s"}, fhicl::Comment{"Amount of time that an art process should run to not be considered \"DOA\""}, 2.0 };
00098 fhicl::Atom<size_t> expected_art_event_processing_time_us{ fhicl::Name{ "expected_art_event_processing_time_us"}, fhicl::Comment{"During shutdown, SMEM will wait for this amount of time while it is checking that the art threads are done reading buffers."}, 100000 };
00099 fhicl::Atom<uint32_t> broadcast_shared_memory_key{ fhicl::Name{ "broadcast_shared_memory_key"}, fhicl::Comment{""}, 0xCEE70000 + getpid() };
00100 fhicl::Atom<size_t> broadcast_buffer_count{ fhicl::Name{ "broadcast_buffer_count"}, fhicl::Comment{"Buffers in the broadcast shared memory segment"}, 10 };
00101 fhicl::Atom<size_t> broadcast_buffer_size{ fhicl::Name{ "broadcast_buffer_size"}, fhicl::Comment{"Size of the buffers in the broadcast shared memory segment"}, 0x100000 };
00102 fhicl::Atom<bool> use_art{ fhicl::Name{ "use_art"}, fhicl::Comment{"Whether to start and manage art threads (Sets art_analyzer count to 0 and overwrite_mode to true when false)"}, true };
00103
00104 fhicl::TableFragment<artdaq::RequestSender::Config> requestSenderConfig;
00105 };
00106 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00107 using Parameters = fhicl::WrappedTable<Config>;
00108 #endif
00109
00143 SharedMemoryEventManager(fhicl::ParameterSet pset, fhicl::ParameterSet art_pset);
00147 virtual ~SharedMemoryEventManager();
00148
00149 private:
00156 bool AddFragment(detail::RawFragmentHeader frag, void* dataPtr);
00157
00158 public:
00166 bool AddFragment(FragmentPtr frag, size_t timeout_usec, FragmentPtr& outfrag);
00167
00174 RawDataType* WriteFragmentHeader(detail::RawFragmentHeader frag, bool dropIfNoBuffersAvailable = false);
00175
00180 void DoneWritingFragment(detail::RawFragmentHeader frag);
00181
00186 size_t GetIncompleteEventCount() { return active_buffers_.size(); }
00187
00192 size_t GetPendingEventCount() { return pending_buffers_.size(); }
00193
00198 size_t GetLockedBufferCount() { return GetBuffersOwnedByManager().size(); }
00199
00204 size_t GetArtEventCount() { return subrun_event_count_; }
00205
00212 size_t GetFragmentCount(Fragment::sequence_id_t seqID, Fragment::type_t type = Fragment::InvalidFragmentType);
00213
00220 size_t GetFragmentCountInBuffer(int buffer, Fragment::type_t type = Fragment::InvalidFragmentType);
00221
00225 void RunArt(std::shared_ptr<art_config_file> config_file, pid_t& pid_out);
00229 void StartArt();
00230
00236 pid_t StartArtProcess(fhicl::ParameterSet pset);
00237
00242 void ShutdownArtProcesses(std::set<pid_t> pids);
00243
00250 void ReconfigureArt(fhicl::ParameterSet art_pset, run_id_t newRun = 0, int n_art_processes = -1);
00251
00261 bool endOfData();
00262
00267 void startRun(run_id_t runID);
00268
00272 void startSubrun();
00273
00278 run_id_t runID() const { return run_id_; }
00279
00284 subrun_id_t subrunID() const { return subrun_id_; }
00285
00290 bool endRun();
00291
00296 bool endSubrun();
00297
00302 void rolloverSubrun(sequence_id_t boundary);
00303
00307 void sendMetrics();
00308
00313 void setRequestMode(detail::RequestMessageMode mode) { if (requests_) requests_->SetRequestMode(mode); }
00314
00319 void setOverwrite(bool overwrite) { overwrite_mode_ = overwrite; }
00320
00324 void SetInitFragment(FragmentPtr frag);
00325
00330 uint32_t GetBroadcastKey() { return broadcasts_.GetKey(); }
00331
00336 RawDataType* GetDroppedDataAddress() { return dropped_data_->dataBegin(); }
00337
00338 private:
00339 size_t num_art_processes_;
00340 size_t const num_fragments_per_event_;
00341 size_t const queue_size_;
00342 run_id_t run_id_;
00343 subrun_id_t subrun_id_;
00344 sequence_id_t subrun_rollover_event_;
00345 sequence_id_t last_released_event_;
00346
00347 std::set<int> active_buffers_;
00348 std::set<int> pending_buffers_;
00349 std::unordered_map<Fragment::sequence_id_t, size_t> released_incomplete_events_;
00350
00351 bool update_run_ids_;
00352 bool overwrite_mode_;
00353 bool send_init_fragments_;
00354 bool running_;
00355
00356 std::unordered_map<int, std::atomic<int>> buffer_writes_pending_;
00357 std::unordered_map<int, std::mutex> buffer_mutexes_;
00358 static std::mutex sequence_id_mutex_;
00359
00360 int incomplete_event_report_interval_ms_;
00361 std::chrono::steady_clock::time_point last_incomplete_event_report_time_;
00362 int broadcast_timeout_ms_;
00363
00364 std::atomic<int> run_event_count_;
00365 std::atomic<int> run_incomplete_event_count_;
00366 std::atomic<int> subrun_event_count_;
00367 std::atomic<int> subrun_incomplete_event_count_;
00368
00369 std::set<pid_t> art_processes_;
00370 std::atomic<bool> restart_art_;
00371 bool always_restart_art_;
00372 fhicl::ParameterSet current_art_pset_;
00373 std::shared_ptr<art_config_file> current_art_config_file_;
00374 double minimum_art_lifetime_s_;
00375 size_t art_event_processing_time_us_;
00376
00377 std::unique_ptr<RequestSender> requests_;
00378 fhicl::ParameterSet data_pset_;
00379
00380 FragmentPtr init_fragment_;
00381 FragmentPtr dropped_data_;
00382
00383 bool broadcastFragment_(FragmentPtr frag, FragmentPtr& outFrag);
00384
00385 detail::RawEventHeader* getEventHeader_(int buffer);
00386
00387 int getBufferForSequenceID_(Fragment::sequence_id_t seqID, bool create_new, Fragment::timestamp_t timestamp = Fragment::InvalidTimestamp);
00388 bool hasFragments_(int buffer);
00389 void complete_buffer_(int buffer);
00390 bool bufferComparator(int bufA, int bufB);
00391 void check_pending_buffers_(std::unique_lock<std::mutex> const& lock = std::unique_lock<std::mutex>(sequence_id_mutex_));
00392
00393 void send_init_frag_();
00394 SharedMemoryManager broadcasts_;
00395 };
00396 }
00397
00398 #endif //ARTDAQ_DAQRATE_SHAREDMEMORYEVENTMANAGER_HH