1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2012 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include "config.h"
19 
20 #include <memcached/engine.h>
21 #include <memcached/protocol_binary.h>
22 
23 #include <iostream>
24 
25 #include "ep_engine.h"
26 #include "histo.h"
27 
28 #ifndef SRC_STATWRITER_H_
29 #define SRC_STATWRITER_H_ 1
30 
31 namespace STATWRITER_NAMESPACE {
32 
add_casted_stat(const char *k, const char *v, ADD_STAT add_stat, const void *cookie)33 inline void add_casted_stat(const char *k, const char *v,
34                             ADD_STAT add_stat, const void *cookie) {
35     EventuallyPersistentEngine *e = ObjectRegistry::onSwitchThread(NULL, true);
36     add_stat(k, static_cast<uint16_t>(strlen(k)),
37              v, static_cast<uint32_t>(strlen(v)), cookie);
38     ObjectRegistry::onSwitchThread(e);
39 }
40 
41 template <typename T>
add_casted_stat(const char *k, const T &v, ADD_STAT add_stat, const void *cookie)42 void add_casted_stat(const char *k, const T &v,
43                             ADD_STAT add_stat, const void *cookie) {
44     std::stringstream vals;
45     vals << v;
46     add_casted_stat(k, vals.str().c_str(), add_stat, cookie);
47 }
48 
49 template <typename T>
add_casted_stat(const char *k, const AtomicValue<T> &v, ADD_STAT add_stat, const void *cookie)50 void add_casted_stat(const char *k, const AtomicValue<T> &v,
51                             ADD_STAT add_stat, const void *cookie) {
52     add_casted_stat(k, v.load(), add_stat, cookie);
53 }
54 
55 /// @cond DETAILS
56 /**
57  * Convert a histogram into a bunch of calls to add stats.
58  */
59 template <typename T>
60 struct histo_stat_adder {
histo_stat_adderSTATWRITER_NAMESPACE::histo_stat_adder61     histo_stat_adder(const char *k, ADD_STAT a, const void *c)
62         : prefix(k), add_stat(a), cookie(c) {}
operator ()STATWRITER_NAMESPACE::histo_stat_adder63     void operator() (const HistogramBin<T>* b) {
64         if (b->count()) {
65             std::stringstream ss;
66             ss << prefix << "_" << b->start() << "," << b->end();
67             add_casted_stat(ss.str().c_str(), b->count(), add_stat, cookie);
68         }
69     }
70     const char *prefix;
71     ADD_STAT add_stat;
72     const void *cookie;
73 };
74 /// @endcond
75 
76 template <typename T>
add_casted_stat(const char *k, const Histogram<T> &v, ADD_STAT add_stat, const void *cookie)77 void add_casted_stat(const char *k, const Histogram<T> &v,
78                             ADD_STAT add_stat, const void *cookie) {
79     histo_stat_adder<T> a(k, add_stat, cookie);
80     std::for_each(v.begin(), v.end(), a);
81 }
82 
83 template <typename P, typename T>
add_prefixed_stat(P prefix, const char *nm, T val, ADD_STAT add_stat, const void *cookie)84 void add_prefixed_stat(P prefix, const char *nm, T val,
85                   ADD_STAT add_stat, const void *cookie) {
86     std::stringstream name;
87     name << prefix << ":" << nm;
88 
89     add_casted_stat(name.str().c_str(), val, add_stat, cookie);
90 }
91 
92 template <typename P, typename T>
add_prefixed_stat(P prefix, const char *nm, Histogram<T> &val, ADD_STAT add_stat, const void *cookie)93 void add_prefixed_stat(P prefix, const char *nm, Histogram<T> &val,
94                   ADD_STAT add_stat, const void *cookie) {
95     std::stringstream name;
96     name << prefix << ":" << nm;
97 
98     add_casted_stat(name.str().c_str(), val, add_stat, cookie);
99 }
100 
101 }
102 
103 using namespace STATWRITER_NAMESPACE;
104 
105 #endif  // SRC_STATWRITER_H_
106