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