opensim-development – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ |
||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * * Redistributions of source code must retain the above copyright |
||
8 | * notice, this list of conditions and the following disclaimer. |
||
9 | * * Redistributions in binary form must reproduce the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer in the |
||
11 | * documentation and/or other materials provided with the distribution. |
||
12 | * * Neither the name of the OpenSimulator Project nor the |
||
13 | * names of its contributors may be used to endorse or promote products |
||
14 | * derived from this software without specific prior written permission. |
||
15 | * |
||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
26 | */ |
||
27 | |||
28 | using System; |
||
29 | using System.Collections.Generic; |
||
30 | using System.Diagnostics; |
||
31 | using System.Linq; |
||
32 | using System.Net.NetworkInformation; |
||
33 | using System.Text; |
||
34 | using System.Threading; |
||
35 | using log4net; |
||
36 | using Nini.Config; |
||
37 | using OpenMetaverse.StructuredData; |
||
38 | using OpenSim.Framework; |
||
39 | |||
40 | namespace OpenSim.Framework.Monitoring |
||
41 | { |
||
42 | public class ServerStatsCollector |
||
43 | { |
||
44 | private readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
||
45 | private readonly string LogHeader = "[SERVER STATS]"; |
||
46 | |||
47 | public bool Enabled = false; |
||
48 | private static Dictionary<string, Stat> RegisteredStats = new Dictionary<string, Stat>(); |
||
49 | |||
50 | public readonly string CategoryServer = "server"; |
||
51 | |||
52 | public readonly string ContainerThreadpool = "threadpool"; |
||
53 | public readonly string ContainerProcessor = "processor"; |
||
54 | public readonly string ContainerMemory = "memory"; |
||
55 | public readonly string ContainerNetwork = "network"; |
||
56 | public readonly string ContainerProcess = "process"; |
||
57 | |||
58 | public string NetworkInterfaceTypes = "Ethernet"; |
||
59 | |||
60 | readonly int performanceCounterSampleInterval = 500; |
||
61 | // int lastperformanceCounterSampleTime = 0; |
||
62 | |||
63 | private class PerfCounterControl |
||
64 | { |
||
65 | public PerformanceCounter perfCounter; |
||
66 | public int lastFetch; |
||
67 | public string name; |
||
68 | public PerfCounterControl(PerformanceCounter pPc) |
||
69 | : this(pPc, String.Empty) |
||
70 | { |
||
71 | } |
||
72 | public PerfCounterControl(PerformanceCounter pPc, string pName) |
||
73 | { |
||
74 | perfCounter = pPc; |
||
75 | lastFetch = 0; |
||
76 | name = pName; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | PerfCounterControl processorPercentPerfCounter = null; |
||
81 | |||
82 | // IRegionModuleBase.Initialize |
||
83 | public void Initialise(IConfigSource source) |
||
84 | { |
||
85 | IConfig cfg = source.Configs["Monitoring"]; |
||
86 | |||
87 | if (cfg != null) |
||
88 | Enabled = cfg.GetBoolean("ServerStatsEnabled", true); |
||
89 | |||
90 | if (Enabled) |
||
91 | { |
||
92 | NetworkInterfaceTypes = cfg.GetString("NetworkInterfaceTypes", "Ethernet"); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public void Start() |
||
97 | { |
||
98 | if (RegisteredStats.Count == 0) |
||
99 | RegisterServerStats(); |
||
100 | } |
||
101 | |||
102 | public void Close() |
||
103 | { |
||
104 | if (RegisteredStats.Count > 0) |
||
105 | { |
||
106 | foreach (Stat stat in RegisteredStats.Values) |
||
107 | { |
||
108 | StatsManager.DeregisterStat(stat); |
||
109 | stat.Dispose(); |
||
110 | } |
||
111 | RegisteredStats.Clear(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | private void MakeStat(string pName, string pDesc, string pUnit, string pContainer, Action<Stat> act) |
||
116 | { |
||
117 | MakeStat(pName, pDesc, pUnit, pContainer, act, MeasuresOfInterest.None); |
||
118 | } |
||
119 | |||
120 | private void MakeStat(string pName, string pDesc, string pUnit, string pContainer, Action<Stat> act, MeasuresOfInterest moi) |
||
121 | { |
||
122 | string desc = pDesc; |
||
123 | if (desc == null) |
||
124 | desc = pName; |
||
125 | Stat stat = new Stat(pName, pName, desc, pUnit, CategoryServer, pContainer, StatType.Pull, moi, act, StatVerbosity.Debug); |
||
126 | StatsManager.RegisterStat(stat); |
||
127 | RegisteredStats.Add(pName, stat); |
||
128 | } |
||
129 | |||
130 | public void RegisterServerStats() |
||
131 | { |
||
132 | // lastperformanceCounterSampleTime = Util.EnvironmentTickCount(); |
||
133 | PerformanceCounter tempPC; |
||
134 | Stat tempStat; |
||
135 | string tempName; |
||
136 | |||
137 | try |
||
138 | { |
||
139 | tempName = "CPUPercent"; |
||
140 | tempPC = new PerformanceCounter("Processor", "% Processor Time", "_Total"); |
||
141 | processorPercentPerfCounter = new PerfCounterControl(tempPC); |
||
142 | // A long time bug in mono is that CPU percent is reported as CPU percent idle. Windows reports CPU percent busy. |
||
143 | tempStat = new Stat(tempName, tempName, "", "percent", CategoryServer, ContainerProcessor, |
||
144 | StatType.Pull, (s) => { GetNextValue(s, processorPercentPerfCounter, Util.IsWindows() ? 1 : -1); }, |
||
145 | StatVerbosity.Info); |
||
146 | StatsManager.RegisterStat(tempStat); |
||
147 | RegisteredStats.Add(tempName, tempStat); |
||
148 | |||
149 | MakeStat("TotalProcessorTime", null, "sec", ContainerProcessor, |
||
150 | (s) => { s.Value = Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds; }); |
||
151 | |||
152 | MakeStat("UserProcessorTime", null, "sec", ContainerProcessor, |
||
153 | (s) => { s.Value = Process.GetCurrentProcess().UserProcessorTime.TotalSeconds; }); |
||
154 | |||
155 | MakeStat("PrivilegedProcessorTime", null, "sec", ContainerProcessor, |
||
156 | (s) => { s.Value = Process.GetCurrentProcess().PrivilegedProcessorTime.TotalSeconds; }); |
||
157 | |||
158 | MakeStat("Threads", null, "threads", ContainerProcessor, |
||
159 | (s) => { s.Value = Process.GetCurrentProcess().Threads.Count; }); |
||
160 | } |
||
161 | catch (Exception e) |
||
162 | { |
||
163 | m_log.ErrorFormat("{0} Exception creating 'Process': {1}", LogHeader, e); |
||
164 | } |
||
165 | |||
166 | MakeStat("BuiltinThreadpoolWorkerThreadsAvailable", null, "threads", ContainerThreadpool, |
||
167 | s => |
||
168 | { |
||
169 | int workerThreads, iocpThreads; |
||
170 | ThreadPool.GetAvailableThreads(out workerThreads, out iocpThreads); |
||
171 | s.Value = workerThreads; |
||
172 | }); |
||
173 | |||
174 | MakeStat("BuiltinThreadpoolIOCPThreadsAvailable", null, "threads", ContainerThreadpool, |
||
175 | s => |
||
176 | { |
||
177 | int workerThreads, iocpThreads; |
||
178 | ThreadPool.GetAvailableThreads(out workerThreads, out iocpThreads); |
||
179 | s.Value = iocpThreads; |
||
180 | }); |
||
181 | |||
182 | if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool && Util.GetSmartThreadPoolInfo() != null) |
||
183 | { |
||
184 | MakeStat("STPMaxThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().MaxThreads); |
||
185 | MakeStat("STPMinThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().MinThreads); |
||
186 | MakeStat("STPConcurrency", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().MaxConcurrentWorkItems); |
||
187 | MakeStat("STPActiveThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().ActiveThreads); |
||
188 | MakeStat("STPInUseThreads", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().InUseThreads); |
||
189 | MakeStat("STPWorkItemsWaiting", null, "threads", ContainerThreadpool, s => s.Value = Util.GetSmartThreadPoolInfo().WaitingCallbacks); |
||
190 | } |
||
191 | |||
192 | MakeStat( |
||
193 | "HTTPRequestsMade", |
||
194 | "Number of outbound HTTP requests made", |
||
195 | "requests", |
||
196 | ContainerNetwork, |
||
197 | s => s.Value = WebUtil.RequestNumber, |
||
198 | MeasuresOfInterest.AverageChangeOverTime); |
||
199 | |||
200 | try |
||
201 | { |
||
202 | List<string> okInterfaceTypes = new List<string>(NetworkInterfaceTypes.Split(',')); |
||
203 | |||
204 | IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces(); |
||
205 | foreach (NetworkInterface nic in nics) |
||
206 | { |
||
207 | if (nic.OperationalStatus != OperationalStatus.Up) |
||
208 | continue; |
||
209 | |||
210 | string nicInterfaceType = nic.NetworkInterfaceType.ToString(); |
||
211 | if (!okInterfaceTypes.Contains(nicInterfaceType)) |
||
212 | { |
||
213 | m_log.DebugFormat("{0} Not including stats for network interface '{1}' of type '{2}'.", |
||
214 | LogHeader, nic.Name, nicInterfaceType); |
||
215 | m_log.DebugFormat("{0} To include, add to comma separated list in [Monitoring]NetworkInterfaceTypes={1}", |
||
216 | LogHeader, NetworkInterfaceTypes); |
||
217 | continue; |
||
218 | } |
||
219 | |||
220 | if (nic.Supports(NetworkInterfaceComponent.IPv4)) |
||
221 | { |
||
222 | IPv4InterfaceStatistics nicStats = nic.GetIPv4Statistics(); |
||
223 | if (nicStats != null) |
||
224 | { |
||
225 | MakeStat("BytesRcvd/" + nic.Name, nic.Name, "KB", ContainerNetwork, |
||
226 | (s) => { LookupNic(s, (ns) => { return ns.BytesReceived; }, 1024.0); }); |
||
227 | MakeStat("BytesSent/" + nic.Name, nic.Name, "KB", ContainerNetwork, |
||
228 | (s) => { LookupNic(s, (ns) => { return ns.BytesSent; }, 1024.0); }); |
||
229 | MakeStat("TotalBytes/" + nic.Name, nic.Name, "KB", ContainerNetwork, |
||
230 | (s) => { LookupNic(s, (ns) => { return ns.BytesSent + ns.BytesReceived; }, 1024.0); }); |
||
231 | } |
||
232 | } |
||
233 | // TODO: add IPv6 (it may actually happen someday) |
||
234 | } |
||
235 | } |
||
236 | catch (Exception e) |
||
237 | { |
||
238 | m_log.ErrorFormat("{0} Exception creating 'Network Interface': {1}", LogHeader, e); |
||
239 | } |
||
240 | |||
241 | MakeStat("ProcessMemory", null, "MB", ContainerMemory, |
||
242 | (s) => { s.Value = Math.Round(Process.GetCurrentProcess().WorkingSet64 / 1024d / 1024d, 3); }); |
||
243 | MakeStat("HeapMemory", null, "MB", ContainerMemory, |
||
244 | (s) => { s.Value = Math.Round(GC.GetTotalMemory(false) / 1024d / 1024d, 3); }); |
||
245 | MakeStat("LastHeapAllocationRate", null, "MB/sec", ContainerMemory, |
||
246 | (s) => { s.Value = Math.Round(MemoryWatchdog.LastHeapAllocationRate * 1000d / 1024d / 1024d, 3); }); |
||
247 | MakeStat("AverageHeapAllocationRate", null, "MB/sec", ContainerMemory, |
||
248 | (s) => { s.Value = Math.Round(MemoryWatchdog.AverageHeapAllocationRate * 1000d / 1024d / 1024d, 3); }); |
||
249 | } |
||
250 | |||
251 | // Notes on performance counters: |
||
252 | // "How To Read Performance Counters": http://blogs.msdn.com/b/bclteam/archive/2006/06/02/618156.aspx |
||
253 | // "How to get the CPU Usage in C#": http://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c |
||
254 | // "Mono Performance Counters": http://www.mono-project.com/Mono_Performance_Counters |
||
255 | private delegate double PerfCounterNextValue(); |
||
256 | private void GetNextValue(Stat stat, PerfCounterControl perfControl) |
||
257 | { |
||
258 | GetNextValue(stat, perfControl, 1.0); |
||
259 | } |
||
260 | private void GetNextValue(Stat stat, PerfCounterControl perfControl, double factor) |
||
261 | { |
||
262 | if (Util.EnvironmentTickCountSubtract(perfControl.lastFetch) > performanceCounterSampleInterval) |
||
263 | { |
||
264 | if (perfControl != null && perfControl.perfCounter != null) |
||
265 | { |
||
266 | try |
||
267 | { |
||
268 | // Kludge for factor to run double duty. If -1, subtract the value from one |
||
269 | if (factor == -1) |
||
270 | stat.Value = 1 - perfControl.perfCounter.NextValue(); |
||
271 | else |
||
272 | stat.Value = perfControl.perfCounter.NextValue() / factor; |
||
273 | } |
||
274 | catch (Exception e) |
||
275 | { |
||
276 | m_log.ErrorFormat("{0} Exception on NextValue fetching {1}: {2}", LogHeader, stat.Name, e); |
||
277 | } |
||
278 | perfControl.lastFetch = Util.EnvironmentTickCount(); |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | // Lookup the nic that goes with this stat and set the value by using a fetch action. |
||
284 | // Not sure about closure with delegates inside delegates. |
||
285 | private delegate double GetIPv4StatValue(IPv4InterfaceStatistics interfaceStat); |
||
286 | private void LookupNic(Stat stat, GetIPv4StatValue getter, double factor) |
||
287 | { |
||
288 | // Get the one nic that has the name of this stat |
||
289 | IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where( |
||
290 | (network) => network.Name == stat.Description); |
||
291 | try |
||
292 | { |
||
293 | foreach (NetworkInterface nic in nics) |
||
294 | { |
||
295 | IPv4InterfaceStatistics intrStats = nic.GetIPv4Statistics(); |
||
296 | if (intrStats != null) |
||
297 | { |
||
298 | double newVal = Math.Round(getter(intrStats) / factor, 3); |
||
299 | stat.Value = newVal; |
||
300 | } |
||
301 | break; |
||
302 | } |
||
303 | } |
||
304 | catch |
||
305 | { |
||
306 | // There are times interfaces go away so we just won't update the stat for this |
||
307 | m_log.ErrorFormat("{0} Exception fetching stat on interface '{1}'", LogHeader, stat.Description); |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | public class ServerStatsAggregator : Stat |
||
313 | { |
||
314 | public ServerStatsAggregator( |
||
315 | string shortName, |
||
316 | string name, |
||
317 | string description, |
||
318 | string unitName, |
||
319 | string category, |
||
320 | string container |
||
321 | ) |
||
322 | : base( |
||
323 | shortName, |
||
324 | name, |
||
325 | description, |
||
326 | unitName, |
||
327 | category, |
||
328 | container, |
||
329 | StatType.Push, |
||
330 | MeasuresOfInterest.None, |
||
331 | null, |
||
332 | StatVerbosity.Info) |
||
333 | { |
||
334 | } |
||
335 | public override string ToConsoleString() |
||
336 | { |
||
337 | StringBuilder sb = new StringBuilder(); |
||
338 | |||
339 | return sb.ToString(); |
||
340 | } |
||
341 | |||
342 | public override OSDMap ToOSDMap() |
||
343 | { |
||
344 | OSDMap ret = new OSDMap(); |
||
345 | |||
346 | return ret; |
||
347 | } |
||
348 | } |
||
349 | } |