log_shuttle.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #pragma once
  2. #include "log.h"
  3. #include "probe.h"
  4. #include <library/cpp/lwtrace/protos/lwtrace.pb.h>
  5. #include <util/system/spinlock.h>
  6. namespace NLWTrace {
  7. template <class TDepot>
  8. class TRunLogShuttleActionExecutor;
  9. ////////////////////////////////////////////////////////////////////////////////
  10. struct THostTimeCalculator {
  11. double K = 0;
  12. ui64 B = 0;
  13. THostTimeCalculator() {
  14. TInstant now = TInstant::Now();
  15. ui64 tsNow = GetCycleCount();
  16. K = 1000000000 / NHPTimer::GetClockRate();
  17. B = now.NanoSeconds() - K * tsNow;
  18. }
  19. ui64 CyclesToEpochNanoseconds(ui64 cycles) const {
  20. return K*cycles + B;
  21. }
  22. ui64 EpochNanosecondsToCycles(ui64 ns) const {
  23. return (ns - B) / K;
  24. }
  25. };
  26. inline ui64 CyclesToEpochNanoseconds(ui64 cycles) {
  27. return Singleton<THostTimeCalculator>()->CyclesToEpochNanoseconds(cycles);
  28. }
  29. inline ui64 EpochNanosecondsToCycles(ui64 ns) {
  30. return Singleton<THostTimeCalculator>()->EpochNanosecondsToCycles(ns);
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////
  33. template <class TDepot>
  34. class TLogShuttle: public IShuttle {
  35. private:
  36. using TExecutor = TRunLogShuttleActionExecutor<TDepot>;
  37. TTrackLog TrackLog;
  38. TExecutor* Executor;
  39. bool Ignore = false;
  40. size_t MaxTrackLength;
  41. TAdaptiveLock Lock;
  42. TAtomic ForkFailed = 0;
  43. public:
  44. explicit TLogShuttle(TExecutor* executor)
  45. : IShuttle(executor->GetTraceIdx(), executor->NewSpanId())
  46. , Executor(executor)
  47. , MaxTrackLength(Executor->GetAction().GetMaxTrackLength() ? Executor->GetAction().GetMaxTrackLength() : 100)
  48. {
  49. }
  50. bool DoAddProbe(TProbe* probe, const TParams& params, ui64 timestamp) override;
  51. void DoEndOfTrack() override;
  52. void DoDrop() override;
  53. void DoSerialize(TShuttleTrace& msg) override;
  54. bool DoFork(TShuttlePtr& child) override;
  55. bool DoJoin(const TShuttlePtr& child) override;
  56. void SetIgnore(bool ignore);
  57. void Clear();
  58. const TTrackLog& GetTrackLog() const {
  59. return TrackLog;
  60. }
  61. };
  62. ////////////////////////////////////////////////////////////////////////////////
  63. template <class TDepot>
  64. class TLogShuttleActionBase: public IExecutor {
  65. private:
  66. const ui64 TraceIdx;
  67. public:
  68. explicit TLogShuttleActionBase(ui64 traceIdx)
  69. : TraceIdx(traceIdx)
  70. {
  71. }
  72. ui64 GetTraceIdx() const {
  73. return TraceIdx;
  74. }
  75. static TLogShuttle<TDepot>* Cast(const TShuttlePtr& shuttle);
  76. static TLogShuttle<TDepot>* Cast(IShuttle* shuttle);
  77. };
  78. ////////////////////////////////////////////////////////////////////////////////
  79. template <class TDepot>
  80. class TRunLogShuttleActionExecutor: public TLogShuttleActionBase<TDepot> {
  81. private:
  82. TSpinLock Lock;
  83. TVector<TShuttlePtr> AllShuttles;
  84. TVector<TShuttlePtr> Parking;
  85. TRunLogShuttleAction Action;
  86. TDepot* Depot;
  87. TAtomic MissedTracks = 0;
  88. TAtomic* LastTrackId;
  89. TAtomic* LastSpanId;
  90. static constexpr int MaxShuttles = 100000;
  91. public:
  92. TRunLogShuttleActionExecutor(ui64 traceIdx, const TRunLogShuttleAction& action, TDepot* depot, TAtomic* lastTrackId, TAtomic* lastSpanId);
  93. ~TRunLogShuttleActionExecutor();
  94. bool DoExecute(TOrbit& orbit, const TParams& params) override;
  95. void RecordShuttle(TLogShuttle<TDepot>* shuttle);
  96. void ParkShuttle(TLogShuttle<TDepot>* shuttle);
  97. void DiscardShuttle();
  98. TShuttlePtr RentShuttle();
  99. ui64 NewSpanId();
  100. const TRunLogShuttleAction& GetAction() const {
  101. return Action;
  102. }
  103. };
  104. ////////////////////////////////////////////////////////////////////////////////
  105. template <class TDepot>
  106. class TEditLogShuttleActionExecutor: public TLogShuttleActionBase<TDepot> {
  107. private:
  108. TEditLogShuttleAction Action;
  109. public:
  110. TEditLogShuttleActionExecutor(ui64 traceIdx, const TEditLogShuttleAction& action);
  111. bool DoExecute(TOrbit& orbit, const TParams& params) override;
  112. };
  113. ////////////////////////////////////////////////////////////////////////////////
  114. template <class TDepot>
  115. class TDropLogShuttleActionExecutor: public TLogShuttleActionBase<TDepot> {
  116. private:
  117. TDropLogShuttleAction Action;
  118. public:
  119. TDropLogShuttleActionExecutor(ui64 traceIdx, const TDropLogShuttleAction& action);
  120. bool DoExecute(TOrbit& orbit, const TParams& params) override;
  121. };
  122. ////////////////////////////////////////////////////////////////////////////////
  123. template <class TDepot>
  124. bool TLogShuttle<TDepot>::DoAddProbe(TProbe* probe, const TParams& params, ui64 timestamp) {
  125. with_lock (Lock) {
  126. if (TrackLog.Items.size() >= MaxTrackLength) {
  127. TrackLog.Truncated = true;
  128. return true;
  129. }
  130. TrackLog.Items.emplace_back();
  131. TTrackLog::TItem* item = &TrackLog.Items.back();
  132. item->ThreadId = 0; // TODO[serxa]: check if it is fast to run TThread::CurrentThreadId();
  133. item->Probe = probe;
  134. if ((item->SavedParamsCount = probe->Event.Signature.ParamCount) > 0) {
  135. probe->Event.Signature.CloneParams(item->Params, params);
  136. }
  137. item->TimestampCycles = timestamp ? timestamp : GetCycleCount();
  138. }
  139. return true;
  140. }
  141. template <class TDepot>
  142. void TLogShuttle<TDepot>::DoEndOfTrack() {
  143. // Record track log if not ignored
  144. if (!Ignore) {
  145. if (AtomicGet(ForkFailed)) {
  146. Executor->DiscardShuttle();
  147. } else {
  148. Executor->RecordShuttle(this);
  149. }
  150. }
  151. Executor->ParkShuttle(this);
  152. }
  153. template <class TDepot>
  154. void TLogShuttle<TDepot>::DoDrop() {
  155. // Do not track log results of dropped shuttles
  156. Executor->ParkShuttle(this);
  157. }
  158. template <class TDepot>
  159. void TLogShuttle<TDepot>::SetIgnore(bool ignore) {
  160. Ignore = ignore;
  161. }
  162. template <class TDepot>
  163. void TLogShuttle<TDepot>::Clear() {
  164. TrackLog.Clear();
  165. AtomicSet(ForkFailed, 0);
  166. }
  167. template <class TDepot>
  168. void TLogShuttle<TDepot>::DoSerialize(TShuttleTrace& msg)
  169. {
  170. with_lock (Lock)
  171. {
  172. if (!GetTrackLog().Items.size()) {
  173. return ;
  174. }
  175. for (auto& record : GetTrackLog().Items) {
  176. auto *rec = msg.AddEvents();
  177. rec->SetName(record.Probe->Event.Name);
  178. rec->SetProvider(record.Probe->Event.GetProvider());
  179. rec->SetTimestampNanosec(
  180. CyclesToEpochNanoseconds(record.TimestampCycles));
  181. record.Probe->Event.Signature.SerializeToPb(record.Params, *rec->MutableParams());
  182. }
  183. }
  184. }
  185. template <class TDepot>
  186. TLogShuttle<TDepot>* TLogShuttleActionBase<TDepot>::Cast(const TShuttlePtr& shuttle) {
  187. return static_cast<TLogShuttle<TDepot>*>(shuttle.Get());
  188. }
  189. template <class TDepot>
  190. TLogShuttle<TDepot>* TLogShuttleActionBase<TDepot>::Cast(IShuttle* shuttle) {
  191. return static_cast<TLogShuttle<TDepot>*>(shuttle);
  192. }
  193. ////////////////////////////////////////////////////////////////////////////////
  194. template <class TDepot>
  195. TRunLogShuttleActionExecutor<TDepot>::TRunLogShuttleActionExecutor(
  196. ui64 traceIdx,
  197. const TRunLogShuttleAction& action,
  198. TDepot* depot,
  199. TAtomic* lastTrackId,
  200. TAtomic* lastSpanId)
  201. : TLogShuttleActionBase<TDepot>(traceIdx)
  202. , Action(action)
  203. , Depot(depot)
  204. , LastTrackId(lastTrackId)
  205. , LastSpanId(lastSpanId)
  206. {
  207. ui64 size = Min<ui64>(Action.GetShuttlesCount() ? Action.GetShuttlesCount() : 1000, MaxShuttles); // Do not allow to allocate too much memory
  208. AllShuttles.reserve(size);
  209. Parking.reserve(size);
  210. for (ui64 i = 0; i < size; i++) {
  211. TShuttlePtr shuttle(new TLogShuttle<TDepot>(this));
  212. AllShuttles.emplace_back(shuttle);
  213. Parking.emplace_back(shuttle);
  214. }
  215. }
  216. template <class TDepot>
  217. TRunLogShuttleActionExecutor<TDepot>::~TRunLogShuttleActionExecutor() {
  218. for (TShuttlePtr& shuttle : AllShuttles) {
  219. shuttle->Kill();
  220. }
  221. }
  222. template <class TDepot>
  223. bool TRunLogShuttleActionExecutor<TDepot>::DoExecute(TOrbit& orbit, const TParams& params) {
  224. Y_UNUSED(params);
  225. if (TShuttlePtr shuttle = RentShuttle()) {
  226. this->Cast(shuttle)->SetIgnore(Action.GetIgnore());
  227. orbit.AddShuttle(shuttle);
  228. } else {
  229. AtomicIncrement(MissedTracks);
  230. }
  231. return true;
  232. }
  233. template <class TDepot>
  234. void TRunLogShuttleActionExecutor<TDepot>::DiscardShuttle() {
  235. AtomicIncrement(MissedTracks);
  236. }
  237. template <class TDepot>
  238. void TRunLogShuttleActionExecutor<TDepot>::RecordShuttle(TLogShuttle<TDepot>* shuttle) {
  239. if (Depot == nullptr) {
  240. return;
  241. }
  242. typename TDepot::TAccessor a(*Depot);
  243. if (TTrackLog* trackLog = a.Add()) {
  244. *trackLog = shuttle->GetTrackLog();
  245. trackLog->Id = AtomicIncrement(*LastTrackId); // Track id is assigned at reporting time
  246. }
  247. }
  248. template <class TDepot>
  249. TShuttlePtr TRunLogShuttleActionExecutor<TDepot>::RentShuttle() {
  250. TGuard<TSpinLock> g(Lock);
  251. if (Parking.empty()) {
  252. return TShuttlePtr();
  253. } else {
  254. TShuttlePtr shuttle = Parking.back();
  255. Parking.pop_back();
  256. return shuttle;
  257. }
  258. }
  259. template <class TDepot>
  260. void TRunLogShuttleActionExecutor<TDepot>::ParkShuttle(TLogShuttle<TDepot>* shuttle) {
  261. shuttle->Clear();
  262. TGuard<TSpinLock> g(Lock);
  263. Parking.emplace_back(shuttle);
  264. }
  265. template <class TDepot>
  266. ui64 TRunLogShuttleActionExecutor<TDepot>::NewSpanId()
  267. {
  268. return LastSpanId ? AtomicIncrement(*LastSpanId) : 0;
  269. }
  270. ////////////////////////////////////////////////////////////////////////////////
  271. template <class TDepot>
  272. TEditLogShuttleActionExecutor<TDepot>::TEditLogShuttleActionExecutor(ui64 traceIdx, const TEditLogShuttleAction& action)
  273. : TLogShuttleActionBase<TDepot>(traceIdx)
  274. , Action(action)
  275. {
  276. }
  277. template <class TDepot>
  278. bool TEditLogShuttleActionExecutor<TDepot>::DoExecute(TOrbit& orbit, const TParams& params) {
  279. Y_UNUSED(params);
  280. bool ignore = Action.GetIgnore();
  281. orbit.ForEachShuttle(this->GetTraceIdx(), [=](IShuttle* shuttle) {
  282. this->Cast(shuttle)->SetIgnore(ignore);
  283. return true;
  284. });
  285. return true;
  286. }
  287. ////////////////////////////////////////////////////////////////////////////////
  288. template <class TDepot>
  289. TDropLogShuttleActionExecutor<TDepot>::TDropLogShuttleActionExecutor(ui64 traceIdx, const TDropLogShuttleAction& action)
  290. : TLogShuttleActionBase<TDepot>(traceIdx)
  291. , Action(action)
  292. {
  293. }
  294. template <class TDepot>
  295. bool TDropLogShuttleActionExecutor<TDepot>::DoExecute(TOrbit& orbit, const TParams& params) {
  296. Y_UNUSED(params);
  297. orbit.ForEachShuttle(this->GetTraceIdx(), [](IShuttle*) {
  298. return false; // Erase shuttle from orbit
  299. });
  300. return true;
  301. }
  302. }