From cda127e30f0049cda21137363e4d759fd7fd4959 Mon Sep 17 00:00:00 2001 From: teravus Date: Fri, 9 Nov 2012 23:55:30 -0500 Subject: * Prep work switching the GetMeshModule over to a poll service. * This still has the image throttler in it.. as is... so it's not suitable for live yet.... The throttler keeps track of the task throttle but doesn't balance the UDP throttle yet. --- .../ClientStack/Linden/Caps/GetMeshModule.cs | 362 ++++++++++++++++++++- .../ClientStack/Linden/Caps/GetTextureModule.cs | 121 +++---- 2 files changed, 410 insertions(+), 73 deletions(-) (limited to 'OpenSim/Region/ClientStack/Linden') diff --git a/OpenSim/Region/ClientStack/Linden/Caps/GetMeshModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/GetMeshModule.cs index 0d7b1fc..8deff81 100644 --- a/OpenSim/Region/ClientStack/Linden/Caps/GetMeshModule.cs +++ b/OpenSim/Region/ClientStack/Linden/Caps/GetMeshModule.cs @@ -27,11 +27,14 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Collections.Specialized; using System.Reflection; using System.IO; +using System.Threading; using System.Web; using Mono.Addins; +using OpenSim.Framework.Monitoring; using log4net; using Nini.Config; using OpenMetaverse; @@ -57,8 +60,42 @@ namespace OpenSim.Region.ClientStack.Linden private IAssetService m_AssetService; private bool m_Enabled = true; private string m_URL; + struct aPollRequest + { + public PollServiceMeshEventArgs thepoll; + public UUID reqID; + public Hashtable request; + } + + public class aPollResponse + { + public Hashtable response; + public int bytes; + } + + + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + private static GetMeshHandler m_getMeshHandler; + + private IAssetService m_assetService = null; + + private Dictionary m_capsDict = new Dictionary(); + private static Thread[] m_workerThreads = null; + + private static OpenMetaverse.BlockingQueue m_queue = + new OpenMetaverse.BlockingQueue(); + + private Dictionary m_pollservices = new Dictionary(); - #region IRegionModuleBase Members + #region ISharedRegionModule Members + + ~GetMeshModule() + { + foreach (Thread t in m_workerThreads) + Watchdog.AbortThread(t.ManagedThreadId); + + } public Type ReplaceableInterface { @@ -83,6 +120,8 @@ namespace OpenSim.Region.ClientStack.Linden return; m_scene = pScene; + + m_assetService = pScene.AssetService; } public void RemoveRegion(Scene scene) @@ -91,6 +130,9 @@ namespace OpenSim.Region.ClientStack.Linden return; m_scene.EventManager.OnRegisterCaps -= RegisterCaps; + m_scene.EventManager.OnDeregisterCaps -= DeregisterCaps; + m_scene.EventManager.OnThrottleUpdate -= ThrottleUpdate; + m_scene = null; } @@ -101,6 +143,27 @@ namespace OpenSim.Region.ClientStack.Linden m_AssetService = m_scene.RequestModuleInterface(); m_scene.EventManager.OnRegisterCaps += RegisterCaps; + // We'll reuse the same handler for all requests. + m_getMeshHandler = new GetMeshHandler(m_assetService); + m_scene.EventManager.OnDeregisterCaps += DeregisterCaps; + m_scene.EventManager.OnThrottleUpdate += ThrottleUpdate; + + if (m_workerThreads == null) + { + m_workerThreads = new Thread[2]; + + for (uint i = 0; i < 2; i++) + { + m_workerThreads[i] = Watchdog.StartThread(DoMeshRequests, + String.Format("MeshWorkerThread{0}", i), + ThreadPriority.Normal, + false, + false, + null, + int.MaxValue); + } + } + } @@ -110,25 +173,209 @@ namespace OpenSim.Region.ClientStack.Linden #endregion + private void DoMeshRequests() + { + while (true) + { + aPollRequest poolreq = m_queue.Dequeue(); + + poolreq.thepoll.Process(poolreq); + } + } + + // Now we know when the throttle is changed by the client in the case of a root agent or by a neighbor region in the case of a child agent. + public void ThrottleUpdate(ScenePresence p) + { + byte[] throttles = p.ControllingClient.GetThrottlesPacked(1); + UUID user = p.UUID; + int imagethrottle = ExtractTaskThrottle(throttles); + PollServiceMeshEventArgs args; + if (m_pollservices.TryGetValue(user, out args)) + { + args.UpdateThrottle(imagethrottle); + } + } + + private int ExtractTaskThrottle(byte[] pthrottles) + { + + byte[] adjData; + int pos = 0; + + if (!BitConverter.IsLittleEndian) + { + byte[] newData = new byte[7 * 4]; + Buffer.BlockCopy(pthrottles, 0, newData, 0, 7 * 4); + + for (int i = 0; i < 7; i++) + Array.Reverse(newData, i * 4, 4); + + adjData = newData; + } + else + { + adjData = pthrottles; + } + + // 0.125f converts from bits to bytes + //int resend = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); + //pos += 4; + // int land = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); + //pos += 4; + // int wind = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); + // pos += 4; + // int cloud = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); + // pos += 4; + pos += 16; + int task = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); + // pos += 4; + //int texture = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); //pos += 4; + //int asset = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); + return task; + } + + private class PollServiceMeshEventArgs : PollServiceEventArgs + { + private List requests = + new List(); + private Dictionary responses = + new Dictionary(); + + private Scene m_scene; + private CapsDataThrottler m_throttler = new CapsDataThrottler(100000, 1400000, 10000); + public PollServiceMeshEventArgs(UUID pId, Scene scene) : + base(null, null, null, null, pId, int.MaxValue) + { + m_scene = scene; + // x is request id, y is userid + HasEvents = (x, y) => + { + lock (responses) + { + bool ret = m_throttler.hasEvents(x, responses); + m_throttler.ProcessTime(); + return ret; + + } + }; + GetEvents = (x, y) => + { + lock (responses) + { + try + { + return responses[x].response; + } + finally + { + responses.Remove(x); + } + } + }; + // x is request id, y is request data hashtable + Request = (x, y) => + { + aPollRequest reqinfo = new aPollRequest(); + reqinfo.thepoll = this; + reqinfo.reqID = x; + reqinfo.request = y; + + m_queue.Enqueue(reqinfo); + }; + + // this should never happen except possible on shutdown + NoEvents = (x, y) => + { + /* + lock (requests) + { + Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString()); + requests.Remove(request); + } + */ + Hashtable response = new Hashtable(); + + response["int_response_code"] = 500; + response["str_response_string"] = "Script timeout"; + response["content_type"] = "text/plain"; + response["keepalive"] = false; + response["reusecontext"] = false; + + return response; + }; + } + + public void Process(aPollRequest requestinfo) + { + Hashtable response; + + UUID requestID = requestinfo.reqID; + + // If the avatar is gone, don't bother to get the texture + if (m_scene.GetScenePresence(Id) == null) + { + response = new Hashtable(); + + response["int_response_code"] = 500; + response["str_response_string"] = "Script timeout"; + response["content_type"] = "text/plain"; + response["keepalive"] = false; + response["reusecontext"] = false; + + lock (responses) + responses[requestID] = new aPollResponse() { bytes = 0, response = response }; + + return; + } + + response = m_getMeshHandler.Handle(requestinfo.request); + lock (responses) + { + responses[requestID] = new aPollResponse() + { + bytes = (int)response["int_bytes"], + response = response + }; + + } + m_throttler.ProcessTime(); + } + + internal void UpdateThrottle(int pimagethrottle) + { + m_throttler.ThrottleBytes = pimagethrottle; + } + } public void RegisterCaps(UUID agentID, Caps caps) { // UUID capID = UUID.Random(); - - //caps.RegisterHandler("GetTexture", new StreamHandler("GET", "/CAPS/" + capID, ProcessGetTexture)); if (m_URL == "localhost") { -// m_log.DebugFormat("[GETMESH]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName); - GetMeshHandler gmeshHandler = new GetMeshHandler(m_AssetService); - IRequestHandler reqHandler - = new RestHTTPHandler( - "GET", - "/CAPS/" + UUID.Random(), - httpMethod => gmeshHandler.ProcessGetMesh(httpMethod, UUID.Zero, null), - "GetMesh", - agentID.ToString()); + string capUrl = "/CAPS/" + UUID.Random() + "/"; + + // Register this as a poll service + PollServiceMeshEventArgs args = new PollServiceMeshEventArgs(agentID, m_scene); + + args.Type = PollServiceEventArgs.EventType.Mesh; + MainServer.Instance.AddPollServiceHTTPHandler(capUrl, args); + + string hostName = m_scene.RegionInfo.ExternalHostName; + uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port; + string protocol = "http"; - caps.RegisterHandler("GetMesh", reqHandler); + if (MainServer.Instance.UseSSL) + { + hostName = MainServer.Instance.SSLCommonName; + port = MainServer.Instance.SSLPort; + protocol = "https"; + } + caps.RegisterHandler("GetMesh", String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, capUrl)); + m_pollservices.Add(agentID, args); + m_capsDict[agentID] = capUrl; + + + } else { @@ -136,6 +383,95 @@ namespace OpenSim.Region.ClientStack.Linden caps.RegisterHandler("GetMesh", m_URL); } } + private void DeregisterCaps(UUID agentID, Caps caps) + { + string capUrl; + PollServiceMeshEventArgs args; + if (m_capsDict.TryGetValue(agentID, out capUrl)) + { + MainServer.Instance.RemoveHTTPHandler("", capUrl); + m_capsDict.Remove(agentID); + } + if (m_pollservices.TryGetValue(agentID, out args)) + { + m_pollservices.Remove(agentID); + } + } + + internal sealed class CapsDataThrottler + { + + private volatile int currenttime = 0; + private volatile int lastTimeElapsed = 0; + private volatile int BytesSent = 0; + private int oversizedImages = 0; + public CapsDataThrottler(int pBytes, int max, int min) + { + ThrottleBytes = pBytes; + lastTimeElapsed = Util.EnvironmentTickCount(); + } + public bool hasEvents(UUID key, Dictionary responses) + { + PassTime(); + // Note, this is called IN LOCK + bool haskey = responses.ContainsKey(key); + if (!haskey) + { + return false; + } + aPollResponse response; + if (responses.TryGetValue(key, out response)) + { + + // Normal + if (BytesSent + response.bytes <= ThrottleBytes) + { + BytesSent += response.bytes; + //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + 1000, unlockyn = false }; + //m_actions.Add(timeBasedAction); + return true; + } + // Big textures + else if (response.bytes > ThrottleBytes && oversizedImages <= ((ThrottleBytes % 50000) + 1)) + { + Interlocked.Increment(ref oversizedImages); + BytesSent += response.bytes; + //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + (((response.bytes % ThrottleBytes)+1)*1000) , unlockyn = false }; + //m_actions.Add(timeBasedAction); + return true; + } + else + { + return false; + } + } + + return haskey; + } + public void ProcessTime() + { + PassTime(); + } + + + private void PassTime() + { + currenttime = Util.EnvironmentTickCount(); + int timeElapsed = Util.EnvironmentTickCountSubtract(currenttime, lastTimeElapsed); + //processTimeBasedActions(responses); + if (Util.EnvironmentTickCountSubtract(currenttime, timeElapsed) >= 1000) + { + lastTimeElapsed = Util.EnvironmentTickCount(); + BytesSent -= ThrottleBytes; + if (BytesSent < 0) BytesSent = 0; + if (BytesSent < ThrottleBytes) + { + oversizedImages = 0; + } + } + } + public int ThrottleBytes; + } } } diff --git a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs index 8cba6c8..c8c709a 100644 --- a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs +++ b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs @@ -364,80 +364,81 @@ namespace OpenSim.Region.ClientStack.Linden poolreq.thepoll.Process(poolreq); } } - } - - internal sealed class CapsDataThrottler - { - - private volatile int currenttime = 0; - private volatile int lastTimeElapsed = 0; - private volatile int BytesSent = 0; - private int oversizedImages = 0; - public CapsDataThrottler(int pBytes, int max, int min) - { - ThrottleBytes = pBytes; - lastTimeElapsed = Util.EnvironmentTickCount(); - } - public bool hasEvents(UUID key, Dictionary responses) + internal sealed class CapsDataThrottler { - PassTime(); - // Note, this is called IN LOCK - bool haskey = responses.ContainsKey(key); - if (!haskey) + + private volatile int currenttime = 0; + private volatile int lastTimeElapsed = 0; + private volatile int BytesSent = 0; + private int oversizedImages = 0; + public CapsDataThrottler(int pBytes, int max, int min) { - return false; + ThrottleBytes = pBytes; + lastTimeElapsed = Util.EnvironmentTickCount(); } - GetTextureModule.aPollResponse response; - if (responses.TryGetValue(key,out response)) + public bool hasEvents(UUID key, Dictionary responses) { - - // Normal - if (BytesSent + response.bytes <= ThrottleBytes) + PassTime(); + // Note, this is called IN LOCK + bool haskey = responses.ContainsKey(key); + if (!haskey) { - BytesSent += response.bytes; - //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + 1000, unlockyn = false }; - //m_actions.Add(timeBasedAction); - return true; - } - // Big textures - else if (response.bytes > ThrottleBytes && oversizedImages <= ((ThrottleBytes%50000) + 1)) - { - Interlocked.Increment(ref oversizedImages); - BytesSent += response.bytes; - //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + (((response.bytes % ThrottleBytes)+1)*1000) , unlockyn = false }; - //m_actions.Add(timeBasedAction); - return true; + return false; } - else + GetTextureModule.aPollResponse response; + if (responses.TryGetValue(key, out response)) { - return false; + + // Normal + if (BytesSent + response.bytes <= ThrottleBytes) + { + BytesSent += response.bytes; + //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + 1000, unlockyn = false }; + //m_actions.Add(timeBasedAction); + return true; + } + // Big textures + else if (response.bytes > ThrottleBytes && oversizedImages <= ((ThrottleBytes % 50000) + 1)) + { + Interlocked.Increment(ref oversizedImages); + BytesSent += response.bytes; + //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + (((response.bytes % ThrottleBytes)+1)*1000) , unlockyn = false }; + //m_actions.Add(timeBasedAction); + return true; + } + else + { + return false; + } } + + return haskey; + } + public void ProcessTime() + { + PassTime(); } - return haskey; - } - public void ProcessTime() - { - PassTime(); - } - - - private void PassTime() - { - currenttime = Util.EnvironmentTickCount(); - int timeElapsed = Util.EnvironmentTickCountSubtract(currenttime, lastTimeElapsed); - //processTimeBasedActions(responses); - if (Util.EnvironmentTickCountSubtract(currenttime, timeElapsed) >= 1000) + + private void PassTime() { - lastTimeElapsed = Util.EnvironmentTickCount(); - BytesSent -= ThrottleBytes; - if (BytesSent < 0) BytesSent = 0; - if (BytesSent < ThrottleBytes) + currenttime = Util.EnvironmentTickCount(); + int timeElapsed = Util.EnvironmentTickCountSubtract(currenttime, lastTimeElapsed); + //processTimeBasedActions(responses); + if (Util.EnvironmentTickCountSubtract(currenttime, timeElapsed) >= 1000) { - oversizedImages = 0; + lastTimeElapsed = Util.EnvironmentTickCount(); + BytesSent -= ThrottleBytes; + if (BytesSent < 0) BytesSent = 0; + if (BytesSent < ThrottleBytes) + { + oversizedImages = 0; + } } } + public int ThrottleBytes; } - public int ThrottleBytes; } + + } -- cgit v1.1