passive_status.h 8.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
gejun's avatar
gejun committed
17

gejun's avatar
gejun committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// Author: Ge,Jun (gejun@baidu.com)
// Date 2014/09/22 11:57:43

#ifndef  BVAR_PASSIVE_STATUS_H
#define  BVAR_PASSIVE_STATUS_H

#include "bvar/variable.h"
#include "bvar/reducer.h"

namespace bvar {

// Display a updated-by-need value. This is done by passing in an user callback
// which is called to produce the value.
// Example:
//   int print_number(void* arg) {
//      ...
//      return 5;
//   }
//
//   // number1 : 5
//   bvar::PassiveStatus<int> status1("number1", print_number, arg);
//
//   // foo_number2 : 5
//   bvar::PassiveStatus<int> status2("Foo", "number2", print_number, arg);
template <typename Tp>
class PassiveStatus : public Variable {
public:
    typedef Tp value_type;
    typedef detail::ReducerSampler<PassiveStatus, Tp, detail::AddTo<Tp>,
                                   detail::MinusFrom<Tp> > sampler_type;
    struct PlaceHolderOp {
        void operator()(Tp&, const Tp&) const {}
    };
51 52
    static const bool ADDITIVE = (butil::is_integral<Tp>::value ||
                                  butil::is_floating_point<Tp>::value ||
gejun's avatar
gejun committed
53 54 55
                                  is_vector<Tp>::value);
    class SeriesSampler : public detail::Sampler {
    public:
56
        typedef typename butil::conditional<
gejun's avatar
gejun committed
57 58 59 60 61 62
        ADDITIVE, detail::AddTo<Tp>, PlaceHolderOp>::type Op;
        explicit SeriesSampler(PassiveStatus* owner)
            : _owner(owner), _vector_names(NULL), _series(Op()) {}
        ~SeriesSampler() {
            delete _vector_names;
        }
gejun's avatar
gejun committed
63
        void take_sample() override { _series.append(_owner->get_value()); }
gejun's avatar
gejun committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        void describe(std::ostream& os) { _series.describe(os, _vector_names); }
        void set_vector_names(const std::string& names) {
            if (_vector_names == NULL) {
                _vector_names = new std::string;
            }
            *_vector_names = names;
        }
    private:
        PassiveStatus* _owner;
        std::string* _vector_names;
        detail::Series<Tp, Op> _series;
    };

public:
    // NOTE: You must be very careful about lifetime of `arg' which should be
    // valid during lifetime of PassiveStatus.
80
    PassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
81 82 83 84 85 86 87 88
                  Tp (*getfn)(void*), void* arg)
        : _getfn(getfn)
        , _arg(arg)
        , _sampler(NULL)
        , _series_sampler(NULL) {
        expose(name);
    }
    
89 90
    PassiveStatus(const butil::StringPiece& prefix,
                  const butil::StringPiece& name,
gejun's avatar
gejun committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                  Tp (*getfn)(void*), void* arg)
        : _getfn(getfn)
        , _arg(arg)
        , _sampler(NULL)
        , _series_sampler(NULL) {
        expose_as(prefix, name);
    }
    
    PassiveStatus(Tp (*getfn)(void*), void* arg) 
        : _getfn(getfn)
        , _arg(arg)
        , _sampler(NULL)
        , _series_sampler(NULL) {
    }

    ~PassiveStatus() {
        hide();
        if (_sampler) {
            _sampler->destroy();
            _sampler = NULL;
        }
        if (_series_sampler) {
            _series_sampler->destroy();
            _series_sampler = NULL;
        }
    }
    
    int set_vector_names(const std::string& names) {
        if (_series_sampler) {
            _series_sampler->set_vector_names(names);
            return 0;
        }
        return -1;
    }

gejun's avatar
gejun committed
126
    void describe(std::ostream& os, bool /*quote_string*/) const override {
gejun's avatar
gejun committed
127 128 129 130
        os << get_value();
    }

#ifdef BAIDU_INTERNAL
gejun's avatar
gejun committed
131
    void get_value(boost::any* value) const override {
gejun's avatar
gejun committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        if (_getfn) {
            *value = _getfn(_arg);
        } else {
            *value = Tp();
        }
    }
#endif
    
    Tp get_value() const {
        return (_getfn ? _getfn(_arg) : Tp());
    }
    
    sampler_type* get_sampler() {
        if (NULL == _sampler) {
            _sampler = new sampler_type(this);
            _sampler->schedule();
        }
        return _sampler;
    }

