diff --git a/base/include/ArchiveSpaceManager.h b/base/include/ArchiveSpaceManager.h index a7431ae53..2f5386bd2 100644 --- a/base/include/ArchiveSpaceManager.h +++ b/base/include/ArchiveSpaceManager.h @@ -2,56 +2,56 @@ #include "Module.h" #include "AIPExceptions.h" -class ArchiveSpaceManagerProps : public ModuleProps +class ArchiveSpaceManagerProps : public ModuleProps { public: - ArchiveSpaceManagerProps(uint32_t _lowerWaterMark, uint32_t _upperWaterMark, string _pathToWatch, int _samplingFreq) - { - lowerWaterMark = _lowerWaterMark; - upperWaterMark = _upperWaterMark; - pathToWatch = _pathToWatch; - samplingFreq = _samplingFreq; - fps = 0.001; - - auto totalSpace = boost::filesystem::space(pathToWatch); - if ((lowerWaterMark > upperWaterMark) || (upperWaterMark > totalSpace.capacity)) - { - LOG_ERROR << "Please enter correct properties!"; + ArchiveSpaceManagerProps(uint64_t _lowerWaterMark, uint64_t _upperWaterMark, string _pathToWatch, int _samplingFreq) + { + lowerWaterMark = _lowerWaterMark; + upperWaterMark = _upperWaterMark; + pathToWatch = _pathToWatch; + samplingFreq = _samplingFreq; + fps = 0.001; + + auto totalSpace = boost::filesystem::space(pathToWatch); + if ((lowerWaterMark > upperWaterMark) || (upperWaterMark > totalSpace.capacity)) + { + LOG_ERROR << "Please enter correct properties!"; std::string errorMsg = "Incorrect properties set for Archive Manager. TotalDiskCapacity <" + std::to_string(totalSpace.capacity) + ">lowerWaterMark<" + std::to_string(lowerWaterMark) + "> UpperWaterMark<" + std::to_string(upperWaterMark) + ">"; - throw AIPException(AIP_FATAL, errorMsg); - } - } + throw AIPException(AIP_FATAL, errorMsg); + } + } - ArchiveSpaceManagerProps(uint32_t maxSizeAllowed, string _pathToWatch, int _samplingFreq) - { - lowerWaterMark = maxSizeAllowed - (maxSizeAllowed / 10); - upperWaterMark = maxSizeAllowed; - pathToWatch = _pathToWatch; - samplingFreq = _samplingFreq; - fps = 0.001; + ArchiveSpaceManagerProps(uint64_t maxSizeAllowed, string _pathToWatch, int _samplingFreq) + { + lowerWaterMark = maxSizeAllowed - (maxSizeAllowed / 10); + upperWaterMark = maxSizeAllowed; + pathToWatch = _pathToWatch; + samplingFreq = _samplingFreq; + fps = 0.001; - auto totalSpace = boost::filesystem::space(pathToWatch); - if ((lowerWaterMark > upperWaterMark) || (upperWaterMark > totalSpace.capacity)) - { - LOG_ERROR << "Please enter correct properties!"; + auto totalSpace = boost::filesystem::space(pathToWatch); + if ((lowerWaterMark > upperWaterMark) || (upperWaterMark > totalSpace.capacity)) + { + LOG_ERROR << "Please enter correct properties!"; std::string errorMsg = "Incorrect properties set for Archive Manager. TotalDiskCapacity <" + std::to_string(totalSpace.capacity) + ">lowerWaterMark<" + std::to_string(lowerWaterMark) + "> UpperWaterMark<" + std::to_string(upperWaterMark) + ">"; - throw AIPException(AIP_FATAL, errorMsg); - } - } + throw AIPException(AIP_FATAL, errorMsg); + } + } - - uint32_t lowerWaterMark; // Lower disk space - uint32_t upperWaterMark; // Higher disk space - std::string pathToWatch; - int samplingFreq; - size_t getSerializeSize() - { + + uint64_t lowerWaterMark; // Lower disk space + uint64_t upperWaterMark; // Higher disk space + std::string pathToWatch; + int samplingFreq; + size_t getSerializeSize() + { return ModuleProps::getSerializeSize() + sizeof(lowerWaterMark) + sizeof(upperWaterMark) + sizeof(pathToWatch) + sizeof(samplingFreq); - } + } private: - friend class boost::serialization::access; + friend class boost::serialization::access; - template + template void serialize(Archive& ar, const unsigned int version) { ar& boost::serialization::base_object(*this); @@ -59,32 +59,32 @@ class ArchiveSpaceManagerProps : public ModuleProps ar& upperWaterMark; ar& pathToWatch; ar& samplingFreq; - } + } }; class ArchiveSpaceManager : public Module { public: - ArchiveSpaceManager(ArchiveSpaceManagerProps _props); + ArchiveSpaceManager(ArchiveSpaceManagerProps _props); virtual ~ArchiveSpaceManager() { } - bool init(); - bool term(); - uint32_t finalArchiveSpace = 0; - void setProps(ArchiveSpaceManagerProps& props); - ArchiveSpaceManagerProps getProps(); + bool init(); + bool term(); + uint64_t finalArchiveSpace = 0; + void setProps(ArchiveSpaceManagerProps &props); + ArchiveSpaceManagerProps getProps(); protected: - bool process(); - bool validateInputPins(); - bool validateOutputPins(); - bool validateInputOutputPins(); + bool produce(); + bool validateInputPins(); + bool validateOutputPins(); + bool validateInputOutputPins(); void addInputPin(framemetadata_sp& metadata, string& pinId); bool handlePropsChange(frame_sp& frame); private: - class Detail; - boost::shared_ptr mDetail; - bool checkDirectory = true; + class Detail; + boost::shared_ptr mDetail; + bool checkDirectory = true; }; \ No newline at end of file diff --git a/base/src/ArchiveSpaceManager.cpp b/base/src/ArchiveSpaceManager.cpp index d5db1a334..b6444083b 100644 --- a/base/src/ArchiveSpaceManager.cpp +++ b/base/src/ArchiveSpaceManager.cpp @@ -20,105 +20,119 @@ class ArchiveSpaceManager::Detail mProps = _props; } - uint32_t estimateDirectorySize(boost::filesystem::path _dir) + uint64_t estimateDirectorySize(boost::filesystem::path _dir) + { + std::cout << "Estimating size for directory: " << _dir << std::endl; + uint64_t dirSize = 0; + int sample = 0; + int inCount = 0; + int countFreq = 0; + uint64_t tempSize = 0; + + for (const auto& entry : boost::filesystem::recursive_directory_iterator(_dir)) { - uint32_t dirSize = 0; - int sample = 0; - int inCount = 0; - int countFreq = 0; - uint32_t tempSize = 0; - - for (const auto& entry : boost::filesystem::recursive_directory_iterator(_dir)) + if (boost::filesystem::is_regular_file(entry)) + { + if (countFreq % mProps.samplingFreq == 0) { - if ((boost::filesystem::is_regular_file(entry))) - { - if (!(countFreq % mProps.samplingFreq)) - { - sample = (rand() % mProps.samplingFreq) + 1; - inCount = 0; - } - if (inCount == sample) - { - tempSize = boost::filesystem::file_size(entry); - dirSize += tempSize * mProps.samplingFreq; - } - inCount++; - countFreq++; - } + sample = (rand() % mProps.samplingFreq); + inCount = 0; } - if (inCount < mProps.samplingFreq) + if (inCount == sample) { - dirSize = dirSize + (tempSize * inCount); + try + { + tempSize = boost::filesystem::file_size(entry); + dirSize += tempSize * mProps.samplingFreq; + } + catch (const std::exception &e) + { + std::cout << "Failed to get file size for " << entry << ": " + << e.what() << std::endl; + } } - return dirSize; + inCount++; + countFreq++; + } + } + if (inCount < mProps.samplingFreq && inCount > 0) + { + dirSize += tempSize * inCount; } + std::cout << "Total Directory Size: " << dirSize << std::endl; + return dirSize; + } - boost::filesystem::path getOldestDirectory(boost::filesystem::path _cam) + boost::filesystem::path getOldestDirectory(boost::filesystem::path _cam) + { + for (const auto& camFolder : boost::filesystem::directory_iterator(_cam)) { - for (const auto& camFolder : boost::filesystem::directory_iterator(_cam)) + for (const auto& folder : boost::filesystem::recursive_directory_iterator(camFolder)) + { + if (boost::filesystem::is_regular_file(folder)) { - for (const auto& folder : boost::filesystem::recursive_directory_iterator(camFolder)) - { - if (boost::filesystem::is_regular_file(folder)) - { - boost::filesystem::path p = folder.path().parent_path(); - return p; - } - } + boost::filesystem::path p = folder.path().parent_path(); + return p; } - return _cam; - }; + } + } + return _cam; + }; - void manageDirectory() + void manageDirectory() + { + auto comparator = [](std::pair& a, std::pair& b) {return a.second < b.second; }; + while (archiveSize > mProps.lowerWaterMark) { - auto comparator = [](std::pair& a, std::pair& b) {return a.second < b.second; }; - while (archiveSize > mProps.lowerWaterMark) - { - for (const auto& camFolder : boost::filesystem::directory_iterator(mProps.pathToWatch)) - { - boost::filesystem::path oldHrDir = getOldestDirectory(camFolder); - foldVector.push_back({ oldHrDir,boost::filesystem::last_write_time(oldHrDir) }); - } + for (const auto& camFolder : boost::filesystem::directory_iterator(mProps.pathToWatch)) + { + boost::filesystem::path oldHrDir = getOldestDirectory(camFolder); + foldVector.push_back({ oldHrDir,boost::filesystem::last_write_time(oldHrDir) }); + } sort(foldVector.begin(), foldVector.end(), comparator); //Sorting the vector - uint32_t tempSize = 0; - boost::filesystem::path delDir = foldVector[0].first; - BOOST_LOG_TRIVIAL(info) << "Deleting file : " << delDir.string(); - tempSize = estimateDirectorySize(delDir); - archiveSize = archiveSize - tempSize; - try - { - boost::filesystem::remove_all(delDir); - } - catch (...) - { - LOG_ERROR << "Could not delete directory!.."; - } - foldVector.clear(); - } + uint64_t tempSize = 0; + boost::filesystem::path delDir = foldVector[0].first; + BOOST_LOG_TRIVIAL(info) << "Deleting file : " << delDir.string(); + tempSize = estimateDirectorySize(delDir); + archiveSize = archiveSize - tempSize; + try + { + boost::filesystem::remove_all(delDir); + } + catch (...) + { + LOG_ERROR << "Could not delete directory!.."; + } + foldVector.clear(); } + } - uint32_t diskOperation() + uint64_t diskOperation() + { + archiveSize = estimateDirectorySize(mProps.pathToWatch); + if (archiveSize > mProps.upperWaterMark) { - archiveSize = estimateDirectorySize(mProps.pathToWatch); - if (archiveSize > mProps.upperWaterMark) - { - manageDirectory(); - } - uint32_t tempSize = archiveSize; - archiveSize = 0; - return tempSize; + manageDirectory(); + } + else + { + LOG_INFO << "DiskSpace is under range"; } - ArchiveSpaceManagerProps mProps; - uint32_t archiveSize = 0; - std::vector> foldVector; + uint64_t tempSize = archiveSize; + archiveSize = 0; + return tempSize; + } + ArchiveSpaceManagerProps mProps; + uint64_t archiveSize = 0; + std::vector> foldVector; }; ArchiveSpaceManager::ArchiveSpaceManager(ArchiveSpaceManagerProps _props) - :Module(SOURCE, "ArchiveSpaceManager", _props) -{ - mDetail.reset(new Detail(_props)); + :Module(SOURCE, "ArchiveSpaceManager", _props) + { + mDetail.reset(new Detail(_props)); } bool ArchiveSpaceManager::validateInputPins() @@ -138,17 +152,17 @@ bool ArchiveSpaceManager::validateInputOutputPins() void ArchiveSpaceManager::addInputPin(framemetadata_sp& metadata, string& pinId) { - Module::addInputPin(metadata, pinId); - Module::addOutputPin(metadata, pinId); + Module::addInputPin(metadata, pinId); + Module::addOutputPin(metadata, pinId); } bool ArchiveSpaceManager::init() { if (!Module::init()) { - return false; - } - return true; + return false; + } + return true; } bool ArchiveSpaceManager::term() @@ -158,31 +172,30 @@ bool ArchiveSpaceManager::term() ArchiveSpaceManagerProps ArchiveSpaceManager::getProps() { - return mDetail->mProps; + return mDetail->mProps; } void ArchiveSpaceManager::setProps(ArchiveSpaceManagerProps& props) { - Module::addPropsToQueue(props); + Module::addPropsToQueue(props); } bool ArchiveSpaceManager::handlePropsChange(frame_sp& frame) { - ArchiveSpaceManagerProps props(mDetail->mProps); - auto ret = Module::handlePropsChange(frame, props); - mDetail->setProps(props); - return ret; + ArchiveSpaceManagerProps props(mDetail->mProps); + auto ret = Module::handlePropsChange(frame, props); + mDetail->setProps(props); + return ret; } -bool ArchiveSpaceManager::process() -{ - try - { - finalArchiveSpace = mDetail->diskOperation(); - } - catch (...) - { - LOG_ERROR << "Archive Disk Manager encountered an error."; - } - return true; +bool ArchiveSpaceManager::produce() { + try + { + finalArchiveSpace = mDetail->diskOperation(); + } + catch (...) + { + LOG_ERROR << "Archive Disk Manager encountered an error."; + } + return true; }