Skip to content

jicahoo/linux

Repository files navigation

linux

Learning Linux and OS knowledge

方法论

  • 学习Linux的某个功能,可以从那些角度去学习呢?
  • 内核的相关运行机制,
  • 相关的系统调用,libc相关的函数
  • 编程使用相关功能,
  • 使用工具观察内核功能相关信息。/proc ....

Q&A

/proc

ptrace

strace

  • 对应Unix-like的truss.
  • 可以查看应用程序相关的系统调用。在研究Go语言的socket编程的时候,用到了,发现某个LWP会停在epoll_wait(4处。类似如下输出:
stack@ubuntu16-dev:~/go$ sudo strace -p 30412
strace: Process 30412 attached
epoll_wait(4,
strace -p 582 #582 is the java process id.
Process 582 attached
[ Process PID=582 runs in 32 bit mode. ]
futex(0xf770cba8, FUTEX_WAIT, 583, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGABRT {si_signo=SIGABRT, si_code=SI_USER, si_pid=7364, si_uid=0} ---
+++ killed by SIGABRT (core dumped) +++
Another more suitable method is to find the signal info from core dump directly.
(gdb) print $_siginfo
$1 = {si_signo = 6, si_errno = 0, si_code = 0, _sifields = {_pad = {7364, 0 <repeats 28 times>}, _kill = {si_pid = 7364, si_uid = 0}, _timer = {si_tid = 7364, si_overrun = 0, si_sigval = {
        sival_int = 0, sival_ptr = 0x0}}, _rt = {si_pid = 7364, si_uid = 0, si_sigval = {sival_int = 0, sival_ptr = 0x0}}, _sigchld = {si_pid = 7364, si_uid = 0, si_status = 0, si_utime = 0,
      si_stime = 0}, _sigfault = {si_addr = 0x1cc4}, _sigpoll = {si_band = 7364, si_fd = 0}}}

ps

  • 列出轻量级线程:ps -p 30408 -L

如何透彻理解epoll.

epoll在不同语言里面的体现

## Go netpoller

Python 3.5 asyncio

  • Code. see python-echo-server-asyncio.py

  • strace result. Saw epoll_wait, mremap.

sudo strace -p 8494  #8489 is the pid of python.
[sudo] password for jichao: 
strace: Process 8494 attached
epoll_wait(3, [{EPOLLIN, {u32=7, u64=3359015547638382599}}], 3, -1000) = 1
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f092accc000
recvfrom(7, "hello\r\n", 262144, 0, NULL, NULL) = 7
mremap(0x7f092accc000, 266240, 4096, MREMAP_MAYMOVE) = 0x7f092accc000
write(2, "EchoServer_127.0.0.1_46318: rece"..., 50) = 50
sendto(7, "hello\r\n", 7, 0, NULL, 0)   = 7
write(2, "EchoServer_127.0.0.1_46318: sent"..., 46) = 46
munmap(0x7f092accc000, 4096)            = 0
epoll_wait(3, [{EPOLLIN, {u32=7, u64=3359015547638382599}}], 3, -1000) = 1
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f092accc000
recvfrom(7, "Nihao\r\n", 262144, 0, NULL, NULL) = 7
mremap(0x7f092accc000, 266240, 4096, MREMAP_MAYMOVE) = 0x7f092accc000
write(2, "EchoServer_127.0.0.1_46318: rece"..., 50) = 50
sendto(7, "Nihao\r\n", 7, 0, NULL, 0)   = 7
write(2, "EchoServer_127.0.0.1_46318: sent"..., 46) = 46
munmap(0x7f092accc000, 4096)            =

CPU wa

Linux device file

About

Learning Linux and OS knowledge

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published