/
JvmGcSampler.java
139 lines (129 loc) · 6.05 KB
/
JvmGcSampler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright 2016 Naver Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.web.mapper.stat;
import com.navercorp.pinpoint.common.server.bo.JvmGcType;
import com.navercorp.pinpoint.common.server.bo.stat.JvmGcBo;
import com.navercorp.pinpoint.web.vo.chart.Point;
import com.navercorp.pinpoint.web.vo.chart.UncollectedPoint;
import com.navercorp.pinpoint.web.vo.stat.chart.DownSampler;
import com.navercorp.pinpoint.web.vo.stat.chart.DownSamplers;
import com.navercorp.pinpoint.web.vo.stat.SampledJvmGc;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author HyunGil Jeong
*/
@Component
public class JvmGcSampler extends AbstractAgentStatSampler<JvmGcBo, SampledJvmGc> {
public static final DownSampler<Long> LONG_DOWN_SAMPLER = DownSamplers.getLongDownSampler(JvmGcBo.UNCOLLECTED_VALUE);
@Override
public SampledJvmGc sampleDataPoints(long timestamp, List<JvmGcBo> dataPoints, JvmGcBo previousDataPoint) {
JvmGcType jvmGcType = JvmGcType.UNKNOWN;
List<Long> heapUseds = new ArrayList<>(dataPoints.size());
List<Long> heapMaxes = new ArrayList<>(dataPoints.size());
List<Long> nonHeapUseds = new ArrayList<>(dataPoints.size());
List<Long> nonHeapMaxes = new ArrayList<>(dataPoints.size());
List<Long> gcOldCounts = new ArrayList<>(dataPoints.size());
List<Long> gcOldTimes = new ArrayList<>(dataPoints.size());
// dataPointsToSample is in descending order
JvmGcBo previousBo = previousDataPoint;
for (int i = dataPoints.size() - 1; i >= 0; --i) {
JvmGcBo jvmGcBo = dataPoints.get(i);
jvmGcType = jvmGcBo.getGcType();
if (jvmGcBo.getHeapUsed() != JvmGcBo.UNCOLLECTED_VALUE) {
heapUseds.add(jvmGcBo.getHeapUsed());
}
if (jvmGcBo.getHeapMax() != JvmGcBo.UNCOLLECTED_VALUE) {
heapMaxes.add(jvmGcBo.getHeapMax());
}
if (jvmGcBo.getNonHeapUsed() != JvmGcBo.UNCOLLECTED_VALUE) {
nonHeapUseds.add(jvmGcBo.getNonHeapUsed());
}
if (jvmGcBo.getNonHeapMax() != JvmGcBo.UNCOLLECTED_VALUE) {
nonHeapMaxes.add(jvmGcBo.getNonHeapMax());
}
if (previousBo != null) {
if (checkJvmRestart(previousBo, jvmGcBo)) {
if (isGcCollected(jvmGcBo)) {
gcOldCounts.add(jvmGcBo.getGcOldCount());
gcOldTimes.add(jvmGcBo.getGcOldTime());
} else {
jvmGcBo.setGcOldCount(0L);
jvmGcBo.setGcOldTime(0L);
}
} else {
if (isGcCollected(jvmGcBo) && isGcCollected(previousBo)) {
gcOldCounts.add(jvmGcBo.getGcOldCount() - previousBo.getGcOldCount());
gcOldTimes.add(jvmGcBo.getGcOldTime() - previousBo.getGcOldTime());
} else {
if (!isGcCollected(jvmGcBo)) {
jvmGcBo.setGcOldCount(previousBo.getGcOldCount());
jvmGcBo.setGcOldTime(previousBo.getGcOldTime());
}
}
}
} else {
if (isGcCollected(jvmGcBo)) {
gcOldCounts.add(0L);
gcOldTimes.add(0L);
}
}
previousBo = jvmGcBo;
}
SampledJvmGc sampledJvmGc = new SampledJvmGc();
sampledJvmGc.setJvmGcType(jvmGcType);
sampledJvmGc.setHeapUsed(createSampledPoint(timestamp, heapUseds));
sampledJvmGc.setHeapMax(createSampledPoint(timestamp, heapMaxes));
sampledJvmGc.setNonHeapUsed(createSampledPoint(timestamp, nonHeapUseds));
sampledJvmGc.setNonHeapMax(createSampledPoint(timestamp, nonHeapMaxes));
sampledJvmGc.setGcOldCount(createSampledPoint(timestamp, gcOldCounts));
sampledJvmGc.setGcOldTime(createSampledPoint(timestamp, gcOldTimes));
return sampledJvmGc;
}
private boolean isGcCollected(JvmGcBo jvmGcBo) {
return jvmGcBo.getGcOldCount() != JvmGcBo.UNCOLLECTED_VALUE && jvmGcBo.getGcOldTime() != JvmGcBo.UNCOLLECTED_VALUE;
}
private boolean checkJvmRestart(JvmGcBo previous, JvmGcBo current) {
if (previous == null) {
return false;
}
if (previous.getStartTimestamp() > 0 && current.getStartTimestamp() > 0) {
return previous.getStartTimestamp() != current.getStartTimestamp();
} else {
// if start timestamp is not serialzied
if (current.getGcOldTime() == JvmGcBo.UNCOLLECTED_VALUE || current.getGcOldCount() == JvmGcBo.UNCOLLECTED_VALUE) {
return false;
} else {
long countDelta = current.getGcOldCount() - previous.getGcOldCount();
long timeDelta = current.getGcOldTime() - previous.getGcOldTime();
return countDelta < 0 && timeDelta < 0;
}
}
}
private Point<Long, Long> createSampledPoint(long timestamp, List<Long> values) {
if (values.isEmpty()) {
return new UncollectedPoint<>(timestamp, JvmGcBo.UNCOLLECTED_VALUE);
} else {
return new Point<>(
timestamp,
LONG_DOWN_SAMPLER.sampleMin(values),
LONG_DOWN_SAMPLER.sampleMax(values),
LONG_DOWN_SAMPLER.sampleAvg(values, 0),
LONG_DOWN_SAMPLER.sampleSum(values));
}
}
}