241 lines
9.0 KiB
C++
Executable File
241 lines
9.0 KiB
C++
Executable File
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
|
// and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable OnixS Software
|
|
// Services Agreement (the Agreement) and Customer end user license agreements granting
|
|
// a non-assignable, non-transferable and non-exclusive license to use the software
|
|
// for it's own data processing purposes under the terms defined in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
|
// of this source code or associated reference material to any other location for further reproduction
|
|
// or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
|
// the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include "OnixS/CME/MDP3/Types.h"
|
|
#include "OnixS/CME/MDP3/ThreadAffinity.h"
|
|
|
|
namespace OnixS
|
|
{
|
|
namespace CME
|
|
{
|
|
namespace MDP3
|
|
{
|
|
ONIXS_CME_MDP3_API_DECL(class, FeedEngine);
|
|
|
|
/// Collection of settings affecting Feed Engine behavior.
|
|
struct
|
|
ONIXS_CME_MDP3_API
|
|
FeedEngineSettings
|
|
{
|
|
/// Number of working threads to be used by feed engine.
|
|
/// @note Default value is '1'.
|
|
unsigned int threadsCount() const
|
|
{
|
|
return threadsCount_;
|
|
}
|
|
|
|
/// Sets threadsCount. @see threadsCount.
|
|
void threadsCount(unsigned int value)
|
|
{
|
|
threadsCount_ = value;
|
|
}
|
|
|
|
/// Defines set of CPUs by their indices (starting from zero)
|
|
/// allowed for each thread used by the Handler while processing
|
|
/// market data to be executed on.
|
|
///
|
|
/// @note By default set is empty thus allowing threads
|
|
/// to be executed on any CPU available in the system.
|
|
const ThreadAffinity& threadsAffinity() const
|
|
{
|
|
return threadsAffinity_;
|
|
}
|
|
|
|
/// @see threadsAffinity.
|
|
ThreadAffinity& threadsAffinity()
|
|
{
|
|
return threadsAffinity_;
|
|
}
|
|
|
|
/// Defines amount of time Feed Engine spends on socket
|
|
/// waiting for I/O while running master processing loop.
|
|
///
|
|
/// Time is measured in milliseconds.
|
|
///
|
|
/// @note Default value is '10'.
|
|
///
|
|
/// @warning Given parameter significantly affects
|
|
/// Handler's responsiveness and load onto CPU!
|
|
unsigned int dataWaitTime() const
|
|
{
|
|
return dataWaitTime_;
|
|
}
|
|
|
|
/// Sets dataWaitTime. @see dataWaitTime.
|
|
void dataWaitTime(unsigned int value)
|
|
{
|
|
dataWaitTime_ = value;
|
|
}
|
|
|
|
/// Defines amount of time Feed Engine keeps cycling before
|
|
/// going to sleep when no useful activity can be done.
|
|
///
|
|
/// Time is measured in milliseconds.
|
|
///
|
|
/// @note Default value is '1'.
|
|
///
|
|
/// @warning Given parameter has direct influence onto CPU load!
|
|
unsigned int redundancySpinTime() const
|
|
{
|
|
return redundancySpinTime_;
|
|
}
|
|
|
|
/// Sets redundancySpinTime. @see redundancySpinTime.
|
|
void redundancySpinTime(unsigned int value)
|
|
{
|
|
redundancySpinTime_ = value;
|
|
}
|
|
|
|
/// Initializes default settings.
|
|
FeedEngineSettings();
|
|
|
|
/// Cleans everything up.
|
|
~FeedEngineSettings();
|
|
|
|
/// Returns textual presentation.
|
|
std::string toString() const
|
|
{
|
|
std::string str;
|
|
toString(str);
|
|
return str;
|
|
}
|
|
|
|
/// Returns textual presentation.
|
|
void toString(std::string&) const;
|
|
|
|
private:
|
|
unsigned int threadsCount_;
|
|
ThreadAffinity threadsAffinity_;
|
|
unsigned int dataWaitTime_;
|
|
unsigned int redundancySpinTime_;
|
|
};
|
|
|
|
/// Identifies reasons feed engine threads becomes idle.
|
|
struct
|
|
ONIXS_CME_MDP3_API
|
|
FeedEngineThreadIdleReasons
|
|
{
|
|
enum Enum
|
|
{
|
|
/// Thread waited for incoming data using corresponding
|
|
/// I/O operations (like 'select') and exited waiting with
|
|
/// no data availability signs.
|
|
DataWaitTimeout,
|
|
|
|
/// Thread entered idle state due to absence of any data and
|
|
/// while other threads are waiting for new incoming data.
|
|
Redundant
|
|
};
|
|
};
|
|
|
|
/// Alias for feed engine thread idle reason.
|
|
typedef
|
|
FeedEngineThreadIdleReasons::Enum
|
|
FeedEngineThreadIdleReason;
|
|
|
|
/// Listener for thread-related events.
|
|
///
|
|
/// Members of this classes are invoked to reflect
|
|
/// various life-time events of threads spawned and
|
|
/// used by the feed engine while processing market data.
|
|
class
|
|
ONIXS_CME_MDP3_API
|
|
FeedEngineListener
|
|
{
|
|
public:
|
|
/// Member invoked by feed engine when
|
|
/// a new processing thread is spawned.
|
|
///
|
|
/// @note Invocation is done within newly
|
|
/// started thread.
|
|
virtual
|
|
void
|
|
onFeedEngineThreadBegin(
|
|
const FeedEngine& source) = 0;
|
|
|
|
/// Member is invoked by feed engine when
|
|
/// processing thread is about to end.
|
|
///
|
|
/// @note Invocation is done within the
|
|
/// thread that is about to end.
|
|
virtual
|
|
void
|
|
onFeedEngineThreadEnd(
|
|
const FeedEngine& source) = 0;
|
|
|
|
/// Is called when feed engine's thread is idle.
|
|
///
|
|
/// Thread becomes idle when either no data is received within
|
|
/// time interval defined by FeedEngineSettings::dataWaitTime
|
|
/// parameter or no pending data is available for processing.
|
|
/// In the first case, callback is invoked with 'DataWaitTimeout'
|
|
/// reason. In the second case, thread is considered as redundant
|
|
/// and thus callback is invoked with 'Redundant' reason.
|
|
/// After callback invocation threads may sleep in kernel to reduce
|
|
/// load onto CPU and racing between feed engine working threads.
|
|
///
|
|
/// @param source Identifies source engine instance going idle.
|
|
///
|
|
/// @param reason Identifies reason of thread idle state.
|
|
///
|
|
/// @param suggestedSleepTime defines amount of time feed engine suggest
|
|
/// for thread to sleep in kernel after invoking given member.
|
|
virtual
|
|
void
|
|
onFeedEngineThreadIdle(
|
|
const FeedEngine& source,
|
|
FeedEngineThreadIdleReason reason,
|
|
UInt32& suggestedSleepTime) = 0;
|
|
|
|
protected:
|
|
virtual ~FeedEngineListener();
|
|
};
|
|
|
|
/// Manages processing machinery for
|
|
/// market data received from feeds.
|
|
class
|
|
ONIXS_CME_MDP3_API
|
|
FeedEngine
|
|
{
|
|
public:
|
|
class Implementation;
|
|
|
|
/// Initializes engine with given configuration.
|
|
FeedEngine(
|
|
const FeedEngineSettings&,
|
|
FeedEngineListener* = NULL);
|
|
|
|
/// Destructs given instance.
|
|
~FeedEngine();
|
|
|
|
private:
|
|
friend class Implementation;
|
|
|
|
Implementation* impl_;
|
|
|
|
FeedEngine(const FeedEngine&);
|
|
FeedEngine& operator =(const FeedEngine&);
|
|
};
|
|
}
|
|
}
|
|
}
|