For each zone log sensor name with max setpoint

Add sensor name that has the maximum setpoint for a PID zone.
Log a debug message when the sensor is changed.
The name is also added to the log file for each log record.

Tested:
Override one CPU temperature sensor
busctl set-property xyz.openbmc_project.CPUSensor /xyz/openbmc_project/sensors/temperature/DTS_CPU1 xyz.openbmc_project.Sensor.Value Value d 82
Observed log message:
swampd[443]: PID Zone 0 max SetPoint 34.5546 requested by DTS_CPU1

Signed-off-by: Nirav Shah <nirav.j2.shah@intel.com>
Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: Ifc12cb9a106da1bf41dd35697210f74ba1b589db
diff --git a/pid/zone.cpp b/pid/zone.cpp
index af40a2b..1eda992 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -101,9 +101,18 @@
     return _zoneId;
 }
 
-void DbusPidZone::addSetPoint(double setpoint)
+void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
 {
-    _SetPoints.push_back(setpoint);
+    _SetPoints.push_back(setPoint);
+    /*
+     * if there are multiple thermal controllers with the same
+     * value, pick the first one in the iterator
+     */
+    if (_maximumSetPoint < setPoint)
+    {
+        _maximumSetPoint = setPoint;
+        _maximumSetPointName = name;
+    }
 }
 
 void DbusPidZone::addRPMCeiling(double ceiling)
@@ -119,6 +128,7 @@
 void DbusPidZone::clearSetPoints(void)
 {
     _SetPoints.clear();
+    _maximumSetPoint = 0;
 }
 
 double DbusPidZone::getFailSafePercent(void) const
@@ -126,7 +136,7 @@
     return _failSafePercent;
 }
 
-double DbusPidZone::getMinThermalSetpoint(void) const
+double DbusPidZone::getMinThermalSetPoint(void) const
 {
     return _minThermalOutputSetPt;
 }
@@ -210,27 +220,46 @@
 
 void DbusPidZone::determineMaxSetPointRequest(void)
 {
-    double max = 0;
     std::vector<double>::iterator result;
-
-    if (_SetPoints.size() > 0)
-    {
-        result = std::max_element(_SetPoints.begin(), _SetPoints.end());
-        max = *result;
-    }
+    double minThermalThreshold = getMinThermalSetPoint();
 
     if (_RPMCeilings.size() > 0)
     {
         result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
-        max = std::min(max, *result);
+        // if Max set point is larger than the lowest ceiling, reset to lowest
+        // ceiling.
+        if (*result < _maximumSetPoint)
+        {
+            _maximumSetPoint = *result;
+            // When using lowest ceiling, controller name is ceiling.
+            _maximumSetPointName = "Ceiling";
+        }
     }
 
     /*
      * If the maximum RPM setpoint output is below the minimum RPM
      * setpoint, set it to the minimum.
      */
-    max = std::max(getMinThermalSetpoint(), max);
-
+    if (minThermalThreshold >= _maximumSetPoint)
+    {
+        _maximumSetPoint = minThermalThreshold;
+        _maximumSetPointName = "";
+    }
+    else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
+    {
+        std::cerr << "PID Zone " << _zoneId << " max SetPoint "
+                  << _maximumSetPoint << " requested by "
+                  << _maximumSetPointName;
+        for (const auto& sensor : _failSafeSensors)
+        {
+            if (sensor.find("Fan") == std::string::npos)
+            {
+                std::cerr << " " << sensor;
+            }
+        }
+        std::cerr << "\n";
+        _maximumSetPointNamePrev.assign(_maximumSetPointName);
+    }
     if (tuningEnabled)
     {
         /*
@@ -240,17 +269,15 @@
          */
         static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
 
-        fileParseRpm(setpointpath, max);
+        fileParseRpm(setpointpath, _maximumSetPoint);
 
         // Allow per-zone setpoint files to override overall setpoint file
         std::ostringstream zoneSuffix;
         zoneSuffix << ".zone" << _zoneId;
         std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
 
-        fileParseRpm(zoneSetpointPath, max);
+        fileParseRpm(zoneSetpointPath, _maximumSetPoint);
     }
-
-    _maximumSetPoint = max;
     return;
 }
 
@@ -260,7 +287,7 @@
      * epoch_ms,setpt,fan1,fan2,fanN,sensor1,sensor2,sensorN,failsafe
      */
 
-    _log << "epoch_ms,setpt";
+    _log << "epoch_ms,setpt,requester";
 
     for (const auto& f : _fanInputs)
     {
@@ -308,6 +335,7 @@
                     now.time_since_epoch())
                     .count();
         _log << "," << _maximumSetPoint;
+        _log << "," << _maximumSetPointName;
     }
 
     for (const auto& f : _fanInputs)