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