-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathCallbackService.h
More file actions
144 lines (131 loc) · 6.88 KB
/
CallbackService.h
File metadata and controls
144 lines (131 loc) · 6.88 KB
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
// 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.
#ifndef O2_FRAMEWORK_CALLBACKSERVICE_H_
#define O2_FRAMEWORK_CALLBACKSERVICE_H_
#include "Framework/CallbackRegistry.h"
#include "Framework/ServiceHandle.h"
#include "Framework/DataProcessingHeader.h"
#include "ServiceRegistry.h"
#include <fairmq/FwdDecls.h>
namespace o2::header
{
struct DataHeader;
}
namespace o2::framework
{
struct ConcreteDataMatcher;
struct EndOfStreamContext;
// A service that data processors can register callback functions invoked by the
// framework at defined steps in the process flow
class CallbackService
{
public:
/// Callbacks are a global service because they will always be
/// invoked by the main thread only.
constexpr static ServiceKind service_kind = ServiceKind::Global;
/// the defined processing steps at which a callback can be invoked
enum struct Id : int {
Start = 0, /**< Invoked before the inner loop is started */
Stop, /**< Invoked when the device is about to be stoped */
Reset, /**< Invoked on device rest */
Idle, /**< Invoked when there was no computation scheduled */
ClockTick, /**< Invoked every iteration of the inner loop */
DataConsumed, /**< Invoked whenever data has been consumed */
/// Invoked when we are notified that no further data will arrive.
/// Notice that one could have more "EndOfData" notifications. Because
/// we could be signaled by control that the data flow restarted.
EndOfStream,
/// Invoked whenever FairMQ notifies us of a new region
///
/// return AlgorithmSpec::InitCallback{[=](InitContext& ic) {
/// auto& callbacks = ic.services().get<CallbackService>();
/// callbacks.set<CallbackService::Id::RegionInfoCallback>([](fair::mq::RegionInfo const& info) {
/// ... do GPU init ...
/// });
/// }
/// ...
/// return [task](ProcessingContext& pc) {
/// // your processing loop. Guaranteed to be called synchronously
/// // with the callback
/// };
/// }};
RegionInfoCallback,
/// Invoked whenever a new timeslice has been created from an enumeration.
/// Users can override this to make sure the fill the DataHeader associated
/// to a timeslice with the wanted quantities.
NewTimeslice,
/// Invoked before the processing callback
PreProcessing,
/// Invoked after the processing callback and before the post processing
/// callback to allow for injecting data in the output stream.
FinaliseOutputs,
/// Invoked after the processing callback,
PostProcessing,
/// Invoked whenever an object from CCDB is deserialised via ROOT.
/// Use this to finalise the initialisation of the object.
CCDBDeserialised,
/// Invoked when new domain info is available
DomainInfoUpdated,
/// Invoked the device undergoes a state change
DeviceStateChanged,
/// Invoked when the device was requested to stop
ExitRequested,
};
using StartCallback = std::function<void()>;
using StopCallback = std::function<void()>;
using ResetCallback = std::function<void()>;
using IdleCallback = std::function<void()>;
using ClockTickCallback = std::function<void()>;
using DataConsumedCallback = std::function<void(ServiceRegistryRef)>;
using EndOfStreamCallback = std::function<void(EndOfStreamContext&)>;
using RegionInfoCallback = std::function<void(fair::mq::RegionInfo const&)>;
using NewTimesliceCallback = std::function<void(o2::header::DataHeader&, DataProcessingHeader&)>;
using PreProcessingCallback = std::function<void(ServiceRegistryRef, int)>;
using FinaliseOutputsCallback = std::function<void(ServiceRegistryRef, int)>;
using PostProcessingCallback = std::function<void(ServiceRegistryRef, int)>;
using CCDBDeserializedCallback = std::function<void(ConcreteDataMatcher&, void*)>;
using DomainInfoUpdatedCallback = std::function<void(ServiceRegistryRef, size_t timeslice, ChannelIndex index)>;
using DeviceStateChangedCallback = std::function<void(ServiceRegistryRef, int newState)>;
using ExitRequestedCallback = std::function<void(ServiceRegistryRef)>;
using Callbacks = CallbackRegistry<Id, //
RegistryPair<Id, Id::Start, StartCallback>, //
RegistryPair<Id, Id::Stop, StopCallback>, //
RegistryPair<Id, Id::Reset, ResetCallback>, //
RegistryPair<Id, Id::Idle, IdleCallback>, //
RegistryPair<Id, Id::ClockTick, ClockTickCallback>, //
RegistryPair<Id, Id::DataConsumed, DataConsumedCallback>, //
RegistryPair<Id, Id::EndOfStream, EndOfStreamCallback>, //
RegistryPair<Id, Id::RegionInfoCallback, RegionInfoCallback>, //
RegistryPair<Id, Id::NewTimeslice, NewTimesliceCallback>, //
RegistryPair<Id, Id::PreProcessing, PreProcessingCallback>, //
RegistryPair<Id, Id::FinaliseOutputs, FinaliseOutputsCallback>, //
RegistryPair<Id, Id::PostProcessing, PostProcessingCallback>, //
RegistryPair<Id, Id::CCDBDeserialised, CCDBDeserializedCallback>, //
RegistryPair<Id, Id::DomainInfoUpdated, DomainInfoUpdatedCallback>, //
RegistryPair<Id, Id::DeviceStateChanged, DeviceStateChangedCallback>, //
RegistryPair<Id, Id::ExitRequested, ExitRequestedCallback> //
>; //
template <Id ID, typename U>
void set(U&& cb)
{
mCallbacks.set<ID>(std::forward<U>(cb));
}
// execute callback for specified processing step with argument pack
template <Id ID, typename... TArgs>
auto call(TArgs&&... args)
{
mCallbacks.call<ID>(std::forward<TArgs>(args)...);
}
private:
Callbacks mCallbacks;
};
} // namespace o2::framework
#endif // O2_FRAMEWORK_CALLBACKSERVICE_H_