Scale down threads for collection at power on

In case where BMC reboots and VPD-Manager re-triggers the collection
process for the FRUs, number of threads has been restricted to 2 for
now (Just a lower value).

The thread count is configurable and can be updated as required.

This is required to scale down CPU utilization at power on.

Change-Id: I7dee6dabf073bc1f17ab4f9f672aa183b00e116a
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
diff --git a/vpd-manager/include/worker.hpp b/vpd-manager/include/worker.hpp
index ba88d33..ac1b3d8 100644
--- a/vpd-manager/include/worker.hpp
+++ b/vpd-manager/include/worker.hpp
@@ -42,11 +42,13 @@
      * initialize the parsed JSON variable.
      *
      * @param[in] pathToConfigJSON - Path to the config JSON, if applicable.
+     * @param[in] i_maxThreadCount - Maximum thread while collecting FRUs VPD.
      *
      * Note: Throws std::exception in case of construction failure. Caller needs
      * to handle to detect successful object creation.
      */
-    Worker(std::string pathToConfigJson = std::string());
+    Worker(std::string pathToConfigJson = std::string(),
+           uint8_t i_maxThreadCount = constants::MAX_THREADS);
 
     /**
      * @brief Destructor
@@ -513,6 +515,8 @@
      * Some FRUs, under some given scenarios should not be collected and
      * skipped.
      *
+     * @param[in] i_vpdFilePath - EEPROM path.
+     *
      * @return True - if path is empty or should be skipped, false otherwise.
      */
     bool skipPathForCollection(const std::string& i_vpdFilePath);
@@ -541,7 +545,6 @@
     std::mutex m_mutex;
 
     // Counting semaphore to limit the number of threads.
-    std::counting_semaphore<constants::MAX_THREADS> m_semaphore{
-        constants::MAX_THREADS};
+    std::counting_semaphore<constants::MAX_THREADS> m_semaphore;
 };
 } // namespace vpd
diff --git a/vpd-manager/src/manager.cpp b/vpd-manager/src/manager.cpp
index 51eb4af..dd00d92 100644
--- a/vpd-manager/src/manager.cpp
+++ b/vpd-manager/src/manager.cpp
@@ -29,7 +29,18 @@
     try
     {
 #ifdef IBM_SYSTEM
-        m_worker = std::make_shared<Worker>(INVENTORY_JSON_DEFAULT);
+        if (dbusUtility::isChassisPowerOn())
+        {
+            // At power on, less number of FRU(s) needs collection. we can scale
+            // down the threads to reduce CPU utilization.
+            m_worker = std::make_shared<Worker>(INVENTORY_JSON_DEFAULT,
+                                                constants::VALUE_1);
+        }
+        else
+        {
+            // Initialize with default configuration
+            m_worker = std::make_shared<Worker>(INVENTORY_JSON_DEFAULT);
+        }
 
         // Set up minimal things that is needed before bus name is claimed.
         m_worker->performInitialSetup();
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index a0b8a24..ff92264 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -25,8 +25,8 @@
 namespace vpd
 {
 
-Worker::Worker(std::string pathToConfigJson) :
-    m_configJsonPath(pathToConfigJson)
+Worker::Worker(std::string pathToConfigJson, uint8_t i_maxThreadCount) :
+    m_configJsonPath(pathToConfigJson), m_semaphore(i_maxThreadCount)
 {
     // Implies the processing is based on some config JSON
     if (!m_configJsonPath.empty())