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 * - Christopher Chan-Nui
35 * - Cecil Reames
36 */
37
38#include "utilities/threading.h"
39#include "main/unusedflag.h"
40
41#include <stdexcept>
42#include <iomanip>
43
44#include "main/bucketsimulator.h"
45
46namespace TPCE {
47
48BucketSimulator::BucketSimulator(int iterstart, int itercount, TIdent iCustomerCount, INT64 simorders,
49 TPCE::RNGSEED base_seed, BucketProgress &progress)
50 : m_rnd(), m_buckets(NULL), m_custcount(iCustomerCount), m_iterstart(iterstart), m_itercount(itercount),
51 m_baseseed(base_seed), m_simorders(simorders),
52 m_maxbucket(static_cast<int>(iCustomerCount / iDefaultLoadUnitSize)), m_progress(progress) {
53 if (iCustomerCount % iDefaultLoadUnitSize != 0) {
54 throw std::range_error("The customer count must be an integral "
55 "multiple of the load unit size!");
56 }
57 if (m_maxbucket < 2) {
58 throw std::range_error("Bucket simulator must have at least 2 buckets!");
59 }
60 m_buckets = new INT64[m_maxbucket];
61}
62
63BucketSimulator::~BucketSimulator() {
64 delete[] m_buckets;
65}
66
67// Calculate the standard deviation of the current bucket
68// Use straightforward method rather than sum of squares...
69double BucketSimulator::calc_stddev() {
70 double sum = 0;
71 for (int i = 0; i < m_maxbucket; ++i) {
72 sum += m_buckets[i];
73 }
74 double mean = sum / (double)m_maxbucket;
75
76 double sumdev2 = 0;
77 for (int i = 0; i < m_maxbucket; ++i) {
78 sumdev2 += (m_buckets[i] - mean) * (m_buckets[i] - mean);
79 }
80 double stddev = sqrt(sumdev2 / (double)(m_maxbucket - 1));
81
82 return stddev;
83}
84
85// Simulates one run and returns the standard deviation of the buckets
86// iterations - Number of "orders" to simulate (note that we don't take into
87// account the 1% rollbacks)
88double BucketSimulator::simulate_onerun(INT64 iorders) {
89 int bucket;
90
91 memset(m_buckets, 0, sizeof(m_buckets[0]) * m_maxbucket);
92
93 for (INT64 i = 0; i < iorders; ++i) {
94 bucket = m_rnd.RndIntRange(0, static_cast<int>((m_custcount - 1) / static_cast<TIdent>(iDefaultLoadUnitSize)));
95 m_buckets[bucket] += 1;
96 }
97
98 return calc_stddev();
99}
100
101// Perform all of the simulations this instance was constructed with and return
102// the maximum standard deviation
103double BucketSimulator::simulate() {
104 double max_stddev = 0;
105 TPCE::RNGSEED seed = m_rnd.RndNthElement(m_baseseed, (RNGSEED)(m_iterstart * m_simorders * RND_STEP_PER_ORDER));
106 m_rnd.SetSeed(seed);
107 for (int i = 0; i < m_itercount; ++i) {
108 TPCE::RNGSEED current_seed = m_rnd.GetSeed();
109 double stddev = simulate_onerun(m_simorders);
110
111 std::ostringstream strm;
112 strm << "StdDev for run " << i + m_iterstart << " is " << std::setprecision(3) << std::fixed << stddev
113 << ", seed was " << current_seed;
114 m_progress.message(strm.str(), 1);
115
116 if (max_stddev < stddev) {
117 max_stddev = stddev;
118 }
119 if (!m_progress.report(stddev)) {
120 break;
121 }
122 }
123 return max_stddev;
124}
125
126// Entry point to be run as a thread
127void BucketSimulator::run(void *thread UNUSED) {
128 simulate();
129}
130
131//////////////////////////////////////////////////////////////////////////////
132
133BucketProgress::BucketProgress(double acceptable_stddev, int total_in, int verbosity, std::ostream *output)
134 : ProgressMeter(total_in, verbosity, output), acceptable_stddev_(acceptable_stddev), max_stddev_(0) {
135}
136
137void BucketProgress::display_message(std::ostream &out) const {
138 ProgressMeter::display_message(out);
139 out << " stddev=" << max_stddev_;
140}
141
142bool BucketProgress::report(double stddev) {
143 inc();
144 TPCE::Locker<ProgressMeter> locker(*this);
145 if (max_stddev_ < stddev) {
146 max_stddev_ = stddev;
147 }
148 return (max_stddev_ < acceptable_stddev_);
149}
150
151double BucketProgress::max_stddev() {
152 TPCE::Locker<ProgressMeter> locker(*this);
153 return max_stddev_;
154}
155
156} // namespace TPCE
157