Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform memory check on files before loading them in #2285

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tomviz/LoadDataReaction.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tomviz/LoadDataReaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions tomviz/SystemMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifdef _WIN32

#include <windows.h>

unsigned long long getAvailableSystemMemory()
{
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullAvailPhys;
}

#else

#include <unistd.h>

unsigned long long getAvailableSystemMemory()
{
long pages = sysconf(_SC_AVPHYS_PAGES);
long page_size = sysconf(_SC_PAGESIZE);
return pages * page_size;
}
#endif