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(), 0777);
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
00083 struct Config
00084 {
00087 fhicl::Atom<size_t> max_event_size_bytes{ fhicl::Name{ "max_event_size_bytes"}, fhicl::Comment{"Maximum event size (all Fragments), in bytes"} };
00089 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 };
00091 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 };
00093 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 };
00095 fhicl::Atom<uint32_t> shared_memory_key{ fhicl::Name{ "shared_memory_key"}, fhicl::Comment{"Key to use for shared memory access"}, 0xBEE70000 + getpid() };
00097 fhicl::Atom<size_t> buffer_count{ fhicl::Name{ "buffer_count"}, fhicl::Comment{"Number of events in the Shared Memory (incomplete + pending art)"} };
00100 fhicl::Atom<size_t> max_fragment_size_bytes{ fhicl::Name{ "max_fragment_size_bytes"}, fhicl::Comment{" Maximum Fragment size, in bytes"} };
00102 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 };
00104 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 };
00106 fhicl::Atom<size_t> art_analyzer_count{ fhicl::Name{ "art_analyzer_count"}, fhicl::Comment{"Number of art procceses to start"}, 1 };
00108 fhicl::Atom<size_t> expected_fragments_per_event{ fhicl::Name{ "expected_fragments_per_event"}, fhicl::Comment{"Number of Fragments to expect per event"} };
00110 fhicl::Atom<int> maximum_oversize_fragment_count{ fhicl::Name{"maximum_oversize_fragment_count"}, fhicl::Comment{"Maximum number of over-size Fragments to drop before throwing an exception. Default is 1, which means to throw an exception if any over-size Fragments are dropped. Set to 0 to disable."},1 };
00112 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 };
00114 fhicl::Atom<bool> use_sequence_id_for_event_number{ fhicl::Name{"use_sequence_id_for_event_number"}, fhicl::Comment{"Whether to use the artdaq Sequence ID (true) or the Timestamp (false) for art Event numbers"}, true };
00116 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 };
00118 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 };
00121 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 };
00123 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 };
00126 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 };
00128 fhicl::Atom<uint32_t> broadcast_shared_memory_key{ fhicl::Name{ "broadcast_shared_memory_key"}, fhicl::Comment{""}, 0xCEE70000 + getpid() };
00130 fhicl::Atom<size_t> broadcast_buffer_count{ fhicl::Name{ "broadcast_buffer_count"}, fhicl::Comment{"Buffers in the broadcast shared memory segment"}, 10 };
00132 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 };
00134 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 };
00136 fhicl::Atom<bool> manual_art{ fhicl::Name{"manual_art"}, fhicl::Comment{"Prints the startup command line for the art process so that the user may (for example) run it in GDB or valgrind"}, false };
00137
00138 fhicl::TableFragment<artdaq::RequestSender::Config> requestSenderConfig;
00139 };
00140 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00141 using Parameters = fhicl::WrappedTable<Config>;
00142 #endif
00143
00149 SharedMemoryEventManager(fhicl::ParameterSet pset, fhicl::ParameterSet art_pset);
00153 virtual ~SharedMemoryEventManager();
00154
00155 private:
00162 bool AddFragment(detail::RawFragmentHeader frag, void* dataPtr);
00163
00164 public:
00172 bool AddFragment(FragmentPtr frag, size_t timeout_usec, FragmentPtr& outfrag);
00173
00180 RawDataType* WriteFragmentHeader(detail::RawFragmentHeader frag, bool dropIfNoBuffersAvailable = false);
00181
00186 void DoneWritingFragment(detail::RawFragmentHeader frag);
00187
00192 size_t GetIncompleteEventCount() { return active_buffers_.size(); }
00193
00198 size_t GetPendingEventCount() { return pending_buffers_.size(); }
00199
00204 size_t GetLockedBufferCount() { return GetBuffersOwnedByManager().size(); }
00205
00210 size_t GetArtEventCount() { return subrun_event_count_; }
00211
00218 size_t GetFragmentCount(Fragment::sequence_id_t seqID, Fragment::type_t type = Fragment::InvalidFragmentType);
00219
00226 size_t GetFragmentCountInBuffer(int buffer, Fragment::type_t type = Fragment::InvalidFragmentType);
00227
00231 void RunArt(std::shared_ptr<art_config_file> config_file, pid_t& pid_out);
00235 void StartArt();
00236
00242 pid_t StartArtProcess(fhicl::ParameterSet pset);
00243
00248 void ShutdownArtProcesses(std::set<pid_t> pids);
00249
00256 void ReconfigureArt(fhicl::ParameterSet art_pset, run_id_t newRun = 0, int n_art_processes = -1);
00257
00267 bool endOfData();
00268
00273 void startRun(run_id_t runID);
00274
00278 void startSubrun();
00279
00284 run_id_t runID() const { return run_id_; }
00285
00290 subrun_id_t subrunID() const { return subrun_id_; }
00291
00296 bool endRun();
00297
00302 bool endSubrun();
00303
00308 void rolloverSubrun(sequence_id_t boundary);
00309
00313 void sendMetrics();
00314
00319 void setRequestMode(detail::RequestMessageMode mode) { if (requests_) requests_->SetRequestMode(mode); }
00320
00325 void setOverwrite(bool overwrite) { overwrite_mode_ = overwrite; }
00326
00330 void SetInitFragment(FragmentPtr frag);
00331
00336 uint32_t GetBroadcastKey() { return broadcasts_.GetKey(); }
00337
00342 RawDataType* GetDroppedDataAddress() { return dropped_data_->dataBegin(); }
00343
00344 private:
00345 size_t num_art_processes_;
00346 size_t const num_fragments_per_event_;
00347 size_t const queue_size_;
00348 run_id_t run_id_;
00349 subrun_id_t subrun_id_;
00350 sequence_id_t subrun_rollover_event_;
00351 sequence_id_t last_released_event_;
00352
00353 std::set<int> active_buffers_;
00354 std::set<int> pending_buffers_;
00355 std::unordered_map<Fragment::sequence_id_t, size_t> released_incomplete_events_;
00356
00357 bool update_run_ids_;
00358 bool use_sequence_id_for_event_number_;
00359 bool overwrite_mode_;
00360 bool send_init_fragments_;
00361 bool running_;
00362
00363 std::unordered_map<int, std::atomic<int>> buffer_writes_pending_;
00364 std::unordered_map<int, std::mutex> buffer_mutexes_;
00365 static std::mutex sequence_id_mutex_;
00366
00367 int incomplete_event_report_interval_ms_;
00368 std::chrono::steady_clock::time_point last_incomplete_event_report_time_;
00369 std::chrono::steady_clock::time_point last_shmem_buffer_metric_update_;
00370 int broadcast_timeout_ms_;
00371
00372 std::atomic<int> run_event_count_;
00373 std::atomic<int> run_incomplete_event_count_;
00374 std::atomic<int> subrun_event_count_;
00375 std::atomic<int> subrun_incomplete_event_count_;
00376 std::atomic<int> oversize_fragment_count_;
00377 int maximum_oversize_fragment_count_;
00378
00379 std::set<pid_t> art_processes_;
00380 std::atomic<bool> restart_art_;
00381 bool always_restart_art_;
00382 bool manual_art_;
00383 fhicl::ParameterSet current_art_pset_;
00384 std::shared_ptr<art_config_file> current_art_config_file_;
00385 double minimum_art_lifetime_s_;
00386 size_t art_event_processing_time_us_;
00387
00388 std::unique_ptr<RequestSender> requests_;
00389 fhicl::ParameterSet data_pset_;
00390
00391 FragmentPtr init_fragment_;
00392 FragmentPtr dropped_data_;
00393
00394 bool broadcastFragment_(FragmentPtr frag, FragmentPtr& outFrag);
00395
00396 detail::RawEventHeader* getEventHeader_(int buffer);
00397
00398 int getBufferForSequenceID_(Fragment::sequence_id_t seqID, bool create_new, Fragment::timestamp_t timestamp = Fragment::InvalidTimestamp);
00399 bool hasFragments_(int buffer);
00400 void complete_buffer_(int buffer);
00401 bool bufferComparator(int bufA, int bufB);
00402 void check_pending_buffers_(std::unique_lock<std::mutex> const& lock = std::unique_lock<std::mutex>(sequence_id_mutex_));
00403
00404 void send_init_frag_();
00405 SharedMemoryManager broadcasts_;
00406 };
00407 }
00408
00409 #endif //ARTDAQ_DAQRATE_SHAREDMEMORYEVENTMANAGER_HH