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.common.IoBuffer;
30  import org.apache.mina.common.IoEvent;
31  
32  /**
33   * A default {@link IoEventSizeEstimator} implementation.
34   * <p>
35   * <a href="http://martin.nobilitas.com/java/sizeof.html">Martin's Java Notes</a>
36   * was used for estimation.  For unknown types, it inspects declaring fields of the
37   * class of the specified event and the parameter of the event.  The size of unknown
38   * declaring fields are approximated to the specified <tt>averageSizePerField</tt>
39   * (default: 64).
40   * <p>
41   * All the estimated sizes of classes are cached for performance improvement.
42   * 
43   * @author The Apache MINA Project (dev@mina.apache.org)
44   * @version $Rev: 595517 $, $Date: 2007-11-15 18:31:56 -0700 (Thu, 15 Nov 2007) $
45   */
46  public class DefaultIoEventSizeEstimator implements IoEventSizeEstimator {
47  
48      private final ConcurrentMap<Class<?>, Integer> class2size = new ConcurrentHashMap<Class<?>, Integer>();
49      
50      public DefaultIoEventSizeEstimator() {
51          class2size.put(boolean.class, 4); // Probably an integer.
52          class2size.put(byte.class, 1);
53          class2size.put(char.class, 2);
54          class2size.put(int.class, 4);
55          class2size.put(short.class, 2);
56          class2size.put(long.class, 8);
57          class2size.put(float.class, 4);
58          class2size.put(double.class, 8);
59          class2size.put(void.class, 0);
60      }
61      
62      public int estimateSize(IoEvent event) {
63          return estimateSize((Object) event) + estimateSize(event.getParameter());
64      }
65      
66      public int estimateSize(Object message) {
67          if (message == null) {
68              return 8;
69          }
70  
71          int answer = 8 + estimateSize(message.getClass(), null);
72          
73          if (message instanceof IoBuffer) {
74              answer += ((IoBuffer) message).remaining();
75          } else if (message instanceof CharSequence) {
76              answer += ((CharSequence) message).length() << 1;
77          } else if (message instanceof Iterable) {
78              for (Object m: (Iterable<?>) message) {
79                  answer += estimateSize(m);
80              }
81          }
82          
83          return align(answer);
84      }
85      
86      private int estimateSize(Class<?> clazz, Set<Class<?>> visitedClasses) {
87          Integer objectSize = class2size.get(clazz);
88          if (objectSize != null) {
89              return objectSize;
90          }
91          
92          if (visitedClasses != null) {
93              if (visitedClasses.contains(clazz)) {
94                  return 0;
95              }
96          } else {
97              visitedClasses = new HashSet<Class<?>>();
98          }
99          
100         visitedClasses.add(clazz);
101         
102         int answer = 8; // Basic overhead.
103         for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
104             Field[] fields = c.getDeclaredFields();
105             for (Field f: fields) {
106                 if ((f.getModifiers() & Modifier.STATIC) != 0) {
107                     // Ignore static fields.
108                     continue;
109                 }
110                 
111                 answer += estimateSize(f.getType(), visitedClasses);
112             }
113         }
114             
115         visitedClasses.remove(clazz);
116         
117         // Some alignment.
118         answer = align(answer);
119         
120         // Put the final answer.
121         class2size.putIfAbsent(clazz, answer);
122         return answer;
123     }
124     
125     private static int align(int size) {
126         if (size % 8 != 0) {
127             size /= 8;
128             size ++;
129             size *= 8;
130         }
131         return size;
132     }
133 }