1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| package org.jboss.cache.interceptors; |
23 |
| |
24 |
| import org.jboss.cache.CacheSPI; |
25 |
| import org.jboss.cache.InvocationContext; |
26 |
| import org.jboss.cache.marshall.MethodCall; |
27 |
| import org.jboss.cache.marshall.MethodDeclarations; |
28 |
| |
29 |
| import java.util.HashMap; |
30 |
| import java.util.Map; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class CacheMgmtInterceptor |
39 |
| extends Interceptor |
40 |
| implements CacheMgmtInterceptorMBean |
41 |
| { |
42 |
| private long m_hit_times = 0; |
43 |
| private long m_miss_times = 0; |
44 |
| private long m_store_times = 0; |
45 |
| private long m_hits = 0; |
46 |
| private long m_misses = 0; |
47 |
| private long m_stores = 0; |
48 |
| private long m_evictions = 0; |
49 |
| private long m_start = System.currentTimeMillis(); |
50 |
| private long m_reset = m_start; |
51 |
| |
52 |
5668
| public void setCache(CacheSPI cache)
|
53 |
| { |
54 |
5668
| super.setCache(cache);
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
2485340
| public Object invoke(InvocationContext ctx) throws Throwable
|
64 |
| { |
65 |
2485340
| MethodCall m = ctx.getMethodCall();
|
66 |
2485340
| Map attributes;
|
67 |
2485340
| Object[] args = m.getArgs();
|
68 |
2485340
| Object retval;
|
69 |
| |
70 |
| |
71 |
2485340
| if (!getStatisticsEnabled())
|
72 |
2139
| return super.invoke(ctx);
|
73 |
| |
74 |
2483201
| long t1, t2;
|
75 |
2483201
| switch (m.getMethodId())
|
76 |
| { |
77 |
1511832
| case MethodDeclarations.getKeyValueMethodLocal_id:
|
78 |
| |
79 |
| |
80 |
1511832
| t1 = System.currentTimeMillis();
|
81 |
1511832
| retval = super.invoke(ctx);
|
82 |
1511819
| t2 = System.currentTimeMillis();
|
83 |
1511819
| if (retval == null)
|
84 |
| { |
85 |
1123844
| m_miss_times = m_miss_times + (t2 - t1);
|
86 |
1123844
| m_misses++;
|
87 |
| } |
88 |
| else |
89 |
| { |
90 |
387975
| m_hit_times = m_hit_times + (t2 - t1);
|
91 |
387975
| m_hits++;
|
92 |
| } |
93 |
1511819
| break;
|
94 |
24
| case MethodDeclarations.putForExternalReadMethodLocal_id:
|
95 |
404736
| case MethodDeclarations.putKeyValMethodLocal_id:
|
96 |
404760
| t1 = System.currentTimeMillis();
|
97 |
404771
| retval = super.invoke(ctx);
|
98 |
404751
| t2 = System.currentTimeMillis();
|
99 |
404751
| m_store_times = m_store_times + (t2 - t1);
|
100 |
404751
| m_stores++;
|
101 |
404751
| break;
|
102 |
11651
| case MethodDeclarations.putDataMethodLocal_id:
|
103 |
2
| case MethodDeclarations.putDataEraseMethodLocal_id:
|
104 |
| |
105 |
11653
| attributes = (Map) args[2];
|
106 |
11653
| t1 = System.currentTimeMillis();
|
107 |
11653
| retval = super.invoke(ctx);
|
108 |
11650
| t2 = System.currentTimeMillis();
|
109 |
| |
110 |
11650
| if (attributes != null && attributes.size() > 0)
|
111 |
| { |
112 |
6029
| m_store_times = m_store_times + (t2 - t1);
|
113 |
6029
| m_stores = m_stores + attributes.size();
|
114 |
| } |
115 |
11650
| break;
|
116 |
118242
| case MethodDeclarations.evictNodeMethodLocal_id:
|
117 |
0
| case MethodDeclarations.evictVersionedNodeMethodLocal_id:
|
118 |
| |
119 |
118242
| retval = super.invoke(ctx);
|
120 |
118240
| m_evictions++;
|
121 |
118240
| break;
|
122 |
436703
| default:
|
123 |
436703
| retval = super.invoke(ctx);
|
124 |
436668
| break;
|
125 |
| } |
126 |
| |
127 |
2483118
| return retval;
|
128 |
| } |
129 |
| |
130 |
2
| public long getHits()
|
131 |
| { |
132 |
2
| return m_hits;
|
133 |
| } |
134 |
| |
135 |
2
| public long getMisses()
|
136 |
| { |
137 |
2
| return m_misses;
|
138 |
| } |
139 |
| |
140 |
3
| public long getStores()
|
141 |
| { |
142 |
3
| return m_stores;
|
143 |
| } |
144 |
| |
145 |
3
| public long getEvictions()
|
146 |
| { |
147 |
3
| return m_evictions;
|
148 |
| } |
149 |
| |
150 |
1
| public double getHitMissRatio()
|
151 |
| { |
152 |
1
| double total = m_hits + m_misses;
|
153 |
1
| if (total == 0)
|
154 |
0
| return 0;
|
155 |
1
| return (m_hits / total);
|
156 |
| } |
157 |
| |
158 |
1
| public double getReadWriteRatio()
|
159 |
| { |
160 |
1
| if (m_stores == 0)
|
161 |
0
| return 0;
|
162 |
1
| return (((double) (m_hits + m_misses) / (double) m_stores));
|
163 |
| } |
164 |
| |
165 |
0
| public long getAverageReadTime()
|
166 |
| { |
167 |
0
| long total = m_hits + m_misses;
|
168 |
0
| if (total == 0)
|
169 |
0
| return 0;
|
170 |
0
| return (m_hit_times + m_miss_times) / total;
|
171 |
| } |
172 |
| |
173 |
0
| public long getAverageWriteTime()
|
174 |
| { |
175 |
0
| if (m_stores == 0)
|
176 |
0
| return 0;
|
177 |
0
| return (m_store_times) / m_stores;
|
178 |
| } |
179 |
| |
180 |
3
| public int getNumberOfAttributes()
|
181 |
| { |
182 |
3
| return cache.getNumberOfAttributes();
|
183 |
| } |
184 |
| |
185 |
3
| public int getNumberOfNodes()
|
186 |
| { |
187 |
3
| return cache.getNumberOfNodes();
|
188 |
| } |
189 |
| |
190 |
2
| public long getElapsedTime()
|
191 |
| { |
192 |
2
| return (System.currentTimeMillis() - m_start) / 1000;
|
193 |
| } |
194 |
| |
195 |
2
| public long getTimeSinceReset()
|
196 |
| { |
197 |
2
| return (System.currentTimeMillis() - m_reset) / 1000;
|
198 |
| } |
199 |
| |
200 |
0
| public Map<String, Object> dumpStatistics()
|
201 |
| { |
202 |
0
| Map<String, Object> retval = new HashMap<String, Object>();
|
203 |
0
| retval.put("Hits", m_hits);
|
204 |
0
| retval.put("Misses", m_misses);
|
205 |
0
| retval.put("Stores", m_stores);
|
206 |
0
| retval.put("Evictions", m_evictions);
|
207 |
0
| retval.put("NumberOfAttributes", cache.getNumberOfAttributes());
|
208 |
0
| retval.put("NumberOfNodes", cache.getNumberOfNodes());
|
209 |
0
| retval.put("ElapsedTime", getElapsedTime());
|
210 |
0
| retval.put("TimeSinceReset", getTimeSinceReset());
|
211 |
0
| retval.put("AverageReadTime", getAverageReadTime());
|
212 |
0
| retval.put("AverageWriteTime", getAverageWriteTime());
|
213 |
0
| retval.put("HitMissRatio", getHitMissRatio());
|
214 |
0
| retval.put("ReadWriteRatio", getReadWriteRatio());
|
215 |
0
| return retval;
|
216 |
| } |
217 |
| |
218 |
1
| public void resetStatistics()
|
219 |
| { |
220 |
1
| m_hits = 0;
|
221 |
1
| m_misses = 0;
|
222 |
1
| m_stores = 0;
|
223 |
1
| m_evictions = 0;
|
224 |
1
| m_hit_times = 0;
|
225 |
1
| m_miss_times = 0;
|
226 |
1
| m_store_times = 0;
|
227 |
1
| m_reset = System.currentTimeMillis();
|
228 |
| } |
229 |
| } |
230 |
| |