1 28 29 30 package com.caucho.tools.profiler; 31 32 import com.caucho.loader.EnvironmentLocal; 33 import com.caucho.util.LruCache; 34 35 import java.util.Collection ; 36 import java.util.Comparator ; 37 import java.util.Iterator ; 38 import java.util.LinkedList ; 39 import java.util.Set ; 40 import java.util.TreeSet ; 41 42 50 public class ProfilerManager { 51 private static final EnvironmentLocal<ProfilerManager> _local = new EnvironmentLocal<ProfilerManager>(); 52 53 private final LruCache<String , ProfilerPoint> _profilerPointMap 54 = new LruCache<String , ProfilerPoint>(1024); 55 56 private boolean _isEnabled = false; 57 58 public static ProfilerManager getLocal() 59 { 60 synchronized (_local) { 61 ProfilerManager local = _local.get(); 62 63 if (local == null) { 64 local = new ProfilerManager(); 65 _local.set(local); 66 } 67 68 return local; 69 } 70 } 71 72 private ProfilerManager() 73 { 74 new ProfilerAdmin(this); 75 } 76 77 80 public void setEnabled(boolean isEnabled) 81 { 82 _isEnabled = isEnabled; 83 } 84 85 public boolean isEnabled() 86 { 87 return _isEnabled; 88 } 89 90 public void enable() 91 { 92 if (!_isEnabled) { 93 reset(); 94 _isEnabled = true; 95 } 96 } 97 98 public void disable() 99 { 100 _isEnabled = false; 101 } 102 103 public ProfilerPoint getProfilerPoint(String name) 104 { 105 synchronized (_profilerPointMap) { 106 ProfilerPoint profilerPoint = _profilerPointMap.get(name); 107 108 if (profilerPoint == null) { 109 profilerPoint = new ProfilerPoint(this, name); 110 _profilerPointMap.put(name, profilerPoint); 111 } 112 113 return profilerPoint; 114 } 115 } 116 117 public CategorizingProfilerPoint getCategorizingProfilerPoint(String name) 118 { 119 synchronized (_profilerPointMap) { 120 ProfilerPoint profilerPoint = _profilerPointMap.get(name); 121 122 if (profilerPoint == null) { 123 profilerPoint = new CategorizingProfilerPoint(this, name); 124 _profilerPointMap.put(name, profilerPoint); 125 } 126 127 return (CategorizingProfilerPoint) profilerPoint; 128 } 129 } 130 131 public Set <ProfilerPoint> getAllProfilerPoints() 132 { 133 synchronized (_profilerPointMap) { 134 TreeSet <ProfilerPoint> allProfilerPoints = new TreeSet <ProfilerPoint>(); 135 136 Iterator <ProfilerPoint> existingValues = _profilerPointMap.values(); 137 138 while (existingValues.hasNext()) 139 allProfilerPoints.add(existingValues.next()); 140 141 return allProfilerPoints; 142 } 143 } 144 145 148 public Collection <ProfilerNode> getAllProfilerNodes() 149 { 150 LinkedList <ProfilerNode> profilerNodes = new LinkedList <ProfilerNode>(); 151 152 fillAllProfilerNodes(profilerNodes); 153 154 return profilerNodes; 155 } 156 157 161 public Collection <ProfilerNode> getAllProfilerNodes(Comparator <ProfilerNode> comparator) 162 { 163 TreeSet <ProfilerNode> profilerNodes = new TreeSet <ProfilerNode>(comparator); 164 165 fillAllProfilerNodes(profilerNodes); 166 167 return profilerNodes; 168 } 169 170 176 public Collection <ProfilerNode> getChildProfilerNodes(ProfilerNode parent) 177 { 178 LinkedList <ProfilerNode> profilerNodes = new LinkedList <ProfilerNode>(); 179 180 fillChildProfilerNodes(parent, profilerNodes); 181 182 return profilerNodes; 183 } 184 185 191 public Collection <ProfilerNode> getChildProfilerNodes(ProfilerNode parent, 192 Comparator <ProfilerNode> comparator) 193 { 194 TreeSet <ProfilerNode> profilerNodes = new TreeSet <ProfilerNode>(comparator); 195 196 fillChildProfilerNodes(parent, profilerNodes); 197 198 return profilerNodes; 199 } 200 201 private void fillAllProfilerNodes(Collection <ProfilerNode> profilerNodes) 202 { 203 synchronized (_profilerPointMap) { 204 Iterator <ProfilerPoint> iter = _profilerPointMap.values(); 205 206 while (iter.hasNext()) { 207 ProfilerPoint profilerPoint = iter.next(); 208 209 synchronized (profilerPoint) { 210 profilerNodes.addAll(profilerPoint.getProfilerNodes()); 211 } 212 } 213 } 214 } 215 216 private void fillChildProfilerNodes(ProfilerNode parent, 217 Collection <ProfilerNode> profilerNodes) 218 { 219 synchronized (_profilerPointMap) { 220 Iterator <ProfilerPoint> iter = _profilerPointMap.values(); 221 222 while (iter.hasNext()) { 223 ProfilerPoint profilerPoint = iter.next(); 224 225 synchronized (profilerPoint) { 226 for (ProfilerNode profilerNode : profilerPoint.getProfilerNodes()) { 227 if (profilerNode.getParent() == parent) 228 profilerNodes.add(profilerNode); 229 } 230 } 231 } 232 } 233 } 234 235 238 public void reset() 239 { 240 synchronized (_profilerPointMap) { 241 Iterator <ProfilerPoint> iter = _profilerPointMap.values(); 242 243 while (iter.hasNext()) { 244 ProfilerPoint profilerPoint = iter.next(); 245 246 profilerPoint.reset(); 247 } 248 } 249 } 250 251 public String toString() 252 { 253 return "ProfilerManager[" + getClass().getClassLoader() + "]"; 254 } 255 256 } 257 | Popular Tags |