user_job_statistics.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include <util/stream/file.h>
  3. #include <util/generic/hash.h>
  4. #include <util/datetime/cputimer.h>
  5. namespace NYtTools {
  6. class TTimeStat;
  7. using TTimeStatHolder = THolder<TTimeStat>;
  8. class TUserJobStatsProxy {
  9. public:
  10. static const FHANDLE JobStatisticsHandle;
  11. private:
  12. THolder<IOutputStream> FetchedOut;
  13. IOutputStream* UsingStream = &Cerr;
  14. public:
  15. // TODO: add inheritance
  16. THashMap<TString, i64> Stats;//will be dumped in CommitStats or desctructor
  17. THashMap<TString, TDuration> TimeStats;//will be dumped in CommitStats or desctructor
  18. TUserJobStatsProxy() { Init(nullptr); }
  19. ~TUserJobStatsProxy() {
  20. CommitStats();
  21. }
  22. TUserJobStatsProxy (IOutputStream* usingStream) {Init(usingStream);}
  23. void Init(IOutputStream* usingStream);
  24. void InitChecked(IOutputStream* ifNotInJob);
  25. void InitIfNotInited(IOutputStream* usingStream);
  26. IOutputStream* GetStream() const { return UsingStream; }
  27. void CommitStats();
  28. void WriteStat(TString name, i64 val); //immidiatly wirtes stat
  29. void WriteStatNoFlush(TString name, i64 val); //immidiatly wirtes stat but do not flush it
  30. //@param name name of statistic to be written in millisecs from creation to destruction
  31. //@param commitOnFinish if false: will update state/write on job finish; if true: write stat in destructor
  32. TTimeStatHolder TimerStart(TString name, bool commitOnFinish = false);
  33. };
  34. class TTimeStat {
  35. TUserJobStatsProxy* Parent;
  36. TString Name;
  37. bool Commit;
  38. TTimeStat(TUserJobStatsProxy* parent, TString name, bool commit);
  39. friend class TUserJobStatsProxy;
  40. TSimpleTimer Timer;
  41. public:
  42. ~TTimeStat();
  43. TDuration Get() const {
  44. return Timer.Get();
  45. }
  46. void Cancel();
  47. void Finish();
  48. };
  49. }