001    package jp.osdl.jbento2.analyzer;
002    
003    import java.util.Calendar;
004    import java.util.Iterator;
005    
006    public class GCBuilder {
007    
008        private GCList list;
009    
010        private Calendar calendar = Calendar.getInstance();
011    
012        private long lastModified = 0;
013    
014        private long startMillis = 0;
015    
016        private long rampUpMillis = 0;
017    
018        private long durationMillis = 0;
019    
020        int youngGcCount = 0;
021    
022        int fullGcCount = 0;
023    
024        long youngGcTime = 0;
025    
026        long fullGcTime = 0;
027    
028        public GCBuilder(GCList list) {
029            this.list = list;
030        }
031    
032        public void setStartMillis(long l) {
033            this.startMillis = l;
034        }
035    
036        public void setLastModified(long l) {
037            this.lastModified = l;
038        }
039    
040        public String build() {
041    
042            if (startMillis == 0) {
043                startMillis = lastModified - list.getLast().getStartTime()
044                        - list.getLast().getProcessingTime();
045            }
046    
047            TextTable tt = new TextTable(12, TextTable.ALIGN_RIGHT, ",");
048            tt.setAlign(0, TextTable.ALIGN_LEFT);
049            tt.addRow(new String[] { "Time", "Timestamp", "EC", "EU", "OC", "OU",
050                    "PC", "PU", "YGC", "YGCT", "FGC", "FGCT" });
051    
052            Iterator ite = list.iterator();
053    
054            long lastMillis = Long.MAX_VALUE;
055            
056            if (durationMillis > 0) {
057                lastMillis = rampUpMillis + durationMillis;
058            }
059    
060            while (ite.hasNext()) {
061                GCSample sample = (GCSample) ite.next();
062                if (rampUpMillis < sample.getStartTime()
063                        && sample.getStartTime() <= lastMillis) {
064                    sample.calculate();
065                    buildRows(sample, tt);
066                }
067            }
068    
069            return tt.toString();
070        }
071    
072        private void buildRows(GCSample sample, TextTable tt) {
073            long gcStartTime = sample.getStartTime();
074            String startTimeStr = getTimeString(startMillis + gcStartTime);
075            tt.addRow(new String[] { startTimeStr, String.valueOf(gcStartTime),
076                    String.valueOf(sample.getNewGC().getMaxSize()),
077                    String.valueOf(sample.getNewGC().getBeforeSize()),
078                    String.valueOf(sample.getTenuredGC().getMaxSize()),
079                    String.valueOf(sample.getTenuredGC().getBeforeSize()),
080                    String.valueOf(sample.getPermGC().getMaxSize()),
081                    String.valueOf(sample.getPermGC().getBeforeSize()),
082                    String.valueOf(youngGcCount), String.valueOf(youngGcTime),
083                    String.valueOf(fullGcCount), String.valueOf(fullGcTime) });
084    
085            if (sample.getType() == GCSample.TYPE_GC) {
086                youngGcCount++;
087                youngGcTime += sample.getProcessingTime();
088            } else {
089                fullGcCount++;
090                fullGcTime += sample.getProcessingTime();
091            }
092    
093            startTimeStr = getTimeString(startMillis + gcStartTime
094                    + sample.getProcessingTime());
095            tt.addRow(new String[] { startTimeStr, String.valueOf(gcStartTime),
096                    String.valueOf(sample.getNewGC().getMaxSize()),
097                    String.valueOf(sample.getNewGC().getAfterSize()),
098                    String.valueOf(sample.getTenuredGC().getMaxSize()),
099                    String.valueOf(sample.getTenuredGC().getAfterSize()),
100                    String.valueOf(sample.getPermGC().getMaxSize()),
101                    String.valueOf(sample.getPermGC().getAfterSize()),
102                    String.valueOf(youngGcCount), String.valueOf(youngGcTime),
103                    String.valueOf(fullGcCount), String.valueOf(fullGcTime) });
104        }
105    
106        public String getTimeString(long time) {
107            calendar.setTimeInMillis(time);
108    
109            int hour = calendar.get(Calendar.HOUR_OF_DAY);
110            int min = calendar.get(Calendar.MINUTE);
111            int sec = calendar.get(Calendar.SECOND);
112            int msec = calendar.get(Calendar.MILLISECOND);
113    
114            StringBuffer buf = new StringBuffer(12);
115            buf.append(hour / 10);
116            buf.append(hour % 10);
117            buf.append(":");
118            buf.append(min / 10);
119            buf.append(min % 10);
120            buf.append(":");
121            buf.append(sec / 10);
122            buf.append(sec % 10);
123            buf.append(".");
124            buf.append(msec / 100);
125            buf.append((msec / 10) % 10);
126            buf.append(msec % 10);
127            return buf.toString();
128        }
129    
130        public long getDurationMillis() {
131            return durationMillis;
132        }
133    
134        public void setDurationMillis(long durationMillis) {
135            this.durationMillis = durationMillis;
136        }
137    
138        public long getRampUpMillis() {
139            return rampUpMillis;
140        }
141    
142        public void setRampUpMillis(long rampUpMillis) {
143            this.rampUpMillis = rampUpMillis;
144        }
145    }