|
| 1 | +// Copyright 2004-present Facebook. All Rights Reserved. |
| 2 | +#include <memory> |
| 3 | +#include <cassert> |
| 4 | +#include "perf_counters.h" |
| 5 | + |
| 6 | +#if FB_DYNO |
| 7 | +#include <dyno_counters.h> |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <ctime> |
| 11 | + |
| 12 | +#define diff_timespec(TS1, TS2) \ |
| 13 | + ((TS1.tv_sec - TS2.tv_sec) * 1000000000ULL + TS1.tv_nsec - TS2.tv_nsec) |
| 14 | + |
| 15 | +//////////////////////////////////////////////////////////////////////////////// |
| 16 | +namespace utils { |
| 17 | + |
| 18 | +template <class T> using unique_ptr = std::unique_ptr<T>; |
| 19 | + |
| 20 | +class SimplePerfCounterFactory : public PerfCounterFactory { |
| 21 | + |
| 22 | + std::shared_ptr<PerfCounter> makeSharedPerfCounter( |
| 23 | + PerfCounterMode mode, PerfCounterType c_type) override; |
| 24 | + |
| 25 | + std::unique_ptr<PerfCounter> makePerfCounter( |
| 26 | + PerfCounterMode mode, PerfCounterType c_type) override; |
| 27 | +}; |
| 28 | + |
| 29 | +class ThreadPerfCounterImpl : public PerfCounter { |
| 30 | + private: |
| 31 | + PerfCounterType _counter_type; |
| 32 | + timespec _time_beg; |
| 33 | + bool _started; |
| 34 | + |
| 35 | + uint64_t get_impl(timespec *time_cur); |
| 36 | + |
| 37 | + public: |
| 38 | + explicit ThreadPerfCounterImpl(PerfCounterType counterType) |
| 39 | + : _counter_type(counterType), _started(false) { } |
| 40 | + |
| 41 | + void start() override; |
| 42 | + void stop() override { _started= false; } |
| 43 | + uint64_t get() override { |
| 44 | + timespec time_cur; |
| 45 | + uint64_t diff_cost= get_impl(&time_cur); |
| 46 | + return diff_cost; |
| 47 | + } |
| 48 | + |
| 49 | + uint64_t getAndRestart() override { |
| 50 | + timespec time_cur; |
| 51 | + uint64_t diff_cost= get_impl(&time_cur); |
| 52 | + _time_beg= time_cur; |
| 53 | + _started= true; |
| 54 | + return diff_cost; |
| 55 | + } |
| 56 | + uint64_t getAndStop() override { |
| 57 | + uint64_t diff_cost= get(); |
| 58 | + _started= false; |
| 59 | + return diff_cost; |
| 60 | + } |
| 61 | + float getMultiplexFactor() override { return 1.0; } |
| 62 | +}; |
| 63 | + |
| 64 | +void ThreadPerfCounterImpl::start() { |
| 65 | + int cpu_res= clock_gettime(CLOCK_THREAD_CPUTIME_ID, &_time_beg); |
| 66 | + _started= (cpu_res == 0); |
| 67 | +} |
| 68 | + |
| 69 | +uint64_t ThreadPerfCounterImpl::get_impl(timespec *time_cur) { |
| 70 | + if (!_started) |
| 71 | + return 0; |
| 72 | + |
| 73 | + int cpu_cur= clock_gettime(CLOCK_THREAD_CPUTIME_ID, time_cur); |
| 74 | + if (cpu_cur != 0) |
| 75 | + return 0; |
| 76 | + |
| 77 | + uint64_t diff_ns= diff_timespec((*time_cur), _time_beg); |
| 78 | + // assuming 2.4 GHz. |
| 79 | + return ((diff_ns * 1.0) / 0.41666); |
| 80 | +} |
| 81 | + |
| 82 | +class ProcessPerfCounterImpl : public PerfCounter { |
| 83 | + public: |
| 84 | + explicit ProcessPerfCounterImpl(PerfCounterType counterType) {} |
| 85 | + |
| 86 | + void start() override { } |
| 87 | + void stop() override { } |
| 88 | + uint64_t get() override { return 0; } |
| 89 | + uint64_t getAndRestart() override { return 0; } |
| 90 | + uint64_t getAndStop() override { return 0; } |
| 91 | + float getMultiplexFactor() override { return 1.0; } |
| 92 | +}; |
| 93 | + |
| 94 | +template <PerfCounterMode> struct CounterModeToType; |
| 95 | + |
| 96 | +template <> struct CounterModeToType<PerfCounterMode::PCM_THREAD> { |
| 97 | + using type = ThreadPerfCounterImpl; |
| 98 | +}; |
| 99 | + |
| 100 | +template <> struct CounterModeToType<PerfCounterMode::PCM_PROCESS> { |
| 101 | + using type = ProcessPerfCounterImpl; |
| 102 | +}; |
| 103 | + |
| 104 | +template <class T> using unique_ptr = std::unique_ptr<T>; |
| 105 | + |
| 106 | +template <template <typename> class T, class D, class... Args> |
| 107 | +struct PerfCounterMaker; |
| 108 | + |
| 109 | +template <class D, class... Args> |
| 110 | +struct PerfCounterMaker<std::shared_ptr, D, Args...> { |
| 111 | + static std::shared_ptr<D> make(Args&&... args) { |
| 112 | + return std::make_shared<D>(std::forward<Args>(args)...); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +template <class D, class... Args> |
| 117 | +struct PerfCounterMaker<unique_ptr, D, Args...> { |
| 118 | + static std::unique_ptr<D> make(Args&&... args) { |
| 119 | + return std::unique_ptr<D>(new D(std::forward<Args>(args)...)); |
| 120 | + } |
| 121 | +}; |
| 122 | + |
| 123 | +template <template <typename> class T, PerfCounterMode mode, class... Args> |
| 124 | +T<PerfCounter> makePerfCounterBase(PerfCounterType counterType, |
| 125 | + Args&&... args) { |
| 126 | + return PerfCounterMaker<T, typename CounterModeToType<mode>::type, |
| 127 | + PerfCounterType, Args...>::make(std::forward<PerfCounterType>(counterType), |
| 128 | + std::forward<Args>(args)...); |
| 129 | +} |
| 130 | + |
| 131 | +template <PerfCounterMode mode, class... Args> |
| 132 | +std::shared_ptr<PerfCounter> makeSharedPerfCounter( |
| 133 | + PerfCounterType counterType, Args&&... args) { |
| 134 | + return makePerfCounterBase<std::shared_ptr, mode, Args...>( |
| 135 | + counterType, std::forward<Args>(args)...); |
| 136 | +} |
| 137 | + |
| 138 | +template <PerfCounterMode mode, class... Args> |
| 139 | +std::unique_ptr<PerfCounter> makePerfCounter(PerfCounterType counterType, |
| 140 | + Args&&... args) { |
| 141 | + return makePerfCounterBase<unique_ptr, mode, Args...>( |
| 142 | + counterType, std::forward<Args>(args)...); |
| 143 | +} |
| 144 | + |
| 145 | +std::shared_ptr<PerfCounter> SimplePerfCounterFactory::makeSharedPerfCounter( |
| 146 | + PerfCounterMode mode, PerfCounterType c_type) { |
| 147 | + switch (mode) { |
| 148 | + case PerfCounterMode::PCM_THREAD : |
| 149 | + return utils::makeSharedPerfCounter<PerfCounterMode::PCM_THREAD>(c_type); |
| 150 | + case PerfCounterMode::PCM_PROCESS : |
| 151 | + return utils::makeSharedPerfCounter<PerfCounterMode::PCM_PROCESS>(c_type); |
| 152 | + default: |
| 153 | + assert(0); |
| 154 | + } |
| 155 | + return nullptr; |
| 156 | +} |
| 157 | + |
| 158 | +std::unique_ptr<PerfCounter> SimplePerfCounterFactory::makePerfCounter( |
| 159 | + PerfCounterMode mode, PerfCounterType c_type) { |
| 160 | + switch (mode) { |
| 161 | + case PerfCounterMode::PCM_THREAD : |
| 162 | + return utils::makePerfCounter<PerfCounterMode::PCM_THREAD>(c_type); |
| 163 | + case PerfCounterMode::PCM_PROCESS : |
| 164 | + return utils::makePerfCounter<PerfCounterMode::PCM_PROCESS>(c_type); |
| 165 | + default: |
| 166 | + assert(0); |
| 167 | + } |
| 168 | + return nullptr; |
| 169 | +} |
| 170 | + |
| 171 | +std::shared_ptr<PerfCounterFactory> |
| 172 | + PerfCounterFactory::getFactory(const std::string& factory_name) { |
| 173 | + |
| 174 | +#if FB_DYNO |
| 175 | + // this should bleed into FB code and FB libraries |
| 176 | + if (factory_name == "dyno") { |
| 177 | + return DynoPerfCounterFactory(); |
| 178 | + } |
| 179 | +#endif |
| 180 | + |
| 181 | + if (factory_name == "simple") { |
| 182 | + return std::shared_ptr<PerfCounterFactory>(new SimplePerfCounterFactory); |
| 183 | + } |
| 184 | + return nullptr; |
| 185 | +} |
| 186 | + |
| 187 | +} |
0 commit comments