aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/PriorityQueue.cs
diff options
context:
space:
mode:
authorMelanie2011-04-25 23:26:37 +0100
committerMelanie2011-04-25 23:26:37 +0100
commita6c53b1ba281e187b3e815ff7c416a0ce5ff04e1 (patch)
treea71aa6ca35f2ccb1a410d9688616f4092c8d5e48 /OpenSim/Framework/PriorityQueue.cs
parentMerge branch 'master' into careminster-presence-refactor (diff)
parentMerge branch 'master' into queuetest (diff)
downloadopensim-SC_OLD-a6c53b1ba281e187b3e815ff7c416a0ce5ff04e1.zip
opensim-SC_OLD-a6c53b1ba281e187b3e815ff7c416a0ce5ff04e1.tar.gz
opensim-SC_OLD-a6c53b1ba281e187b3e815ff7c416a0ce5ff04e1.tar.bz2
opensim-SC_OLD-a6c53b1ba281e187b3e815ff7c416a0ce5ff04e1.tar.xz
Merge branch 'queuetest' into careminster-presence-refactor
Diffstat (limited to 'OpenSim/Framework/PriorityQueue.cs')
-rw-r--r--OpenSim/Framework/PriorityQueue.cs105
1 files changed, 85 insertions, 20 deletions
diff --git a/OpenSim/Framework/PriorityQueue.cs b/OpenSim/Framework/PriorityQueue.cs
index eec2a92..3e6fdaa 100644
--- a/OpenSim/Framework/PriorityQueue.cs
+++ b/OpenSim/Framework/PriorityQueue.cs
@@ -42,22 +42,40 @@ namespace OpenSim.Framework
42 42
43 public delegate bool UpdatePriorityHandler(ref uint priority, ISceneEntity entity); 43 public delegate bool UpdatePriorityHandler(ref uint priority, ISceneEntity entity);
44 44
45 // Heap[0] for self updates 45 /// <summary>
46 // Heap[1..12] for entity updates 46 /// Total number of queues (priorities) available
47 47 /// </summary>
48 public const uint NumberOfQueues = 12; 48 public const uint NumberOfQueues = 12;
49 public const uint ImmediateQueue = 0; 49
50 /// <summary>
51 /// Number of queuest (priorities) that are processed immediately
52 /// </summary.
53 public const uint NumberOfImmediateQueues = 2;
50 54
51 private MinHeap<MinHeapItem>[] m_heaps = new MinHeap<MinHeapItem>[NumberOfQueues]; 55 private MinHeap<MinHeapItem>[] m_heaps = new MinHeap<MinHeapItem>[NumberOfQueues];
52 private Dictionary<uint, LookupItem> m_lookupTable; 56 private Dictionary<uint, LookupItem> m_lookupTable;
57
58 // internal state used to ensure the deqeues are spread across the priority
59 // queues "fairly". queuecounts is the amount to pull from each queue in
60 // each pass. weighted towards the higher priority queues
53 private uint m_nextQueue = 0; 61 private uint m_nextQueue = 0;
62 private uint m_countFromQueue = 0;
63 private uint[] m_queueCounts = { 8, 4, 4, 2, 2, 2, 2, 1, 1, 1, 1, 1 };
64
65 // next request is a counter of the number of updates queued, it provides
66 // a total ordering on the updates coming through the queue and is more
67 // lightweight (and more discriminating) than tick count
54 private UInt64 m_nextRequest = 0; 68 private UInt64 m_nextRequest = 0;
55 69
70 /// <summary>
71 /// Lock for enqueue and dequeue operations on the priority queue
72 /// </summary>
56 private object m_syncRoot = new object(); 73 private object m_syncRoot = new object();
57 public object SyncRoot { 74 public object SyncRoot {
58 get { return this.m_syncRoot; } 75 get { return this.m_syncRoot; }
59 } 76 }
60 77
78#region constructor
61 public PriorityQueue() : this(MinHeap<MinHeapItem>.DEFAULT_CAPACITY) { } 79 public PriorityQueue() : this(MinHeap<MinHeapItem>.DEFAULT_CAPACITY) { }
62 80
63 public PriorityQueue(int capacity) 81 public PriorityQueue(int capacity)
@@ -66,8 +84,16 @@ namespace OpenSim.Framework
66 84
67 for (int i = 0; i < m_heaps.Length; ++i) 85 for (int i = 0; i < m_heaps.Length; ++i)
68 m_heaps[i] = new MinHeap<MinHeapItem>(capacity); 86 m_heaps[i] = new MinHeap<MinHeapItem>(capacity);
87
88 m_nextQueue = NumberOfImmediateQueues;
89 m_countFromQueue = m_queueCounts[m_nextQueue];
69 } 90 }
91#endregion Constructor
70 92
93#region PublicMethods
94 /// <summary>
95 /// Return the number of items in the queues
96 /// </summary>
71 public int Count 97 public int Count
72 { 98 {
73 get 99 get
@@ -75,10 +101,14 @@ namespace OpenSim.Framework
75 int count = 0; 101 int count = 0;
76 for (int i = 0; i < m_heaps.Length; ++i) 102 for (int i = 0; i < m_heaps.Length; ++i)
77 count += m_heaps[i].Count; 103 count += m_heaps[i].Count;
104
78 return count; 105 return count;
79 } 106 }
80 } 107 }
81 108
109 /// <summary>
110 /// Enqueue an item into the specified priority queue
111 /// </summary>
82 public bool Enqueue(uint pqueue, IEntityUpdate value) 112 public bool Enqueue(uint pqueue, IEntityUpdate value)
83 { 113 {
84 LookupItem lookup; 114 LookupItem lookup;
@@ -100,32 +130,62 @@ namespace OpenSim.Framework
100 return true; 130 return true;
101 } 131 }
102 132
133 /// <summary>
134 /// Remove an item from one of the queues. Specifically, it removes the
135 /// oldest item from the next queue in order to provide fair access to
136 /// all of the queues
137 /// </summary>
103 public bool TryDequeue(out IEntityUpdate value, out Int32 timeinqueue) 138 public bool TryDequeue(out IEntityUpdate value, out Int32 timeinqueue)
104 { 139 {
105 // If there is anything in priority queue 0, return it first no 140 // If there is anything in priority queue 0, return it first no
106 // matter what else. Breaks fairness. But very useful. 141 // matter what else. Breaks fairness. But very useful.
107 if (m_heaps[ImmediateQueue].Count > 0) 142 for (int iq = 0; iq < NumberOfImmediateQueues; iq++)
108 { 143 {
109 MinHeapItem item = m_heaps[ImmediateQueue].RemoveMin(); 144 if (m_heaps[iq].Count > 0)
145 {
146 MinHeapItem item = m_heaps[iq].RemoveMin();
147 m_lookupTable.Remove(item.Value.Entity.LocalId);
148 timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
149 value = item.Value;
150
151 return true;
152 }
153 }
154
155 // To get the fair queing, we cycle through each of the
156 // queues when finding an element to dequeue.
157 // We pull (NumberOfQueues - QueueIndex) items from each queue in order
158 // to give lower numbered queues a higher priority and higher percentage
159 // of the bandwidth.
160
161 // Check for more items to be pulled from the current queue
162 if (m_heaps[m_nextQueue].Count > 0 && m_countFromQueue > 0)
163 {
164 m_countFromQueue--;
165
166 MinHeapItem item = m_heaps[m_nextQueue].RemoveMin();
110 m_lookupTable.Remove(item.Value.Entity.LocalId); 167 m_lookupTable.Remove(item.Value.Entity.LocalId);
111 timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime); 168 timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
112 value = item.Value; 169 value = item.Value;
113 170
114 return true; 171 return true;
115 } 172 }
116 173
174 // Find the next non-immediate queue with updates in it
117 for (int i = 0; i < NumberOfQueues; ++i) 175 for (int i = 0; i < NumberOfQueues; ++i)
118 { 176 {
119 // To get the fair queing, we cycle through each of the 177 m_nextQueue = (uint)((m_nextQueue + 1) % NumberOfQueues);
120 // queues when finding an element to dequeue, this code 178 m_countFromQueue = m_queueCounts[m_nextQueue];
121 // assumes that the distribution of updates in the queues 179
122 // is polynomial, probably quadractic (eg distance of PI * R^2) 180 // if this is one of the immediate queues, just skip it
123 uint h = (uint)((m_nextQueue + i) % NumberOfQueues); 181 if (m_nextQueue < NumberOfImmediateQueues)
124 if (m_heaps[h].Count > 0) 182 continue;
183
184 if (m_heaps[m_nextQueue].Count > 0)
125 { 185 {
126 m_nextQueue = (uint)((h + 1) % NumberOfQueues); 186 m_countFromQueue--;
127 187
128 MinHeapItem item = m_heaps[h].RemoveMin(); 188 MinHeapItem item = m_heaps[m_nextQueue].RemoveMin();
129 m_lookupTable.Remove(item.Value.Entity.LocalId); 189 m_lookupTable.Remove(item.Value.Entity.LocalId);
130 timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime); 190 timeinqueue = Util.EnvironmentTickCountSubtract(item.EntryTime);
131 value = item.Value; 191 value = item.Value;
@@ -139,6 +199,10 @@ namespace OpenSim.Framework
139 return false; 199 return false;
140 } 200 }
141 201
202 /// <summary>
203 /// Reapply the prioritization function to each of the updates currently
204 /// stored in the priority queues.
205 /// </summary
142 public void Reprioritize(UpdatePriorityHandler handler) 206 public void Reprioritize(UpdatePriorityHandler handler)
143 { 207 {
144 MinHeapItem item; 208 MinHeapItem item;
@@ -174,17 +238,18 @@ namespace OpenSim.Framework
174 } 238 }
175 } 239 }
176 240
241 /// <summary>
242 /// </summary>
177 public override string ToString() 243 public override string ToString()
178 { 244 {
179 string s = ""; 245 string s = "";
180 for (int i = 0; i < NumberOfQueues; i++) 246 for (int i = 0; i < NumberOfQueues; i++)
181 { 247 s += String.Format("{0,7} ",m_heaps[i].Count);
182 if (s != "") s += ",";
183 s += m_heaps[i].Count.ToString();
184 }
185 return s; 248 return s;
186 } 249 }
187 250
251#endregion PublicMethods
252
188#region MinHeapItem 253#region MinHeapItem
189 private struct MinHeapItem : IComparable<MinHeapItem> 254 private struct MinHeapItem : IComparable<MinHeapItem>
190 { 255 {