From 6e0c79b8fe76c7d2c983cb7b258a75a3300e78f2 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Thu, 1 Oct 2009 17:42:13 -0700 Subject: * Rewrote LLImageManager to use a real priority queue and hold minimal state * Rewrote the logic in J2KImage.RunUpdate() * Added a default avatar texture (I made it myself) --- .../Region/ClientStack/LindenUDP/LLImageManager.cs | 324 ++++++++------------- 1 file changed, 117 insertions(+), 207 deletions(-) (limited to 'OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs') diff --git a/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs b/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs index b039049..a82eaae 100644 --- a/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs +++ b/OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs @@ -27,26 +27,28 @@ using System; using System.Threading; +using System.Collections; using System.Collections.Generic; +using System.Reflection; using OpenMetaverse; using OpenMetaverse.Imaging; using OpenSim.Framework; using OpenSim.Region.Framework.Interfaces; using OpenSim.Services.Interfaces; using log4net; -using System.Reflection; namespace OpenSim.Region.ClientStack.LindenUDP { - public class LLImageManager { - - //Public interfaces: - //Constructor - (LLClientView, IAssetCache, IJ2KDecoder); - //void EnqueueReq - (TextureRequestArgs) - //ProcessImageQueue - //Close + private sealed class J2KImageComparer : IComparer + { + public int Compare(J2KImage x, J2KImage y) + { + return x.m_requestedPriority.CompareTo(y.m_requestedPriority); + } + } + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private bool m_shuttingdown = false; private long m_lastloopprocessed = 0; @@ -54,28 +56,12 @@ namespace OpenSim.Region.ClientStack.LindenUDP private LLClientView m_client; //Client we're assigned to private IAssetService m_assetCache; //Asset Cache private IJ2KDecoder m_j2kDecodeModule; //Our J2K module + private C5.IntervalHeap m_priorityQueue = new C5.IntervalHeap(10, new J2KImageComparer()); - private readonly AssetBase m_missingsubstitute; //Sustitute for bad decodes - private Dictionary m_imagestore; // Our main image storage dictionary - private SortedList m_priorities; // For fast image lookup based on priority - private Dictionary m_priorityresolver; //Enabling super fast assignment of images with the same priorities - - private const double doubleMinimum = .0000001; - - public int m_outstandingtextures = 0; - //Constructor public LLImageManager(LLClientView client, IAssetService pAssetCache, IJ2KDecoder pJ2kDecodeModule) { - - m_imagestore = new Dictionary(); - m_priorities = new SortedList(); - m_priorityresolver = new Dictionary(); m_client = client; m_assetCache = pAssetCache; - if (pAssetCache != null) - m_missingsubstitute = pAssetCache.Get("5748decc-f629-461c-9a36-a35a221fe21f"); - else - m_log.Error("[ClientView] - couldn't set missing image, all manner of things will probably break"); m_j2kDecodeModule = pJ2kDecodeModule; } @@ -88,174 +74,147 @@ namespace OpenSim.Region.ClientStack.LindenUDP //Make sure we're not shutting down.. if (!m_shuttingdown) { + J2KImage imgrequest; - //Do we already know about this UUID? - if (m_imagestore.ContainsKey(newRequest.RequestedAssetID)) - { - //Check the packet sequence to make sure this isn't older than - //one we've already received - - J2KImage imgrequest = m_imagestore[newRequest.RequestedAssetID]; + // Do a linear search for this texture download + m_priorityQueue.Find(delegate(J2KImage img) { return img.m_requestedUUID == newRequest.RequestedAssetID; }, out imgrequest); - // This is the inbound request sequence number. We can ignore - // "old" ones. - - if (newRequest.requestSequence > imgrequest.m_lastSequence) + if (imgrequest != null) + { + if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f) { + m_log.Debug("[JPEG2000]: (CAN) ID=" + newRequest.RequestedAssetID); - imgrequest.m_lastSequence = newRequest.requestSequence; - - //Check the priority + try { m_priorityQueue.Delete(imgrequest.m_priorityQueueHandle); } + catch (Exception) { } + } + else + { + m_log.DebugFormat("[JPEG2000]: (UPD) ID={0}: D={1}, S={2}, P={3}", + newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority); - double priority = imgrequest.m_requestedPriority; - if (priority != newRequest.Priority) + //Check the packet sequence to make sure this isn't older than + //one we've already received + if (newRequest.requestSequence > imgrequest.m_lastSequence) { - //Remove the old priority - m_priorities.Remove(imgrequest.m_designatedPriorityKey); - //Assign a new unique priority - imgrequest.m_requestedPriority = newRequest.Priority; - imgrequest.m_designatedPriorityKey = AssignPriority(newRequest.RequestedAssetID, newRequest.Priority); - } + //Update the sequence number of the last RequestImage packet + imgrequest.m_lastSequence = newRequest.requestSequence; - //Update the requested discard level - imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; + //Update the requested discard level + imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; - //Update the requested packet number - imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; + //Update the requested packet number + imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; - //Check if this will create an outstanding texture request - bool activated = imgrequest.m_completedSendAtCurrentDiscardLevel; - //Run an update - - imgrequest.RunUpdate(); + //Update the requested priority + imgrequest.m_requestedPriority = newRequest.Priority; + try { m_priorityQueue.Replace(imgrequest.m_priorityQueueHandle, imgrequest); } + catch (Exception) { imgrequest.m_priorityQueueHandle = null; m_priorityQueue.Add(ref imgrequest.m_priorityQueueHandle, imgrequest); } - if (activated && !imgrequest.m_completedSendAtCurrentDiscardLevel && imgrequest.m_decoded) - { - Interlocked.Increment(ref m_outstandingtextures); + //Run an update + imgrequest.RunUpdate(); } } } else { - J2KImage imgrequest = new J2KImage(this); - - //Assign our missing substitute - imgrequest.m_MissingSubstitute = m_missingsubstitute; + if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f) + { + m_log.DebugFormat("[JPEG2000]: (IGN) ID={0}: D={1}, S={2}, P={3}", + newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority); + } + else + { + m_log.DebugFormat("[JPEG2000]: (NEW) ID={0}: D={1}, S={2}, P={3}", + newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority); - //Assign our decoder module - imgrequest.m_j2kDecodeModule = m_j2kDecodeModule; + imgrequest = new J2KImage(this); - //Assign our asset cache module - imgrequest.m_assetCache = m_assetCache; + //Assign our decoder module + imgrequest.m_j2kDecodeModule = m_j2kDecodeModule; - //Assign a priority based on our request - imgrequest.m_designatedPriorityKey = AssignPriority(newRequest.RequestedAssetID, newRequest.Priority); + //Assign our asset cache module + imgrequest.m_assetCache = m_assetCache; - //Assign the requested discard level - imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; + //Assign the requested discard level + imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; - //Assign the requested packet number - imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; + //Assign the requested packet number + imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; - //Assign the requested priority - imgrequest.m_requestedPriority = newRequest.Priority; + //Assign the requested priority + imgrequest.m_requestedPriority = newRequest.Priority; - //Assign the asset uuid - imgrequest.m_requestedUUID = newRequest.RequestedAssetID; + //Assign the asset uuid + imgrequest.m_requestedUUID = newRequest.RequestedAssetID; - m_imagestore.Add(imgrequest.m_requestedUUID, imgrequest); + //Assign the requested priority + imgrequest.m_requestedPriority = newRequest.Priority; - //Run an update - imgrequest.RunUpdate(); + //Add this download to the priority queue + m_priorityQueue.Add(ref imgrequest.m_priorityQueueHandle, imgrequest); + //Run an update + imgrequest.RunUpdate(); + } } } } - private double AssignPriority(UUID pAssetID, double pPriority) + public bool ProcessImageQueue(int count, int maxpack) { - - //First, find out if we can just assign directly - if (m_priorityresolver.ContainsKey((int)pPriority) == false) + //count is the number of textures we want to process in one go. + //As part of this class re-write, that number will probably rise + //since we're processing in a more efficient manner. + + // this can happen during Close() + if (m_client == null) + return false; + + int numCollected = 0; + + //Calculate our threshold + int threshold; + if (m_lastloopprocessed == 0) { - m_priorities.Add((double)((int)pPriority), pAssetID); - m_priorityresolver.Add((int)pPriority, 0); - return (double)((int)pPriority); + if (m_client.PacketHandler == null || m_client.PacketHandler.PacketQueue == null || m_client.PacketHandler.PacketQueue.TextureThrottle == null) + return false; + //This is decent for a semi fast machine, but we'll calculate it more accurately based on time below + threshold = m_client.PacketHandler.PacketQueue.TextureThrottle.Current / 6300; + m_lastloopprocessed = DateTime.Now.Ticks; } else { - //Use the hash lookup goodness of a secondary dictionary to find a free slot - double mFreePriority = ((int)pPriority) + (doubleMinimum * (m_priorityresolver[(int)pPriority] + 1)); - m_priorities[mFreePriority] = pAssetID; - m_priorityresolver[(int)pPriority]++; - return mFreePriority; - } + double throttleseconds = ((double)DateTime.Now.Ticks - (double)m_lastloopprocessed) / (double)TimeSpan.TicksPerSecond; + throttleseconds = throttleseconds * m_client.PacketHandler.PacketQueue.TextureThrottle.Current; + //Average of 1000 bytes per packet + throttleseconds = throttleseconds / 1000; + //Safe-zone multiplier of 2.0 + threshold = (int)(throttleseconds * 2.0); + m_lastloopprocessed = DateTime.Now.Ticks; - } + } - public bool ProcessImageQueue(int count, int maxpack) - { + if (m_client.PacketHandler == null) + return false; - // this can happen during Close() - if (m_client == null) + if (m_client.PacketHandler.PacketQueue == null) return false; - - //Count is the number of textures we want to process in one go. - //As part of this class re-write, that number will probably rise - //since we're processing in a more efficient manner. - - int numCollected = 0; - //Calculate our threshold - int threshold; - if (m_lastloopprocessed == 0) - { - if (m_client.PacketHandler == null || m_client.PacketHandler.PacketQueue == null || m_client.PacketHandler.PacketQueue.TextureThrottle == null) - return false; - //This is decent for a semi fast machine, but we'll calculate it more accurately based on time below - threshold = m_client.PacketHandler.PacketQueue.TextureThrottle.Current / 6300; - m_lastloopprocessed = DateTime.Now.Ticks; - } - else - { - double throttleseconds = ((double)DateTime.Now.Ticks - (double)m_lastloopprocessed) / (double)TimeSpan.TicksPerSecond; - throttleseconds = throttleseconds * m_client.PacketHandler.PacketQueue.TextureThrottle.Current; - - //Average of 1000 bytes per packet - throttleseconds = throttleseconds / 1000; - - //Safe-zone multiplier of 2.0 - threshold = (int)(throttleseconds * 2.0); - m_lastloopprocessed = DateTime.Now.Ticks; - - } - - if (threshold < 10) - { - threshold = 10; - } - - if (m_client.PacketHandler == null) - return false; - - if (m_client.PacketHandler.PacketQueue == null) - return false; - - //First of all make sure our packet queue isn't above our threshold + if (threshold < 10) + threshold = 10; //Uncomment this to see what the texture stack is doing - //m_log.Debug("Queue: " + m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount.ToString() + " Threshold: " + threshold.ToString() + " outstanding: " + m_outstandingtextures.ToString()); - if (m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount < threshold && m_outstandingtextures > 0) - { - bool justreset = false; - - for (int x = m_priorities.Count - 1; x > -1; x--) + //m_log.Debug("Queue: " + m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount.ToString() + " Threshold: " + threshold.ToString() + " outstanding: " + m_outstandingtextures.ToString()); + if (m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount < threshold) + { + while (m_priorityQueue.Count > 0) { - - J2KImage imagereq = m_imagestore[m_priorities.Values[x]]; - if (imagereq.m_decoded == true && !imagereq.m_completedSendAtCurrentDiscardLevel) + J2KImage imagereq = m_priorityQueue.FindMax(); + + if (imagereq.m_decoded == true) { // we need to test this here now that we are dropping assets if (!imagereq.m_hasasset) @@ -265,78 +224,29 @@ namespace OpenSim.Region.ClientStack.LindenUDP continue; } - numCollected++; + ++numCollected; + //SendPackets will send up to ten packets per cycle if (imagereq.SendPackets(m_client, maxpack)) { - //Send complete - if (!imagereq.m_completedSendAtCurrentDiscardLevel) - { - // I think this field imagereq.m_completedSendAtCurrentDiscardLevel - // is completely redundant - //imagereq.m_completedSendAtCurrentDiscardLevel = true; - - Interlocked.Decrement(ref m_outstandingtextures); - - // First and foremost, drop the reference to the asset - // so that the asset doesn't stay in memory forever. - // We'll Get it again from the asset service (usually cache) - // if/when the client requests it again. - // In order not to mess much with the current implementation - // of this management code, we drop only the asset reference - // but keep the image request itself. - imagereq.DropAsset(); - - //Re-assign priority to bottom - //Remove the old priority - m_priorities.Remove(imagereq.m_designatedPriorityKey); - int lowest; - if (m_priorities.Count > 0) - { - lowest = (int)m_priorities.Keys[0]; - lowest--; - } - else - { - lowest = -10000; - } - m_priorities.Add((double)lowest, imagereq.m_requestedUUID); - imagereq.m_designatedPriorityKey = (double)lowest; - if (m_priorityresolver.ContainsKey((int)lowest)) - { - m_priorityresolver[(int)lowest]++; - } - else - { - m_priorityresolver.Add((int)lowest, 0); - } - } - } - if (numCollected == count) - { - break; + // Send complete. Destroy any knowledge of this transfer + try { m_priorityQueue.Delete(imagereq.m_priorityQueueHandle); } + catch (Exception) { } } } - if (numCollected == count || m_outstandingtextures == 0) - break; - if (numCollected % m_outstandingtextures == 0 && !justreset) - { - //We've gotten as much as we can from the stack, - //reset to the top so that we can send MOAR DATA (nomnomnom)! - x = m_priorities.Count - 1; - justreset = true; //prevents us from getting stuck in a loop - } + if (numCollected == count) + break; } } - return m_outstandingtextures != 0; + return m_priorityQueue.Count > 0; } //Faux destructor public void Close() { - + m_shuttingdown = true; m_j2kDecodeModule = null; m_assetCache = null; -- cgit v1.1