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 
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 
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 
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 
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 
133 static void driveTime(int64_t& seriesCnt, int64_t setPwm, int64_t goal,
134                       std::vector<std::tuple<tstamp, int64_t, int64_t>>& series,
135                       std::vector<std::unique_ptr<Sensor>>& fanSensors)
136 {
137     using namespace std::literals::chrono_literals;
138 
139     bool reading = true;
140 
141     auto& fan0 = fanSensors.at(0);
142     auto& fan1 = fanSensors.at(1);
143 
144     auto& s0 = series.at(0);
145     tstamp t0 = std::get<0>(s0);
146 
147     fan0->write(setPwm);
148     fan1->write(setPwm);
149 
150     while (reading)
151     {
152         ReadReturn r0 = fan0->read();
153         ReadReturn r1 = fan1->read();
154         int64_t n0 = static_cast<int64_t>(r0.value);
155         int64_t n1 = static_cast<int64_t>(r1.value);
156         tstamp t1 = std::chrono::high_resolution_clock::now();
157 
158         series.push_back(std::make_tuple(t1, n0, n1));
159 
160         auto duration =
161             std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
162                 .count();
163         if (duration >= (20000000us).count())
164         {
165             reading = false;
166         }
167     }
168 
169     return;
170 }
171 
172 int driveMain(void)
173 {
174     /* Time series of the data, the timestamp after both are read and the
175      * values. */
176     std::vector<std::tuple<tstamp, int64_t, int64_t>> series;
177     int64_t seriesCnt = 0; /* in case vector count isn't constant time */
178     int drive = DRIVE;
179 
180     /*
181      * The fan map:
182      *  --> 0 | 4
183      *  --> 1 | 5
184      *  --> 2 | 6
185      *  --> 3 | 7
186      */
187     std::vector<std::string> fans = {"/sys/class/hwmon/hwmon0/fan0_input",
188                                      "/sys/class/hwmon/hwmon0/fan4_input"};
189 
190     std::vector<std::string> pwms = {"/sys/class/hwmon/hwmon0/pwm0",
191                                      "/sys/class/hwmon/hwmon0/pwm4"};
192 
193     std::vector<std::unique_ptr<Sensor>> fanSensors;
194 
195     auto fan0 = Create(fans[0], pwms[0]);
196     auto fan1 = Create(fans[1], pwms[1]);
197 
198     ReadReturn r0 = fan0->read();
199     ReadReturn r1 = fan1->read();
200     int64_t pwm0_value = static_cast<int64_t>(r0.value);
201     int64_t pwm1_value = static_cast<int64_t>(r1.value);
202 
203     if (MAX_PWM != pwm0_value || MAX_PWM != pwm1_value)
204     {
205         std::cerr << "bad PWM starting point.\n";
206         return -EINVAL;
207     }
208 
209     r0 = fan0->read();
210     r1 = fan1->read();
211     int64_t fan0_start = r0.value;
212     int64_t fan1_start = r1.value;
213     tstamp t1 = std::chrono::high_resolution_clock::now();
214 
215     /*
216      * I've done experiments, and seen 9080,10243 as a starting point
217      * which leads to a 50% goal of 4830.5, which is higher than the
218      * average that they reach, 4668.  -- i guess i could try to figure out
219      * a good increase from one to the other, but how fast they're going
220      * actually influences how much they influence, so at slower speeds the
221      * improvement is less.
222      */
223 
224     series.push_back(std::make_tuple(t1, fan0_start, fan1_start));
225     seriesCnt += 1;
226 
227     int64_t average = (fan0_start + fan1_start) / 2;
228     int64_t goal = 0.5 * average;
229 
230     std::cerr << "goal: " << goal << "\n";
231 
232     // fan0 @ 128: 4691
233     // fan4 @ 128: 4707
234 
235     fanSensors.push_back(std::move(fan0));
236     fanSensors.push_back(std::move(fan1));
237 
238     if (DRIVE_TIME == drive)
239     {
240         driveTime(seriesCnt, 128, goal, series, fanSensors);
241     }
242     else if (DRIVE_GOAL == drive)
243     {
244         driveGoal(seriesCnt, 128, goal, series, fanSensors);
245     }
246     tstamp tp = t1;
247 
248     /* Output the values and the timepoints as a time series for review. */
249     for (const auto& t : series)
250     {
251         tstamp ts = std::get<0>(t);
252         int64_t n0 = std::get<1>(t);
253         int64_t n1 = std::get<2>(t);
254 
255         auto duration =
256             std::chrono::duration_cast<std::chrono::microseconds>(ts - tp)
257                 .count();
258         std::cout << duration << "us, " << n0 << ", " << n1 << "\n";
259 
260         tp = ts;
261     }
262 
263     return 0;
264 }
265 
266 } // namespace pid_control
267