Sophie

Sophie

distrib > Fedora > 17 > i386 > media > updates > by-pkgid > 675c8c8167236dfcf8d66da674f931e8 > files > 47

erlang-doc-R15B-03.3.fc17.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../otp_doc.css" type="text/css">
<title>Erlang -- Drivers</title>
</head>
<body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container">
<script id="js" type="text/javascript" language="JavaScript" src="../js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript">
            <!--
              function getWinHeight() {
                var myHeight = 0;
                if( typeof( window.innerHeight ) == 'number' ) {
                  //Non-IE
                  myHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth ||
                                                         document.documentElement.clientHeight ) ) {
                  //IE 6+ in 'standards compliant mode'
                  myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                  //IE 4 compatible
                  myHeight = document.body.clientHeight;
                }
                return myHeight;
              }

              function setscrollpos() {
                var objf=document.getElementById('loadscrollpos');
                 document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2;
              }

              function addEvent(obj, evType, fn){
                if (obj.addEventListener){
                obj.addEventListener(evType, fn, true);
                return true;
              } else if (obj.attachEvent){
                var r = obj.attachEvent("on"+evType, fn);
                return r;
              } else {
                return false;
              }
             }

             addEvent(window, 'load', setscrollpos);

             //--></script><div id="leftnav"><div class="innertube">
<img alt="Erlang logo" src="../erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="../pdf/otp-system-documentation-5.9.3.1.pdf">PDF</a><br><a href="../index.html">Top</a></small><p><strong>Efficiency Guide</strong><br><strong>User's Guide</strong><br><small>Version 5.9.3.1</small></p>
<br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p>
<ul class="flipMenu" imagepath="../js/flipmenu">
<li id="no" title="Introduction" expanded="false">Introduction<ul>
<li><a href="introduction.html">
              Top of chapter
            </a></li>
<li title="Purpose"><a href="introduction.html#id62954">Purpose</a></li>
<li title="Prerequisites"><a href="introduction.html#id65420">Prerequisites</a></li>
</ul>
</li>
<li id="no" title="The Eight Myths of Erlang Performance" expanded="false">The Eight Myths of Erlang Performance<ul>
<li><a href="myths.html">
              Top of chapter
            </a></li>
<li title="Myth: Funs are slow"><a href="myths.html#id64448">Myth: Funs are slow</a></li>
<li title="Myth: List comprehensions are slow"><a href="myths.html#id61406">Myth: List comprehensions are slow</a></li>
<li title="Myth: Tail-recursive functions are MUCH faster
    than recursive functions"><a href="myths.html#id62291">Myth: Tail-recursive functions are MUCH faster
    than recursive functions</a></li>
<li title="Myth: '++' is always bad"><a href="myths.html#id60058">Myth: '++' is always bad</a></li>
<li title="Myth: Strings are slow"><a href="myths.html#id62534">Myth: Strings are slow</a></li>
<li title="Myth: Repairing a Dets file is very slow"><a href="myths.html#id61135">Myth: Repairing a Dets file is very slow</a></li>
<li title="Myth: BEAM is a stack-based byte-code virtual machine (and therefore slow)"><a href="myths.html#id64006">Myth: BEAM is a stack-based byte-code virtual machine (and therefore slow)</a></li>
<li title="Myth: Use '_' to speed up your program when a variable is not used"><a href="myths.html#id64026">Myth: Use '_' to speed up your program when a variable is not used</a></li>
</ul>
</li>
<li id="no" title="Common Caveats" expanded="false">Common Caveats<ul>
<li><a href="commoncaveats.html">
              Top of chapter
            </a></li>
<li title="The timer module"><a href="commoncaveats.html#id62547">The timer module</a></li>
<li title="list_to_atom/1"><a href="commoncaveats.html#id61885">list_to_atom/1</a></li>
<li title="length/1"><a href="commoncaveats.html#id60806">length/1</a></li>
<li title="setelement/3"><a href="commoncaveats.html#id58300">setelement/3</a></li>
<li title="size/1"><a href="commoncaveats.html#id65402">size/1</a></li>
<li title="split_binary/2"><a href="commoncaveats.html#id60836">split_binary/2</a></li>
<li title="The '--' operator"><a href="commoncaveats.html#id61766">The '--' operator</a></li>
</ul>
</li>
<li id="no" title="Constructing and matching binaries" expanded="false">Constructing and matching binaries<ul>
<li><a href="binaryhandling.html">
              Top of chapter
            </a></li>
