aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs (renamed from OpenSim/Framework/PacketPool.cs)97
1 files changed, 83 insertions, 14 deletions
diff --git a/OpenSim/Framework/PacketPool.cs b/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs
index 41d17c5..9f22fb4 100644
--- a/OpenSim/Framework/PacketPool.cs
+++ b/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs
@@ -31,10 +31,10 @@ using System.Reflection;
31using OpenMetaverse; 31using OpenMetaverse;
32using OpenMetaverse.Packets; 32using OpenMetaverse.Packets;
33using log4net; 33using log4net;
34using OpenSim.Framework.Monitoring;
34 35
35namespace OpenSim.Framework 36namespace OpenSim.Region.ClientStack.LindenUDP
36{ 37{
37
38 public sealed class PacketPool 38 public sealed class PacketPool
39 { 39 {
40 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 40 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
@@ -44,14 +44,32 @@ namespace OpenSim.Framework
44 private bool packetPoolEnabled = true; 44 private bool packetPoolEnabled = true;
45 private bool dataBlockPoolEnabled = true; 45 private bool dataBlockPoolEnabled = true;
46 46
47 private PercentageStat m_packetsReusedStat = new PercentageStat(
48 "PacketsReused",
49 "Packets reused",
50 "Number of packets reused out of all requests to the packet pool",
51 "clientstack",
52 "packetpool",
53 StatType.Push,
54 null,
55 StatVerbosity.Debug);
56
57 private PercentageStat m_blocksReusedStat = new PercentageStat(
58 "PacketDataBlocksReused",
59 "Packet data blocks reused",
60 "Number of data blocks reused out of all requests to the packet pool",
61 "clientstack",
62 "packetpool",
63 StatType.Push,
64 null,
65 StatVerbosity.Debug);
66
67 /// <summary>
68 /// Pool of packets available for reuse.
69 /// </summary>
47 private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>(); 70 private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>();
48 71
49 private static Dictionary<Type, Stack<Object>> DataBlocks = 72 private static Dictionary<Type, Stack<Object>> DataBlocks = new Dictionary<Type, Stack<Object>>();
50 new Dictionary<Type, Stack<Object>>();
51
52 static PacketPool()
53 {
54 }
55 73
56 public static PacketPool Instance 74 public static PacketPool Instance
57 { 75 {
@@ -70,8 +88,45 @@ namespace OpenSim.Framework
70 get { return dataBlockPoolEnabled; } 88 get { return dataBlockPoolEnabled; }
71 } 89 }
72 90
91 private PacketPool()
92 {
93 StatsManager.RegisterStat(m_packetsReusedStat);
94 StatsManager.RegisterStat(m_blocksReusedStat);
95
96 StatsManager.RegisterStat(
97 new Stat(
98 "PacketsPoolCount",
99 "Objects within the packet pool",
100 "The number of objects currently stored within the packet pool",
101 "",
102 "clientstack",
103 "packetpool",
104 StatType.Pull,
105 stat => { lock (pool) { stat.Value = pool.Count; } },
106 StatVerbosity.Debug));
107
108 StatsManager.RegisterStat(
109 new Stat(
110 "PacketDataBlocksPoolCount",
111 "Objects within the packet data block pool",
112 "The number of objects currently stored within the packet data block pool",
113 "",
114 "clientstack",
115 "packetpool",
116 StatType.Pull,
117 stat => { lock (DataBlocks) { stat.Value = DataBlocks.Count; } },
118 StatVerbosity.Debug));
119 }
120
121 /// <summary>
122 /// Gets a packet of the given type.
123 /// </summary>
124 /// <param name='type'></param>
125 /// <returns>Guaranteed to always return a packet, whether from the pool or newly constructed.</returns>
73 public Packet GetPacket(PacketType type) 126 public Packet GetPacket(PacketType type)
74 { 127 {
128 m_packetsReusedStat.Consequent++;
129
75 Packet packet; 130 Packet packet;
76 131
77 if (!packetPoolEnabled) 132 if (!packetPoolEnabled)
@@ -81,13 +136,19 @@ namespace OpenSim.Framework
81 { 136 {
82 if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0) 137 if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0)
83 { 138 {
139// m_log.DebugFormat("[PACKETPOOL]: Building {0} packet", type);
140
84 // Creating a new packet if we cannot reuse an old package 141 // Creating a new packet if we cannot reuse an old package
85 packet = Packet.BuildPacket(type); 142 packet = Packet.BuildPacket(type);
86 } 143 }
87 else 144 else
88 { 145 {
146// m_log.DebugFormat("[PACKETPOOL]: Pulling {0} packet", type);
147
89 // Recycle old packages 148 // Recycle old packages
90 packet = (pool[type]).Pop(); 149 m_packetsReusedStat.Antecedent++;
150
151 packet = pool[type].Pop();
91 } 152 }
92 } 153 }
93 154
@@ -136,7 +197,7 @@ namespace OpenSim.Framework
136 { 197 {
137 PacketType type = GetType(bytes); 198 PacketType type = GetType(bytes);
138 199
139 Array.Clear(zeroBuffer, 0, zeroBuffer.Length); 200// Array.Clear(zeroBuffer, 0, zeroBuffer.Length);
140 201
141 int i = 0; 202 int i = 0;
142 Packet packet = GetPacket(type); 203 Packet packet = GetPacket(type);
@@ -183,6 +244,7 @@ namespace OpenSim.Framework
183 switch (packet.Type) 244 switch (packet.Type)
184 { 245 {
185 // List pooling packets here 246 // List pooling packets here
247 case PacketType.AgentUpdate:
186 case PacketType.PacketAck: 248 case PacketType.PacketAck:
187 case PacketType.ObjectUpdate: 249 case PacketType.ObjectUpdate:
188 case PacketType.ImprovedTerseObjectUpdate: 250 case PacketType.ImprovedTerseObjectUpdate:
@@ -197,7 +259,9 @@ namespace OpenSim.Framework
197 259
198 if ((pool[type]).Count < 50) 260 if ((pool[type]).Count < 50)
199 { 261 {
200 (pool[type]).Push(packet); 262// m_log.DebugFormat("[PACKETPOOL]: Pushing {0} packet", type);
263
264 pool[type].Push(packet);
201 } 265 }
202 } 266 }
203 break; 267 break;
@@ -209,16 +273,21 @@ namespace OpenSim.Framework
209 } 273 }
210 } 274 }
211 275
212 public static T GetDataBlock<T>() where T: new() 276 public T GetDataBlock<T>() where T: new()
213 { 277 {
214 lock (DataBlocks) 278 lock (DataBlocks)
215 { 279 {
280 m_blocksReusedStat.Consequent++;
281
216 Stack<Object> s; 282 Stack<Object> s;
217 283
218 if (DataBlocks.TryGetValue(typeof(T), out s)) 284 if (DataBlocks.TryGetValue(typeof(T), out s))
219 { 285 {
220 if (s.Count > 0) 286 if (s.Count > 0)
287 {
288 m_blocksReusedStat.Antecedent++;
221 return (T)s.Pop(); 289 return (T)s.Pop();
290 }
222 } 291 }
223 else 292 else
224 { 293 {
@@ -229,7 +298,7 @@ namespace OpenSim.Framework
229 } 298 }
230 } 299 }
231 300
232 public static void ReturnDataBlock<T>(T block) where T: new() 301 public void ReturnDataBlock<T>(T block) where T: new()
233 { 302 {
234 if (block == null) 303 if (block == null)
235 return; 304 return;
@@ -244,4 +313,4 @@ namespace OpenSim.Framework
244 } 313 }
245 } 314 }
246 } 315 }
247} 316} \ No newline at end of file