Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

Feb 18-20, 2025
Register
Loading...
Note: File does not exist in v3.15.
  1#include <fcntl.h>
  2#include <stdio.h>
  3#include <stdlib.h>
  4#include <unistd.h>
  5
  6#include <sys/ioctl.h>
  7#include <sys/mman.h>
  8#include <sys/prctl.h>
  9#include <sys/stat.h>
 10#include <sys/types.h>
 11
 12#include <linux/types.h>
 13
 14#define MB (1UL << 20)
 15#define PAGE_SIZE sysconf(_SC_PAGESIZE)
 16
 17#define GUP_FAST_BENCHMARK	_IOWR('g', 1, struct gup_benchmark)
 18#define GUP_LONGTERM_BENCHMARK	_IOWR('g', 2, struct gup_benchmark)
 19#define GUP_BENCHMARK		_IOWR('g', 3, struct gup_benchmark)
 20
 21struct gup_benchmark {
 22	__u64 get_delta_usec;
 23	__u64 put_delta_usec;
 24	__u64 addr;
 25	__u64 size;
 26	__u32 nr_pages_per_call;
 27	__u32 flags;
 28	__u64 expansion[10];	/* For future use */
 29};
 30
 31int main(int argc, char **argv)
 32{
 33	struct gup_benchmark gup;
 34	unsigned long size = 128 * MB;
 35	int i, fd, filed, opt, nr_pages = 1, thp = -1, repeats = 1, write = 0;
 36	int cmd = GUP_FAST_BENCHMARK, flags = MAP_PRIVATE;
 37	char *file = "/dev/zero";
 38	char *p;
 39
 40	while ((opt = getopt(argc, argv, "m:r:n:f:tTLUwSH")) != -1) {
 41		switch (opt) {
 42		case 'm':
 43			size = atoi(optarg) * MB;
 44			break;
 45		case 'r':
 46			repeats = atoi(optarg);
 47			break;
 48		case 'n':
 49			nr_pages = atoi(optarg);
 50			break;
 51		case 't':
 52			thp = 1;
 53			break;
 54		case 'T':
 55			thp = 0;
 56			break;
 57		case 'L':
 58			cmd = GUP_LONGTERM_BENCHMARK;
 59			break;
 60		case 'U':
 61			cmd = GUP_BENCHMARK;
 62			break;
 63		case 'w':
 64			write = 1;
 65			break;
 66		case 'f':
 67			file = optarg;
 68			break;
 69		case 'S':
 70			flags &= ~MAP_PRIVATE;
 71			flags |= MAP_SHARED;
 72			break;
 73		case 'H':
 74			flags |= (MAP_HUGETLB | MAP_ANONYMOUS);
 75			break;
 76		default:
 77			return -1;
 78		}
 79	}
 80
 81	filed = open(file, O_RDWR|O_CREAT);
 82	if (filed < 0) {
 83		perror("open");
 84		exit(filed);
 85	}
 86
 87	gup.nr_pages_per_call = nr_pages;
 88	gup.flags = write;
 89
 90	fd = open("/sys/kernel/debug/gup_benchmark", O_RDWR);
 91	if (fd == -1)
 92		perror("open"), exit(1);
 93
 94	p = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, filed, 0);
 95	if (p == MAP_FAILED)
 96		perror("mmap"), exit(1);
 97	gup.addr = (unsigned long)p;
 98
 99	if (thp == 1)
100		madvise(p, size, MADV_HUGEPAGE);
101	else if (thp == 0)
102		madvise(p, size, MADV_NOHUGEPAGE);
103
104	for (; (unsigned long)p < gup.addr + size; p += PAGE_SIZE)
105		p[0] = 0;
106
107	for (i = 0; i < repeats; i++) {
108		gup.size = size;
109		if (ioctl(fd, cmd, &gup))
110			perror("ioctl"), exit(1);
111
112		printf("Time: get:%lld put:%lld us", gup.get_delta_usec,
113			gup.put_delta_usec);
114		if (gup.size != size)
115			printf(", truncated (size: %lld)", gup.size);
116		printf("\n");
117	}
118
119	return 0;
120}