/* * A program that randomly runs between user and kernel space. * It writes to /dev/delay to do a busy loop in kernel. * * Copyright (C) 2001 MontaVista Software Inc. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * */ #include #include #include #include #include #include #include /* * CONFIGURE */ #define PER_KERNEL 100 /* 20% in kernel */ #define MAX_SYSCALL 1024 /* max syscall duration */ main() { unsigned long period; int fd = open("/dev/delay", O_WRONLY); if (fd < 0) { printf("failed to open /dev/delay\n"); printf("Did you mknod /dev/delay and inmod delay.o?\n"); exit(-1); } { /* set random seed */ struct timeval curr; gettimeofday(&curr, 0); srand(curr.tv_usec); } printf("do_delay setup : %d%% in userland, %d%% in kernel\n", 100 - PER_KERNEL, PER_KERNEL); printf("\tsyscall duration is randomly distributed between 0 to %d usecs.\n", MAX_SYSCALL); printf("Now loop forever ...\n"); for(;;) { int delay; int ret; delay= rand() % MAX_SYSCALL; ret = write(fd, &delay, sizeof(delay)); if (ret != sizeof(delay)) printf("write failed. %d\n", ret); delay= (rand() % MAX_SYSCALL) * (100 - PER_KERNEL) / PER_KERNEL; if (delay) { usleep(delay); } } }