1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors
34 * - Doug Johnson, Matt Emmerton
35 */
36
37/******************************************************************************
38 * Description: Implementation of the CE class.
39 * See CE.h for a description.
40 ******************************************************************************/
41
42#include "main/CE.h"
43
44using namespace TPCE;
45
46// Initialization that is common for all constructors.
47void CCE::Initialize(PDriverCETxnSettings pTxnParamSettings) {
48 m_pLogger->SendToLogger(m_DriverGlobalSettings);
49
50 // Always configure the CE with default settings, to ensure that it is
51 // set up properly.
52 SetTxnTunables(&m_DriverCETxnSettings);
53
54 // If user tunables are provided, set them now. If they are invalid, they
55 // will not be used and we will continue to use the defaults set above.
56 if (pTxnParamSettings) {
57 SetTxnTunables(pTxnParamSettings);
58 }
59}
60
61// Automatically generate unique RNG seeds.
62// The CRandom class uses an unsigned 64-bit value for the seed.
63// This routine automatically generates two unique seeds. One is used for
64// the TxnInput generator RNG, and the other is for the TxnMixGenerator RNG.
65// The 64 bits are used as follows.
66//
67// Bits 0 - 31 Caller provided unique unsigned 32-bit id.
68// Bit 32 0 for TxnInputGenerator, 1 for TxnMixGenerator
69// Bits 33 - 43 Number of days since the base time. The base time
70// is set to be January 1 of the most recent year that is
71// a multiple of 5. This allows enough space for the last
72// field, and it makes the algorithm "timeless" by resetting
73// the generated values every 5 years.
74// Bits 44 - 63 Current time of day measured in 1/10's of a second.
75//
76void CCE::AutoSetRNGSeeds(UINT32 UniqueId) {
77 CDateTime Now;
78 INT32 BaseYear;
79 INT32 Tmp1, Tmp2;
80
81 Now.GetYMD(&BaseYear, &Tmp1, &Tmp2);
82
83 // Set the base year to be the most recent year that was a multiple of 5.
84 BaseYear -= (BaseYear % 5);
85 CDateTime Base(BaseYear, 1, 1); // January 1st in the BaseYear
86
87 // Initialize the seed with the current time of day measured in 1/10's of a
88 // second. This will use up to 20 bits.
89 RNGSEED Seed;
90 Seed = Now.MSec() / 100;
91
92 // Now add in the number of days since the base time.
93 // The number of days in the 5 year period requires 11 bits.
94 // So shift up by that much to make room in the "lower" bits.
95 Seed <<= 11;
96 Seed += (RNGSEED)((INT64)Now.DayNo() - (INT64)Base.DayNo());
97
98 // So far, we've used up 31 bits.
99 // Save the "last" bit of the "upper" 32 for the RNG id.
100 // In addition, make room for the caller's 32-bit unique id.
101 // So shift a total of 33 bits.
102 Seed <<= 33;
103
104 // Now the "upper" 32-bits have been set with a value for RNG 0.
105 // Add in the sponsor's unique id for the "lower" 32-bits.
106 Seed += UniqueId;
107
108 // Set the TxnMixGenerator RNG to the unique seed.
109 m_TxnMixGenerator.SetRNGSeed(Seed);
110 m_DriverCESettings.cur.TxnMixRNGSeed = Seed;
111
112 // Set the RNG Id to 1 for the TxnInputGenerator.
113 Seed |= UINT64_CONST(0x0000000100000000);
114 m_TxnInputGenerator.SetRNGSeed(Seed);
115 m_DriverCESettings.cur.TxnInputRNGSeed = Seed;
116}
117
118/*
119 * Constructor - no partitioning by C_ID, automatic RNG seed generation
120 * (requires unique input)
121 */
122CCE::CCE(CCESUTInterface *pSUT, CBaseLogger *pLogger, const DataFileManager &dfm, TIdent iConfiguredCustomerCount,
123 TIdent iActiveCustomerCount, INT32 iScaleFactor, INT32 iDaysOfInitialTrades, UINT32 UniqueId,
124 const PDriverCETxnSettings pDriverCETxnSettings)
125 : m_DriverGlobalSettings(iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor, iDaysOfInitialTrades),
126 m_DriverCESettings(UniqueId, 0, 0), m_pSUT(pSUT), m_pLogger(pLogger),
127 m_TxnMixGenerator(&m_DriverCETxnSettings, m_pLogger),
128 m_TxnInputGenerator(dfm, iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor,
129 iDaysOfInitialTrades * HoursPerWorkDay, m_pLogger, &m_DriverCETxnSettings) {
130 m_pLogger->SendToLogger("CE object constructed using constructor 1 (valid "
131 "for publication: YES).");
132
133 Initialize(pDriverCETxnSettings);
134 AutoSetRNGSeeds(UniqueId);
135
136 m_pLogger->SendToLogger(m_DriverCESettings); // log the RNG seeds
137}
138
139/*
140 * Constructor - no partitioning by C_ID, RNG seeds provided
141 */
142CCE::CCE(CCESUTInterface *pSUT, CBaseLogger *pLogger, const DataFileManager &dfm, TIdent iConfiguredCustomerCount,
143 TIdent iActiveCustomerCount, INT32 iScaleFactor, INT32 iDaysOfInitialTrades, UINT32 UniqueId,
144 RNGSEED TxnMixRNGSeed, RNGSEED TxnInputRNGSeed, const PDriverCETxnSettings pDriverCETxnSettings)
145 : m_DriverGlobalSettings(iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor, iDaysOfInitialTrades),
146 m_DriverCESettings(UniqueId, TxnMixRNGSeed, TxnInputRNGSeed), m_pSUT(pSUT), m_pLogger(pLogger),
147 m_TxnMixGenerator(&m_DriverCETxnSettings, TxnMixRNGSeed, m_pLogger),
148 m_TxnInputGenerator(dfm, iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor,
149 iDaysOfInitialTrades * HoursPerWorkDay, TxnInputRNGSeed, m_pLogger, &m_DriverCETxnSettings) {
150 m_pLogger->SendToLogger("CE object constructed using constructor 2 (valid "
151 "for publication: NO).");
152
153 Initialize(pDriverCETxnSettings);
154
155 m_pLogger->SendToLogger(m_DriverCESettings); // log the RNG seeds
156}
157
158/*
159 * Constructor - partitioning by C_ID, automatic RNG seed generation (requires
160 * unique input)
161 */
162CCE::CCE(CCESUTInterface *pSUT, CBaseLogger *pLogger, const DataFileManager &dfm, TIdent iConfiguredCustomerCount,
163 TIdent iActiveCustomerCount, TIdent iMyStartingCustomerId, TIdent iMyCustomerCount, INT32 iPartitionPercent,
164 INT32 iScaleFactor, INT32 iDaysOfInitialTrades, UINT32 UniqueId,
165 const PDriverCETxnSettings pDriverCETxnSettings)
166 : m_DriverGlobalSettings(iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor, iDaysOfInitialTrades),
167 m_DriverCESettings(UniqueId, 0, 0),
168 m_DriverCEPartitionSettings(iMyStartingCustomerId, iMyCustomerCount, iPartitionPercent), m_pSUT(pSUT),
169 m_pLogger(pLogger), m_TxnMixGenerator(&m_DriverCETxnSettings, m_pLogger),
170 m_TxnInputGenerator(dfm, iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor,
171 iDaysOfInitialTrades * HoursPerWorkDay, iMyStartingCustomerId, iMyCustomerCount,
172 iPartitionPercent, m_pLogger, &m_DriverCETxnSettings) {
173 m_pLogger->SendToLogger("CE object constructed using constructor 3 (valid "
174 "for publication: YES).");
175
176 Initialize(pDriverCETxnSettings);
177 AutoSetRNGSeeds(UniqueId);
178
179 m_pLogger->SendToLogger(m_DriverCEPartitionSettings); // log the partition settings
180 m_pLogger->SendToLogger(m_DriverCESettings); // log the RNG seeds
181}
182
183/*
184 * Constructor - partitioning by C_ID, RNG seeds provided
185 */
186CCE::CCE(CCESUTInterface *pSUT, CBaseLogger *pLogger, const DataFileManager &dfm, TIdent iConfiguredCustomerCount,
187 TIdent iActiveCustomerCount, TIdent iMyStartingCustomerId, TIdent iMyCustomerCount, INT32 iPartitionPercent,
188 INT32 iScaleFactor, INT32 iDaysOfInitialTrades, UINT32 UniqueId, RNGSEED TxnMixRNGSeed,
189 RNGSEED TxnInputRNGSeed, const PDriverCETxnSettings pDriverCETxnSettings)
190 : m_DriverGlobalSettings(iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor, iDaysOfInitialTrades),
191 m_DriverCESettings(UniqueId, TxnMixRNGSeed, TxnInputRNGSeed),
192 m_DriverCEPartitionSettings(iMyStartingCustomerId, iMyCustomerCount, iPartitionPercent), m_pSUT(pSUT),
193 m_pLogger(pLogger), m_TxnMixGenerator(&m_DriverCETxnSettings, TxnMixRNGSeed, m_pLogger),
194 m_TxnInputGenerator(dfm, iConfiguredCustomerCount, iActiveCustomerCount, iScaleFactor,
195 iDaysOfInitialTrades * HoursPerWorkDay, iMyStartingCustomerId, iMyCustomerCount,
196 iPartitionPercent, TxnInputRNGSeed, m_pLogger, &m_DriverCETxnSettings) {
197 m_pLogger->SendToLogger("CE object constructed using constructor 4 (valid "
198 "for publication: NO).");
199
200 Initialize(pDriverCETxnSettings);
201
202 m_pLogger->SendToLogger(m_DriverCEPartitionSettings); // log the partition settings
203 m_pLogger->SendToLogger(m_DriverCESettings); // log the RNG seeds
204}
205
206CCE::~CCE(void) {
207 m_pLogger->SendToLogger("CE object destroyed.");
208}
209
210RNGSEED CCE::GetTxnInputGeneratorRNGSeed(void) {
211 return (m_TxnInputGenerator.GetRNGSeed());
212}
213
214RNGSEED CCE::GetTxnMixGeneratorRNGSeed(void) {
215 return (m_TxnMixGenerator.GetRNGSeed());
216}
217
218void CCE::SetTxnTunables(const PDriverCETxnSettings pTxnParamSettings) {
219 if (pTxnParamSettings->IsValid()) {
220 // Update Tunables
221 if (pTxnParamSettings != &m_DriverCETxnSettings) // only copy from a different location
222 {
223 m_DriverCETxnSettings = *pTxnParamSettings;
224 }
225
226 // Trigger Runtime Updates
227 m_TxnMixGenerator.UpdateTunables();
228 m_TxnInputGenerator.UpdateTunables();
229 } else {
230 m_pLogger->SendToLogger("ERROR: CCE::SetTxnTunables() failed due to invalid tunables.");
231 }
232}
233
234void CCE::DoTxn(void) {
235 int iTxnType = m_TxnMixGenerator.GenerateNextTxnType();
236
237 switch (iTxnType) {
238 case CCETxnMixGenerator::BROKER_VOLUME:
239 m_TxnInputGenerator.GenerateBrokerVolumeInput(m_BrokerVolumeTxnInput);
240 m_pSUT->BrokerVolume(&m_BrokerVolumeTxnInput);
241 break;
242 case CCETxnMixGenerator::CUSTOMER_POSITION:
243 m_TxnInputGenerator.GenerateCustomerPositionInput(m_CustomerPositionTxnInput);
244 m_pSUT->CustomerPosition(&m_CustomerPositionTxnInput);
245 break;
246 case CCETxnMixGenerator::MARKET_WATCH:
247 m_TxnInputGenerator.GenerateMarketWatchInput(m_MarketWatchTxnInput);
248 m_pSUT->MarketWatch(&m_MarketWatchTxnInput);
249 break;
250 case CCETxnMixGenerator::SECURITY_DETAIL:
251 m_TxnInputGenerator.GenerateSecurityDetailInput(m_SecurityDetailTxnInput);
252 m_pSUT->SecurityDetail(&m_SecurityDetailTxnInput);
253 break;
254 case CCETxnMixGenerator::TRADE_LOOKUP:
255 m_TxnInputGenerator.GenerateTradeLookupInput(m_TradeLookupTxnInput);
256 m_pSUT->TradeLookup(&m_TradeLookupTxnInput);
257 break;
258 case CCETxnMixGenerator::TRADE_ORDER:
259 bool bExecutorIsAccountOwner;
260 INT32 iTradeType;
261 m_TxnInputGenerator.GenerateTradeOrderInput(m_TradeOrderTxnInput, iTradeType, bExecutorIsAccountOwner);
262 m_pSUT->TradeOrder(&m_TradeOrderTxnInput, iTradeType, bExecutorIsAccountOwner);
263 break;
264 case CCETxnMixGenerator::TRADE_STATUS:
265 m_TxnInputGenerator.GenerateTradeStatusInput(m_TradeStatusTxnInput);
266 m_pSUT->TradeStatus(&m_TradeStatusTxnInput);
267 break;
268 case CCETxnMixGenerator::TRADE_UPDATE:
269 m_TxnInputGenerator.GenerateTradeUpdateInput(m_TradeUpdateTxnInput);
270 m_pSUT->TradeUpdate(&m_TradeUpdateTxnInput);
271 break;
272 default:
273 cerr << "CE: Generated illegal transaction" << endl;
274 exit(1);
275 }
276}
277