1 /*
2  *     Copyright 2010 NorthScale, Inc.
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  */
16 #include <stdio.h>
17 
18 #include <algorithm>
19 #include <limits>
20 
21 #include "atomic_unordered_map.h"
22 #include "checkpoint.h"
23 #include "checkpoint_config.h"
24 #include "checkpoint_manager.h"
25 #include "common.h"
26 #include "couch-kvstore/couch-kvstore.h"
27 #include "dcp/response.h"
28 #include "dcp/stream.h"
29 #include "hash_table_stat_visitor.h"
30 #include "item.h"
31 #include "kvstore_priv.h"
32 #include "persistence_callback.h"
33 #include "probabilistic_counter.h"
34 #include "stats.h"
35 #include "stored-value.h"
36 #include "vbucket.h"
37 #include "vbucketmap.h"
38 #include <platform/histogram.h>
39 #include <platform/timeutils.h>
40 
display(const char *name, size_t size)41 static void display(const char *name, size_t size) {
42     std::cout << name << "\t" << size << std::endl;
43 }
44 
45 template <typename T, template <class> class Traits>
46 struct histo_for_inner {
operator ()histo_for_inner47     void operator()(const std::unique_ptr<HistogramBin<T, Traits>>& bin) {
48         std::cout << "   " << bin->start() << " - ";
49         if (bin->end() == Traits<T>::max()) {
50             std::cout << "inf";
51         } else {
52             std::cout << bin->end();
53         }
54         std::cout << std::endl;
55     }
56 };
57 
58 template <typename T, template <class> class Traits>
display(const char* name, const Histogram<T, Traits>& histo)59 static void display(const char* name, const Histogram<T, Traits>& histo) {
60     std::cout << name << std::endl;
61     std::for_each(histo.begin(), histo.end(), histo_for_inner<T, Traits>());
62 }
63 
main(int, char **)64 int main(int, char **) {
65     std::string s;
66 
67     display("GIGANTOR", GIGANTOR);
68     display("StoredValue", sizeof(StoredValue));
69     display("StoredValue with 15 byte key",
70             StoredValue::getRequiredStorage(
71                     DocKey("1234567890abcde", DocKeyEncodesCollectionId::No)));
72     display("Ordered Stored Value", sizeof(OrderedStoredValue));
73     display("Blob", sizeof(Blob));
74     display("value_t", sizeof(value_t));
75     display("HashTable", sizeof(HashTable));
76     display("Item", sizeof(Item));
77     display("VBucket", sizeof(VBucket));
78     display("VBucketMap", sizeof(VBucketMap));
79     display("Stats", sizeof(EPStats));
80     display("CheckpointManager", sizeof(CheckpointManager));
81     display("Checkpoint\t", sizeof(Checkpoint));
82     display("CheckpointConfig", sizeof(CheckpointConfig));
83     display("Histogram<whatever>", Histogram<size_t>().getMemFootPrint());
84     display("HistogramBin<size_t>", sizeof(HistogramBin<size_t>));
85     display("HistogramBin<int>", sizeof(HistogramBin<int>));
86     display("HistogramBin<microseconds>",
87             sizeof(MicrosecondHistogram::bin_type));
88     display("MicrosecondHistogram", MicrosecondHistogram().getMemFootPrint());
89     EPStats stats;
90     const size_t size = GlobalTask::allTaskIds.size();
91     stats.schedulingHisto.resize(size);
92     stats.taskRuntimeHisto.resize(size);
93     display("EPStats", stats.getMemFootPrint());
94     display("FileStats", FileStats().getMemFootPrint());
95     display("KVStoreStats", KVStoreStats().getMemFootPrint());
96     display("Histogram<size_t>{ExponentialGenerator<size_t>(1, 2), 50}",
97             Histogram<size_t>{ExponentialGenerator<size_t>(1, 2), 50}
98                     .getMemFootPrint());
99     display("HdrHistogram frequency histo",
100             stats.activeOrPendingFrequencyValuesEvictedHisto.getMemFootPrint());
101     display("Hdr1sfMicroSecHistogram",
102             Hdr1sfMicroSecHistogram().getMemFootPrint());
103     display("Hdr2sfMicroSecHistogram",
104             Hdr2sfMicroSecHistogram().getMemFootPrint());
105 
106     display("HdrUint8Histogram", HdrUint8Histogram().getMemFootPrint());
107     display("Hdr1sfInt32Histogram", Hdr1sfInt32Histogram().getMemFootPrint());
108 
109     display("HdrHistogram(0, std::numeric_limits<int32_t>::max(), 2)",
110             HdrHistogram(0, std::numeric_limits<int32_t>::max() - 1, 2)
111                     .getMemFootPrint());
112     display("HdrHistogram(0, std::numeric_limits<int32_t>::max(), 1)",
113             HdrHistogram(0, std::numeric_limits<int32_t>::max() - 1, 1)
114                     .getMemFootPrint());
115     display("HdrHistogram(0, std::numeric_limits<int64_t>::max(), 2)",
116             HdrHistogram(0, std::numeric_limits<int64_t>::max() - 1, 2)
117                     .getMemFootPrint());
118     display("HdrHistogram(0, std::numeric_limits<int64_t>::max(), 1)",
119             HdrHistogram(0, std::numeric_limits<int64_t>::max() - 1, 1)
120                     .getMemFootPrint());
121 
122     display("IORequest", sizeof(IORequest));
123     display("CouchRequest", sizeof(CouchRequest));
124     display("PersistenceCallback", sizeof(PersistenceCallback));
125     display("AtomicUnorderedMap<uint32_t, SingleThreadedRCPtr<Stream>>",
126             sizeof(AtomicUnorderedMap<uint32_t, SingleThreadedRCPtr<Stream>>));
127     display("ProbabilisticCounter<uint8_t>",
128             sizeof(ProbabilisticCounter<uint8_t>));
129     display("DcpResponse", sizeof(DcpResponse));
130     display("MutationResponse", sizeof(MutationResponse));
131     display("queued_item", sizeof(queued_item));
132 
133     std::cout << std::endl << "Histogram Ranges" << std::endl << std::endl;
134 
135     HashTableDepthStatVisitor dv;
136     display("Default Histo", stats.diskInsertHisto.getMemFootPrint());
137     display("Commit Histo", stats.diskCommitHisto.getMemFootPrint());
138     display("Hash table depth histo", dv.depthHisto.getMemFootPrint());
139     return 0;
140 }
141