71
Learning Dtrace 2012.11.20 Outsider at

Learning Dtrace

Embed Size (px)

DESCRIPTION

pre

Citation preview

Page 1: Learning Dtrace

LearningDtrace2012.11.20Outsider at

Page 2: Learning Dtrace

DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time.

http://dtrace.org/blogs/about/dtracepony/

Page 3: Learning Dtrace

DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time.

http://dtrace.org/blogs/about/dtracepony/

Page 4: Learning Dtrace

DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time.

http://dtrace.org/blogs/about/dtracepony/

Page 5: Learning Dtrace

DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time.

http://dtrace.org/blogs/about/dtracepony/

Page 6: Learning Dtrace

DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time.

http://dtrace.org/blogs/about/dtracepony/

Page 7: Learning Dtrace

http://www.flickr.com/photos/ghost_of_kuji/370072145

Bryan Cantrill Mike Shapiro Adam Leventhal

Page 8: Learning Dtrace

Originallydevelopedfor

2003

Page 9: Learning Dtrace

Open-sourcedin 2005

http://www.flickr.com/photos/tinou/171803338

Page 10: Learning Dtrace

http://www.flickr.com/photos/epu/4299657320

Ported to Unix-like systems

Page 11: Learning Dtrace

MITTechnology Review2005

“Innovators Under 35”http://www2.technologyreview.com/tr35/profile.aspx?trid=91

Page 12: Learning Dtrace

TechnologyInnovationAwards 2006

Gold Winnerhttp://www.dowjones.com/innovation/ei_winners_2006.html

Page 13: Learning Dtrace

STUG award 2008

https://www.usenix.org/about/stug

Page 14: Learning Dtrace

Dive into Dtrace

Page 15: Learning Dtrace
Page 16: Learning Dtrace

코드가 실행되는 지점동적으로 생성프로덕션에서 사용가능

Probe

Page 17: Learning Dtrace

Probe List:

sudo dtrace -l

Page 18: Learning Dtrace

Probe의 내부 ID

Page 19: Learning Dtrace

Probe를 제공하는 곳(ex: syscall, profile)

Page 20: Learning Dtrace

Unix 모듈이나Probe의 어플리케이션 라이브러리의 이름

Page 21: Learning Dtrace

Probe가 존재하는함수의 이름

Page 22: Learning Dtrace

Probe의 이름

Page 23: Learning Dtrace

sudo dtrace -l -n PROBE이름

Page 24: Learning Dtrace

sudo dtrace -l -f FUNCTION

Page 25: Learning Dtrace

sudo dtrace -l -P PROVIDER

Page 26: Learning Dtrace

sudo dtrace -l -m MODULE

Page 27: Learning Dtrace

DScriptD Programming Language

Page 28: Learning Dtrace

.d 확장자컴파일해서 커널레벨에서 실행안전성을 위한 유효성 검사프로덕션에서 안전하게 실행

Features

Page 29: Learning Dtrace

DScript

KernelUser

DProgram

Output

Dtrace

Page 30: Learning Dtrace

probe-description/optional predicate/{ action statements;}

0102030405

Page 31: Learning Dtrace

probe-description/optional predicate/{ action statements;}

0102030405

provider:module:function:name(ex: syscall:::entry)

Page 32: Learning Dtrace

probe-description/optional predicate/{ action statements;}

0102030405

시스템의 상태를 수집하는 명령문

Page 33: Learning Dtrace

probe-description/optional predicate/{ action statements;}

0102030405

0이 아니거나 true면 action 수행

Page 34: Learning Dtrace

begin-end.dBEGIN { trace("begin the beguine"); exit(0); }END{ trace("that's all...");}

010203040506070809

Page 35: Learning Dtrace

begin-end.dBEGIN { trace("begin the beguine"); exit(0); }END{ trace("that's all...");}

010203040506070809

sudo dtrace -s begin-end.d

Page 36: Learning Dtrace

timer.dprofile:::tick-5sec{ trace("5sec timer");}profile:::tick-800msec{ trace("800msec timer");}

0102030405060708

Page 37: Learning Dtrace

beer.dint bottles;BEGIN{ bottles = 5; }profile:::tick-1sec/bottles >= 0/{ printf("%d bottles on the wall\n", bottles); printf("%d bottles.\n", bottles); printf("take one down, pass it around\n"); printf("%d bottles on the wall\n\n", bottles); bottles--; }profile:::tick-1sec/bottles < 0/{ exit(0); }END { printf("that's all..."); }

0102030405060708091011121314151617

Page 38: Learning Dtrace

no if-elseno loop?: operator is exist

No Flow Control

Page 39: Learning Dtrace

process.dproc:::exec-success{ printf("%s", execname);}

01020304

Page 40: Learning Dtrace

@: aggregation’s prefixname: aggregation’s namekey: D expression list (comma-separated)aggfunc: aggregation function

Aggregate

@name[key] = aggfunc(args)

Page 41: Learning Dtrace

