utils.h 3.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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
#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_UTILS_H
#define BAIDU_PADDLE_SERVING_PREDICTOR_UTILS_H

#include "common/macros.h"
#include "common/inner_common.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

class TimerFlow {
public:
    static const int MAX_SIZE = 1024;

    TimerFlow() {
        init();
    }

    void init() {
        _csize = 0;
        _name = NULL;
        _started = false;
        _auto = false;
    }

    TimerFlow(const char* name) : _csize(0), _name(name) {
        _last = _start = base::cpuwide_time_us();
        _auto = true;
        _started = true;
    }

    void set_name(const char* name) {
        _name = name;
    }

    void start() {
        _last = _start = base::cpuwide_time_us();
        _started = true;
    }

    bool check(const char* tag) {
        if (!_started) {
            LOG(WARNING) << "Timer not started yet!";
            return false;
        }
        uint64_t now = base::cpuwide_time_us();
        if (!appendf("%s:%lu|", tag, now - _last)) {
            LOG(WARNING) 
                << "Failed check timer: " << _name 
                << ", value = [" << tag << ":" 
                << (now - _last) << "]!" << noflush;
            return false;
        }

        _last = now;
        return true;
    }

    std::string info() {
        return std::string(_buf);
    }

    void end() {
        uint64_t now = base::cpuwide_time_us();
        if (!appendf("total:%lu", now - _start)) {
            LOG(WARNING) << "Failed dump time_info[" << _name << "]";
        }
        _started = false;
    }

    ~TimerFlow() {
        if (!_auto) {
            return;
        }
        uint64_t now = base::cpuwide_time_us();
        if (appendf("total:%lu,%s", now - _start, _name)) {
            LOG(INFO) 
                << " " << _name << "_tc=[" << _buf << "]";
        } else {
            LOG(WARNING) << "Failed dump time_info[" << _name << "]";
        }
    }

private:
    bool appendf(const char* fmt, ...) {
        va_list ap;
        va_start(ap, fmt);
        try {
            int bytes = vsnprintf(_buf + _csize, MAX_SIZE - _csize, fmt, ap);
            if (bytes >= MAX_SIZE - _csize || bytes < 0) {
                LOG(WARNING) << "Overflow when appendf!" << noflush;
                return false;
            }
            _csize += bytes;
        } CATCH_ANY_AND_RET(false);

        va_end(ap);
        return true;
    }

private:
    char _buf[1024];
    int _csize;
    uint64_t _start;
    uint64_t _last;
    const char* _name;
    bool _started;
    bool _auto;
};

template<bool flag>
struct derived_from_message {};

template<typename T, typename TBase>
class TIsDerivedFromB {
private:
    static uint8_t check(TBase*) {
        return 1;
    }

    static uint32_t check(void*) {
        return 0;
    }

public:
    enum {
        // function call cannot apprear in a constant-expression
        RESULT = (sizeof(uint8_t) == sizeof(check((T*)(NULL)))),
    };
};

template<typename TBase>
class IsDerivedFrom {
private:
    static bool check(TBase*) {
        return true;
    }

    static bool check(void*) {
        return false;
    }

public:
    template<typename T>
    static bool yes(T* x) {
        return check(x);
    }
};

} // predictor
} // paddle_serving
} // baidu

#endif