diff --git a/base/include/ArchiveSpaceManager.h b/base/include/ArchiveSpaceManager.h index 2f5386bd2..9802a38a4 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(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!"; + 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(uint64_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); + } + } - - uint64_t lowerWaterMark; // Lower disk space - uint64_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(); - uint64_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 produce(); - 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 b6444083b..03009b0cf 100644 --- a/base/src/ArchiveSpaceManager.cpp +++ b/base/src/ArchiveSpaceManager.cpp @@ -21,118 +21,118 @@ class ArchiveSpaceManager::Detail } 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; + int sample = 0; + int inCount = 0; + int countFreq = 0; uint64_t tempSize = 0; - for (const auto& entry : boost::filesystem::recursive_directory_iterator(_dir)) - { + for (const auto& entry : boost::filesystem::recursive_directory_iterator(_dir)) + { if (boost::filesystem::is_regular_file(entry)) - { + { if (countFreq % mProps.samplingFreq == 0) - { + { sample = (rand() % mProps.samplingFreq); - inCount = 0; - } - if (inCount == sample) + inCount = 0; + } + if (inCount == sample) { try - { - tempSize = boost::filesystem::file_size(entry); - dirSize += tempSize * mProps.samplingFreq; + { + 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; } + } + inCount++; + countFreq++; + } } - inCount++; - countFreq++; - } - } if (inCount < mProps.samplingFreq && inCount > 0) - { + { dirSize += tempSize * inCount; - } + } std::cout << "Total Directory Size: " << dirSize << std::endl; - return dirSize; - } + return dirSize; + } - boost::filesystem::path getOldestDirectory(boost::filesystem::path _cam) - { - for (const auto& camFolder : boost::filesystem::directory_iterator(_cam)) + boost::filesystem::path getOldestDirectory(boost::filesystem::path _cam) { - for (const auto& folder : boost::filesystem::recursive_directory_iterator(camFolder)) - { - if (boost::filesystem::is_regular_file(folder)) + for (const auto& camFolder : boost::filesystem::directory_iterator(_cam)) { - boost::filesystem::path p = folder.path().parent_path(); - return p; + 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; + } + } } - } - } - return _cam; - }; + return _cam; + }; - void manageDirectory() - { - auto comparator = [](std::pair& a, std::pair& b) {return a.second < b.second; }; - while (archiveSize > mProps.lowerWaterMark) + void manageDirectory() { - 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) }); - } + 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) }); + } sort(foldVector.begin(), foldVector.end(), comparator); //Sorting the vector 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(); + 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 diskOperation() - { - archiveSize = estimateDirectorySize(mProps.pathToWatch); - if (archiveSize > mProps.upperWaterMark) { - manageDirectory(); - } + archiveSize = estimateDirectorySize(mProps.pathToWatch); + if (archiveSize > mProps.upperWaterMark) + { + manageDirectory(); + } else { LOG_INFO << "DiskSpace is under range"; } uint64_t tempSize = archiveSize; - archiveSize = 0; - return tempSize; - } - ArchiveSpaceManagerProps mProps; + 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() @@ -152,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() @@ -172,30 +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::produce() { - try - { - finalArchiveSpace = mDetail->diskOperation(); - } - catch (...) - { - LOG_ERROR << "Archive Disk Manager encountered an error."; - } - return true; + try + { + finalArchiveSpace = mDetail->diskOperation(); + } + catch (...) + { + LOG_ERROR << "Archive Disk Manager encountered an error."; + } + return true; }