<li title="How binaries are implemented"><a href="binaryhandling.html#id62932">How binaries are implemented</a></li>
<li title="Constructing binaries"><a href="binaryhandling.html#id66066">Constructing binaries</a></li>
<li title="Matching binaries"><a href="binaryhandling.html#id63610">Matching binaries</a></li>
</ul>
</li>
<li id="no" title="List handling" expanded="false">List handling<ul>
<li><a href="listHandling.html">
              Top of chapter
            </a></li>
<li title="Creating a list"><a href="listHandling.html#id66709">Creating a list</a></li>
<li title="List comprehensions"><a href="listHandling.html#id66805">List comprehensions</a></li>
<li title="Deep and flat lists"><a href="listHandling.html#id66875">Deep and flat lists</a></li>
<li title="Why you should not worry about recursive lists functions"><a href="listHandling.html#id67017">Why you should not worry about recursive lists functions</a></li>
</ul>
</li>
<li id="no" title="Functions" expanded="false">Functions<ul>
<li><a href="functions.html">
              Top of chapter
            </a></li>
<li title="Pattern matching"><a href="functions.html#id67142">Pattern matching</a></li>
<li title="Function Calls "><a href="functions.html#id67362">Function Calls </a></li>
<li title="Memory usage in recursion"><a href="functions.html#id67506">Memory usage in recursion</a></li>
</ul>
</li>
<li id="no" title="Tables and databases" expanded="false">Tables and databases<ul>
<li><a href="tablesDatabases.html">
              Top of chapter
            </a></li>
<li title="Ets, Dets and Mnesia"><a href="tablesDatabases.html#id67596">Ets, Dets and Mnesia</a></li>
<li title="Ets specific"><a href="tablesDatabases.html#id67983">Ets specific</a></li>
<li title="Mnesia specific"><a href="tablesDatabases.html#id68087">Mnesia specific</a></li>
</ul>
</li>
<li id="no" title="Processes" expanded="false">Processes<ul>
<li><a href="processes.html">
              Top of chapter
            </a></li>
<li title="Creation of an Erlang process"><a href="processes.html#id68191">Creation of an Erlang process</a></li>
<li title="Process messages"><a href="processes.html#id68339">Process messages</a></li>
<li title="The SMP emulator"><a href="processes.html#id68531">The SMP emulator</a></li>
</ul>
</li>
<li id="loadscrollpos" title="Drivers" expanded="true">Drivers<ul>
<li><a href="drivers.html">
              Top of chapter
            </a></li>
<li title="Drivers and concurrency"><a href="drivers.html#id68634">Drivers and concurrency</a></li>
<li title="Avoiding copying of binaries when calling a driver"><a href="drivers.html#id68675">Avoiding copying of binaries when calling a driver</a></li>
<li title="Returning small binaries from a driver"><a href="drivers.html#id68743">Returning small binaries from a driver</a></li>
<li title="Returning big binaries without copying from a driver"><a href="drivers.html#id68777">Returning big binaries without copying from a driver</a></li>
</ul>
</li>
<li id="no" title="Advanced" expanded="false">Advanced<ul>
<li><a href="advanced.html">
              Top of chapter
            </a></li>
<li title="Memory"><a href="advanced.html#id68919">Memory</a></li>
<li title="System limits"><a href="advanced.html#id69276">System limits</a></li>
</ul>
</li>
<li id="no" title="Profiling" expanded="false">Profiling<ul>
<li><a href="profiling.html">
              Top of chapter
            </a></li>
<li title="Do not guess about performance - profile"><a href="profiling.html#id69579">Do not guess about performance - profile</a></li>
<li title="Big systems"><a href="profiling.html#id69661">Big systems</a></li>
<li title="What to look for"><a href="profiling.html#id69681">What to look for</a></li>
<li title="Tools"><a href="profiling.html#id69738">Tools</a></li>
<li title="Benchmarking"><a href="profiling.html#id70245">Benchmarking</a></li>
</ul>
</li>
</ul>
</div></div>
<div id="content">
<div class="innertube">
<h1>9 Drivers</h1>
  

  <p>This chapter provides a (very) brief overview on how to write efficient
  drivers. It is assumed that you already have a good understanding of
  drivers.</p>

  <h3><a name="id68634">9.1 
        Drivers and concurrency</a></h3>
    

    <p>The run-time system will always take a lock before running
    any code in a driver.</p>

    <p>By default, that lock will be at the driver level, meaning that
    if several ports have been opened to the same driver, only code for
    one port at the same time can be running.</p>

    <p>A driver can be configured to instead have one lock for each port.</p>

    <p>If a driver is used in a functional way (i.e. it holds no state,
    but only does some heavy calculation and returns a result), several
    ports with registered names can be opened beforehand and the port to
    be used can be chosen based on the scheduler ID like this:</p>

    <div class="example"><pre>
