PerformanceMonitor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using SuperSocket.Common;
  9. using SuperSocket.SocketBase;
  10. using SuperSocket.SocketBase.Config;
  11. using SuperSocket.SocketBase.Metadata;
  12. namespace SuperSocket.SocketEngine
  13. {
  14. //class PerformanceMonitor : IPerformanceMonitor
  15. //{
  16. // public event Action<NodeStatus> OnStatusUpdate;
  17. // private Timer m_PerformanceTimer;
  18. // private int m_TimerInterval;
  19. // private ILog m_PerfLog;
  20. // private IWorkItem[] m_AppServers;
  21. // private IWorkItem m_ServerManager;
  22. // private ProcessPerformanceCounterHelper m_Helper;
  23. // private List<KeyValuePair<string, StatusInfoAttribute[]>> m_ServerStatusMetadataSource;
  24. // public PerformanceMonitor(IRootConfig config, IEnumerable<IWorkItem> appServers, IWorkItem serverManager, ILogFactory logFactory)
  25. // {
  26. // m_PerfLog = logFactory.GetLog("Performance");
  27. // m_AppServers = appServers.ToArray();
  28. // m_ServerManager = serverManager;
  29. // m_Helper = new ProcessPerformanceCounterHelper(Process.GetCurrentProcess());
  30. // m_TimerInterval = config.PerformanceDataCollectInterval * 1000;
  31. // m_PerformanceTimer = new Timer(OnPerformanceTimerCallback);
  32. // }
  33. // private void SetupServerStatusMetadata()
  34. // {
  35. // if (m_ServerStatusMetadataSource == null)
  36. // {
  37. // m_ServerStatusMetadataSource = new List<KeyValuePair<string, StatusInfoAttribute[]>>(m_AppServers.Length + 1);
  38. // m_ServerStatusMetadataSource.Add(
  39. // new KeyValuePair<string, StatusInfoAttribute[]>(string.Empty,
  40. // new StatusInfoAttribute[]
  41. // {
  42. // new StatusInfoAttribute(StatusInfoKeys.CpuUsage) { Name = "CPU Usage", Format = "{0:0.00}%", Order = 0 },
  43. // new StatusInfoAttribute(StatusInfoKeys.MemoryUsage) { Name = "Physical Memory Usage", Format = "{0:N}", Order = 1 },
  44. // new StatusInfoAttribute(StatusInfoKeys.TotalThreadCount) { Name = "Total Thread Count", Order = 2 },
  45. // new StatusInfoAttribute(StatusInfoKeys.AvailableWorkingThreads) { Name = "Available Working Threads", Order = 3 },
  46. // new StatusInfoAttribute(StatusInfoKeys.AvailableCompletionPortThreads) { Name = "Available Completion Port Threads", Order = 4 },
  47. // new StatusInfoAttribute(StatusInfoKeys.MaxWorkingThreads) { Name = "Maximum Working Threads", Order = 5 },
  48. // new StatusInfoAttribute(StatusInfoKeys.MaxCompletionPortThreads) { Name = "Maximum Completion Port Threads", Order = 6 }
  49. // }));
  50. // for (var i = 0; i < m_AppServers.Length; i++)
  51. // {
  52. // var server = m_AppServers[i];
  53. // m_ServerStatusMetadataSource.Add(
  54. // new KeyValuePair<string, StatusInfoAttribute[]>(server.Name, server.GetServerStatusMetadata().OrderBy(s => s.Order).ToArray()));
  55. // }
  56. // if (m_ServerManager != null && m_ServerManager.State == ServerState.Running)
  57. // {
  58. // m_ServerManager.TransferSystemMessage("ServerMetadataCollected", m_ServerStatusMetadataSource);
  59. // }
  60. // }
  61. // }
  62. // public void Start()
  63. // {
  64. // SetupServerStatusMetadata();
  65. // m_PerformanceTimer.Change(0, m_TimerInterval);
  66. // }
  67. // public void Stop()
  68. // {
  69. // m_PerformanceTimer.Change(Timeout.Infinite, Timeout.Infinite);
  70. // }
  71. // private void OnPerformanceTimerCallback(object state)
  72. // {
  73. // var nodeStatus = new NodeStatus();
  74. // StatusInfoCollection bootstrapStatus = new StatusInfoCollection();
  75. // m_Helper.Collect(bootstrapStatus);
  76. // nodeStatus.BootstrapStatus = bootstrapStatus;
  77. // var instancesStatus = new List<StatusInfoCollection>(m_AppServers.Length);
  78. // var perfBuilder = new StringBuilder();
  79. // perfBuilder.AppendLine("---------------------------------------------------");
  80. // perfBuilder.AppendLine(string.Format("CPU Usage: {0:0.00}%, Physical Memory Usage: {1:N}, Total Thread Count: {2}", bootstrapStatus[StatusInfoKeys.CpuUsage], bootstrapStatus[StatusInfoKeys.MemoryUsage], bootstrapStatus[StatusInfoKeys.TotalThreadCount]));
  81. // perfBuilder.AppendLine(string.Format("AvailableWorkingThreads: {0}, AvailableCompletionPortThreads: {1}", bootstrapStatus[StatusInfoKeys.AvailableWorkingThreads], bootstrapStatus[StatusInfoKeys.AvailableCompletionPortThreads]));
  82. // perfBuilder.AppendLine(string.Format("MaxWorkingThreads: {0}, MaxCompletionPortThreads: {1}", bootstrapStatus[StatusInfoKeys.MaxWorkingThreads], bootstrapStatus[StatusInfoKeys.MaxCompletionPortThreads]));
  83. // for (var i = 0; i < m_AppServers.Length; i++)
  84. // {
  85. // var s = m_AppServers[i];
  86. // var metadata = m_ServerStatusMetadataSource[i + 1].Value;
  87. // if (metadata == null)
  88. // {
  89. // perfBuilder.AppendLine(string.Format("{0} ----------------------------------", s.Name));
  90. // perfBuilder.AppendLine(string.Format("{0}: {1}", "State", s.State));
  91. // }
  92. // else
  93. // {
  94. // var serverStatus = s.CollectServerStatus(bootstrapStatus);
  95. // instancesStatus.Add(serverStatus);
  96. // perfBuilder.AppendLine(string.Format("{0} ----------------------------------", serverStatus.Tag));
  97. // for (var j = 0; j < metadata.Length; j++)
  98. // {
  99. // var statusInfoAtt = metadata[j];
  100. // if (!statusInfoAtt.OutputInPerfLog)
  101. // continue;
  102. // var statusValue = serverStatus[statusInfoAtt.Key];
  103. // if (statusValue == null)
  104. // continue;
  105. // perfBuilder.AppendLine(
  106. // string.Format("{0}: {1}", statusInfoAtt.Name,
  107. // string.IsNullOrEmpty(statusInfoAtt.Format) ? statusValue : string.Format(statusInfoAtt.Format, statusValue)));
  108. // }
  109. // }
  110. // }
  111. // m_PerfLog.Info(perfBuilder.ToString());
  112. // nodeStatus.InstancesStatus = instancesStatus.ToArray();
  113. // if (OnStatusUpdate != null) OnStatusUpdate(nodeStatus);
  114. // if (m_ServerManager != null && m_ServerManager.State == ServerState.Running)
  115. // {
  116. // m_ServerManager.TransferSystemMessage("ServerStatusCollected", nodeStatus);
  117. // }
  118. // }
  119. // public void Dispose()
  120. // {
  121. // if (m_PerformanceTimer != null)
  122. // {
  123. // m_PerformanceTimer.Dispose();
  124. // m_PerformanceTimer = null;
  125. // }
  126. // if (m_Helper != null)
  127. // {
  128. // m_Helper.Dispose();
  129. // m_Helper = null;
  130. // }
  131. // }
  132. // public int StatusUpdateInterval
  133. // {
  134. // get { return m_TimerInterval / 1000; }
  135. // set
  136. // {
  137. // var newTimerInterval = value * 1000;
  138. // if (m_TimerInterval == newTimerInterval)
  139. // return;
  140. // m_TimerInterval = newTimerInterval;
  141. // Stop();
  142. // Start();
  143. // }
  144. // }
  145. //}
  146. #pragma warning disable CS0067
  147. class PerformanceMonitor : IPerformanceMonitor
  148. {
  149. public int StatusUpdateInterval { get; set; }
  150. public event Action<NodeStatus> OnStatusUpdate;
  151. public void Dispose()
  152. {
  153. }
  154. public void Start()
  155. {
  156. }
  157. public void Stop()
  158. {
  159. }
  160. }
  161. #pragma warning restore CS0067
  162. }