Skip to content

Instantly share code, notes, and snippets.

@benelog
Last active September 3, 2021 19:18
Show Gist options
  • Save benelog/1246239 to your computer and use it in GitHub Desktop.
Save benelog/1246239 to your computer and use it in GitHub Desktop.
Btrace scripts
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.BTraceUtils.Aggregations;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.aggregation.Aggregation;
import com.sun.btrace.aggregation.AggregationFunction;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Duration;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnEvent;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.TLS;
@BTrace
public class ConnectionMonitor {
private static Aggregation servletDuration = Aggregations.newAggregation(AggregationFunction.AVERAGE);
private static Aggregation servletCount = Aggregations.newAggregation(AggregationFunction.COUNT);
private static Aggregation prepareDuration = Aggregations.newAggregation(AggregationFunction.AVERAGE);
private static Aggregation prepareCount = Aggregations.newAggregation(AggregationFunction.COUNT);
private static Aggregation executeDuration = Aggregations.newAggregation(AggregationFunction.AVERAGE);
private static Aggregation executeCount = Aggregations.newAggregation(AggregationFunction.COUNT);
@TLS static long executeTimeInThread;
@TLS static long prepareTimeInThread;
@OnMethod(clazz = "org.springframework.web.servlet.FrameworkServlet",
method = "service",
location = @Location(Kind.RETURN))
public static void servlet(@Duration long duration) {
Aggregations.addToAggregation(servletDuration, duration/1000);
Aggregations.addToAggregation(servletCount, 1);
}
@OnMethod(clazz = "cubrid.jdbc.driver.CUBRIDConnection",
method = "prepareStatement",
location = @Location(Kind.RETURN))
public static void statementPrepare(@Duration long duration) {
Aggregations.addToAggregation(prepareDuration, duration/1000);
Aggregations.addToAggregation(prepareCount, 1);
}
@OnMethod(clazz = "cubrid.jdbc.driver.CUBRIDPreparedStatement",
method = "execute",
location = @Location(Kind.RETURN))
public static void statementExecute(@Duration long duration) {
Aggregations.addToAggregation(executeDuration, duration/1000);
Aggregations.addToAggregation(executeCount, 1);
}
@OnEvent
public static void summary() {
print("# org.springframework.web.servlet.FrameworkServlet.service");
Aggregations.printAggregation("- call count:", servletCount);
Aggregations.printAggregation("- average duration(microseconds):", servletDuration);
print("# cubrid.jdbc.driver.CUBRIDConnection.prepareStatement");
Aggregations.printAggregation("- call count : ", prepareCount);
Aggregations.printAggregation("- average duration(microseconds) :", prepareDuration);
print("# cubrid.jdbc.driver.CUBRIDPreparedStatement.execute");
Aggregations.printAggregation("- call count :", executeCount);
Aggregations.printAggregation("- average duration(microseconds):", executeDuration);
Sys.exit(0);
}
}
import com.sun.btrace.BTraceUtils.Aggregations;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.aggregation.Aggregation;
import com.sun.btrace.aggregation.AggregationFunction;
import com.sun.btrace.aggregation.AggregationKey;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Duration;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnEvent;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import static com.sun.btrace.BTraceUtils.*;
@BTrace
public class DaoMonitor {
private static Aggregation servletDuration = Aggregations.newAggregation(AggregationFunction.AVERAGE);
private static Aggregation servletCount = Aggregations.newAggregation(AggregationFunction.COUNT);
private static Aggregation daoDuration = Aggregations.newAggregation(AggregationFunction.AVERAGE);
private static Aggregation daoCount = Aggregations.newAggregation(AggregationFunction.COUNT);
@OnMethod(clazz = "org.springframework.web.servlet.FrameworkServlet",
method = "service",
location = @Location(Kind.RETURN))
public static void servlet(@Duration long duration) {
Aggregations.addToAggregation(servletDuration,
duration/1000);
Aggregations.addToAggregation(servletCount, 1);
}
@OnMethod(clazz = "/edu\\.tdd\\.crm\\..*Repository/",
method = "/.*/",
location = @Location(Kind.RETURN))
public static void dao(@Duration long duration, @ProbeClassName String className, @ProbeMethodName String probeMethod) {
String callName = concat(concat(className , "."), probeMethod);
AggregationKey aggregationKey = Aggregations.newAggregationKey(callName);
Aggregations.addToAggregation(daoDuration, aggregationKey, duration/1000);
Aggregations.addToAggregation(daoCount, aggregationKey, 1L);
}
@OnEvent
public static void summary() {
println("# org.springframework.web.servlet.FrameworkServlet.service");
Aggregations.printAggregation("- count:", servletCount);
Aggregations.printAggregation("- average duration(microseconds):", servletDuration);
println("# DAO");
Aggregations.printAggregation("- count", daoCount);
Aggregations.printAggregation("- average duration(microseconds)", daoDuration);
Sys.exit(0);
}
}
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
import java.util.Map;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnEvent;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
@BTrace
public class DbcpActiveConnectionMonitor {
private static Map<Object, Object> activeNumsByUrl = newHashMap();
private static final String DS_CLASS = "org.apache.commons.dbcp.BasicDataSource";
@OnMethod(clazz = DS_CLASS, method = "getConnection")
public static void onGetConnection(@Self Object basicDataSource) {
Class<?> dsClass = classOf(basicDataSource);
Object url = getUrl(dsClass, basicDataSource);
Object numberOfActive = getNumberOfActive(basicDataSource, dsClass);
put(activeNumsByUrl,url,numberOfActive);
}
@OnEvent
public static void print() {
println("-------------- active connection numbers by DB connection --------------------");
if (size(activeNumsByUrl) != 0) {
printMap(activeNumsByUrl);
} else {
println("no 'getConnection' calls");
}
Sys.exit(0);
}
private static Object getUrl(Class<?> dsClass, Object basicDataSource) {
Field urlField = field(dsClass, "url");
return get(urlField, basicDataSource);
}
private static Object getNumberOfActive(Object basicDataSource, Class<?> dsClass) {
Field poolField = field(dsClass, "connectionPool");
Object pool = get(poolField, basicDataSource);
Field numActiveField = field(classOf(pool), "_numActive");
Object numActive = get(numActiveField, pool);
return numActive;
}
}
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
@BTrace
public class DbcpMonitor {
private static final String DS_CLASS = "org.apache.commons.dbcp.BasicDataSource";
@OnMethod(clazz = DS_CLASS, method = "getConnection")
public static void onGetConnection(@Self Object basicDataSource) {
Class<?> dsClass = classOf(basicDataSource);
printTitle(dsClass, basicDataSource);
printFields(basicDataSource);
printNumberOfActive(basicDataSource, dsClass);
println("=======================================");
Sys.exit(0);
}
private static void printTitle(Class<?> dsClass, Object basicDataSource) {
Field urlField = field(dsClass, "url");
Object url = get(urlField, basicDataSource);
print("=====DBCP BasicDataSource info (");
print(url);
println(" ) ========");
}
private static void printNumberOfActive(Object basicDataSource, Class<?> dsClass) {
Field poolField = field(dsClass, "connectionPool");
Object numActive = getNumActive(basicDataSource, poolField);
println(concat("===== number of Active : ", str(numActive)));
}
private static Object getNumActive(Object basicDataSource, Field poolField) {
Object pool = get(poolField, basicDataSource);
Field numActiveField = field(classOf(pool), "_numActive");
Object numActive = get(numActiveField, pool);
return numActive;
}
}
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
import com.sun.btrace.BTraceUtils.Sys;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
@BTrace
public class DbcpMonitorSimple {
private static final String DS_CLASS = "org.apache.commons.dbcp.BasicDataSource";
@OnMethod(clazz = DS_CLASS, method = "getConnection")
public static void onGetConnection(@Self Object basicDataSource) {
Field urlField = field(DS_CLASS, "url");
Object url = get(urlField, basicDataSource);
print("=====DBCP BasicDataSource info (");
print(url);
println(" ) ==========");
printFields(basicDataSource);
Field poolField = field(DS_CLASS, "connectionPool");
Object pool = get(poolField, basicDataSource);
println("=====connectionPool (GenericObjectPool) info====");
printFields(pool);
println("==========");
Sys.exit(0);
}
}
<dependency>
<groupId>com.sun.tools.btrace</groupId>
<artifactId>btrace-client</artifactId>
<version>1.2.3</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment