From ab6dc478188e1609d9656e65d410d08270066bf0 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Fri, 13 Aug 2010 23:15:11 +0100 Subject: refactor: move binary statistics logging from scene into separate region module --- .../Statistics/Logging/BinaryLoggingModule.cs | 169 +++++++++++++++++++++ OpenSim/Region/Framework/Scenes/Scene.Inventory.cs | 9 +- OpenSim/Region/Framework/Scenes/Scene.cs | 113 +------------- 3 files changed, 181 insertions(+), 110 deletions(-) create mode 100644 OpenSim/Region/CoreModules/Framework/Statistics/Logging/BinaryLoggingModule.cs diff --git a/OpenSim/Region/CoreModules/Framework/Statistics/Logging/BinaryLoggingModule.cs b/OpenSim/Region/CoreModules/Framework/Statistics/Logging/BinaryLoggingModule.cs new file mode 100644 index 0000000..b75a700 --- /dev/null +++ b/OpenSim/Region/CoreModules/Framework/Statistics/Logging/BinaryLoggingModule.cs @@ -0,0 +1,169 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSimulator Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using log4net; +using Mono.Addins; +using Nini.Config; +using OpenMetaverse; +using OpenMetaverse.Packets; +using OpenSim.Framework; +using OpenSim.Region.Framework; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.Avatar.Attachments +{ + [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BinaryLoggingModule")] + public class BinaryLoggingModule : INonSharedRegionModule + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + protected bool m_collectStats; + protected Scene m_scene = null; + + public string Name { get { return "Binary Statistics Logging Module"; } } + public Type ReplaceableInterface { get { return null; } } + + public void Initialise(IConfigSource source) + { + try + { + IConfig statConfig = source.Configs["Statistics.Binary"]; + if (statConfig.Contains("enabled") && statConfig.GetBoolean("enabled")) + { + if (statConfig.Contains("collect_region_stats")) + { + if (statConfig.GetBoolean("collect_region_stats")) + { + m_collectStats = true; + } + } + if (statConfig.Contains("region_stats_period_seconds")) + { + m_statLogPeriod = TimeSpan.FromSeconds(statConfig.GetInt("region_stats_period_seconds")); + } + if (statConfig.Contains("stats_dir")) + { + m_statsDir = statConfig.GetString("stats_dir"); + } + } + } + catch + { + // if it doesn't work, we don't collect anything + } + } + + public void AddRegion(Scene scene) + { + m_scene = scene; + } + + public void RemoveRegion(Scene scene) + { + } + + public void RegionLoaded(Scene scene) + { + if (m_collectStats) + m_scene.StatsReporter.OnSendStatsResult += LogSimStats; + } + + public void Close() + { + } + + public class StatLogger + { + public DateTime StartTime; + public string Path; + public System.IO.BinaryWriter Log; + } + + static StatLogger m_statLog = null; + static TimeSpan m_statLogPeriod = TimeSpan.FromSeconds(300); + static string m_statsDir = String.Empty; + static Object m_statLockObject = new Object(); + + private void LogSimStats(SimStats stats) + { + SimStatsPacket pack = new SimStatsPacket(); + pack.Region = new SimStatsPacket.RegionBlock(); + pack.Region.RegionX = stats.RegionX; + pack.Region.RegionY = stats.RegionY; + pack.Region.RegionFlags = stats.RegionFlags; + pack.Region.ObjectCapacity = stats.ObjectCapacity; + //pack.Region = //stats.RegionBlock; + pack.Stat = stats.StatsBlock; + pack.Header.Reliable = false; + + // note that we are inside the reporter lock when called + DateTime now = DateTime.Now; + + // hide some time information into the packet + pack.Header.Sequence = (uint)now.Ticks; + + lock (m_statLockObject) // m_statLog is shared so make sure there is only executer here + { + try + { + if (m_statLog == null || now > m_statLog.StartTime + m_statLogPeriod) + { + // First log file or time has expired, start writing to a new log file + if (m_statLog != null && m_statLog.Log != null) + { + m_statLog.Log.Close(); + } + m_statLog = new StatLogger(); + m_statLog.StartTime = now; + m_statLog.Path = (m_statsDir.Length > 0 ? m_statsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "") + + String.Format("stats-{0}.log", now.ToString("yyyyMMddHHmmss")); + m_statLog.Log = new BinaryWriter(File.Open(m_statLog.Path, FileMode.Append, FileAccess.Write)); + } + + // Write the serialized data to disk + if (m_statLog != null && m_statLog.Log != null) + m_statLog.Log.Write(pack.ToBytes()); + } + catch (Exception ex) + { + m_log.Error("statistics gathering failed: " + ex.Message, ex); + if (m_statLog != null && m_statLog.Log != null) + { + m_statLog.Log.Close(); + } + m_statLog = null; + } + } + return; + } + } +} diff --git a/OpenSim/Region/Framework/Scenes/Scene.Inventory.cs b/OpenSim/Region/Framework/Scenes/Scene.Inventory.cs index 4f5a65e..9963225 100644 --- a/OpenSim/Region/Framework/Scenes/Scene.Inventory.cs +++ b/OpenSim/Region/Framework/Scenes/Scene.Inventory.cs @@ -834,7 +834,7 @@ namespace OpenSim.Region.Framework.Scenes ScenePresence presence; if (TryGetScenePresence(remoteClient.AgentId, out presence)) { - byte[] data = null; +// byte[] data = null; AssetBase asset = new AssetBase(); asset.FullID = olditemID; @@ -842,8 +842,10 @@ namespace OpenSim.Region.Framework.Scenes asset.Name = name; asset.Description = description; - CreateNewInventoryItem(remoteClient, remoteClient.AgentId.ToString(), folderID, name, 0, callbackID, asset, invType, (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, Util.UnixTimeSinceEpoch()); - + CreateNewInventoryItem( + remoteClient, remoteClient.AgentId.ToString(), folderID, name, 0, callbackID, asset, invType, + (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, + (uint)PermissionMask.All, (uint)PermissionMask.All, Util.UnixTimeSinceEpoch()); } else { @@ -876,7 +878,6 @@ namespace OpenSim.Region.Framework.Scenes InventoryService.DeleteFolders(remoteClient.AgentId, folderIDs); } - /// /// Send the details of a prim's inventory to the client. /// diff --git a/OpenSim/Region/Framework/Scenes/Scene.cs b/OpenSim/Region/Framework/Scenes/Scene.cs index 5f6748e..f62851a 100644 --- a/OpenSim/Region/Framework/Scenes/Scene.cs +++ b/OpenSim/Region/Framework/Scenes/Scene.cs @@ -481,73 +481,6 @@ namespace OpenSim.Region.Framework.Scenes #endregion - #region BinaryStats - - public class StatLogger - { - public DateTime StartTime; - public string Path; - public System.IO.BinaryWriter Log; - } - static StatLogger m_statLog = null; - static TimeSpan m_statLogPeriod = TimeSpan.FromSeconds(300); - static string m_statsDir = String.Empty; - static Object m_statLockObject = new Object(); - private void LogSimStats(SimStats stats) - { - SimStatsPacket pack = new SimStatsPacket(); - pack.Region = new SimStatsPacket.RegionBlock(); - pack.Region.RegionX = stats.RegionX; - pack.Region.RegionY = stats.RegionY; - pack.Region.RegionFlags = stats.RegionFlags; - pack.Region.ObjectCapacity = stats.ObjectCapacity; - //pack.Region = //stats.RegionBlock; - pack.Stat = stats.StatsBlock; - pack.Header.Reliable = false; - - // note that we are inside the reporter lock when called - DateTime now = DateTime.Now; - - // hide some time information into the packet - pack.Header.Sequence = (uint)now.Ticks; - - lock (m_statLockObject) // m_statLog is shared so make sure there is only executer here - { - try - { - if (m_statLog == null || now > m_statLog.StartTime + m_statLogPeriod) - { - // First log file or time has expired, start writing to a new log file - if (m_statLog != null && m_statLog.Log != null) - { - m_statLog.Log.Close(); - } - m_statLog = new StatLogger(); - m_statLog.StartTime = now; - m_statLog.Path = (m_statsDir.Length > 0 ? m_statsDir + System.IO.Path.DirectorySeparatorChar.ToString() : "") - + String.Format("stats-{0}.log", now.ToString("yyyyMMddHHmmss")); - m_statLog.Log = new BinaryWriter(File.Open(m_statLog.Path, FileMode.Append, FileAccess.Write)); - } - - // Write the serialized data to disk - if (m_statLog != null && m_statLog.Log != null) - m_statLog.Log.Write(pack.ToBytes()); - } - catch (Exception ex) - { - m_log.Error("statistics gathering failed: " + ex.Message, ex); - if (m_statLog != null && m_statLog.Log != null) - { - m_statLog.Log.Close(); - } - m_statLog = null; - } - } - return; - } - - #endregion - #region Constructors public Scene(RegionInfo regInfo, AgentCircuitManager authen, @@ -752,38 +685,6 @@ namespace OpenSim.Region.Framework.Scenes } m_strictAccessControl = startupConfig.GetBoolean("StrictAccessControl", m_strictAccessControl); - - #region BinaryStats - - try - { - IConfig statConfig = m_config.Configs["Statistics.Binary"]; - if (statConfig.Contains("enabled") && statConfig.GetBoolean("enabled")) - { - if (statConfig.Contains("collect_region_stats")) - { - if (statConfig.GetBoolean("collect_region_stats")) - { - // if enabled, add us to the event. If not enabled, I won't get called - StatsReporter.OnSendStatsResult += LogSimStats; - } - } - if (statConfig.Contains("region_stats_period_seconds")) - { - m_statLogPeriod = TimeSpan.FromSeconds(statConfig.GetInt("region_stats_period_seconds")); - } - if (statConfig.Contains("stats_dir")) - { - m_statsDir = statConfig.GetString("stats_dir"); - } - } - } - catch - { - // if it doesn't work, we don't collect anything - } - - #endregion BinaryStats } catch { @@ -2652,7 +2553,7 @@ namespace OpenSim.Region.Framework.Scenes if (!VerifyClient(aCircuit, ep, out vialogin)) { // uh-oh, this is fishy - m_log.WarnFormat("[Scene]: Agent {0} with session {1} connecting with unidentified end point {2}. Refusing service.", + m_log.WarnFormat("[SCENE]: Agent {0} with session {1} connecting with unidentified end point {2}. Refusing service.", client.AgentId, client.SessionId, ep.ToString()); try { @@ -2660,13 +2561,13 @@ namespace OpenSim.Region.Framework.Scenes } catch (Exception e) { - m_log.DebugFormat("[Scene]: Exception while closing aborted client: {0}", e.StackTrace); + m_log.DebugFormat("[SCENE]: Exception while closing aborted client: {0}", e.StackTrace); } return; } } - m_log.Debug("[Scene] Adding new agent " + client.Name + " to scene " + RegionInfo.RegionName); + m_log.Debug("[SCENE]: Adding new agent " + client.Name + " to scene " + RegionInfo.RegionName); ScenePresence sp = CreateAndAddScenePresence(client); if (aCircuit != null) @@ -2695,7 +2596,7 @@ namespace OpenSim.Region.Framework.Scenes // Do the verification here if ((aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaLogin) != 0) { - m_log.DebugFormat("[Scene]: Incoming client {0} {1} in region {2} via Login", aCircuit.firstname, aCircuit.lastname, RegionInfo.RegionName); + m_log.DebugFormat("[SCENE]: Incoming client {0} {1} in region {2} via Login", aCircuit.firstname, aCircuit.lastname, RegionInfo.RegionName); vialogin = true; IUserAgentVerificationModule userVerification = RequestModuleInterface(); if (userVerification != null && ep != null) @@ -2705,11 +2606,11 @@ namespace OpenSim.Region.Framework.Scenes if (!userVerification.VerifyClient(aCircuit, /*ep.Address.ToString() */ addr.ToString())) { // uh-oh, this is fishy - m_log.DebugFormat("[Scene]: User Client Verification for {0} {1} in {2} returned false", aCircuit.firstname, aCircuit.lastname, RegionInfo.RegionName); + m_log.DebugFormat("[SCENE]: User Client Verification for {0} {1} in {2} returned false", aCircuit.firstname, aCircuit.lastname, RegionInfo.RegionName); return false; } else - m_log.DebugFormat("[Scene]: User Client Verification for {0} {1} in {2} returned true", aCircuit.firstname, aCircuit.lastname, RegionInfo.RegionName); + m_log.DebugFormat("[SCENE]: User Client Verification for {0} {1} in {2} returned true", aCircuit.firstname, aCircuit.lastname, RegionInfo.RegionName); } } @@ -2740,7 +2641,7 @@ namespace OpenSim.Region.Framework.Scenes } catch (Exception e) { - m_log.DebugFormat("[Scene]: Exception while closing aborted client: {0}", e.StackTrace); + m_log.DebugFormat("[SCENE]: Exception while closing aborted client: {0}", e.StackTrace); } } else -- cgit v1.1