1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.comm.netty; |
20 | |
|
21 | |
import com.yammer.metrics.core.Histogram; |
22 | |
import com.yammer.metrics.core.Meter; |
23 | |
import com.yammer.metrics.core.NoOpHistogram; |
24 | |
import com.yammer.metrics.core.NoOpMeter; |
25 | |
import io.netty.buffer.ByteBuf; |
26 | |
import org.apache.giraph.metrics.MeterDesc; |
27 | |
import org.apache.giraph.metrics.MetricNames; |
28 | |
import org.apache.giraph.metrics.SuperstepMetricsRegistry; |
29 | |
import org.apache.giraph.time.SystemTime; |
30 | |
import org.apache.giraph.time.Time; |
31 | |
|
32 | |
import java.text.DecimalFormat; |
33 | |
import java.util.concurrent.atomic.AtomicLong; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ByteCounterDelegate implements ByteCounter { |
40 | |
|
41 | |
public static final double MEGABYTE = 1024f * 1024f; |
42 | |
|
43 | 0 | private static final DecimalFormat DOUBLE_FORMAT = |
44 | |
new DecimalFormat("#######.####"); |
45 | |
|
46 | 0 | private static final Time TIME = SystemTime.get(); |
47 | |
|
48 | 0 | private final AtomicLong bytesProcessed = new AtomicLong(); |
49 | |
|
50 | 0 | private final AtomicLong bytesProcessedPerSuperstep = new AtomicLong(); |
51 | |
|
52 | 0 | private final AtomicLong processedRequests = new AtomicLong(); |
53 | |
|
54 | 0 | private final AtomicLong startMsecs = new AtomicLong(); |
55 | |
|
56 | 0 | private final AtomicLong metricsWindowLastUpdatedMsecs = new AtomicLong(); |
57 | |
|
58 | |
|
59 | |
|
60 | 0 | private Meter processedRequestsMeter = NoOpMeter.INSTANCE; |
61 | |
|
62 | 0 | private Histogram processedBytesHist = NoOpHistogram.INSTANCE; |
63 | |
|
64 | |
|
65 | |
private final boolean isInbound; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 0 | public ByteCounterDelegate(boolean isInBound) { |
73 | 0 | this.isInbound = isInBound; |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public void newSuperstep(SuperstepMetricsRegistry superstepMetrics) { |
82 | 0 | if (isInbound) { |
83 | 0 | processedRequestsMeter = superstepMetrics.getMeter( |
84 | |
MeterDesc.RECEIVED_REQUESTS); |
85 | 0 | processedBytesHist = superstepMetrics.getUniformHistogram( |
86 | |
MetricNames.RECEIVED_BYTES); |
87 | |
} else { |
88 | 0 | processedRequestsMeter = superstepMetrics.getMeter( |
89 | |
MeterDesc.SENT_REQUESTS); |
90 | 0 | processedBytesHist = superstepMetrics.getUniformHistogram( |
91 | |
MetricNames.SENT_BYTES); |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public int byteBookkeeper(ByteBuf buf) { |
102 | 0 | int processedBytes = buf.readableBytes(); |
103 | 0 | bytesProcessed.addAndGet(processedBytes); |
104 | 0 | bytesProcessedPerSuperstep.addAndGet(processedBytes); |
105 | 0 | processedBytesHist.update(processedBytes); |
106 | 0 | processedRequests.incrementAndGet(); |
107 | 0 | processedRequestsMeter.mark(); |
108 | 0 | return processedBytes; |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public void resetBytes() { |
115 | 0 | bytesProcessed.set(0); |
116 | 0 | processedRequests.set(0); |
117 | 0 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void resetStartMsecs() { |
123 | 0 | startMsecs.set(TIME.getMilliseconds()); |
124 | 0 | } |
125 | |
|
126 | |
@Override |
127 | |
public void resetAll() { |
128 | 0 | resetBytes(); |
129 | 0 | resetStartMsecs(); |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public long getBytesProcessedPerSuperstep() { |
137 | 0 | return bytesProcessedPerSuperstep.get(); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public void resetBytesProcessedPerSuperstep() { |
144 | 0 | bytesProcessedPerSuperstep.set(0); |
145 | 0 | } |
146 | |
|
147 | |
public long getBytesProcessed() { |
148 | 0 | return bytesProcessed.get(); |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public double getMbytesPerSecProcessed() { |
155 | 0 | return bytesProcessed.get() * 1000f / |
156 | 0 | (1 + TIME.getMilliseconds() - startMsecs.get()) / MEGABYTE; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public String getMetricsHelper(double mBytesProcessed, |
166 | |
double mBytesProcessedPerReq) { |
167 | 0 | if (isInbound) { |
168 | 0 | return "MBytes/sec received = " + |
169 | 0 | DOUBLE_FORMAT.format(getMbytesPerSecProcessed()) + |
170 | 0 | ", MBytesReceived = " + DOUBLE_FORMAT.format(mBytesProcessed) + |
171 | |
", ave received req MBytes = " + |
172 | 0 | DOUBLE_FORMAT.format(mBytesProcessedPerReq) + |
173 | |
", secs waited = " + |
174 | 0 | ((TIME.getMilliseconds() - startMsecs.get()) / 1000f); |
175 | |
} else { |
176 | 0 | return "MBytes/sec sent = " + |
177 | 0 | DOUBLE_FORMAT.format(getMbytesPerSecProcessed()) + |
178 | 0 | ", MBytesSent = " + DOUBLE_FORMAT.format(mBytesProcessed) + |
179 | |
", ave sent req MBytes = " + |
180 | 0 | DOUBLE_FORMAT.format(mBytesProcessedPerReq) + |
181 | |
", secs waited = " + |
182 | 0 | ((TIME.getMilliseconds() - startMsecs.get()) / 1000f); |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
@Override |
187 | |
public String getMetrics() { |
188 | 0 | double mBytesProcessed = bytesProcessed.get() / MEGABYTE; |
189 | 0 | long curProcessedRequests = processedRequests.get(); |
190 | 0 | double mBytesProcessedPerReq = (curProcessedRequests == 0) ? 0 : |
191 | |
mBytesProcessed / curProcessedRequests; |
192 | |
|
193 | 0 | return getMetricsHelper(mBytesProcessed, mBytesProcessedPerReq); |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public String getMetricsWindow(int minMsecsWindow) { |
198 | 0 | long lastUpdatedMsecs = metricsWindowLastUpdatedMsecs.get(); |
199 | 0 | long curMsecs = TIME.getMilliseconds(); |
200 | 0 | if (curMsecs - lastUpdatedMsecs > minMsecsWindow) { |
201 | |
|
202 | 0 | if (metricsWindowLastUpdatedMsecs.compareAndSet(lastUpdatedMsecs, |
203 | |
curMsecs)) { |
204 | 0 | String metrics = getMetrics(); |
205 | 0 | resetAll(); |
206 | 0 | return metrics; |
207 | |
} |
208 | |
} |
209 | 0 | return null; |
210 | |
} |
211 | |
} |