Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <memory> |
| 19 | #include <tuple> |
| 20 | |
| 21 | #include "drive.hpp" |
| 22 | |
| 23 | #include "interfaces.hpp" |
| 24 | #include "sensors/pluggable.hpp" |
| 25 | #include "sysfs/sysfswrite.hpp" |
| 26 | #include "sysfs/sysfsread.hpp" |
| 27 | |
| 28 | using tstamp = std::chrono::high_resolution_clock::time_point; |
| 29 | |
| 30 | #define DRIVE_TIME 1 |
| 31 | #define DRIVE_GOAL 2 |
| 32 | #define DRIVE DRIVE_TIME |
| 33 | #define MAX_PWM 255 |
| 34 | |
| 35 | static std::unique_ptr<Sensor> Create( |
| 36 | std::string readpath, |
| 37 | std::string writepath) |
| 38 | { |
| 39 | return std::make_unique<PluggableSensor>( |
| 40 | readpath, |
| 41 | 0, /* default the timeout to disabled */ |
| 42 | std::make_unique<SysFsRead>(readpath), |
| 43 | std::make_unique<SysFsWrite>(writepath, 0, MAX_PWM)); |
| 44 | } |
| 45 | |
| 46 | int64_t getAverage(std::tuple<tstamp, int64_t, int64_t>& values) |
| 47 | { |
| 48 | return (std::get<1>(values) + std::get<2>(values)) / 2; |
| 49 | } |
| 50 | |
| 51 | bool valueClose(int64_t value, int64_t goal) |
| 52 | { |
| 53 | #if 0 |
| 54 | int64_t delta = 100; /* within 100 */ |
| 55 | if (value < (goal + delta) && |
| 56 | value > (goal - delta)) |
| 57 | { |
| 58 | return true; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | /* let's make sure it's below goal. */ |
| 63 | if (value < goal) |
| 64 | { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | static void driveGoal( |
| 72 | int64_t& seriesCnt, |
| 73 | int64_t setPwm, |
| 74 | int64_t goal, |
| 75 | std::vector<std::tuple<tstamp, int64_t, int64_t>>& series, |
| 76 | std::vector<std::unique_ptr<Sensor>>& fanSensors) |
| 77 | { |
| 78 | bool reading = true; |
| 79 | |
| 80 | auto& fan0 = fanSensors.at(0); |
| 81 | auto& fan1 = fanSensors.at(1); |
| 82 | |
| 83 | fan0->write(setPwm); |
| 84 | fan1->write(setPwm); |
| 85 | |
| 86 | while (reading) |
| 87 | { |
| 88 | bool check = false; |
| 89 | ReadReturn r0 = fan0->read(); |
| 90 | ReadReturn r1 = fan1->read(); |
| 91 | int64_t n0 = static_cast<int64_t>(r0.value); |
| 92 | int64_t n1 = static_cast<int64_t>(r1.value); |
| 93 | |
| 94 | tstamp t1 = std::chrono::high_resolution_clock::now(); |
| 95 | |
| 96 | series.push_back(std::make_tuple(t1, n0, n1)); |
| 97 | seriesCnt += 1; |
| 98 | |
| 99 | int64_t avgn = (n0 + n1) / 2; |
| 100 | /* check last three values against goal if this is close */ |
| 101 | check = valueClose(avgn, goal); |
| 102 | |
| 103 | /* We know the last entry is within range. */ |
| 104 | if (check && seriesCnt > 3) |
| 105 | { |
| 106 | /* n-2 values */ |
| 107 | std::tuple<tstamp, int64_t, int64_t> nm2 = series.at(seriesCnt - 3); |
| 108 | /* n-1 values */ |
| 109 | std::tuple<tstamp, int64_t, int64_t> nm1 = series.at(seriesCnt - 2); |
| 110 | |
| 111 | int64_t avgnm2 = getAverage(nm2); |
| 112 | int64_t avgnm1 = getAverage(nm1); |
| 113 | |
| 114 | int64_t together = (avgnm2 + avgnm1) / 2; |
| 115 | |
| 116 | reading = !valueClose(together, goal); |
| 117 | |
| 118 | if (!reading) |
| 119 | { |
| 120 | std::cerr << "finished reaching goal\n"; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* Early abort for testing. */ |
| 125 | if (seriesCnt > 150000) |
| 126 | { |
| 127 | std::cerr << "aborting after 150000 reads.\n"; |
| 128 | reading = false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | static void driveTime( |
| 136 | int64_t& seriesCnt, |
| 137 | int64_t setPwm, |
| 138 | int64_t goal, |
| 139 | std::vector<std::tuple<tstamp, int64_t, int64_t>>& series, |
| 140 | std::vector<std::unique_ptr<Sensor>>& fanSensors) |
| 141 | { |
| 142 | using namespace std::literals::chrono_literals; |
| 143 | |
| 144 | bool reading = true; |
| 145 | |
| 146 | auto& fan0 = fanSensors.at(0); |
| 147 | auto& fan1 = fanSensors.at(1); |
| 148 | |
| 149 | auto& s0 = series.at(0); |
| 150 | tstamp t0 = std::get<0>(s0); |
| 151 | |
| 152 | fan0->write(setPwm); |
| 153 | fan1->write(setPwm); |
| 154 | |
| 155 | while (reading) |
| 156 | { |
| 157 | ReadReturn r0 = fan0->read(); |
| 158 | ReadReturn r1 = fan1->read(); |
| 159 | int64_t n0 = static_cast<int64_t>(r0.value); |
| 160 | int64_t n1 = static_cast<int64_t>(r1.value); |
| 161 | tstamp t1 = std::chrono::high_resolution_clock::now(); |
| 162 | |
| 163 | series.push_back(std::make_tuple(t1, n0, n1)); |
| 164 | |
| 165 | auto duration = std::chrono::duration_cast<std::chrono::microseconds> |
| 166 | (t1 - t0).count(); |
| 167 | if (duration >= (20000000us).count()) |
| 168 | { |
| 169 | reading = false; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | int driveMain(void) |
| 177 | { |
| 178 | /* Time series of the data, the timestamp after both are read and the values. */ |
| 179 | std::vector<std::tuple<tstamp, int64_t, int64_t>> series; |
| 180 | int64_t seriesCnt = 0; /* in case vector count isn't constant time */ |
| 181 | int drive = DRIVE; |
| 182 | |
| 183 | /* |
| 184 | * The fan map: |
| 185 | * --> 0 | 4 |
| 186 | * --> 1 | 5 |
| 187 | * --> 2 | 6 |
| 188 | * --> 3 | 7 |
| 189 | */ |
| 190 | std::vector<std::string> fans = |
| 191 | { |
| 192 | "/sys/class/hwmon/hwmon0/fan0_input", |
| 193 | "/sys/class/hwmon/hwmon0/fan4_input" |
| 194 | }; |
| 195 | |
| 196 | std::vector<std::string> pwms = |
| 197 | { |
| 198 | "/sys/class/hwmon/hwmon0/pwm0", |
| 199 | "/sys/class/hwmon/hwmon0/pwm4" |
| 200 | }; |
| 201 | |
| 202 | std::vector<std::unique_ptr<Sensor>> fanSensors; |
| 203 | |
| 204 | auto fan0 = Create(fans[0], pwms[0]); |
| 205 | auto fan1 = Create(fans[1], pwms[1]); |
| 206 | |
| 207 | ReadReturn r0 = fan0->read(); |
| 208 | ReadReturn r1 = fan1->read(); |
| 209 | int64_t pwm0_value = static_cast<int64_t>(r0.value); |
| 210 | int64_t pwm1_value = static_cast<int64_t>(r1.value); |
| 211 | |
| 212 | if (MAX_PWM != pwm0_value || MAX_PWM != pwm1_value) |
| 213 | { |
| 214 | std::cerr << "bad PWM starting point.\n"; |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | r0 = fan0->read(); |
| 219 | r1 = fan1->read(); |
| 220 | int64_t fan0_start = r0.value; |
| 221 | int64_t fan1_start = r1.value; |
| 222 | tstamp t1 = std::chrono::high_resolution_clock::now(); |
| 223 | |
| 224 | /* |
| 225 | * I've done experiments, and seen 9080,10243 as a starting point |
| 226 | * which leads to a 50% goal of 4830.5, which is higher than the |
| 227 | * average that they reach, 4668. -- i guess i could try to figure out |
| 228 | * a good increase from one to the other, but how fast they're going |
| 229 | * actually influences how much they influence, so at slower speeds the |
| 230 | * improvement is less. |
| 231 | */ |
| 232 | |
| 233 | series.push_back(std::make_tuple(t1, fan0_start, fan1_start)); |
| 234 | seriesCnt += 1; |
| 235 | |
| 236 | int64_t average = (fan0_start + fan1_start) / 2; |
| 237 | int64_t goal = 0.5 * average; |
| 238 | |
| 239 | std::cerr << "goal: " << goal << "\n"; |
| 240 | |
| 241 | // fan0 @ 128: 4691 |
| 242 | // fan4 @ 128: 4707 |
| 243 | |
| 244 | fanSensors.push_back(std::move(fan0)); |
| 245 | fanSensors.push_back(std::move(fan1)); |
| 246 | |
| 247 | if (DRIVE_TIME == drive) |
| 248 | { |
| 249 | driveTime(seriesCnt, 128, goal, series, fanSensors); |
| 250 | } |
| 251 | else if (DRIVE_GOAL == drive) |
| 252 | { |
| 253 | driveGoal(seriesCnt, 128, goal, series, fanSensors); |
| 254 | } |
| 255 | tstamp tp = t1; |
| 256 | |
| 257 | /* Output the values and the timepoints as a time series for review. */ |
| 258 | for (auto& t : series) |
| 259 | { |
| 260 | tstamp ts = std::get<0>(t); |
| 261 | int64_t n0 = std::get<1>(t); |
| 262 | int64_t n1 = std::get<2>(t); |
| 263 | |
| 264 | auto duration = std::chrono::duration_cast<std::chrono::microseconds> |
| 265 | (ts - tp).count(); |
| 266 | std::cout << duration << "us, " << n0 << ", " << n1 << "\n"; |
| 267 | |
| 268 | tp = ts; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |