123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- #pragma once
- #include <util/datetime/base.h>
- #include <util/generic/algorithm.h>
- #include <util/generic/list.h>
- #include <util/generic/map.h>
- #include <util/generic/ptr.h>
- #include <util/generic/singleton.h>
- #include <util/generic/vector.h>
- #include <util/str_stl.h>
- #include <util/stream/output.h>
- #include <util/string/util.h>
- #include <library/cpp/deprecated/atomic/atomic.h>
- #include <util/system/defaults.h>
- #include <util/system/guard.h>
- #include <util/system/sem.h>
- #include <util/system/spinlock.h>
- #include <array>
- namespace NMonitoring {
- #define BEGIN_OUTPUT_COUNTERS \
- void OutputImpl(IOutputStream& out) { \
- char prettyBuf[32];
- #define END_OUTPUT_COUNTERS \
- out.Flush(); \
- }
- #define OUTPUT_NAMED_COUNTER(var, name) out << name << ": \t" << var << NMonitoring::PrettyNum(var, prettyBuf, 32) << '\n'
- #define OUTPUT_COUNTER(var) OUTPUT_NAMED_COUNTER(var, #var);
- char* PrettyNumShort(i64 val, char* buf, size_t size);
- char* PrettyNum(i64 val, char* buf, size_t size);
- // This class is deprecated. Please consider to use
- // library/cpp/monlib/metrics instead. See more info at
- // https://wiki.yandex-team.ru/solomon/libs/monlib_cpp/
- class TDeprecatedCounter {
- public:
- using TValue = TAtomic;
- using TValueBase = TAtomicBase;
- TDeprecatedCounter()
- : Value()
- , Derivative(false)
- {
- }
- TDeprecatedCounter(TValueBase value, bool derivative = false)
- : Value(value)
- , Derivative(derivative)
- {
- }
- bool ForDerivative() const {
- return Derivative;
- }
- operator TValueBase() const {
- return AtomicGet(Value);
- }
- TValueBase Val() const {
- return AtomicGet(Value);
- }
- void Set(TValueBase val) {
- AtomicSet(Value, val);
- }
- TValueBase Inc() {
- return AtomicIncrement(Value);
- }
- TValueBase Dec() {
- return AtomicDecrement(Value);
- }
- TValueBase Add(const TValueBase val) {
- return AtomicAdd(Value, val);
- }
- TValueBase Sub(const TValueBase val) {
- return AtomicAdd(Value, -val);
- }
- // operator overloads convinient
- void operator++() {
- Inc();
- }
- void operator++(int) {
- Inc();
- }
- void operator--() {
- Dec();
- }
- void operator--(int) {
- Dec();
- }
- void operator+=(TValueBase rhs) {
- Add(rhs);
- }
- void operator-=(TValueBase rhs) {
- Sub(rhs);
- }
- TValueBase operator=(TValueBase rhs) {
- AtomicSwap(&Value, rhs);
- return rhs;
- }
- bool operator!() const {
- return AtomicGet(Value) == 0;
- }
- TAtomic& GetAtomic() {
- return Value;
- }
- private:
- TAtomic Value;
- bool Derivative;
- };
- template <typename T>
- struct TDeprecatedCountersBase {
- virtual ~TDeprecatedCountersBase() {
- }
- virtual void OutputImpl(IOutputStream&) = 0;
- static T& Instance() {
- return *Singleton<T>();
- }
- static void Output(IOutputStream& out) {
- Instance().OutputImpl(out);
- }
- };
- // This class is deprecated. Please consider to use
- // library/cpp/monlib/metrics instead. See more info at
- // https://wiki.yandex-team.ru/solomon/libs/monlib_cpp/
- //
- // Groups of G counters, defined by T type.
- // Less(a,b) returns true, if a < b.
- // It's threadsafe.
- template <typename T, typename G, typename TL = TLess<T>>
- class TDeprecatedCounterGroups {
- public:
- typedef TMap<T, G*> TGroups;
- typedef TVector<T> TGroupsNames;
- typedef THolder<TGroupsNames> TGroupsNamesPtr;
- private:
- class TCollection {
- struct TElement {
- T* Name;
- G* Counters;
- public:
- static bool Compare(const TElement& a, const TElement& b) {
- return Less(*(a.Name), *(b.Name));
- }
- }; // TElement
- private:
- TArrayHolder<TElement> Elements;
- size_t Size;
- public:
- TCollection()
- : Size(0)
- {
- }
- TCollection(const TCollection& collection)
- : Elements(new TElement[collection.Size])
- , Size(collection.Size)
- {
- for (int i = 0; i < Size; ++i) {
- Elements[i] = collection.Elements[i];
- }
- }
- TCollection(const TCollection& collection, T* name, G* counters)
- : Elements(new TElement[collection.Size + 1])
- , Size(collection.Size + 1)
- {
- for (size_t i = 0; i < Size - 1; ++i) {
- Elements[i] = collection.Elements[i];
- }
- Elements[Size - 1].Name = name;
- Elements[Size - 1].Counters = counters;
- for (size_t i = 1; i < Size; ++i) {
- size_t j = i;
- while (j > 0 &&
- TElement::Compare(Elements[j], Elements[j - 1])) {
- std::swap(Elements[j], Elements[j - 1]);
- --j;
- }
- }
- }
- G* Find(const T& name) const {
- G* result = nullptr;
- if (Size == 0) {
- return nullptr;
- }
- size_t l = 0;
- size_t r = Size - 1;
- while (l < r) {
- size_t m = (l + r) / 2;
- if (Less(*(Elements[m].Name), name)) {
- l = m + 1;
- } else {
- r = m;
- }
- }
- if (!Less(*(Elements[l].Name), name) && !Less(name, *(Elements[l].Name))) {
- result = Elements[l].Counters;
- }
- return result;
- }
- void Free() {
- for (size_t i = 0; i < Size; ++i) {
- T* name = Elements[i].Name;
- G* counters = Elements[i].Counters;
- Elements[i].Name = nullptr;
- Elements[i].Counters = nullptr;
- delete name;
- delete counters;
- }
- Size = 0;
- }
- TGroupsNamesPtr GetNames() const {
- TGroupsNamesPtr result(new TGroupsNames());
- for (size_t i = 0; i < Size; ++i) {
- result->push_back(*(Elements[i].Name));
- }
- return result;
- }
- }; // TCollection
- struct TOldGroup {
- TCollection* Collection;
- ui64 Time;
- };
- private:
- TCollection* Groups;
- TList<TOldGroup> OldGroups;
- TSpinLock AddMutex;
- ui64 Timeout;
- static TL Less;
- private:
- G* Add(const T& name) {
- TGuard<TSpinLock> guard(AddMutex);
- G* result = Groups->Find(name);
- if (result == nullptr) {
- T* newName = new T(name);
- G* newCounters = new G();
- TCollection* newGroups =
- new TCollection(*Groups, newName, newCounters);
- ui64 now = ::Now().MicroSeconds();
- TOldGroup group;
- group.Collection = Groups;
- group.Time = now;
- OldGroups.push_back(group);
- for (ui32 i = 0; i < 5; ++i) {
- if (OldGroups.front().Time + Timeout < now) {
- delete OldGroups.front().Collection;
- OldGroups.front().Collection = nullptr;
- OldGroups.pop_front();
- } else {
- break;
- }
- }
- Groups = newGroups;
- result = Groups->Find(name);
- }
- return result;
- }
- public:
- TDeprecatedCounterGroups(ui64 timeout = 5 * 1000000L) {
- Groups = new TCollection();
- Timeout = timeout;
- }
- virtual ~TDeprecatedCounterGroups() {
- TGuard<TSpinLock> guard(AddMutex);
- Groups->Free();
- delete Groups;
- Groups = nullptr;
- typename TList<TOldGroup>::iterator i;
- for (i = OldGroups.begin(); i != OldGroups.end(); ++i) {
- delete i->Collection;
- i->Collection = nullptr;
- }
- OldGroups.clear();
- }
- bool Has(const T& name) const {
- TCollection* groups = Groups;
- return groups->Find(name) != nullptr;
- }
- G* Find(const T& name) const {
- TCollection* groups = Groups;
- return groups->Find(name);
- }
- // Get group with the name, if it exists.
- // If there is no group with the name, add new group.
- G& Get(const T& name) {
- G* result = Find(name);
- if (result == nullptr) {
- result = Add(name);
- Y_ASSERT(result != nullptr);
- }
- return *result;
- }
- // Get copy of groups names array.
- TGroupsNamesPtr GetGroupsNames() const {
- TCollection* groups = Groups;
- TGroupsNamesPtr result = groups->GetNames();
- return result;
- }
- }; // TDeprecatedCounterGroups
- template <typename T, typename G, typename TL>
- TL TDeprecatedCounterGroups<T, G, TL>::Less;
- }
- static inline IOutputStream& operator<<(IOutputStream& o, const NMonitoring::TDeprecatedCounter& rhs) {
- return o << rhs.Val();
- }
- template <size_t N>
- static inline IOutputStream& operator<<(IOutputStream& o, const std::array<NMonitoring::TDeprecatedCounter, N>& rhs) {
- for (typename std::array<NMonitoring::TDeprecatedCounter, N>::const_iterator it = rhs.begin(); it != rhs.end(); ++it) {
- if (!!*it)
- o << *it << Endl;
- }
- return o;
- }
|