Source Code Cross Referenced for PersistSize.java in  » Science » Cougaar12_4 » org » cougaar » core » qos » profile » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.core.qos.profile 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.cougaar.core.qos.profile;
002:
003:        import java.lang.reflect.*;
004:        import java.io.*;
005:        import java.text.*;
006:        import java.util.*;
007:        import java.util.regex.*;
008:        import org.cougaar.core.agent.*;
009:        import org.cougaar.core.component.*;
010:        import org.cougaar.core.mts.*;
011:        import org.cougaar.core.node.*;
012:        import org.cougaar.core.qos.metrics.*;
013:        import org.cougaar.core.service.*;
014:        import org.cougaar.core.service.wp.*;
015:        import org.cougaar.core.thread.*;
016:        import org.cougaar.core.wp.resolver.*;
017:        import org.cougaar.util.*;
018:
019:        /**
020:         * This component profiles persistence activity for each
021:         * agent and the aggegate node "sum".
022:         * <p>
023:         * Persistence must be enabled with<br>
024:         * &nbsp;&nbsp;-Dorg.cougaar.core.persistence.enable=true<br>
025:         * otherwise this profiler does nothing.
026:         * <p>
027:         * Example output:<pre> 
028:         *   pdir_AgentA - #sumActive, mostRecent, sumAll, timestamp,
029:         *     time_delta, max, first, last, numFiles,
030:         *   pdir_AgentA - 11461496, 2809731, 11461531, 1103647670000,
031:         *     7143025, 3286631, 0, 5, 8, 
032:         *   psum_NodeA - #sumActive, sumAll
033:         *   psum_NodeA - 63742420, 63742665
034:         * <pre> 
035:         *
036:         * @see ProfilerCoordinator required coordinator component
037:         * @see org.cougaar.core.qos.metrics.PersistenceAdapterPlugin
038:         *   required per-agent component
039:         */
040:        public class PersistSize extends ProfilerBase {
041:            private static final DecimalFormat INT_FORMAT = new DecimalFormat(
042:                    "########0");
043:            private static final DecimalFormat LONG_FORMAT = new DecimalFormat(
044:                    "################0");
045:            private static final DecimalFormat DELTA_FORMAT = new DecimalFormat(
046:                    "_00000");
047:            private boolean enabled;
048:            private MessageAddress nodeId;
049:            private AgentContainer ac;
050:            private String baseDir;
051:            private final Map logs = new HashMap();
052:
053:            public void load() {
054:                super .load();
055:
056:                enabled = "true".equals(System
057:                        .getProperty("org.cougaar.core.persistence.enable"));
058:                if (!enabled) {
059:                    return;
060:                }
061:
062:                NodeIdentificationService nis = (NodeIdentificationService) sb
063:                        .getService(this , NodeIdentificationService.class, null);
064:                nodeId = nis.getMessageAddress();
065:                sb.releaseService(this , NodeIdentificationService.class, nis);
066:
067:                NodeControlService ncs = (NodeControlService) sb.getService(
068:                        this , NodeControlService.class, null);
069:                ac = ncs.getRootContainer();
070:                sb.releaseService(this , NodeControlService.class, ncs);
071:
072:                String installPath = System.getProperty(
073:                        "org.cougaar.install.path", "/tmp");
074:                String workspaceDir = System.getProperty(
075:                        "org.cougaar.workspace", installPath + "/workspace");
076:                baseDir = workspaceDir + "/P";
077:            }
078:
079:            public void run() {
080:                if (enabled) {
081:                    logP();
082:                }
083:            }
084:
085:            private void logP() {
086:                long sumActive = 0;
087:                long sumAll = 0;
088:                Set agents = ac.getAgentAddresses();
089:                for (Iterator iter = agents.iterator(); iter.hasNext();) {
090:                    String agent = ((MessageAddress) iter.next()).getAddress();
091:                    PersistenceData p = readP(agent);
092:                    if (p == null) {
093:                        continue;
094:                    }
095:                    sumActive += p.sumActive;
096:                    sumAll += p.sumAll;
097:                    getLog("pdir_" + agent).shout(p.toString());
098:                }
099:                getLog("psum_" + nodeId).shout(
100:                        formatLong(sumActive) + ", " + formatLong(sumAll));
101:            }
102:
103:            private String formatLong(long l) {
104:                return LONG_FORMAT.format(l);
105:            }
106:
107:            private PersistenceData readP(String agent) {
108:                String dir = baseDir + "/" + agent;
109:                File f = new File(dir);
110:                if (!f.isDirectory()) {
111:                    return null;
112:                }
113:                PersistenceData p = new PersistenceData();
114:                // read sequence numbers
115:                File sequenceFile = new File(dir, "sequence");
116:                if (sequenceFile.exists()) {
117:                    try {
118:                        DataInputStream sequenceStream = new DataInputStream(
119:                                new BufferedInputStream(new FileInputStream(
120:                                        sequenceFile)));
121:                        p.first = sequenceStream.readInt();
122:                        p.last = sequenceStream.readInt();
123:                        p.timestamp = sequenceFile.lastModified();
124:                        sequenceStream.close();
125:                        // most recent delta
126:                        p.mostRecent = getDeltaFile(dir, p.last - 1).length();
127:                        // total for rehydrate
128:                        for (int i = p.first; i < p.last; i++) {
129:                            p.sumActive += getDeltaFile(dir, i).length();
130:                        }
131:                    } catch (Exception e) {
132:                    }
133:                }
134:                // sum disk usage (includes ancient deltas)
135:                File[] fa = f.listFiles();
136:                p.numFiles = fa.length;
137:                for (int i = 0; i < fa.length; i++) {
138:                    long len = fa[i].length();
139:                    if (len > p.max) {
140:                        p.max = len;
141:                    }
142:                    p.sumAll += len;
143:                }
144:                return p;
145:            }
146:
147:            private LoggingService getLog(String key) {
148:                key = key.replace('.', '_');
149:                LoggingService log;
150:                synchronized (logs) {
151:                    log = (LoggingService) logs.get(key);
152:                    if (log == null) {
153:                        log = (LoggingService) sb.getService(
154:                                "org.cougaar.core.qos.profile.persistence."
155:                                        + key, LoggingService.class, null);
156:                        logs.put(key, log);
157:                        log.shout(getHeader(key));
158:                        if (align) {
159:                            String s = getAlign(key);
160:                            for (int i = 0, n = getRunCount(); i < n; i++) {
161:                                log.shout(s);
162:                            }
163:                        }
164:                    }
165:                }
166:                return log;
167:            }
168:
169:            private String getHeader(String key) {
170:                return (key.startsWith("psum") ? "#" + "sumActive, " + "sumAll"
171:                        : PersistenceData.HEADER);
172:            }
173:
174:            private String getAlign(String key) {
175:                return (key.startsWith("psum") ? "0, 0" : PersistenceData.ALIGN);
176:            }
177:
178:            private File getDeltaFile(String dir, int num) {
179:                return new File(dir, "delta" + DELTA_FORMAT.format(num));
180:            }
181:
182:            private static final class PersistenceData {
183:                public static final String HEADER = "#" + "sumActive, "
184:                        + "mostRecent, " + "sumAll, " + "timestamp, "
185:                        + "time_delta, " + "max, " + "first, " + "last, "
186:                        + "numFiles, ";
187:                public static final String ALIGN = "0, " + "0, " + "0, "
188:                        + "0, " + // timestamp?
189:                        "0, " + "0, " + "0, " + "0, " + "0, ";
190:                public long sumActive;
191:                public long mostRecent;
192:                public long sumAll;
193:                public long timestamp;
194:                public long max;
195:                public int first;
196:                public int last;
197:                public int numFiles;
198:
199:                public String toString() {
200:                    long now = System.currentTimeMillis();
201:                    // 0, 0, 8588, 1103326510000, 14395, 8553, 0, 1, 0,
202:                    return formatLong(sumActive) + ", "
203:                            + formatLong(mostRecent) + ", "
204:                            + formatLong(sumAll) + ", " + formatLong(timestamp)
205:                            + ", "
206:                            + formatLong(timestamp > 0 ? now - timestamp : 0)
207:                            + ", " + formatLong(max) + ", " + formatInt(first)
208:                            + ", " + formatInt(last) + ", "
209:                            + formatInt(numFiles) + ", ";
210:                }
211:
212:                private String formatInt(int i) {
213:                    return INT_FORMAT.format(i);
214:                }
215:
216:                private String formatLong(long l) {
217:                    return LONG_FORMAT.format(l);
218:                }
219:            }
220:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.