blob: 7b767038a1a96caa44f4c7a6e6f68f5739f94f9e [file] [log] [blame]
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +05301#pragma once
2
3#include <cstring>
4#include <vector>
5#include <experimental/filesystem>
6#include <functional>
7#include <sdbusplus/bus.hpp>
Deepak Kodihalli5f031f32017-07-26 08:25:59 -05008#include <powercap.hpp>
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +05309#include "occ_pass_through.hpp"
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053010#include "occ_status.hpp"
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050011#include "occ_finder.hpp"
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053012#include "config.h"
13
14namespace sdbusRule = sdbusplus::bus::match::rules;
15
16namespace open_power
17{
18namespace occ
19{
20
21/** @class Manager
22 * @brief Builds and manages OCC objects
23 */
24struct Manager
25{
26 public:
27 Manager() = delete;
28 Manager(const Manager&) = delete;
29 Manager& operator=(const Manager&) = delete;
30 Manager(Manager&&) = default;
31 Manager& operator=(Manager&&) = default;
32 ~Manager() = default;
33
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053034 /** @brief Adds OCC pass-through and status objects on the bus
35 * when corresponding CPU inventory is created.
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053036 *
37 * @param[in] bus - handle to the bus
38 * @param[in] event - Unique ptr reference to sd_event
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053039 */
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053040 Manager(sdbusplus::bus::bus& bus,
41 EventPtr& event) :
42 bus(bus),
43 event(event)
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053044 {
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050045 // Check if CPU inventory exists already.
46 auto occs = open_power::occ::finder::get();
47 if (occs.empty())
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053048 {
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050049 // Need to watch for CPU inventory creation.
50 for (auto id = 0; id < MAX_CPUS; ++id)
51 {
52 auto path = std::string(CPU_PATH) + std::to_string(id);
53 cpuMatches.emplace_back(
54 bus,
55 sdbusRule::interfacesAdded() +
56 sdbusRule::argNpath(0, path),
57 std::bind(std::mem_fn(&Manager::cpuCreated),
58 this, std::placeholders::_1));
59 }
60 }
61 else
62 {
63 for (const auto& occ : occs)
64 {
65 // CPU inventory exists already, OCC objects can be created.
66 createObjects(occ);
67 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053068 }
69 }
70
71 /** @brief Callback that responds to cpu creation in the inventory -
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053072 * by creating the needed objects.
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053073 *
74 * @param[in] msg - bus message
75 *
76 * @returns 0 to indicate success
77 */
78 int cpuCreated(sdbusplus::message::message& msg)
79 {
80 namespace fs = std::experimental::filesystem;
81
82 sdbusplus::message::object_path o;
83 msg.read(o);
84 fs::path cpuPath(std::string(std::move(o)));
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053085
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050086 auto name = cpuPath.filename().string();
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +053087 auto index = name.find(CPU_NAME);
88 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
89
Deepak Kodihalli5f031f32017-07-26 08:25:59 -050090 createObjects(name);
91
92 return 0;
93 }
94
95 private:
96 /** @brief Create child OCC objects.
97 *
98 * @param[in] occ - the occ name, such as occ0.
99 */
100 void createObjects(const std::string& occ)
101 {
102 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
103
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530104 passThroughObjects.emplace_back(
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530105 std::make_unique<PassThrough>(
106 bus,
107 path.c_str()));
108
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530109 statusObjects.emplace_back(
110 std::make_unique<Status>(
111 bus,
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530112 event,
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530113 path.c_str()));
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500114
115 // Create the power cap monitor object for master occ (0)
Deepak Kodihalli5f031f32017-07-26 08:25:59 -0500116 if (!pcap)
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500117 {
118 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
119 bus,
Deepak Kodihalli5f031f32017-07-26 08:25:59 -0500120 *statusObjects.front());
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500121 }
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530122 }
123
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530124 /** @brief reference to the bus */
125 sdbusplus::bus::bus& bus;
126
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530127 /** @brief reference to sd_event wrapped in unique_ptr */
128 EventPtr& event;
129
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530130 /** @brief OCC pass-through objects */
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530131 std::vector<std::unique_ptr<PassThrough>> passThroughObjects;
132
133 /** @brief OCC Status objects */
134 std::vector<std::unique_ptr<Status>> statusObjects;
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530135
Andrew Geissler52cf26a2017-07-06 12:56:32 -0500136 /** @brief Power cap monitor and occ notification object */
137 std::unique_ptr<open_power::occ::powercap::PowerCap> pcap;
138
Vishwanatha Subbanna2180b2d2017-06-28 14:05:57 +0530139 /** @brief sbdbusplus match objects */
140 std::vector<sdbusplus::bus::match_t> cpuMatches;
141};
142
143} // namespace occ
144} // namespace open_power