OnixS C++ Fenics UST BIMP Market Data Handler  1.1.0
API documentation
FeedEngine.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18  */
19 
20 #pragma once
21 
26 
27 #include <string>
28 
29 namespace OnixS
30 {
31  namespace FenicsUST
32  {
33  namespace MarketData
34  {
35  namespace Bimp
36  {
37  /// Designed to reflect various aspects of feed engine processing flow.
39  {
40  public:
41  /// Aliases integral type whose bits are used to indicate flag presence.
42  typedef UInt32 Flags;
43 
44  ///
45  explicit NetFeedEngineProcessResult(UInt32 flags) ONIXS_FENICSUST_BIMP_NOEXCEPT
46  {
47  value_.raw_ = flags;
48  }
49 
50  /// Indicates whether feed-related events like data reception or absence have been dispatched.
51  bool eventsDispatched() const ONIXS_FENICSUST_BIMP_NOEXCEPT
52  {
53  return 0 != value_.bits_.eventsDispatched_;
54  }
55 
56  /// Indicates whether feed-related events like data reception or absence have been dispatched.
57  void eventsDispatched(bool state) ONIXS_FENICSUST_BIMP_NOEXCEPT
58  {
59  value_.bits_.eventsDispatched_ = state ? 1 : 0;
60  }
61 
62  /// Indicates whether processing had to sleep in kernel while checking data availability.
63  bool ioWaited() const ONIXS_FENICSUST_BIMP_NOEXCEPT
64  {
65  return 0 != value_.bits_.ioWaited_;
66  }
67 
68  /// Indicates whether processing had to sleep in kernel while checking data availability.
69  void ioWaited(bool state) ONIXS_FENICSUST_BIMP_NOEXCEPT
70  {
71  value_.bits_.ioWaited_ = state ? 1 : 0;
72  }
73 
74  /// Reserved (unused) flags.
75  Flags reserved() const ONIXS_FENICSUST_BIMP_NOEXCEPT
76  {
77  return value_.bits_.reserved_;
78  }
79 
80  /// Reserved (unused) flags.
81  void reserved(Flags flags) ONIXS_FENICSUST_BIMP_NOEXCEPT
82  {
83  value_.bits_.reserved_ = flags;
84  }
85 
86  private:
87  struct Bitset
88  {
89  Flags eventsDispatched_ : 1;
90  Flags ioWaited_ : 1;
91  Flags reserved_ : 30;
92  };
93 
94  union Value
95  {
96  Bitset bits_;
97  Flags raw_;
98  };
99 
100  Value value_;
101  };
102 
103  /// The Feed Engine machinery
104  class ONIXS_FENICSUST_BIMP_API FeedEngine
105  {
106  public:
107  /// Carries out pending actions like data retrieval and event dispatching.
108  /// This function must be invoked after each handler-associated action (i.e., start, stop).
110 
111  /// Provides information about the actual implementation of the feed engine.
112  std::string info();
113 
114  ///
115  virtual ~FeedEngine();
116 
117  protected:
118  explicit FeedEngine(void* impl);
119 
120  private:
121  FeedEngine(const FeedEngine&);
122  FeedEngine& operator=(const FeedEngine&);
123 
124  void* const impl_;
125  friend struct FeHelper;
126  };
127 
128  /// Carries out pending actions like
129  /// data retrieval and event dispatching.
130  ///
131  /// The returned value indicates whether any
132  /// events have been handled by the engine.
133  inline bool process(FeedEngine& engine)
134  {
135  return engine.process().eventsDispatched();
136  }
137 
138  /// The given class implements feed engine concept using pool of working threads and standard socket API.
139  class ONIXS_FENICSUST_BIMP_API SocketFeedEngine : public FeedEngine
140  {
141  public:
142  /// Constructor
143  /// \param socketBufferSize Defines size of receiving buffer in bytes for sockets.
144  /// \param dataWaitTime Defines amount of time Feed Engine spends on
145  /// waiting for I/O while running master processing loop. Time is measured in milliseconds.
146  ///
147  /// \param watch Watch service to be used by Feed Engine.
148  ///
149  /// Watch is used by Feed Engine to assign time
150  /// points to packets received from the feeds.
151  ///
152  /// @note By default, Utc watch service is used.
153  explicit SocketFeedEngine(
154  UInt32 dataWaitTime = 10, UInt32 socketBufferSize = 8 * 1024 * 1024, WatchService& watch = UtcWatch::service());
155 
156  ///
157  ~SocketFeedEngine() ONIXS_FENICSUST_BIMP_OVERRIDE;
158  };
159 
160  /// The given class implements the Feed Engine concept using the Solarlfare ef_vi SDK.
161  class ONIXS_FENICSUST_BIMP_API EfViFeedEngine : public FeedEngine
162  {
163  public:
164  /// Constructor
165  /// \param receiveRingSize Number of buffers in a virtual interface receive ring.
166  /// Single buffer is used for a single network packet.
167  /// Thus the receive ring must be capacious enough to
168  /// place incoming packets during market data bursts.
169  ///
170  /// \param watch Watch service to be used by Feed Engine.
171  ///
172  /// Watch is used by Feed Engine to assign time
173  /// points to packets received from the feeds.
174  ///
175  /// @note By default, NIC watch service is used.
176  explicit EfViFeedEngine(UInt32 receiveRingSize = 4095, WatchService& watch = NicWatch::service());
177 
178  ///
179  ~EfViFeedEngine() ONIXS_FENICSUST_BIMP_OVERRIDE;
180  };
181 
182  /// Identifies reasons feed engine threads becomes idle.
184  {
185  enum Reason
186  {
187  /// Thread waited for incoming data using corresponding
188  /// I/O operations (like 'select') and exited waiting with
189  /// no data availability signs.
191 
192  /// Thread entered idle state due to absence of any data and
193  /// while other threads are waiting for new incoming data.
194  Redundant
195  };
196  };
197 
198  ///
200  {
202  : threadAffinity_()
203  , spinBeforeIdleTime_(1)
204  , threadCount_(1)
205  {
206  }
207 
208  /// Defines set of CPUs allowed for each working thread
209  /// to be executed on while processing market data by Handlers
210  /// bound to Feed Engine instance which is configured by given
211  /// settings.
212  ///
213  /// @note By default set is empty thus allowing threads
214  /// to be executed on any CPU available in the system.
216  {
217  return threadAffinity_;
218  }
219 
220  /// Defines set of CPUs allowed for each working thread
221  /// to be executed on while processing market data by Handlers
222  /// bound to Feed Engine instance which is configured by given
223  /// settings.
224  ///
225  /// @note By default set is empty thus allowing threads
226  /// to be executed on any CPU available in the system.
228  {
229  return threadAffinity_;
230  }
231 
232  /// Number of working threads to be used by feed engine.
233  ///
234  /// @note Default value is '1'.
235  UInt32 threadCount() const
236  {
237  return threadCount_;
238  }
239 
240  /// Sets threadsCount. @see threadsCount.
241  void threadCount(UInt32 value)
242  {
243  threadCount_ = value;
244  }
245 
246  /// Defines amount of time Feed Engine keeps cycling before
247  /// going to sleep when no useful activity can be done.
248  ///
249  /// Time is measured in milliseconds.
250  ///
251  /// @note Default value is '1'.
252  ///
253  /// @warning Given parameter has direct influence onto CPU load!
254  UInt32 spinBeforeIdleTime() const
255  {
256  return spinBeforeIdleTime_;
257  }
258 
259  /// Sets redundancySpinTime. @see redundancySpinTime.
260  void spinBeforeIdleTime(UInt32 value)
261  {
262  spinBeforeIdleTime_ = value;
263  }
264 
265  private:
266  ThreadAffinity threadAffinity_;
267  UInt32 spinBeforeIdleTime_;
268  UInt32 threadCount_;
269  };
270 
271  class ONIXS_FENICSUST_BIMP_API FeedEngineThreadPool;
272 
273  /// Listener for thread-related events.
274  ///
275  /// Members of this classes are invoked to reflect
276  /// various life-time events of threads spawned and
277  /// used by the feed engine while processing market data.
278  struct ONIXS_FENICSUST_BIMP_API FeedEngineThreadPoolListener
279  {
280  /// Member invoked by feed engine when
281  /// a new processing thread is spawned.
282  ///
283  /// @note Invocation is done within newly
284  /// started thread.
286 
287  /// Member is invoked by feed engine when
288  /// processing thread is about to end.
289  ///
290  /// @note Invocation is done within the
291  /// thread that is about to end.
293 
294  /// Is called when feed engine's thread is idle.
295  ///
296  /// Integer parameter-variable defines amount of time feed engine
297  /// suggest for thread to sleep in kernel after invoking given member.
299 
300  ///
302  };
303 
304  /// A pool of threads executing feed engine tasks.
305  class ONIXS_FENICSUST_BIMP_API FeedEngineThreadPool
306  {
307  public:
309  const FeedEngineThreadPoolSettings&, FeedEngine*, FeedEngineThreadPoolListener* = ONIXS_FENICSUST_BIMP_NULLPTR);
310 
312 
313  const FeedEngineThreadPoolSettings settings() const;
314 
315  private:
317  FeedEngineThreadPool& operator=(const FeedEngineThreadPool&);
318 
319  const FeedEngineThreadPoolSettings settings_;
320  class FeedEngineThreadPoolImpl;
321  FeedEngineThreadPoolImpl* impl_;
322  };
323 
324  /// Current thread related tasks.
325  class ONIXS_FENICSUST_BIMP_API ThisThread
326  {
327  public:
328  /// Sets the processor affinity mask for the current thread.
329  static void affinity(const ThreadAffinity&);
330  static void affinity(CpuIndex);
331  };
332  }
333  }
334  }
335 }
static NicWatch & service()
Returns watch service.
virtual void onFeedEngineThreadEnd(const FeedEngineThreadPool &)
Definition: FeedEngine.h:292
NetFeedEngineProcessResult(UInt32 flags) ONIXS_FENICSUST_BIMP_NOEXCEPT
Definition: FeedEngine.h:45
void ioWaited(bool state) ONIXS_FENICSUST_BIMP_NOEXCEPT
Indicates whether processing had to sleep in kernel while checking data availability.
Definition: FeedEngine.h:69
static UtcWatch & service()
Returns watch service.
void spinBeforeIdleTime(UInt32 value)
Sets redundancySpinTime.
Definition: FeedEngine.h:260
Designed to reflect various aspects of feed engine processing flow.
Definition: FeedEngine.h:38
The given class implements feed engine concept using pool of working threads and standard socket API...
Definition: FeedEngine.h:139
bool eventsDispatched() const ONIXS_FENICSUST_BIMP_NOEXCEPT
Indicates whether feed-related events like data reception or absence have been dispatched.
Definition: FeedEngine.h:51
bool process(FeedEngine &engine)
Definition: FeedEngine.h:133
virtual void onFeedEngineThreadIdle(const FeedEngineThreadPool &, FeedEngineThreadIdle::Reason, UInt32 &)
Definition: FeedEngine.h:298
size_t CpuIndex
Zero-based index of CPU.
Definition: Defines.h:107
void reserved(Flags flags) ONIXS_FENICSUST_BIMP_NOEXCEPT
Reserved (unused) flags.
Definition: FeedEngine.h:81
Represents set of CPU indices.
Definition: Defines.h:110
The given class implements the Feed Engine concept using the Solarlfare ef_vi SDK.
Definition: FeedEngine.h:161
A pool of threads executing feed engine tasks.
Definition: FeedEngine.h:305
Current thread related tasks.
Definition: FeedEngine.h:325
UInt32 Flags
Aliases integral type whose bits are used to indicate flag presence.
Definition: FeedEngine.h:42
virtual void onFeedEngineThreadBegin(const FeedEngineThreadPool &)
Definition: FeedEngine.h:285
bool ioWaited() const ONIXS_FENICSUST_BIMP_NOEXCEPT
Indicates whether processing had to sleep in kernel while checking data availability.
Definition: FeedEngine.h:63
Identifies reasons feed engine threads becomes idle.
Definition: FeedEngine.h:183
class ONIXS_FENICSUST_BIMP_API FeedEngineThreadPool
Definition: FeedEngine.h:271
Flags reserved() const ONIXS_FENICSUST_BIMP_NOEXCEPT
Reserved (unused) flags.
Definition: FeedEngine.h:75
void eventsDispatched(bool state) ONIXS_FENICSUST_BIMP_NOEXCEPT
Indicates whether feed-related events like data reception or absence have been dispatched.
Definition: FeedEngine.h:57