| 1 | package net.minecraft.profiler;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.util.Collections;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import org.apache.logging.log4j.LogManager;
|
| 9 | import org.apache.logging.log4j.Logger;
|
| 10 |
|
| 11 | public class Profiler
|
| 12 | {
|
| 13 | private static final Logger logger = LogManager.getLogger();
|
| 14 | private final List<String> sectionList = Lists.<String>newArrayList();
|
| 15 | private final List<Long> timestampList = Lists.<Long>newArrayList();
|
| 16 |
|
| 17 | /** Flag profiling enabled */
|
| 18 | public boolean profilingEnabled;
|
| 19 |
|
| 20 | /** Current profiling section */
|
| 21 | private String profilingSection = "";
|
| 22 | private final Map<String, Long> profilingMap = Maps.<String, Long>newHashMap();
|
| 23 |
|
| 24 | /**
|
| 25 | * Clear profiling.
|
| 26 | */
|
| 27 | public void clearProfiling()
|
| 28 | {
|
| 29 | this.profilingMap.clear();
|
| 30 | this.profilingSection = "";
|
| 31 | this.sectionList.clear();
|
| 32 | }
|
| 33 |
|
| 34 | /**
|
| 35 | * Start section
|
| 36 | */
|
| 37 | public void startSection(String name)
|
| 38 | {
|
| 39 | if (this.profilingEnabled)
|
| 40 | {
|
| 41 | if (this.profilingSection.length() > 0)
|
| 42 | {
|
| 43 | this.profilingSection = this.profilingSection + ".";
|
| 44 | }
|
| 45 |
|
| 46 | this.profilingSection = this.profilingSection + name;
|
| 47 | this.sectionList.add(this.profilingSection);
|
| 48 | this.timestampList.add(Long.valueOf(System.nanoTime()));
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | /**
|
| 53 | * End section
|
| 54 | */
|
| 55 | public void endSection()
|
| 56 | {
|
| 57 | if (this.profilingEnabled)
|
| 58 | {
|
| 59 | long i = System.nanoTime();
|
| 60 | long j = ((Long)this.timestampList.remove(this.timestampList.size() - 1)).longValue();
|
| 61 | this.sectionList.remove(this.sectionList.size() - 1);
|
| 62 | long k = i - j;
|
| 63 |
|
| 64 | if (this.profilingMap.containsKey(this.profilingSection))
|
| 65 | {
|
| 66 | this.profilingMap.put(this.profilingSection, Long.valueOf(((Long)this.profilingMap.get(this.profilingSection)).longValue() + k));
|
| 67 | }
|
| 68 | else
|
| 69 | {
|
| 70 | this.profilingMap.put(this.profilingSection, Long.valueOf(k));
|
| 71 | }
|
| 72 |
|
| 73 | if (k > 100000000L)
|
| 74 | {
|
| 75 | logger.warn("Something\'s taking too long! \'" + this.profilingSection + "\' took aprox " + (double)k / 1000000.0D + " ms");
|
| 76 | }
|
| 77 |
|
| 78 | this.profilingSection = !this.sectionList.isEmpty() ? (String)this.sectionList.get(this.sectionList.size() - 1) : "";
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | public List<Profiler.Result> getProfilingData(String profilerName)
|
| 83 | {
|
| 84 | if (!this.profilingEnabled)
|
| 85 | {
|
| 86 | return null;
|
| 87 | }
|
| 88 | else
|
| 89 | {
|
| 90 | long i = this.profilingMap.containsKey("root") ? ((Long)this.profilingMap.get("root")).longValue() : 0L;
|
| 91 | long j = this.profilingMap.containsKey(profilerName) ? ((Long)this.profilingMap.get(profilerName)).longValue() : -1L;
|
| 92 | List<Profiler.Result> list = Lists.<Profiler.Result>newArrayList();
|
| 93 |
|
| 94 | if (profilerName.length() > 0)
|
| 95 | {
|
| 96 | profilerName = profilerName + ".";
|
| 97 | }
|
| 98 |
|
| 99 | long k = 0L;
|
| 100 |
|
| 101 | for (String s : this.profilingMap.keySet())
|
| 102 | {
|
| 103 | if (s.length() > profilerName.length() && s.startsWith(profilerName) && s.indexOf(".", profilerName.length() + 1) < 0)
|
| 104 | {
|
| 105 | k += ((Long)this.profilingMap.get(s)).longValue();
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | float f = (float)k;
|
| 110 |
|
| 111 | if (k < j)
|
| 112 | {
|
| 113 | k = j;
|
| 114 | }
|
| 115 |
|
| 116 | if (i < k)
|
| 117 | {
|
| 118 | i = k;
|
| 119 | }
|
| 120 |
|
| 121 | for (String s1 : this.profilingMap.keySet())
|
| 122 | {
|
| 123 | if (s1.length() > profilerName.length() && s1.startsWith(profilerName) && s1.indexOf(".", profilerName.length() + 1) < 0)
|
| 124 | {
|
| 125 | long l = ((Long)this.profilingMap.get(s1)).longValue();
|
| 126 | double d0 = (double)l * 100.0D / (double)k;
|
| 127 | double d1 = (double)l * 100.0D / (double)i;
|
| 128 | String s2 = s1.substring(profilerName.length());
|
| 129 | list.add(new Profiler.Result(s2, d0, d1));
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | for (String s3 : this.profilingMap.keySet())
|
| 134 | {
|
| 135 | this.profilingMap.put(s3, Long.valueOf(((Long)this.profilingMap.get(s3)).longValue() * 999L / 1000L));
|
| 136 | }
|
| 137 |
|
| 138 | if ((float)k > f)
|
| 139 | {
|
| 140 | list.add(new Profiler.Result("unspecified", (double)((float)k - f) * 100.0D / (double)k, (double)((float)k - f) * 100.0D / (double)i));
|
| 141 | }
|
| 142 |
|
| 143 | Collections.sort(list);
|
| 144 | list.add(0, new Profiler.Result(profilerName, 100.0D, (double)k * 100.0D / (double)i));
|
| 145 | return list;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | /**
|
| 150 | * End current section and start a new section
|
| 151 | */
|
| 152 | public void endStartSection(String name)
|
| 153 | {
|
| 154 | this.endSection();
|
| 155 | this.startSection(name);
|
| 156 | }
|
| 157 |
|
| 158 | public String getNameOfLastSection()
|
| 159 | {
|
| 160 | return this.sectionList.size() == 0 ? "[UNKNOWN]" : (String)this.sectionList.get(this.sectionList.size() - 1);
|
| 161 | }
|
| 162 |
|
| 163 | public static final class Result implements Comparable<Profiler.Result>
|
| 164 | {
|
| 165 | public double field_76332_a;
|
| 166 | public double field_76330_b;
|
| 167 | public String field_76331_c;
|
| 168 |
|
| 169 | public Result(String profilerName, double usePercentage, double totalUsePercentage)
|
| 170 | {
|
| 171 | this.field_76331_c = profilerName;
|
| 172 | this.field_76332_a = usePercentage;
|
| 173 | this.field_76330_b = totalUsePercentage;
|
| 174 | }
|
| 175 |
|
| 176 | public int compareTo(Profiler.Result p_compareTo_1_)
|
| 177 | {
|
| 178 | return p_compareTo_1_.field_76332_a < this.field_76332_a ? -1 : (p_compareTo_1_.field_76332_a > this.field_76332_a ? 1 : p_compareTo_1_.field_76331_c.compareTo(this.field_76331_c));
|
| 179 | }
|
| 180 |
|
| 181 | public int getColor()
|
| 182 | {
|
| 183 | return (this.field_76331_c.hashCode() & 11184810) + 4473924;
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|