[20190401]跟踪dbms_lock.sleep调用.txt

时间:2022-08-12 10:49:41

[20190401]跟踪dbms_lock.sleep调用.txt

--//自己在semtimedop函数调用理解错误,加深理解,跟踪dbms_lock.sleep调用的情况.

1.环境:
SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

grant EXECUTE ON  dbms_lock to scott;

SCOTT@book> @ spid
       SID    SERIAL# PROCESS                  SERVER    SPID       PID  P_SERIAL# C50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
        51          7 60257                    DEDICATED 60258       27        123 alter system kill session '51,7' immediate;

$ strace -fTr -e semtimedop -p 60258
Process 60258 attached - interrupt to quit

--//仅仅跟踪semtimedop.

2.测试:
SCOTT@book> exec dbms_lock.sleep(5.3)
PL/SQL procedure successfully completed.

$ strace -fTr -e semtimedop -p 60258
Process 60258 attached - interrupt to quit
     0.000000 semtimedop(309821440, 0x7fffe52f9ac0, 1, {5, 300000000}) = -1 EAGAIN (Resource temporarily unavailable) <5.300886>
--//可以发现实际上执行dbms_lock.sleep(5.3),调用函数是semtimedop,睡眠5.3秒.

SCOTT@book> exec dbms_lock.sleep(10.3)
PL/SQL procedure successfully completed.

$ strace -fTr -e semtimedop -p 60258
..
    65.217301 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EAGAIN (Resource temporarily unavailable) <3.000460>
     3.000610 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EAGAIN (Resource temporarily unavailable) <3.000960>
     3.001073 semtimedop(309821440, 0x7fffe52f9ac0, 1, {4, 300000000}) = -1 EAGAIN (Resource temporarily unavailable) <4.300934>

--//你可以基本看出执行dbms_lock.sleep大致算法,oracle许多东西都与3秒有关.
--//分成多个3秒,开始调用semtimedop 3秒,剩下部分小于2*3秒时,调用剩余的部分.

--//再次测试,在执行的过程中打开新终端窗口,执行kill -1 60258.
SCOTT@book> exec dbms_lock.sleep(10.3)
--//中间执行:
$ kill -1 60258

PL/SQL procedure successfully completed.

--//注:kill -1 并不会杀死进程,仅仅挂起.可以通过kill -l了解一些细节.
 $ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

$ strace -fTr -e semtimedop -p 60258
..
   154.662451 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EAGAIN (Resource temporarily unavailable) <3.000463>
     3.000651 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EINTR (Interrupted system call) <1.573761>
     1.573876 --- SIGHUP (Hangup) @ 0 (0) ---
     0.000203 semtimedop(309821440, 0x7fffe52f9ac0, 1, {5, 730000000}) = -1 EAGAIN (Resource temporarily unavailable) <5.730875>
--//在第2个3秒中发出了中断,仅仅执行1.57秒.最后1次调用执行 4.3+1.43 = 5.73秒.

--//再次测试,中间按ctrl+c看看:
SCOTT@book> exec dbms_lock.sleep(10.3)
BEGIN dbms_lock.sleep(10.3); END;

*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
ORA-06512: at "SYS.DBMS_LOCK", line 205
ORA-06512: at line 1

$ strace -fTr -e semtimedop -p 60258
..
    14.543729 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EINTR (Interrupted system call) <2.880053>
     2.880177 --- SIGURG (Urgent I/O condition) @ 0 (0) ---
--//中断不再继续执行.

--//再次测试,中间执行kill -19 60258, 19表示SIGSTOP,暂停执行.
$ strace -fTr -e semtimedop -p 60258
..

172.564623 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EINTR (Interrupted system call) <2.343982>
     2.344113 --- SIGSTOP (Stopped (signal)) @ 0 (0) ---
     0.000071 --- SIGSTOP (Stopped (signal)) @ 0 (0) ---
     0.000326 semtimedop(309821440, 0x7fffe52f9ac0, 1, {3, 0}) = -1 EAGAIN (Resource temporarily unavailable) <3.000768>
     3.000904 semtimedop(309821440, 0x7fffe52f9ac0, 1, {4, 960000000}) = -1 EAGAIN (Resource temporarily unavailable) <4.960862>

--//可以发现仅仅暂停第1个3秒的执行.总的sleep时间是不变的.
--//2.343982+3.000768+4.960862 = 10.305612
--//后续的sql语句并不影响执行.

--//做这些测试仅仅是加强理解..^_^.

--//顺便说一下linux sleep调用的是nanosleep.
$ strace sleep 10.3
...
brk(0x1d63000)                          = 0x1d63000
nanosleep({10, 300000001}, NULL)        = 0
close(1)                                = 0
exit_group(0)                           = ?