/*
 * DF-2675 pinread: pin self to <cpu> then pread <nbytes> at <offset> from
 * <file> (one controlled buffer-cache allocation on a chosen cpu).
 */
#include <sys/types.h>
#include <sys/usched.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
	int fd, cpu;
	off_t off;
	size_t len;
	char *buf;

	if (argc != 5) {
		fprintf(stderr, "usage: pinread cpu file offset len\n");
		return 1;
	}
	cpu = atoi(argv[1]);
	off = atoll(argv[3]);
	len = atol(argv[4]);
	{ int pin_cpu = cpu;
	if (usched_set(0, USCHED_SET_CPU, &pin_cpu, sizeof(pin_cpu)) < 0)
		err(1, "usched_set"); }
	fd = open(argv[2], O_RDONLY);
	if (fd < 0) err(1, "open");
	buf = malloc(len);
	if (!buf) err(1, "malloc");
	if (pread(fd, buf, len, off) != (ssize_t)len)
		err(1, "pread");
	printf("PINREAD cpu=%d %s off=%lld len=%lu first=%02x\n",
	       cpu, argv[2], (long long)off, (unsigned long)len,
	       (unsigned char)buf[0]);
	return 0;
}
