Skip to content

Commit

Permalink
[#2853] Optimize trace format of agent
Browse files Browse the repository at this point in the history
 - Refactor TraceFormat
  • Loading branch information
emeroad committed Apr 24, 2017
1 parent e5179e2 commit 244969e
Show file tree
Hide file tree
Showing 40 changed files with 1,489 additions and 236 deletions.
Expand Up @@ -21,7 +21,7 @@
import com.navercorp.pinpoint.collector.receiver.AbstractDispatchHandler;
import com.navercorp.pinpoint.collector.receiver.DataReceiver;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.profiler.context.DefaultSpanChunkFactory;
import com.navercorp.pinpoint.profiler.context.SpanChunkFactoryV1;
import com.navercorp.pinpoint.profiler.context.Span;
import com.navercorp.pinpoint.profiler.context.SpanChunk;
import com.navercorp.pinpoint.profiler.context.SpanChunkFactory;
Expand Down Expand Up @@ -152,7 +152,7 @@ private Span createSpan(int spanEventSize) throws InterruptedException {

private SpanChunk createSpanChunk(int spanEventSize) throws InterruptedException {

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

List<SpanEvent> originalSpanEventList = createSpanEventList(spanEventSize);
SpanChunk spanChunk = spanChunkFactory.create(originalSpanEventList);
Expand Down
Expand Up @@ -28,7 +28,7 @@
import com.navercorp.pinpoint.profiler.context.id.AtomicIdGenerator;
import com.navercorp.pinpoint.profiler.context.CallStackFactory;
import com.navercorp.pinpoint.profiler.context.id.DefaultAsyncIdGenerator;
import com.navercorp.pinpoint.profiler.context.DefaultCallStackFactory;
import com.navercorp.pinpoint.profiler.context.CallStackFactoryV1;
import com.navercorp.pinpoint.profiler.context.recorder.DefaultRecorderFactory;
import com.navercorp.pinpoint.profiler.context.DefaultServerMetaDataHolder;
import com.navercorp.pinpoint.profiler.context.DefaultSpanFactory;
Expand Down Expand Up @@ -120,7 +120,7 @@ public MockTraceContextFactory(ProfilerConfig profilerConfig) {
this.sqlMetaDataService = new DefaultSqlMetaDataService(agentId, agentStartTime, enhancedDataSender, jdbcSqlCacheSize);


CallStackFactory callStackFactory = new DefaultCallStackFactory(64);
CallStackFactory callStackFactory = new CallStackFactoryV1(64);
TraceIdFactory traceIdFactory = new DefaultTraceIdFactory(agentId, agentStartTime, idGenerator);
SpanFactory spanFactory = new DefaultSpanFactory(applicationName, agentId, agentStartTime, agentServiceType);

Expand Down
@@ -1,11 +1,11 @@
/*
* Copyright 2014 NAVER Corp.
* Copyright 2017 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
* 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,
Expand All @@ -16,129 +16,21 @@

package com.navercorp.pinpoint.profiler.context;

import java.util.Arrays;

/**
* @author netspider
* @author emeroad
* @author jaehong.kim
* @author Woonduk Kang(emeroad)
*/
public class CallStack {
private static final int STACK_SIZE = 8;
private static final int DEFAULT_INDEX = 0;

private SpanEvent[] stack = new SpanEvent[STACK_SIZE];

private final Span span;
private final int maxDepth;
private int index = DEFAULT_INDEX;
private int overflowIndex = 0;
private short sequence;
private int latestStackIndex = 0;

public CallStack(Span span) {
this(span, -1);
}

public CallStack(Span span, int maxDepth) {
this.span = span;
this.maxDepth = maxDepth;
}

public Span getSpan() {
return span;
}

public int getIndex() {
if(isOverflow()) {
return index + overflowIndex;
}

return index;
}

public int push(final SpanEvent spanEvent) {
if (isOverflow()) {
overflowIndex++;
return index + overflowIndex;
}

checkExtend(index + 1);
spanEvent.setSequence(sequence++);
stack[index++] = spanEvent;
if(latestStackIndex != index) {
latestStackIndex = index;
spanEvent.setDepth(latestStackIndex);
}

return index;
}

private void checkExtend(final int size) {
final SpanEvent[] originalStack = this.stack;
if (size >= originalStack.length) {
final int copyStackSize = originalStack.length << 1;
final SpanEvent[] copyStack = new SpanEvent[copyStackSize];
System.arraycopy(originalStack, 0, copyStack, 0, originalStack.length);
this.stack = copyStack;
}
}

public SpanEvent pop() {
if(isOverflow() && overflowIndex > 0) {
overflowIndex--;
return new SpanEvent(span);
}

final SpanEvent spanEvent = peek();
if (spanEvent != null) {
stack[index - 1] = null;
index--;
}

return spanEvent;
}

public SpanEvent peek() {
if (index == DEFAULT_INDEX) {
return null;
}

if(isOverflow() && overflowIndex > 0) {
return new SpanEvent(span);
}
public interface CallStack {
int getIndex();

return stack[index - 1];
}
int push(SpanEvent spanEvent);

public boolean empty() {
return index == DEFAULT_INDEX;
}
SpanEvent pop();

public SpanEvent[] copyStackFrame() {
// without synchronization arraycopy, last index is null reference
final SpanEvent[] currentStack = this.stack;
final SpanEvent[] copyStack = new SpanEvent[currentStack.length];
System.arraycopy(currentStack, 0, copyStack, 0, currentStack.length);
return copyStack;
}
SpanEvent peek();

public int getMaxDepth() {
return maxDepth;
}
boolean empty();

boolean isOverflow() {
return maxDepth != -1 && maxDepth < index;
}
SpanEvent[] copyStackFrame();

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{stack=");
builder.append(Arrays.toString(stack));
builder.append(", index=");
builder.append(index);
builder.append("}");
return builder.toString();
}
}
int getMaxDepth();
}
Expand Up @@ -16,27 +16,19 @@

package com.navercorp.pinpoint.profiler.context;

import com.google.inject.Inject;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;

/**
* @author Woonduk Kang(emeroad)
*/
public class DefaultCallStackFactory implements CallStackFactory {
public class CallStackFactoryV1 implements CallStackFactory {

private final int maxDepth;

@Inject
public DefaultCallStackFactory(ProfilerConfig profilerConfig) {
this(profilerConfig.getCallStackMaxDepth());
}

public DefaultCallStackFactory(int maxDepth) {
public CallStackFactoryV1(int maxDepth) {
this.maxDepth = maxDepth;
}

@Override
public CallStack newCallStack(Span span) {
return new CallStack(span, maxDepth);
return new DepthCompressCallStack(span, maxDepth);
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2017 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.profiler.context;

/**
* @author Woonduk Kang(emeroad)
*/
public class CallStackFactoryV2 implements CallStackFactory {

private final int maxDepth;

public CallStackFactoryV2(int maxDepth) {
this.maxDepth = maxDepth;
}

@Override
public CallStack newCallStack(Span span) {
return new DefaultCallStack(span, maxDepth);
}
}

0 comments on commit 244969e

Please sign in to comment.