aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore17
-rw-r--r--LICENSE15
-rw-r--r--Makefile46
-rw-r--r--README41
-rw-r--r--TODO2
-rw-r--r--config.mk8
-rw-r--r--coredown.178
-rw-r--r--coreup.172
-rw-r--r--coreupdown.c77
9 files changed, 356 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e9dfb32
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+*\#*
+*~
+*.o
+*.a
+*.lo
+*.su
+*.so
+*.so.*
+*.dll
+*.dylib
+*.gch
+*.gcov
+*.gcno
+*.gcda
+/coreupdown
+/coreup
+/coredown
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..fccd785
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+ISC License
+
+© 2024 Mattias Andrée <maandree@kth.se>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..f0d383d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,46 @@
+.POSIX:
+
+CONFIGFILE = config.mk
+include $(CONFIGFILE)
+
+OBJ =\
+ coreupdown.o
+
+HDR =
+
+all: coreupdown
+$(OBJ): $(HDR)
+
+.c.o:
+ $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS)
+
+coreupdown: $(OBJ)
+ $(CC) -o $@ $(OBJ) $(LDFLAGS)
+
+install: coreupdown
+ mkdir -p -- "$(DESTDIR)$(PREFIX)/bin"
+ mkdir -p -- "$(DESTDIR)$(MANPREFIX)/man1"
+ test -f "$(DESTDIR)$(PREFIX)/bin/coreup" || test ! -e "$(DESTDIR)$(PREFIX)/bin/coreup"
+ test -f "$(DESTDIR)$(PREFIX)/bin/coredown" || test ! -e "$(DESTDIR)$(PREFIX)/bin/coredown"
+ cp -- coreupdown "$(DESTDIR)$(PREFIX)/bin/coreup"
+ ln -sf coreup "$(DESTDIR)$(PREFIX)/bin/coredown"
+ cp -- coreup.1 coredown.1 "$(DESTDIR)$(MANPREFIX)/man1"
+
+postinstall:
+ chown '0:0' "$(DESTDIR)$(PREFIX)/bin/coreup"
+ chmod '4755' "$(DESTDIR)$(PREFIX)/bin/coreup"
+
+uninstall:
+ -rm -f -- "$(DESTDIR)$(PREFIX)/bin/coreup"
+ -rm -f -- "$(DESTDIR)$(PREFIX)/bin/coredown"
+ -rm -f -- "$(DESTDIR)$(MANPREFIX)/man1/coreup.1"
+ -rm -f -- "$(DESTDIR)$(MANPREFIX)/man1/coredown.1"
+
+clean:
+ -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.gch *.gcov *.gcno *.gcda
+ -rm -f -- coreupdown
+
+.SUFFIXES:
+.SUFFIXES: .o .c
+
+.PHONY: all install uninstall clean
diff --git a/README b/README
new file mode 100644
index 0000000..bbb5a4f
--- /dev/null
+++ b/README
@@ -0,0 +1,41 @@
+MAME
+ coreup - Enable all CPU core
+ coredown - Disable all but one CPU core
+
+SYNPOSIS
+ coreup [count]
+ coredown [count]
+
+DESCRIPTION
+ The coreup utility enables all CPU cores on the machine.
+
+ The coredown utility disables all CPU cores on the machine
+ expect the CPU 0.
+
+ This is designed as a workaround for virtual machines that
+ have input performance issues (keyboard input lag and keys
+ getting stuck) when more than one CPU core is online.
+
+OPTIONS
+ No options are supported.
+
+OPERANDS
+ The following operand is supported:
+
+ count
+ Enable the first count CPU cores and disable the rest.
+
+EXIT STATUS
+ If the coreup and coredown utility fails it will exit with one
+ of the following statuses:
+
+ 0 Successful completion.
+
+ 1 An error occurred.
+
+NOTES
+ The is no difference between the coreup(1) and coredown(1)
+ utilities when the count operand is specified.
+
+SEE ALSO
+ None.
diff --git a/TODO b/TODO
new file mode 100644
index 0000000..a399993
--- /dev/null
+++ b/TODO
@@ -0,0 +1,2 @@
+Add coreupdownd to automatically run coreup and coredown when the
+machine has high or low CPU usage
diff --git a/config.mk b/config.mk
new file mode 100644
index 0000000..f1d6591
--- /dev/null
+++ b/config.mk
@@ -0,0 +1,8 @@
+PREFIX = /usr
+MANPREFIX = $(PREFIX)/share/man
+
+CC = c99
+
+CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE
+CFLAGS =
+LDFLAGS =
diff --git a/coredown.1 b/coredown.1
new file mode 100644
index 0000000..666ea46
--- /dev/null
+++ b/coredown.1
@@ -0,0 +1,78 @@
+.TH COREDOWN 1 COREDOWNDOWN
+.SH NAME
+coredown - Disable all but one CPU core
+.SH SYNPOSIS
+coredown
+.RI [ count ]
+.SH DESCRIPTION
+The
+.B coredown
+utility disables all CPU cores on the machine
+expect the CPU 0.
+.PP
+This is designed as a workaround for virtual machines
+that have input performance issues (keyboard input lag
+and keys getting stuck) when more than one CPU core is
+online.
+.SH OPTIONS
+No options are supported.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I count
+Enable the first
+.I count
+CPU cores and disable the rest.
+.SH STDIN
+The
+.B coredown
+utility does not use the standard input.
+.SH INPUT FILES
+None.
+.SH ENVIRONMENT VARIABLES
+No environment variables affects the execution of
+.B coredown
+.SH ASYNCHRONOUS EVENTS
+Default.
+.SH STDOUT
+The
+.B coredown
+utility does not use the standard output.
+.SH STDERR
+The standard error is used for diagnostic messages.
+.SH OUTPUT FILES
+None.
+.SH EXTENDED DESCRIPTION
+None.
+.SH EXIT STATUS
+If the
+.B coredown
+utility fails it will exit with one of the following statuses:
+.TP
+0
+Successful completion.
+.TP
+1
+An error occurred.
+.SH CONSEQUENCES OF ERRORS
+Default.
+.SH APPLICATION USAGE
+None.
+.SH EXAMPLES
+None.
+.SH RATIONALE
+None.
+.SH NOTES
+The is no difference between the
+.BR coreup (1)
+and
+.BR coredown (1)
+utilities when the
+.I count
+operand is specified.
+.SH BUGS
+None.
+.SH FUTURE DIRECTIONS
+None.
+.SH SEE ALSO
+.BR coreup (1)
diff --git a/coreup.1 b/coreup.1
new file mode 100644
index 0000000..1dfe99f
--- /dev/null
+++ b/coreup.1
@@ -0,0 +1,72 @@
+.TH COREUP 1 COREUPDOWN
+.SH MAME
+coreup - Enable all CPU core
+.SH SYNPOSIS
+coreup
+.RI [ count ]
+.SH DESCRIPTION
+The
+.B coreup
+utility enables all CPU cores on the machine.
+.SH OPTIONS
+No options are supported.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I count
+Enable the first
+.I count
+CPU cores and disable the rest.
+.SH STDIN
+The
+.B coreup
+utility does not use the standard input.
+.SH INPUT FILES
+None.
+.SH ENVIRONMENT VARIABLES
+No environment variables affects the execution of
+.B coreup
+.SH ASYNCHRONOUS EVENTS
+Default.
+.SH STDOUT
+The
+.B coreup
+utility does not use the standard output.
+.SH STDERR
+The standard error is used for diagnostic messages.
+.SH OUTPUT FILES
+None.
+.SH EXTENDED DESCRIPTION
+None.
+.SH EXIT STATUS
+If the
+.B coreup
+utility fails it will exit with one of the following statuses:
+.TP
+0
+Successful completion.
+.TP
+1
+An error occurred.
+.SH CONSEQUENCES OF ERRORS
+Default.
+.SH APPLICATION USAGE
+None.
+.SH EXAMPLES
+None.
+.SH RATIONALE
+None.
+.SH NOTES
+The is no difference between the
+.BR coreup (1)
+and
+.BR coredown (1)
+utilities when the
+.I count
+operand is specified.
+.SH BUGS
+None.
+.SH FUTURE DIRECTIONS
+None.
+.SH SEE ALSO
+.BR coredown (1)
diff --git a/coreupdown.c b/coreupdown.c
new file mode 100644
index 0000000..1de5007
--- /dev/null
+++ b/coreupdown.c
@@ -0,0 +1,77 @@
+/* See LICENSE file for copyright and license details. */
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ unsigned long i, count;
+ const char *command, *argv0;
+ char path[sizeof("/sys/devices/system/cpu/cpu/online") + 3 * sizeof(i)];
+ int fd, ret = 0;
+
+ if (argc == 0) {
+ wrong_command:
+ fprintf(stderr, "usage: coreup [count]\nusage: coredown [count]\n");
+ return 1;
+ }
+
+ argv0 = *argv++;
+ argc--;
+ command = strrchr(argv0, '/');
+ command = command ? &command[1] : argv0;
+
+ if (!strcmp(command, "coreup"))
+ count = ULONG_MAX;
+ else if (!strcmp(command, "coredown"))
+ count = 1;
+ else
+ goto wrong_command;
+
+ if (argc && !strcmp(argv[0], "--")) {
+ argv++;
+ argc--;
+ }
+
+ if (argc > 1) {
+ usage:
+ fprintf(stderr, "usage: %s [count]\n", argv0);
+ return 1;
+ }
+
+ if (argc >= 1) {
+ char *end;
+ errno = 0;
+ if (!isdigit(**argv))
+ goto usage;
+ count = strtoul(*argv, &end, 10);
+ if (!count || errno || *end)
+ goto usage;
+ argv++;
+ argc--;
+ }
+
+ for (i = 1; i; i++) {
+ sprintf(path, "/sys/devices/system/cpu/cpu%zu/online", i);
+ fd = open(path, O_WRONLY);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ break;
+ fprintf(stderr, "%s: open %s O_WRONLY\n", argv0, path);
+ return 1;
+ }
+ if (write(fd, i < count ? "1\n" : "0\n", 2) < 0) {
+ fprintf(stderr, "%s: write %s\n", argv0, path);
+ ret = 1;
+ }
+ close(fd);
+ }
+
+ return ret;
+}