The previous articles introduce the fundamentals of Kilim from the code level, but for some of these details, such as how the task's execution state is managed, the problem is still not answered from the code, and this article is further down to the bytecode level. 1. What is the difference between the code before and after the Kilim byte code rewriting.
Here's a picture of Kilim Official document, which clearly shows the original code and the Kilim rewrite of the process code.
You can see the original code on the left, which is different from our common function, where the declaration throws a pausable exception. In fact, this exception is not thrown during the run, and its actual effect is similar to the annotation, which allows Kilim to identify which code requires the Weaver tool for bytecode rewriting.
And the left of the original code and the right rewrite code, the original method without parameters after the bytecode rewrite a parameter fiber, because the fiber is dependent on the task and exist, so fiber throughout the task chain of execution need to participate in the whole, In order to record and manage the execution status of a task.
So we'll just decompile the bytecode to see if this is the case, here's a simple task:
public class Ptlogintask extends Task {
mailbox<verifyrequest> Mailbox = new mailbox<verifyrequest> (); C1/>public Ptlogintask () {
super ();
This.start ();
}
@Override public
Void Execute () throws Pausable, Exception {while
(true) {
verifyrequest req = Mailbox.get () ;
Send (req);
}
public void Send (Verifyrequest req) throws Pausable, Exception {
req.mbox.put ("OK");
}
public static class Verifyrequest {
long uin;
Mailbox<string> mbox;
Public Verifyrequest (Long UIn, mailbox<string> mbox) {
This.uin = UIn;
This.mbox = mbox;
}
}
The task of the source code compiled after the generation of class files using the Kilim provided by the Weaver tool processing, and finally through the JAVAP command to decompile the bytecode file, we take the function Send as an example to see the generated byte code. We found that there are indeed two versions in the Send function bytecode:
One is the original version of the function
public void Send (Verifyrequest req) throws Pausable, Exception
The other is a version that adds the fiber parameter after overloading:
public void Send (Verifyrequest req, Fiber f) throws Pausable, Exception
At the same time, we found that the implementation of the original version has been rewritten by Kilim as a static method errnotwoven that directly calls the task, which prints the thread stack directly and exits directly, that is, the method is actually modified by Kilim to prohibit the invocation. Once called, a stack of exceptions similar to the following is thrown:
To sum up, a function throws a Pausable exception, which indicates that the function is paused, kilim the method weave, overloads a version containing the fiber parameter, and prevents direct calls to the original version of the function. 2. Kilim Rewrite byte code profiling
Before parsing the execution of bytecode, briefly describe the structure of the JVM run-time thread stack.
A method call chain in one thread can be very long, and many methods are in the execution state at the same time. Each of these methods will be a run-time stack frame structure, the execution engine running all bytecode instructions are only for the current stack frame operation, the concept structure of the stack frame as shown in the following figure:
A stack frame is a data structure used to support virtual machines for method invocation and method execution, which is the stack element of virtual Machine stack in virtual run-time data areas. The stack frame stores the method's local variable table, operand stack, method return address, and some additional information.
The stack of operands in the stack frame is also often referred to as the Operation Stack, which is a back-in-first, LIFO stack. As with local variable tables, the maximum depth of the operand stack is also written to the Max_stacks data item of the code attribute at compile time. Each element of the operand stack can be any Java data type, including long and double. The 32-bit data type occupies a stack capacity of 1, and the 64-bit data type occupies a stack capacity of 2. At any point in the method execution, the operand stack does not exceed the maximum value set in the Max_stacks data entry.
When a method has just begun to execute, the operation of this method stack is empty, in the implementation of the method, there will be a variety of bytecode instructions to the operand stack to write and extract content, that is, into the stack out of the stack operation. For example, the arithmetic operation is done through the operand stack, or when other methods are invoked by the operand stack for parameter passing. And each method from the start of the call to the completion of the process, corresponds to a stack frame in the virtual machine from the stack to the process of stack.
After introducing the basic structure of the above stack frame, the following is the decoding of the byte code of the JAVAP command. The Execute method and the Send method byte codes are as follows:
public void execute (kilim. Fiber) throws Kilim. Pausable, java.lang.Exception;
Code:
0:aload_1 // loading variable fiber with index 1 in local variable table, incorporating operand stack top
1:dup /// copy stack top fiber object reference, merged stack, at this time there are two copies on the stack fiber
2:astore_2 // references the replicated fiber object to the stack and stores the local variable table index to 2
3:getfield #44; Field kilim/fiber.pc:i
6:tableswitch{//0 to 2
0:52; //Pc=0 , indicating that the first entry into this function is performed
1:35; //Pc=1 , which indicates that the last execution to Mailbox.get paused, jump directly to the function
2:45; //pc=2 , which indicates that the last execution to send paused, jump directly to the function to perform
DEFAULT:32}
32:invokestatic #47; Method KILIM/FIBER.WRONGPC: () V
35:aload_2 //mailbox.get resume execution, where the recovery context begins, before this step is loaded
A Fiber object reference stored to a local variable table index of 2
36:invokevirtual #51; Method Kilim/fiber.getcallee: () Ljava/lang/object;
39:checkcast #23; Class Kilim/mailbox// Here Make sure the pause is Mailbox
42:goto 56
45:aconst_null //Send resumes execution, here begins the recovery context
46:astore_1 // The null object is referenced out of the stack and stored in the local variable table index of 1, where the incoming verifyrequest variable has been saved to the corresponding state within the Send function because the Send function is paused
47:aload_0 // Load This reference to the top of the operand stack, the local variable table index is 0 always this
48:aconst_null // reference null object to operand stack top
49:goto 129 // jump directly to the Send function to perform
52:aload_0
53:getfield #26; Field Mailbox:lkilim/mailbox;
56:aload_2
57:invokevirtual #55; Method Kilim/fiber.down: ()lkilim/fiber;
60:invokevirtual #59; Method Kilim/mailbox.get: (lkilim/fiber;)Ljava/lang/object;
63:aload_2
64:invokevirtual #63; Method Kilim/fiber.up: ()I
67:tableswitch{//0 to 3
0:123; //not_pausing__no_state = 0
1:123; //not_pausing__has_state = 1
2:96; //pausing__no_state = 2
3:121; //pausing__has_state = 3
DEFAULT:123}
96:pop //mailbox.get function paused, start saving stack frame to state
97:new #65; Class Kilim/state
100:dup
101:invokespecial #66; Method Kilim/state. " <init> ":() V
104:astore_3 // instantiate a state and store it in the 3rd position of the local variable table
105:aload_3 // load just instantiated state reference into operand stack
106:aload_0
107:putfield #70; Field Kilim/state.self:ljava/lang/object;
// These two steps set the Self object in state, that is, the class instance to which the Get function belongs
110:aload_3
111:iconst_1 // load constant 1 into operand stack
112:putfield #71; Field kilim/state.pc:i // set the pc=1 in state
115:aload_2
116:aload_3
117:invokevirtual #75; Method Kilim/fiber.setstate: (lkilim/state;) V
//This step will instantiate the state, and the object is stored in the fiber Statestack
120:return // Direct return, identifying this function execution to Mailbox.get paused
121:pop
122:return
123:checkcast #7; Class Kilim/ptlogintask$verifyrequest
//This step from mailbox.get pause after resuming execution completes jump here and make sure to return verifyrequest
126:astore_1 // verifyrequest variable out of stack and stored in local variable table index to 1
127:aload_0 // point to itself this variable into the stack
128:aload_1 //verifyrequest variable into stack
129:aload_2; //fiber object reference into Stack
130:invokevirtual #55; Method Kilim/fiber.down: ()lkilim/fiber;
133:invokevirtual #79; Method Send: (lkilim/ptlogintask$verifyrequest; Lkilim/fiber;)V
136:aload_2
137:invokevirtual #63; Method Kilim/fiber.up: ()I
140:tableswitch{//0 to 3
0:197; //not_pausing__no_state = 0
1:197; //not_pausing__has_state = 1
2:172; //pausing__no_state = 2
3:196; //pausing__has_state = 3
default:197}
172:new #65; Class Kilim/state//Send function paused, start saving stack frame to state
175:dup
176:invokespecial #66; Method Kilim/state. " <init> ":() V
179:astore_3 // The state out stack that is instantiated above is stored in the 3rd position of the local variable table
180:aload_3
181:aload_0 // set the Self object in state, that is, the class instance to which the Send function belongs
182:putfield #70; Field Kilim/state.self:ljava/lang/object;
185:aload_3