<?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/"
	>

<channel>
	<title>std::cout &#60;&#60; "toto" ;</title>
	<atom:link href="http://gansanay.net/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gansanay.net/blog</link>
	<description>Le développement, son architecture, sa gestion, et d'autres choses.</description>
	<pubDate>Wed, 22 Apr 2009 09:13:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>fr</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mémoire utilisée par le processus courant. Episode 1 : MacOS X</title>
		<link>http://gansanay.net/blog/?p=14</link>
		<comments>http://gansanay.net/blog/?p=14#comments</comments>
		<pubDate>Tue, 21 Apr 2009 15:38:43 +0000</pubDate>
		<dc:creator>gu</dc:creator>
		
		<category><![CDATA[Système]]></category>

		<guid isPermaLink="false">http://gansanay.net/geek/?p=14</guid>
		<description><![CDATA[C&#8217;est le morceau de code que j&#8217;aime bien avoir sous la main quand je commence à développer quelque chose de conséquent : voir l&#8217;augmentation de la mémoire utilisée après chaque instruction.
A la base, sous MacOS X, on se dit qu&#8217;on va pouvoir utiliser la méthode Unix classique à base de getrusage. Mais hélas, par cette [...]]]></description>
			<content:encoded><![CDATA[<p>C&#8217;est le morceau de code que j&#8217;aime bien avoir sous la main quand je commence à développer quelque chose de conséquent : voir l&#8217;augmentation de la mémoire utilisée après chaque instruction.</p>
<p>A la base, sous MacOS X, on se dit qu&#8217;on va pouvoir utiliser la méthode Unix classique à base de <a href="http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/ManPages/man2/getrusage.2.html">getrusage</a>. Mais hélas, par cette méthode on n&#8217;obtient que des usages mémoire nuls. Ce qui est normal vu que dans la structure <code>rusage</code>, seules les infos de temps sont remplies, et tout le reste est simplement défini mais laissé nul. L&#8217;explication est dans <code>sys/resource.h</code> :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
 * A structure representing an accounting of resource utilization.  The
 * address of an instance of this structure is the second parameter to
 * getrusage().
 *
 * Note: All values other than ru_utime and ru_stime are implementaiton
 *       defined and subject to change in a future release.  Their use
 *       is discouraged for standards compliant programs.
 */</span>
<span style="color: #993333;">struct</span>   rusage <span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">struct</span> timeval ru_utime<span style="color: #339933;">;</span>   <span style="color: #808080; font-style: italic;">/* user time used (PL) */</span>
   <span style="color: #993333;">struct</span> timeval ru_stime<span style="color: #339933;">;</span>   <span style="color: #808080; font-style: italic;">/* system time used (PL) */</span>
<span style="color: #339933;">#if defined(_POSIX_C_SOURCE) &amp;&amp; !defined(_DARWIN_C_SOURCE)</span>
   <span style="color: #993333;">long</span>  ru_opaque<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">14</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* implementation defined */</span>
<span style="color: #339933;">#else /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */</span>
   <span style="color: #808080; font-style: italic;">/*
    * Informational aliases for source compatibility with programs
    * that need more information than that provided by standards,
    * and which do not mind being OS-dependent.
    */</span>
   <span style="color: #993333;">long</span>  ru_maxrss<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* max resident set size (PL) */</span>
<span style="color: #339933;">#define  ru_first ru_ixrss /* internal: ruadd() range start */</span>
   <span style="color: #993333;">long</span>  ru_ixrss<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* integral shared memory size (NU) */</span>
   <span style="color: #993333;">long</span>  ru_idrss<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* integral unshared data (NU)  */</span>
   <span style="color: #993333;">long</span>  ru_isrss<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* integral unshared stack (NU) */</span>
   <span style="color: #993333;">long</span>  ru_minflt<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* page reclaims (NU) */</span>
   <span style="color: #993333;">long</span>  ru_majflt<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* page faults (NU) */</span>
   <span style="color: #993333;">long</span>  ru_nswap<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* swaps (NU) */</span>
   <span style="color: #993333;">long</span>  ru_inblock<span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* block input operations (atomic) */</span>
   <span style="color: #993333;">long</span>  ru_oublock<span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* block output operations (atomic) */</span>
   <span style="color: #993333;">long</span>  ru_msgsnd<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* messages sent (atomic) */</span>
   <span style="color: #993333;">long</span>  ru_msgrcv<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* messages received (atomic) */</span>
   <span style="color: #993333;">long</span>  ru_nsignals<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* signals received (atomic) */</span>
   <span style="color: #993333;">long</span>  ru_nvcsw<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* voluntary context switches (atomic) */</span>
   <span style="color: #993333;">long</span>  ru_nivcsw<span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* involuntary &quot; */</span>
<span style="color: #339933;">#define  ru_last     ru_nivcsw   /* internal: ruadd() range end */</span>
<span style="color: #339933;">#endif   /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Sachant cela, on n&#8217;a plus qu&#8217;à trouver comment le noyau Darwin nous donne l&#8217;info. Réponse dans les sources du kernel, ou plus aisément dans la <a href="http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/">documentation des interfaces</a>. Il existe effectivement une <a href="http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_basic_info.html">structure d&#8217;informations sur un processus</a> dans <code>mach/task_info.h</code> :</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> task_basic_info <span style="color: #009900;">&#123;</span>
        integer_t       suspend_count<span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* suspend count for task */</span>
        vm_size_t       virtual_size<span style="color: #339933;">;</span>   <span style="color: #808080; font-style: italic;">/* virtual memory size (bytes) */</span>
        vm_size_t       resident_size<span style="color: #339933;">;</span>  <span style="color: #808080; font-style: italic;">/* resident memory size (bytes) */</span>
        time_value_t    user_time<span style="color: #339933;">;</span>      <span style="color: #808080; font-style: italic;">/* total user run time for
                                           terminated threads */</span>
        time_value_t    system_time<span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* total system run time for
                                           terminated threads */</span>
	policy_t	policy<span style="color: #339933;">;</span>		<span style="color: #808080; font-style: italic;">/* default policy for new threads */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>et une méthode <a href="http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_info.html"><code>task_info</code></a> pour l&#8217;obtenir.</p>
<h3>Le code qui va bien !</h3>
<p>La fonction qui, au final, me sert à lire l&#8217;occupation mémoire du processus courant est la suivante :</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include    /* pour mach_task_self() */</span>
<span style="color: #339900;">#include</span>
<span style="color: #339900;">#include </span>
&nbsp;
<span style="color: #0000ff;">size_t</span> getusedmem_macosx<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">size_t</span> <span style="color: #000040;">*</span>res_size, <span style="color: #0000ff;">size_t</span> <span style="color: #000040;">*</span>virt_size <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">struct</span> task_basic_info t_info <span style="color: #008080;">;</span>
    mach_msg_type_number_t t_info_count <span style="color: #000080;">=</span> TASK_BASIC_INFO_COUNT <span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> KERN_SUCCESS <span style="color: #000040;">!</span><span style="color: #000080;">=</span> task_info<span style="color: #008000;">&#40;</span> mach_task_self<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
        TASK_BASIC_INFO, <span style="color: #008000;">&#40;</span>task_info_t<span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>t_info, <span style="color: #000040;">&amp;</span>t_info_count <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #000040;">*</span>res_size <span style="color: #000080;">=</span> t_info.<span style="color: #007788;">resident_size</span> <span style="color: #008080;">;</span>
    <span style="color: #000040;">*</span>virt_size <span style="color: #000080;">=</span> t_info.<span style="color: #007788;">virtual_size</span> <span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://gansanay.net/blog/?feed=rss2&amp;p=14</wfw:commentRss>
		</item>
	</channel>
</rss>
