passive_status.h 8.22 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed 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
15 16 17 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
// 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 {}
    };
48 49
    static const bool ADDITIVE = (butil::is_integral<Tp>::value ||
                                  butil::is_floating_point<Tp>::value ||
gejun's avatar
gejun committed
50 51 52
                                  is_vector<Tp>::value);
    class SeriesSampler : public detail::Sampler {
    public:
53
        typedef typename butil::conditional<
gejun's avatar
gejun committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        ADDITIVE, detail::AddTo<Tp>, PlaceHolderOp>::type Op;
        explicit SeriesSampler(PassiveStatus* owner)
            : _owner(owner), _vector_names(NULL), _series(Op()) {}
        ~SeriesSampler() {
            delete _vector_names;
        }
        void take_sample() { _series.append(_owner->get_value()); }
        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.
77
    PassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
78 79 80 81 82 83 84 85
                  Tp (*getfn)(void*), void* arg)
        : _getfn(getfn)
        , _arg(arg)
        , _sampler(NULL)
        , _series_sampler(NULL) {
        expose(name);
    }
    
86 87
    PassiveStatus(const butil::StringPiece& prefix,
                  const butil::StringPiece& name,
gejun's avatar
gejun committed
88 89 90 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                  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;
    }

    void describe(std::ostream& os, bool /*quote_string*/) const {
        os << get_value();
    }

#ifdef BAIDU_INTERNAL
    void get_value(boost::any* value) const {
        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>(); }

    int describe_series(std::ostream& os, const SeriesOptions& options) const {
        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:
    // @Variable
169 170
    int expose_impl(const butil::StringPiece& prefix,
                    const butil::StringPiece& name,
gejun's avatar
gejun committed
171 172 173 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
                    DisplayFilter display_filter) {
        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.
201
    PassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
202 203 204 205 206
                  void (*print)(std::ostream&, void*), void* arg)
        : _print(print), _arg(arg) {
        expose(name);
    }

207 208
    PassiveStatus(const butil::StringPiece& prefix,
                  const butil::StringPiece& name,
gejun's avatar
gejun committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                  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();
    }

    void describe(std::ostream& os, bool quote_string) const {
        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:
247
    BasicPassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
248 249 250
                       Tp (*getfn)(void*), void* arg)
        : PassiveStatus<Tp>(name, getfn, arg) {}
    
251 252
    BasicPassiveStatus(const butil::StringPiece& prefix,
                       const butil::StringPiece& name,
gejun's avatar
gejun committed
253 254 255 256 257 258 259 260 261 262
                       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:
263
    BasicPassiveStatus(const butil::StringPiece& name,
gejun's avatar
gejun committed
264 265 266
                       void (*print)(std::ostream&, void*), void* arg)
        : PassiveStatus<std::string>(name, print, arg) {}
    
267 268
    BasicPassiveStatus(const butil::StringPiece& prefix,
                       const butil::StringPiece& name,
gejun's avatar
gejun committed
269 270 271 272 273 274 275 276 277 278 279
                       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