OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  7.4.2
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  /// Collection of settings affecting Feed Engine behavior.
97  class ONIXS_EUREX_EMDI_API FeedEngineSettings
98  {
99  ThreadAffinity threadAffinity_;
100  UInt32 threadCount_;
101  UInt32 dataWaitTime_;
102  UInt32 spinBeforeIdleTime_;
103  UInt32 socketBufferSize_;
104  FeedEngineMode::Enum mode_;
105 
106  public:
107  /// Initializes options with default values.
109 
110 
111  /// Cleans everything up.
113 
114  /// Defines API to use.
115  /// Select mode is default
117  {
118  return mode_;
119  }
120 
122  {
123  mode_ = mode;
124  }
125 
126  /// Defines set of CPUs allowed for each working thread
127  /// to be executed on while processing market data by Handlers
128  /// bound to Feed Engine instance which is configured by given
129  /// settings.
130  ///
131  /// @note By default set is empty thus allowing threads
132  /// to be executed on any CPU available in the system.
134  {
135  return threadAffinity_;
136  }
137 
138  /// Defines set of CPUs allowed for each working thread
139  /// to be executed on while processing market data by Handlers
140  /// bound to Feed Engine instance which is configured by given
141  /// settings.
142  ///
143  /// @note By default set is empty thus allowing threads
144  /// to be executed on any CPU available in the system.
146  {
147  return threadAffinity_;
148  }
149 
150  /// Number of working threads to be used by feed engine.
151  ///
152  /// @note Default value is '1'.
154  {
155  return threadCount_;
156  }
157 
158  /// Sets threadsCount. @see threadsCount.
159  void threadCount(UInt32 value)
160  {
161  threadCount_ = value;
162  }
163 
164  /// Defines amount of time Feed Engine spends on
165  /// waiting for I/O while running master processing loop.
166  ///
167  /// Time is measured in milliseconds.
168  ///
169  /// @note Default value is '10'.
170  ///
171  /// @warning Given parameter significantly affects
172  /// Handler's responsiveness and load onto CPU!
174  {
175  return dataWaitTime_;
176  }
177 
178  /// Sets dataWaitTime. @see dataWaitTime.
179  void dataWaitTime(UInt32 value)
180  {
181  dataWaitTime_ = value;
182  }
183 
184  /// Defines amount of time Feed Engine keeps cycling before
185  /// going to sleep when no useful activity can be done.
186  ///
187  /// Time is measured in milliseconds.
188  ///
189  /// @note Default value is '1'.
190  ///
191  /// @warning Given parameter has direct influence onto CPU load!
193  {
194  return spinBeforeIdleTime_;
195  }
196 
197  /// Sets redundancySpinTime. @see redundancySpinTime.
199  {
200  spinBeforeIdleTime_ = value;
201  }
202 
203  /// Defines size of receiving buffer in bytes for sockets.
204  ///
205  /// @note Default value is 8 MiB.
206  ///
207  /// @note Ignored in ef_vi mode
209  {
210  return socketBufferSize_;
211  }
212 
213  /// Sets udpSocketBufferSize. @see udpSocketBufferSize.
215  {
216  socketBufferSize_ = value;
217  }
218 
219  /// Returns the string representation.
220  std::string toString() const;
221  };
222 
223  ONIXS_EUREX_EMDI_API std::ostream& operator << (std::ostream& stream, const FeedEngineSettings& settings);
224 
225  /// Identifies reasons feed engine threads becomes idle.
227  {
228  enum Reason
229  {
230  /// Thread waited for incoming data using corresponding
231  /// I/O operations (like 'select') and exited waiting with
232  /// no data availability signs.
234 
235  /// Thread entered idle state due to absence of any data and
236  /// while other threads are waiting for new incoming data.
237  Redundant
238  };
239  };
240 
241  //
243 
244  /// Listener for thread-related events.
245  ///
246  /// Members of this classes are invoked to reflect
247  /// various life-time events of threads spawned and
248  /// used by the feed engine while processing market data.
249  struct ONIXS_EUREX_EMDI_API FeedEngineListener
250  {
251  /// Member invoked by feed engine when
252  /// a new processing thread is spawned.
253  ///
254  /// @note Invocation is done within newly
255  /// started thread.
256  virtual void onFeedEngineThreadBegin(const FeedEngine&) {}
257 
258  /// Member is invoked by feed engine when
259  /// processing thread is about to end.
260  ///
261  /// @note Invocation is done within the
262  /// thread that is about to end.
263  virtual void onFeedEngineThreadEnd(const FeedEngine&) {}
264 
265  /// Is called when feed engine's thread is idle.
266  ///
267  /// Thread becomes idle when either no data is received within
268  /// time interval defined by FeedEngineSettings::dataWaitTime
269  /// parameter or no pending data is available for processing.
270  /// In the first case, callback is invoked with 'DataWaitTimeout'
271  /// reason. In the second case, thread is considered as redundant
272  /// and thus callback is invoked with 'Redundant' reason.
273  /// After callback invocation threads may sleep in kernel to reduce
274  /// load onto CPU and racing between feed engine working threads.
275  ///
276  /// Integer parameter-variable defines amount of time feed engine
277  /// suggest for thread to sleep in kernel after invoking given member.
279 
280  ///
281  virtual ~FeedEngineListener() {}
282  };
283 
284  class FeedEngineImpl;
285 
286  /// Manages processing machinery for market data received from feeds.
287  class ONIXS_EUREX_EMDI_API FeedEngine
288  {
289  public:
290  /// Initializes engine with given configuration.
291  explicit
292  FeedEngine(const FeedEngineSettings&, FeedEngineListener* = ONIXS_EUREX_EMDI_NULLPTR);
293 
294  /// Destructs given instance.
295  ~FeedEngine();
296 
297  /// Settings used define behavior of given instance.
298  const FeedEngineSettings& settings() const;
299 
300  ///Feed engine info
301  std::string info() const;
302 
303  private:
304  // Copying is not supposed for given class.
305  FeedEngine(const FeedEngine&);
306  FeedEngine& operator =(const FeedEngine&);
307 
308  friend class FeedEngineImpl;
309 
310  FeedEngineImpl* impl_;
311  };
312  }
313  }
314 }
virtual void onFeedEngineThreadBegin(const FeedEngine &)
Definition: FeedEngine.h:256
Manages processing machinery for market data received from feeds.
Definition: FeedEngine.h:287
Represents set of CPU indices.
Definition: FeedEngine.h:39
void spinBeforeIdleTime(UInt32 value)
Sets redundancySpinTime.
Definition: FeedEngine.h:198
void threadCount(UInt32 value)
Sets threadsCount.
Definition: FeedEngine.h:159
void mode(FeedEngineMode::Enum mode)
Definition: FeedEngine.h:121
FeedEngineMode::Enum mode() const
Definition: FeedEngine.h:116
unsigned int UInt32
Definition: Numeric.h:41
virtual void onFeedEngineThreadEnd(const FeedEngine &)
Definition: FeedEngine.h:263
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:226
ONIXS_EUREX_EMDI_API_DECL(class, Message)
const ThreadAffinity & threadAffinity() const
Definition: FeedEngine.h:133
ONIXS_EUREX_EMDI_API std::ostream & operator<<(std::ostream &stream, const FeedEngineSettings &settings)
void dataWaitTime(UInt32 value)
Sets dataWaitTime.
Definition: FeedEngine.h:179
Collection of settings affecting Feed Engine behavior.
Definition: FeedEngine.h:97
virtual void onFeedEngineThreadIdle(const FeedEngine &, FeedEngineThreadIdle::Reason, UInt32 &)
Definition: FeedEngine.h:278
void socketBufferSize(UInt32 value)
Sets udpSocketBufferSize.
Definition: FeedEngine.h:214
API to use by the Feed Engine.
Definition: FeedEngine.h:81