opensim – Blame information for rev 1

Subversion Repositories:
Rev:
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.IO;
31 using System.Reflection;
32 using log4net;
33 using Mono.Addins;
34 using Nini.Config;
35 using OpenMetaverse;
36 using OpenMetaverse.Packets;
37 using OpenSim.Framework;
38 using OpenSim.Region.Framework;
39 using OpenSim.Region.Framework.Interfaces;
40 using OpenSim.Region.Framework.Scenes;
41  
42 namespace OpenSim.Region.CoreModules.Framework.Statistics.Logging
43 {
44 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BinaryLoggingModule")]
45 public class BinaryLoggingModule : INonSharedRegionModule
46 {
47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48  
49 protected bool m_collectStats;
50 protected Scene m_scene = null;
51  
52 public string Name { get { return "Binary Statistics Logging Module"; } }
53 public Type ReplaceableInterface { get { return null; } }
54  
55 public void Initialise(IConfigSource source)
56 {
57 try
58 {
59 IConfig statConfig = source.Configs["Statistics.Binary"];
60 if (statConfig != null && statConfig.Contains("enabled") && statConfig.GetBoolean("enabled"))
61 {
62 if (statConfig.Contains("collect_region_stats"))
63 {
64 if (statConfig.GetBoolean("collect_region_stats"))
65 {
66 m_collectStats = true;
67 }
68 }
69 if (statConfig.Contains("region_stats_period_seconds"))
70 {
71 m_statLogPeriod = TimeSpan.FromSeconds(statConfig.GetInt("region_stats_period_seconds"));
72 }
73 if (statConfig.Contains("stats_dir"))
74 {
75 m_statsDir = statConfig.GetString("stats_dir");
76 }
77 }
78 }
79 catch
80 {
81 // if it doesn't work, we don't collect anything
82 }
83 }
84  
85 public void AddRegion(Scene scene)
86 {
87 m_scene = scene;
88 }
89  
90 public void RemoveRegion(Scene scene)
91 {
92 }
93  
94 public void RegionLoaded(Scene scene)
95 {
96 if (m_collectStats)
97 m_scene.StatsReporter.OnSendStatsResult += LogSimStats;
98 }
99  
100 public void Close()
101 {
102 }
103  
104 public class StatLogger
105 {
106 public DateTime StartTime;
107 public string Path;
108 public System.IO.BinaryWriter Log;
109 }
110  
111 static StatLogger m_statLog = null;
112 static TimeSpan m_statLogPeriod = TimeSpan.FromSeconds(300);
113 static string m_statsDir = String.Empty;
114 static Object m_statLockObject = new Object();
115  
116 private void LogSimStats(SimStats stats)
117 {
118 SimStatsPacket pack = new SimStatsPacket();
119 pack.Region = new SimStatsPacket.RegionBlock();
120 pack.Region.RegionX = stats.RegionX;
121 pack.Region.RegionY = stats.RegionY;
122 pack.Region.RegionFlags = stats.RegionFlags;
123 pack.Region.ObjectCapacity = stats.ObjectCapacity;
124 //pack.Region = //stats.RegionBlock;
125 pack.Stat = stats.StatsBlock;
126 pack.Header.Reliable = false;
127  
128 // note that we are inside the reporter lock when called
129 DateTime now = DateTime.Now;
130  
131 // hide some time information into the packet
132 pack.Header.Sequence = (uint)now.Ticks;
133  
134 lock (m_statLockObject) // m_statLog is shared so make sure there is only executer here
135 {
136 try
137 {
138 if (m_statLog == null || now > m_statLog.StartTime + m_statLogPeriod)
139 {
140 // First log file or time has expired, start writing to a new log file
141 if (m_statLog != null && m_statLog.Log != null)
142 {
143 m_statLog.Log.Close();
144 }
145 m_statLog = new StatLogger();
146 m_statLog.StartTime = now;
147 m_statLog.Path = (m_statsDir.Length > 0 ? m_statsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "")
148 + String.Format("stats-{0}.log", now.ToString("yyyyMMddHHmmss"));
149 m_statLog.Log = new BinaryWriter(File.Open(m_statLog.Path, FileMode.Append, FileAccess.Write));
150 }
151  
152 // Write the serialized data to disk
153 if (m_statLog != null && m_statLog.Log != null)
154 m_statLog.Log.Write(pack.ToBytes());
155 }
156 catch (Exception ex)
157 {
158 m_log.Error("statistics gathering failed: " + ex.Message, ex);
159 if (m_statLog != null && m_statLog.Log != null)
160 {
161 m_statLog.Log.Close();
162 }
163 m_statLog = null;
164 }
165 }
166 return;
167 }
168 }
169 }