|
| 1 | +/** |
| 2 | + * intel_pstate.c - automatically control turbo frequency for MacBook Pro |
| 3 | + * Copyright (C) 2021 Gokturk Yuksek <gokturk@gentoo.org> |
| 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 | + */ |
| 16 | + |
| 17 | +#include <stdio.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include <string.h> |
| 20 | +#include <unistd.h> |
| 21 | +#include <sys/types.h> |
| 22 | +#include <dirent.h> |
| 23 | +#include <errno.h> |
| 24 | + |
| 25 | +#include "intel_pstate.h" |
| 26 | +#include "util.h" |
| 27 | +#include <syslog.h> |
| 28 | + |
| 29 | +static int read_int(FILE *fp, int *val) |
| 30 | +{ |
| 31 | + char buf[4]; /* Maximum we can read in this module is '100\0' */ |
| 32 | + ssize_t ret; |
| 33 | + |
| 34 | + ret = pread(fileno(fp), buf, sizeof(buf), 0); |
| 35 | + if (ret >= 0) { |
| 36 | + buf[ret] = '\0'; |
| 37 | + *val = atoi(buf); |
| 38 | + return 0; |
| 39 | + } else { |
| 40 | + return (int)ret; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +static inline int write_str(FILE *fp, char *str, int len) |
| 45 | +{ |
| 46 | + ssize_t ret; |
| 47 | + ret = (int)pwrite(fileno(fp), str, len, 0); |
| 48 | + if (ret == len) |
| 49 | + return 0; |
| 50 | + else |
| 51 | + return -1; |
| 52 | +} |
| 53 | + |
| 54 | +static int write_int(FILE *fp, int val) |
| 55 | +{ |
| 56 | + char buf[4]; /* Maximum we can read in this module is '100\0' */ |
| 57 | + int ret; |
| 58 | + |
| 59 | + ret = snprintf(buf, sizeof(buf), "%d", val); |
| 60 | + if (ret < 0) |
| 61 | + return ret; |
| 62 | + |
| 63 | + return write_str(fp, buf, ret); |
| 64 | +} |
| 65 | + |
| 66 | +int intel_pstate_init(t_intel_pstate *intel_pstate) |
| 67 | +{ |
| 68 | + const char *path_max_perf_pct = INTEL_PT_PATH "/max_perf_pct"; |
| 69 | + const char *path_min_perf_pct = INTEL_PT_PATH "/min_perf_pct"; |
| 70 | + FILE *fp; |
| 71 | + int err; |
| 72 | + |
| 73 | + fp = fopen(path_max_perf_pct, "w+"); |
| 74 | + if (!fp) |
| 75 | + return -1; |
| 76 | + /* Save the initial value of max_perf_pct, to restore it on exit */ |
| 77 | + err = read_int(fp, &intel_pstate->preserved_max_perf_pct); |
| 78 | + if (err) |
| 79 | + return err; |
| 80 | + /* Start with the maximum performance percentage at 100% */ |
| 81 | + err = write_str(fp, "100", 4); |
| 82 | + if (err) |
| 83 | + return err; |
| 84 | + intel_pstate->f_max_perf_pct = fp; |
| 85 | + |
| 86 | + fp = fopen(path_min_perf_pct, "w+"); |
| 87 | + if (!fp) |
| 88 | + return -1; |
| 89 | + /* Save the initial value of min_perf_pct, to restore it on exit */ |
| 90 | + err = read_int(fp, &intel_pstate->preserved_min_perf_pct); |
| 91 | + if (err) |
| 92 | + return err; |
| 93 | + /* Set the minimum performance percentage to 0 */ |
| 94 | + err = write_str(fp, "0", 2); |
| 95 | + if (err) |
| 96 | + return err; |
| 97 | + intel_pstate->f_min_perf_pct = fp; |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +int intel_pstate_is_available(void) |
| 103 | +{ |
| 104 | + DIR* dir = opendir(INTEL_PT_PATH); |
| 105 | + |
| 106 | + if ((!dir) && ENOENT == errno) |
| 107 | + return 0; |
| 108 | + |
| 109 | + closedir(dir); |
| 110 | + return 1; |
| 111 | +} |
| 112 | + |
| 113 | +int intel_pstate_adjust(t_intel_pstate *intel_pstate, int step) |
| 114 | +{ |
| 115 | + int val; |
| 116 | + int err; |
| 117 | + |
| 118 | + if (!intel_pstate) |
| 119 | + return 1; |
| 120 | + |
| 121 | + err = read_int(intel_pstate->f_max_perf_pct, &val); |
| 122 | + if (err) |
| 123 | + return err; |
| 124 | + |
| 125 | + mbp_log(LOG_INFO, "Adjusting intel_pstate: val: %d, step: %d", val, step); |
| 126 | + |
| 127 | + val += step; |
| 128 | + if (val < 0) |
| 129 | + val = 0; |
| 130 | + if (val > 100) |
| 131 | + val = 100; |
| 132 | + |
| 133 | + return write_int(intel_pstate->f_max_perf_pct, val); |
| 134 | +} |
| 135 | + |
| 136 | +void intel_pstate_exit(t_intel_pstate *intel_pstate) |
| 137 | +{ |
| 138 | + if (!intel_pstate) |
| 139 | + return; |
| 140 | + |
| 141 | + (void)write_int(intel_pstate->f_max_perf_pct, intel_pstate->preserved_max_perf_pct); |
| 142 | + (void)write_int(intel_pstate->f_min_perf_pct, intel_pstate->preserved_min_perf_pct); |
| 143 | + |
| 144 | + fclose(intel_pstate->f_max_perf_pct); |
| 145 | + fclose(intel_pstate->f_min_perf_pct); |
| 146 | + |
| 147 | + memset(intel_pstate, 0, sizeof(*intel_pstate)); |
| 148 | +} |
0 commit comments