/**
 * gcc -Wall -fPIC -shared -O2 -o libgetcpu.so sched_getcpu.c
 * LD_PRELOAD=./libgetcpu.so ./test.sh
 *
 */
#include <stdio.h>
#include <sched.h>
#include <stdbool.h>
#include <pthread.h>

#define _GNU_SOURCE

#include <stdint.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/syscall.h>
#include <sys/types.h>

int sched_getcpu(void)
{
	int cpu_id;

	asm volatile("mrs %0, S3_3_c15_c3_1" : "=r" (cpu_id));

	return cpu_id;
}

int main(int argc, char *argv[])
{
	return printf("cpu %3d\n", sched_getcpu());
}
