Skip to content

Commit ae215a1

Browse files
committed
add uksmctl stuff
1 parent a0877c6 commit ae215a1

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
uksmstat/uksmstat
22
uksmstat/*.o
3+
uksmctl/uksmctl
4+
uksmctl/*.o

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
all:
22
$(MAKE) -C uksmstat
3+
$(MAKE) -C uksmctl
34

45
install:
56
$(MAKE) -C uksmstat install
7+
$(MAKE) -C uksmctl install
68

79
clean:
810
$(MAKE) -C uksmstat clean
11+
$(MAKE) -C uksmctl clean
912

uksmctl/Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
PROG = uksmctl
2+
OBJ = uksmctl.o
3+
PREFIX ?= /usr/local
4+
CC ?= cc
5+
CFLAGS ?= -O3 -std=c99 -W -Wall -pedantic -D_GNU_SOURCE
6+
LDADD ?=
7+
8+
all: build
9+
10+
build: $(PROG)
11+
12+
$(PROG): $(OBJ)
13+
$(CC) $(CFLAGS) $(OBJ) $(LDADD) -o $@
14+
15+
%.o: %.c
16+
$(CC) $(CFLAGS) -c $<
17+
18+
install:
19+
install -Dm 0755 $(PROG) $(DESTDIR)$(PREFIX)/bin/$(PROG)
20+
21+
clean:
22+
rm -f $(OBJ) $(PROG)
23+

uksmctl/uksmctl.c

+219
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
uksmctl — small tool to control UKSM
3+
Copyright © 2012 Oleksandr Natalenko aka post-factum <[email protected]>
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <unistd.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <sys/stat.h>
23+
#include <sys/types.h>
24+
#include <sysexits.h>
25+
26+
#define UKSMDIR "/sys/kernel/mm/uksm"
27+
#define UKSMRUN UKSMDIR"/run"
28+
29+
void show_help()
30+
{
31+
fprintf(stdout, "uksmctl - small tool to control UKSM statistics\n");
32+
fprintf(stdout, "© Oleksandr Natalenko aka post-factum, 2012\n");
33+
fprintf(stdout, "Distributed under terms and conditions of GPLv3+. See COPYING for details.\n");
34+
fprintf(stdout, "Usage: uksmctl <options>\n");
35+
fprintf(stdout, "Options:\n");
36+
fprintf(stdout, "\t-a: activate UKSM\n");
37+
fprintf(stdout, "\t-d: deactivate UKSM\n");
38+
fprintf(stdout, "\t-s: toggle UKSM state\n");
39+
fprintf(stdout, "\t-v: be verbose (up to -vv)\n");
40+
fprintf(stdout, "\t-h: show this help\n");
41+
exit(EX_OK);
42+
}
43+
44+
int main(int argc, char **argv)
45+
{
46+
// define vars
47+
int opts = 0, activate = 0, deactivate = 0, toggle = 0, verbose = 0;
48+
struct stat sb;
49+
FILE *f;
50+
51+
// check root privileges
52+
if (0 != getuid())
53+
{
54+
fprintf(stderr, "You have to be root in order to use uksmctl\n");
55+
exit(EX_NOPERM);
56+
}
57+
58+
// check if there's uksm
59+
if (0 != stat(UKSMDIR, &sb) && S_ISDIR(sb.st_mode))
60+
{
61+
fprintf(stderr, "Unable to find uksm interface in %s\n", UKSMDIR);
62+
exit(EX_OSFILE);
63+
}
64+
65+
// parse cmdline options
66+
while (-1 != (opts = getopt(argc, argv, "adsvh")))
67+
{
68+
switch (opts)
69+
{
70+
case 'a':
71+
activate = 1;
72+
break;
73+
case 'd':
74+
deactivate = 1;
75+
break;
76+
case 's':
77+
toggle = 1;
78+
break;
79+
case 'v':
80+
verbose++;
81+
if (verbose > 2)
82+
{
83+
fprintf(stderr, "Invalid -v switches count\n");
84+
exit(EX_USAGE);
85+
}
86+
break;
87+
case 'h':
88+
show_help();
89+
break;
90+
default:
91+
fprintf(stderr, "Unknown option: %c\n", opts);
92+
exit(EX_USAGE);
93+
break;
94+
}
95+
}
96+
97+
// activate UKSM
98+
if (1 == activate)
99+
{
100+
f = fopen(UKSMRUN, "w");
101+
if (NULL == f)
102+
{
103+
fprintf(stderr, "Unable to open run file\n");
104+
exit(EX_OSFILE);
105+
}
106+
fprintf(f, "%d", 1);
107+
fclose(f);
108+
109+
switch (verbose)
110+
{
111+
case 1:
112+
fprintf(stdout, "1\n");
113+
break;
114+
case 2:
115+
fprintf(stdout, "UKSM activated\n");
116+
}
117+
118+
exit(EX_OK);
119+
}
120+
121+
// deactivate UKSM
122+
if (1 == deactivate)
123+
{
124+
f = fopen(UKSMRUN, "w");
125+
if (NULL == f)
126+
{
127+
fprintf(stderr, "Unable to open run file\n");
128+
exit(EX_OSFILE);
129+
}
130+
fprintf(f, "%d", 0);
131+
fclose(f);
132+
133+
switch (verbose)
134+
{
135+
case 1:
136+
fprintf(stdout, "0\n");
137+
case 2:
138+
fprintf(stdout, "UKSM deactivated\n");
139+
}
140+
141+
exit(EX_OK);
142+
}
143+
144+
// toggle UKSM state
145+
if (1 == toggle)
146+
{
147+
f = fopen(UKSMRUN, "r");
148+
if (NULL == f)
149+
{
150+
fprintf(stderr, "Unable to open run file\n");
151+
exit(EX_OSFILE);
152+
}
153+
unsigned int run = 0;
154+
fscanf(f, "%d", &run);
155+
fclose(f);
156+
157+
switch (run)
158+
{
159+
case 0:
160+
run = 1;
161+
switch (verbose)
162+
{
163+
case 2:
164+
fprintf(stdout, "UKSM was inactive, activating\n");
165+
break;
166+
}
167+
break;
168+
case 1:
169+
run = 0;
170+
switch (verbose)
171+
{
172+
case 2:
173+
fprintf(stdout, "UKSM was active, deactivating\n");
174+
break;
175+
}
176+
break;
177+
}
178+
179+
f = fopen(UKSMRUN, "w");
180+
if (NULL == f)
181+
{
182+
fprintf(stderr, "Unable to open run file\n");
183+
exit(EX_OSFILE);
184+
}
185+
fprintf(f, "%d", run);
186+
fclose(f);
187+
188+
switch (run)
189+
{
190+
case 0:
191+
switch (verbose)
192+
{
193+
case 1:
194+
fprintf(stdout, "0\n");
195+
break;
196+
case 2:
197+
fprintf(stdout, "UKSM deactivated\n");
198+
break;
199+
}
200+
break;
201+
case 1:
202+
switch (verbose)
203+
{
204+
case 1:
205+
fprintf(stdout, "1\n");
206+
break;
207+
case 2:
208+
fprintf(stdout, "UKSM activated\n");
209+
break;
210+
}
211+
break;
212+
}
213+
214+
exit(EX_OK);
215+
}
216+
217+
return EX_OK;
218+
}
219+

0 commit comments

Comments
 (0)