L4Re Operating System Framework – Interface and Usage Documentation
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 <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;
}
L4::Cap< L4::Scheduler > scheduler() const noexcept
Get the scheduler capability for the task.
Definition env:282
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:103
C++ interface for capabilities.
Definition capability.h:222
Environment interface.
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:51
#define L4_MWORD_BITS
Size of machine words in bits.
Definition l4int.h:33
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:636
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:272
l4_sched_param_t l4_sched_param(unsigned prio, l4_umword_t quantum=0) L4_NOTHROW
Construct scheduler parameter.
Definition scheduler.h:282
Scheduler object functions.
String.
l4_umword_t map
Bitmap of CPUs.
Definition scheduler.h:88
Scheduler parameter set.
Definition scheduler.h:180
l4_sched_cpu_set_t affinity
CPU affinity.
Definition scheduler.h:181
Capability allocator.