sensors: use std set and map
This change fixed the HEAD where clang-tidy complains about exceptions.
It's a subset of this transition in the whole code base:
https://gerrit.openbmc.org/c/openbmc/bmcweb/+/54811
This commit leaves changing from ordered containers to unordered ones
as future improvements.
I tested this commit on real hardware with 300+ sensors. No big
difference on performance.
```
# before
time wget -qO- 'http://localhost/redfish/v1/Chassis/abc/Sensors?$expand=.' > /dev/null
real 0m0.778s
user 0m0.000s
sys 0m0.000s
# after
time wget -qO- 'http://localhost:18080/redfish/v1/Chassis/abc/Sensors?$expand=.' > /dev/nul
real 0m0.728s
user 0m0.000s
sys 0m0.030s
```
Tested:
1. code compiles;
2. Tested service validator. No errors in Sensor collections.
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I75e377d38f20340b4f2fa01041f1f3ebf679e411
diff --git a/redfish-core/lib/metric_report_definition.hpp b/redfish-core/lib/metric_report_definition.hpp
index ee8447d..8f45fca 100644
--- a/redfish-core/lib/metric_report_definition.hpp
+++ b/redfish-core/lib/metric_report_definition.hpp
@@ -10,6 +10,7 @@
#include <query.hpp>
#include <registries/privilege_registry.hpp>
+#include <map>
#include <tuple>
#include <variant>
@@ -335,7 +336,7 @@
AddReport& operator=(const AddReport&) = delete;
AddReport& operator=(AddReport&&) = delete;
- void insert(const boost::container::flat_map<std::string, std::string>& el)
+ void insert(const std::map<std::string, std::string>& el)
{
uriToDbus.insert(el.begin(), el.end());
}
@@ -403,8 +404,7 @@
chassis, sensorType,
[asyncResp, addReportReq](
const boost::beast::http::status status,
- const boost::container::flat_map<std::string, std::string>&
- uriToDbus) {
+ const std::map<std::string, std::string>& uriToDbus) {
if (status != boost::beast::http::status::ok)
{
BMCWEB_LOG_ERROR
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 489c49d..09e21e8 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -18,7 +18,6 @@
#include <app.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
-#include <boost/container/flat_map.hpp>
#include <boost/range/algorithm/replace_copy_if.hpp>
#include <dbus_singleton.hpp>
#include <dbus_utility.hpp>
@@ -29,6 +28,9 @@
#include <utils/query_param.hpp>
#include <cmath>
+#include <iterator>
+#include <map>
+#include <set>
#include <utility>
#include <variant>
@@ -180,7 +182,7 @@
public:
using DataCompleteCb = std::function<void(
const boost::beast::http::status status,
- const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
+ const std::map<std::string, std::string>& uriToDbus)>;
struct SensorData
{
@@ -234,7 +236,7 @@
if (dataComplete && metadata)
{
- boost::container::flat_map<std::string, std::string> map;
+ std::map<std::string, std::string> map;
if (asyncResp->res.result() == boost::beast::http::status::ok)
{
for (auto& sensor : *metadata)
@@ -340,7 +342,7 @@
template <typename Callback>
void getObjectsWithConnection(
const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
+ const std::shared_ptr<std::set<std::string>>& sensorNames,
Callback&& callback)
{
BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
@@ -366,11 +368,8 @@
// Make unique list of connections only for requested sensor types and
// found in the chassis
- boost::container::flat_set<std::string> connections;
+ std::set<std::string> connections;
std::set<std::pair<std::string, std::string>> objectsWithConnection;
- // Intrinsic to avoid malloc. Most systems will have < 8 sensor
- // producers
- connections.reserve(8);
BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
for (const std::string& tsensor : *sensorNames)
@@ -414,13 +413,12 @@
* @param callback Callback for processing gathered connections
*/
template <typename Callback>
-void getConnections(
- std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
- Callback&& callback)
+void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
+ const std::shared_ptr<std::set<std::string>> sensorNames,
+ Callback&& callback)
{
auto objectsWithConnectionCb =
- [callback](const boost::container::flat_set<std::string>& connections,
+ [callback](const std::set<std::string>& connections,
const std::set<std::pair<std::string, std::string>>&
/*objectsWithConnection*/) { callback(connections); };
getObjectsWithConnection(sensorsAsyncResp, sensorNames,
@@ -439,8 +437,7 @@
inline void reduceSensorList(
const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
const std::vector<std::string>* allSensors,
- const std::shared_ptr<boost::container::flat_set<std::string>>&
- activeSensors)
+ const std::shared_ptr<std::set<std::string>>& activeSensors)
{
if (sensorsAsyncResp == nullptr)
{
@@ -632,9 +629,8 @@
return;
}
}
- const std::shared_ptr<boost::container::flat_set<std::string>>
- culledSensorList =
- std::make_shared<boost::container::flat_set<std::string>>();
+ const std::shared_ptr<std::set<std::string>> culledSensorList =
+ std::make_shared<std::set<std::string>>();
reduceSensorList(sensorsAsyncResp, &nodeSensorList,
culledSensorList);
callback(culledSensorList);
@@ -660,8 +656,7 @@
*
* The callback must have the following signature:
* @code
- * callback(std::shared_ptr<boost::container::flat_map<std::string,
- * std::string>> objectMgrPaths)
+ * callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
* @endcode
*
* @param sensorsAsyncResp Pointer to object holding response data.
@@ -691,9 +686,8 @@
}
// Loop over returned object paths
- std::shared_ptr<boost::container::flat_map<std::string, std::string>>
- objectMgrPaths = std::make_shared<
- boost::container::flat_map<std::string, std::string>>();
+ std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
+ std::make_shared<std::map<std::string, std::string>>();
for (const std::pair<
std::string,
std::vector<std::pair<std::string, std::vector<std::string>>>>&
@@ -1187,8 +1181,8 @@
crow::connections::systemBus->async_method_call(
[path, sensorsAsyncResp](
const boost::system::error_code& err,
- const boost::container::flat_map<
- std::string, dbus::utility::DbusVariantType>& ret) {
+ const std::map<std::string,
+ dbus::utility::DbusVariantType>& ret) {
if (err)
{
return; // don't have to have this
@@ -1595,9 +1589,8 @@
static void getInventoryItemsData(
std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
- std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
- std::shared_ptr<boost::container::flat_map<std::string, std::string>>
- objectMgrPaths,
+ std::shared_ptr<std::set<std::string>> invConnections,
+ std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Callback&& callback, size_t invConnectionsIndex = 0)
{
BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
@@ -1611,7 +1604,8 @@
}
// Get inventory item data from current connection
- auto it = invConnections->nth(invConnectionsIndex);
+ auto it = invConnections->begin();
+ std::advance(it, invConnectionsIndex);
if (it != invConnections->end())
{
const std::string& invConnection = *it;
@@ -1683,8 +1677,7 @@
*
* The callback must have the following signature:
* @code
- * callback(std::shared_ptr<boost::container::flat_set<std::string>>
- * invConnections)
+ * callback(std::shared_ptr<std::set<std::string>> invConnections)
* @endcode
*
* @param sensorsAsyncResp Pointer to object holding response data.
@@ -1722,10 +1715,8 @@
}
// Make unique list of connections for desired inventory items
- std::shared_ptr<boost::container::flat_set<std::string>>
- invConnections =
- std::make_shared<boost::container::flat_set<std::string>>();
- invConnections->reserve(8);
+ std::shared_ptr<std::set<std::string>> invConnections =
+ std::make_shared<std::set<std::string>>();
// Loop through objects from GetSubTree
for (const std::pair<
@@ -1783,9 +1774,8 @@
template <typename Callback>
static void getInventoryItemAssociations(
const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
- const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
- objectMgrPaths,
+ const std::shared_ptr<std::set<std::string>>& sensorNames,
+ const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
Callback&& callback)
{
BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
@@ -1951,8 +1941,7 @@
void getInventoryLedData(
std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
- std::shared_ptr<boost::container::flat_map<std::string, std::string>>
- ledConnections,
+ std::shared_ptr<std::map<std::string, std::string>> ledConnections,
Callback&& callback, size_t ledConnectionsIndex = 0)
{
BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
@@ -1966,7 +1955,8 @@
}
// Get inventory item data from current connection
- auto it = ledConnections->nth(ledConnectionsIndex);
+ auto it = ledConnections->begin();
+ std::advance(it, ledConnectionsIndex);
if (it != ledConnections->end())
{
const std::string& ledPath = (*it).first;
@@ -2078,9 +2068,8 @@
}
// Build map of LED object paths to connections
- std::shared_ptr<boost::container::flat_map<std::string, std::string>>
- ledConnections = std::make_shared<
- boost::container::flat_map<std::string, std::string>>();
+ std::shared_ptr<std::map<std::string, std::string>> ledConnections =
+ std::make_shared<std::map<std::string, std::string>>();
// Loop through objects from GetSubTree
for (const std::pair<
@@ -2141,8 +2130,7 @@
void getPowerSupplyAttributesData(
const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
- const boost::container::flat_map<std::string, std::string>&
- psAttributesConnections,
+ const std::map<std::string, std::string>& psAttributesConnections,
Callback&& callback)
{
BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
@@ -2155,7 +2143,7 @@
}
// Assuming just one connection (service) for now
- auto it = psAttributesConnections.nth(0);
+ auto it = psAttributesConnections.begin();
const std::string& psAttributesPath = (*it).first;
const std::string& psAttributesConnection = (*it).second;
@@ -2265,8 +2253,7 @@
// Currently we only support 1 power supply attribute, use this for
// all the power supplies. Build map of object path to connection.
// Assume just 1 connection and 1 path for now.
- boost::container::flat_map<std::string, std::string>
- psAttributesConnections;
+ std::map<std::string, std::string> psAttributesConnections;
if (subtree[0].first.empty() || subtree[0].second.empty())
{
@@ -2329,9 +2316,8 @@
template <typename Callback>
static void getInventoryItems(
std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
- std::shared_ptr<boost::container::flat_map<std::string, std::string>>
- objectMgrPaths,
+ const std::shared_ptr<std::set<std::string>> sensorNames,
+ std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
Callback&& callback)
{
BMCWEB_LOG_DEBUG << "getInventoryItems enter";
@@ -2343,8 +2329,7 @@
auto getInventoryItemsConnectionsCb =
[sensorsAsyncResp, inventoryItems, objectMgrPaths,
callback{std::forward<const Callback>(callback)}](
- std::shared_ptr<boost::container::flat_set<std::string>>
- invConnections) {
+ std::shared_ptr<std::set<std::string>> invConnections) {
BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
callback{std::move(callback)}]() {
@@ -2470,10 +2455,9 @@
*/
inline void getSensorData(
const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
- const boost::container::flat_set<std::string>& connections,
- const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
- objectMgrPaths,
+ const std::shared_ptr<std::set<std::string>>& sensorNames,
+ const std::set<std::string>& connections,
+ const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
{
BMCWEB_LOG_DEBUG << "getSensorData enter";
@@ -2679,18 +2663,17 @@
BMCWEB_LOG_DEBUG << "getSensorData exit";
}
-inline void processSensorList(
- const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
+inline void
+ processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
+ const std::shared_ptr<std::set<std::string>>& sensorNames)
{
- auto getConnectionCb =
- [sensorsAsyncResp, sensorNames](
- const boost::container::flat_set<std::string>& connections) {
+ auto getConnectionCb = [sensorsAsyncResp, sensorNames](
+ const std::set<std::string>& connections) {
BMCWEB_LOG_DEBUG << "getConnectionCb enter";
auto getObjectManagerPathsCb =
- [sensorsAsyncResp, sensorNames,
- connections](const std::shared_ptr<boost::container::flat_map<
- std::string, std::string>>& objectMgrPaths) {
+ [sensorsAsyncResp, sensorNames, connections](
+ const std::shared_ptr<std::map<std::string, std::string>>&
+ objectMgrPaths) {
BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
auto getInventoryItemsCb =
[sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
@@ -2732,8 +2715,7 @@
BMCWEB_LOG_DEBUG << "getChassisData enter";
auto getChassisCb =
[sensorsAsyncResp](
- const std::shared_ptr<boost::container::flat_set<std::string>>&
- sensorNames) {
+ const std::shared_ptr<std::set<std::string>>& sensorNames) {
BMCWEB_LOG_DEBUG << "getChassisCb enter";
processSensorList(sensorsAsyncResp, sensorNames);
BMCWEB_LOG_DEBUG << "getChassisCb exit";
@@ -2758,12 +2740,12 @@
* @param sensorsModified The list of sensors that were found as a result of
* repeated calls to this function
*/
-inline bool findSensorNameUsingSensorPath(
- std::string_view sensorName,
- boost::container::flat_set<std::string>& sensorsList,
- boost::container::flat_set<std::string>& sensorsModified)
+inline bool
+ findSensorNameUsingSensorPath(std::string_view sensorName,
+ std::set<std::string>& sensorsList,
+ std::set<std::string>& sensorsModified)
{
- for (auto& chassisSensor : sensorsList)
+ for (const auto& chassisSensor : sensorsList)
{
sdbusplus::message::object_path path(chassisSensor);
std::string thisSensorName = path.filename();
@@ -2828,13 +2810,11 @@
auto getChassisSensorListCb =
[sensorAsyncResp, overrideMap](
- const std::shared_ptr<boost::container::flat_set<std::string>>&
- sensorsList) {
+ const std::shared_ptr<std::set<std::string>>& sensorsList) {
// Match sensor names in the PATCH request to those managed by the
// chassis node
- const std::shared_ptr<boost::container::flat_set<std::string>>
- sensorNames =
- std::make_shared<boost::container::flat_set<std::string>>();
+ const std::shared_ptr<std::set<std::string>> sensorNames =
+ std::make_shared<std::set<std::string>>();
for (const auto& item : overrideMap)
{
const auto& sensor = item.first;
@@ -2849,10 +2829,10 @@
}
// Get the connection to which the memberId belongs
auto getObjectsWithConnectionCb =
- [sensorAsyncResp, overrideMap](
- const boost::container::flat_set<std::string>& /*connections*/,
- const std::set<std::pair<std::string, std::string>>&
- objectsWithConnection) {
+ [sensorAsyncResp,
+ overrideMap](const std::set<std::string>& /*connections*/,
+ const std::set<std::pair<std::string, std::string>>&
+ objectsWithConnection) {
if (objectsWithConnection.size() != overrideMap.size())
{
BMCWEB_LOG_INFO
@@ -2946,11 +2926,11 @@
}
auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
- auto callback =
- [asyncResp, mapCompleteCb{std::move(mapComplete)}](
- const boost::beast::http::status status,
- const boost::container::flat_map<std::string, std::string>&
- uriToDbus) { mapCompleteCb(status, uriToDbus); };
+ auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
+ const boost::beast::http::status status,
+ const std::map<std::string, std::string>& uriToDbus) {
+ mapCompleteCb(status, uriToDbus);
+ };
auto resp = std::make_shared<SensorsAsyncResp>(
asyncResp, chassis, pathIt->second, node, std::move(callback));
@@ -2962,13 +2942,13 @@
inline void getChassisCallback(
const std::shared_ptr<SensorsAsyncResp>& asyncResp,
- const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
+ const std::shared_ptr<std::set<std::string>>& sensorNames)
{
BMCWEB_LOG_DEBUG << "getChassisCallback enter";
nlohmann::json& entriesArray =
asyncResp->asyncResp->res.jsonValue["Members"];
- for (auto& sensor : *sensorNames)
+ for (const auto& sensor : *sensorNames)
{
BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
@@ -3097,9 +3077,8 @@
BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
<< "': " << sensorPath;
- const std::shared_ptr<boost::container::flat_set<std::string>>
- sensorList =
- std::make_shared<boost::container::flat_set<std::string>>();
+ const std::shared_ptr<std::set<std::string>> sensorList =
+ std::make_shared<std::set<std::string>>();
sensorList->emplace(sensorPath);
processSensorList(asyncResp, sensorList);