Enhancement of Dump cap algorithm

Added support for collecting mini dump data incase available
dump size is less than maximum dump size. Introduced minimum
dump size to achieve this.

Change-Id: I266ff6ea71443974a99f6f5fe54fb9f12c90f2ab
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/configure.ac b/configure.ac
index 3a042cd..3fb15c9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -67,6 +67,10 @@
 AS_IF([test x$BMC_DUMP_MAX_SIZE == x], [BMC_DUMP_MAX_SIZE=200])
 AC_DEFINE_UNQUOTED([BMC_DUMP_MAX_SIZE], [$BMC_DUMP_MAX_SIZE], [Maximum size of one bmc dump in kilo bytes])
 
+AC_ARG_VAR(BMC_DUMP_MIN_SPACE_REQD, [Minimum space required for one bmc dump in kilo bytes])
+AS_IF([test x$BMC_DUMP_MIN_SPACE_REQD == x], [BMC_DUMP_MIN_SPACE_REQD=20])
+AC_DEFINE_UNQUOTED([BMC_DUMP_MIN_SPACE_REQD], [$BMC_DUMP_MIN_SPACE_REQD], [Minimum space required for one bmc dump in kilo bytes])
+
 AC_ARG_VAR(BMC_DUMP_TOTAL_SIZE, [Total size of the bmc dump in kilo bytes])
 AS_IF([test x$BMC_DUMP_TOTAL_SIZE == x], [BMC_DUMP_TOTAL_SIZE=1024])
 AC_DEFINE_UNQUOTED([BMC_DUMP_TOTAL_SIZE], [$BMC_DUMP_TOTAL_SIZE], [Total size of the dump in kilo bytes])
diff --git a/dump_manager.cpp b/dump_manager.cpp
index 80e117b..ef840f3 100644
--- a/dump_manager.cpp
+++ b/dump_manager.cpp
@@ -81,17 +81,11 @@
         elog<InternalFailure>();
     }
 
-    //Increment active dump count.
-    activeDumpCount++;
-
     return ++lastEntryId;
 }
 
 void Manager::createEntry(const fs::path& file)
 {
-    //Decrement the Dump in progress counter.
-    activeDumpCount = (activeDumpCount == 0 ? 0 : activeDumpCount - 1);
-
     //Dump File Name format obmcdump_ID_EPOCHTIME.EXT
     static constexpr auto ID_POS         = 1;
     static constexpr auto EPOCHTIME_POS  = 2;
@@ -214,22 +208,33 @@
 
     auto size = 0;
 
-    // Maximum number of dump is based on total dump size
-    // and individual dump Max size configured in the system.
-    // Set the new dump size to max, in case sum of available
-    // dump and active dumps is less than maximum number of dumps.
-
-    constexpr auto dumpCount = BMC_DUMP_TOTAL_SIZE / BMC_DUMP_MAX_SIZE;
-
-    if ((entries.size() + activeDumpCount) < dumpCount)
+    //Get current size of the dump directory.
+    for (const auto& p : fs::recursive_directory_iterator(BMC_DUMP_PATH))
     {
-        size = BMC_DUMP_MAX_SIZE;
+        if (!fs::is_directory(p))
+        {
+            size += fs::file_size(p);
+        }
     }
-    else
+
+    //Convert size into KB
+    size = size / 1024;
+
+    //Set the Dump size to Maximum  if the free space is greater than
+    //Dump max size otherwise return the available size.
+
+    size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
+
+    if (size < BMC_DUMP_MIN_SPACE_REQD)
     {
         //Reached to maximum limit
         elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
     }
+    if (size > BMC_DUMP_MAX_SIZE)
+    {
+        size = BMC_DUMP_MAX_SIZE;
+    }
+
     return size;
 }
 
diff --git a/dump_manager.hpp b/dump_manager.hpp
index 9856f1e..ef3cd22 100644
--- a/dump_manager.hpp
+++ b/dump_manager.hpp
@@ -64,7 +64,6 @@
             bus(bus),
             eventLoop(event.get()),
             lastEntryId(0),
-            activeDumpCount(0),
             dumpWatch(eventLoop,
                   IN_NONBLOCK,
                   IN_CLOSE_WRITE | IN_CREATE,
@@ -137,11 +136,9 @@
           */
         void removeWatch(const fs::path& path);
 
-        /** @brief Calculate per dump allowed size based on number of
-          *        existing dumps and total dump size. Returns
-          *        allowed size for a dump in kilo bytes.
-          *  @returns 0 indicates no space is available in dump location
-          *           non zero value indicates size of one dump.
+        /** @brief Calculate per dump allowed size based on the available
+          *        size in the dump location.
+          *  @returns dump size in kilobytes.
           */
         size_t getAllowedSize();
 
@@ -157,9 +154,6 @@
         /** @brief Id of the last Dump entry */
         uint32_t lastEntryId;
 
-        /** @brief active dump count */
-        uint32_t activeDumpCount;
-
         /** @brief Dump main watch object */
         Watch dumpWatch;