SlideShare a Scribd company logo
strace for Perl 
Mongers 
zentooo @ DeNA
ß…Ãu
@zentoo or @zentooo
°ntDtÒ–ê±
áϱ–©–ÖòÖèÂÇÔ 
¨–ß
p€g§a†ó
àÇ–®w©À{N–ŠgŸ
stracet{ 
Ö캸ÆsPía˜rP—¶¸Åß²–êžtracee—k 
zÖê
iz¼w|¶¸Åß²–êo 
rD”ƒ
¶¸Åß²–ê{|¡Öé°–¶æòX 
j[OSz API 
File I/O (open, lseek, write, read, close) 
Network (socket, bind, listen, accept, connect) 
Memory (brk, sbrk, mmap) 
Process (fork, wait, kill) 
Thread (pthread_create)
¶¸Åß²–êž-,gfPerlsÔ £ê 
I/OsY—Wƒ
¶¸Åß²–êUYsPerlscPÖìº 
¸žf˜—Wƒ
¶¸Åß²–êUYsPerlsÌÂÇî–® 
Öì¯èÞò¯Xca—Wƒ
¡Öé°–¶æò{OSzzzÖ
±¿seX
Q. Web¡Öé°–¶æòz› 
ÌorD”ƒ
A. HTTPé®§¸Çž*]r 
HTTPë¸Üò¸ž4e_tl 
”„
Q. HTTPé®§¸Çž*]r 
HTTPë¸Üò¸ž4eoru 
RPR_tl”„
A. acceptcrreadcrwritecrclosel 
”Ϫãì–„
ŠNgm®]
Web¡ÖézV›Ì 
1. HTTPé®§¸Çž*]— 
2. _zw‰•zÈèÝXN— 
MySQL„z¡®º¸ 
memcached„z¡®º¸ 
(Æ–¾z^|logz„Y‹etc) 
3. /_zw‰•zÈèÝXN— 
4. HTTPë¸Üò¸ž4e
¶¸Åß²–ê§w™e—t... 
1. int fd = accept(...); // é®§¸Ç*]r 
2. read(fd, ...);{{{// é®§¸ÇzÆ?­Ÿs 
3. _zw‰•zÈèÝXN— 
socket, connect, write, read, close 
open, write, read, close 
4. /_zw‰•zÈèÝXN— 
5. write(fd, ...);{{{// ë¸Üò¸4e
a•w»ƒe—t 
1. int fd = accept(...); // é®§¸Ç*]r 
2. read(fd, ...);{{{// é®§¸ÇzÆ?­Ÿs 
3. ‰•zÈèÝ / 
4. write(fd, ...);{{{// ë¸Üò¸4e
DEMO #1 - just to strace
stracež­ŒÔz¬Úò (1) 
↓システムコール名 返り値↓ 
read(5, GET / HTTP/1.0rnHost: xxx-xxxxxx..., 131072) = 308 
↑引数(バッファなどの場合はよしなに展開される)
stracež­ŒÔz¬Úò (2) 
fd (Ô £êÆ¢¸®éÖ¾) w¶öe— 
↓コレ 
read(5, GET / HTTP/1.0rnHost: xxx-xxxxxx..., 131072) = 308
fd (Ô £êÆ¢¸®éÖ¾) 
Ô £êÌÂÇî–®¼°ÂÇvuž³ÎBckÖ9 
„zÜ£ò¾ 
(0, 1, 2) = (stdin, stdout, stderr) 
i˜’ø{«zw•˜—
fd with file 
int fd = open(/tmp/yapc2014, ...); 
read(fd, buffer); 
write(fd, buffer); 
close(fd);
fd with client socket 
int fd = socket(); 
connect(fd, addr, ...); 
read(fd, buffer); 
write(fd, buffer); 
close(fd);
fdžðRzXè2z2˜ 
Ô £êzf# 
opens4orYkfdXclosewUa˜—Šs 
®è£¡òÇ´£Èsz¼°ÂÇzf# 
sockets4orYkfdXclosewUa˜—Šs 
´–Ï´£ÈwV]—®è£¡òǼ°ÂÇzf# 
accepts4orYkfdXclosewUa˜—Šs
Web¡Öézstracež­ŒÔz¬Úò 
// こっから 
accept(4, {sa_family=AF_INET, ...)}, [16]) = 5 
(中略) 
read(5, GET / HTTP/1.0rnHost: xxx-xxxxxx..., 131072) = 308 
(中略) 
write(5, HTTP/1.1 200 OKrnDate: Sun, 10 A..., 2218) = 2218 
// ここまでが一つのリクエスト/レスポンス 
// keepaliveしてないならここで close
DEMO #2 - strace web app
N[Šs2˜vzsº“Ž 
Twiggy, Twiggy::Preforkvuevent-drivenv´–Ï 
(epoll_wait) 
multi socket listencr—ÔzStarlet (select)
Web¡ÖézV›Ì($) 
lPkPzÝ „Ðzó
 sþUwv— 
HTTPé®§¸Çž*]— 
_zw‰•zÈèÝXN— 
MySQL„z¡®º¸ 
memcached„z¡®º¸ 
(Æ–¾z^|logz„Y‹etc) 
/_zw‰•zÈèÝXN— 
HTTPë¸Üò¸ž4e
¶öÜ£òÇ - MySQL 
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 10 
fcntl(10, F_SETFL, O_RDONLY) = 0 
fcntl(10, F_GETFL) = 0x2 (flags O_RDWR) 
fcntl(10, F_GETFL) = 0x2 (flags O_RDWR) 
fcntl(10, F_SETFL, O_RDWR|O_NONBLOCK) = 0 
connect(10, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr(10.0.0.1 
fcntl(10, F_SETFL, O_RDWR) = 0 
poll([{fd=10, events=POLLIN|POLLPRI}], 1, 4000) = 1 ([{fd=10, revents=POLLIN} 
setsockopt(10, SOL_SOCKET, SO_RCVTIMEO, 200334110000000setsockopt(10, SOL_SOCKET, SO_SNDTIMEO, 200334110000000setsockopt(10, SOL_IP, IP_TOS, [8], 4) = 0 
setsockopt(10, SOL_TCP, TCP_NODELAY, [1], 4) = 0 
setsockopt(10, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0
¶öÜ£òÇ - memcached 
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 8 
fcntl(8, F_GETFL) = 0x2 (flags O_RDWR) 
fcntl(8, F_SETFL, O_RDWR|O_NONBLOCK) = 0 
connect(8, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr(10.0.0.1 
poll([{fd=8, events=POLLOUT}], 1, 250) = 1 ([{fd=8, revents=POLLOUT}]) 
getsockopt(8, SOL_SOCKET, SO_ERROR, [-511598409602301952], [4]) = 0 
setsockopt(8, SOL_TCP, TCP_NODELAY, [1], 4) = 0 
sendmsg(8, {msg_name(0)=NULL, msg_iov(6)=[{set, 3}, { csrf_token:, 20}, 
read(8, 0x59f9f50, 1536) = -1 EAGAIN (Resource temporarily unavailable) 
poll([{fd=8, events=POLLIN}], 1, 300) = 1 ([{fd=8, revents=POLLIN}]) 
read(8, STOREDrn, 1536)
DEMO #3 - strace web app 
with DB and memcached
Öì¯èß{­oktV–w 
”WvP 
•d®§éžDxPcr— 
­ork”–cache misscr— 
Ï7wretrycr— 
TCP ConnectionžPŠ›grvP 
TCP ConnectionmŸtˆ˜rvP
öº #1 - memcached retry 
problem
+180ms 
tN—APIzë¸Üò¸X|éé–¸ w180msËB 
50ms or diev•4x[•P¶vvPtP]vPëØê
?_	 
Cache::Memcached::FastzèÂЖá·ä–ê 
(Ò1
XNokzs)
M[èo 
strace -p $pid -e 'trace=sendmsg' 
(Cache::Memcached::FastXsendmsgžžRz{—orPk)
Ë 
sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_local:, 13} 
sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_local:, 13} 
sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_local:, 13} 
sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_local:, 13} 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote1:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote1:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote1:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote1:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote2:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote2:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote2:, 
sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { cache_remote2:,
̾|•c 
èÂЖá·ä–êwretryAÿ (maxs3x) XâorPk 
Cache::Memcached::Fastzget{cache misstiz)z§ 
è–žæ*sYvP 
cache missz_w3xretrycrk 
retry¹{20msec 
20 msec × 3 (retry) × 3 = 180 msec 
izhit rate{uRvŸƒtPRø{N–ppŽget,{ 
retrycvP7cžâ˜rwE
Ë - with timestamp 
22:45:34.517703 sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { 22:45:34.537967 sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { 22:45:34.558936 sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { 22:45:34.579275 sendmsg(6, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, { 22:45:34.587428 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:34.608296 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:34.628673 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:34.649146 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:35.135129 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:35.155516 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:35.176027 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3}, 
22:45:35.196453 sendmsg(11, {msg_name(0)=NULL, msg_iov(4)=[{get, 3},
stracecr‹—t´ö¿ 
ÙžpWvPÖêwc—
ÙžpWvPÖê 
strace 
tcpdump 
gdb
Ùžp[Žz 
zM6 
¡Öézì¯ 
Ôw{¡Öéz²–È
öº #2 - accept(2) 
thundering herd
What is thundering herd 
The thundering herd problem occurs when a large 
number of processes waiting for an event are awoken 
when that event occurs, but only one process is able to 
proceed at a time. - Wikipedia 
*[zÖ캸X§zÇé«–s•Ôw”YceX|ö 
Uz{v{´zÖ캸scW훘vP (Šk{í› 
˜—…Y) Ôwé¼–¸XÏ7wv—þU 
”[N—z{Æ–¾zremote cacheXexpireckÔw´D 
wDB„z®§éX—|tW
accept(2) and prefork server 
DÖ캸X•Ôw´pzsocket fdžacceptc|i˜j 
˜Õì® 
Linux kernelX”cvwqueueingcr[˜—zs|é®§ 
¸ÇXakÔwOYÖX—Ö캸{´pl] 
(ˆæ) 
http://d.hatena.ne.jp/naoya/20070311/1173629378
accept(2) and event-driven/ 
multi-process server 
DÖ캸X•Ôw´pzsocket fdžepoll_waitsÁ 
listen socketXreadIÿ = acceptXIÿwvokÔFs 
epoll_waitcr—ÀrzÖ캸XOYÖX— 
´pzÖ캸Xacceptw´ 
)zÖ캸{óorî (EAGAIN)
accept(2) thundering herd 
accept(2)zó–XÖ캸Dté®§¸ÇDzÀ]‘s 
Pf 
loadXÖX— 
vmstatsf—t... 
ustsytcsz–Xg–ÖX— (50, 50, 100000tW) 
Ö캸zcontext switch
TeZXOÀWt­›˜—
DEMO #3 - EAGAIN and again 
and again
wEn 
iŽiŽXevent-drivenvŸlciŸvwR£wÖ캸 
N^v[rPP”y
vv,w”–100Ö캸[•Pw (¼{300[•PN 
ok)
7• 
xæz¡–¬Å®ÀâltÇtuRc”RŽvPDXcr 
P— 
Monoceros‹kPwaccepte—Ö캸té®§¸Çž 
{ve—Ö캸žñÀw
]—tWƒ 
event-driventÝêÀÖ캸{øPgXuP with Perl 
IO::AIO (AnyEvent::IOzbackend) tWŽÇt¶ò¯êÖ 
캸¼völok–...
Štwâ–Še
stracez›—Pt_™ 
ß¿váQXVVP (ø˜˜|uRwsŽv—X) 
kŠwO_—,zÌÎw{¹PrvP (Devel::KYTProftW 
sì¯cck[X”Pƒ¥
stracez”Pt_™ 
̼XÓPs|Ö캸wattache—l]sef˜ 
— 
Web¡Öé°–¶æòzDÓmX
W—
stracelt
W•vP_t 
¶¸Åß²–êž]›vPvCPUÏ¥òÈvÏ7v{v 
¡Öé°–¶æòszÏ7loop, deep recursion 
µzuP¡ê³é¹ßtW 
-d NYTProf 
Çèò¸Ü–Çä’AszþU 
tcpdumptWtsharktWngreptWžPŠc“R
straces
W—_t 
é®§¸Ç*]r|ë¸Üò¸4eŠszÔì– 
Æ–¾¡®º¸zöA 
öUwDxmemcachedW•getcr—W|DxDB‹w 
Por—W 
öUwcachea˜rP—data 
öUwPícrP—SELECT 
iz)|ŽP	z6crPvP“”
H 
|k–¼l]ustrace{¸ÿzÖêdvP 
žT|ÚàW•ä—tWiŸv_tŽvP 
sŽ|žP[X
WorP˜|V7€­¦ 
Web¡ÖézDÓmX
W—
ˆæ` 
The Linux Programing Interface 
ŠPc|K¨„€h(÷§cWvP]u...)Vee 
à˜§hŽN—]u1000Ù–·Ý|pŠ–„€s{v[ 
ï1 
À󭌫Pvc|éÔ ëò¸tcrŽ6q 
äz˜{Veel]u|±¿­Œz{ޏXûPzs 
pRzLinuxÖì¯èÞò¯ 
tWz[X?|{PPWŽc 
˜vP
™G (?) 
Web+DBz2014ñ10KÿwMÌ(Perl Hackers Hub)ž„Wg 
rá[_twv–Šck

More Related Content

PDF
wordpress with nginx on virtualization, jail
PDF
gemdiff
PDF
Varnish Cache and Django (Falcon, Flask etc)
PDF
Год в Github bugbounty, опыт участия
PDF
5 issues
PPTX
Buffer overflow for Beginners
PDF
PFIセミナー資料 H27.10.22
wordpress with nginx on virtualization, jail
gemdiff
Varnish Cache and Django (Falcon, Flask etc)
Год в Github bugbounty, опыт участия
5 issues
Buffer overflow for Beginners
PFIセミナー資料 H27.10.22

What's hot (20)

PDF
Pf: the OpenBSD packet filter
PDF
Containers for sysadmins
PPS
36 gotas de-sabiduria
DOC
X64服务器 lnmp服务器部署标准 new
PDF
Abusing text/template for data transformation
PPT
download presentation
PPT
GeeCON 2013 - EJB application guided by tests
PDF
Profiling and optimizing go programs
PDF
SSH I/O Streaming via Redis-based Persistent Message Queue -Mani Tadayon
PDF
nouka inventry manager
PDF
Python and rust 2018 pythonkorea jihun
KEY
About memcached
PDF
Automated tests - facts and myths
PDF
BlockChain implementation by python
PDF
Ruby Postgres
PDF
Memory Management in WordPress
PDF
root-ksk-2010
PDF
Go for the paranoid network programmer
Pf: the OpenBSD packet filter
Containers for sysadmins
36 gotas de-sabiduria
X64服务器 lnmp服务器部署标准 new
Abusing text/template for data transformation
download presentation
GeeCON 2013 - EJB application guided by tests
Profiling and optimizing go programs
SSH I/O Streaming via Redis-based Persistent Message Queue -Mani Tadayon
nouka inventry manager
Python and rust 2018 pythonkorea jihun
About memcached
Automated tests - facts and myths
BlockChain implementation by python
Ruby Postgres
Memory Management in WordPress
root-ksk-2010
Go for the paranoid network programmer
Ad

Similar to strace for Perl Mongers (20)

KEY
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
PPT
Session Server - Maintaing State between several Servers
PDF
并发模型介绍
PDF
Linux sever building
PDF
Debugging: Rules & Tools
DOCX
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
PDF
Debugging: Rules And Tools - PHPTek 11 Version
PPT
Triple Blitz Strike
PDF
httpd — Apache Web Server
PDF
Performance tweaks and tools for Linux (Joe Damato)
PDF
Muduo network library
PDF
Alternative Infrastucture
KEY
Deploying Plack Web Applications: OSCON 2011
PDF
Debugging Ruby Systems
PDF
PHP projects beyond the LAMP stack
PDF
Building Scalable Websites with Perl
ODP
Apache httpd 2.4: The Cloud Killer App
PDF
AnyMQ, Hippie, and the real-time web
KEY
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
PPT
Perlbal Tutorial
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
Session Server - Maintaing State between several Servers
并发模型介绍
Linux sever building
Debugging: Rules & Tools
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Debugging: Rules And Tools - PHPTek 11 Version
Triple Blitz Strike
httpd — Apache Web Server
Performance tweaks and tools for Linux (Joe Damato)
Muduo network library
Alternative Infrastucture
Deploying Plack Web Applications: OSCON 2011
Debugging Ruby Systems
PHP projects beyond the LAMP stack
Building Scalable Websites with Perl
Apache httpd 2.4: The Cloud Killer App
AnyMQ, Hippie, and the real-time web
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Perlbal Tutorial
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing

strace for Perl Mongers