[Kst] branches/work/kst/portto4/kst/src

Barth Netterfield netterfield at astro.utoronto.ca
Mon Aug 22 16:47:41 UTC 2011


SVN commit 1248918 by netterfield:

Add out of memory heuristics to change vector range dialog.
Not implemented in anything resembling an intelligent way under windows or mac.

Attempt to fix despike filter crash.
CCBUG: 280373 

should be tested still.


 M  +12 -18    libkst/datacollection.cpp  
 M  +2 -0      libkst/datacollection.h  
 M  +0 -1      libkst/scalar.cpp  
 M  +32 -0     libkstapp/changedatasampledialog.cpp  
 M  +2 -5      libkstapp/datawizard.cpp  
 M  +1 -1      libkstapp/memorywidget.cpp  
 M  +1 -0      libkstapp/plotitem.cpp  
 M  +10 -5     plugins/filters/despike/filterdespike.cpp  
 M  +6 -3      widgets/scalarselector.cpp  


--- branches/work/kst/portto4/kst/src/libkst/datacollection.cpp #1248917:1248918
@@ -30,34 +30,28 @@
 static QMutex bigLock;
 
 void *realloc(void *ptr, size_t size) {
-#ifdef __linux__
-  QMutexLocker ml(&bigLock);
-  meminfo();
-  unsigned long bFree = S(kb_main_free + kb_main_buffers + kb_main_cached);
-  if (size > bFree) {
-    const unsigned long sz = size;
-    qDebug("Tried to allocate too much memory! (Wanted %lu, had %lu)", sz, bFree);
-    return 0L;
-  }
-#endif
   return ::realloc(ptr, size);
 }
 
 void *malloc(size_t size) {
+  return ::malloc(size);
+}
+
+unsigned long Data::AvailableMemory() {
+  unsigned long available_memory = 1024*1024*1024;
+  // FIXME: under windows or mac, this is totally wrong.
+  // Under windows, try GlobalMemoryStatusEx
+  // (http://msdn.microsoft.com/en-us/library/aa366589)
+  // but, I don't have access to a windows devel environment...
+
 #ifdef __linux__
   QMutexLocker ml(&bigLock);
   meminfo();
-  unsigned long bFree = S(kb_main_free + kb_main_buffers + kb_main_cached);
-  if (size > bFree) {
-    const unsigned long sz = size;
-    qDebug("Tried to allocate too much memory! (Wanted %lu, had %lu)", sz, bFree);
-    return 0L;
-  }
+  available_memory = S(kb_main_free + kb_main_cached) - 30*1024*1024; // 30MB margin
 #endif
-  return ::malloc(size);
+  return available_memory;
 }
 
-
 Data *Data::_self = 0L;
 void Data::cleanup() {
     delete _self;
--- branches/work/kst/portto4/kst/src/libkst/datacollection.h #1248917:1248918
@@ -57,6 +57,8 @@
       * -1 if not in layout.
       */
     virtual int columns() const;
+
+    static unsigned long AvailableMemory();
 };
 
 /** Bad choice for location - maybe move it later */
