<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; NIF</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/nif/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>例证NIF使用的误区</title>
		<link>http://blog.yufeng.info/archives/953</link>
		<comments>http://blog.yufeng.info/archives/953#comments</comments>
		<pubDate>Thu, 02 Dec 2010 07:11:31 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[NIF]]></category>
		<category><![CDATA[堵死调度器]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=953</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: 例证NIF使用的误区 NIF是什么? A NIF library contains native implementation of some functions of an Erlang module. 不清楚的同学请参考http://www.erlang.org/doc/man/erl_nif.html. 这个功能对于扩展Erlang,利用现有的遗留c的财产,提高性能非常有帮助. 但是通常同学们会无视手册里面的一句话: Avoid doing lengthy work in NIF calls as that may degrade the responsiveness of the VM. NIFs are called directly by the same scheduler thread that executed the calling Erlang code. The calling scheduler [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/953">例证NIF使用的误区</a></p>
</div>
<p>NIF是什么?  A NIF library contains native implementation of some functions of an Erlang module.<br />
不清楚的同学请参考<a href="http://www.erlang.org/doc/man/erl_nif.html">http://www.erlang.org/doc/man/erl_nif.html</a>.<br />
这个功能对于扩展Erlang,利用现有的遗留c的财产,提高性能非常有帮助.<br />
但是通常同学们会无视手册里面的一句话:</p>
<blockquote><p>Avoid doing lengthy work in NIF calls as that may degrade the responsiveness of the VM. NIFs are called directly by the same scheduler thread that executed the calling Erlang code. The calling scheduler will thus be blocked from doing any other work until the NIF returns</p></blockquote>
<p>导致了非常严重的设计问题. 比如在NIF里面调用mysql client api, 作费时的IO操作等等, 我已经看到好几个同学这么干了,为了揭示这个问题的严重性, davisp同学为我们写了个例子来演示这个问题:  代码在这里<a href=" https://github.com/davisp/sleepy"></p>
<p>https://github.com/davisp/sleepy</a>.</p>
<blockquote><p>Sleepy &#8211; A misbehaving NIF<br />
This demonstrates what happens if a NIF takes a long time and is called from as many schedulers as exist in the VM. Namely, that the VM is halted until the NIF functions return.</p></blockquote>
<p>我们来演示下把:<br />
<span id="more-953"></span></p>
<pre class="brush: bash; title: ; notranslate">
#  git clone https://github.com/davisp/sleepy.git
git clone https://github.com/davisp/sleepy.git
Initialized empty Git repository in /tmp/sleepy/.git/
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
# cd sleepy
## 这个davisp同学是个Mac控, 我们在 linux下工作的, 需要修改下Makefile
# diff Makefile Makefile.orig
2c2
&lt; INCLUDES = -I$(OTPROOT)/erts-5.7.5/include/
---
&gt; INCLUDES = -I$(OTPROOT)/erts-5.8.2/include/
5c5
&lt; GCCFLAGS = -O3 -fPIC -bundle -flat_namespace -undefined suppress -fno-common -Wall
---
&gt; #GCCFLAGS = -O3 -fPIC -bundle -flat_namespace -undefined suppress -fno-common -Wall
8c8
&lt; #GCCFLAGS = -O3 -fPIC -shared -fno-common -Wall
---
&gt; GCCFLAGS = -O3 -fPIC -shared -fno-common -Wall

##david同学写的代码不够体贴人
# diff sleepy.erl  sleepy.erl.orig
6c6
&lt;     erlang:load_nif(&quot;./sleepy&quot;, 0).
---
&gt;     erlang:load_nif(&quot;sleepy&quot;, 0).

# 好吧, 编译运行

# make run
erl -noshell -s lockvm lock -s init stop
Starting heartbeat.
Tick
Tick
Tick
Tick
Locking the VM 从这开始你的VM被堵死
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
</pre>
<p>我们可以看到NIF在sleep, 堵死了所有的调度器, 你的erlang VM也在睡觉, zZzZZz, 好舒服哦, 你的系统性能就惨了.</p>
<p>结论: 避免在NIF里面作费时的操作.
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/953/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>R13B04  NIF improvements</title>
		<link>http://blog.yufeng.info/archives/213</link>
		<comments>http://blog.yufeng.info/archives/213#comments</comments>
		<pubDate>Tue, 16 Feb 2010 03:37:38 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[NIF]]></category>
		<category><![CDATA[R13B04]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=213</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: R13B04 NIF improvements OTP-8335 NIF improvements: Driver API for multi-threading made available for NIFs. Support for mempory managed (garbage collected) resource objects. A way to pass &#8220;pointers&#8221; to native data structures between C and Erlang in a safe way. Various new functions, like enif_inspect_iolist_as_binary, enif_make_sub_binary, enif_get_string, enif_get_atom, enif_make_tuple_from_array, enif_make_list_from_array, enif_make_existing_atom. Post Footer [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/213">R13B04  NIF improvements</a></p>
</div>
<p><strong>OTP-8335  NIF improvements:</strong></p>
<p>              Driver API for multi-threading made available for NIFs.</p>
<p>              Support for mempory managed (garbage collected) resource objects.<br />
              A way to pass &#8220;pointers&#8221; to native data structures between C and<br />
              Erlang in a safe way.</p>
<p>              Various new functions, like enif_inspect_iolist_as_binary,<br />
              enif_make_sub_binary, enif_get_string, enif_get_atom,<br />
              enif_make_tuple_from_array, enif_make_list_from_array,<br />
              enif_make_existing_atom.</p>
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/213/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

