Skip to content

Commit

Permalink
[#2584] Refactor ActiveTraceDump
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad authored and koo-taejin committed Sep 13, 2017
1 parent 694c4a8 commit 7b10634
Show file tree
Hide file tree
Showing 23 changed files with 1,020 additions and 381 deletions.
Expand Up @@ -72,19 +72,53 @@ public static ThreadInfo[] dumpAllThread() {
// }
}

/**
* @deprecated Since 1.7.0. Use {@link #getThreadInfo(long)}
*/
@Deprecated
public static ThreadInfo findThread(Thread thread) {
return findThread(thread.getId());
Assert.requireNonNull(thread, "thread must not be null");
return getThreadInfo(thread.getId());
}

/**
* @deprecated Since 1.7.0. Use {@link #getThreadInfo(long, int)}
*/
@Deprecated
public static ThreadInfo findThread(Thread thread, int stackTraceMaxDepth) {
return findThread(thread.getId(), stackTraceMaxDepth);
Assert.requireNonNull(thread, "thread must not be null");
return getThreadInfo(thread.getId(), stackTraceMaxDepth);
}

/**
* @deprecated Since 1.7.0. Use {@link #getThreadInfo(long)}
*/
@Deprecated
public static ThreadInfo findThread(long id) {
return findThread(id, DEFAULT_STACK_TRACE_MAX_DEPTH);
return getThreadInfo(id, DEFAULT_STACK_TRACE_MAX_DEPTH);
}

public static ThreadInfo getThreadInfo(long id) {
return getThreadInfo(id, DEFAULT_STACK_TRACE_MAX_DEPTH);
}

/**
* @deprecated Since 1.7.0. Use {@link #getThreadInfo(long, int)}
*/
@Deprecated
public static ThreadInfo findThread(long id, int stackTraceMaxDepth) {
return getThreadInfo(id, stackTraceMaxDepth);
}

public static ThreadInfo getThreadInfo(long id, int stackTraceMaxDepth) {
if (stackTraceMaxDepth <= 0) {
return THREAD_MX_BEAN.getThreadInfo(id);
} else {
return THREAD_MX_BEAN.getThreadInfo(id, stackTraceMaxDepth);
}
}

public static ThreadInfo[] findThread(long[] id, int stackTraceMaxDepth) {
if (stackTraceMaxDepth <= 0) {
return THREAD_MX_BEAN.getThreadInfo(id);
} else {
Expand Down
Expand Up @@ -27,7 +27,9 @@ public interface ActiveTraceRepository {

ActiveTraceHistogram getActiveTraceHistogram(long timeStamp);

List<ActiveTraceSnapshot> collect();
List<ActiveTraceSnapshot> snapshot();

List<Long> getThreadIdList();

ActiveTraceHandle register(TraceRoot traceRoot);

Expand Down
Expand Up @@ -122,12 +122,13 @@ private ActiveTraceHandle register0(ActiveTrace activeTrace) {

// @ThreadSafe
@Override
public List<ActiveTraceSnapshot> collect() {
public List<ActiveTraceSnapshot> snapshot() {
if (this.activeTraceInfoMap.isEmpty()) {
return Collections.emptyList();
}
final Collection<ActiveTrace> activeTraceCollection = this.activeTraceInfoMap.values();
final List<ActiveTraceSnapshot> collectData = new ArrayList<ActiveTraceSnapshot>(activeTraceCollection.size());

for (ActiveTrace trace : activeTraceCollection) {
final long startTime = trace.getStartTime();
// not started
Expand All @@ -143,6 +144,31 @@ public List<ActiveTraceSnapshot> collect() {
return collectData;
}


// @ThreadSafe
@Override
public List<Long> getThreadIdList() {
if (this.activeTraceInfoMap.isEmpty()) {
return Collections.emptyList();
}
final Collection<ActiveTrace> activeTraceCollection = this.activeTraceInfoMap.values();
final List<Long> collectData = new ArrayList<Long>(activeTraceCollection.size());

for (ActiveTrace trace : activeTraceCollection) {
final long startTime = trace.getStartTime();
// not started
if (!isStarted(startTime)) {
continue;
}
final ActiveTraceSnapshot snapshot = trace.snapshot();
collectData.add(snapshot.getThreadId());
}
if (isDebug) {
logger.debug("activeTraceSnapshot size:{}", collectData.size());
}
return collectData;
}

// @ThreadSafe
@Override
public ActiveTraceHistogram getActiveTraceHistogram(long currentTime) {
Expand Down
Expand Up @@ -43,7 +43,12 @@ public ActiveTraceHistogram getActiveTraceHistogram(long timeStamp) {
}

@Override
public List<ActiveTraceSnapshot> collect() {
public List<Long> getThreadIdList() {
return null;
}

@Override
public List<ActiveTraceSnapshot> snapshot() {
return Collections.emptyList();
}

Expand Down
Expand Up @@ -80,7 +80,7 @@ void doTask() {
deadlockOutput.append("================================================================").append(LINE_SEPARATOR);

for (long deadlockedThreadId : deadlockedThreadIds) {
ThreadInfo threadInfo = ThreadMXBeanUtils.findThread(deadlockedThreadId);
ThreadInfo threadInfo = ThreadMXBeanUtils.getThreadInfo(deadlockedThreadId);
deadlockOutput.append(createThreadDump(threadInfo));
}
deadlockOutput.append("================================================================").append(LINE_SEPARATOR);
Expand Down
@@ -0,0 +1,151 @@
/*
* 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.receiver.service;

import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.common.util.ThreadMXBeanUtils;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceSnapshot;


import java.lang.management.ThreadInfo;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* @author Woonduk Kang(emeroad)
*/
public class ActiveThreadDumpCoreService {

private final ActiveTraceRepository activeTraceRepository;

private final Comparator<ThreadDump> reverseOrder = Collections.reverseOrder(new ThreadDumpComparator());

public ActiveThreadDumpCoreService(ActiveTraceRepository activeTraceRepository) {
this.activeTraceRepository = Assert.requireNonNull(activeTraceRepository, "activeTraceRepository must not be null");
}

public Collection<ThreadDump> getActiveThreadDumpList(ThreadDumpRequest request) {

final List<ActiveTraceSnapshot> activeTraceInfoList = activeTraceRepository.snapshot();

return getActiveThreadDumpList(activeTraceInfoList, request);
}

private Collection<ThreadDump> getActiveThreadDumpList(List<ActiveTraceSnapshot> activeTraceInfoList, ThreadDumpRequest request) {

if (request.isEnableFilter()) {
return filterActiveThreadDump(activeTraceInfoList, request);
} else {

return getAllActiveThreadDump(activeTraceInfoList, request);
}
}

private Collection<ThreadDump> filterActiveThreadDump(List<ActiveTraceSnapshot> activeTraceInfoList, ThreadDumpRequest request) {

final Collection<ThreadDump> result = new LimitedList<ThreadDump>(request.getLimit(), reverseOrder);

for (ActiveTraceSnapshot activeTraceInfo : activeTraceInfoList) {
final long threadId = activeTraceInfo.getThreadId();
if (!isTraceThread(threadId)) {
continue;
}

final ThreadDump threadDump = filter(activeTraceInfo, request);
if (threadDump != null) {
result.add(threadDump);
}
}

return result;
}

private ThreadDump filter(ActiveTraceSnapshot activeTraceInfo, ThreadDumpRequest request) {

if (request.isEnableLocalTransactionIdFilter()) {

final long localTransactionId = activeTraceInfo.getLocalTransactionId();
if (request.findLocalTransactionId(localTransactionId)) {

final long threadId = activeTraceInfo.getThreadId();
final ThreadInfo threadInfo = getThreadInfo(threadId, request.getStackTrace());
if (threadInfo != null) {
return newThreadDump(activeTraceInfo, threadInfo);
}
}
}

if (request.isEnableThreadNameFilter()) {
// native call
final long threadId = activeTraceInfo.getThreadId();
final ThreadInfo threadInfo = getThreadInfo(threadId, request.getStackTrace());
if (threadInfo != null) {
if (request.findThreadName(threadInfo.getThreadName())) {
return newThreadDump(activeTraceInfo, threadInfo);
}
}
}

return null;
}

private Collection<ThreadDump> getAllActiveThreadDump(List<ActiveTraceSnapshot> activeTraceInfoList, ThreadDumpRequest request) {
Collection<ThreadDump> activeThreadDumpList = new LimitedList<ThreadDump>(request.getLimit(), reverseOrder);

for (ActiveTraceSnapshot activeTraceInfo : activeTraceInfoList) {
final long threadId = activeTraceInfo.getThreadId();
if (!isTraceThread(threadId)) {
continue;
}
final ThreadInfo threadInfo = getThreadInfo(threadId, request.getStackTrace());
if (threadInfo != null) {
ThreadDump threadDump = newThreadDump(activeTraceInfo, threadInfo);
activeThreadDumpList.add(threadDump);
}
}

return activeThreadDumpList;
}

private boolean isTraceThread(long threadId) {
if (threadId == -1) {
return false;
}
return true;
}



private ThreadInfo getThreadInfo(long threadId, StackTrace dumpType) {
if (threadId == -1) {
return null;
}

if (StackTrace.DUMP == dumpType) {
return ThreadMXBeanUtils.getThreadInfo(threadId);
} else {
return ThreadMXBeanUtils.getThreadInfo(threadId, 0);
}
}

private ThreadDump newThreadDump(ActiveTraceSnapshot activeTraceInfo, ThreadInfo threadInfo) {
return new ThreadDump(activeTraceInfo, threadInfo);
}
}

0 comments on commit 7b10634

Please sign in to comment.