    detail::AddTo<Tp> op() const { return detail::AddTo<Tp>(); }
    detail::MinusFrom<Tp> inv_op() const { return detail::MinusFrom<Tp>(); }

155
    int describe_series(std::ostream& os, const SeriesOptions& options) const override {
gejun's avatar
gejun committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        if (_series_sampler == NULL) {
            return 1;
        }
        if (!options.test_only) {
            _series_sampler->describe(os);
        }
        return 0;
    }

    Tp reset() {
        CHECK(false) << "PassiveStatus::reset() should never be called, abort";
        abort();
    }

protected:
171 172
    int expose_impl(const butil::StringPiece& prefix,
                    const butil::StringPiece& name,
173
                    DisplayFilter display_filter) override {
gejun's avatar
gejun committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        const int rc = Variable::expose_impl(prefix, name, display_filter);
        if (ADDITIVE &&
            rc == 0 &&
            _series_sampler == NULL &&
            FLAGS_save_series) {
            _series_sampler = new SeriesSampler(this);
            _series_sampler->schedule();
        }
        return rc;
    }

private:
    Tp (*_getfn)(void*);
    void* _arg;
    sampler_type* _sampler;
    SeriesSampler* _series_sampler;
};

// ccover g++ may complain about ADDITIVE is undefined unless it's 
// explicitly declared here.
template <typename Tp> const bool PassiveStatus<Tp>::ADDITIVE;

// Specialize std::string for using std::ostream& as a more friendly
// interface for user's callback.
template <>
class PassiveStatus<std::string> : public Variable {
public:
    // NOTE: You must be very careful about lifetime of `arg' which should be
    // valid during lifetime of PassiveStatus.
203
    PassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
204 205 206 207 208
                  void (*print)(std::ostream&, void*), void* arg)
        : _print(print), _arg(arg) {
        expose(name);
    }

209 210
    PassiveStatus(const butil::StringPiece& prefix,
                  const butil::StringPiece& name,
gejun's avatar
gejun committed
211 212 213 214 215 216 217 218 219 220 221 222
                  void (*print)(std::ostream&, void*), void* arg)
        : _print(print), _arg(arg) {
        expose_as(prefix, name);
    }

    PassiveStatus(void (*print)(std::ostream&, void*), void* arg) 
        : _print(print), _arg(arg) {}

    ~PassiveStatus() {
        hide();
    }

gejun's avatar
gejun committed
223
    void describe(std::ostream& os, bool quote_string) const override {
gejun's avatar
gejun committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        if (quote_string) {
            if (_print) {
                os << '"';
                _print(os, _arg);
                os << '"';
            } else {
                os << "\"null\"";
            }
        } else {
            if (_print) {
                _print(os, _arg);
            } else {
                os << "null";
            }
        }
    }

private:
    void (*_print)(std::ostream&, void*);
    void* _arg;
};

template <typename Tp>
class BasicPassiveStatus : public PassiveStatus<Tp> {
public:
249
    BasicPassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
250 251 252
                       Tp (*getfn)(void*), void* arg)
        : PassiveStatus<Tp>(name, getfn, arg) {}
    
253 254
    BasicPassiveStatus(const butil::StringPiece& prefix,
                       const butil::StringPiece& name,
gejun's avatar
gejun committed
255 256 257 258 259 260 261 262 263 264
                       Tp (*getfn)(void*), void* arg)
        : PassiveStatus<Tp>(prefix, name, getfn, arg) {}

    BasicPassiveStatus(Tp (*getfn)(void*), void* arg)
        : PassiveStatus<Tp>(getfn, arg) {}
};

template <>
class BasicPassiveStatus<std::string> : public PassiveStatus<std::string> {
public:
265
    BasicPassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
266 267 268
                       void (*print)(std::ostream&, void*), void* arg)
        : PassiveStatus<std::string>(name, print, arg) {}
    
269 270
    BasicPassiveStatus(const butil::StringPiece& prefix,
                       const butil::StringPiece& name,
gejun's avatar
gejun committed
271 272 273 274 275 276 277 278 279 280 281
                       void (*print)(std::ostream&, void*), void* arg)
        : PassiveStatus<std::string>(prefix, name, print, arg) {}

    BasicPassiveStatus(void (*print)(std::ostream&, void*), void* arg)
        : PassiveStatus<std::string>(print, arg) {}
};


}  // namespace bvar

#endif  //BVAR_PASSIVE_STATUS_H