9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Maksim Radziwill" <7c00@wp.pl>
To: <9fans@cse.psu.edu>
Subject: [9fans] A quite amusing thing...
Date: Sat, 22 Feb 2003 20:17:50 +0100	[thread overview]
Message-ID: <000601c2daa7$17972450$270363d9@makr4j0ty5i9an> (raw)

[-- Attachment #1: Type: text/plain, Size: 5773 bytes --]

Hi everyone,
I recently have found an amusing thing between (FSD-)Amoeba and Plan9 :
The code for cpu speed identification is the same ;)

Here I paste it. If you don't believe youre eyes check out
/sys/src/9/pc/i8253.c in the plan9 system
/profile/module/amoeba/src/machdep/dev/ibm_at/timer/pit.c in the
FSD-Amoeba system
(the sys source must be installed of course)

You can get FSD-Amoeba at fsd-amoeba.sourceforge.net it is the
continuation of
the original Amoeba project (www.cs.vu.nl/pub/amoeba) who has been
terminated
in 1996.

++++++ i8253.c the guesscpuhz function
void
guesscpuhz(int aalcycles)
{
      int cpufreq, loops, incr, x, y;
      uvlong a, b;

      /* find biggest loop that doesn't wrap */
      incr = 16000000/(aalcycles*HZ*2);
      x = 2000;
      for(loops = incr; loops < 64*1024; loops += incr) {

            /*
             *  measure time for the loop
             *
             *                MOVL  loops,CX
             *    aaml1:           AAM
             *                LOOP  aaml1
             *
             *  the time for the loop should be independent of external
             *  cache and memory system since it fits in the execution
             *  prefetch buffer.
             *
             */
            outb(Tmode, Latch0);
            if(m->havetsc)
                  rdtsc(&a);
            x = inb(T0cntr);
            x |= inb(T0cntr)<<8;
            aamloop(loops);
            outb(Tmode, Latch0);
            if(m->havetsc)
                  rdtsc(&b);
            y = inb(T0cntr);
            y |= inb(T0cntr)<<8;
            x -= y;

            if(x < 0)
                  x += Freq/HZ;

            if(x > Freq/(3*HZ))
                  break;
      }

      /*
       *  figure out clock frequency and a loop multiplier for delay().
       *  n.b. counter goes up by 2*Freq
       */
      cpufreq = loops*((aalcycles*2*Freq)/x);
      m->loopconst = (cpufreq/1000)/aalcycles;  /* AAM+LOOP's for 1 ms
*/

      if(m->havetsc){

            /* counter goes up by 2*Freq */
            b = (b-a)<<1;
            b *= Freq;
            b /= x;

            /*
             *  round to the nearest megahz
             */
            m->cpumhz = (b+500000)/1000000L;
            m->cpuhz = b;
      } else {
            /*
             *  add in possible 0.5% error and convert to MHz
             */
            m->cpumhz = (cpufreq + cpufreq/200)/1000000;
            m->cpuhz = cpufreq;
      }

      i8253.hz = Freq<<Tickshift;
}

+++++ Amoeba's pit.c cpuspeed function

/* For cpu speed measurement. We need timer access. */

void
cpuspeed(int aalcycles, int havecycleclock)
{
        int cpufreq, loops, incr, x, y;
        unsigned long ax1,dx1,ax2,dx2,a,b;

        pit_init();

        /* find biggest loop that doesn't wrap */
        incr = 16000000/(aalcycles*HZ*2);
        x = 2000;
        for(loops = incr; loops < 64*1024; loops += incr) {

                /*
                 *  measure time for the loop
                 *
                 *                        MOVL        loops,CX
                 *        aaml1:                 AAM
                 *                        LOOP        aaml1
                 *
                 *  the time for the loop should be independent of
external
                 *  cache and memory system since it fits in the
execution
                 *  prefetch buffer.
                 *
                 */

                /* Beware of counter reset's (wraps)... */

                /* Read the cpu internal clock if available */
                if(havecycleclock)
                        rdmsr(0x10, &ax1,&dx1);
                x = (int)pit_channel0();

                aamloop(loops);

                if(havecycleclock)
                        rdmsr(0x10, &ax2,&dx2);

                y = (int)pit_channel0();

                x -= y;

                if(x < 0)
                        x += PIT_FREQ/PIT_HZ;

                if(x > PIT_FREQ/(3*PIT_HZ))
                        break;
        }

        /*
         *  figure out clock frequency and a loop multiplier for
delay().
         *  n.b. counter goes up by 2*PIT_FREQ
         */
        cpufreq = loops*((aalcycles*PIT_FREQ)/x);
        cpu.loopconst = (cpufreq/1000)/aalcycles; /* AAM+LOOP's for 1ms
*/

        /* check aalcycle value */
        if(kernel_option("cud") == 1)
                printf("cpuspeed: aalcycle MHz=%d\n",
                        (cpufreq + cpufreq/200)/1000000);

        if(havecycleclock){


                if(dx2 == dx1)
                        b = (ax2-ax1);
                else        /* counter wrap */
                        b = (ax2+0xffffffff-ax1);

                b /= x;
                b *= PIT_FREQ;


                /*
                 *  round to the nearest megahz
                 */
                cpu.cpumhz = (b+500000)/1000000L;
                cpu.cpuhz = b;
        } else {
                /*
                 *  add in possible 0.5% error and convert to MHz
                 */
                cpu.cpumhz = (cpufreq + cpufreq/200)/1000000;
                cpu.cpuhz = cpufreq;
        }

}


The thing is even more amusing since I heard someone saying plan9 is
written from the ground up... ;)

Hmm... It is possible too that Amoeba developer's have copied this code
but I doubt it.
Or maybe I'm missing something and the code comes from an other external
source...
(Don't forget these all are only hypotheses)

If someone could explain this it would be great, since I would like to
know the
only real true (story) ;)

Maks,


[-- Attachment #2: Type: text/html, Size: 69140 bytes --]

             reply	other threads:[~2003-02-22 19:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-22 19:17 Maksim Radziwill [this message]
2003-02-22 20:30 ` Russ Cox
2003-02-22 20:51 ` David Presotto
2003-02-23  2:16 ` Skip Tavakkolian
2003-02-23  3:53 ` John Packer
2003-02-23  6:10   ` northern snowfall
2003-02-23  8:30     ` John Packer
2003-02-23 20:26       ` northern snowfall
2003-02-23 21:28         ` John Packer
2003-02-23 22:00           ` northern snowfall
2003-02-22 20:34 philw
2003-02-22 20:40 ` Russ Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000601c2daa7$17972450$270363d9@makr4j0ty5i9an' \
    --to=7c00@wp.pl \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).