aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unistat/unistat.cpp
blob: 21bf49ff012a09d87d5cf8cdf9bbc8b63b7af421 (plain) (blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
201
202
203
204
205
206
207
208
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "unistat.h"
#include <util/generic/strbuf.h>

using namespace NUnistat;

TIntervalsBuilder& TIntervalsBuilder::Append(double value) {
    Y_ENSURE(Intervals_.empty() || Intervals_.back() < value);
    Intervals_.push_back(value);
    return *this;
}

TIntervalsBuilder& TIntervalsBuilder::AppendFlat(size_t count, double step) {
    Y_ENSURE(Intervals_.size() > 0 && step > 0);
    double x = Intervals_.back();
    for (size_t i = 0; i < count; ++i) {
        x += step;
        Intervals_.push_back(x);
    }
    return *this;
}

TIntervalsBuilder& TIntervalsBuilder::AppendExp(size_t count, double base) {
    Y_ENSURE(Intervals_.size() > 0 && base > 1);
    double x = Intervals_.back();
    for (size_t i = 0; i < count; ++i) {
        x *= base;
        Intervals_.push_back(x);
    }
    return *this;
}

TIntervalsBuilder& TIntervalsBuilder::FlatRange(size_t count, double start, double stop) {
    Y_ENSURE(count > 0 && start < stop);
    Append(start);
    AppendFlat(count - 1, (stop - start) / count);
    return *this;
}

TIntervalsBuilder& TIntervalsBuilder::ExpRange(size_t count, double start, double stop) {
    Y_ENSURE(count > 0 && start < stop);
    Append(start);
    AppendExp(count - 1, std::pow(stop / start, 1. / count));
    return *this;
}

TIntervals TIntervalsBuilder::Build() {
    Y_ENSURE(Intervals_.size() <= MAX_HISTOGRAM_SIZE);
    Y_ENSURE(IsSorted(Intervals_.begin(), Intervals_.end()));
    return std::move(Intervals_);
}

IHolePtr TUnistat::DrillFloatHole(const TString& name, const TString& suffix, TPriority priority, TStartValue startValue, EAggregationType type, bool alwaysVisible) {
    return DrillFloatHole(name, "", suffix, priority, startValue, type, alwaysVisible);
}

IHolePtr TUnistat::DrillFloatHole(const TString& name, const TString& description, const TString& suffix, TPriority priority, TStartValue startValue, EAggregationType type, bool alwaysVisible) {
    {
        TReadGuard guard(Mutex);
        IHolePtr* exhole = Holes.FindPtr(name);
        if (exhole) {
            return *exhole;
        }
    }
    {
        TWriteGuard guard(Mutex);
        IHolePtr* exhole = Holes.FindPtr(name);
        if (exhole) {
            return *exhole;
        }
        IHolePtr hole = new TFloatHole(name, description, suffix, type, priority.Priority, startValue.StartValue, alwaysVisible);
        for (const auto& tag : GlobalTags) {
            hole->AddTag(tag.first, tag.second);
        }
        Holes[name] = hole;
        HolesByPriorityAndTags.insert(hole.Get());
        return hole;
    }
}

IHolePtr TUnistat::DrillHistogramHole(const TString& name, const TString& suffix, TPriority priority, const NUnistat::TIntervals& intervals, EAggregationType type, bool alwaysVisible) {
    return DrillHistogramHole(name, "", suffix, priority, intervals, type, alwaysVisible);
}

IHolePtr TUnistat::DrillHistogramHole(const TString& name, const TString& description, const TString& suffix, TPriority priority, const NUnistat::TIntervals& intervals, EAggregationType type, bool alwaysVisible) {
    {
        TReadGuard guard(Mutex);
        IHolePtr* exhole = Holes.FindPtr(name);
        if (exhole) {
            return *exhole;
        }
    }
    {
        TWriteGuard guard(Mutex);
        IHolePtr* exhole = Holes.FindPtr(name);
        if (exhole) {
            return *exhole;
        }
        IHolePtr hole = new THistogramHole(name, description, suffix, type, priority.Priority, intervals, alwaysVisible);
        for (const auto& tag : GlobalTags) {
            hole->AddTag(tag.first, tag.second);
        }
        Holes[name] = hole;
        HolesByPriorityAndTags.insert(hole.Get());
        return hole;
    }
}

void TUnistat::AddGlobalTag(const TString& tagName, const TString& tagValue) {
    TWriteGuard guard(Mutex);
    GlobalTags[tagName] = tagValue;
    for (IHole* hole : HolesByPriorityAndTags) {
        hole->AddTag(tagName, tagValue);
    }
}

bool TUnistat::PushSignalUnsafeImpl(const TStringBuf holename, double signal) {
    IHolePtr* hole = Holes.FindPtr(holename);
    if (!hole) {
        return false;
    }

    (*hole)->PushSignal(signal);
    return true;
}

TMaybe<TInstanceStats::TMetric> TUnistat::GetSignalValueUnsafeImpl(const TStringBuf holename) {
    IHolePtr* hole = Holes.FindPtr(holename);
    if (!hole) {
        return Nothing();
    }

    TInstanceStats stat;
    (*hole)->ExportToProto(stat);
    if (!stat.GetMetric().size()) {
        return Nothing();
    }
    return std::move(*stat.MutableMetric(0));
}

bool TUnistat::ResetSignalUnsafeImpl(const TStringBuf holename) {
    IHolePtr* hole = Holes.FindPtr(holename);
    if (!hole) {
        return false;
    }

    (*hole)->ResetSignal();
    return true;
}

TString TUnistat::CreateInfoDump(int level) const {
    TReadGuard guard(Mutex);
    NJsonWriter::TBuf json;
    json.BeginObject();
    for (IHole* hole : HolesByPriorityAndTags) {
        if (hole->GetPriority() < level) {
            break;
        }
        hole->PrintInfo(json);
    }
    json.EndObject();
    return json.Str();
}

TString TUnistat::CreateJsonDump(int level, bool allHoles) const {
    TReadGuard guard(Mutex);
    NJsonWriter::TBuf json;
    json.BeginList();
    bool filterZeroHoles = !allHoles && level >= 0;
    for (IHole* hole : HolesByPriorityAndTags) {
        if (hole->GetPriority() < level) {
            break;
        }
        hole->PrintValue(json, filterZeroHoles);
    }
    json.EndList();
    return json.Str();
}

TString TUnistat::CreatePushDump(int level, const NUnistat::TTags& tags, ui32 ttl, bool allHoles) const {
    TReadGuard guard(Mutex);
    NJsonWriter::TBuf json;
    json.BeginList();
    bool filterZeroHoles = !allHoles && level >= 0;
    int prevPriority = Max<int>();
    TString prevTagsStr;
    for (IHole* hole : HolesByPriorityAndTags) {
        int priority = hole->GetPriority();
        TString tagStr = hole->GetTagsStr();
        if (priority < level) {
            break;
        }
        if (prevPriority != priority || prevTagsStr != tagStr) {
            if (prevPriority != Max<int>()) {
                json.EndList();
                json.EndObject();
            }
            json.BeginObject();
            if (ttl) {
                json.WriteKey("ttl").WriteULongLong(ttl);
            }
            json.WriteKey("tags").BeginObject()
                .WriteKey("_priority").WriteInt(hole->GetPriority());
            for (const auto& tag : tags) {
                json.WriteKey(tag.first);
                json.WriteString(tag.second);
            }
            for (const auto& tag : hole->GetTags()) {
                json.WriteKey(tag.first);
                json.WriteString(tag.second);
            }
            json.EndObject();
            json.WriteKey("values").BeginList();
            prevPriority = priority;
            prevTagsStr = tagStr;
        }
        hole->PrintToPush(json, filterZeroHoles);
    }
    if (prevPriority != Max<int>()) {
        json.EndList();
        json.EndObject();
    }
    json.EndList();
    return json.Str();
}

void TUnistat::ExportToProto(TInstanceStats& stats, int level) const {
    TReadGuard guard(Mutex);

    for (IHole* hole : HolesByPriorityAndTags) {
        if (hole->GetPriority() < level) {
            break;
        }
        hole->ExportToProto(stats);
    }
}

TString TUnistat::GetSignalDescriptions() const {
    TReadGuard guard(Mutex);
    NJsonWriter::TBuf json;
    json.BeginObject();

    for (IHole* hole : HolesByPriorityAndTags) {
        json.WriteKey(hole->GetName());
        json.WriteString(hole->GetDescription());
    }

    json.EndObject();
    return json.Str();
}

TVector<TString> TUnistat::GetHolenames() const {
    TReadGuard guard(Mutex);

    TVector<TString> holenames;
    for (const auto& [holeName, holeData] : Holes) {
        holenames.emplace_back(holeName);
    }

    return holenames;
}

bool TUnistat::EraseHole(const TString& name) {
    TWriteGuard guard(Mutex);
    auto holeIterator = Holes.find(name);
    if (holeIterator == Holes.end()) {
        return false;
    }

    HolesByPriorityAndTags.erase(holeIterator->second.Get());
    Holes.erase(holeIterator);
    return true;
}

void TFloatHole::PrintInfo(NJsonWriter::TBuf& json) const {
    TValue value;
    value.Atomic = AtomicGet(Value.Atomic);
    json.WriteKey(Name)
        .BeginObject()
        .WriteKey(TStringBuf("Priority"))
        .WriteInt(Priority)
        .WriteKey(TStringBuf("Value"))
        .WriteDouble(value.Value)
        .WriteKey(TStringBuf("Type"))
        .WriteString(ToString(Type))
        .WriteKey(TStringBuf("Suffix"))
        .WriteString(Suffix)
        .WriteKey(TStringBuf("Tags"))
        .WriteString(GetTagsStr())
        .EndObject();
}

void TFloatHole::PrintValue(NJsonWriter::TBuf& json, bool check) const {
    if (check && !AtomicGet(Pushed)) {
        return;
    }

    TValue value;
    value.Atomic = AtomicGet(Value.Atomic);
    json.BeginList()
        .WriteString(TString::Join(GetTagsStr(), Name, TStringBuf("_"), Suffix))
        .WriteDouble(value.Value)
        .EndList();
}

void TFloatHole::PrintToPush(NJsonWriter::TBuf& json, bool check) const {
    if (check && !AtomicGet(Pushed)) {
        return;
    }

    TValue value;
    value.Atomic = AtomicGet(Value.Atomic);
    json.BeginObject()
        .WriteKey("name").WriteString(TString::Join(Name, TStringBuf("_"), Suffix))
        .WriteKey("val").WriteDouble(value.Value)
        .EndObject();
}


void TFloatHole::ExportToProto(TInstanceStats& stats) const {
    if (!AtomicGet(Pushed)) {
        return;
    }

    TValue value;
    value.Atomic = AtomicGet(Value.Atomic);
    TInstanceStats::TMetric* metric = stats.AddMetric();
    metric->SetName(TString::Join(GetTagsStr(), Name, TStringBuf("_"), Suffix));
    metric->SetNumber(value.Value);
}

void TFloatHole::PushSignal(double signal) {
    AtomicSet(Pushed, 1);
    TValue old, toset;
    do {
        old.Atomic = AtomicGet(Value.Atomic);
        switch (Type) {
            case EAggregationType::Max:
                toset.Value = Max(signal, old.Value);
                break;
            case EAggregationType::Min:
                toset.Value = Min(signal, old.Value);
                break;
            case EAggregationType::Sum:
                toset.Value = signal + old.Value;
                break;
            case EAggregationType::LastValue:
                toset.Value = signal;
                break;
            default:
                assert(0);
        }
    } while (!AtomicCas(&Value.Atomic, toset.Atomic, old.Atomic));
}

void TFloatHole::ResetSignal() {
    AtomicSet(Value.Atomic, 0);
    AtomicSet(Pushed, 0);
}

void THistogramHole::PrintInfo(NJsonWriter::TBuf& json) const {
    json.WriteKey(Name)
        .BeginObject()
        .WriteKey(TStringBuf("Priority"))
        .WriteInt(Priority)
        .WriteKey(TStringBuf("Value"));

    PrintWeights(json);

    json.WriteKey(TStringBuf("Type"))
        .WriteString(ToString(Type));
    json.WriteKey(TStringBuf("Suffix"))
        .WriteString(Suffix);
    json.WriteKey(TStringBuf("Tags"))
        .WriteString(GetTagsStr())
        .EndObject();
}

void THistogramHole::PrintValue(NJsonWriter::TBuf& json, bool check) const {
    if (check && !AtomicGet(Pushed)) {
        return;
    }

    json.BeginList()
        .WriteString(TString::Join(GetTagsStr(), Name, TStringBuf("_"), Suffix));

    PrintWeights(json);

    json.EndList();
}

void THistogramHole::PrintToPush(NJsonWriter::TBuf& json, bool check) const {
    if (check && !AtomicGet(Pushed)) {
        return;
    }

    json.BeginObject()
        .WriteKey("name").WriteString(TString::Join(Name, TStringBuf("_"), Suffix))
        .WriteKey("val");
    PrintWeights(json);
    json.EndObject();
}

void THistogramHole::PrintWeights(NJsonWriter::TBuf& json) const {
    json.BeginList();
    for (size_t i = 0, size = Weights.size(); i < size; ++i) {
        json.BeginList()
            .WriteDouble(Intervals[i])
            .WriteLongLong(AtomicGet(Weights[i]))
            .EndList();
    }
    json.EndList();
}

void THistogramHole::ExportToProto(TInstanceStats& stats) const {
    if (!AtomicGet(Pushed)) {
        return;
    }

    TInstanceStats::TMetric* metric = stats.AddMetric();
    metric->SetName(TString::Join(GetTagsStr(), Name, TStringBuf("_"), Suffix));

    TInstanceStats::THistogram* hgram = metric->MutableHgram();
    for (size_t i = 0, size = Weights.size(); i < size; ++i) {
        TInstanceStats::THistogram::TBucket* bucket = hgram->AddBucket();
        bucket->SetBoundary(Intervals[i]);
        bucket->SetWeight(AtomicGet(Weights[i]));
    }
}

void THistogramHole::PushSignal(double signal) {
    AtomicSet(Pushed, 1);

    const size_t i = UpperBound(Intervals.cbegin(), Intervals.cend(), signal) - Intervals.cbegin(); // Intervals[i - 1] <= signal < Intervals[i]
    if (i > 0) {
        AtomicIncrement(Weights[i - 1]);
    }
}

void THistogramHole::ResetSignal() {
    for (size_t i = 0; i < Intervals.size(); ++i) {
        SetWeight(i, 0);
    }
    AtomicSet(Pushed, 0);
}

void THistogramHole::SetWeight(ui32 index, TAtomicBase value) {
    AtomicSet(Weights.at(index), value);
}

void TUnistat::Reset() {
    TWriteGuard guard(Mutex);
    Holes.clear();
    HolesByPriorityAndTags.clear();
    GlobalTags.clear();
}

void TUnistat::ResetSignals() {
    for (auto& hole : Holes) {
        hole.second->ResetSignal();
    }
}