aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/user_job_statistics/user_job_statistics.h
blob: 6939d20417b9eed5ca5d7c3a94b5ea8d3b43d165 (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
#pragma once

#include <util/stream/file.h>
#include <util/generic/hash.h>
#include <util/datetime/cputimer.h>

namespace NYtTools {
    class TTimeStat;
    using TTimeStatHolder = THolder<TTimeStat>;

    class TUserJobStatsProxy {
    public:
        static const FHANDLE JobStatisticsHandle;
    private:
        THolder<IOutputStream> FetchedOut;
        IOutputStream* UsingStream = &Cerr;
    public:
        // TODO: add inheritance
        THashMap<TString, i64> Stats;//will be dumped in CommitStats or desctructor
        THashMap<TString, TDuration> TimeStats;//will be dumped in CommitStats or desctructor

        TUserJobStatsProxy() { Init(nullptr); }
        ~TUserJobStatsProxy() {
            CommitStats();
        }
        TUserJobStatsProxy (IOutputStream* usingStream) {Init(usingStream);}

        void Init(IOutputStream* usingStream);
        void InitChecked(IOutputStream* ifNotInJob);
        void InitIfNotInited(IOutputStream* usingStream);
        IOutputStream* GetStream() const { return UsingStream; }
        void CommitStats();
        void WriteStat(TString name, i64 val); //immidiatly wirtes stat
        void WriteStatNoFlush(TString name, i64 val); //immidiatly wirtes stat but do not flush it

        //@param name                name of statistic to be written in millisecs from creation to destruction
        //@param commitOnFinish      if false: will update state/write on job finish; if true: write stat in destructor
        TTimeStatHolder TimerStart(TString name, bool commitOnFinish = false);
    };

    class TTimeStat {
        TUserJobStatsProxy* Parent;
        TString Name;
        bool Commit;

        TTimeStat(TUserJobStatsProxy* parent, TString name, bool commit);
        friend class TUserJobStatsProxy;

        TSimpleTimer Timer;
    public:
        ~TTimeStat();
        TDuration Get() const {
            return Timer.Get();
        }
        void Cancel();
        void Finish();
    };
}