diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/PacketPool.cs (renamed from OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs) | 97 |
1 files changed, 14 insertions, 83 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs b/OpenSim/Framework/PacketPool.cs index 9f22fb4..41d17c5 100644 --- a/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs +++ b/OpenSim/Framework/PacketPool.cs | |||
@@ -31,10 +31,10 @@ using System.Reflection; | |||
31 | using OpenMetaverse; | 31 | using OpenMetaverse; |
32 | using OpenMetaverse.Packets; | 32 | using OpenMetaverse.Packets; |
33 | using log4net; | 33 | using log4net; |
34 | using OpenSim.Framework.Monitoring; | ||
35 | 34 | ||
36 | namespace OpenSim.Region.ClientStack.LindenUDP | 35 | namespace OpenSim.Framework |
37 | { | 36 | { |
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,32 +44,14 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
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> | ||
70 | private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>(); | 47 | private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>(); |
71 | 48 | ||
72 | private static Dictionary<Type, Stack<Object>> DataBlocks = new Dictionary<Type, Stack<Object>>(); | 49 | private static Dictionary<Type, Stack<Object>> DataBlocks = |
50 | new Dictionary<Type, Stack<Object>>(); | ||
51 | |||
52 | static PacketPool() | ||
53 | { | ||
54 | } | ||
73 | 55 | ||
74 | public static PacketPool Instance | 56 | public static PacketPool Instance |
75 | { | 57 | { |
@@ -88,45 +70,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
88 | get { return dataBlockPoolEnabled; } | 70 | get { return dataBlockPoolEnabled; } |
89 | } | 71 | } |
90 | 72 | ||
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> | ||
126 | public Packet GetPacket(PacketType type) | 73 | public Packet GetPacket(PacketType type) |
127 | { | 74 | { |
128 | m_packetsReusedStat.Consequent++; | ||
129 | |||
130 | Packet packet; | 75 | Packet packet; |
131 | 76 | ||
132 | if (!packetPoolEnabled) | 77 | if (!packetPoolEnabled) |
@@ -136,19 +81,13 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
136 | { | 81 | { |
137 | if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0) | 82 | if (!pool.ContainsKey(type) || pool[type] == null || (pool[type]).Count == 0) |
138 | { | 83 | { |
139 | // m_log.DebugFormat("[PACKETPOOL]: Building {0} packet", type); | ||
140 | |||
141 | // Creating a new packet if we cannot reuse an old package | 84 | // Creating a new packet if we cannot reuse an old package |
142 | packet = Packet.BuildPacket(type); | 85 | packet = Packet.BuildPacket(type); |
143 | } | 86 | } |
144 | else | 87 | else |
145 | { | 88 | { |
146 | // m_log.DebugFormat("[PACKETPOOL]: Pulling {0} packet", type); | ||
147 | |||
148 | // Recycle old packages | 89 | // Recycle old packages |
149 | m_packetsReusedStat.Antecedent++; | 90 | packet = (pool[type]).Pop(); |
150 | |||
151 | packet = pool[type].Pop(); | ||
152 | } | 91 | } |
153 | } | 92 | } |
154 | 93 | ||
@@ -197,7 +136,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
197 | { | 136 | { |
198 | PacketType type = GetType(bytes); | 137 | PacketType type = GetType(bytes); |
199 | 138 | ||
200 | // Array.Clear(zeroBuffer, 0, zeroBuffer.Length); | 139 | Array.Clear(zeroBuffer, 0, zeroBuffer.Length); |
201 | 140 | ||
202 | int i = 0; | 141 | int i = 0; |
203 | Packet packet = GetPacket(type); | 142 | Packet packet = GetPacket(type); |
@@ -244,7 +183,6 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
244 | switch (packet.Type) | 183 | switch (packet.Type) |
245 | { | 184 | { |
246 | // List pooling packets here | 185 | // List pooling packets here |
247 | case PacketType.AgentUpdate: | ||
248 | case PacketType.PacketAck: | 186 | case PacketType.PacketAck: |
249 | case PacketType.ObjectUpdate: | 187 | case PacketType.ObjectUpdate: |
250 | case PacketType.ImprovedTerseObjectUpdate: | 188 | case PacketType.ImprovedTerseObjectUpdate: |
@@ -259,9 +197,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
259 | 197 | ||
260 | if ((pool[type]).Count < 50) | 198 | if ((pool[type]).Count < 50) |
261 | { | 199 | { |
262 | // m_log.DebugFormat("[PACKETPOOL]: Pushing {0} packet", type); | 200 | (pool[type]).Push(packet); |
263 | |||
264 | pool[type].Push(packet); | ||
265 | } | 201 | } |
266 | } | 202 | } |
267 | break; | 203 | break; |
@@ -273,21 +209,16 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
273 | } | 209 | } |
274 | } | 210 | } |
275 | 211 | ||
276 | public T GetDataBlock<T>() where T: new() | 212 | public static T GetDataBlock<T>() where T: new() |
277 | { | 213 | { |
278 | lock (DataBlocks) | 214 | lock (DataBlocks) |
279 | { | 215 | { |
280 | m_blocksReusedStat.Consequent++; | ||
281 | |||
282 | Stack<Object> s; | 216 | Stack<Object> s; |
283 | 217 | ||
284 | if (DataBlocks.TryGetValue(typeof(T), out s)) | 218 | if (DataBlocks.TryGetValue(typeof(T), out s)) |
285 | { | 219 | { |
286 | if (s.Count > 0) | 220 | if (s.Count > 0) |
287 | { | ||
288 | m_blocksReusedStat.Antecedent++; | ||
289 | return (T)s.Pop(); | 221 | return (T)s.Pop(); |
290 | } | ||
291 | } | 222 | } |
292 | else | 223 | else |
293 | { | 224 | { |
@@ -298,7 +229,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
298 | } | 229 | } |
299 | } | 230 | } |
300 | 231 | ||
301 | public void ReturnDataBlock<T>(T block) where T: new() | 232 | public static void ReturnDataBlock<T>(T block) where T: new() |
302 | { | 233 | { |
303 | if (block == null) | 234 | if (block == null) |
304 | return; | 235 | return; |
@@ -313,4 +244,4 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
313 | } | 244 | } |
314 | } | 245 | } |
315 | } | 246 | } |
316 | } \ No newline at end of file | 247 | } |