Loading...
Searching...
No Matches
examples/sys/migrate/thread_migrate.cc

Thread migration example.

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 <l4/re/util/cap_alloc>
#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)
{
l4_sched_cpu_set_t cs = l4_sched_cpu_set(0, 0);
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)
{
l4_sched_param_t sp = l4_sched_param(20);
c = get_next_cpu(c);
sp.affinity = l4_sched_cpu_set(c, 0);
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;
}
L4Re::Env::env
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
env
Environment interface.
l4_umword_t
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
L4_MWORD_BITS
#define L4_MWORD_BITS
Size of machine words in bits.
Definition l4int.h:22
l4_error
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
Definition ipc.h:646
l4_sched_cpu_set
l4_sched_cpu_set_t l4_sched_cpu_set(l4_umword_t offset, unsigned char granularity, l4_umword_t map=1) L4_NOTHROW
Definition scheduler.h:279
l4_sched_param
l4_sched_param_t l4_sched_param(unsigned prio, l4_umword_t quantum=0) L4_NOTHROW
Construct scheduler parameter.
Definition scheduler.h:289
scheduler
Scheduler object functions.
string.h
String.
l4_sched_cpu_set_t
CPU sets.
Definition scheduler.h:59
l4_sched_cpu_set_t::map
l4_umword_t map
Bitmap of CPUs.
Definition scheduler.h:77
l4_sched_param_t
Scheduler parameter set.
Definition scheduler.h:174
l4_sched_param_t::affinity
l4_sched_cpu_set_t affinity
CPU affinity.
Definition scheduler.h:176
cap_alloc
Capability allocator.