Subject: Sleazy timing code
To: None <port-alpha@netbsd.org, port-i386@netbsd.org>
From: Hal Murray <murray@pa.dec.com>
List: port-i386
Date: 05/30/2000 03:25:05
I have some sleazy code that I use to measure CPU usage.  The idea 
is to run a low priority job that eats up all the otherwise unused 
CPU cycles.  The ones it doesn't get were obviously "used" by something.  

  The info that top and friends show are too inaccurate for my interests.  
  They get sampled on clock interrupts and that can easily get into 
  lock step with network traffic. 

The code I use looks like this:

  for ( ; ; ) {
    for (j = 0; j < xxx; j++) {}
    counter[cpu].ticks++;
    if (quit) break;
  }

xxx is adjusted (by trial and error) so that the counter gets bumped 
often enough to be accurate and infrequently enough so that the time 
spent bumping the counter doesn't use too many cycles. 

The key is to work out a calibration factor.  For that, I need two 
things.  One is the speed of the CPU.  The other is the number of 
cycles per inner loop iteration. 

So my first question...  Is there any clean way to get the CPU speed 
on a NetBSD system?  I've looked in things like sysctl hw but haven't 
found it.

  Right now, I just #include a small chunk of per-platform code and 
  maintain that by hand.  It's ugly, but it works. 

The other problem is the cycles-per-loop.  Typical numbers on i386 
systems are 4 to 6.  It obviously depends upon the type of CPU chip.  
It also seems to vary by 1 depending upon the alignment of the code 
in memory.  I don't care what it is as long as it doesn't change 
when I recompile something. 

What I've been doing so far is to put that subroutine first in its 
module, and load that module first.  That worked for 1.4.2.  (And 
since it worked, I didn't mess with it.)

But it doesn't seem to be working on the latest snapshot, at least 
on i386 systems.  I haven't tried an Alpha yet.

Is this a quirk with the new ELF stuff?  Is it rearranging code for 
me? 

Is there any way to force the compiler/loader to put some code at 
a specified alignment?

  If I put it into a library will it get page aligned or something 
  helpful like that? 

  I don't care how fast it goes as long as it does the same thing 
  after I change some other code and recompile/load. 

Is that even the right question?  Did something I haven't thought 
about change between 1.4.2 and the latest shapshot?