In general, you want your duity cycle to be faster than the PM's sampling rate.

To mimic real things, block on a Kernal object. This is normally the file or whatever that it needs to complete. But make it a "waitable timer" and set it to go off at a precise time. So your loop would chew CPU for so many quantums, then block for so many. Look up the quantum value (how long a timeslice is), and use high-precision timing primitives rather than localtime, since you must get an order of magnitude (or two) more granularity.

The waitable timer is specified to milisecond resolution. The native 64-bit time value (GetSystemTimeAsFileTime) is updated once a quantum, so you can watch it jump if you are reading it in a loop. That tells you what the timeslice size is, too!

A quick check:

use strict; use warnings; use Win32::API; my $f= new Win32::API ('Kernel32.dll', 'GetSystemTimeAsFileTime', ['P' +], 'V'); my $buffer= 'x'x8; #8 byte value for (1..100000000) { $f->Call ($buffer); } # printf "%vx\n", $buffer;
under PermMon shows that it does stay in User mode. That is, the constant calling to GetSystemTimeyadayada doesn't mess things up by constantly switching to kernel mode. Is should be simple, if the internal variable is simply accessed and returned.

I'm getting roughly 300,000 iterations per second, which is quite enough to get high-granularity: spin for a few miliseconds, block for a few miliseconds.

—John


In reply to Re: gulpng CPU with precision by John M. Dlugosz
in thread gulpng CPU with precision by mandog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.