Skip to content

Commit 244969e

Browse files
committed
[pinpoint-apm#2853] Optimize trace format of agent
- Refactor TraceFormat
1 parent e5179e2 commit 244969e

40 files changed

+1489
-236
lines changed

‎collector/src/test/java/com/navercorp/pinpoint/collector/receiver/udp/SpanStreamUDPSenderTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.navercorp.pinpoint.collector.receiver.AbstractDispatchHandler;
2222
import com.navercorp.pinpoint.collector.receiver.DataReceiver;
2323
import com.navercorp.pinpoint.common.trace.ServiceType;
24-
import com.navercorp.pinpoint.profiler.context.DefaultSpanChunkFactory;
24+
import com.navercorp.pinpoint.profiler.context.SpanChunkFactoryV1;
2525
import com.navercorp.pinpoint.profiler.context.Span;
2626
import com.navercorp.pinpoint.profiler.context.SpanChunk;
2727
import com.navercorp.pinpoint.profiler.context.SpanChunkFactory;
@@ -152,7 +152,7 @@ private Span createSpan(int spanEventSize) throws InterruptedException {
152152

153153
private SpanChunk createSpanChunk(int spanEventSize) throws InterruptedException {
154154

155-
SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory("applicationName", "agentId", 0, ServiceType.STAND_ALONE);
155+
SpanChunkFactory spanChunkFactory = new SpanChunkFactoryV1("applicationName", "agentId", 0, ServiceType.STAND_ALONE);
156156

157157
List<SpanEvent> originalSpanEventList = createSpanEventList(spanEventSize);
158158
SpanChunk spanChunk = spanChunkFactory.create(originalSpanEventList);

‎profiler-test/src/main/java/com/navercorp/pinpoint/test/MockTraceContextFactory.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.navercorp.pinpoint.profiler.context.id.AtomicIdGenerator;
2929
import com.navercorp.pinpoint.profiler.context.CallStackFactory;
3030
import com.navercorp.pinpoint.profiler.context.id.DefaultAsyncIdGenerator;
31-
import com.navercorp.pinpoint.profiler.context.DefaultCallStackFactory;
31+
import com.navercorp.pinpoint.profiler.context.CallStackFactoryV1;
3232
import com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory;
3333
import com.navercorp.pinpoint.profiler.context.DefaultServerMetaDataHolder;
3434
import com.navercorp.pinpoint.profiler.context.DefaultSpanFactory;
@@ -120,7 +120,7 @@ public MockTraceContextFactory(ProfilerConfig profilerConfig) {
120120
this.sqlMetaDataService = new DefaultSqlMetaDataService(agentId, agentStartTime, enhancedDataSender, jdbcSqlCacheSize);
121121

122122

123-
CallStackFactory callStackFactory = new DefaultCallStackFactory(64);
123+
CallStackFactory callStackFactory = new CallStackFactoryV1(64);
124124
TraceIdFactory traceIdFactory = new DefaultTraceIdFactory(agentId, agentStartTime, idGenerator);
125125
SpanFactory spanFactory = new DefaultSpanFactory(applicationName, agentId, agentStartTime, agentServiceType);
126126

Lines changed: 12 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2014 NAVER Corp.
2+
* Copyright 2017 NAVER Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,129 +16,21 @@
1616

1717
package com.navercorp.pinpoint.profiler.context;
1818

19-
import java.util.Arrays;
20-
2119
/**
22-
* @author netspider
23-
* @author emeroad
24-
* @author jaehong.kim
20+
* @author Woonduk Kang(emeroad)
2521
*/
26-
public class CallStack {
27-
private static final int STACK_SIZE = 8;
28-
private static final int DEFAULT_INDEX = 0;
29-
30-
private SpanEvent[] stack = new SpanEvent[STACK_SIZE];
31-
32-
private final Span span;
33-
private final int maxDepth;
34-
private int index = DEFAULT_INDEX;
35-
private int overflowIndex = 0;
36-
private short sequence;
37-
private int latestStackIndex = 0;
38-
39-
public CallStack(Span span) {
40-
this(span, -1);
41-
}
42-
43-
public CallStack(Span span, int maxDepth) {
44-
this.span = span;
45-
this.maxDepth = maxDepth;
46-
}
47-
48-
public Span getSpan() {
49-
return span;
50-
}
51-
52-
public int getIndex() {
53-
if(isOverflow()) {
54-
return index + overflowIndex;
55-
}
56-
57-
return index;
58-
}
59-
60-
public int push(final SpanEvent spanEvent) {
61-
if (isOverflow()) {
62-
overflowIndex++;
63-
return index + overflowIndex;
64-
}
65-
66-
checkExtend(index + 1);
67-
spanEvent.setSequence(sequence++);
68-
stack[index++] = spanEvent;
69-
if(latestStackIndex != index) {
70-
latestStackIndex = index;
71-
spanEvent.setDepth(latestStackIndex);
72-
}
73-
74-
return index;
75-
}
76-
77-
private void checkExtend(final int size) {
78-
final SpanEvent[] originalStack = this.stack;
79-
if (size >= originalStack.length) {
80-
final int copyStackSize = originalStack.length << 1;
81-
final SpanEvent[] copyStack = new SpanEvent[copyStackSize];
82-
System.arraycopy(originalStack, 0, copyStack, 0, originalStack.length);
83-
this.stack = copyStack;
84-
}
85-
}
86-
87-
public SpanEvent pop() {
88-
if(isOverflow() && overflowIndex > 0) {
89-
overflowIndex--;
90-
return new SpanEvent(span);
91-
}
92-
93-
final SpanEvent spanEvent = peek();
94-
if (spanEvent != null) {
95-
stack[index - 1] = null;
96-
index--;
97-
}
98-
99-
return spanEvent;
100-
}
101-
102-
public SpanEvent peek() {
103-
if (index == DEFAULT_INDEX) {
104-
return null;
105-
}
106-
107-
if(isOverflow() && overflowIndex > 0) {
108-
return new SpanEvent(span);
109-
}
22+
public interface CallStack {
23+
int getIndex();
11024

111-
return stack[index - 1];
112-
}
25+
int push(SpanEvent spanEvent);
11326

114-
public boolean empty() {
115-
return index == DEFAULT_INDEX;
116-
}
27+
SpanEvent pop();
11728

118-
public SpanEvent[] copyStackFrame() {
119-
// without synchronization arraycopy, last index is null reference
120-
final SpanEvent[] currentStack = this.stack;
121-
final SpanEvent[] copyStack = new SpanEvent[currentStack.length];
122-
System.arraycopy(currentStack, 0, copyStack, 0, currentStack.length);
123-
return copyStack;
124-
}
29+
SpanEvent peek();
12530

126-
public int getMaxDepth() {
127-
return maxDepth;
128-
}
31+
boolean empty();
12932

130-
boolean isOverflow() {
131-
return maxDepth != -1 && maxDepth < index;
132-
}
33+
SpanEvent[] copyStackFrame();
13334

134-
@Override
135-
public String toString() {
136-
StringBuilder builder = new StringBuilder();
137-
builder.append("{stack=");
138-
builder.append(Arrays.toString(stack));
139-
builder.append(", index=");
140-
builder.append(index);
141-
builder.append("}");
142-
return builder.toString();
143-
}
144-
}
35+
int getMaxDepth();
36+
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,19 @@
1616

1717
package com.navercorp.pinpoint.profiler.context;
1818

19-
import com.google.inject.Inject;
20-
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
21-
2219
/**
2320
* @author Woonduk Kang(emeroad)
2421
*/
25-
public class DefaultCallStackFactory implements CallStackFactory {
22+
public class CallStackFactoryV1 implements CallStackFactory {
2623

2724
private final int maxDepth;
2825

29-
@Inject
30-
public DefaultCallStackFactory(ProfilerConfig profilerConfig) {
31-
this(profilerConfig.getCallStackMaxDepth());
32-
}
33-
34-
public DefaultCallStackFactory(int maxDepth) {
26+
public CallStackFactoryV1(int maxDepth) {
3527
this.maxDepth = maxDepth;
3628
}
3729

3830
@Override
3931
public CallStack newCallStack(Span span) {
40-
return new CallStack(span, maxDepth);
32+
return new DepthCompressCallStack(span, maxDepth);
4133
}
4234
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2017 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.navercorp.pinpoint.profiler.context;
18+
19+
/**
20+
* @author Woonduk Kang(emeroad)
21+
*/
22+
public class CallStackFactoryV2 implements CallStackFactory {
23+
24+
private final int maxDepth;
25+
26+
public CallStackFactoryV2(int maxDepth) {
27+
this.maxDepth = maxDepth;
28+
}
29+
30+
@Override
31+
public CallStack newCallStack(Span span) {
32+
return new DefaultCallStack(span, maxDepth);
33+
}
34+
}

0 commit comments

Comments
 (0)