L4Re - L4 Runtime Environment
examples/sys/migrate/thread_migrate.cc

Thread migration example.

/*
* (c) 2008-2009 Author(s)
* economic rights: Technische Universit├Ąt Dresden (Germany)
*
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
*/
#include <l4/sys/scheduler>
#include <l4/re/env>
#include <pthread-l4.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
enum { NR_THREADS = 12 };
static L4::Cap<L4::Thread> threads[NR_THREADS];
static l4_umword_t cpu_map, cpu_nrs;
/* Function for the threads. The content is not really relevant, so lets
* just sleep around a bit. */
static void *thread_fn(void *)
{
while (1)
sleep(1);
return 0;
}
/* Check how many CPUs we have available.
*/
static int check_cpus(void)
{
if (l4_error(L4Re::Env::env()->scheduler()->info(&cpu_nrs, &cs)) < 0)
return 1;
cpu_map = cs.map;
printf("%ld maximal supported CPUs.\n", cpu_nrs);
if (cpu_nrs >= L4_MWORD_BITS)
{
printf("Will only handle %ld CPUs.\n", cpu_nrs);
cpu_nrs = L4_MWORD_BITS;
}
else if (cpu_nrs == 1)
printf("Only found 1 CPU.\n");
return cpu_nrs < 2;
}
/* Create a couple of threads and store their capabilities in an array */
static int create_threads(void)
{
unsigned i;
for (i = 0; i < NR_THREADS; ++i)
{
pthread_t t;
if (pthread_create(&t, NULL, thread_fn, NULL))
return 1;
threads[i] = L4::Cap<L4::Thread>(pthread_l4_cap(t));
}
printf("Created %d threads.\n", NR_THREADS);
return 0;
}
/* Helper function to get the next CPU */
static unsigned get_next_cpu(unsigned c)
{
unsigned x = c;
for (;;)
{
x = (x + 1) % cpu_nrs;
if (L4Re::Env::env()->scheduler()->is_online(x))
return x;
if (x == c)
return c;
}
}
/* Function that shuffles the threads on the available CPUs */
static void shuffle(void)
{
unsigned start = 0;
while (1)
{
unsigned t;
unsigned c = start;
for (t = 0; t < NR_THREADS; ++t)
{
c = get_next_cpu(c);
if (l4_error(L4Re::Env::env()->scheduler()->run_thread(threads[t], sp)))
printf("Error migrating thread%02d to CPU%02d\n", t, c);
printf("Migrated Thread%02d -> CPU%02d\n", t, c);
}
start++;
if (start == cpu_nrs)
start = 0;
sleep(1);
}
}
int main(void)
{
if (check_cpus())
return 1;
if (create_threads())
return 1;
shuffle();
return 0;
}