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