summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-ast2500/recipes-phosphor/sensors/dbus-sensors/0012-PSUSensors-Move-to-GetSensorConfiguration.patch
blob: b719565235c7ab3a43c8f2639a3fece112f6a67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
From 867d1a00e330e2490184794f2c3913694ad36f09 Mon Sep 17 00:00:00 2001
From: Zhikui Ren <zhikui.ren@intel.com>
Date: Thu, 5 Nov 2020 22:32:28 -0800
Subject: [PATCH 12/12] PSUSensors: Move to GetSensorConfiguration

GetSensorConfiguration has improved to have retries
and avoid more expensive GetManagedOjects calls. Use
it.

Tested
PSU sensors are created consistently ac_cycle test

Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: Id6b374ff415cc7c6c0d83b55f12551556838f2b7
---
 src/PSUSensorMain.cpp | 69 +++++++++++++++++++++++++++++--------------
 1 file changed, 47 insertions(+), 22 deletions(-)

diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index be55089..86bf393 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -216,27 +216,15 @@ static void
     }
 }
 
-void createSensors(boost::asio::io_service& io,
-                   sdbusplus::asio::object_server& objectServer,
-                   std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
+static void createSensorsCallback(
+    boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
+    std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
+    const ManagedObjectType& sensorConfigs,
+    const std::shared_ptr<boost::container::flat_set<std::string>>&
+        sensorsChanged)
 {
-
-    ManagedObjectType sensorConfigs;
     int numCreated = 0;
-    bool useCache = false;
-
-    // TODO may need only modify the ones that need to be changed.
-    sensors.clear();
-    for (const char* type : sensorTypes)
-    {
-        if (!getSensorConfiguration(type, dbusConnection, sensorConfigs,
-                                    useCache))
-        {
-            std::cerr << "error get sensor config from entity manager\n";
-            return;
-        }
-        useCache = true;
-    }
+    bool firstScan = sensorsChanged == nullptr;
 
     std::vector<fs::path> pmbusPaths;
     if (!findFiles(fs::path("/sys/class/hwmon"), "name", pmbusPaths))
@@ -400,6 +388,23 @@ void createSensors(boost::asio::io_service& io,
             std::cerr << "Cannot find psu name, invalid configuration\n";
             continue;
         }
+
+        // on rescans, only update sensors we were signaled by
+        if (!firstScan)
+        {
+            std::string psuNameStr = "/" + *psuName;
+            auto it =
+                std::find_if(sensorsChanged->begin(), sensorsChanged->end(),
+                             [psuNameStr](std::string& s) {
+                                 return boost::ends_with(s, psuNameStr);
+                             });
+
+            if (it == sensorsChanged->end())
+            {
+                continue;
+            }
+            sensorsChanged->erase(it);
+        }
         checkEvent(directory.string(), eventMatch, eventPathList);
         checkGroupEvent(directory.string(), groupEventMatch,
                         groupEventPathList);
@@ -779,7 +784,8 @@ void createSensors(boost::asio::io_service& io,
                           << sensorPathStr << "\" type \"" << sensorType
                           << "\"\n";
             }
-
+            // destruct existing one first if already created
+            sensors[sensorName] = nullptr;
             sensors[sensorName] = std::make_shared<PSUSensor>(
                 sensorPathStr, sensorType, objectServer, dbusConnection, io,
                 sensorName, std::move(sensorThresholds), *interfacePath,
@@ -808,6 +814,22 @@ void createSensors(boost::asio::io_service& io,
     return;
 }
 
+void createSensors(
+    boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
+    std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
+    const std::shared_ptr<boost::container::flat_set<std::string>>&
+        sensorsChanged)
+{
+    auto getter = std::make_shared<GetSensorConfiguration>(
+        dbusConnection, [&io, &objectServer, &dbusConnection, sensorsChanged](
+                            const ManagedObjectType& sensorConfigs) {
+            createSensorsCallback(io, objectServer, dbusConnection,
+                                  sensorConfigs, sensorsChanged);
+        });
+    getter->getConfiguration(
+        std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
+}
+
 void propertyInitialize(void)
 {
     sensorTable = {{"power", "power/"},
@@ -891,10 +913,12 @@ int main()
     systemBus->request_name("xyz.openbmc_project.PSUSensor");
     sdbusplus::asio::object_server objectServer(systemBus);
     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
+    auto sensorsChanged =
+        std::make_shared<boost::container::flat_set<std::string>>();
 
     propertyInitialize();
 
-    io.post([&]() { createSensors(io, objectServer, systemBus); });
+    io.post([&]() { createSensors(io, objectServer, systemBus, nullptr); });
     boost::asio::deadline_timer filterTimer(io);
     std::function<void(sdbusplus::message::message&)> eventHandler =
         [&](sdbusplus::message::message& message) {
@@ -903,6 +927,7 @@ int main()
                 std::cerr << "callback method error\n";
                 return;
             }
+            sensorsChanged->insert(message.get_path());
             filterTimer.expires_from_now(boost::posix_time::seconds(3));
             filterTimer.async_wait([&](const boost::system::error_code& ec) {
                 if (ec == boost::asio::error::operation_aborted)
@@ -913,7 +938,7 @@ int main()
                 {
                     std::cerr << "timer error\n";
                 }
-                createSensors(io, objectServer, systemBus);
+                createSensors(io, objectServer, systemBus, sensorsChanged);
             });
         };
 
-- 
2.17.1