blob: cec3789041dea8bfeabe735443b0b917fa48685d [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001/**
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
James Feist7136a5a2018-07-19 09:52:05 -070017#include "config.h"
Patrick Venturee6206562018-03-08 15:36:53 -080018
Patrick Ventureba8ffa72019-02-11 12:03:56 -080019#include "build/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020#include "conf.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080021#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070022#include "pid/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080023#include "pid/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024#include "pid/pidthread.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080025#include "pid/tuning.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080026#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070027#include "sensors/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080028#include "sensors/buildjson.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080029#include "sensors/manager.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080030#include "threads/busthread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080032
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070033#include <getopt.h>
34
35#include <chrono>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036#include <iostream>
37#include <map>
38#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039#include <sdbusplus/bus.hpp>
40#include <thread>
41#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080042#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080044
Patrick Venture9f044412018-09-20 19:49:55 -070045#if CONFIGURE_DBUS
46#include "dbus/dbusconfiguration.hpp"
47#endif
48
Patrick Venturee6206562018-03-08 15:36:53 -080049/* The YAML converted sensor list. */
James Feistf81f2882019-02-26 11:26:36 -080050std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080051/* The YAML converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080052std::map<int64_t, conf::PIDConf> zoneConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080053/* The YAML converted Zone configuration. */
James Feistf81f2882019-02-26 11:26:36 -080054std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080055
Patrick Venturec19f5d42019-02-14 10:59:47 -080056/** the swampd daemon will check for the existence of this file. */
57constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
58
Patrick Venturee6206562018-03-08 15:36:53 -080059int main(int argc, char* argv[])
60{
61 int rc = 0;
Patrick Venturee6206562018-03-08 15:36:53 -080062 std::string configPath = "";
63
64 while (1)
65 {
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066 // clang-format off
67 static struct option long_options[] = {
Patrick Venturee6206562018-03-08 15:36:53 -080068 {"conf", required_argument, 0, 'c'},
Patrick Venturec32e3fc2019-02-28 10:01:11 -080069 {"tuning", required_argument, 0, 't'},
Patrick Venturee6206562018-03-08 15:36:53 -080070 {0, 0, 0, 0}
71 };
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 // clang-format on
Patrick Venturee6206562018-03-08 15:36:53 -080073
74 int option_index = 0;
Patrick Venturec32e3fc2019-02-28 10:01:11 -080075 int c = getopt_long(argc, argv, "t:c:", long_options, &option_index);
Patrick Venturee6206562018-03-08 15:36:53 -080076
77 if (c == -1)
78 {
79 break;
80 }
81
82 switch (c)
83 {
84 case 'c':
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070085 configPath = std::string{optarg};
Patrick Venturee6206562018-03-08 15:36:53 -080086 break;
Patrick Venturec32e3fc2019-02-28 10:01:11 -080087 case 't':
88 tuningLoggingEnabled = true;
89 tuningLoggingPath = std::string{optarg};
90 break;
Patrick Venturee6206562018-03-08 15:36:53 -080091 default:
92 /* skip garbage. */
93 continue;
94 }
95 }
96
James Feist9fa90c12019-01-11 15:35:22 -080097 auto modeControlBus = sdbusplus::bus::new_system();
Patrick Venture4cb7c052019-02-14 11:11:33 -080098 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
99 // Create a manager for the ModeBus because we own it.
100 sdbusplus::server::manager::manager(modeControlBus, modeRoot);
101
Patrick Venture9f044412018-09-20 19:49:55 -0700102#if CONFIGURE_DBUS
James Feist7136a5a2018-07-19 09:52:05 -0700103 {
Patrick Venturec179d402018-10-30 19:51:55 -0700104 dbus_configuration::init(modeControlBus);
James Feist7136a5a2018-07-19 09:52:05 -0700105 }
Patrick Venture18b13112019-02-14 11:43:59 -0800106#else
107 const std::string& path =
108 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -0800109
110 /*
111 * When building the sensors, if any of the dbus passive ones aren't on the
112 * bus, it'll fail immediately.
113 */
Patrick Venture18b13112019-02-14 11:43:59 -0800114 try
Patrick Venturee6206562018-03-08 15:36:53 -0800115 {
Patrick Venture18b13112019-02-14 11:43:59 -0800116 auto jsonData = parseValidateJson(path);
117 sensorConfig = buildSensorsFromJson(jsonData);
118 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venturee6206562018-03-08 15:36:53 -0800119 }
Patrick Venture18b13112019-02-14 11:43:59 -0800120 catch (const std::exception& e)
121 {
122 std::cerr << "Failed during building: " << e.what() << "\n";
123 exit(EXIT_FAILURE); /* fatal error. */
124 }
125#endif
Patrick Venture4cb7c052019-02-14 11:11:33 -0800126
127 SensorManager mgmr = buildSensors(sensorConfig);
128 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
129 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800130
131 if (0 == zones.size())
132 {
133 std::cerr << "No zones defined, exiting.\n";
134 return rc;
135 }
136
137 /*
138 * All sensors are managed by one manager, but each zone has a pointer to
139 * it.
140 */
141
Patrick Venturec179d402018-10-30 19:51:55 -0700142 auto& hostSensorBus = mgmr.getHostBus();
143 auto& passiveListeningBus = mgmr.getPassiveBus();
Patrick Venturee6206562018-03-08 15:36:53 -0800144
145 std::cerr << "Starting threads\n";
146
147 /* TODO(venture): Ask SensorManager if we have any passive sensors. */
Patrick Venturec179d402018-10-30 19:51:55 -0700148 struct ThreadParams p = {std::ref(passiveListeningBus), ""};
149 std::thread l(busThread, std::ref(p));
Patrick Venturee6206562018-03-08 15:36:53 -0800150
151 /* TODO(venture): Ask SensorManager if we have any host sensors. */
152 static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
Patrick Venturec179d402018-10-30 19:51:55 -0700153 struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
154 std::thread te(busThread, std::ref(e));
Patrick Venturee6206562018-03-08 15:36:53 -0800155
156 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
Patrick Venturec179d402018-10-30 19:51:55 -0700157 struct ThreadParams m = {std::ref(modeControlBus), modeBus};
158 std::thread tm(busThread, std::ref(m));
Patrick Venturee6206562018-03-08 15:36:53 -0800159
160 std::vector<std::thread> zoneThreads;
161
162 /* TODO(venture): This was designed to have one thread per zone, but really
163 * it could have one thread for all the zones and iterate through each
164 * sequentially as it goes -- and it'd probably be fast enough to do that,
165 * however, a system isn't likely going to have more than a couple zones.
166 * If it only has a couple zones, then this is fine.
167 */
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700168 for (const auto& i : zones)
Patrick Venturee6206562018-03-08 15:36:53 -0800169 {
170 std::cerr << "pushing zone" << std::endl;
Patrick Venture7af157b2018-10-30 11:24:40 -0700171 zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
Patrick Venturee6206562018-03-08 15:36:53 -0800172 }
173
174 l.join();
175 te.join();
176 tm.join();
177 for (auto& t : zoneThreads)
178 {
179 t.join();
180 }
181
182 return rc;
183}