[20190314]使用strace注意的問題.txt

lfree發表於2019-03-14

[20190314]使用strace注意的問題.txt


--//使用strace跟蹤程式,我發現我自己忽略一個問題.一般看函式呼叫花費時間一般使用如下引數:


-r    Print a relative timestamp upon entry to each system call.  This records the time difference between the beginning

      of successive system calls.

-t    Prefix each line of the trace with the time of day.

-tt   If given twice, the time printed will include the microseconds.

-ttt  If given thrice, the time printed will include the microseconds and the leading portion will be printed as the

      number of seconds since the epoch.

-T    Show the time spent in system calls. This records the time difference between the beginning and the end of each

      system call.


--//注意-T,-r引數的區別.-T顯示的system calls的時間,顯示在最後.

--//-r 顯示相對時間,顯示在前面.

--//透過例子說明:


$ export TNS_ADMIN=/tmp

--//在tmp目錄下建立如下檔案:

$ cat tnsnames.ora

CLIENT_CON =

 (DESCRIPTION =

  (LOAD_BALANCE=OFF)

   (TRANSPORT_CONNECT_TIMEOUT=1)

   (ADDRESS = (PROTOCOL = TCP)(HOST =10.10.10.10)(PORT = 1522))

   (ADDRESS = (PROTOCOL = TCP)(HOST =11.11.11.11)(PORT = 1522))

   (ADDRESS = (PROTOCOL = TCP)(HOST =12.12.12.12)(PORT = 1522))

   (ADDRESS = (PROTOCOL = TCP)(HOST =13.13.13.13)(PORT = 1522))

   (ADDRESS = (PROTOCOL = TCP)(HOST =14.14.14.14)(PORT = 1522))

   (ADDRESS = (PROTOCOL = TCP)(HOST =15.15.15.15)(PORT = 1522))

  (CONNECT_DATA =

     (SERVER = DEDICATED)

     (SERVICE_NAME = orclgg)

   ))


$ strace -r -tT -f tnsping client_con 2>&1 | grep -C1  -E 'poll'

     0.000084 mmap(NULL, 528384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f0c2342a000 <0.000023>

     0.000078 poll([{fd=4, events=POLLOUT}], 1, 1000) = 0 (Timeout) <1.001095>

     1.001171 close(4)                  = 0 <0.000076>

--

     0.000122 times({tms_utime=0, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 10709770534 <0.000027>

     0.000085 poll([{fd=4, events=POLLOUT}], 1, 1000) = 0 (Timeout) <1.001072>

     1.001181 close(4)                  = 0 <0.000111>

--

     0.000128 times({tms_utime=0, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 10709770634 <0.000026>

     0.000084 poll([{fd=4, events=POLLOUT}], 1, 1000) = 0 (Timeout) <1.001086>

     1.001340 close(4)                  = 0 <0.000055>

--

     0.000126 times({tms_utime=0, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 10709770735 <0.000056>

     0.000116 poll([{fd=4, events=POLLOUT}], 1, 1000) = 0 (Timeout) <1.001093>

     1.001261 close(4)                  = 0 <0.000080>

--

     0.000125 times({tms_utime=1, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 10709770835 <0.000027>

     0.000084 poll([{fd=4, events=POLLOUT}], 1, 1000) = 0 (Timeout) <1.001079>

     1.001265 close(4)                  = 0 <0.000082>

--

     0.000120 times({tms_utime=1, tms_stime=0, tms_cutime=0, tms_cstime=0}) = 10709770935 <0.000027>

     0.000085 poll([{fd=4, events=POLLOUT}], 1, 1000) = 0 (Timeout) <1.001085>

     1.001255 close(4)                  = 0 <0.000055>


--//說明-C1,顯示匹配poll前後1行.

--//另外可以發現-t顯示時間的引數如下,在使用-r引數時.

--//-r顯示的時間在前面,不可能close花費1秒,實際上poll呼叫後,呼叫close前的時間.

--//-T顯示的時間在後面,這個是system call花費的時間.

--//注意這些區別對於診斷問題幫助很大.遇到不熟悉的函式可以使用man檢視,比如:


$ man poll


POLL(2)                    Linux Programmer's Manual                   POLL(2)


NAME

       poll, ppoll - wait for some event on a file descriptor


SYNOPSIS

       #include <poll.h>


       int poll(struct pollfd *fds, nfds_t nfds, int timeout);


       #define _GNU_SOURCE

       #include <poll.h>


       int ppoll(struct pollfd *fds, nfds_t nfds,

               const struct timespec *timeout, const sigset_t *sigmask);


DESCRIPTION

       poll() performs a similar task to select(2): it waits for one of a set of file descriptors to become ready to perform I/O.


  The set of file descriptors to be monitored is specified in the fds argument, which is an array of nfds

  structures of the following form:


           struct pollfd {

               int   fd;         /* file descriptor */

               short events;     /* requested events */

               short revents;    /* returned events */

           };


       The field fd contains a file descriptor for an open file.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2638331/,如需轉載,請註明出處,否則將追究法律責任。

相關文章