OnixS C++ eSpeed ITCH Market Data Handler  1.7.3
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 #include <set>
29 
31 
32 /// Zero-based index of CPU.
33 typedef size_t CpuIndex;
34 
35 /// Represents set of CPU indices.
36 class ONIXS_ESPEED_ITCH_API ThreadAffinity
37 {
38  typedef std::set<CpuIndex> CpuIndexes;
39 
40 public:
41  /// Initializes empty set.
43 
44  /// Initializes as copy of other set.
46 
47  /// Utilizes all the resources.
48  ~ThreadAffinity();
49 
50  /// Indicates whether is empty.
51  bool empty() const;
52 
53  /// Copies set into another set.
54  void copyTo(CpuIndexes&) const;
55 
56  /// Adds CPU index into set.
57  bool insert(CpuIndex index);
58 
59  /// Removes CPU index from the set.
60  bool erase(CpuIndex index);
61 
62  /// Makes set empty.
63  void clear();
64 
65  /// Re-initializes instance as copy of other set.
66  ThreadAffinity& operator = (const ThreadAffinity&);
67 
68  /// Returns the string representation.
69  std::string toString() const;
70 
71 private:
72 
73  /// System index.
74  CpuIndexes* indices_;
75 };
76 
77 /// Collection of settings affecting Feed Engine behavior.
78 class ONIXS_ESPEED_ITCH_API FeedEngineSettings
79 {
80  ThreadAffinity threadAffinity_;
81 
82  UInt32 threadCount_;
83 
84  UInt32 dataWaitTime_;
85  UInt32 spinBeforeIdleTime_;
86 
87  UInt32 socketBufferSize_;
88 
89 public:
90  /// Initializes options with default values.
92 
93 
94  /// Cleans everything up.
96 
97 
98  /// Defines set of CPUs allowed for each working thread
99  /// to be executed on while processing market data by Handlers
100  /// bound to Feed Engine instance which is configured by given
101  /// settings.
102  ///
103  /// @note By default set is empty thus allowing threads
104  /// to be executed on any CPU available in the system.
106  {
107  return threadAffinity_;
108  }
109 
110  /// Defines set of CPUs allowed for each working thread
111  /// to be executed on while processing market data by Handlers
112  /// bound to Feed Engine instance which is configured by given
113  /// settings.
114  ///
115  /// @note By default set is empty thus allowing threads
116  /// to be executed on any CPU available in the system.
118  {
119  return threadAffinity_;
120  }
121 
122  /// Number of working threads to be used by feed engine.
123  ///
124  /// @note Default value is '1'.
125  UInt32 threadCount() const
126  {
127  return threadCount_;
128  }
129 
130  /// Sets threadsCount. @see threadsCount.
131  void threadCount(UInt32 value)
132  {
133  threadCount_ = value;
134  }
135 
136  /// Defines amount of time Feed Engine spends on socket
137  /// waiting for I/O while running master processing loop.
138  ///
139  /// Time is measured in milliseconds.
140  ///
141  /// @note Default value is '10'.
142  ///
143  /// @warning Given parameter significantly affects
144  /// Handler's responsiveness and load onto CPU!
145  UInt32 dataWaitTime() const
146  {
147  return dataWaitTime_;
148  }
149 
150  /// Sets dataWaitTime. @see dataWaitTime.
151  void dataWaitTime(UInt32 value)
152  {
153  dataWaitTime_ = value;
154  }
155 
156  /// Defines amount of time Feed Engine keeps cycling before
157  /// going to sleep when no useful activity can be done.
158  ///
159  /// Time is measured in milliseconds.
160  ///
161  /// @note Default value is '1'.
162  ///
163  /// @warning Given parameter has direct influence onto CPU load!
164  UInt32 spinBeforeIdleTime() const
165  {
166  return spinBeforeIdleTime_;
167  }
168 
169  /// Sets redundancySpinTime. @see redundancySpinTime.
170  void spinBeforeIdleTime(UInt32 value)
171  {
172  spinBeforeIdleTime_ = value;
173  }
174 
175  /// Defines size of receiving buffer in bytes for sockets.
176  ///
177  /// @note Default value is 8 MiB.
178  UInt32 socketBufferSize() const
179  {
180  return socketBufferSize_;
181  }
182 
183  /// Sets udpSocketBufferSize. @see udpSocketBufferSize.
184  void socketBufferSize(UInt32 value)
185  {
186  socketBufferSize_ = value;
187  }
188 
189  /// Returns the string representation.
190  std::string toString() const;
191 };
192 
193 ONIXS_ESPEED_ITCH_API std::ostream& operator << (std::ostream& stream, const FeedEngineSettings& settings);
194 
195 /// Identifies reasons feed engine threads becomes idle.
197 {
198  enum Reason
199  {
200  /// Thread waited for incoming data using corresponding
201  /// I/O operations (like 'select') and exited waiting with
202  /// no data availability signs.
204 
205  /// Thread entered idle state due to absence of any data and
206  /// while other threads are waiting for new incoming data.
207  Redundant
208  };
209 };
210 
211 //
213 
214 /// Listener for thread-related events.
215 ///
216 /// Members of this classes are invoked to reflect
217 /// various life-time events of threads spawned and
218 /// used by the feed engine while processing market data.
219 struct ONIXS_ESPEED_ITCH_API FeedEngineListener
220 {
221  /// Member invoked by feed engine when
222  /// a new processing thread is spawned.
223  ///
224  /// @note Invocation is done within newly
225  /// started thread.
226  virtual void onFeedEngineThreadBegin(const FeedEngine&) {}
227 
228  /// Member is invoked by feed engine when
229  /// processing thread is about to end.
230  ///
231  /// @note Invocation is done within the
232  /// thread that is about to end.
233  virtual void onFeedEngineThreadEnd(const FeedEngine&) {}
234 
235  /// Is called when feed engine's thread is idle.
236  ///
237  /// Thread becomes idle when either no data is received within
238  /// time interval defined by FeedEngineSettings::dataWaitTime
239  /// parameter or no pending data is available for processing.
240  /// In the first case, callback is invoked with 'DataWaitTimeout'
241  /// reason. In the second case, thread is considered as redundant
242  /// and thus callback is invoked with 'Redundant' reason.
243  /// After callback invocation threads may sleep in kernel to reduce
244  /// load onto CPU and racing between feed engine working threads.
245  ///
246  /// Integer parameter-variable defines amount of time feed engine
247  /// suggest for thread to sleep in kernel after invoking given member.
249 };
250 
251 class FeedEngineImpl;
252 
253 /// Manages processing machinery for market data received from feeds.
254 class ONIXS_ESPEED_ITCH_API FeedEngine
255 {
256 public:
257  /// Initializes engine with given configuration.
258  FeedEngine(const FeedEngineSettings&, FeedEngineListener* = ONIXS_ESPEED_ITCH_NULLPTR);
259 
260  /// Destructs given instance.
261  ~FeedEngine();
262 
263  /// Settings used define behavior of given instance.
264  const FeedEngineSettings& settings() const;
265 
266 private:
267  // Copying is not supposed for given class.
268  FeedEngine(const FeedEngine&);
269  FeedEngine& operator =(const FeedEngine&);
270 
271  friend class FeedEngineImpl;
272 
273  FeedEngineImpl* impl_;
274 };
275 
#define ONIXS_ESPEED_ITCH_NAMESPACE_END
Definition: Bootstrap.h:31
#define ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN
Definition: Bootstrap.h:27
UInt32 socketBufferSize() const
Definition: FeedEngine.h:178
UInt32 spinBeforeIdleTime() const
Definition: FeedEngine.h:164
virtual void onFeedEngineThreadBegin(const FeedEngine &)
Definition: FeedEngine.h:226
UInt32 dataWaitTime() const
Definition: FeedEngine.h:145
void dataWaitTime(UInt32 value)
Sets dataWaitTime.
Definition: FeedEngine.h:151
Identifies reasons feed engine threads becomes idle.
Definition: FeedEngine.h:196
void socketBufferSize(UInt32 value)
Sets udpSocketBufferSize.
Definition: FeedEngine.h:184
UInt32 threadCount() const
Definition: FeedEngine.h:125
Collection of settings affecting Feed Engine behavior.
Definition: FeedEngine.h:78
virtual void onFeedEngineThreadIdle(const FeedEngine &, FeedEngineThreadIdle::Reason, UInt32 &)
Definition: FeedEngine.h:248
ThreadAffinity & threadAffinity()
Definition: FeedEngine.h:117
ONIXS_ESPEED_ITCH_API std::ostream & operator<<(std::ostream &stream, const FeedEngineSettings &settings)
void threadCount(UInt32 value)
Sets threadsCount.
Definition: FeedEngine.h:131
const ThreadAffinity & threadAffinity() const
Definition: FeedEngine.h:105
virtual void onFeedEngineThreadEnd(const FeedEngine &)
Definition: FeedEngine.h:233
ONIXS_ESPEED_ITCH_NAMESPACE_BEGIN typedef size_t CpuIndex
Zero-based index of CPU.
Definition: FeedEngine.h:33
Represents set of CPU indices.
Definition: FeedEngine.h:36
Manages processing machinery for market data received from feeds.
Definition: FeedEngine.h:254
ONIXS_ESPEED_ITCH_API_DECL(class, FeedEngine)
void spinBeforeIdleTime(UInt32 value)
Sets redundancySpinTime.
Definition: FeedEngine.h:170