Skip to content

Instantly share code, notes, and snippets.

@nbareil
Created October 17, 2011 13:07
Show Gist options
  • Save nbareil/1292564 to your computer and use it in GitHub Desktop.
Save nbareil/1292564 to your computer and use it in GitHub Desktop.
Checking scheduler behavior
/*
gcc -O0 scheduler.c -o scheduler-read -DREAD
or
gcc -O0 scheduler.c -o scheduler-read -DYIELD
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#define NCHILDS 8
#define SZ 100*4096
void uselesschild(int n, int useless)
{
char *s = malloc(SZ);
unsigned int i = 0;
if (!s)
{
perror("malloc() failed\n");
exit(1);
}
memset(s, 0, SZ);
while (i < SZ-1)
{
s[i++] = 'A' + n;
putchar('A' + n);
fflush(stdout);
if (useless)
#ifdef READ
read(0, 0, 0);
#endif
#ifdef YIELD
sched_yield();
#endif
}
s[SZ-1] = '\x00';
}
int main(void)
{
pid_t child[NCHILDS], pid;
unsigned int i;
cpu_set_t cpus;
CPU_ZERO(&cpus);
CPU_SET(0, &cpus);
sched_setaffinity(0, sizeof cpus, &cpus);
for (i = 0 ; i < NCHILDS/2; i++)
{
child[i] = pid = fork();
if (pid == -1)
{
perror("fork failed");
exit(1);
}
if (pid == 0)
{
uselesschild(i, i %2);
exit(0);
}
}
while (waitpid(-1, &i, 0) != -1);
}
/* Local Variables: */
/* c-default-style: k&r */
/* End: */
#! /usr/bin/env python
'''
$ ./bench-scheduler-yield | python grapher.py
$ geeqie out.png
'''
import fileinput
import random
from PIL import Image, ImageDraw
img = Image.new('RGBA', (12000, 100))
draw = ImageDraw.Draw(img)
x=0
y=0
height=100
prev='0'
occur=0
colors={}
for line in fileinput.input():
for c in line:
if c != prev:
width = 1 + ((occur+1) / 1200) * 100
occur=0
colors[c] = colors.get(c, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
draw.rectangle([x, y, x+width, y+height], fill=colors[c])
x += width
else:
occur += 1
prev = c
img.save('out.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment