diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs b/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs index fc9406b..71f6fe1 100644 --- a/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs +++ b/OpenSim/Region/ClientStack/Linden/UDP/PacketPool.cs | |||
@@ -31,6 +31,7 @@ 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; | ||
34 | 35 | ||
35 | namespace OpenSim.Region.ClientStack.LindenUDP | 36 | namespace OpenSim.Region.ClientStack.LindenUDP |
36 | { | 37 | { |
@@ -43,17 +44,28 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
43 | private bool packetPoolEnabled = true; | 44 | private bool packetPoolEnabled = true; |
44 | private bool dataBlockPoolEnabled = true; | 45 | private bool dataBlockPoolEnabled = true; |
45 | 46 | ||
47 | private PercentageStat m_packetsReusedStat = new PercentageStat( | ||
48 | "PacketsReused", | ||
49 | "Packets reused", | ||
50 | "clientstack", | ||
51 | "packetpool", | ||
52 | StatVerbosity.Debug, | ||
53 | "Number of packets reused out of all requests to the packet pool"); | ||
54 | |||
55 | private PercentageStat m_blocksReusedStat = new PercentageStat( | ||
56 | "BlocksReused", | ||
57 | "Blocks reused", | ||
58 | "clientstack", | ||
59 | "packetpool", | ||
60 | StatVerbosity.Debug, | ||
61 | "Number of data blocks reused out of all requests to the packet pool"); | ||
62 | |||
46 | /// <summary> | 63 | /// <summary> |
47 | /// Pool of packets available for reuse. | 64 | /// Pool of packets available for reuse. |
48 | /// </summary> | 65 | /// </summary> |
49 | private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>(); | 66 | private readonly Dictionary<PacketType, Stack<Packet>> pool = new Dictionary<PacketType, Stack<Packet>>(); |
50 | 67 | ||
51 | private static Dictionary<Type, Stack<Object>> DataBlocks = | 68 | private static Dictionary<Type, Stack<Object>> DataBlocks = new Dictionary<Type, Stack<Object>>(); |
52 | new Dictionary<Type, Stack<Object>>(); | ||
53 | |||
54 | static PacketPool() | ||
55 | { | ||
56 | } | ||
57 | 69 | ||
58 | public static PacketPool Instance | 70 | public static PacketPool Instance |
59 | { | 71 | { |
@@ -72,8 +84,21 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
72 | get { return dataBlockPoolEnabled; } | 84 | get { return dataBlockPoolEnabled; } |
73 | } | 85 | } |
74 | 86 | ||
87 | private PacketPool() | ||
88 | { | ||
89 | StatsManager.RegisterStat(m_packetsReusedStat); | ||
90 | StatsManager.RegisterStat(m_blocksReusedStat); | ||
91 | } | ||
92 | |||
93 | /// <summary> | ||
94 | /// Gets a packet of the given type. | ||
95 | /// </summary> | ||
96 | /// <param name='type'></param> | ||
97 | /// <returns>Guaranteed to always return a packet, whether from the pool or newly constructed.</returns> | ||
75 | public Packet GetPacket(PacketType type) | 98 | public Packet GetPacket(PacketType type) |
76 | { | 99 | { |
100 | m_packetsReusedStat.Consequent++; | ||
101 | |||
77 | Packet packet; | 102 | Packet packet; |
78 | 103 | ||
79 | if (!packetPoolEnabled) | 104 | if (!packetPoolEnabled) |
@@ -89,6 +114,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
89 | else | 114 | else |
90 | { | 115 | { |
91 | // Recycle old packages | 116 | // Recycle old packages |
117 | m_packetsReusedStat.Antecedent++; | ||
118 | |||
92 | packet = (pool[type]).Pop(); | 119 | packet = (pool[type]).Pop(); |
93 | } | 120 | } |
94 | } | 121 | } |
@@ -138,7 +165,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
138 | { | 165 | { |
139 | PacketType type = GetType(bytes); | 166 | PacketType type = GetType(bytes); |
140 | 167 | ||
141 | Array.Clear(zeroBuffer, 0, zeroBuffer.Length); | 168 | // Array.Clear(zeroBuffer, 0, zeroBuffer.Length); |
142 | 169 | ||
143 | int i = 0; | 170 | int i = 0; |
144 | Packet packet = GetPacket(type); | 171 | Packet packet = GetPacket(type); |
@@ -185,6 +212,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
185 | switch (packet.Type) | 212 | switch (packet.Type) |
186 | { | 213 | { |
187 | // List pooling packets here | 214 | // List pooling packets here |
215 | case PacketType.AgentUpdate: | ||
188 | case PacketType.PacketAck: | 216 | case PacketType.PacketAck: |
189 | case PacketType.ObjectUpdate: | 217 | case PacketType.ObjectUpdate: |
190 | case PacketType.ImprovedTerseObjectUpdate: | 218 | case PacketType.ImprovedTerseObjectUpdate: |
@@ -211,16 +239,21 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
211 | } | 239 | } |
212 | } | 240 | } |
213 | 241 | ||
214 | public static T GetDataBlock<T>() where T: new() | 242 | public T GetDataBlock<T>() where T: new() |
215 | { | 243 | { |
216 | lock (DataBlocks) | 244 | lock (DataBlocks) |
217 | { | 245 | { |
246 | m_blocksReusedStat.Consequent++; | ||
247 | |||
218 | Stack<Object> s; | 248 | Stack<Object> s; |
219 | 249 | ||
220 | if (DataBlocks.TryGetValue(typeof(T), out s)) | 250 | if (DataBlocks.TryGetValue(typeof(T), out s)) |
221 | { | 251 | { |
222 | if (s.Count > 0) | 252 | if (s.Count > 0) |
253 | { | ||
254 | m_blocksReusedStat.Antecedent++; | ||
223 | return (T)s.Pop(); | 255 | return (T)s.Pop(); |
256 | } | ||
224 | } | 257 | } |
225 | else | 258 | else |
226 | { | 259 | { |
@@ -231,7 +264,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
231 | } | 264 | } |
232 | } | 265 | } |
233 | 266 | ||
234 | public static void ReturnDataBlock<T>(T block) where T: new() | 267 | public void ReturnDataBlock<T>(T block) where T: new() |
235 | { | 268 | { |
236 | if (block == null) | 269 | if (block == null) |
237 | return; | 270 | return; |