<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>thanks Gary,</div><div><br></div>I think you are right that the reported time warps are just&nbsp;suspiciously&nbsp;close to 2^32 nano seconds (~4.3 seconds) and the frequency of these time drift events is also matching, i.e., in the 10 second loop the problem appears 2-3 times on average.<div><br></div><div>I did add the less than max&nbsp;safety and it does appear to do the trick but just leaves me worried a bit that the "less save" code works better than the save one.<br><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">(defun warp ()</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">&nbsp;&nbsp;(declare (optimize (safety 2)))</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">&nbsp;&nbsp;(dotimes (i 1000)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">&nbsp;&nbsp; &nbsp;(let ((t1 (#_mach_absolute_time)))</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">&nbsp;&nbsp; &nbsp; &nbsp;(sleep 0.01)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">&nbsp;&nbsp; &nbsp; &nbsp;(let ((t2 (#_mach_absolute_time)))</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 12px/normal Helvetica; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;(when (&gt; t1 t2) (print (- t2 t1)))))))</div><div><br></div><div>In CCL intel 64 the problem never seems to manifest itself.</div><div><br></div><div>For now I can get by with this solution. Is there any reason to think why this could change in the future again or could&nbsp;(declare (optimize (safety 2))) have any negative consequences in CCL 64?</div><div><br></div><div>alex</div><div><br><div><div>On May 16, 2009, at 6:23 AM, Gary Byers wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>AFAIK, mach_absolute_time() is implemented in terms of the "rdtsc" (Read Time<br>Stamp Counter") instruction, which returns the value of a 64-bit on-chip<br>counter that increments once every machine cycle.<br><br>There are a few things that can make it difficult to use the value returned<br>by rdtsc for high-resolution timing.<br><br>1) the time-stamp counters are per-cpu, which generally means that unless the<br>OS takes steps to get and keep the TSCs of all CPU cores in sync, the value<br>returned by a rdtsc executed on CPU B may be less than a value returned on<br>CPU A. &nbsp;As far as I know, OSX deals with this pretty well.<br><br>2) CPUs (especially those used in laptops and many consumer machines)<br>don't always run at the same clock rate these days; they'll often<br>switch into low-power states where the number of machine cycles per second<br>is less then the maximum. &nbsp;On a Core2-Quad desktop machine running Linux<br><br>$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies 2394000 1596000<br><br>which says that a machine marketed as having a 2.4GHz CPU can also run at<br>a bit under 1.6GHz.<br><br>Things like mach_absolute_time() have to be able to account for the fact that<br>the CPU frequency is dynamic (and therefore the values returned by rdtsc have<br>to be scaled by different amounts depending on the CPU frequency.) &nbsp;I'm sure<br>that mach_absolute_time() does deal with this, but I remember looking at the<br>code once and thinking that it was a lot more complicated than I would have<br>guessed.<br><br>3) [Even fuzzier.] &nbsp;'rdtsc' instructions can be virtualized (meaning that things<br>like VMWare and Parallels can detect their use by at least some programs.) &nbsp;I<br>don't know enough about the technology involved to know whether this means that<br>a 'hypervisor' (or whatever they call it) can affect the results returned by<br>rdtsc to host-OS programs.<br><br>Both the result of the rdtsc instruction and the 64-bit integer<br>returned by mach_absolute_time() are returned as a pair of 32-bit<br>integers (containing the low and high 32 bits of the result.) &nbsp;I find<br>it awfully suspicious that the absolute value of difference between t2<br>and t1 in the cases where time travel occurred is as close as it is to<br>2^32 (as if some code in mach_absolute_time() neglected to add a carry<br>bit out of the low half into the high half.<br><br>CCL just takes those 2 32-bit halves of mach_absolute_time()'s result<br>and makes a lisp integer (almost always a bignum in the 32-bit lisp)<br>out of them. (There -is- a bug here, in that 64-bit return values from<br>foreign function calls aren't handled correctly in the x8632 CCL when<br>the argument and return-value processing occur out of line; in your<br>DOTIMES loop, that'd all happen in compiled code unless DEBUG or SAFETY<br>optimize settings are cranked up, and I don't think that there's a problem<br>when they aren't.<br><br>So yes, I can think of a few explanations. &nbsp;I have no idea if any of them<br>is correct, or what the workaround would be.<br><br>FWIW, I can't get the DOTIMES loop below to fail unless I add a<br>"(DECLARE (OPTIMIZE (SAFETY 3)))"; when it fails in that case, it's<br>confused about the sign bit of the low 32 bits of the result (bit 31),<br>and you're seeing apparent confusion about the value of the low bit of<br>the high 32 bits (bit 32).<br><br><br>On Fri, 15 May 2009, Alexander Repenning wrote:<br><br><blockquote type="cite">In some animations I found irregularities which I traced back to some strange behavior of mach_absolute_time<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">This loop should produce no output. On PPCs it works but on my Intel-Mac (with CCL 32) I get some output indicating that once in a while my computer appears to travel back in time for about 4 seconds<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">(dotimes (i 1000)<br></blockquote><blockquote type="cite">(let ((t1 (#_mach_absolute_time)))<br></blockquote><blockquote type="cite"> &nbsp;(sleep 0.01)<br></blockquote><blockquote type="cite"> &nbsp;(let ((t2 (#_mach_absolute_time)))<br></blockquote><blockquote type="cite"> &nbsp;&nbsp;&nbsp;(when (&gt; t1 t2) (print (- t2 t1))))))<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">-4284773073<br></blockquote><blockquote type="cite">-4284777830<br></blockquote><blockquote type="cite">-4284869530<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Can anybody reproduce this and is there any explanation, work around for this kind of madness?<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Alex<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Prof. Alexander Repenning<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">University of Colorado<br></blockquote><blockquote type="cite">Computer Science Department<br></blockquote><blockquote type="cite">Boulder, CO 80309-430<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">vCard: <a href="http://www.cs.colorado.edu/~ralex/AlexanderRepenning.vcf">http://www.cs.colorado.edu/~ralex/AlexanderRepenning.vcf</a><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><br></div></blockquote></div><br><div apple-content-edited="true"> <div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">Prof. Alexander Repenning</font></p><p style="margin: 0.0px 0.0px 0.0px 0.0px"><br class="khtml-block-placeholder"></p><p style="margin: 0.0px 0.0px 0.0px 0.0px">University of Colorado</p><p style="margin: 0.0px 0.0px 0.0px 0.0px">Computer Science Department</p><p style="margin: 0.0px 0.0px 0.0px 0.0px">Boulder, CO 80309-430</p><p style="margin: 0.0px 0.0px 0.0px 0.0px"><br class="khtml-block-placeholder"></p><p style="margin: 0.0px 0.0px 0.0px 0.0px"><font face="Helvetica" size="3" style="font: 12.0px Helvetica">vCard: <a href="http://www.cs.colorado.edu/~ralex/AlexanderRepenning.vcf">http://www.cs.colorado.edu/~ralex/AlexanderRepenning.vcf</a></font></p><br class="Apple-interchange-newline"></span></div> </div><br></div></div></div></body></html>