-define(PORT_NAMES(),
	{some_driver_01, some_driver_02, some_driver_03, some_driver_04,
	 some_driver_05, some_driver_06, some_driver_07, some_driver_08,
	 some_driver_09, some_driver_10, some_driver_11, some_driver_12,
	 some_driver_13, some_driver_14, some_driver_15, some_driver_16}).

client_port() -&gt;
    element(erlang:system_info(scheduler_id) rem tuple_size(?PORT_NAMES()) + 1,
	    ?PORT_NAMES()).</pre></div>

    <p>As long as there are no more than 16 schedulers, there will never
    be any lock contention on the port lock for the driver.</p>	    

  

  <h3><a name="id68675">9.2 
        Avoiding copying of binaries when calling a driver</a></h3>
    

    <p>There are basically two ways to avoid copying a binary that is
    sent to a driver.</p>

    <p>If the <span class="code">Data</span> argument for
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erlang.html#port_control-3');">port_control/3</a></span>
    is a binary, the driver will be passed a pointer to the contents of
    the binary and the binary will not be copied.
    If the <span class="code">Data</span> argument is an iolist (list of binaries and lists),
    all binaries in the iolist will be copied.</p>

    <p>Therefore, if you want to send both a pre-existing binary and some
    additional data to a driver without copying the binary, you must call
    <span class="code">port_control/3</span> twice; once with the binary and once with the
    additional data. However, that will only work if there is only one
    process communicating with the port (because otherwise another process
    could call the driver in-between the calls).</p>

    <p>Another way to avoid copying binaries is to implement an <span class="code">outputv</span>
    callback (instead of an <span class="code">output</span> callback) in the driver.
    If a driver has an <span class="code">outputv</span> callback, refc binaries passed
    in an iolist in the <span class="code">Data</span> argument for
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erlang.html#port_command-2');">port_command/2</a></span>
    will be passed as references to the driver.</p>
  

  <h3><a name="id68743">9.3 
        Returning small binaries from a driver</a></h3>
    

    <p>The run-time system can represent binaries up to 64 bytes as
    heap binaries. They will always be copied when sent in a messages,
    but they will require less memory if they are not sent to another
    process and garbage collection is cheaper.</p>

    <p>If you know that the binaries you return are always small,
    you should use driver API calls that do not require a pre-allocated
    binary, for instance
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#int%20driver_output-3');">driver_output()</a></span>
    or 
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#int%20driver_output_term-3');">driver_output_term()</a></span>
    using the <span class="code">ERL_DRV_BUF2BINARY</span> format,
    to allow the run-time to construct a heap binary.</p>

  

  <h3><a name="id68777">9.4 
        Returning big binaries without copying from a driver</a></h3>
    

    <p>To avoid copying data when a big binary is sent or returned from
    the driver to an Erlang process, the driver must first allocate the
    binary and then send it to an Erlang process in some way.</p>
    
    <p>Use <span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#ErlDrvBinary*%20driver_alloc_binary-1');">driver_alloc_binary()</a></span> to allocate a binary.</p>

    <p>There are several ways to send a binary created with
    <span class="code">driver_alloc_binary()</span>.</p>

    <ul>
    <li>
<p>From the <span class="code">control</span> callback, a binary can be returned provided
        that
	<span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#void%20set_port_control_flags-2');">set_port_control()</a></span>
	 has been called with the flag value <span class="code">PORT_CONTROL_FLAG_BINARY</span>.</p>
    </li>

    <li><p>A single binary can be sent with
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#int%20driver_output_binary-6');">driver_output_binary()</a></span>.</p></li>

    <li>
<p>Using
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#int%20driver_output_term-3');">driver_output_term()</a></span>
    or
    <span class="bold_code"><a href="javascript:erlhref('../../','erts','erl_driver.html#int%20driver_send_term-4');">driver_send_term()</a></span>,
    a binary can be included in an Erlang term.</p>
    </li>
    </ul>

  

</div>
<div class="footer">
<hr>
<p>Copyright © 2001-2012 Ericsson AB. All Rights Reserved.</p>
</div>
</div>
</div></body>
</html>