Latency.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "Latency.h"
  7. #include "nsThreadUtils.h"
  8. #include "mozilla/Logging.h"
  9. #include <cmath>
  10. #include <algorithm>
  11. #include <mozilla/Services.h>
  12. #include <mozilla/StaticPtr.h>
  13. #include "nsContentUtils.h"
  14. using namespace mozilla;
  15. const char* LatencyLogIndex2Strings[] = {
  16. "Audio MediaStreamTrack",
  17. "Video MediaStreamTrack",
  18. "Cubeb",
  19. "AudioStream",
  20. "NetEQ",
  21. "AudioCapture Base",
  22. "AudioCapture Samples",
  23. "AudioTrackInsertion",
  24. "MediaPipeline Audio Insertion",
  25. "AudioTransmit",
  26. "AudioReceive",
  27. "MediaPipelineAudioPlayout",
  28. "MediaStream Create",
  29. "AudioStream Create",
  30. "AudioSendRTP",
  31. "AudioRecvRTP"
  32. };
  33. static StaticRefPtr<AsyncLatencyLogger> gAsyncLogger;
  34. LogModule*
  35. GetLatencyLog()
  36. {
  37. static LazyLogModule sLog("MediaLatency");
  38. return sLog;
  39. }
  40. class LogEvent : public Runnable
  41. {
  42. public:
  43. LogEvent(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue,
  44. TimeStamp aTimeStamp) :
  45. mIndex(aIndex),
  46. mID(aID),
  47. mValue(aValue),
  48. mTimeStamp(aTimeStamp)
  49. {}
  50. LogEvent(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue) :
  51. mIndex(aIndex),
  52. mID(aID),
  53. mValue(aValue),
  54. mTimeStamp(TimeStamp())
  55. {}
  56. ~LogEvent() {}
  57. NS_IMETHOD Run() override {
  58. AsyncLatencyLogger::Get(true)->WriteLog(mIndex, mID, mValue, mTimeStamp);
  59. return NS_OK;
  60. }
  61. protected:
  62. AsyncLatencyLogger::LatencyLogIndex mIndex;
  63. uint64_t mID;
  64. int64_t mValue;
  65. TimeStamp mTimeStamp;
  66. };
  67. void LogLatency(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
  68. {
  69. AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue);
  70. }
  71. void LogTime(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
  72. {
  73. TimeStamp now = TimeStamp::Now();
  74. AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue, now);
  75. }
  76. void LogTime(AsyncLatencyLogger::LatencyLogIndex aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
  77. {
  78. AsyncLatencyLogger::Get()->Log(aIndex, aID, aValue, aTime);
  79. }
  80. void LogTime(uint32_t aIndex, uint64_t aID, int64_t aValue)
  81. {
  82. LogTime(static_cast<AsyncLatencyLogger::LatencyLogIndex>(aIndex), aID, aValue);
  83. }
  84. void LogTime(uint32_t aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
  85. {
  86. LogTime(static_cast<AsyncLatencyLogger::LatencyLogIndex>(aIndex), aID, aValue, aTime);
  87. }
  88. void LogLatency(uint32_t aIndex, uint64_t aID, int64_t aValue)
  89. {
  90. LogLatency(static_cast<AsyncLatencyLogger::LatencyLogIndex>(aIndex), aID, aValue);
  91. }
  92. /* static */
  93. void AsyncLatencyLogger::InitializeStatics()
  94. {
  95. NS_ASSERTION(NS_IsMainThread(), "Main thread only");
  96. //Make sure that the underlying logger is allocated.
  97. GetLatencyLog();
  98. gAsyncLogger = new AsyncLatencyLogger();
  99. }
  100. /* static */
  101. void AsyncLatencyLogger::ShutdownLogger()
  102. {
  103. gAsyncLogger = nullptr;
  104. }
  105. /* static */
  106. AsyncLatencyLogger* AsyncLatencyLogger::Get(bool aStartTimer)
  107. {
  108. // Users don't generally null-check the result since we should live longer than they
  109. MOZ_ASSERT(gAsyncLogger);
  110. if (aStartTimer) {
  111. gAsyncLogger->Init();
  112. }
  113. return gAsyncLogger;
  114. }
  115. NS_IMPL_ISUPPORTS(AsyncLatencyLogger, nsIObserver)
  116. AsyncLatencyLogger::AsyncLatencyLogger()
  117. : mThread(nullptr),
  118. mMutex("AsyncLatencyLogger")
  119. {
  120. NS_ASSERTION(NS_IsMainThread(), "Main thread only");
  121. nsContentUtils::RegisterShutdownObserver(this);
  122. }
  123. AsyncLatencyLogger::~AsyncLatencyLogger()
  124. {
  125. AsyncLatencyLogger::Shutdown();
  126. }
  127. void AsyncLatencyLogger::Shutdown()
  128. {
  129. nsContentUtils::UnregisterShutdownObserver(this);
  130. MutexAutoLock lock(mMutex);
  131. if (mThread) {
  132. mThread->Shutdown();
  133. }
  134. mStart = TimeStamp(); // make sure we don't try to restart it for any reason
  135. }
  136. void AsyncLatencyLogger::Init()
  137. {
  138. MutexAutoLock lock(mMutex);
  139. if (mStart.IsNull()) {
  140. nsresult rv = NS_NewNamedThread("Latency Logger", getter_AddRefs(mThread));
  141. NS_ENSURE_SUCCESS_VOID(rv);
  142. mStart = TimeStamp::Now();
  143. }
  144. }
  145. void AsyncLatencyLogger::GetStartTime(TimeStamp &aStart)
  146. {
  147. MutexAutoLock lock(mMutex);
  148. aStart = mStart;
  149. }
  150. nsresult
  151. AsyncLatencyLogger::Observe(nsISupports* aSubject, const char* aTopic,
  152. const char16_t* aData)
  153. {
  154. MOZ_ASSERT(NS_IsMainThread());
  155. if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
  156. Shutdown();
  157. }
  158. return NS_OK;
  159. }
  160. // aID is a sub-identifier (in particular a specific MediaStramTrack)
  161. void AsyncLatencyLogger::WriteLog(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue,
  162. TimeStamp aTimeStamp)
  163. {
  164. if (aTimeStamp.IsNull()) {
  165. MOZ_LOG(GetLatencyLog(), LogLevel::Debug,
  166. ("Latency: %s,%llu,%lld,%lld",
  167. LatencyLogIndex2Strings[aIndex], aID, GetTimeStamp(), aValue));
  168. } else {
  169. MOZ_LOG(GetLatencyLog(), LogLevel::Debug,
  170. ("Latency: %s,%llu,%lld,%lld,%lld",
  171. LatencyLogIndex2Strings[aIndex], aID, GetTimeStamp(), aValue,
  172. static_cast<int64_t>((aTimeStamp - gAsyncLogger->mStart).ToMilliseconds())));
  173. }
  174. }
  175. int64_t AsyncLatencyLogger::GetTimeStamp()
  176. {
  177. TimeDuration t = TimeStamp::Now() - mStart;
  178. return t.ToMilliseconds();
  179. }
  180. void AsyncLatencyLogger::Log(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue)
  181. {
  182. TimeStamp null;
  183. Log(aIndex, aID, aValue, null);
  184. }
  185. void AsyncLatencyLogger::Log(LatencyLogIndex aIndex, uint64_t aID, int64_t aValue, TimeStamp &aTime)
  186. {
  187. if (MOZ_LOG_TEST(GetLatencyLog(), LogLevel::Debug)) {
  188. nsCOMPtr<nsIRunnable> event = new LogEvent(aIndex, aID, aValue, aTime);
  189. if (mThread) {
  190. mThread->Dispatch(event, NS_DISPATCH_NORMAL);
  191. }
  192. }
  193. }