1 28 29 30 package com.caucho.tools.profiler; 31 32 import java.util.Collection ; 33 import java.util.Collections ; 34 import java.util.IdentityHashMap ; 35 import java.util.Map ; 36 37 42 public class ProfilerPoint 43 implements Comparable <ProfilerPoint> 44 { 45 private static final Profiler NOOP_PROFILER = new Profiler() { 46 public void finish() 47 { 48 } 49 50 public String toString() 51 { 52 return "NoopProfiler[]"; 53 } 54 }; 55 56 private final ProfilerManager _profilerManager; 57 private final String _name; 58 59 private long _longHashCode; 60 61 private Map <ProfilerNode, ProfilerNode> _childProfilerNodesMap; 62 63 ProfilerPoint(ProfilerManager profilerManager, String name) 64 { 65 assert profilerManager != null; 66 assert name != null; 67 68 _profilerManager = profilerManager; 69 _name = name; 70 } 71 72 protected ProfilerManager getProfilerManager() 73 { 74 return _profilerManager; 75 } 76 77 public String getName() 78 { 79 return _name; 80 } 81 82 public ProfilerPoint createProfilerPoint(String name) 83 { 84 return getProfilerManager().getProfilerPoint(name); 85 } 86 87 public Profiler start() 88 { 89 if (!_profilerManager.isEnabled()) 90 return NOOP_PROFILER; 91 92 ThreadProfiler profiler = ThreadProfiler.current(); 93 94 profiler.start(this); 95 96 return profiler; 97 } 98 99 protected Profiler start(ProfilerPoint parent) 100 { 101 if (!getProfilerManager().isEnabled()) 102 return NOOP_PROFILER; 103 104 ThreadProfiler profiler = ThreadProfiler.current(); 105 106 profiler.start(parent, this); 107 108 return profiler; 109 } 110 111 115 Collection <ProfilerNode> getProfilerNodes() 116 { 117 if (_childProfilerNodesMap == null) 118 return Collections.emptyList(); 119 else 120 return _childProfilerNodesMap.values(); 121 } 122 123 ProfilerNode getProfilerNode(ProfilerNode parentNode) 124 { 125 synchronized (this) { 126 ProfilerNode node; 127 128 if (_childProfilerNodesMap == null) { 129 _childProfilerNodesMap = new IdentityHashMap <ProfilerNode, ProfilerNode>(); 130 node = null; 131 } 132 else 133 node = _childProfilerNodesMap.get(parentNode); 134 135 if (node == null) { 136 node = new ProfilerNode(parentNode, this); 137 138 _childProfilerNodesMap.put(parentNode, node); 139 } 140 141 return node; 142 } 143 } 144 145 148 void reset() 149 { 150 synchronized (this) { 151 if (_childProfilerNodesMap != null) 152 _childProfilerNodesMap.clear(); 153 } 154 } 155 156 public boolean equals(Object o) 157 { 158 if (o == this) 159 return true; 160 161 if (o == null) 162 return false; 163 164 if (!(o instanceof ProfilerPoint)) 165 return false; 166 167 final ProfilerPoint other = (ProfilerPoint) o; 168 169 if (longHashCode() != other.longHashCode()) 170 return false; 171 172 return getName().equals(other.getName()); 173 } 174 175 public int compareTo(ProfilerPoint other) 176 { 177 return getName().compareTo(other.getName()); 178 } 179 180 public long longHashCode() 181 { 182 if (_longHashCode == 0) { 183 long longHashCode = 7; 184 185 final int len = _name.length(); 186 187 for (int i = 0; i < len; i++) 188 longHashCode = longHashCode * 33 ^ _name.charAt(i); 189 190 _longHashCode = longHashCode; 191 } 192 193 return _longHashCode; 194 } 195 196 public int hashCode() 197 { 198 return (int) longHashCode(); 199 } 200 201 public String toString() 202 { 203 return "ProfilerPoint[" + getName() + "]"; 204 } 205 } 206 | Popular Tags |