aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/log_shuttle.h
blob: d3d5fe0bdee5fa6d57f38662ee4b46f6499851ea (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
#pragma once

#include "log.h"
#include "probe.h"

#include <library/cpp/lwtrace/protos/lwtrace.pb.h> 
 
#include <util/system/spinlock.h>

namespace NLWTrace {
    template <class TDepot>
    class TRunLogShuttleActionExecutor;

    ////////////////////////////////////////////////////////////////////////////////

    struct THostTimeCalculator {
        double K = 0; 
        ui64 B = 0; 
 
        THostTimeCalculator() {
            TInstant now = TInstant::Now(); 
            ui64 tsNow = GetCycleCount(); 
            K = 1000000000 / NHPTimer::GetClockRate(); 
            B = now.NanoSeconds() - K * tsNow; 
        } 
 
        ui64 CyclesToEpochNanoseconds(ui64 cycles) const {
            return K*cycles + B; 
        } 
 
        ui64 EpochNanosecondsToCycles(ui64 ns) const {
            return (ns - B) / K; 
        } 
    }; 
 
    inline ui64 CyclesToEpochNanoseconds(ui64 cycles) {
        return Singleton<THostTimeCalculator>()->CyclesToEpochNanoseconds(cycles); 
    } 
 
    inline ui64 EpochNanosecondsToCycles(ui64 ns) {
        return Singleton<THostTimeCalculator>()->EpochNanosecondsToCycles(ns); 
    } 
 
    //////////////////////////////////////////////////////////////////////////////// 
 
    template <class TDepot>
    class TLogShuttle: public IShuttle {
    private:
        using TExecutor = TRunLogShuttleActionExecutor<TDepot>;
        TTrackLog TrackLog;
        TExecutor* Executor;
        bool Ignore = false;
        size_t MaxTrackLength;
        TAdaptiveLock Lock;
        TAtomic ForkFailed = 0;

    public:
        explicit TLogShuttle(TExecutor* executor)
            : IShuttle(executor->GetTraceIdx(), executor->NewSpanId())
            , Executor(executor)
            , MaxTrackLength(Executor->GetAction().GetMaxTrackLength() ? Executor->GetAction().GetMaxTrackLength() : 100)
        {
        }

        bool DoAddProbe(TProbe* probe, const TParams& params, ui64 timestamp) override; 
        void DoEndOfTrack() override;
        void DoDrop() override;
        void DoSerialize(TShuttleTrace& msg) override; 
        bool DoFork(TShuttlePtr& child) override;
        bool DoJoin(const TShuttlePtr& child) override;

        void SetIgnore(bool ignore);
        void Clear();

        const TTrackLog& GetTrackLog() const {
            return TrackLog;
        }
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    class TLogShuttleActionBase: public IExecutor {
    private:
        const ui64 TraceIdx;

    public:
        explicit TLogShuttleActionBase(ui64 traceIdx)
            : TraceIdx(traceIdx)
        {
        }
        ui64 GetTraceIdx() const {
            return TraceIdx;
        }

        static TLogShuttle<TDepot>* Cast(const TShuttlePtr& shuttle);
        static TLogShuttle<TDepot>* Cast(IShuttle* shuttle);
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    class TRunLogShuttleActionExecutor: public TLogShuttleActionBase<TDepot> {
    private:
        TSpinLock Lock;
        TVector<TShuttlePtr> AllShuttles;
        TVector<TShuttlePtr> Parking;
        TRunLogShuttleAction Action;
        TDepot* Depot;

        TAtomic MissedTracks = 0;
        TAtomic* LastTrackId;
        TAtomic* LastSpanId;

        static constexpr int MaxShuttles = 100000;

    public:
        TRunLogShuttleActionExecutor(ui64 traceIdx, const TRunLogShuttleAction& action, TDepot* depot, TAtomic* lastTrackId, TAtomic* lastSpanId);
        ~TRunLogShuttleActionExecutor();
        bool DoExecute(TOrbit& orbit, const TParams& params) override;
        void RecordShuttle(TLogShuttle<TDepot>* shuttle);
        void ParkShuttle(TLogShuttle<TDepot>* shuttle);
        void DiscardShuttle();
        TShuttlePtr RentShuttle();
        ui64 NewSpanId();
        const TRunLogShuttleAction& GetAction() const {
            return Action;
        }
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    class TEditLogShuttleActionExecutor: public TLogShuttleActionBase<TDepot> {
    private:
        TEditLogShuttleAction Action;

    public:
        TEditLogShuttleActionExecutor(ui64 traceIdx, const TEditLogShuttleAction& action);
        bool DoExecute(TOrbit& orbit, const TParams& params) override;
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    class TDropLogShuttleActionExecutor: public TLogShuttleActionBase<TDepot> {
    private:
        TDropLogShuttleAction Action;

    public:
        TDropLogShuttleActionExecutor(ui64 traceIdx, const TDropLogShuttleAction& action);
        bool DoExecute(TOrbit& orbit, const TParams& params) override;
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    bool TLogShuttle<TDepot>::DoAddProbe(TProbe* probe, const TParams& params, ui64 timestamp) { 
        with_lock (Lock) {
            if (TrackLog.Items.size() >= MaxTrackLength) {
                TrackLog.Truncated = true;
                return true;
            }
            TrackLog.Items.emplace_back();
            TTrackLog::TItem* item = &TrackLog.Items.back();
            item->ThreadId = 0; // TODO[serxa]: check if it is fast to run TThread::CurrentThreadId();
            item->Probe = probe;
            if ((item->SavedParamsCount = probe->Event.Signature.ParamCount) > 0) {
                probe->Event.Signature.CloneParams(item->Params, params);
            }
            item->TimestampCycles = timestamp ? timestamp : GetCycleCount(); 
        }

        return true;
    }

    template <class TDepot>
    void TLogShuttle<TDepot>::DoEndOfTrack() {
        // Record track log if not ignored
        if (!Ignore) {
            if (AtomicGet(ForkFailed)) {
                Executor->DiscardShuttle();
            } else {
                Executor->RecordShuttle(this);
            }
        }
        Executor->ParkShuttle(this);
    }

    template <class TDepot>
    void TLogShuttle<TDepot>::DoDrop() {
        // Do not track log results of dropped shuttles
        Executor->ParkShuttle(this);
    }

    template <class TDepot>
    void TLogShuttle<TDepot>::SetIgnore(bool ignore) {
        Ignore = ignore;
    }

    template <class TDepot>
    void TLogShuttle<TDepot>::Clear() {
        TrackLog.Clear();
    }

    template <class TDepot>
    void TLogShuttle<TDepot>::DoSerialize(TShuttleTrace& msg) 
    { 
        with_lock (Lock) 
        { 
            if (!GetTrackLog().Items.size()) { 
                return ; 
            } 
            for (auto& record : GetTrackLog().Items) { 
                auto *rec = msg.AddEvents(); 
                rec->SetName(record.Probe->Event.Name); 
                rec->SetProvider(record.Probe->Event.GetProvider()); 
                rec->SetTimestampNanosec( 
                    CyclesToEpochNanoseconds(record.TimestampCycles)); 
                record.Probe->Event.Signature.SerializeToPb(record.Params, *rec->MutableParams()); 
            } 
        } 
    } 
 
    template <class TDepot> 
    TLogShuttle<TDepot>* TLogShuttleActionBase<TDepot>::Cast(const TShuttlePtr& shuttle) {
        return static_cast<TLogShuttle<TDepot>*>(shuttle.Get());
    }

    template <class TDepot>
    TLogShuttle<TDepot>* TLogShuttleActionBase<TDepot>::Cast(IShuttle* shuttle) {
        return static_cast<TLogShuttle<TDepot>*>(shuttle);
    }

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    TRunLogShuttleActionExecutor<TDepot>::TRunLogShuttleActionExecutor(
        ui64 traceIdx,
        const TRunLogShuttleAction& action,
        TDepot* depot,
        TAtomic* lastTrackId,
        TAtomic* lastSpanId)
        : TLogShuttleActionBase<TDepot>(traceIdx)
        , Action(action)
        , Depot(depot)
        , LastTrackId(lastTrackId)
        , LastSpanId(lastSpanId)
    {
        ui64 size = Min<ui64>(Action.GetShuttlesCount() ? Action.GetShuttlesCount() : 1000, MaxShuttles); // Do not allow to allocate too much memory 
        AllShuttles.reserve(size);
        Parking.reserve(size);
        for (ui64 i = 0; i < size; i++) {
            TShuttlePtr shuttle(new TLogShuttle<TDepot>(this));
            AllShuttles.emplace_back(shuttle);
            Parking.emplace_back(shuttle);
        }
    }

    template <class TDepot>
    TRunLogShuttleActionExecutor<TDepot>::~TRunLogShuttleActionExecutor() {
        for (TShuttlePtr& shuttle : AllShuttles) {
            shuttle->Kill();
        }
    }

    template <class TDepot>
    bool TRunLogShuttleActionExecutor<TDepot>::DoExecute(TOrbit& orbit, const TParams& params) {
        Y_UNUSED(params);
        if (TShuttlePtr shuttle = RentShuttle()) {
            this->Cast(shuttle)->SetIgnore(Action.GetIgnore());
            orbit.AddShuttle(shuttle);
        } else {
            AtomicIncrement(MissedTracks);
        }
        return true;
    }

    template <class TDepot>
    void TRunLogShuttleActionExecutor<TDepot>::DiscardShuttle() {
        AtomicIncrement(MissedTracks);
    }

    template <class TDepot>
    void TRunLogShuttleActionExecutor<TDepot>::RecordShuttle(TLogShuttle<TDepot>* shuttle) {
        if (Depot == nullptr) { 
            return; 
        } 
        typename TDepot::TAccessor a(*Depot);
        if (TTrackLog* trackLog = a.Add()) {
            *trackLog = shuttle->GetTrackLog();
            trackLog->Id = AtomicIncrement(*LastTrackId); // Track id is assigned at reporting time
        }
    }

    template <class TDepot>
    TShuttlePtr TRunLogShuttleActionExecutor<TDepot>::RentShuttle() {
        TGuard<TSpinLock> g(Lock);
        if (Parking.empty()) {
            return TShuttlePtr();
        } else {
            TShuttlePtr shuttle = Parking.back();
            Parking.pop_back();
            return shuttle;
        }
    }

    template <class TDepot>
    void TRunLogShuttleActionExecutor<TDepot>::ParkShuttle(TLogShuttle<TDepot>* shuttle) {
        shuttle->Clear();
        TGuard<TSpinLock> g(Lock);
        Parking.emplace_back(shuttle);
    }

    template <class TDepot>
    ui64 TRunLogShuttleActionExecutor<TDepot>::NewSpanId()
    {
        return LastSpanId ? AtomicIncrement(*LastSpanId) : 0;
    }

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    TEditLogShuttleActionExecutor<TDepot>::TEditLogShuttleActionExecutor(ui64 traceIdx, const TEditLogShuttleAction& action)
        : TLogShuttleActionBase<TDepot>(traceIdx)
        , Action(action)
    {
    }

    template <class TDepot>
    bool TEditLogShuttleActionExecutor<TDepot>::DoExecute(TOrbit& orbit, const TParams& params) {
        Y_UNUSED(params);
        bool ignore = Action.GetIgnore();
        orbit.ForEachShuttle(this->GetTraceIdx(), [=](IShuttle* shuttle) {
            this->Cast(shuttle)->SetIgnore(ignore);
            return true;
        });
        return true;
    }

    ////////////////////////////////////////////////////////////////////////////////

    template <class TDepot>
    TDropLogShuttleActionExecutor<TDepot>::TDropLogShuttleActionExecutor(ui64 traceIdx, const TDropLogShuttleAction& action)
        : TLogShuttleActionBase<TDepot>(traceIdx)
        , Action(action)
    {
    }

    template <class TDepot>
    bool TDropLogShuttleActionExecutor<TDepot>::DoExecute(TOrbit& orbit, const TParams& params) {
        Y_UNUSED(params);
        orbit.ForEachShuttle(this->GetTraceIdx(), [](IShuttle*) {
            return false; // Erase shuttle from orbit
        });
        return true;
    }

}