12#include <fairmq/shmem/Common.h>
13#include <fairmq/shmem/UnmanagedRegion.h>
14#include <fairmq/shmem/Segment.h>
15#include <fairmq/shmem/Monitor.h>
17#include <fairmq/tools/Unique.h>
19#include <fairlogger/Logger.h>
21#include <boost/algorithm/string.hpp>
22#include <boost/program_options.hpp>
32#if !defined(__MACH__) && !defined(__APPLE__)
35#define MPOL_PREFERRED 1
37#define MPOL_INTERLEAVE 3
45volatile sig_atomic_t gStopping = 0;
46volatile sig_atomic_t gResetContent = 0;
60 ShmManager(uint64_t _shmId,
const vector<string>& _segments,
const vector<string>& _regions, uint64_t _refcount_segment_size,
bool zero =
true)
61 :
shmId(
fair::mq::shmem::makeShmIdStr(_shmId))
63 LOG(info) <<
"Starting ShmManager for shmId: " <<
shmId;
64 LOG(info) <<
"Performing full reset...";
67 LOG(info) <<
"Adding managed segments...";
70 LOG(info) <<
"Adding unmanaged regions...";
71 AddRegions(_regions, _refcount_segment_size, zero);
73 LOG(info) <<
"Shared memory is ready for use.";
78 for (
const auto&
s : _segments) {
80 boost::algorithm::split(conf,
s, boost::algorithm::is_any_of(
","));
81 if (conf.size() != 3) {
82 LOG(
error) <<
"incorrect format for --segments. Expecting pairs of <id>,<size><numaid>.";
83 fair::mq::shmem::Monitor::Cleanup(fair::mq::shmem::ShmId{
shmId});
84 throw runtime_error(
"incorrect format for --segments. Expecting pairs of <id>,<size>,<numaid>.");
88 segmentCfgs.emplace_back(fair::mq::shmem::SegmentConfig{
id,
size,
"rbtree_best_fit"});
90#if !defined(__MACH__) && !defined(__APPLE__)
91 int numaid = stoi(conf.at(2));
93 LOG(info) <<
"Setting memory allocation to process default";
95 }
else if (numaid == -1) {
96 LOG(info) <<
"Setting memory allocation to NUMA interleaving";
97 unsigned long nodemask = 0xffffff;
98 syscall(SYS_set_mempolicy,
MPOL_INTERLEAVE, &nodemask,
sizeof(nodemask) * 8);
100 LOG(info) <<
"Setting memory allocation to NUMA id " << numaid;
101 unsigned long nodemask = 1 << numaid;
102 syscall(SYS_set_mempolicy,
MPOL_BIND, &nodemask,
sizeof(nodemask) * 8);
106 auto ret =
segments.emplace(
id, fair::mq::shmem::Segment(
shmId,
id,
size, fair::mq::shmem::rbTreeBestFit));
107 fair::mq::shmem::Segment&
segment = ret.first->second;
108 LOG(info) <<
"Created segment " <<
id <<
" of size " <<
segment.GetSize() <<
", starting at " <<
segment.GetData() <<
". Locking...";
110 LOG(info) <<
"Done.";
112 LOG(info) <<
"Zeroing...";
114 LOG(info) <<
"Done.";
119 void AddRegions(
const vector<string>& _regions, uint64_t _refcount_segment_size,
bool zero)
121 for (
const auto&
r : _regions) {
123 boost::algorithm::split(conf,
r, boost::algorithm::is_any_of(
","));
124 if (conf.size() != 3) {
125 LOG(
error) <<
"incorrect format for --regions. Expecting pairs of <id>,<size>,<numaid>.";
126 fair::mq::shmem::Monitor::Cleanup(fair::mq::shmem::ShmId{
shmId});
127 throw runtime_error(
"incorrect format for --regions. Expecting pairs of <id>,<size>,<numaid>.");
131 fair::mq::RegionConfig cfg;
134 if (_refcount_segment_size != 1) {
135 cfg.rcSegmentSize = _refcount_segment_size;
139#if !defined(__MACH__) && !defined(__APPLE__)
140 int numaid = stoi(conf.at(2));
142 LOG(info) <<
"Setting memory allocation to process default";
144 }
else if (numaid == -1) {
145 LOG(info) <<
"Setting memory allocation to NUMA interleaving";
146 unsigned long nodemask = 0xffffff;
147 syscall(SYS_set_mempolicy,
MPOL_INTERLEAVE, &nodemask,
sizeof(nodemask) * 8);
149 LOG(info) <<
"Setting memory allocation to NUMA id " << numaid;
150 unsigned long nodemask = 1 << numaid;
151 syscall(SYS_set_mempolicy,
MPOL_BIND, &nodemask,
sizeof(nodemask) * 8);
155 auto ret =
regions.emplace(
id, make_unique<fair::mq::shmem::UnmanagedRegion>(
shmId, cfg));
156 fair::mq::shmem::UnmanagedRegion& region = *(ret.first->second);
157 LOG(info) <<
"Created unamanged region " <<
id <<
" of size " << region.GetSize() <<
", starting at " << region.GetData() <<
". Locking...";
159 LOG(info) <<
"Done.";
161 LOG(info) <<
"Zeroing...";
163 LOG(info) <<
"Done.";
170 std::lock_guard<std::mutex> lock(
localMtx);
172 if (!(fair::mq::shmem::Monitor::SegmentIsPresent(fair::mq::shmem::ShmId{
shmId}, sc.id))) {
177 if (!(fair::mq::shmem::Monitor::RegionIsPresent(fair::mq::shmem::ShmId{
shmId}, rc.id.value()))) {
186 std::lock_guard<std::mutex> lock(
localMtx);
194 fair::mq::shmem::Monitor::Cleanup(fair::mq::shmem::ShmId{
shmId});
200 fair::mq::shmem::Monitor::Cleanup(fair::mq::shmem::ShmId{
shmId});
206 map<uint16_t, unique_ptr<fair::mq::shmem::UnmanagedRegion>>
regions;
213 fair::Logger::SetConsoleColor(
true);
221 bool checkPresence =
true;
225 vector<string> regions;
227 options_description desc(
"Options");
230 "segments",
value<vector<string>>(&
segments)->multitoken()->composing(),
"Segments, as <id>,<size>,<numaid> <id>,<size>,<numaid> <id>,<size>,<numaid> ... (numaid: -2 disabled, -1 interleave, >=0 node)")(
231 "regions",
value<vector<string>>(®ions)->multitoken()->composing(),
"Regions, as <id>,<size> <id>,<size>,<numaid> <id>,<size>,<numaid> ...")(
232 "nozero",
value<bool>(&nozero)->default_value(
false)->implicit_value(
true),
"Do not zero segments after initialization")(
233 "check-presence",
value<bool>(&checkPresence)->default_value(
true)->implicit_value(
true),
"Check periodically if configured segments/regions are still present, and cleanup and leave if they are not")(
234 "refcount-segment-size",
value<uint64_t>(&refcount_segment_size)->default_value(1),
"Size in bytes of refCount segment (global setting affecting all unmanaged regions, 1 = use default, 0 = disable rc segment)")(
235 "help,h",
"Print help");
238 store(parse_command_line(argc, argv, desc), vm);
240 if (vm.count(
"help")) {
241 LOG(info) <<
"ShmManager"
251 std::thread resetContentThread([&shmManager]() {
253 std::this_thread::sleep_for(std::chrono::milliseconds(50));
254 if (gResetContent == 1) {
255 LOG(info) <<
"Resetting content for shmId " << shmManager.
shmId;
258 LOG(info) <<
"Done resetting content for shmId " << shmManager.
shmId;
266 LOG(
error) <<
"Failed to find segments, exiting.";
269 std::this_thread::sleep_for(std::chrono::milliseconds(500));
273 if (resetContentThread.joinable()) {
274 resetContentThread.join();
277 LOG(info) <<
"stopping.";
278 }
catch (exception&
e) {
279 LOG(
error) <<
"Exception reached the top of main: " <<
e.what() <<
", exiting";
unsigned short int uint16_t
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
char error[CURL_ERROR_SIZE]
void resetContentHandler(int)
GLsizei const GLfloat * value
ShmManager(uint64_t _shmId, const vector< string > &_segments, const vector< string > &_regions, uint64_t _refcount_segment_size, bool zero=true)
void AddSegments(const vector< string > &_segments, bool zero)
map< uint16_t, fair::mq::shmem::Segment > segments
map< uint16_t, unique_ptr< fair::mq::shmem::UnmanagedRegion > > regions
void AddRegions(const vector< string > &_regions, uint64_t _refcount_segment_size, bool zero)
std::vector< fair::mq::shmem::SegmentConfig > segmentCfgs
std::vector< fair::mq::RegionConfig > regionCfgs
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"