[120] | 1 | package org.apache.hadoop.mapreduce; |
---|
| 2 | |
---|
| 3 | import java.io.DataInput; |
---|
| 4 | import java.io.DataOutput; |
---|
| 5 | import java.io.IOException; |
---|
| 6 | import java.util.Collection; |
---|
| 7 | import java.util.IdentityHashMap; |
---|
| 8 | import java.util.Iterator; |
---|
| 9 | import java.util.Map; |
---|
| 10 | import java.util.TreeMap; |
---|
| 11 | |
---|
| 12 | import org.apache.hadoop.io.Text; |
---|
| 13 | import org.apache.hadoop.io.Writable; |
---|
| 14 | |
---|
| 15 | public class Counters implements Writable,Iterable<CounterGroup> { |
---|
| 16 | /** |
---|
| 17 | * A cache from enum values to the associated counter. Dramatically speeds up |
---|
| 18 | * typical usage. |
---|
| 19 | */ |
---|
| 20 | private Map<Enum<?>, Counter> cache = new IdentityHashMap<Enum<?>, Counter>(); |
---|
| 21 | |
---|
| 22 | private TreeMap<String, CounterGroup> groups = |
---|
| 23 | new TreeMap<String, CounterGroup>(); |
---|
| 24 | |
---|
| 25 | public Counters() { |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | Counters(org.apache.hadoop.mapred.Counters counters) { |
---|
| 29 | for(org.apache.hadoop.mapred.Counters.Group group: counters) { |
---|
| 30 | String name = group.getName(); |
---|
| 31 | CounterGroup newGroup = new CounterGroup(name, group.getDisplayName()); |
---|
| 32 | groups.put(name, newGroup); |
---|
| 33 | for(Counter counter: group) { |
---|
| 34 | newGroup.addCounter(counter); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public Counter findCounter(String groupName, String counterName) { |
---|
| 40 | CounterGroup grp = getGroup(groupName); |
---|
| 41 | return grp.findCounter(counterName); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Find the counter for the given enum. The same enum will always return the |
---|
| 46 | * same counter. |
---|
| 47 | * @param key the counter key |
---|
| 48 | * @return the matching counter object |
---|
| 49 | */ |
---|
| 50 | public synchronized Counter findCounter(Enum<?> key) { |
---|
| 51 | Counter counter = cache.get(key); |
---|
| 52 | if (counter == null) { |
---|
| 53 | counter = findCounter(key.getDeclaringClass().getName(), key.toString()); |
---|
| 54 | cache.put(key, counter); |
---|
| 55 | } |
---|
| 56 | return counter; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Returns the names of all counter classes. |
---|
| 61 | * @return Set of counter names. |
---|
| 62 | */ |
---|
| 63 | public synchronized Collection<String> getGroupNames() { |
---|
| 64 | return groups.keySet(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | @Override |
---|
| 68 | public Iterator<CounterGroup> iterator() { |
---|
| 69 | return groups.values().iterator(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Returns the named counter group, or an empty group if there is none |
---|
| 74 | * with the specified name. |
---|
| 75 | */ |
---|
| 76 | public synchronized CounterGroup getGroup(String groupName) { |
---|
| 77 | CounterGroup grp = groups.get(groupName); |
---|
| 78 | if (grp == null) { |
---|
| 79 | grp = new CounterGroup(groupName); |
---|
| 80 | groups.put(groupName, grp); |
---|
| 81 | } |
---|
| 82 | return grp; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Returns the total number of counters, by summing the number of counters |
---|
| 87 | * in each group. |
---|
| 88 | */ |
---|
| 89 | public synchronized int countCounters() { |
---|
| 90 | int result = 0; |
---|
| 91 | for (CounterGroup group : this) { |
---|
| 92 | result += group.size(); |
---|
| 93 | } |
---|
| 94 | return result; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Write the set of groups. |
---|
| 99 | * The external format is: |
---|
| 100 | * #groups (groupName group)* |
---|
| 101 | * |
---|
| 102 | * i.e. the number of groups followed by 0 or more groups, where each |
---|
| 103 | * group is of the form: |
---|
| 104 | * |
---|
| 105 | * groupDisplayName #counters (false | true counter)* |
---|
| 106 | * |
---|
| 107 | * where each counter is of the form: |
---|
| 108 | * |
---|
| 109 | * name (false | true displayName) value |
---|
| 110 | */ |
---|
| 111 | @Override |
---|
| 112 | public synchronized void write(DataOutput out) throws IOException { |
---|
| 113 | out.writeInt(groups.size()); |
---|
| 114 | for (org.apache.hadoop.mapreduce.CounterGroup group: groups.values()) { |
---|
| 115 | Text.writeString(out, group.getName()); |
---|
| 116 | group.write(out); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Read a set of groups. |
---|
| 122 | */ |
---|
| 123 | @Override |
---|
| 124 | public synchronized void readFields(DataInput in) throws IOException { |
---|
| 125 | int numClasses = in.readInt(); |
---|
| 126 | groups.clear(); |
---|
| 127 | while (numClasses-- > 0) { |
---|
| 128 | String groupName = Text.readString(in); |
---|
| 129 | CounterGroup group = new CounterGroup(groupName); |
---|
| 130 | group.readFields(in); |
---|
| 131 | groups.put(groupName, group); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Return textual representation of the counter values. |
---|
| 137 | */ |
---|
| 138 | public synchronized String toString() { |
---|
| 139 | StringBuilder sb = new StringBuilder("Counters: " + countCounters()); |
---|
| 140 | for (CounterGroup group: this) { |
---|
| 141 | sb.append("\n\t" + group.getDisplayName()); |
---|
| 142 | for (Counter counter: group) { |
---|
| 143 | sb.append("\n\t\t" + counter.getDisplayName() + "=" + |
---|
| 144 | counter.getValue()); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | return sb.toString(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Increments multiple counters by their amounts in another Counters |
---|
| 152 | * instance. |
---|
| 153 | * @param other the other Counters instance |
---|
| 154 | */ |
---|
| 155 | public synchronized void incrAllCounters(Counters other) { |
---|
| 156 | for(Map.Entry<String, CounterGroup> rightEntry: other.groups.entrySet()) { |
---|
| 157 | CounterGroup left = groups.get(rightEntry.getKey()); |
---|
| 158 | CounterGroup right = rightEntry.getValue(); |
---|
| 159 | if (left == null) { |
---|
| 160 | left = new CounterGroup(right.getName(), right.getDisplayName()); |
---|
| 161 | groups.put(rightEntry.getKey(), left); |
---|
| 162 | } |
---|
| 163 | left.incrAllCounters(right); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | public boolean equals(Object genericRight) { |
---|
| 168 | if (genericRight instanceof Counters) { |
---|
| 169 | Iterator<CounterGroup> right = ((Counters) genericRight).groups. |
---|
| 170 | values().iterator(); |
---|
| 171 | Iterator<CounterGroup> left = groups.values().iterator(); |
---|
| 172 | while (left.hasNext()) { |
---|
| 173 | if (!right.hasNext() || !left.next().equals(right.next())) { |
---|
| 174 | return false; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | return !right.hasNext(); |
---|
| 178 | } |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | public int hashCode() { |
---|
| 183 | return groups.hashCode(); |
---|
| 184 | } |
---|
| 185 | } |
---|