diff options
author | John Hurliman | 2009-10-01 17:42:13 -0700 |
---|---|---|
committer | John Hurliman | 2009-10-01 17:42:13 -0700 |
commit | 6e0c79b8fe76c7d2c983cb7b258a75a3300e78f2 (patch) | |
tree | 0de3c250460102fc59e9373e4bb33de2afc5ef3a /OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs | |
parent | Fixing LLClientView memory leak (diff) | |
download | opensim-SC_OLD-6e0c79b8fe76c7d2c983cb7b258a75a3300e78f2.zip opensim-SC_OLD-6e0c79b8fe76c7d2c983cb7b258a75a3300e78f2.tar.gz opensim-SC_OLD-6e0c79b8fe76c7d2c983cb7b258a75a3300e78f2.tar.bz2 opensim-SC_OLD-6e0c79b8fe76c7d2c983cb7b258a75a3300e78f2.tar.xz |
* 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)
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/LindenUDP/LLImageManager.cs | 324 |
1 files changed, 117 insertions, 207 deletions
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 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Threading; | 29 | using System.Threading; |
30 | using System.Collections; | ||
30 | using System.Collections.Generic; | 31 | using System.Collections.Generic; |
32 | using System.Reflection; | ||
31 | using OpenMetaverse; | 33 | using OpenMetaverse; |
32 | using OpenMetaverse.Imaging; | 34 | using OpenMetaverse.Imaging; |
33 | using OpenSim.Framework; | 35 | using OpenSim.Framework; |
34 | using OpenSim.Region.Framework.Interfaces; | 36 | using OpenSim.Region.Framework.Interfaces; |
35 | using OpenSim.Services.Interfaces; | 37 | using OpenSim.Services.Interfaces; |
36 | using log4net; | 38 | using log4net; |
37 | using System.Reflection; | ||
38 | 39 | ||
39 | namespace OpenSim.Region.ClientStack.LindenUDP | 40 | namespace OpenSim.Region.ClientStack.LindenUDP |
40 | { | 41 | { |
41 | |||
42 | public class LLImageManager | 42 | public class LLImageManager |
43 | { | 43 | { |
44 | 44 | private sealed class J2KImageComparer : IComparer<J2KImage> | |
45 | //Public interfaces: | 45 | { |
46 | //Constructor - (LLClientView, IAssetCache, IJ2KDecoder); | 46 | public int Compare(J2KImage x, J2KImage y) |
47 | //void EnqueueReq - (TextureRequestArgs) | 47 | { |
48 | //ProcessImageQueue | 48 | return x.m_requestedPriority.CompareTo(y.m_requestedPriority); |
49 | //Close | 49 | } |
50 | } | ||
51 | |||
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 52 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
51 | private bool m_shuttingdown = false; | 53 | private bool m_shuttingdown = false; |
52 | private long m_lastloopprocessed = 0; | 54 | private long m_lastloopprocessed = 0; |
@@ -54,28 +56,12 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
54 | private LLClientView m_client; //Client we're assigned to | 56 | private LLClientView m_client; //Client we're assigned to |
55 | private IAssetService m_assetCache; //Asset Cache | 57 | private IAssetService m_assetCache; //Asset Cache |
56 | private IJ2KDecoder m_j2kDecodeModule; //Our J2K module | 58 | private IJ2KDecoder m_j2kDecodeModule; //Our J2K module |
59 | private C5.IntervalHeap<J2KImage> m_priorityQueue = new C5.IntervalHeap<J2KImage>(10, new J2KImageComparer()); | ||
57 | 60 | ||
58 | private readonly AssetBase m_missingsubstitute; //Sustitute for bad decodes | ||
59 | private Dictionary<UUID,J2KImage> m_imagestore; // Our main image storage dictionary | ||
60 | private SortedList<double,UUID> m_priorities; // For fast image lookup based on priority | ||
61 | private Dictionary<int, int> m_priorityresolver; //Enabling super fast assignment of images with the same priorities | ||
62 | |||
63 | private const double doubleMinimum = .0000001; | ||
64 | |||
65 | public int m_outstandingtextures = 0; | ||
66 | //Constructor | ||
67 | public LLImageManager(LLClientView client, IAssetService pAssetCache, IJ2KDecoder pJ2kDecodeModule) | 61 | public LLImageManager(LLClientView client, IAssetService pAssetCache, IJ2KDecoder pJ2kDecodeModule) |
68 | { | 62 | { |
69 | |||
70 | m_imagestore = new Dictionary<UUID,J2KImage>(); | ||
71 | m_priorities = new SortedList<double,UUID>(); | ||
72 | m_priorityresolver = new Dictionary<int, int>(); | ||
73 | m_client = client; | 63 | m_client = client; |
74 | m_assetCache = pAssetCache; | 64 | m_assetCache = pAssetCache; |
75 | if (pAssetCache != null) | ||
76 | m_missingsubstitute = pAssetCache.Get("5748decc-f629-461c-9a36-a35a221fe21f"); | ||
77 | else | ||
78 | m_log.Error("[ClientView] - couldn't set missing image, all manner of things will probably break"); | ||
79 | m_j2kDecodeModule = pJ2kDecodeModule; | 65 | m_j2kDecodeModule = pJ2kDecodeModule; |
80 | } | 66 | } |
81 | 67 | ||
@@ -88,174 +74,147 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
88 | //Make sure we're not shutting down.. | 74 | //Make sure we're not shutting down.. |
89 | if (!m_shuttingdown) | 75 | if (!m_shuttingdown) |
90 | { | 76 | { |
77 | J2KImage imgrequest; | ||
91 | 78 | ||
92 | //Do we already know about this UUID? | 79 | // Do a linear search for this texture download |
93 | if (m_imagestore.ContainsKey(newRequest.RequestedAssetID)) | 80 | m_priorityQueue.Find(delegate(J2KImage img) { return img.m_requestedUUID == newRequest.RequestedAssetID; }, out imgrequest); |
94 | { | ||
95 | //Check the packet sequence to make sure this isn't older than | ||
96 | //one we've already received | ||
97 | |||
98 | J2KImage imgrequest = m_imagestore[newRequest.RequestedAssetID]; | ||
99 | 81 | ||
100 | // This is the inbound request sequence number. We can ignore | 82 | if (imgrequest != null) |
101 | // "old" ones. | 83 | { |
102 | 84 | if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f) | |
103 | if (newRequest.requestSequence > imgrequest.m_lastSequence) | ||
104 | { | 85 | { |
86 | m_log.Debug("[JPEG2000]: (CAN) ID=" + newRequest.RequestedAssetID); | ||
105 | 87 | ||
106 | imgrequest.m_lastSequence = newRequest.requestSequence; | 88 | try { m_priorityQueue.Delete(imgrequest.m_priorityQueueHandle); } |
107 | 89 | catch (Exception) { } | |
108 | //Check the priority | 90 | } |
91 | else | ||
92 | { | ||
93 | m_log.DebugFormat("[JPEG2000]: (UPD) ID={0}: D={1}, S={2}, P={3}", | ||
94 | newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority); | ||
109 | 95 | ||
110 | double priority = imgrequest.m_requestedPriority; | 96 | //Check the packet sequence to make sure this isn't older than |
111 | if (priority != newRequest.Priority) | 97 | //one we've already received |
98 | if (newRequest.requestSequence > imgrequest.m_lastSequence) | ||
112 | { | 99 | { |
113 | //Remove the old priority | 100 | //Update the sequence number of the last RequestImage packet |
114 | m_priorities.Remove(imgrequest.m_designatedPriorityKey); | 101 | imgrequest.m_lastSequence = newRequest.requestSequence; |
115 | //Assign a new unique priority | ||
116 | imgrequest.m_requestedPriority = newRequest.Priority; | ||
117 | imgrequest.m_designatedPriorityKey = AssignPriority(newRequest.RequestedAssetID, newRequest.Priority); | ||
118 | } | ||
119 | 102 | ||
120 | //Update the requested discard level | 103 | //Update the requested discard level |
121 | imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; | 104 | imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; |
122 | 105 | ||
123 | //Update the requested packet number | 106 | //Update the requested packet number |
124 | imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; | 107 | imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; |
125 | 108 | ||
126 | //Check if this will create an outstanding texture request | 109 | //Update the requested priority |
127 | bool activated = imgrequest.m_completedSendAtCurrentDiscardLevel; | 110 | imgrequest.m_requestedPriority = newRequest.Priority; |
128 | //Run an update | 111 | try { m_priorityQueue.Replace(imgrequest.m_priorityQueueHandle, imgrequest); } |
129 | 112 | catch (Exception) { imgrequest.m_priorityQueueHandle = null; m_priorityQueue.Add(ref imgrequest.m_priorityQueueHandle, imgrequest); } | |
130 | imgrequest.RunUpdate(); | ||
131 | 113 | ||
132 | if (activated && !imgrequest.m_completedSendAtCurrentDiscardLevel && imgrequest.m_decoded) | 114 | //Run an update |
133 | { | 115 | imgrequest.RunUpdate(); |
134 | Interlocked.Increment(ref m_outstandingtextures); | ||
135 | } | 116 | } |
136 | } | 117 | } |
137 | } | 118 | } |
138 | else | 119 | else |
139 | { | 120 | { |
140 | J2KImage imgrequest = new J2KImage(this); | 121 | if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f) |
141 | 122 | { | |
142 | //Assign our missing substitute | 123 | m_log.DebugFormat("[JPEG2000]: (IGN) ID={0}: D={1}, S={2}, P={3}", |
143 | imgrequest.m_MissingSubstitute = m_missingsubstitute; | 124 | newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority); |
125 | } | ||
126 | else | ||
127 | { | ||
128 | m_log.DebugFormat("[JPEG2000]: (NEW) ID={0}: D={1}, S={2}, P={3}", | ||
129 | newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority); | ||
144 | 130 | ||
145 | //Assign our decoder module | 131 | imgrequest = new J2KImage(this); |
146 | imgrequest.m_j2kDecodeModule = m_j2kDecodeModule; | ||
147 | 132 | ||
148 | //Assign our asset cache module | 133 | //Assign our decoder module |
149 | imgrequest.m_assetCache = m_assetCache; | 134 | imgrequest.m_j2kDecodeModule = m_j2kDecodeModule; |
150 | 135 | ||
151 | //Assign a priority based on our request | 136 | //Assign our asset cache module |
152 | imgrequest.m_designatedPriorityKey = AssignPriority(newRequest.RequestedAssetID, newRequest.Priority); | 137 | imgrequest.m_assetCache = m_assetCache; |
153 | 138 | ||
154 | //Assign the requested discard level | 139 | //Assign the requested discard level |
155 | imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; | 140 | imgrequest.m_requestedDiscardLevel = newRequest.DiscardLevel; |
156 | 141 | ||
157 | //Assign the requested packet number | 142 | //Assign the requested packet number |
158 | imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; | 143 | imgrequest.m_requestedPacketNumber = newRequest.PacketNumber; |
159 | 144 | ||
160 | //Assign the requested priority | 145 | //Assign the requested priority |
161 | imgrequest.m_requestedPriority = newRequest.Priority; | 146 | imgrequest.m_requestedPriority = newRequest.Priority; |
162 | 147 | ||
163 | //Assign the asset uuid | 148 | //Assign the asset uuid |
164 | imgrequest.m_requestedUUID = newRequest.RequestedAssetID; | 149 | imgrequest.m_requestedUUID = newRequest.RequestedAssetID; |
165 | 150 | ||
166 | m_imagestore.Add(imgrequest.m_requestedUUID, imgrequest); | 151 | //Assign the requested priority |
152 | imgrequest.m_requestedPriority = newRequest.Priority; | ||
167 | 153 | ||
168 | //Run an update | 154 | //Add this download to the priority queue |
169 | imgrequest.RunUpdate(); | 155 | m_priorityQueue.Add(ref imgrequest.m_priorityQueueHandle, imgrequest); |
170 | 156 | ||
157 | //Run an update | ||
158 | imgrequest.RunUpdate(); | ||
159 | } | ||
171 | } | 160 | } |
172 | } | 161 | } |
173 | } | 162 | } |
174 | 163 | ||
175 | private double AssignPriority(UUID pAssetID, double pPriority) | 164 | public bool ProcessImageQueue(int count, int maxpack) |
176 | { | 165 | { |
177 | 166 | //count is the number of textures we want to process in one go. | |
178 | //First, find out if we can just assign directly | 167 | //As part of this class re-write, that number will probably rise |
179 | if (m_priorityresolver.ContainsKey((int)pPriority) == false) | 168 | //since we're processing in a more efficient manner. |
169 | |||
170 | // this can happen during Close() | ||
171 | if (m_client == null) | ||
172 | return false; | ||
173 | |||
174 | int numCollected = 0; | ||
175 | |||
176 | //Calculate our threshold | ||
177 | int threshold; | ||
178 | if (m_lastloopprocessed == 0) | ||
180 | { | 179 | { |
181 | m_priorities.Add((double)((int)pPriority), pAssetID); | 180 | if (m_client.PacketHandler == null || m_client.PacketHandler.PacketQueue == null || m_client.PacketHandler.PacketQueue.TextureThrottle == null) |
182 | m_priorityresolver.Add((int)pPriority, 0); | 181 | return false; |
183 | return (double)((int)pPriority); | 182 | //This is decent for a semi fast machine, but we'll calculate it more accurately based on time below |
183 | threshold = m_client.PacketHandler.PacketQueue.TextureThrottle.Current / 6300; | ||
184 | m_lastloopprocessed = DateTime.Now.Ticks; | ||
184 | } | 185 | } |
185 | else | 186 | else |
186 | { | 187 | { |
187 | //Use the hash lookup goodness of a secondary dictionary to find a free slot | 188 | double throttleseconds = ((double)DateTime.Now.Ticks - (double)m_lastloopprocessed) / (double)TimeSpan.TicksPerSecond; |
188 | double mFreePriority = ((int)pPriority) + (doubleMinimum * (m_priorityresolver[(int)pPriority] + 1)); | 189 | throttleseconds = throttleseconds * m_client.PacketHandler.PacketQueue.TextureThrottle.Current; |
189 | m_priorities[mFreePriority] = pAssetID; | ||
190 | m_priorityresolver[(int)pPriority]++; | ||
191 | return mFreePriority; | ||
192 | } | ||
193 | 190 | ||
191 | //Average of 1000 bytes per packet | ||
192 | throttleseconds = throttleseconds / 1000; | ||
194 | 193 | ||
194 | //Safe-zone multiplier of 2.0 | ||
195 | threshold = (int)(throttleseconds * 2.0); | ||
196 | m_lastloopprocessed = DateTime.Now.Ticks; | ||
195 | 197 | ||
196 | } | 198 | } |
197 | 199 | ||
198 | public bool ProcessImageQueue(int count, int maxpack) | 200 | if (m_client.PacketHandler == null) |
199 | { | 201 | return false; |
200 | 202 | ||
201 | // this can happen during Close() | 203 | if (m_client.PacketHandler.PacketQueue == null) |
202 | if (m_client == null) | ||
203 | return false; | 204 | return false; |
204 | |||
205 | //Count is the number of textures we want to process in one go. | ||
206 | //As part of this class re-write, that number will probably rise | ||
207 | //since we're processing in a more efficient manner. | ||
208 | |||
209 | int numCollected = 0; | ||
210 | 205 | ||
211 | //Calculate our threshold | 206 | if (threshold < 10) |
212 | int threshold; | 207 | threshold = 10; |
213 | if (m_lastloopprocessed == 0) | ||
214 | { | ||
215 | if (m_client.PacketHandler == null || m_client.PacketHandler.PacketQueue == null || m_client.PacketHandler.PacketQueue.TextureThrottle == null) | ||
216 | return false; | ||
217 | //This is decent for a semi fast machine, but we'll calculate it more accurately based on time below | ||
218 | threshold = m_client.PacketHandler.PacketQueue.TextureThrottle.Current / 6300; | ||
219 | m_lastloopprocessed = DateTime.Now.Ticks; | ||
220 | } | ||
221 | else | ||
222 | { | ||
223 | double throttleseconds = ((double)DateTime.Now.Ticks - (double)m_lastloopprocessed) / (double)TimeSpan.TicksPerSecond; | ||
224 | throttleseconds = throttleseconds * m_client.PacketHandler.PacketQueue.TextureThrottle.Current; | ||
225 | |||
226 | //Average of 1000 bytes per packet | ||
227 | throttleseconds = throttleseconds / 1000; | ||
228 | |||
229 | //Safe-zone multiplier of 2.0 | ||
230 | threshold = (int)(throttleseconds * 2.0); | ||
231 | m_lastloopprocessed = DateTime.Now.Ticks; | ||
232 | |||
233 | } | ||
234 | |||
235 | if (threshold < 10) | ||
236 | { | ||
237 | threshold = 10; | ||
238 | } | ||
239 | |||
240 | if (m_client.PacketHandler == null) | ||
241 | return false; | ||
242 | |||
243 | if (m_client.PacketHandler.PacketQueue == null) | ||
244 | return false; | ||
245 | |||
246 | //First of all make sure our packet queue isn't above our threshold | ||
247 | 208 | ||
248 | //Uncomment this to see what the texture stack is doing | 209 | //Uncomment this to see what the texture stack is doing |
249 | //m_log.Debug("Queue: " + m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount.ToString() + " Threshold: " + threshold.ToString() + " outstanding: " + m_outstandingtextures.ToString()); | 210 | //m_log.Debug("Queue: " + m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount.ToString() + " Threshold: " + threshold.ToString() + " outstanding: " + m_outstandingtextures.ToString()); |
250 | if (m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount < threshold && m_outstandingtextures > 0) | 211 | if (m_client.PacketHandler.PacketQueue.TextureOutgoingPacketQueueCount < threshold) |
251 | { | 212 | { |
252 | bool justreset = false; | 213 | while (m_priorityQueue.Count > 0) |
253 | |||
254 | for (int x = m_priorities.Count - 1; x > -1; x--) | ||
255 | { | 214 | { |
256 | 215 | J2KImage imagereq = m_priorityQueue.FindMax(); | |
257 | J2KImage imagereq = m_imagestore[m_priorities.Values[x]]; | 216 | |
258 | if (imagereq.m_decoded == true && !imagereq.m_completedSendAtCurrentDiscardLevel) | 217 | if (imagereq.m_decoded == true) |
259 | { | 218 | { |
260 | // we need to test this here now that we are dropping assets | 219 | // we need to test this here now that we are dropping assets |
261 | if (!imagereq.m_hasasset) | 220 | if (!imagereq.m_hasasset) |
@@ -265,78 +224,29 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
265 | continue; | 224 | continue; |
266 | } | 225 | } |
267 | 226 | ||
268 | numCollected++; | 227 | ++numCollected; |
228 | |||
269 | //SendPackets will send up to ten packets per cycle | 229 | //SendPackets will send up to ten packets per cycle |
270 | if (imagereq.SendPackets(m_client, maxpack)) | 230 | if (imagereq.SendPackets(m_client, maxpack)) |
271 | { | 231 | { |
272 | //Send complete | 232 | // Send complete. Destroy any knowledge of this transfer |
273 | if (!imagereq.m_completedSendAtCurrentDiscardLevel) | 233 | try { m_priorityQueue.Delete(imagereq.m_priorityQueueHandle); } |
274 | { | 234 | catch (Exception) { } |
275 | // I think this field imagereq.m_completedSendAtCurrentDiscardLevel | ||
276 | // is completely redundant | ||
277 | //imagereq.m_completedSendAtCurrentDiscardLevel = true; | ||
278 | |||
279 | Interlocked.Decrement(ref m_outstandingtextures); | ||
280 | |||
281 | // First and foremost, drop the reference to the asset | ||
282 | // so that the asset doesn't stay in memory forever. | ||
283 | // We'll Get it again from the asset service (usually cache) | ||
284 | // if/when the client requests it again. | ||
285 | // In order not to mess much with the current implementation | ||
286 | // of this management code, we drop only the asset reference | ||
287 | // but keep the image request itself. | ||
288 | imagereq.DropAsset(); | ||
289 | |||
290 | //Re-assign priority to bottom | ||
291 | //Remove the old priority | ||
292 | m_priorities.Remove(imagereq.m_designatedPriorityKey); | ||
293 | int lowest; | ||
294 | if (m_priorities.Count > 0) | ||
295 | { | ||
296 | lowest = (int)m_priorities.Keys[0]; | ||
297 | lowest--; | ||
298 | } | ||
299 | else | ||
300 | { | ||
301 | lowest = -10000; | ||
302 | } | ||
303 | m_priorities.Add((double)lowest, imagereq.m_requestedUUID); | ||
304 | imagereq.m_designatedPriorityKey = (double)lowest; | ||
305 | if (m_priorityresolver.ContainsKey((int)lowest)) | ||
306 | { | ||
307 | m_priorityresolver[(int)lowest]++; | ||
308 | } | ||
309 | else | ||
310 | { | ||
311 | m_priorityresolver.Add((int)lowest, 0); | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | if (numCollected == count) | ||
316 | { | ||
317 | break; | ||
318 | } | 235 | } |
319 | } | 236 | } |
320 | if (numCollected == count || m_outstandingtextures == 0) | ||
321 | break; | ||
322 | if (numCollected % m_outstandingtextures == 0 && !justreset) | ||
323 | { | ||
324 | //We've gotten as much as we can from the stack, | ||
325 | //reset to the top so that we can send MOAR DATA (nomnomnom)! | ||
326 | x = m_priorities.Count - 1; | ||
327 | 237 | ||
328 | justreset = true; //prevents us from getting stuck in a loop | 238 | if (numCollected == count) |
329 | } | 239 | break; |
330 | } | 240 | } |
331 | } | 241 | } |
332 | 242 | ||
333 | return m_outstandingtextures != 0; | 243 | return m_priorityQueue.Count > 0; |
334 | } | 244 | } |
335 | 245 | ||
336 | //Faux destructor | 246 | //Faux destructor |
337 | public void Close() | 247 | public void Close() |
338 | { | 248 | { |
339 | 249 | ||
340 | m_shuttingdown = true; | 250 | m_shuttingdown = true; |
341 | m_j2kDecodeModule = null; | 251 | m_j2kDecodeModule = null; |
342 | m_assetCache = null; | 252 | m_assetCache = null; |