Skip to content

Commit

Permalink
add the update count by DML to the profile information.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjokim committed Dec 31, 2015
1 parent 556104d commit 041e865
Show file tree
Hide file tree
Showing 6 changed files with 689 additions and 616 deletions.
89 changes: 59 additions & 30 deletions scouter.agent.java/src/scouter/agent/asm/jdbc/PsExecuteMV.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
/*
* Copyright 2015 Scouter Project.
*
* 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.
*/

/*
* Copyright 2015 Scouter Project.
*
* 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 scouter.agent.asm.jdbc;

import java.util.HashSet;
import java.util.Set;

import scouter.agent.trace.TraceSQL;
import scouter.org.objectweb.asm.Label;
import scouter.org.objectweb.asm.MethodVisitor;
import scouter.org.objectweb.asm.Opcodes;
import scouter.org.objectweb.asm.Type;
import scouter.org.objectweb.asm.commons.LocalVariablesSorter;
import java.util.HashSet;
import java.util.Set;

import scouter.agent.asm.util.AsmUtil;
import scouter.agent.trace.TraceSQL;
import scouter.org.objectweb.asm.Label;
import scouter.org.objectweb.asm.MethodVisitor;
import scouter.org.objectweb.asm.Opcodes;
import scouter.org.objectweb.asm.Type;
import scouter.org.objectweb.asm.commons.LocalVariablesSorter;


public class PsExecuteMV extends LocalVariablesSorter implements Opcodes {
Expand All @@ -42,24 +43,26 @@ public static boolean isTarget(String name) {

private final static String TRACESQL = TraceSQL.class.getName().replace('.', '/');
private final static String START_METHOD = "start";
private final static String END_METHOD = "end";
private static final String END_SIGNATURE = "(Ljava/lang/Object;Ljava/lang/Throwable;)V";
private static final String START_SIGNATURE = "(Ljava/lang/Object;Lscouter/agent/trace/SqlParameter;)Ljava/lang/Object;";
private final static String END_METHOD = "end";
private static final String END_SIGNATURE = "(Ljava/lang/Object;Ljava/lang/Throwable;I)V";

public PsExecuteMV(int access, String desc, MethodVisitor mv, String owner) {
super(ASM4,access, desc, mv);
this.owner = owner;
this.returnType = Type.getReturnType(desc);
}
private Label startFinally = new Label();

private String owner;
private int statIdx;
private final Type returnType;

@Override
public void visitCode() {
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, owner, TraceSQL.PSTMT_PARAM_FIELD, "Lscouter/agent/trace/SqlParameter;");
mv.visitFieldInsn(GETFIELD, owner, TraceSQL.PSTMT_PARAM_FIELD, "Lscouter/agent/trace/SqlParameter;");

mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, START_METHOD, START_SIGNATURE,false);

Expand All @@ -72,8 +75,30 @@ public void visitCode() {
@Override
public void visitInsn(int opcode) {
if ((opcode >= IRETURN && opcode <= RETURN)) {
mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);

switch (returnType.getSort()) {
case Type.BOOLEAN:
case Type.INT:
int i = newLocal(returnType);
mv.visitVarInsn(Opcodes.ISTORE, i);
mv.visitVarInsn(Opcodes.ILOAD, i);

mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);

mv.visitVarInsn(Opcodes.ILOAD, i);

if(returnType.getSort()== Type.BOOLEAN){
mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, "toInt", "(Z)I",false);
}
break;
default:
mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);
AsmUtil.PUSH(mv,0);
}


mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, END_METHOD, END_SIGNATURE,false);
}
mv.visitInsn(opcode);
Expand All @@ -90,8 +115,12 @@ public void visitMaxs(int maxStack, int maxLocals) {

mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitVarInsn(Opcodes.ALOAD, errIdx);
AsmUtil.PUSH(mv,0);

mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, END_METHOD, END_SIGNATURE,false);
mv.visitInsn(ATHROW);
mv.visitMaxs(maxStack + 8, maxLocals + 2);
}


}
37 changes: 34 additions & 3 deletions scouter.agent.java/src/scouter/agent/asm/jdbc/StExecuteMV.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashSet;
import java.util.Set;

import scouter.agent.asm.util.AsmUtil;
import scouter.agent.trace.TraceSQL;
import scouter.org.objectweb.asm.Label;
import scouter.org.objectweb.asm.MethodVisitor;
Expand All @@ -35,6 +36,8 @@ public class StExecuteMV extends LocalVariablesSorter implements Opcodes {
target.add("executeBatch");
}

private final Type returnType;

public static boolean isTarget(String name) {
return target.contains(name);
}
Expand All @@ -43,10 +46,11 @@ public static boolean isTarget(String name) {
private final static String START_METHOD = "start";
private static final String START_SIGNATURE = "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;";
private final static String END_METHOD = "end";
private static final String END_SIGNATURE = "(Ljava/lang/Object;Ljava/lang/Throwable;)V";
private static final String END_SIGNATURE = "(Ljava/lang/Object;Ljava/lang/Throwable;I)V";

public StExecuteMV(int access, String desc, MethodVisitor mv, String owner) {
super(ASM4, access, desc, mv);
this.returnType = Type.getReturnType(desc);
}

private Label startFinally = new Label();
Expand All @@ -68,8 +72,30 @@ public void visitCode() {
@Override
public void visitInsn(int opcode) {
if ((opcode >= IRETURN && opcode <= RETURN)) {
mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);

switch (returnType.getSort()) {
case Type.BOOLEAN:
case Type.INT:
int i = newLocal(returnType);
mv.visitVarInsn(Opcodes.ISTORE, i);
mv.visitVarInsn(Opcodes.ILOAD, i);

mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);

mv.visitVarInsn(Opcodes.ILOAD, i);

if(returnType.getSort()== Type.BOOLEAN){
mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, "toInt", "(Z)I",false);
}
break;
default:
mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitInsn(Opcodes.ACONST_NULL);
AsmUtil.PUSH(mv,0);
}


mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, END_METHOD, END_SIGNATURE, false);
}
mv.visitInsn(opcode);
Expand All @@ -86,8 +112,13 @@ public void visitMaxs(int maxStack, int maxLocals) {

mv.visitVarInsn(Opcodes.ALOAD, statIdx);
mv.visitVarInsn(Opcodes.ALOAD, errIdx);
AsmUtil.PUSH(mv,0);

mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACESQL, END_METHOD, END_SIGNATURE, false);
mv.visitInsn(ATHROW);
mv.visitMaxs(maxStack + 8, maxLocals + 2);
}



}
12 changes: 11 additions & 1 deletion scouter.agent.java/src/scouter/agent/trace/TraceSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private static String escapeLiteral(String sql, SqlStep2 step) {
private static SQLException tooManyFetch = new TOO_MANY_RECORDS("TOO_MANY_RECORDS", "TOO_MANY_RECORDS");
private static SQLException connectionOpenFail = new CONNECTION_OPEN_FAIL("CONNECTION_OPEN_FAIL",
"CONNECTION_OPEN_FAIL");
public static void end(Object stat, Throwable thr) {
public static void end(Object stat, Throwable thr, int updated) {
if (stat == null) {
if (conf.log_background_sql && thr != null) {
Logger.println("BG-SQL:" + thr);
Expand All @@ -223,6 +223,7 @@ public static void end(Object stat, Throwable thr) {
TraceContext tctx = lctx.context;
SqlStep2 ps = (SqlStep2) lctx.stepSingle;
ps.elapsed = (int) (System.currentTimeMillis() - tctx.startTime) - ps.start_time;
ps.updated = updated;
if (tctx.profile_thread_cputime) {
ps.cputime = (int) (SysJMX.getCurrentThreadCPU() - tctx.startCpu) - ps.start_cpu;
}
Expand Down Expand Up @@ -618,4 +619,13 @@ public static void sqlMap(String methodName, String sqlname) {
.append(sqlname).append(" }").toString());
ctx.profile.add(p);
}

/**
* used for tracing the return of xxx.execute()
* @param b
* @return
*/
public static int toInt(boolean b){
return b?1:0;
}
}
Loading

0 comments on commit 041e865

Please sign in to comment.