aboutsummaryrefslogtreecommitdiff
path: root/pagesize.c
blob: c67d296d05c18b79bc8d3a41c12cbedd0c4557ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************
 *   User front end for using huge pages Copyright (C) 2008, IBM           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the Lesser GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.                   *
 *                                                                         *
 *   You should have received a copy of the Lesser 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.             *
 ***************************************************************************/

/*
 * pagesize exposes the available and hardware supported page sizes on
 * the system.
 *
 * This program should be treated as an ABI for using libhugetlbfs.
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>

#define _GNU_SOURCE /* for getopt_long */
#include <unistd.h>
#include <getopt.h>

#define REPORT_UTIL "pagesize"
#include "libhugetlbfs_internal.h"
#include "hugetlbfs.h"

extern int errno;
extern int optind;
extern char *optarg;

#define OPTION(opts, text)	fprintf(stderr, " %-25s  %s\n", opts, text)
#define CONT(text) 		fprintf(stderr, " %-25s  %s\n", "", text)

void print_usage()
{
	fprintf(stderr, "hugectl [options] target\n");
	fprintf(stderr, "options:\n");

	OPTION("--help, -h", "Prints this message");

	OPTION("--all, -a", "show all supported page sizes");
	OPTION("--huge-only, -H", "show only huge page sizes");
}

static int cmpsizes(const void *p1, const void *p2)
{
	return *((long *)p1) > *((long *)p2);
}

#define MAX_PAGESIZES 32

int main(int argc, char** argv)
{
	int opt_all = 0;
	int opt_huge = 0;

	char opts[] = "+haH";
	int ret = 0, index = 0;
	struct option long_opts[] = {
		{"all",       no_argument, NULL, 'a'},
		{"huge-only", no_argument, NULL, 'H'},

		{0},
	};

	long pagesizes[MAX_PAGESIZES];
	int i;

	hugetlbfs_setup_debug();

	while (ret != -1) {
		ret = getopt_long(argc, argv, opts, long_opts, &index);
		switch (ret) {
		case '?':
			print_usage();
			exit(EXIT_FAILURE);

		case 'h':
			print_usage();
			exit(EXIT_SUCCESS);

		case 'a':
			opt_all = 1;
			INFO("selecting all page sizes\n");
			break;

		case 'H':
			opt_huge = 1;
			opt_all = 1;
			INFO("selecting only huge page sizes\n");
			break;

		case -1:
			break;

		default:
			WARNING("unparsed option %08x\n", ret);
			ret = -1;
			break;
		}
	}
	index = optind;
	if ((argc - index) != 0) {
		print_usage();
		exit(EXIT_FAILURE);
	}

	if (!opt_all) {
		pagesizes[0] = sysconf(_SC_PAGESIZE);
		ret = 1;
	} else if (opt_huge)
		ret = gethugepagesizes(pagesizes, MAX_PAGESIZES);
	else
		ret = getpagesizes(pagesizes, MAX_PAGESIZES);
	if (ret < 0) {
		ERROR("failed to get list of supported page sizes\n");
		exit(EXIT_FAILURE);
	}

	qsort(pagesizes, ret, sizeof(long), cmpsizes);
	for (i = 0; i < ret; i++) {
		printf("%ld\n", pagesizes[i]);
	}

	exit(EXIT_SUCCESS);
}