Skip to content

Commit

Permalink
Add configuration option for triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsunyan committed Jul 6, 2019
1 parent ac5ae9a commit ea0e74c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ independently, but only one rule can be declared in this case.
### Applying Configuration

Run `intel-undervolt read` to read current values and `intel-undervolt apply` to apply configured
values. You can apply your configuration automatically enabling `intel-undervolt` service.
values.

You can apply your configuration automatically enabling `intel-undervolt` service. Elogind
users should pass `yes` to `enable` option in `intel-undervolt.conf`.

### Measuring the Power Consumption

Expand Down
15 changes: 14 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ struct config_t * load_config(struct config_t * old_config, bool * nl) {
config->fd_msr = -1;
config->fd_mem = -1;
}
config->enable = false;
config->undervolts = NULL;
for (i = 0; i < ARRAY_SIZE(config->power); i++) {
config->power[i].apply = false;
Expand All @@ -347,6 +348,7 @@ struct config_t * load_config(struct config_t * old_config, bool * nl) {
sprintf(fdarg, "%d", fd[1]);
execlp("/bin/sh", "/bin/sh", "-c", "readonly fd=$1;"
"pz() { printf '%s\\0' \"$@\" >&$fd; };"
"enable() { pz enable \"$1\"; };"
"apply() { pz apply; pz undervolt \"$1\" \"$2\" \"$3\"; };"
"undervolt() { pz undervolt \"$1\" \"$2\" \"$3\"; };"
"tdp() { pz tdp; pz power package \"$1\" \"$2\"; };"
Expand Down Expand Up @@ -390,7 +392,18 @@ struct config_t * load_config(struct config_t * old_config, bool * nl) {
#define iuv_read_line_error() iuv_read_line_error_action({})

while (iuv_read_line()) {
if (!strcmp(line, "undervolt")) {
if (!strcmp(line, "enable")) {
bool enable;
iuv_read_line_error();
if (!strcmp(line, "yes")) {
enable = true;
} else if (!strcmp(line, "no")) {
enable = false;
} else {
iuv_print_break("Invalid value: %s\n", line);
}
config->enable = enable;
} else if (!strcmp(line, "undervolt")) {
int index;
int len;
char * title;
Expand Down
1 change: 1 addition & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct daemon_action_t {
struct config_t {
int fd_msr;
int fd_mem;
bool enable;
struct array_t * undervolts;
struct power_limit_t power[ARRAY_SIZE(power_domains)];
bool tjoffset_apply;
Expand Down
5 changes: 5 additions & 0 deletions intel-undervolt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Enable or Disable Triggers (elogind)
# Usage: enable [yes/no]

enable no

# CPU Undervolting
# Usage: undervolt ${index} ${display_name} ${undervolt_value}
# Example: undervolt 2 'CPU Cache' -25.84
Expand Down
2 changes: 1 addition & 1 deletion intel-undervolt.elogind
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
if [ "$1" = 'post' ]; then
intel-undervolt apply
intel-undervolt apply --trigger
fi
14 changes: 10 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ static bool handle_arg(struct arg_t * arg, const char * value, bool * consume_ne
return false;
}
arg->value = value;
if (arg->mode == ARG_MODE_FLOAT) {
if (arg->mode == ARG_MODE_EMPTY) {
arg->float_value = 1;
} else if (arg->mode == ARG_MODE_FLOAT) {
char * tmp = NULL;
arg->float_value = strtof(value, &tmp);
if (tmp && (tmp == value || tmp[0])) {
Expand Down Expand Up @@ -171,10 +173,14 @@ static bool arg_check_measure_sleep(struct arg_t * arg) {
int main(int argc, char ** argv) {
if (argc >= 2 && !strcmp(argv[1], "read")) {
return parse_args(argc - 2, &argv[2], NULL) &&
read_apply_mode(false) ? 0 : 1;
read_apply_mode(false, false) ? 0 : 1;
} else if (argc >= 2 && !strcmp(argv[1], "apply")) {
return parse_args(argc - 2, &argv[2], NULL) &&
read_apply_mode(true) ? 0 : 1;
struct arg_t args[2] = {
ARG_EMPTY('t', "trigger", NULL),
ARG_END
};
return parse_args(argc - 2, &argv[2], args) &&
read_apply_mode(true, arg(args, "trigger")->present) ? 0 : 1;
} else if (argc >= 2 && !strcmp(argv[1], "measure")) {
struct arg_t args[3] = {
ARG_STRING('f', "format", arg_check_measure_format, "terminal"),
Expand Down
21 changes: 13 additions & 8 deletions modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@
#include <string.h>
#include <unistd.h>

bool read_apply_mode(bool write) {
bool read_apply_mode(bool write, bool trigger) {
bool nl = false;
struct config_t * config = load_config(NULL, &nl);
bool success = true;
unsigned int i;

if (config) {
success &= undervolt(config, &nl, write);
for (i = 0; i < ARRAY_SIZE(config->power); i++) {
success &= power_limit(config, i, &nl, write);
}
success &= tjoffset(config, &nl, write);
if (trigger && !config->enable) {
fprintf(stderr, "Triggers are disabled\n");
return false;
} else {
success &= undervolt(config, &nl, write);
for (i = 0; i < ARRAY_SIZE(config->power); i++) {
success &= power_limit(config, i, &nl, write);
}
success &= tjoffset(config, &nl, write);

free_config(config);
return success;
free_config(config);
return success;
}
} else {
fprintf(stderr, "Failed to setup the program\n");
return false;
Expand Down
2 changes: 1 addition & 1 deletion modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdbool.h>

bool read_apply_mode(bool write);
bool read_apply_mode(bool write, bool trigger);
int daemon_mode();

#endif

0 comments on commit ea0e74c

Please sign in to comment.