BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRSystemPerfMetrics.cs

OVRSystemPerfMetrics.cs

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