OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  11.0.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 
22 #include <string>
23 #include <set>
24 
27 #include <OnixS/Eurex/MarketData/Compiler.h>
28 
29 namespace OnixS
30 {
31  namespace Eurex
32  {
33  namespace MarketData
34  {
35  /// Zero-based index of CPU.
36  typedef size_t CpuIndex;
37 
38  /// Represents set of CPU indices.
39  class ONIXS_EUREX_EMDI_API ThreadAffinity
40  {
41  typedef std::set<CpuIndex> CpuIndexes;
42 
43  public:
44  /// Initializes empty set.
46 
47  /// Initializes as copy of other set.
49 
50  /// Utilizes all the resources.
51  ~ThreadAffinity();
52 
53  /// Indicates whether is empty.
54  bool empty() const;
55 
56  /// Copies set into another set.
57  void copyTo(CpuIndexes&) const;
58 
59  /// Adds CPU index into set.
60  bool insert(CpuIndex index);
61 
62  /// Removes CPU index from the set.
63  bool erase(CpuIndex index);
64 
65  /// Makes set empty.
66  void clear();
67 
68  /// Re-initializes instance as copy of other set.
69  ThreadAffinity& operator = (const ThreadAffinity&);
70 
71  /// Returns the string representation.
72  std::string toString() const;
73 
74  private:
75 
76  /// System index.
77  CpuIndexes* indices_;
78  };
79 
80  /// API to use by the Feed Engine
81  struct ONIXS_EUREX_EMDI_API FeedEngineMode
82  {
83  enum Enum
84  {
85  /// Select-based engine
87 
88  /// Solarflare ef_vi-based engine
89  EfVi
90  };
91 
92  /// Returns the string representation.
93  static std::string toString(FeedEngineMode::Enum value);
94  };
95 
96  /// Setting used in ef_vi mode only
97  class ONIXS_EUREX_EMDI_API EfViEngineSettings
98  {
99  UInt32 receiveRingSize_;
100 
101  public:
103 
104  /// Number of buffers in a virtual interface receive ring.
105  ///
106  /// Single buffer is used for a single network packet.
107  /// Thus the receive ring must be capacious enough to
108  /// place incoming packets during market data bursts.
109  ///
110  /// The default value is 4095
112  {
113  return receiveRingSize_;
114  }
115 
116  /// Number of buffers in a virtual interface receive ring.
117  ///
118  /// Single buffer is used for a single network packet.
119  /// Thus the receive ring must be capacious enough to
120  /// place incoming packets during market data bursts.
121  ///
122  /// By default, the parameter is set to '-1' which means
123  /// receive ring will be constructed of the default capacity.
125  {
126  receiveRingSize_ = size;
127  }
128 
129  std::string toString() const;
130  };
131 
132  /// Collection of settings affecting Feed Engine behavior.
133  class ONIXS_EUREX_EMDI_API FeedEngineSettings
134  {
135  ThreadAffinity threadAffinity_;
136  UInt32 threadCount_;
137  UInt32 dataWaitTime_;
138  UInt32 spinBeforeIdleTime_;
139  UInt32 socketBufferSize_;
140 
141  FeedEngineMode::Enum mode_;
142  EfViEngineSettings efViEngineSettings_;
143 
144  public:
145  /// Initializes options with default values.
147 
148  /// Cleans everything up.
150 
151  /// Defines API to use.
152  /// Select mode is default
154  {
155  return mode_;
156  }
157 
159  {
160  mode_ = mode;
161  }
162 
163  /// Defines set of CPUs allowed for each working thread
164  /// to be executed on while processing market data by Handlers
165  /// bound to Feed Engine instance which is configured by given
166  /// settings.
167  ///
168  /// @note By default set is empty thus allowing threads
169  /// to be executed on any CPU available in the system.
171  {
172  return threadAffinity_;
173  }
174 
175  /// Defines set of CPUs allowed for each working thread
176  /// to be executed on while processing market data by Handlers
177  /// bound to Feed Engine instance which is configured by given
178  /// settings.
179  ///
180  /// @note By default set is empty thus allowing threads
181  /// to be executed on any CPU available in the system.
183  {
184  return threadAffinity_;
185  }
186 
187  /// Number of working threads to be used by feed engine.
188  ///
189  /// @note Default value is '1'.
191  {
192  return threadCount_;
193  }
194 
195  /// Sets threadsCount. @see threadsCount.
196  void threadCount(UInt32 value)
197  {
198  threadCount_ = value;
199  }
200 
201  /// Defines amount of time Feed Engine spends on
202  /// waiting for I/O while running master processing loop.
203  ///
204  /// Time is measured in milliseconds.
205  ///
206  /// @note Default value is '10'.
207  ///
208  /// @warning Given parameter significantly affects
209  /// Handler's responsiveness and load onto CPU!
211  {
212  return dataWaitTime_;
213  }
214 
215  /// Sets dataWaitTime. @see dataWaitTime.
216  void dataWaitTime(UInt32 value)
217  {
218  dataWaitTime_ = value;
219  }
220 
221  /// Defines amount of time Feed Engine keeps cycling before
222  /// going to sleep when no useful activity can be done.
223  ///
224  /// Time is measured in milliseconds.
225  ///
226  /// @note Default value is '1'.
227  ///
228  /// @warning Given parameter has direct influence onto CPU load!
230  {
231  return spinBeforeIdleTime_;
232  }
233 
234  /// Sets redundancySpinTime. @see redundancySpinTime.
236  {
237  spinBeforeIdleTime_ = value;
238  }
239 
240  /// Defines size of receiving buffer in bytes for sockets.
241  ///
242  /// @note Default value is 8 MiB.
243  ///
244  /// @note Ignored in ef_vi mode
246  {
247  return socketBufferSize_;
248  }
249 
250  /// Sets udpSocketBufferSize. @see udpSocketBufferSize.
252  {
253  socketBufferSize_ = value;
254  }
255 
256  /// Set ef_vi specific settings
257  /// Ignored in non-ef_vi mode
258  void efViSettings(const EfViEngineSettings& setting)
259  {
260  efViEngineSettings_ = setting;
261  }
262 
263  /// ef_vi specific settings
265  {
266  return efViEngineSettings_;
267  }
268 
269  /// Returns the string representation.
270  std::string toString() const;
271  };
272 
273  ONIXS_EUREX_EMDI_API std::ostream& operator << (std::ostream& stream, const FeedEngineSettings& settings);
274 
275  /// Identifies reasons feed engine threads becomes idle.
277  {
278  enum Reason
279  {
280  /// Thread waited for incoming data using corresponding
281  /// I/O operations (like 'select') and exited waiting with
282  /// no data availability signs.
284 
285  /// Thread entered idle state due to absence of any data and
286  /// while other threads are waiting for new incoming data.
287  Redundant
288  };
289  };
290 
291  //
293 
294  /// Listener for thread-related events.
295  ///
296  /// Members of this classes are invoked to reflect
297  /// various life-time events of threads spawned and
298  /// used by the feed engine while processing market data.
299  struct ONIXS_EUREX_EMDI_API FeedEngineListener
300  {
301  /// Member invoked by feed engine when
302  /// a new processing thread is spawned.
303  ///
304  /// @note Invocation is done within newly
305  /// started thread.
306  virtual void onFeedEngineThreadBegin(const FeedEngine&) {}
307 
308  /// Member is invoked by feed engine when
309  /// processing thread is about to end.
310  ///
311  /// @note Invocation is done within the
312  /// thread that is about to end.
313  virtual void onFeedEngineThreadEnd(const FeedEngine&) {}
314 
315  /// Is called when feed engine's thread is idle.
316  ///
317  /// Thread becomes idle when either no data is received within
318  /// time interval defined by FeedEngineSettings::dataWaitTime
319  /// parameter or no pending data is available for processing.
320  /// In the first case, callback is invoked with 'DataWaitTimeout'
321  /// reason. In the second case, thread is considered as redundant
322  /// and thus callback is invoked with 'Redundant' reason.
323  /// After callback invocation threads may sleep in kernel to reduce
324  /// load onto CPU and racing between feed engine working threads.
325  ///
326  /// Integer parameter-variable defines amount of time feed engine
327  /// suggest for thread to sleep in kernel after invoking given member.
329 
330  ///
331  virtual ~FeedEngineListener() {}
332  };
333 
334  class FeedEngineImpl;
335 
336  /// Manages processing machinery for market data received from feeds.
337  class ONIXS_EUREX_EMDI_API FeedEngine
338  {
339  public:
340  /// Initializes engine with given configuration.
341  explicit
342  FeedEngine(const FeedEngineSettings&, FeedEngineListener* = ONIXS_EUREX_EMDI_NULLPTR);
343 
344  /// Destructs given instance.
345  ~FeedEngine();
346 
347  /// Settings used define behavior of given instance.
348  const FeedEngineSettings& settings() const;
349 
350  ///Feed engine info
351  std::string info() const;
352 
353  private:
354  // Copying is not supposed for given class.
355  FeedEngine(const FeedEngine&);
356  FeedEngine& operator =(const FeedEngine&);
357 
358  friend class FeedEngineImpl;
359 
360  FeedEngineImpl* impl_;
361  };
362  }
363  }
364 }
virtual void onFeedEngineThreadBegin(const FeedEngine &)
Definition: FeedEngine.h:306
Manages processing machinery for market data received from feeds.
Definition: FeedEngine.h:337
Represents set of CPU indices.
Definition: FeedEngine.h:39
void spinBeforeIdleTime(UInt32 value)
Sets redundancySpinTime.
Definition: FeedEngine.h:235
ONIXS_EUREX_EMDI_API std::ostream & operator<<(std::ostream &stream, const FeedEngineSettings &settings)
void threadCount(UInt32 value)
Sets threadsCount.
Definition: FeedEngine.h:196
void mode(FeedEngineMode::Enum mode)
Definition: FeedEngine.h:158
FeedEngineMode::Enum mode() const
Definition: FeedEngine.h:153
unsigned int UInt32
Definition: Numeric.h:41
virtual void onFeedEngineThreadEnd(const FeedEngine &)
Definition: FeedEngine.h:313
Definition: Defines.h:30
size_t CpuIndex
Zero-based index of CPU.
Definition: FeedEngine.h:36
Identifies reasons feed engine threads becomes idle.
Definition: FeedEngine.h:276
Setting used in ef_vi mode only.
Definition: FeedEngine.h:97
ONIXS_EUREX_EMDI_API_DECL(class, Message)
const EfViEngineSettings & efViSettings() const
ef_vi specific settings
Definition: FeedEngine.h:264
const ThreadAffinity & threadAffinity() const
Definition: FeedEngine.h:170
void dataWaitTime(UInt32 value)
Sets dataWaitTime.
Definition: FeedEngine.h:216
Collection of settings affecting Feed Engine behavior.
Definition: FeedEngine.h:133
virtual void onFeedEngineThreadIdle(const FeedEngine &, FeedEngineThreadIdle::Reason, UInt32 &)
Definition: FeedEngine.h:328
void efViSettings(const EfViEngineSettings &setting)
Definition: FeedEngine.h:258
void socketBufferSize(UInt32 value)
Sets udpSocketBufferSize.
Definition: FeedEngine.h:251
API to use by the Feed Engine.
Definition: FeedEngine.h:81