/*
 *  rqlsamp  by Davide Libenzi ( linux kernel scheduler run queue length sampler )
 *  Version 0.11 - Copyright (C) 2001  Davide Libenzi
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Davide Libenzi <davidel@xmailserver.org>
 *
 *
 *  The purpose of this tool is to measure the scheduler run queue length using
 *  the Balanced Multi Queue Scheduler ( BMQS ) patch plus the  rqlsamp  patch.
 *  Build:
 *
 *  gcc -o rqlsamp rqlsamp.c
 *
 *  Use:
 *
 *  rqlsamp [--sample-mstime ms] [--test-stime s] [-- cmdpath [arg] ...]
 *
 *  --sample-mstime = Set the sample time in milliseconds
 *  --test-stime    = Set the test time in seconds ( default == 0, continue )
 *  --              = Separate the optional command to be executed during the test time
 *  cmdpath         = Command to be executed
 *  arg             = Command argouments
 *
 *  The output is :
 *
 *  CPU0RQL CPU1RQL ... GRTRQL
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <asm/page.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/rqlsamp.h>



#define MAX_CPUS			64
#define STD_SLEEP_TIME		250

static int samples[MAX_CPUS + 1][2];


int main(int argc, char *argv[]) {
	int ii, rqlfd, icmd = -1, retcpus;
	unsigned long testtime = 0, sleeptime = STD_SLEEP_TIME, runtime = 0;
	pid_t expid = -1;
	char const * rqlfile = "/dev/rqlsamp";

	for (ii = 1; ii < argc; ii++) {
		if (strcmp(argv[ii], "--sample-mstime") == 0) {
			if (++ii < argc)
				sleeptime = (unsigned long) atol(argv[ii]);
			continue;
		}
		if (strcmp(argv[ii], "--test-stime") == 0) {
			if (++ii < argc)
				testtime = (unsigned long) atol(argv[ii]) * 1000;
			continue;
		}
		if (strcmp(argv[ii], "--") == 0) {
			icmd = ++ii;
			break;
		}
	}
	if ((rqlfd = open(rqlfile, O_RDONLY)) == -1) {
		perror(rqlfile);
		return 1;
	}
	if (icmd > 0 && icmd < argc) {
		expid = fork();
		if (expid == -1) {
			perror("fork");
			close(rqlfd);
			return 5;
		} else if (expid == 0) {
			setpgid(0, getpid());
			execv(argv[icmd], &argv[icmd]);
			exit(0);
		}
	}
	for (; testtime == 0 || runtime < testtime; runtime += sleeptime) {
		usleep(sleeptime * 1000);
		retcpus = read(rqlfd, samples, sizeof(samples));
		if (retcpus <= 0) {
			perror(rqlfile);
			break;
		}
		retcpus /= 2 * sizeof(int);
		for (ii = 0; ii < retcpus; ii++) {
			fprintf(stdout, "%d ", samples[ii][1]);
		}
		fprintf(stdout, "\n");
	}
	if (expid > 0 && kill(-expid, SIGKILL))
		perror("SIGKILL");
	close(rqlfd);
	return 0;

}

