OVRSystemPerfMetrics.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.IO;
  18. using System.Net;
  19. using System.Net.Sockets;
  20. using System.Text;
  21. using System.Threading;
  22. using UnityEngine;
  23. using UnityEngine.Networking;
  24. using Debug = UnityEngine.Debug;
  25. public class OVRSystemPerfMetrics
  26. {
  27. public const int TcpListeningPort = 32419;
  28. public const int PayloadTypeMetrics = 100;
  29. public const int MaxBufferLength = 65536;
  30. public const int MaxMessageLength = MaxBufferLength - sizeof(int);
  31. public class PerfMetrics
  32. {
  33. public int frameCount;
  34. public float frameTime;
  35. public float deltaFrameTime;
  36. public bool appCpuTime_IsValid;
  37. public float appCpuTime;
  38. public bool appGpuTime_IsValid;
  39. public float appGpuTime;
  40. public bool compositorCpuTime_IsValid;
  41. public float compositorCpuTime;
  42. public bool compositorGpuTime_IsValid;
  43. public float compositorGpuTime;
  44. public bool compositorDroppedFrameCount_IsValid;
  45. public int compositorDroppedFrameCount;
  46. public bool systemGpuUtilPercentage_IsValid;
  47. public float systemGpuUtilPercentage;
  48. public bool systemCpuUtilAveragePercentage_IsValid;
  49. public float systemCpuUtilAveragePercentage;
  50. public bool systemCpuUtilWorstPercentage_IsValid;
  51. public float systemCpuUtilWorstPercentage;
  52. public bool deviceCpuClockFrequencyInMHz_IsValid;
  53. public float deviceCpuClockFrequencyInMHz;
  54. public bool deviceGpuClockFrequencyInMHz_IsValid;
  55. public float deviceGpuClockFrequencyInMHz;
  56. public bool deviceCpuClockLevel_IsValid;
  57. public int deviceCpuClockLevel;
  58. public bool deviceGpuClockLevel_IsValid;
  59. public int deviceGpuClockLevel;
  60. public string ToJSON()
  61. {
  62. OVRSimpleJSON.JSONObject jsonNode = new OVRSimpleJSON.JSONObject();
  63. jsonNode.Add("frameCount", new OVRSimpleJSON.JSONNumber(frameCount));
  64. jsonNode.Add("frameTime", new OVRSimpleJSON.JSONNumber(frameTime));
  65. jsonNode.Add("deltaFrameTime", new OVRSimpleJSON.JSONNumber(deltaFrameTime));
  66. if (appCpuTime_IsValid)
  67. {
  68. jsonNode.Add("appCpuTime", new OVRSimpleJSON.JSONNumber(appCpuTime));
  69. }
  70. if (appGpuTime_IsValid)
  71. {
  72. jsonNode.Add("appGpuTime", new OVRSimpleJSON.JSONNumber(appGpuTime));
  73. }
  74. if (compositorCpuTime_IsValid)
  75. {
  76. jsonNode.Add("compositorCpuTime", new OVRSimpleJSON.JSONNumber(compositorCpuTime));
  77. }
  78. if (compositorGpuTime_IsValid)
  79. {
  80. jsonNode.Add("compositorGpuTime", new OVRSimpleJSON.JSONNumber(compositorGpuTime));
  81. }
  82. if (compositorDroppedFrameCount_IsValid)
  83. {
  84. jsonNode.Add("compositorDroppedFrameCount", new OVRSimpleJSON.JSONNumber(compositorDroppedFrameCount));
  85. }
  86. if (systemGpuUtilPercentage_IsValid)
  87. {
  88. jsonNode.Add("systemGpuUtilPercentage", new OVRSimpleJSON.JSONNumber(systemGpuUtilPercentage));
  89. }
  90. if (systemCpuUtilAveragePercentage_IsValid)
  91. {
  92. jsonNode.Add("systemCpuUtilAveragePercentage", new OVRSimpleJSON.JSONNumber(systemCpuUtilAveragePercentage));
  93. }
  94. if (systemCpuUtilWorstPercentage_IsValid)
  95. {
  96. jsonNode.Add("systemCpuUtilWorstPercentage", new OVRSimpleJSON.JSONNumber(systemCpuUtilWorstPercentage));
  97. }
  98. if (deviceCpuClockFrequencyInMHz_IsValid)
  99. {
  100. jsonNode.Add("deviceCpuClockFrequencyInMHz", new OVRSimpleJSON.JSONNumber(deviceCpuClockFrequencyInMHz));
  101. }
  102. if (deviceGpuClockFrequencyInMHz_IsValid)
  103. {
  104. jsonNode.Add("deviceGpuClockFrequencyInMHz", new OVRSimpleJSON.JSONNumber(deviceGpuClockFrequencyInMHz));
  105. }
  106. if (deviceCpuClockLevel_IsValid)
  107. {
  108. jsonNode.Add("deviceCpuClockLevel", new OVRSimpleJSON.JSONNumber(deviceCpuClockLevel));
  109. }
  110. if (deviceGpuClockLevel_IsValid)
  111. {
  112. jsonNode.Add("deviceGpuClockLevel", new OVRSimpleJSON.JSONNumber(deviceGpuClockLevel));
  113. }
  114. string str = jsonNode.ToString();
  115. return str;
  116. }
  117. public bool LoadFromJSON(string json)
  118. {
  119. OVRSimpleJSON.JSONObject jsonNode = OVRSimpleJSON.JSONObject.Parse(json) as OVRSimpleJSON.JSONObject;
  120. if (jsonNode == null)
  121. {
  122. return false;
  123. }
  124. frameCount = jsonNode["frameCount"] != null ? jsonNode["frameCount"].AsInt : 0;
  125. frameTime = jsonNode["frameTime"] != null ? jsonNode["frameTime"].AsFloat : 0;
  126. deltaFrameTime = jsonNode["deltaFrameTime"] != null ? jsonNode["deltaFrameTime"].AsFloat : 0;
  127. appCpuTime_IsValid = jsonNode["appCpuTime"] != null;
  128. appCpuTime = appCpuTime_IsValid ? jsonNode["appCpuTime"].AsFloat : 0;
  129. appGpuTime_IsValid = jsonNode["appGpuTime"] != null;
  130. appGpuTime = appGpuTime_IsValid ? jsonNode["appGpuTime"].AsFloat : 0;
  131. compositorCpuTime_IsValid = jsonNode["compositorCpuTime"] != null;
  132. compositorCpuTime = compositorCpuTime_IsValid ? jsonNode["compositorCpuTime"].AsFloat : 0;
  133. compositorGpuTime_IsValid = jsonNode["compositorGpuTime"] != null;
  134. compositorGpuTime = compositorGpuTime_IsValid ? jsonNode["compositorGpuTime"].AsFloat : 0;
  135. compositorDroppedFrameCount_IsValid = jsonNode["compositorDroppedFrameCount"] != null;
  136. compositorDroppedFrameCount = compositorDroppedFrameCount_IsValid ? jsonNode["ompositorDroppedFrameCount"].AsInt : 0;
  137. systemGpuUtilPercentage_IsValid = jsonNode["systemGpuUtilPercentage"] != null;
  138. systemGpuUtilPercentage = systemGpuUtilPercentage_IsValid ? jsonNode["systemGpuUtilPercentage"].AsFloat : 0;
  139. systemCpuUtilAveragePercentage_IsValid = jsonNode["systemCpuUtilAveragePercentage"] != null;
  140. systemCpuUtilAveragePercentage = systemCpuUtilAveragePercentage_IsValid ? jsonNode["systemCpuUtilAveragePercentage"].AsFloat : 0;
  141. systemCpuUtilWorstPercentage_IsValid = jsonNode["systemCpuUtilWorstPercentage"] != null;
  142. systemCpuUtilWorstPercentage = systemCpuUtilWorstPercentage_IsValid ? jsonNode["systemCpuUtilWorstPercentage"].AsFloat : 0;
  143. deviceCpuClockFrequencyInMHz_IsValid = jsonNode["deviceCpuClockFrequencyInMHz"] != null;
  144. deviceCpuClockFrequencyInMHz = deviceCpuClockFrequencyInMHz_IsValid ? jsonNode["deviceCpuClockFrequencyInMHz"].AsFloat : 0;
  145. deviceGpuClockFrequencyInMHz_IsValid = jsonNode["deviceGpuClockFrequencyInMHz"] != null;
  146. deviceGpuClockFrequencyInMHz = deviceGpuClockFrequencyInMHz_IsValid ? jsonNode["deviceGpuClockFrequencyInMHz"].AsFloat : 0;
  147. deviceCpuClockLevel_IsValid = jsonNode["deviceCpuClockLevel"] != null;
  148. deviceCpuClockLevel = deviceCpuClockLevel_IsValid ? jsonNode["deviceCpuClockLevel"].AsInt : 0;
  149. deviceGpuClockLevel_IsValid = jsonNode["deviceGpuClockLevel"] != null;
  150. deviceGpuClockLevel = deviceGpuClockLevel_IsValid ? jsonNode["deviceGpuClockLevel"].AsInt : 0;
  151. return true;
  152. }
  153. }
  154. public class OVRSystemPerfMetricsTcpServer : MonoBehaviour
  155. {
  156. public static OVRSystemPerfMetricsTcpServer singleton = null;
  157. private OVRNetwork.OVRNetworkTcpServer tcpServer = new OVRNetwork.OVRNetworkTcpServer();
  158. public int listeningPort = OVRSystemPerfMetrics.TcpListeningPort;
  159. void OnEnable()
  160. {
  161. if (singleton != null)
  162. {
  163. Debug.LogError("Mutiple OVRSystemPerfMetricsTcpServer exists");
  164. return;
  165. }
  166. else
  167. {
  168. singleton = this;
  169. }
  170. if (Application.isEditor)
  171. {
  172. Application.runInBackground = true;
  173. }
  174. tcpServer.StartListening(listeningPort);
  175. }
  176. void OnDisable()
  177. {
  178. tcpServer.StopListening();
  179. singleton = null;
  180. Debug.Log("[OVRSystemPerfMetricsTcpServer] server destroyed");
  181. }
  182. private void Update()
  183. {
  184. if (tcpServer.HasConnectedClient())
  185. {
  186. PerfMetrics metrics = GatherPerfMetrics();
  187. string json = metrics.ToJSON();
  188. byte[] bytes = Encoding.UTF8.GetBytes(json);
  189. tcpServer.Broadcast(OVRSystemPerfMetrics.PayloadTypeMetrics, bytes);
  190. }
  191. }
  192. PerfMetrics GatherPerfMetrics()
  193. {
  194. PerfMetrics metrics = new PerfMetrics();
  195. metrics.frameCount = Time.frameCount;
  196. metrics.frameTime = Time.unscaledTime;
  197. metrics.deltaFrameTime = Time.unscaledDeltaTime;
  198. float? floatValue;
  199. int? intValue;
  200. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.App_CpuTime_Float);
  201. metrics.appCpuTime_IsValid = floatValue.HasValue;
  202. metrics.appCpuTime = floatValue.GetValueOrDefault();
  203. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.App_GpuTime_Float);
  204. metrics.appGpuTime_IsValid = floatValue.HasValue;
  205. metrics.appGpuTime = floatValue.GetValueOrDefault();
  206. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.Compositor_CpuTime_Float);
  207. metrics.compositorCpuTime_IsValid = floatValue.HasValue;
  208. metrics.compositorCpuTime = floatValue.GetValueOrDefault();
  209. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.Compositor_GpuTime_Float);
  210. metrics.compositorGpuTime_IsValid = floatValue.HasValue;
  211. metrics.compositorGpuTime = floatValue.GetValueOrDefault();
  212. intValue = OVRPlugin.GetPerfMetricsInt(OVRPlugin.PerfMetrics.Compositor_DroppedFrameCount_Int);
  213. metrics.compositorDroppedFrameCount_IsValid = intValue.HasValue;
  214. metrics.compositorDroppedFrameCount = intValue.GetValueOrDefault();
  215. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.System_GpuUtilPercentage_Float);
  216. metrics.systemGpuUtilPercentage_IsValid = floatValue.HasValue;
  217. metrics.systemGpuUtilPercentage = floatValue.GetValueOrDefault();
  218. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.System_CpuUtilAveragePercentage_Float);
  219. metrics.systemCpuUtilAveragePercentage_IsValid = floatValue.HasValue;
  220. metrics.systemCpuUtilAveragePercentage = floatValue.GetValueOrDefault();
  221. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.System_CpuUtilWorstPercentage_Float);
  222. metrics.systemCpuUtilWorstPercentage_IsValid = floatValue.HasValue;
  223. metrics.systemCpuUtilWorstPercentage = floatValue.GetValueOrDefault();
  224. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.Device_CpuClockFrequencyInMHz_Float);
  225. metrics.deviceCpuClockFrequencyInMHz_IsValid = floatValue.HasValue;
  226. metrics.deviceCpuClockFrequencyInMHz = floatValue.GetValueOrDefault();
  227. floatValue = OVRPlugin.GetPerfMetricsFloat(OVRPlugin.PerfMetrics.Device_GpuClockFrequencyInMHz_Float);
  228. metrics.deviceGpuClockFrequencyInMHz_IsValid = floatValue.HasValue;
  229. metrics.deviceGpuClockFrequencyInMHz = floatValue.GetValueOrDefault();
  230. intValue = OVRPlugin.GetPerfMetricsInt(OVRPlugin.PerfMetrics.Device_CpuClockLevel_Int);
  231. metrics.deviceCpuClockLevel_IsValid = intValue.HasValue;
  232. metrics.deviceCpuClockLevel = intValue.GetValueOrDefault();
  233. intValue = OVRPlugin.GetPerfMetricsInt(OVRPlugin.PerfMetrics.Device_GpuClockLevel_Int);
  234. metrics.deviceGpuClockLevel_IsValid = intValue.HasValue;
  235. metrics.deviceGpuClockLevel = intValue.GetValueOrDefault();
  236. return metrics;
  237. }
  238. }
  239. }