MemoryCounterData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. #region copyright
  2. //-------------------------------------------------------
  3. // Copyright (C) Dmitriy Yukhanov [https://codestage.net]
  4. //-------------------------------------------------------
  5. #endregion
  6. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  7. #define ENABLE_GFX_MEMORY
  8. #endif
  9. namespace CodeStage.AdvancedFPSCounter.CountersData
  10. {
  11. using System.Collections;
  12. using UnityEngine;
  13. using UnityEngine.Profiling;
  14. using Utils;
  15. /// <summary>
  16. /// Shows memory usage data.
  17. /// </summary>
  18. [System.Serializable]
  19. public class MemoryCounterData : UpdatableCounterData
  20. {
  21. // ----------------------------------------------------------------------------
  22. // constants
  23. // ----------------------------------------------------------------------------
  24. public const long MemoryDivider = 1048576; // 1024^2
  25. private const string TextStart = "<color=#{0}>";
  26. private const string LineStartTotal = "MEM TOTAL: ";
  27. private const string LineStartAllocated = "MEM ALLOC: ";
  28. private const string LineStartMono = "MEM MONO: ";
  29. #if ENABLE_GFX_MEMORY
  30. private const string LineStartGfx = "MEM GFX: ";
  31. #endif
  32. private const string LineEnd = " MB";
  33. private const string TextEnd = "</color>";
  34. // ----------------------------------------------------------------------------
  35. // properties exposed to the inspector
  36. // ----------------------------------------------------------------------------
  37. #region Precise
  38. [Tooltip("Allows to output memory usage more precisely thus using a bit more system resources.")]
  39. [SerializeField]
  40. private bool precise = true;
  41. /// <summary>
  42. /// Allows to output memory usage more precisely thus using a bit more system resources.
  43. /// </summary>
  44. public bool Precise
  45. {
  46. get { return precise; }
  47. set
  48. {
  49. if (precise == value || !Application.isPlaying) return;
  50. precise = value;
  51. if (!enabled) return;
  52. Refresh();
  53. }
  54. }
  55. #endregion
  56. #region Total
  57. [Tooltip("Allows to see private memory amount reserved for application. This memory can’t be used by other applications.")]
  58. [SerializeField]
  59. private bool total = true;
  60. /// <summary>
  61. /// Allows to see private memory amount reserved for application. This memory can’t be used by other applications.
  62. /// </summary>
  63. public bool Total
  64. {
  65. get { return total; }
  66. set
  67. {
  68. if (total == value || !Application.isPlaying) return;
  69. total = value;
  70. if (!total) LastTotalValue = 0;
  71. if (!enabled) return;
  72. Refresh();
  73. }
  74. }
  75. #endregion
  76. #region Allocated
  77. [Tooltip("Allows to see amount of memory, currently allocated by application.")]
  78. [SerializeField]
  79. private bool allocated = true;
  80. /// <summary>
  81. /// Allows to see amount of memory, currently allocated by application.
  82. /// </summary>
  83. public bool Allocated
  84. {
  85. get { return allocated; }
  86. set
  87. {
  88. if (allocated == value || !Application.isPlaying) return;
  89. allocated = value;
  90. if (!allocated) LastAllocatedValue = 0;
  91. if (!enabled) return;
  92. Refresh();
  93. }
  94. }
  95. #endregion
  96. #region MonoUsage
  97. [Tooltip("Allows to see amount of memory, allocated by managed Mono objects, " +
  98. "such as UnityEngine.Object and everything derived from it for example.")]
  99. [SerializeField]
  100. private bool monoUsage;
  101. /// <summary>
  102. /// Allows to see amount of memory, allocated by managed Mono objects,
  103. /// such as UnityEngine.Object and everything derived from it for example.
  104. /// </summary>
  105. public bool MonoUsage
  106. {
  107. get { return monoUsage; }
  108. set
  109. {
  110. if (monoUsage == value || !Application.isPlaying) return;
  111. monoUsage = value;
  112. if (!monoUsage) LastMonoValue = 0;
  113. if (!enabled) return;
  114. Refresh();
  115. }
  116. }
  117. #endregion
  118. #region GFX
  119. [Tooltip("Allows to see amount of allocated memory for the graphics driver (dev builds only).")]
  120. [SerializeField]
  121. private bool gfx = true;
  122. /// <summary>
  123. /// Allows to see amount of allocated memory for the graphics driver.
  124. /// Requires Unity 2018.1.0 or newer and Development build.
  125. /// This value is not included into the Total memory counter.
  126. /// </summary>
  127. public bool Gfx
  128. {
  129. get { return gfx; }
  130. set
  131. {
  132. if (gfx == value || !Application.isPlaying) return;
  133. gfx = value;
  134. if (!gfx) LastGfxValue = 0;
  135. if (!enabled) return;
  136. Refresh();
  137. }
  138. }
  139. #endregion
  140. // ----------------------------------------------------------------------------
  141. // properties only accessible from code
  142. // ----------------------------------------------------------------------------
  143. /// <summary>
  144. /// Last total memory readout.
  145. /// </summary>
  146. /// In megabytes if #Precise is false, in bytes otherwise.
  147. /// @see Total
  148. public long LastTotalValue { get; private set; }
  149. /// <summary>
  150. /// Last allocated memory readout.
  151. /// </summary>
  152. /// In megabytes if #Precise is false, in bytes otherwise.
  153. /// @see Allocated
  154. public long LastAllocatedValue { get; private set; }
  155. /// <summary>
  156. /// Last Mono memory readout.
  157. /// </summary>
  158. /// In megabytes if #Precise is false, in bytes otherwise.
  159. /// @see MonoUsage
  160. public long LastMonoValue { get; private set; }
  161. /// <summary>
  162. /// Last graphics driver memory readout.
  163. /// </summary>
  164. /// In megabytes if #Precise is false, in bytes otherwise.
  165. /// @see Gfx
  166. public long LastGfxValue { get; private set; }
  167. // ----------------------------------------------------------------------------
  168. // constructor
  169. // ----------------------------------------------------------------------------
  170. internal MemoryCounterData()
  171. {
  172. color = new Color32(234, 238, 101, 255);
  173. style = FontStyle.Bold;
  174. }
  175. // ----------------------------------------------------------------------------
  176. // internal methods
  177. // ----------------------------------------------------------------------------
  178. internal override void UpdateValue(bool force)
  179. {
  180. if (!enabled) return;
  181. if (force)
  182. {
  183. if (!inited && (HasData()))
  184. {
  185. Activate();
  186. return;
  187. }
  188. if (inited && (!HasData()))
  189. {
  190. Deactivate();
  191. return;
  192. }
  193. }
  194. if (total)
  195. {
  196. var value = Profiler.GetTotalReservedMemoryLong();
  197. long divisionResult = 0;
  198. bool newValue;
  199. if (precise)
  200. {
  201. newValue = LastTotalValue != value;
  202. }
  203. else
  204. {
  205. divisionResult = value / MemoryDivider;
  206. newValue = LastTotalValue != divisionResult;
  207. }
  208. if (newValue || force)
  209. {
  210. LastTotalValue = precise ? value : divisionResult;
  211. dirty = true;
  212. }
  213. }
  214. if (allocated)
  215. {
  216. var value = Profiler.GetTotalAllocatedMemoryLong();
  217. long divisionResult = 0;
  218. bool newValue;
  219. if (precise)
  220. {
  221. newValue = LastAllocatedValue != value;
  222. }
  223. else
  224. {
  225. divisionResult = value / MemoryDivider;
  226. newValue = (LastAllocatedValue != divisionResult);
  227. }
  228. if (newValue || force)
  229. {
  230. LastAllocatedValue = precise ? value : divisionResult;
  231. dirty = true;
  232. }
  233. }
  234. if (monoUsage)
  235. {
  236. var monoMemory = System.GC.GetTotalMemory(false);
  237. long divisionResult = 0;
  238. bool newValue;
  239. if (precise)
  240. {
  241. newValue = (LastMonoValue != monoMemory);
  242. }
  243. else
  244. {
  245. divisionResult = monoMemory / MemoryDivider;
  246. newValue = (LastMonoValue != divisionResult);
  247. }
  248. if (newValue || force)
  249. {
  250. LastMonoValue = precise ? monoMemory : divisionResult;
  251. dirty = true;
  252. }
  253. }
  254. #if ENABLE_GFX_MEMORY
  255. if (gfx)
  256. {
  257. var value = Profiler.GetAllocatedMemoryForGraphicsDriver();
  258. long divisionResult = 0;
  259. bool newValue;
  260. if (precise)
  261. {
  262. newValue = LastGfxValue != value;
  263. }
  264. else
  265. {
  266. divisionResult = value / MemoryDivider;
  267. newValue = LastGfxValue != divisionResult;
  268. }
  269. if (newValue || force)
  270. {
  271. LastGfxValue = precise ? value : divisionResult;
  272. dirty = true;
  273. }
  274. }
  275. #endif
  276. if (!dirty || main.OperationMode != OperationMode.Normal) return;
  277. var needNewLine = false;
  278. text.Length = 0;
  279. text.Append(colorCached);
  280. if (total)
  281. {
  282. text.Append(LineStartTotal);
  283. if (precise)
  284. {
  285. text.AppendLookup(LastTotalValue / (float)MemoryDivider);
  286. }
  287. else
  288. {
  289. text.AppendLookup(LastTotalValue);
  290. }
  291. text.Append(LineEnd);
  292. needNewLine = true;
  293. }
  294. if (allocated)
  295. {
  296. if (needNewLine) text.Append(AFPSCounter.NewLine);
  297. text.Append(LineStartAllocated);
  298. if (precise)
  299. {
  300. text.AppendLookup(LastAllocatedValue / (float)MemoryDivider);
  301. }
  302. else
  303. {
  304. text.AppendLookup(LastAllocatedValue);
  305. }
  306. text.Append(LineEnd);
  307. needNewLine = true;
  308. }
  309. if (monoUsage)
  310. {
  311. if (needNewLine) text.Append(AFPSCounter.NewLine);
  312. text.Append(LineStartMono);
  313. if (precise)
  314. {
  315. text.AppendLookup(LastMonoValue / (float)MemoryDivider);
  316. }
  317. else
  318. {
  319. text.AppendLookup(LastMonoValue);
  320. }
  321. text.Append(LineEnd);
  322. #if ENABLE_GFX_MEMORY
  323. needNewLine = true;
  324. #endif
  325. }
  326. #if ENABLE_GFX_MEMORY
  327. if (gfx)
  328. {
  329. if (needNewLine) text.Append(AFPSCounter.NewLine);
  330. text.Append(LineStartGfx);
  331. if (precise)
  332. {
  333. text.AppendLookup(LastGfxValue / (float)MemoryDivider);
  334. }
  335. else
  336. {
  337. text.AppendLookup(LastGfxValue);
  338. }
  339. text.Append(LineEnd);
  340. }
  341. #endif
  342. text.Append(TextEnd);
  343. ApplyTextStyles();
  344. }
  345. // ----------------------------------------------------------------------------
  346. // protected methods
  347. // ----------------------------------------------------------------------------
  348. protected override void PerformActivationActions()
  349. {
  350. base.PerformActivationActions();
  351. if (!HasData()) return;
  352. LastTotalValue = 0;
  353. LastAllocatedValue = 0;
  354. LastMonoValue = 0;
  355. if (main.OperationMode == OperationMode.Normal)
  356. {
  357. if (colorCached == null)
  358. {
  359. colorCached = string.Format(TextStart, AFPSCounter.Color32ToHex(color));
  360. }
  361. text.Append(colorCached);
  362. if (total)
  363. {
  364. if (precise)
  365. {
  366. text.Append(LineStartTotal).Append("0.00").Append(LineEnd);
  367. }
  368. else
  369. {
  370. text.Append(LineStartTotal).Append(0).Append(LineEnd);
  371. }
  372. }
  373. if (allocated)
  374. {
  375. if (text.Length > 0) text.Append(AFPSCounter.NewLine);
  376. if (precise)
  377. {
  378. text.Append(LineStartAllocated).Append("0.00").Append(LineEnd);
  379. }
  380. else
  381. {
  382. text.Append(LineStartAllocated).Append(0).Append(LineEnd);
  383. }
  384. }
  385. if (monoUsage)
  386. {
  387. if (text.Length > 0) text.Append(AFPSCounter.NewLine);
  388. if (precise)
  389. {
  390. text.Append(LineStartMono).Append("0.00").Append(LineEnd);
  391. }
  392. else
  393. {
  394. text.Append(LineStartMono).Append(0).Append(LineEnd);
  395. }
  396. }
  397. #if ENABLE_GFX_MEMORY
  398. if (gfx)
  399. {
  400. if (text.Length > 0) text.Append(AFPSCounter.NewLine);
  401. if (precise)
  402. {
  403. text.Append(LineStartGfx).Append("0.00").Append(LineEnd);
  404. }
  405. else
  406. {
  407. text.Append(LineStartGfx).Append(0).Append(LineEnd);
  408. }
  409. }
  410. #endif
  411. text.Append(TextEnd);
  412. ApplyTextStyles();
  413. dirty = true;
  414. }
  415. }
  416. protected override void PerformDeActivationActions()
  417. {
  418. base.PerformDeActivationActions();
  419. if (text != null) text.Length = 0;
  420. main.MakeDrawableLabelDirty(anchor);
  421. }
  422. protected override IEnumerator UpdateCounter()
  423. {
  424. while (true)
  425. {
  426. UpdateValue();
  427. main.UpdateTexts();
  428. var previousUpdateTime = Time.unscaledTime;
  429. while (Time.unscaledTime < previousUpdateTime + updateInterval)
  430. {
  431. yield return null;
  432. }
  433. }
  434. }
  435. protected override bool HasData()
  436. {
  437. return total || allocated || monoUsage || gfx;
  438. }
  439. protected override void CacheCurrentColor()
  440. {
  441. colorCached = string.Format(TextStart, AFPSCounter.Color32ToHex(color));
  442. }
  443. }
  444. }