Debian Codegraph @

pagesize.c

raw symbols SCIP
1 /***************************************************************************
2 * User front end for using huge pages Copyright (C) 2008, IBM *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the Lesser GNU General Public License as *
6 * published by the Free Software Foundation; either version 2.1 of the *
7 * License, or at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the Lesser GNU General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 /*
21 * pagesize exposes the available and hardware supported page sizes on
22 * the system.
23 *
24 * This program should be treated as an ABI for using libhugetlbfs.
25 */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <limits.h>
32
33 #define _GNU_SOURCE /* for getopt_long */
34 #include <unistd.h>
35 #include <getopt.h>
36
37 #define REPORT_UTIL "pagesize"
38 #include "libhugetlbfs_internal.h"
39 #include "hugetlbfs.h"
40
41 extern int errno;
42 extern int optind;
43 extern char *optarg;
44
45 #define OPTION(opts, text) fprintf(stderr, " %-25s %s\n", opts, text)
46 #define CONT(text) fprintf(stderr, " %-25s %s\n", "", text)
47
48 void print_usage()
49 {
50 fprintf(stderr, "pagesize [options] target\n");
51 fprintf(stderr, "options:\n");
52
53 OPTION("--help, -h", "Prints this message");
54
55 OPTION("--all, -a", "show all supported page sizes");
56 OPTION("--huge-only, -H", "show only huge page sizes");
57 }
58
59 static int cmpsizes(const void *p1, const void *p2)
60 {
61 return *((long *)p1) > *((long *)p2);
62 }
63
64 #define MAX_PAGESIZES 32
65
66 int main(int argc, char** argv)
67 {
68 int opt_all = 0;
69 int opt_huge = 0;
70
71 char opts[] = "+haH";
72 int ret = 0, index = 0;
73 struct option long_opts[] = {
74 {"all", no_argument, NULL, 'a'},
75 {"huge-only", no_argument, NULL, 'H'},
76
77 {0},
78 };
79
80 long pagesizes[MAX_PAGESIZES];
81 int i;
82
83 hugetlbfs_setup_debug();
84
85 while (ret != -1) {
86 ret = getopt_long(argc, argv, opts, long_opts, &index);
87 switch (ret) {
88 case '?':
89 print_usage();
90 exit(EXIT_FAILURE);
91
92 case 'h':
93 print_usage();
94 exit(EXIT_SUCCESS);
95
96 case 'a':
97 opt_all = 1;
98 INFO("selecting all page sizes\n");
99 break;
100
101 case 'H':
102 opt_huge = 1;
103 opt_all = 1;
104 INFO("selecting only huge page sizes\n");
105 break;
106
107 case -1:
108 break;
109
110 default:
111 WARNING("unparsed option %08x\n", ret);
112 ret = -1;
113 break;
114 }
115 }
116 index = optind;
117 if ((argc - index) != 0) {
118 print_usage();
119 exit(EXIT_FAILURE);
120 }
121
122 if (!opt_all) {
123 pagesizes[0] = sysconf(_SC_PAGESIZE);
124 ret = 1;
125 } else if (opt_huge)
126 ret = gethugepagesizes(pagesizes, MAX_PAGESIZES);
127 else
128 ret = getpagesizes(pagesizes, MAX_PAGESIZES);
129 if (ret < 0) {
130 ERROR("failed to get list of supported page sizes\n");
131 exit(EXIT_FAILURE);
132 }
133
134 qsort(pagesizes, ret, sizeof(long), cmpsizes);
135 for (i = 0; i < ret; i++) {
136 printf("%ld\n", pagesizes[i]);
137 }
138
139 exit(EXIT_SUCCESS);
140 }