View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.executor;
21  
22  import java.lang.reflect.Field;
23  import java.lang.reflect.Modifier;
24  import java.util.HashSet;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentHashMap;
27  import java.util.concurrent.ConcurrentMap;
28  
29  import org.apache.mina.core.buffer.IoBuffer;
30  import org.apache.mina.core.session.IoEvent;
31  import org.apache.mina.core.write.WriteRequest;
32  
33  /**
34   * A default {@link IoEventSizeEstimator} implementation.
35   * <p>
36   * <a href="http://martin.nobilitas.com/java/sizeof.html">Martin's Java Notes</a>
37   * was used for estimation.  For unknown types, it inspects declaring fields of the
38   * class of the specified event and the parameter of the event.  The size of unknown
39   * declaring fields are approximated to the specified <tt>averageSizePerField</tt>
40   * (default: 64).
41   * <p>
42   * All the estimated sizes of classes are cached for performance improvement.
43   *
44   * @author The Apache MINA Project (dev@mina.apache.org)
45   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
46   */
47  public class DefaultIoEventSizeEstimator implements IoEventSizeEstimator {
48  
49      private final ConcurrentMap<Class<?>, Integer> class2size = new ConcurrentHashMap<Class<?>, Integer>();
50  
51      public DefaultIoEventSizeEstimator() {
52          class2size.put(boolean.class, 4); // Probably an integer.
53          class2size.put(byte.class, 1);
54          class2size.put(char.class, 2);
55          class2size.put(int.class, 4);
56          class2size.put(short.class, 2);
57          class2size.put(long.class, 8);
58          class2size.put(float.class, 4);
59          class2size.put(double.class, 8);
60          class2size.put(void.class, 0);
61      }
62  
63      public int estimateSize(IoEvent event) {
64          return estimateSize((Object) event) + estimateSize(event.getParameter());
65      }
66  
67      public int estimateSize(Object message) {
68          if (message == null) {
69              return 8;
70          }
71  
72          int answer = 8 + estimateSize(message.getClass(), null);
73  
74          if (message instanceof IoBuffer) {
75              answer += ((IoBuffer) message).remaining();
76          } else if (message instanceof WriteRequest) {
77              answer += estimateSize(((WriteRequest) message).getMessage());
78          } else if (message instanceof CharSequence) {
79              answer += ((CharSequence) message).length() << 1;
80          } else if (message instanceof Iterable) {
81              for (Object m: (Iterable<?>) message) {
82                  answer += estimateSize(m);
83              }
84          }
85  
86          return align(answer);
87      }
88  
89      private int estimateSize(Class<?> clazz, Set<Class<?>> visitedClasses) {
90          Integer objectSize = class2size.get(clazz);
91          if (objectSize != null) {
92              return objectSize;
93          }
94  
95          if (visitedClasses != null) {
96              if (visitedClasses.contains(clazz)) {
97                  return 0;
98              }
99          } else {
100             visitedClasses = new HashSet<Class<?>>();
101         }
102 
103         visitedClasses.add(clazz);
104 
105         int answer = 8; // Basic overhead.
106         for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
107             Field[] fields = c.getDeclaredFields();
108             for (Field f: fields) {
109                 if ((f.getModifiers() & Modifier.STATIC) != 0) {
110                     // Ignore static fields.
111                     continue;
112                 }
113 
114                 answer += estimateSize(f.getType(), visitedClasses);
115             }
116         }
117 
118         visitedClasses.remove(clazz);
119 
120         // Some alignment.
121         answer = align(answer);
122 
123         // Put the final answer.
124         class2size.putIfAbsent(clazz, answer);
125         return answer;
126     }
127 
128     private static int align(int size) {
129         if (size % 8 != 0) {
130             size /= 8;
131             size ++;
132             size *= 8;
133         }
134         return size;
135     }
136 }