Why the measurement of execution cycles is so different in different runs of a multithreaded program?

1

I solved a problem of concurrency that simulates a manufacturing cell products. I use 13 unnamed semaphores , which are handled by 8 threads running the "parts" of the problem.

The execution cycle measurement "t1 " is performed after phtread_create , and " t2" is performed after pthread_join, thus get the time worked the pthreads ( which is the value that interests me ) . Subtraction of t2 -t1 gives the execution time of the threads that are over.

I noticed that the range of values ​​obtained is too broad , and I have measured values ​​close to 30,000 cycles. And a value of 146 million cycles.

To measure cicles I use a driver " cicle_count " in which it reads the registry value R15 of Dual -core processor ARM Cortex A9, this register counts the number of instructions executed . The embedded system I use is the " ZedBoard " :

http://www.digilentinc.com/Products/Detail.cfm?NavPath=2,400.1028&Prod=ZEDBOARD

I made measurements with 2-threads problems, simpler and get similar values ​​between each measurement.

What am I doing wrong? can change both the measurement of execution time between each run of the program?

Edit 2: codes whit measurement dual and monocore.

For dual core execution (run. / Aplicacion.out):

I created two identical drivers (same code written, except the name of the device "DEVICE_NAME") but one is called "contador_ciclos1" and the other is called "contador_ciclos2" (it's okay that the two drivers are the same??)

these drivers are inserted one on each core with a script:

insmod hpn_driver.ko
-> Taskset 1 insmod contador_ciclos1.ko
-> Taskset 2 insmod contador_ciclos2.ko
 
mknod / dev / hpn_driver c 251 0
-> Mknod / dev/contador_ciclos1 c 250 0
-> Mknod / dev/contador_ciclos2 c 249 0

then, in the application I open the two drivers with two file descriptors, and take relevant measurements whit t2-t1 (core1) and t4-t3 (core2).

To monocore execution, insert the two drivers, but only open a driver and run taskset 1 to measure clock cycles on the driver "contador_ciclos1". And olny check to t2-t1 cicles.

this is a pseudo-code of the applicacion:

  int i = 0;
  char buff[24];
  volatile unsigned int tiempo1, tiempo2;
  tiempo1 = 0;
  tiempo2 = 0;
  volatile unsigned int overhead = 0;

 int fd1, fd2 = 0;

 fd1 = open("/dev/contador_ciclos1", O_RDWR | O_NOCTTY | O_NONBLOCK);
 fd2 = open("/dev/contador_ciclos2", O_RDWR | O_NOCTTY | O_NONBLOCK);


    iret1 = pthread_create( &thread1, NULL     , hilo1_funcion, (void*) mensaje1);
        if (iret1!=0){
        printf("Fallo en creacion hilo 1 %d\n", iret1);
        }

    //create 8 Pthreads...
    //measurement "t1" from core 1 and "t3" from core 2:

  for(i=0;i<10;i++){
  read (fd1,buff,11);
  sscanf(buff,"%u",&tiempo1);
  }

  for(i=0;i<10;i++){
  read (fd2,buff,11);
  sscanf(buff,"%u",&tiempo3);
  }

  pthread_join( thread1, NULL);
  pthread_join( thread2, NULL);

  //measurement "t2" from core 1 and t4 from core 2:

  read (fd1,buff,11);
  sscanf(buff,"%u",&tiempo2);
  read (fd2,buff,11);
  sscanf(buff,"%u",&tiempo4);

  printf(COLOR_VERDE"Ciclos_ejecucion_core1: %u"COLOR_RESET "\n", (tiempo2 - tiempo1));
  printf(COLOR_VERDE"Ciclos_ejecucion_core2: %u"COLOR_RESET "\n", (tiempo4 - tiempo3));

This is the code of "phtread 1" that use the semaphores...

void *hilo1_funcion( void *ptr ){

    //~ printf(COLOR_MAGENTA"HILO 1"COLOR_RESET"\n");   

    int fabricados3=0; 
    int rc=0;   

    /* Mientras que no se hayan finalizado de fabricar todos los productos 3,
     *  el robot 1 funciona */
    while (fabricados3<productos3){

            rc = sem_trywait(&P3M3);

                     if (rc==-1 && errno == EAGAIN) {    /* no teller available */

                        //~ printf("ROBOT 1 semaforo P3M3 ocupado"COLOR_RESET"\n");      

                     }

           if (rc==0){
                fabricados3++;
                //~ printf(COLOR_CYAN"          PRODUCTO TIPO 3 FINALIZADO - Cantidad de productos tipo 3 fabricados: %d"COLOR_RESET"\n", fabricados3);
                sem_post(&P3R1);
             }  


    //~ printf("YIELD ROBOT 1\n");
    pthread_yield();

    }

    printf(COLOR_ROJO"FIN ROBOT 1"COLOR_RESET"\n");         

    pthread_exit(NULL);
    return NULL;
}

This is a pseudo code of the driver "contador_ciclos1.ko", the same at contador_ciclos2.ko (

static inline void init_perfcounters (int do_reset, int enable_divider)
{
  // in general enable all counters (including cycle counter)
  static int value = 1;

  // peform reset:
  if (do_reset)
  {
    value |= 2;     // reset all counters to zero.
    value |= 4;     // reset cycle counter to zero.
  }

  if (enable_divider)
    value |= 8;     // enable "by 64" divider for CCNT.

  value |= 16;

  // program the performance-counter control-register:
  asm volatile ("MCR p15, 0, %0, c9, c12, 0\t\n" :: "r"(value));

  // enable all counters:
  asm volatile ("MCR p15, 0, %0, c9, c12, 1\t\n" :: "r"(0x8000000f));

  // clear overflows:
  asm volatile ("MCR p15, 0, %0, c9, c12, 3\t\n" :: "r"(0x8000000f));
}
static inline unsigned int get_cyclecount (void)
{
  volatile unsigned int value;
  asm volatile ("MRC p15, 0, %0, c9, c13, 0\t\n": "=r"(value));
  return value;
}


int init_module(void)
{
    init_perfcounters(1,0);
    Major = register_chrdev(0, DEVICE_NAME, &fops);
}

static ssize_t device_read(struct file *filp,   
               char *buffer,    
               size_t length,
               loff_t * offset)
{

        volatile unsigned int counter;
        long bytes_read;
        char msg[BUF_LEN];

        counter = get_cyclecount();

        sprintf(msg, "%u", counter);
        bytes_read = copy_to_user(buffer,msg,12);
        if (bytes_read) printk(KERN_ALERT "error en contador.\n");
        return bytes_read;

}
c
linux
pthreads
arm
performance
asked on Stack Overflow Nov 12, 2013 by eduardosufan • edited Nov 13, 2013 by eduardosufan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0