count() : 호출횟수sum(expr) : 표현식의 전체 값avg(expr) : 표현식의 평균min(expr) : 표현식 중 가장 작은 값max(expr) : 표현식 중 가장 큰 값quantize(expr) : 2제곱의 빈도분포lquantize(expr,lower-bound, upper-bound, step-value) : 선형 빈도분포

Aggregate Function

Page 42: Learning Dtrace

aggr-count.dsyscall::read:entry{ @counts[execname] = count();}profile:::tick-5s{ exit(0);}

0102030405060708

Page 43: Learning Dtrace

aggr-quant.dsyscall::read:entry{ self->ts = timestamp;}syscall::read:return/self->ts/{ delta = timestamp - self->ts; @quanttime[execname] = quantize(delta);}profile:::tick-5s{ exit(0);}

0102030405060708091011121314

Page 44: Learning Dtrace

Dtracewith Node.js

Page 45: Learning Dtrace

http://www.flickr.com/photos/abandonedhero/3404826467

OS Requirements-Dtrace-ustack helper

Page 46: Learning Dtrace

USDTUser-Level Statically Defined Tracing

landed in node v0.6.7

Page 47: Learning Dtrace

http://www.flickr.com/photos/jepoirrier/2043728206

Full featureswork on OpenSolaris

Page 48: Learning Dtrace

Mac OS

Dtraceustack helper

Page 49: Learning Dtrace

Linux

Dtraceustack helper

Page 50: Learning Dtrace

WindowsDtrace

ustack helper

Page 51: Learning Dtrace

http://www.flickr.com/photos/vectorlyme/206472613

node.js is compiled with

--with-dtrace

Page 52: Learning Dtrace

“SunOS에서 활성화되지만 다른 시스템에선 동작하지 않는다”

Page 53: Learning Dtrace

Dtrace with node.json SmartOS of Joyent

Demo

Page 54: Learning Dtrace

server.jsvar http = require('http');http.createServer(function(req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n');}).listen(8124);

console.log('Server running!');

010203040506070809

Page 55: Learning Dtrace

http-server.dBEGIN{ printf("%7s %2s %5s %20s (%5s) %8s %s (%s)\n", "WHO", "FD", "RPORT", "REMOTE", "BUFFR", "METHOD", "URL", "FWDFOR");}

node*:::http-server-request{ printf("+SERVER %2d %5d %20s (%5d) %8s %s (%s)\n", args[1]->fd, args[1]->remotePort, args[1]->remoteAddress, args[1]->bufferSize, args[0]->method, args[0]->url, args[0]->forwardedFor);}

node*:::http-server-response{ printf("-SERVER %2d %5d %20s (%5d)\n", args[0]->fd, args[0]->remotePort, args[0]->remoteAddress, args[0]->bufferSize);}

010203040506070809101112131415161718192021

Page 56: Learning Dtrace

request-count.dhttp-server-request{ @[args[0]->url]=count()}

01020304

Page 57: Learning Dtrace

loop.jsnew Error().stack;

function main() { func1(); }function func1() { func2(); }

function func2() { (function () { for (;;); })();}main();

0102030405060708091011

Page 58: Learning Dtrace

profile.dprofile-97/execname == "node" && arg1/{ @[jstack(150, 8000)] = count();}tick-10sec{ exit(0);}

010203040506070809

Page 59: Learning Dtrace

FlameGraphstack trace visualizer

Page 61: Learning Dtrace

node-stackvis

$ npm install -g stackvis

Page 62: Learning Dtrace

$ sudo dtrace -s DSCRIPT > INPUT$ stackvis dtrace flamegraph-svg < INPUT > OUTPUT

Make FrameGraph

Page 63: Learning Dtrace

Alternativeon non-OpenSolarises

Page 64: Learning Dtrace

node-dtrace-providerhttps://github.com/chrisa/node-dtrace-provider

$ npm install dtrace-provider

Page 65: Learning Dtrace

var d = require('dtrace-provider');var dtp = d.createDTraceProvider('nodeapp');var p1 = dtp.addProbe('probe1', 'int', 'int');var p2 = dtp.addProbe('probe2', 'char *');dtp.enable();

0102030405

Page 66: Learning Dtrace

dtp.fire("probe1", function(p) { return [1, 2];});dtp.fire("probe2", function(p) { return ["hello, dtrace via provider", "foo"];});

010203040506

Page 67: Learning Dtrace

data type : int, charmaximum argument : 32

Limitations

Page 68: Learning Dtrace

interval.jsfunction interval(msg) { console.log(msg);}

setInterval(function() { interval('Hello Dtrace');}, 1000);

01020304050607

Page 69: Learning Dtrace

interval.jsinterval.jsvar d = require('dtrace-provider');

var dtp = d.createDTraceProvider('nodeapp');var p2 = dtp.addProbe('echo', 'char *');

function interval(msg) { dtp.fire('echo', function () { return [msg]; }); console.log(msg);} setInterval(function() { interval('Hello Dtrace');}, 1000);dtp.enable();

010203040506070809101112131415

Page 70: Learning Dtrace

interval.dnodeapp*:::echo{ trace(copyinstr(arg0));}

01020304

Page 71: Learning Dtrace

Thank you~

http://[email protected]@outsideris