forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathO2PrimaryServerDevice.h
696 lines (605 loc) · 25.4 KB
/
O2PrimaryServerDevice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @author Sandro Wenzel
#ifndef O2_DEVICES_PRIMSERVDEVICE_H_
#define O2_DEVICES_PRIMSERVDEVICE_H_
#include <fairmq/Device.h>
#include <fairmq/TransportFactory.h>
#include <FairPrimaryGenerator.h>
#include <Generators/GeneratorFactory.h>
#include <fairmq/Message.h>
#include <DetectorsBase/Stack.h>
#include <SimulationDataFormat/MCEventHeader.h>
#include <SimulationDataFormat/DigitizationContext.h>
#include <TMessage.h>
#include <TClass.h>
#include <SimulationDataFormat/PrimaryChunk.h>
#include <Generators/GeneratorFromFile.h>
#include <Generators/PrimaryGenerator.h>
#include <SimConfig/SimConfig.h>
#include <CommonUtils/ConfigurableParam.h>
#include <CommonUtils/RngHelper.h>
#include <DetectorsBase/SimFieldUtils.h>
#include <Field/MagneticField.h>
#include <TGeoGlobalMagField.h>
#include <typeinfo>
#include <thread>
#include <TROOT.h>
#include <TStopwatch.h>
#include <fstream>
#include <iostream>
#include <atomic>
#include "PrimaryServerState.h"
#include "SimPublishChannelHelper.h"
#include <chrono>
#include <CCDB/BasicCCDBManager.h>
#include <TRandom3.h>
namespace o2
{
namespace devices
{
class O2PrimaryServerDevice final : public fair::mq::Device
{
public:
/// constructor
O2PrimaryServerDevice()
{
mUseFixedChunkSeed = getenv("ALICEO2_O2SIM_SUBEVENTSEED") && atoi(getenv("ALICEO2_O2SIM_SUBEVENTSEED"));
if (mUseFixedChunkSeed) {
mFixedChunkSeed = atol(getenv("ALICEO2_O2SIM_SUBEVENTSEED"));
}
}
/// Default destructor
~O2PrimaryServerDevice() final
{
try {
if (mGeneratorThread.joinable()) {
mGeneratorThread.join();
}
if (mControlThread.joinable()) {
mControlThread.join();
}
} catch (...) {
}
}
protected:
void initGenerator()
{
TStopwatch timer;
timer.Start();
const auto& conf = mSimConfig;
auto& ccdbmgr = o2::ccdb::BasicCCDBManager::instance();
ccdbmgr.setURL(conf.getConfigData().mCCDBUrl);
ccdbmgr.setTimestamp(conf.getTimestamp());
// init magnetic field as it might be needed by the generator
if (TGeoGlobalMagField::Instance()->GetField() == nullptr) {
TGeoGlobalMagField::Instance()->SetField(o2::base::SimFieldUtils::createMagField());
TGeoGlobalMagField::Instance()->Lock();
}
// look if we find a cached instances of Pythia8 or external generators in order to avoid
// (long) initialization times.
// This is evidently a bit weak, as generators might need reconfiguration (to be treated later).
// For now, we'd like to allow for fast switches between say a pythia8 instance and reading from kinematics
// to continue an already started simulation.
//
// Not using cached instances for external kinematics since these might change input filenames etc.
// and are in any case quickly setup.
mPrimGen = nullptr;
if (conf.getGenerator().compare("extkin") != 0 || conf.getGenerator().compare("extkinO2") != 0) {
auto iter = mPrimGeneratorCache.find(conf.getGenerator());
if (iter != mPrimGeneratorCache.end()) {
mPrimGen = iter->second;
LOG(info) << "Found cached generator for " << conf.getGenerator();
}
}
if (mPrimGen == nullptr) {
mPrimGen = new o2::eventgen::PrimaryGenerator;
o2::eventgen::GeneratorFactory::setPrimaryGenerator(conf, mPrimGen);
// setup vertexing
auto vtxMode = conf.getVertexMode();
using o2::conf::VertexMode;
if (vtxMode == VertexMode::kNoVertex || vtxMode == VertexMode::kDiamondParam) {
mPrimGen->setVertexMode(vtxMode);
} else if (vtxMode == VertexMode::kCCDB) {
// we need to fetch the CCDB object
mPrimGen->setVertexMode(vtxMode, ccdbmgr.getForTimeStamp<o2::dataformats::MeanVertexObject>("GLO/Calib/MeanVertex", conf.getTimestamp()));
} else {
LOG(fatal) << "Unsupported vertex mode";
}
auto embedinto_filename = conf.getEmbedIntoFileName();
if (!embedinto_filename.empty()) {
mPrimGen->embedInto(embedinto_filename);
}
mPrimGen->Init();
mPrimGeneratorCache[conf.getGenerator()] = mPrimGen;
}
mPrimGen->SetEvent(&mEventHeader);
// A good moment to couple to collision context
auto collContextFileName_PrefixPair = mSimConfig.getCollContextFilenameAndEventPrefix();
auto collContextFileName = collContextFileName_PrefixPair.first;
if (collContextFileName.size() > 0) {
LOG(info) << "Simulation has collission context";
mCollissionContext = o2::steer::DigitizationContext::loadFromFile(collContextFileName);
if (mCollissionContext) {
const auto& vertices = mCollissionContext->getInteractionVertices();
LOG(info) << "We found " << vertices.size() << " vertices included ";
// initialize the eventID to collID mapping
const auto source = mCollissionContext->findSimPrefix(collContextFileName_PrefixPair.second);
if (source == -1) {
LOG(fatal) << "Wrong simulation prefix";
}
mEventID_to_CollID.clear();
mEventID_to_CollID = mCollissionContext->getCollisionIndicesForSource(source);
}
}
LOG(info) << "Generator initialization took " << timer.CpuTime() << "s";
if (mMaxEvents > 0) {
generateEvent(); // generate a first event
}
}
// function generating one event
void generateEvent(/*bool changeState = false*/)
{
bool changeState = true; // false;
LOG(info) << "Event generation started ";
if (changeState) {
stateTransition(O2PrimaryServerState::WaitingEvent, "GENEVENT");
}
TStopwatch timer;
timer.Start();
try {
bool valid = false;
int retry_counter = 0;
const int MAX_RETRY = 100;
do {
mStack->Reset();
// see if we the vertex comes from the collision context
if (mCollissionContext) {
const auto& vertices = mCollissionContext->getInteractionVertices();
if (vertices.size() > 0) {
auto collisionindex = mEventID_to_CollID.at(mEventCounter);
auto& vertex = vertices.at(collisionindex);
LOG(info) << "Setting vertex " << vertex << " for event " << mEventCounter << " for prefix " << mSimConfig.getOutPrefix();
mPrimGen->setExternalVertexForNextEvent(vertex.X(), vertex.Y(), vertex.Z());
}
}
mPrimGen->GenerateEvent(mStack);
if (mStack->getPrimaries().size() > 0) {
valid = true;
} else {
retry_counter++;
if (retry_counter > MAX_RETRY) {
LOG(warn) << "Not able to generate a non-empty event in " << MAX_RETRY << " trials";
// empty event is sent out
valid = true;
}
}
} while (!valid);
} catch (std::exception const& e) {
LOG(error) << " Exception occurred during event gen " << e.what();
}
timer.Stop();
LOG(info) << "Event generation took " << timer.CpuTime() << "s"
<< " and produced " << mStack->getPrimaries().size() << " primaries ";
if (changeState) {
stateTransition(O2PrimaryServerState::ReadyToServe, "GENEVENT");
}
}
// launches a thread that listens for status requests from outside asynchronously
void launchInfoThread()
{
static std::vector<std::thread> threads;
LOG(info) << "LAUNCHING STATUS THREAD";
auto lambda = [this]() {
while (mState != O2PrimaryServerState::Stopped) {
auto& channel = GetChannels().at("o2sim-primserv-info").at(0);
if (!channel.IsValid()) {
LOG(error) << "channel primserv-info not valid";
}
std::unique_ptr<fair::mq::Message> request(channel.NewSimpleMessage(-1));
int timeout = 100; // 100ms --> so as not to block and allow for proper termination of this thread
if (channel.Receive(request, timeout) > 0) {
LOG(info) << "INFO REQUEST RECEIVED";
if (*(int*)(request->GetData()) == (int)O2PrimaryServerInfoRequest::Status) {
LOG(info) << "Received status request";
// request needs to be a simple enum of type O2PrimaryServerInfoRequest
std::unique_ptr<fair::mq::Message> reply(channel.NewSimpleMessage((int)mState.load()));
if (channel.Send(reply) > 0) {
LOG(info) << "Send status successful";
}
} else if (*(int*)request->GetData() == (int)O2PrimaryServerInfoRequest::Config) {
HandleConfigRequest(channel);
} else {
LOG(fatal) << "UNKNOWN REQUEST";
std::unique_ptr<fair::mq::Message> reply(channel.NewSimpleMessage(404));
channel.Send(reply);
}
}
}
mInfoThreadStopped = true;
};
threads.push_back(std::thread(lambda));
threads.back().detach();
}
void InitTask() final
{
// fatal without core dump
fair::Logger::OnFatal([] { throw fair::FatalException("Fatal error occured. Exiting without core dump..."); });
o2::simpubsub::publishMessage(GetChannels()["primary-notifications"].at(0), "SERVER : INITIALIZING");
stateTransition(O2PrimaryServerState::Initializing, "INITTASK");
LOG(info) << "Init Server device ";
// init sim config
auto& vm = GetConfig()->GetVarMap();
auto& conf = o2::conf::SimConfig::Instance();
if (vm.count("isRun5")) {
conf.setRun5();
}
conf.resetFromParsedMap(vm);
// update the parameters from an INI/JSON file, if given (overrides code-based version)
o2::conf::ConfigurableParam::updateFromFile(conf.getConfigFile());
// update the parameters from stuff given at command line (overrides file-based version)
o2::conf::ConfigurableParam::updateFromString(conf.getKeyValueString());
// customize the level of log output
FairLogger::GetLogger()->SetLogScreenLevel(conf.getLogSeverity().c_str());
FairLogger::GetLogger()->SetLogVerbosityLevel(conf.getLogVerbosity().c_str());
// from now on mSimConfig should be used within this process
mSimConfig = conf;
mStack = new o2::data::Stack();
mStack->setExternalMode(true);
// MC ENGINE
LOG(info) << "ENGINE SET TO " << vm["mcEngine"].as<std::string>();
// CHUNK SIZE
mChunkGranularity = vm["chunkSize"].as<unsigned int>();
LOG(info) << "CHUNK SIZE SET TO " << mChunkGranularity;
// initial initial seed --> we should store this somewhere
mInitialSeed = vm["seed"].as<ULong_t>();
mInitialSeed = o2::utils::RngHelper::setGRandomSeed(mInitialSeed);
mSeedGenerator.SetSeed(mInitialSeed);
LOG(info) << "RNG INITIAL SEED " << mInitialSeed;
mMaxEvents = conf.getNEvents();
// need to make ROOT thread-safe since we use ROOT services in all places
ROOT::EnableThreadSafety();
launchInfoThread();
// launch initialization of particle generator asynchronously
// so that we reach the RUNNING state of the server quickly
// and do not block here
mGeneratorThread = std::thread(&O2PrimaryServerDevice::initGenerator, this);
if (mGeneratorThread.joinable()) {
try {
mGeneratorThread.join();
} catch (std::exception const& e) {
LOG(warn) << "Exception during thread join ..ignoring";
}
}
// init pipe
auto pipeenv = getenv("ALICE_O2SIMSERVERTODRIVER_PIPE");
if (pipeenv) {
mPipeToDriver = atoi(pipeenv);
LOG(info) << "ASSIGNED PIPE HANDLE " << mPipeToDriver;
} else {
LOG(info) << "DID NOT FIND ENVIRONMENT VARIABLE TO INIT PIPE";
}
mAsService = vm["asservice"].as<bool>();
if (mAsService) {
mControlChannel = fair::mq::Channel{"o2sim-control", "sub", fTransportFactory};
auto controlsocketname = getenv("ALICE_O2SIMCONTROL");
if (!controlsocketname) {
LOG(fatal) << "Internal error: Socketname for control input missing";
}
mControlChannel.Connect(std::string(controlsocketname));
mControlChannel.Validate();
}
if (mMaxEvents <= 0) {
if (mAsService) {
stateTransition(O2PrimaryServerState::Idle, "INITTASK");
}
} else {
stateTransition(O2PrimaryServerState::ReadyToServe, "INITTASK");
}
// feedback to driver that we are done initializing
if (mPipeToDriver != -1) {
int message = -111; // special code meaning end of initialization
if (write(mPipeToDriver, &message, sizeof(int))) {
}
}
}
// function for intermediate/on-the-fly reinitializations
bool ReInit(o2::conf::SimReconfigData const& reconfig)
{
LOG(info) << "ReInit Server device ";
if (reconfig.stop) {
return false;
}
// mSimConfig.getConfigData().mKeyValueTokens=reconfig.keyValueTokens;
// Think about this:
// update the parameters from an INI/JSON file, if given (overrides code-based version)
o2::conf::ConfigurableParam::updateFromFile(reconfig.configFile);
// update the parameters from stuff given at command line (overrides file-based version)
o2::conf::ConfigurableParam::updateFromString(reconfig.keyValueTokens);
// initial initial seed --> we should store this somewhere
mInitialSeed = reconfig.startSeed;
mInitialSeed = o2::utils::RngHelper::setGRandomSeed(mInitialSeed);
mSeedGenerator.SetSeed(mInitialSeed);
LOG(info) << "RNG INITIAL SEED " << mInitialSeed;
mMaxEvents = reconfig.nEvents;
// updating the simconfig member with new information especially concerning the generators
// TODO: put this into utility function?
mSimConfig.getConfigData().mGenerator = reconfig.generator;
mSimConfig.getConfigData().mTrigger = reconfig.trigger;
mSimConfig.getConfigData().mExtKinFileName = reconfig.extKinfileName;
mEventCounter = 0;
mPartCounter = 0;
mNeedNewEvent = true;
// reinit generator and start generation of a new event
if (mGeneratorThread.joinable()) {
try {
mGeneratorThread.join();
} catch (std::exception const& e) {
LOG(warn) << "Exception during thread join ..ignoring";
}
}
// mGeneratorThread = std::thread(&O2PrimaryServerDevice::initGenerator, this);
initGenerator();
return true;
}
// method reacting to requests to get the simulation configuration
bool HandleConfigRequest(fair::mq::Channel& channel)
{
LOG(info) << "Received config request";
// just sending the simulation configuration to anyone that wants it
const auto& confdata = mSimConfig.getConfigData();
TMessage* tmsg = new TMessage(kMESS_OBJECT);
tmsg->WriteObjectAny((void*)&confdata, TClass::GetClass(typeid(confdata)));
auto free_tmessage = [](void* data, void* hint) { delete static_cast<TMessage*>(hint); };
std::unique_ptr<fair::mq::Message> message(
fTransportFactory->CreateMessage(tmsg->Buffer(), tmsg->BufferSize(), free_tmessage, tmsg));
// send answer
if (channel.Send(message) > 0) {
LOG(info) << "config reply send ";
return true;
}
return true;
}
bool ConditionalRun() override
{
// we might come here in IDLE mode
if (mState.load() == O2PrimaryServerState::Idle) {
if (mWaitingControlInput.load() == 0) {
if (mControlThread.joinable()) {
mControlThread.join();
}
mControlThread = std::thread(&O2PrimaryServerDevice::waitForControlInput, this);
}
}
auto& channel = GetChannels().at("primary-get").at(0);
PrimaryChunkRequest requestpayload;
std::unique_ptr<fair::mq::Message> request(channel.NewSimpleMessage(requestpayload));
auto bytes = channel.Receive(request);
if (bytes < 0) {
LOG(error) << "Some error/interrupt occurred on socket during receive";
if (NewStatePending()) { // new state is typically pending if (term) signal was received
WaitForNextState();
// ask ourselves for termination of this loop
stateTransition(O2PrimaryServerState::Stopped, "CONDRUN");
}
return false;
}
TStopwatch timer;
timer.Start();
auto& r = *((PrimaryChunkRequest*)(request->GetData()));
LOG(debug) << "PARTICLE REQUEST IN STATE " << PrimStateToString[(int)mState.load()] << " from " << r.workerid << ":" << r.requestid;
auto prestate = mState.load();
auto more = HandleRequest(request, 0, channel);
if (!more) {
if (mAsService) {
if (prestate == O2PrimaryServerState::ReadyToServe || prestate == O2PrimaryServerState::WaitingEvent) {
stateTransition(O2PrimaryServerState::Idle, "CONDRUN");
}
} else {
stateTransition(O2PrimaryServerState::Stopped, "CONDRUN");
}
}
timer.Stop();
auto time = timer.CpuTime();
LOG(debug) << "COND-RUN TOOK " << time << " s";
return mState != O2PrimaryServerState::Stopped;
}
void PostRun() override
{
while (!mInfoThreadStopped) {
LOG(info) << "Waiting info thread";
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
}
}
bool HandleRequest(fair::mq::MessagePtr& request, int /*index*/, fair::mq::Channel& channel)
{
// LOG(debug) << "GOT A REQUEST WITH SIZE " << request->GetSize();
// std::string requeststring(static_cast<char*>(request->GetData()), request->GetSize());
// LOG(info) << "NORMAL REQUEST STRING " << requeststring;
bool workavailable = true;
if (mEventCounter >= mMaxEvents && mNeedNewEvent) {
workavailable = false;
}
if (!(mState == O2PrimaryServerState::ReadyToServe || mState == O2PrimaryServerState::WaitingEvent)) {
// send a zero answer
workavailable = false;
}
PrimaryChunkAnswer header{mState, workavailable};
fair::mq::Parts reply;
std::unique_ptr<fair::mq::Message> headermsg(channel.NewSimpleMessage(header));
reply.AddPart(std::move(headermsg));
LOG(debug) << "Received request for work " << mEventCounter << " " << mMaxEvents << " " << mNeedNewEvent << " available " << workavailable;
if (workavailable) {
if (mNeedNewEvent) {
// we need a newly generated event now
if (mGeneratorThread.joinable()) {
try {
mGeneratorThread.join();
} catch (std::exception const& e) {
LOG(warn) << "Exception during thread join ..ignoring";
}
}
// also if we are still in event waiting stage (doing some busy sleep)
while (mState.load() == O2PrimaryServerState::WaitingEvent) {
LOG(info) << "Waiting for event generation do become fully available";
usleep(100);
}
mNeedNewEvent = false;
mPartCounter = 0;
mEventCounter++;
}
auto& prims = mStack->getPrimaries();
auto numberofparts = (int)std::ceil(prims.size() / (1. * mChunkGranularity));
// number of parts should be at least 1 (even if empty)
numberofparts = std::max(1, numberofparts);
LOG(debug) << "Have " << prims.size() << " " << numberofparts;
o2::data::PrimaryChunk m;
o2::data::SubEventInfo i;
i.eventID = workavailable ? mEventCounter : -1;
i.maxEvents = mMaxEvents;
i.part = mPartCounter + 1;
i.nparts = numberofparts;
// assign a deterministic (yet collision free seed) to process this particle chunk in Geant
// limit range to uint32_t since internal limit of TRandom (despite API suggesting otherwise)
const uint64_t drawnSeed = (uint64_t)(static_cast<double>(std::numeric_limits<uint32_t>::max()) * mSeedGenerator.Rndm());
i.seed = mUseFixedChunkSeed ? mFixedChunkSeed : drawnSeed;
i.index = m.mParticles.size();
i.mMCEventHeader = mEventHeader;
m.mSubEventInfo = i;
int endindex = prims.size() - mPartCounter * mChunkGranularity;
int startindex = prims.size() - (mPartCounter + 1) * mChunkGranularity;
LOG(debug) << "indices " << startindex << " " << endindex;
if (startindex < 0) {
startindex = 0;
}
if (endindex < 0) {
endindex = 0;
}
for (int index = startindex; index < endindex; ++index) {
m.mParticles.emplace_back(prims[index]);
}
LOG(info) << "Sending " << m.mParticles.size() << " particles";
LOG(info) << "treating ev " << mEventCounter << " part " << i.part << " out of " << i.nparts;
// feedback to driver if new event started
if (mPipeToDriver != -1 && i.part == 1 && workavailable) {
if (write(mPipeToDriver, &mEventCounter, sizeof(mEventCounter))) {
}
}
mPartCounter++;
if (mPartCounter == numberofparts) {
mNeedNewEvent = true;
// start generation of a new event
if (mEventCounter < mMaxEvents) {
mGeneratorThread = std::thread(&O2PrimaryServerDevice::generateEvent, this);
}
}
TMessage* tmsg = new TMessage(kMESS_OBJECT);
tmsg->WriteObjectAny((void*)&m, TClass::GetClass("o2::data::PrimaryChunk"));
auto free_tmessage = [](void* data, void* hint) { delete static_cast<TMessage*>(hint); };
std::unique_ptr<fair::mq::Message> message(channel.NewMessage(tmsg->Buffer(), tmsg->BufferSize(), free_tmessage, tmsg));
reply.AddPart(std::move(message));
}
// send answer
TStopwatch timer;
timer.Start();
auto code = Send(reply, "primary-get", 0, 5000); // we introduce timeout in order not to block other requests
timer.Stop();
auto time = timer.CpuTime();
if (code > 0) {
LOG(debug) << "Reply send in " << time << "s";
return workavailable;
} else {
LOG(warn) << "Sending process had problems. Return code : " << code << " time " << time << "s";
}
return false; // -> error should not get here
}
void stateTransition(O2PrimaryServerState to, const char* message)
{
LOG(info) << message << " CHANGING STATE TO " << PrimStateToString[(int)to];
mState = to;
}
void waitForControlInput()
{
mWaitingControlInput.store(1);
if (mState.load() != O2PrimaryServerState::Idle) {
mWaitingControlInput.store(0);
return;
}
o2::simpubsub::publishMessage(GetChannels()["primary-notifications"].at(0), o2::simpubsub::simStatusString("PRIMSERVER", "STATUS", "AWAITING INPUT"));
// this means we are idling
std::unique_ptr<fair::mq::Message> reply(mControlChannel.NewMessage());
bool ok = false;
LOG(info) << "WAITING FOR CONTROL INPUT";
if (mControlChannel.Receive(reply) > 0) {
stateTransition(O2PrimaryServerState::Initializing, "CONTROL");
auto data = reply->GetData();
auto size = reply->GetSize();
std::string command(reinterpret_cast<char const*>(data), size);
LOG(info) << "message: " << command;
o2::conf::SimReconfigData reconfig;
o2::conf::parseSimReconfigFromString(command, reconfig);
LOG(info) << "Processing " << reconfig.nEvents << " new events";
try {
LOG(info) << "REINIT START";
ok = ReInit(reconfig);
LOG(info) << "REINIT DONE";
} catch (std::exception e) {
LOG(info) << "Exception during reinit";
}
} else {
LOG(info) << "NOTHING RECEIVED";
}
if (ok) {
// stateTransition(O2PrimaryServerState::ReadyToServe, "CONTROL"); --> SHOULD BE DONE FROM EVENT GENERATOR (which get's however called only when mEvents>0)
} else {
stateTransition(O2PrimaryServerState::Stopped, "CONTROL");
}
mWaitingControlInput.store(0);
}
private:
o2::conf::SimConfig mSimConfig = o2::conf::SimConfig::Instance(); // local sim config object
o2::eventgen::PrimaryGenerator* mPrimGen = nullptr; // the current primary generator
o2::dataformats::MCEventHeader mEventHeader;
o2::data::Stack* mStack = nullptr; // the stack which is filled (pointer since constructor to be called only init method)
int mChunkGranularity = 500; // how many primaries to send to a worker
int mPartCounter = 0;
bool mNeedNewEvent = true;
int mMaxEvents = 2;
ULong_t mInitialSeed = 0;
bool mUseFixedChunkSeed = false;
ULong_t mFixedChunkSeed = 0;
int mPipeToDriver = -1; // handle for direct piper to driver (to communicate meta info)
int mEventCounter = 0;
std::thread mGeneratorThread; //! a thread used to concurrently init the particle generator
// or to generate events
std::thread mControlThread; //! a thread used to wait for control commands
// Keeps various generators instantiated in memory
// useful when running simulation as a service (when generators
// change between batches)
// TODO: some care needs to be taken (or the user warned) that the caching is based on generator name
// and that parameter-based reconfiguration is not yet implemented (for which we would need to hash all
// configuration parameters as well)
std::map<std::string, o2::eventgen::PrimaryGenerator*> mPrimGeneratorCache;
std::atomic<O2PrimaryServerState> mState{O2PrimaryServerState::Initializing};
std::atomic<int> mWaitingControlInput{0};
std::atomic<bool> mInfoThreadStopped{false};
bool mAsService = false;
// a dedicate (on-the-fly channel) for control messages
fair::mq::Channel mControlChannel;
// some information specific to use case when we have a collision context
o2::steer::DigitizationContext* mCollissionContext = nullptr; //!
std::unordered_map<int, int> mEventID_to_CollID; //!
TRandom3 mSeedGenerator; //! specific random generator for seed generation for work chunks
};
} // namespace devices
} // namespace o2
#endif