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,