Skip to content

Commit

Permalink
[TProfile]:增加mysql的慢日志拦截;增加asm3的说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
violetgo committed Jul 15, 2015
1 parent de8abe9 commit a00ce7e
Show file tree
Hide file tree
Showing 15 changed files with 645 additions and 13 deletions.
Binary file added doc/ASM_3.0_使用指南.pdf
Binary file not shown.
15 changes: 14 additions & 1 deletion src/main/java/com/taobao/profile/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public class Manager {
*/
private boolean isDebugMode;

/**
* 记录慢查询的时间;超过这个值的查询才会记录;如果设置为-1表示不启用慢日志记录
*/
private static int recordTime;

/**
* 私有构造器
*/
Expand All @@ -127,7 +132,7 @@ public void initialization() {
moreThanEndTime = (now.compareTo(profConfig.getEndProfTime()) > 0 );
isDebugMode = profConfig.isDebugMode();
PORT = profConfig.getPort();

recordTime = profConfig.getRecordTime();
setProfFilter();
}

Expand Down Expand Up @@ -219,6 +224,14 @@ public boolean isDebugMode() {
return isDebugMode;
}

public static int getRecordTime() {
return recordTime;
}

public static void setRecordTime(int recordTime) {
Manager.recordTime = recordTime;
}

/**
* 设置包名过滤器
*
Expand Down
187 changes: 185 additions & 2 deletions src/main/java/com/taobao/profile/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
*/
package com.taobao.profile;

import java.util.concurrent.atomic.AtomicInteger;

import com.taobao.profile.dependence_query.RecordSlowQuery;
import com.taobao.profile.dependence_query.SlowQueryData;
import com.taobao.profile.runtime.ThreadData;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
* 此类收集应用代码的运行时数据
*
Expand All @@ -34,6 +38,11 @@ public class Profiler {
*/
public static ThreadData[] threadProfile = new ThreadData[size];

/**
* 记录慢日志的数组
*/
public static SlowQueryData[] slowQueryProfile = new SlowQueryData[size];

/**
* 方法开始时调用,采集开始时间
*
Expand Down Expand Up @@ -133,5 +142,179 @@ public static void clearData() {
}
profilerData.clear();
}

for (int index = 0; index < slowQueryProfile.length; index++) {
SlowQueryData profilerData = slowQueryProfile[index];
if (profilerData == null) {
continue;
}
profilerData.clear();
}
}

/** add for dependence**/

/***
* 获取线程ID;如果不在需要profile的区间内;则返回-1
* @return
*/
private static long getThreadID(){
if (!Manager.instance().canProfile()) {
return -1;
}
long threadId = Thread.currentThread().getId();
if (threadId >= size) {
return -1;
}
return threadId;
}

/**
* 获取时间;用于计算函数执行时间;支持纳秒取值;
* @return
*/
private static long getCurTime(){
long curTime;
if (Manager.isNeedNanoTime()) {
curTime = System.nanoTime();
} else {
curTime = System.currentTimeMillis();
}
return curTime;
}

/**
* 获取当前线程的信息;如果不存在则会重新分配一个;
* @param threadId
* @return
*/
private static SlowQueryData getThreadData(long threadId){
SlowQueryData thrData = slowQueryProfile[(int) threadId];
if (thrData == null) {
thrData = new SlowQueryData();
slowQueryProfile[(int) threadId] = thrData;
}
return thrData;
}

/**
* 是否需要记录;只超过10ms的查询
* @param useTime
* @return
*/
private static boolean isNeedRecord(long useTime){
int time = Manager.getRecordTime();
if (Manager.isNeedNanoTime()) {
time = time * 1000000;
if (useTime > time) {
return true;
}
} else if (useTime > time) {
return true;
}
return false;
}

/**
* 弹出方法的堆栈信息;
* @param thrData
* @return
*/
private static Object[] popStack(SlowQueryData thrData){
if(thrData==null){
return null;
}
// 栈太深则抛弃部分数据
if (thrData.profileData.size() > 20000) {
thrData.stackNum--;
thrData.stackFrame.pop();
return null;
}
thrData.stackNum--;
Object[] frameData = thrData.stackFrame.pop();
return frameData;
}

/**
* 开始记录mysql的信息
* @param host
* @param port
* @param db
* @param sql
*/
public static void start4Mysql(String host,int port,String db,String sql){
long threadId = getThreadID();

if(threadId==-1){
return;
}

if(Manager.getRecordTime()==-1){
return;
}

long startTime = getCurTime();
try {
SlowQueryData thrData = getThreadData(threadId);

Object[] frameData = new Object[6];
frameData[0] = thrData.stackNum;
frameData[1] = startTime;
frameData[2] = host;
frameData[3] = port;
frameData[4] = db;
frameData[5] = sql;
thrData.stackFrame.push(frameData);
thrData.stackNum++;
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* mysql记录结束
*/
public static void end4Mysql(){
long threadId = getThreadID();

if(threadId==-1){
return;
}

if(Manager.getRecordTime()==-1){
return;
}

long endTime = getCurTime();

SlowQueryData thrData = getThreadData(threadId);
Object[] frameData = popStack(thrData);
if(frameData==null){
return ;
}

RecordSlowQuery record = new RecordSlowQuery();
Map<String, String> map = new HashMap<String, String>();

map.put("host", (String) frameData[2]);
map.put("port", frameData[3].toString());
map.put("db", (String) frameData[4]);
map.put("sql", (String) frameData[5]);
record.setRequestDesc(map);
record.setUseTime(endTime - (Long) frameData[1]);
record.setType("MYSQL");

StringBuilder sb = new StringBuilder();
sb.append("MYSQL");
sb.append((String) frameData[2]);
sb.append(frameData[3].toString());
sb.append((String) frameData[4]);

if(!isNeedRecord(record.getUseTime())){
return;
}
map.put("nanoTime", Manager.isNeedNanoTime() + "");

thrData.profileData.push(record);
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/taobao/profile/config/ProfConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public class ProfConfig {
*/
private int port;

/**
* 记录慢查询的时间;超过这个值的查询才会记录;如果设置为-1表示不启用慢日志记录
*/
private int recordTime;

/**
* 构造方法
*/
Expand Down Expand Up @@ -226,6 +231,8 @@ private void loadConfig(Properties properties) throws VariableNotFoundException
String excludeClassLoader = properties.getProperty("excludeClassLoader");
String debugMode = properties.getProperty("debugMode");
String port = properties.getProperty("port");
String recordTime = properties.getProperty("recordTime","-1");

setPort(port == null ? 50000 : Integer.valueOf(port));
setDebugMode("true".equalsIgnoreCase(debugMode == null ? null : debugMode.trim()));
setExcludeClassLoader(excludeClassLoader);
Expand Down Expand Up @@ -253,6 +260,13 @@ private void loadConfig(Properties properties) throws VariableNotFoundException
} else {
setSamplerIntervalTime(Integer.valueOf(samplerIntervalTime.trim()));
}

if(recordTime==null){
setRecordTime(-1);
}else{
setRecordTime(Integer.valueOf(recordTime));
}

}


Expand Down Expand Up @@ -462,4 +476,12 @@ public int getPort() {
public void setPort(int port) {
this.port = port;
}

public int getRecordTime() {
return recordTime;
}

public void setRecordTime(int recordTime) {
this.recordTime = recordTime;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/taobao/profile/config/ProfFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ProfFilter {
/**
* 注入的Package集合
*/
private static Set<String> includePackage = new HashSet<String>();
protected static Set<String> includePackage = new HashSet<String>();
/**
* 不注入的Package集合
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.taobao.profile.dependence_query;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;

/**
* @author weigao
* @since 15/5/25
*/
public abstract class IClassAdapter extends ClassAdapter {

/**
* 本地唯一的跟踪ID;使用纳秒作为标记
*/
protected long localTrackID;

/**
* asm注入的类名
*/
protected String mClassName;

public IClassAdapter(ClassVisitor classVisitor, String theClass) {
super(classVisitor);
this.mClassName = theClass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.taobao.profile.dependence_query;

import com.taobao.profile.Profiler;
import com.taobao.profile.runtime.MethodCache;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;

/**
* @author weigao
* @since 15/5/25
*/
public abstract class IMethodAdapter extends MethodAdapter {

/**
* 方法ID
*/
protected int mMethodId = 0;

public IMethodAdapter(MethodVisitor methodVisitor,String fileName, String className, String methodName) {
super(methodVisitor);
mMethodId = MethodCache.Request();
MethodCache.UpdateMethodName(mMethodId, fileName, className, methodName);
// 记录方法数
Profiler.instrumentMethodCount.getAndIncrement();
}

}
Loading

0 comments on commit a00ce7e

Please sign in to comment.