--- branches/work/kst/portto4/kst/src/libkst/scalar.cpp #1248917:1248918
@@ -35,7 +35,6 @@
     : Primitive(store, 0L), _value(0.0), _orphan(false), _displayable(true), _editable(false) {
 
   _initializeShortName();
-
 }
 
 void Scalar::_initializeShortName() {
--- branches/work/kst/portto4/kst/src/libkstapp/changedatasampledialog.cpp #1248917:1248918
@@ -239,8 +239,40 @@
 void ChangeDataSampleDialog::apply() {
   _selectedVectorList->selectAll();
   QList<QListWidgetItem*> selectedItems = _selectedVectorList->selectedItems();
+
+  // see if we have enough memory
+  //FIXME: doesn't consider data objects that depend on this, and it should
+  //FIXME: doesn't work under windows or mac
+  long current_memory_used = 0;
+  long memory_needed = 0;
   for (int i = 0; i < selectedItems.size(); ++i) {
     if (DataVectorPtr vector = kst_cast<DataVector>(_store->retrieveObject(selectedItems.at(i)->text()))) {
+      current_memory_used += vector->length()*sizeof(double);
+      long ns=0;
+      if (_dataRange->readToEnd()) {
+        ns = vector->fileLength() - (int)_dataRange->start();
+      } else {
+        ns = (int)_dataRange->range();
+      }
+      if (_dataRange->doSkip()) {
+        ns/=_dataRange->skip();
+      } else {
+        ns *= vector->samplesPerFrame();
+      }
+      memory_needed += ns*sizeof(double);
+    }
+  }
+
+  long memory_available = Data::AvailableMemory();
+  if (memory_needed-current_memory_used > memory_available) {
+    //QApplication::restoreOverrideCursor();
+    QMessageBox::warning(this, i18n("Insufficient Memory"), i18n("You requested to read in %1 MB of data but it seems that you only have approximately %2 MB of usable memory available.  You cannot load this much data."
+                                                                 ).arg((memory_needed-current_memory_used)/(1024*1024)).arg(memory_available/(1024*1024)));
+    return;
+  }
+
+  for (int i = 0; i < selectedItems.size(); ++i) {
+    if (DataVectorPtr vector = kst_cast<DataVector>(_store->retrieveObject(selectedItems.at(i)->text()))) {
       vector->writeLock();
       int from = (_dataRange->countFromEnd() ? -1 : (int)_dataRange->start());
       int range = (_dataRange->readToEnd() ? -1 : (int)_dataRange->range());
--- branches/work/kst/portto4/kst/src/libkstapp/datawizard.cpp #1248917:1248918
@@ -776,12 +776,9 @@
   emit dataSourceLoaded(ds->fileName());
 
   // check for sufficient memory
-  unsigned long memoryRequested = 0, memoryAvailable = 1024*1024*1024; // 1GB
+  unsigned long memoryRequested = 0;
+  unsigned long memoryAvailable = Data::AvailableMemory();
   double frames;
-#ifdef __linux__
-  meminfo();
-  memoryAvailable = S(kb_main_free + kb_main_buffers + kb_main_cached);
-#endif
 
   ds->writeLock();
 
--- branches/work/kst/portto4/kst/src/libkstapp/memorywidget.cpp #1248917:1248918
@@ -34,7 +34,7 @@
 void MemoryWidget::updateFreeMemory() {
 #ifdef __linux__
   meminfo();
-  unsigned long mi = S(kb_main_free + kb_main_buffers + kb_main_cached);
+  unsigned long mi = S(kb_main_free + kb_main_cached);
   setText(tr("%1 MB available").arg(mi / (1024 * 1024)));
 #endif
 }
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #1248917:1248918
@@ -3509,6 +3509,7 @@
 PlotLabel::PlotLabel(PlotItem *plotItem) : QObject(),
   _plotItem(plotItem),
   _visible(true),
+  _fontScale(12),
   _fontUseGlobal(true),
   _isAuto(true) {
 
--- branches/work/kst/portto4/kst/src/plugins/filters/despike/filterdespike.cpp #1248917:1248918
@@ -36,7 +36,7 @@
       _scalarNSigma->setObjectStore(store);
       _scalarSpacing->setObjectStore(store);
       _scalarNSigma->setDefaultValue(5.0);
-      _scalarSpacing->setDefaultValue(1);
+      _scalarSpacing->setDefaultValue(1.0);
     }
 
     void setupSlots(QWidget* dialog) {
@@ -254,8 +254,8 @@
       }
     }
   }
-  // do a 2 point difference for lat dx points
-  for (i=N-dx; i<N; i++) {
+  // do a 2 point difference for last dx points
+  for (i=N-dx-1; i<N; i++) {
     if (fabs(inputVector->value(i-dx) - inputVector->value(i))>cut) {
       if (spike_start<0) { 
         spike_start = i-border;
@@ -278,6 +278,11 @@
       }
     }
   }
+  if (spike_start>=0) {
+    for (int j=spike_start; j<N; j++) {
+      outputVector->value()[j] = last_good;
+    }
+  }
 
   Kst::LabelInfo label_info = inputVector->labelInfo();
   label_info.name = i18n("Despiked %1").arg(label_info.name);
@@ -352,10 +357,10 @@
     FilterDespikeSource* object = store->createObject<FilterDespikeSource>();
 
     if (setupInputsOutputs) {
+      object->setInputScalar(SCALAR_SPACING_IN, config->selectedSpacingScalar());
+      object->setInputScalar(SCALAR_NSIGMA_IN, config->selectedNSigmaScalar());
       object->setupOutputs();
       object->setInputVector(VECTOR_IN, config->selectedVector());
-      object->setInputScalar(SCALAR_NSIGMA_IN, config->selectedNSigmaScalar());
-      object->setInputScalar(SCALAR_SPACING_IN, config->selectedSpacingScalar());
     }
 
     object->setPluginName(pluginName());
--- branches/work/kst/portto4/kst/src/widgets/scalarselector.cpp #1248917:1248918
@@ -115,15 +115,18 @@
 
     ScalarPtr scalar = _store->createObject<Scalar>();
     scalar->setValue(value);
-    scalar->setOrphan(true);
     scalar->setEditable(true);
     scalar->setDescriptiveName(QString());
+    scalar->setOrphan(false);
+
+    _scalar->clearEditText();
+    fillScalars();
+    scalar->setOrphan(true);
+
     scalar->writeLock();
     scalar->registerChange();
     scalar->unlock();
 
-    _scalar->clearEditText();
-    fillScalars();
     setSelectedScalar(scalar);
 
     return scalar;


More information about the Kst mailing list