001    package railo.runtime.engine;
002    
003    import railo.commons.lang.SizeOf;
004    import railo.runtime.CFMLFactory;
005    import railo.runtime.CFMLFactoryImpl;
006    import railo.runtime.Mapping;
007    import railo.runtime.MappingImpl;
008    import railo.runtime.config.Config;
009    import railo.runtime.config.ConfigImpl;
010    import railo.runtime.config.ConfigServer;
011    import railo.runtime.config.ConfigWeb;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.op.Caster;
014    import railo.runtime.query.QueryCacheSupport;
015    import railo.runtime.type.Collection;
016    import railo.runtime.type.DoubleStruct;
017    import railo.runtime.type.KeyImpl;
018    import railo.runtime.type.Scope;
019    import railo.runtime.type.Struct;
020    import railo.runtime.type.StructImpl;
021    import railo.runtime.type.scope.ScopeContext;
022    
023    public class Surveillance {
024    
025            private static final Collection.Key MEMORY = KeyImpl.intern("memory");
026            private static final Collection.Key SCOPES = KeyImpl.intern("scopes");
027            private static final Collection.Key MAPPINGS = KeyImpl.intern("mappings");
028            private static final Collection.Key PAGE_POOL = KeyImpl.intern("pagePool");
029            private static final Collection.Key CLASS_LOADER = KeyImpl.intern("classLoader");
030            private static final Collection.Key QUERY_CACHE = KeyImpl.intern("queryCache");
031            private static final Collection.Key PAGE_CONTEXT_STACK = KeyImpl.intern("pageContextStack");
032            
033            
034            public static Struct getInfo(ConfigImpl config) throws PageException {
035                    
036                    Struct sct=new StructImpl();
037                    
038                    // memory
039                    DoubleStruct mem=new DoubleStruct();
040                    sct.set(MEMORY, mem);
041                    getInfoMemory(mem, config);
042                    
043                    // count
044                    //ScopeContext sc = ((CFMLFactoryImpl)config.getFactory()).getScopeContext();
045                    //sc.getSessionCount(pc)
046                    
047                    
048                    return sct;
049            }
050    
051            
052            private static void getInfoMemory(Struct parent, ConfigImpl config) throws PageException {
053                    DoubleStruct server = new DoubleStruct();
054                    DoubleStruct web = new DoubleStruct();
055                    parent.set("server", server);
056                    parent.set("web", web);
057                    
058                    boolean isConfigWeb=config instanceof ConfigWeb;
059                    
060                    // server
061                    /*ConfigServer cs=isConfigWeb?
062                                    config.getConfigServerImpl():
063                                    ((ConfigServer)config);
064                    */
065                    
066                    //infoResources(server,cs);
067                    // web
068                    if(isConfigWeb){
069                            _getInfoMemory(web, server,config);
070                    }
071                    else  {
072                            ConfigWeb[] configs = ((ConfigServer) config).getConfigWebs();
073                            for(int i=0;i<configs.length;i++){
074                                    _getInfoMemory(web,server, (ConfigImpl) configs[i]);    
075                            }
076                    }
077            }
078    
079            private static void _getInfoMemory(Struct web, Struct server, ConfigImpl config) throws PageException {
080                    DoubleStruct sct = new DoubleStruct();
081                    //long start=System.currentTimeMillis();
082                    infoMapping(sct,config);
083                            //print.out(System.currentTimeMillis()-start);
084                    //infoResources(sct,config);
085                            //print.out(System.currentTimeMillis()-start);
086                    
087                    infoScopes(sct,server,config);
088                            //print.out(System.currentTimeMillis()-start);
089                    infoPageContextStack(sct,config.getFactory());
090                            //print.out(System.currentTimeMillis()-start);
091                    infoQueryCache(sct,config.getFactory());
092                            //print.out(System.currentTimeMillis()-start);
093                    //size+=infoResources(sct,cs);
094                    
095                    web.set(config.getConfigDir().getPath(), sct);
096            }
097    
098            
099    
100    
101    
102            private static void infoMapping(Struct parent,Config config) throws PageException {
103                    DoubleStruct map=new DoubleStruct();
104                    infoMapping(map,config.getMappings(),false);
105            infoMapping(map,config.getCustomTagMappings(),true);
106            parent.set(MAPPINGS, map);
107            }
108    
109            private static void infoMapping(Struct map, Mapping[] mappings, boolean isCustomTagMapping) throws PageException {
110                    if(mappings==null) return;
111                    
112                    DoubleStruct sct=new DoubleStruct();
113            
114                    long size;
115                    MappingImpl mapping;
116                    for(int i=0;i<mappings.length;i++)   {
117                            mapping=(MappingImpl) mappings[i];
118                            
119                            // archive classloader
120                            size=SizeOf.size(mapping.getClassLoaderForArchive());
121                            sct.set("archiveClassLoader", Caster.toDouble(size));
122                            
123                            // physical classloader
124                            size=0;
125                            try {
126                                    size=SizeOf.size(mapping.touchPCLCollection());
127                            } catch (Exception e) {}
128                            sct.set("physicalClassLoader", Caster.toDouble(size));
129                            
130                            // pagepool
131                            size=SizeOf.size(mapping.getPageSourcePool());
132                            sct.set(PAGE_POOL, Caster.toDouble(size));
133                            
134                            map.set(!isCustomTagMapping?
135                                            mapping.getVirtual():mapping.getStrPhysical(), 
136                                            sct);
137                    }
138            }
139    
140            private static void infoScopes(Struct web,Struct server,ConfigImpl config) throws PageException {
141                    ScopeContext sc = ((CFMLFactoryImpl)config.getFactory()).getScopeContext();
142                    DoubleStruct webScopes=new DoubleStruct();
143                    DoubleStruct srvScopes=new DoubleStruct();
144                    
145                    long s;
146                    s=sc.getScopesSize(Scope.SCOPE_SESSION);
147                    webScopes.set("session", Caster.toDouble(s));
148                    
149                    s=sc.getScopesSize(Scope.SCOPE_APPLICATION);
150                    webScopes.set("application", Caster.toDouble(s));
151                    
152                    s=sc.getScopesSize(Scope.SCOPE_CLUSTER);
153                    srvScopes.set("cluster", Caster.toDouble(s));
154    
155                    s=sc.getScopesSize(Scope.SCOPE_SERVER);
156                    srvScopes.set("server", Caster.toDouble(s));
157    
158                    s=sc.getScopesSize(Scope.SCOPE_CLIENT);
159                    webScopes.set("client", Caster.toDouble(s));
160                    
161                    web.set(SCOPES, webScopes);
162                    server.set(SCOPES, srvScopes);
163            }
164    
165            private static void infoQueryCache(Struct parent,CFMLFactory factory) throws PageException {
166                    long size= ((QueryCacheSupport)factory.getQueryCache()).sizeOf();
167                    parent.set(QUERY_CACHE, Caster.toDouble(size));
168            }
169            
170            private static void infoPageContextStack(Struct parent,CFMLFactory factory) throws PageException {
171                    long size= ((CFMLFactoryImpl)factory).getPageContextesSize();
172                    parent.set(PAGE_CONTEXT_STACK, Caster.toDouble(size));
173            }
174    
175            /*private static void infoResources(Struct parent, Config config) throws PageException {
176                    // add server proviers ti a set for checking
177                    Set set=new HashSet();
178                    if(config instanceof ConfigWeb){
179                            ConfigServerImpl cs=((ConfigImpl)config).getConfigServerImpl();
180                            ResourceProvider[] providers = cs.getResourceProviders();
181                            for(int i=0;i<providers.length;i++){
182                                    set.add(providers[i]);
183                            }
184                    }
185                    
186                    ResourceProvider[] providers = ((ConfigImpl)config).getResourceProviders();
187                    DoubleStruct sct=new DoubleStruct();
188                    long s;
189                    for(int i=0;i<providers.length;i++){
190                            if(!set.contains(providers[i])){
191                                    s=SizeOf.size(providers[i]);
192                                    sct.set(providers[i].getScheme(), Caster.toDouble(s));
193                            }
194                    }
195                    parent.set("resourceProviders", sct);
196            }*/
197    }