diff --git a/tomviz/LoadDataReaction.cxx b/tomviz/LoadDataReaction.cxx index 9bb9c43fe..6572418e6 100644 --- a/tomviz/LoadDataReaction.cxx +++ b/tomviz/LoadDataReaction.cxx @@ -21,6 +21,7 @@ #include "PythonUtilities.h" #include "RAWFileReaderDialog.h" #include "RecentFilesMenu.h" +#include "SystemMemory.h" #include "TimeSeriesStep.h" #include "Utilities.h" #include "vtkOMETiffReader.h" @@ -227,6 +228,25 @@ DataSource* LoadDataReaction::loadData(const QString& fileName, return loadData(fileNames, val); } +unsigned long long estimateMemoryRequired(const QString& fileName) +{ + QFileInfo info(fileName); + // Perform different checks based upon the file type + auto ext = info.suffix().toLower(); + + // FIXME: compute estimation + return 1000; +} + +bool LoadDataReaction::checkMemoryUsage(const QString& fileName) +{ + auto requiredMemory = estimateMemoryRequired(fileName); + auto availMemory = getAvailableSystemMemory(); + + // FIXME: add check + return true; +} + DataSource* LoadDataReaction::loadData(const QStringList& fileNames, const QJsonObject& options) { @@ -243,6 +263,13 @@ DataSource* LoadDataReaction::loadData(const QStringList& fileNames, if (fileNames.size() > 0) { fileName = fileNames[0]; } + + if (!checkMemoryUsage(fileName)) { + // If it fails the memory check, it means the user was warned that they + // may not have enough memory, and they canceled the load. + return nullptr; + } + QFileInfo info(fileName); if (info.suffix().toLower() == "tvh5") { // Need to specify a path inside the tvh5 file to load diff --git a/tomviz/LoadDataReaction.h b/tomviz/LoadDataReaction.h index 02c55fce1..c3f6c3a66 100644 --- a/tomviz/LoadDataReaction.h +++ b/tomviz/LoadDataReaction.h @@ -45,6 +45,14 @@ class LoadDataReaction : public pqReaction static DataSource* loadData(const QString& fileName, const QJsonObject& options = QJsonObject()); + /// Approximate how much memory will be used by loading in the file, + /// compare that to what is available on the user's system, and warn the + /// user if they may run out of memory. If the warning appears, the user + /// may choose to cancel or proceed. If they cancel, this will return + /// `false`. If the warning doesn't appear or the user doesn't cancel, + /// this returns `true`. + static bool checkMemoryUsage(const QString& fileName); + /// Load data files from the specified locations, options can be used to pass /// additional parameters to the method, such as defaultModules, addToRecent, /// and child, or pvXML to pass to the ParaView reader. diff --git a/tomviz/SystemMemory.h b/tomviz/SystemMemory.h new file mode 100644 index 000000000..cc8357952 --- /dev/null +++ b/tomviz/SystemMemory.h @@ -0,0 +1,23 @@ +#ifdef _WIN32 + +#include + +unsigned long long getAvailableSystemMemory() +{ + MEMORYSTATUSEX status; + status.dwLength = sizeof(status); + GlobalMemoryStatusEx(&status); + return status.ullAvailPhys; +} + +#else + +#include + +unsigned long long getAvailableSystemMemory() +{ + long pages = sysconf(_SC_AVPHYS_PAGES); + long page_size = sysconf(_SC_PAGESIZE); + return pages * page_size; +} +#endif