Solved execute function in all cpus

alemoppo

New Member

Reaction score: 1
Messages: 19

Hello,

Excuse me if I'm "abusing" the forum but I can't find a solution.

I want to run a single function in all the CPUs.
I tried with:
C:
static void print()
{
    uprintf("hello world from %d cpu!\r\n", PCPU_GET(cpuid));
}

/* --- */

//call "print" function in all CPUs
smp_rendezvous(NULL, print, NULL, NULL);

But i see only one (randomly) of:
"hello world from 0 cpu!"
"hello world from 1 cpu!"
"hello world from 2 cpu!"

I expected to see it 3 times with different number.

What's wrong?

C:
#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h>  /* uprintf */
#include <sys/errno.h>
#include <sys/param.h>  /* defines used in kernel.h */
#include <sys/kernel.h> /* types used in module initialization */
#include <sys/smp.h>

static void print()
{
    uprintf("hello world from %d cpu!\r\n", PCPU_GET(cpuid));
}

static int
skel_loader(struct module *m, int what, void *arg)
{
  int err = 0;

  switch (what) {
  case MOD_LOAD:                /* kldload */
 
    smp_rendezvous(NULL, print, NULL, NULL);
 
    uprintf("Skeleton KLD loaded.\n");
    break;
  case MOD_UNLOAD:
    uprintf("Skeleton KLD unloaded.\n");
    break;
  default:
    err = EOPNOTSUPP;
    break;
  }
  return(err);
}

/* Declare this module to the rest of the kernel */

static moduledata_t skel_mod = {
  "skel",
  skel_loader,
  NULL
};

DECLARE_MODULE(skeleton, skel_mod, SI_SUB_KLD, SI_ORDER_ANY);

Thanks.
 
OP
alemoppo

alemoppo

New Member

Reaction score: 1
Messages: 19

I assume uprintf doesn't fulfill these requirements.
Oh, thank you, it can be! Did you have some idea how to verify (have an output in any mode) that i'm running in all CPUs?
I thought about incrementing a variable, but maybe there is a conflict because everyone is writing and reading in the same area.

Thanks.
 

Zirias

Daemon

Reaction score: 1,416
Messages: 2,474

Did you have some idea how to verify (have an output in any mode) that i'm running in all CPUs?
maybe: define static buffers, write content there, output later? Or, more simple, increment a byte at offset cpuid in a char array and check afterwards?
volatile will be needed…
 
OP
alemoppo

alemoppo

New Member

Reaction score: 1
Messages: 19

That's correct!

C:
static unsigned char a = 0,b = 0,c = 0, error = 0;

static void print()
{
    //uprintf("hello world from %d cpu!\r\n", PCPU_GET(cpuid));
    switch(PCPU_GET(cpuid))
    {
        case 0:
        a++;
        break;
        case 1:
        b++;
        break;
        case 2:
        c++;
        break;
        default:
        error++;
        break;
    }
}

This sets
a: 1, b: 1, c: 1: error: 0

Thank you!
 
Top