From d05cbc552ab86fec25c73256c3709b809d182fb0 Mon Sep 17 00:00:00 2001 From: ouv Date: Thu, 30 Nov 2006 16:59:51 +0000 Subject: [PATCH] Cache improvement --- src/VISUGUI/VisuGUI_CacheDlg.cxx | 29 ++++++ src/VISUGUI/VisuGUI_CacheDlg.h | 3 + src/VISUGUI/VisuGUI_Slider.cxx | 11 ++- src/VISU_I/VISU_ColoredPrs3dCache_i.cc | 117 +++++++++++++++++++++++-- src/VISU_I/VISU_ColoredPrs3dCache_i.hh | 21 +++-- 5 files changed, 163 insertions(+), 18 deletions(-) diff --git a/src/VISUGUI/VisuGUI_CacheDlg.cxx b/src/VISUGUI/VisuGUI_CacheDlg.cxx index 5e0be2c2..a17535f2 100644 --- a/src/VISUGUI/VisuGUI_CacheDlg.cxx +++ b/src/VISUGUI/VisuGUI_CacheDlg.cxx @@ -84,6 +84,35 @@ VisuGUI_CacheDlg::VisuGUI_CacheDlg( VISU::ColoredPrs3dCache_var theCache, connect( myLimitedMemoryButton, SIGNAL( toggled( bool ) ), myLimitedMemory, SLOT( setEnabled( bool ) ) ); + // Current state + QGroupBox* aStateGroup = new QGroupBox( tr( "MEMORY STATE" ), this ); + aStateGroup->setColumnLayout( 0, Qt::Vertical ); + aStateGroup->layout()->setSpacing( 0 ); + aStateGroup->layout()->setMargin( 0 ); + + QGridLayout* aStateLayout = new QGridLayout( aStateGroup->layout(), 2, 2 ); + aStateLayout->setSpacing(6); + aStateLayout->setMargin(6); + + QLabel* aUsedMemoryLabel = new QLabel( tr( "USED_BY_CACHE" ), aStateGroup ); + myUsedMemory = new QLineEdit( aStateGroup ); + myUsedMemory->setText( QString::number( (double)myCache->GetMemorySize() ) + " Mb" ); + myUsedMemory->setReadOnly( true ); + myUsedMemory->setEnabled( false ); + myUsedMemory->setPaletteForegroundColor( Qt::black ); + + QLabel* aFreeMemoryLabel = new QLabel( tr( "FREE" ), aStateGroup ); + myFreeMemory = new QLineEdit( aStateGroup ); + myFreeMemory->setText( QString::number( aLimitedMemoryMax ) + " Mb" ); + myFreeMemory->setReadOnly( true ); + myFreeMemory->setEnabled( false ); + myFreeMemory->setPaletteForegroundColor( Qt::black ); + + aStateLayout->addWidget( aUsedMemoryLabel, 0, 0 ); + aStateLayout->addWidget( myUsedMemory, 0, 1 ); + aStateLayout->addWidget( aFreeMemoryLabel, 1, 0 ); + aStateLayout->addWidget( myFreeMemory, 1, 1 ); + // Ok / Cancel QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" ); GroupButtons->setColumnLayout(0, Qt::Vertical ); diff --git a/src/VISUGUI/VisuGUI_CacheDlg.h b/src/VISUGUI/VisuGUI_CacheDlg.h index f60521f8..71693e76 100644 --- a/src/VISUGUI/VisuGUI_CacheDlg.h +++ b/src/VISUGUI/VisuGUI_CacheDlg.h @@ -63,6 +63,9 @@ private: QRadioButton* myMimimalMemoryButton; QRadioButton* myLimitedMemoryButton; QtxDblSpinBox* myLimitedMemory; + + QLineEdit* myUsedMemory; + QLineEdit* myFreeMemory; }; #endif diff --git a/src/VISUGUI/VisuGUI_Slider.cxx b/src/VISUGUI/VisuGUI_Slider.cxx index 068783ea..1d191390 100644 --- a/src/VISUGUI/VisuGUI_Slider.cxx +++ b/src/VISUGUI/VisuGUI_Slider.cxx @@ -68,6 +68,8 @@ #include #include +#define SHOW_MEMORY_STATE + /*! Constructor */ @@ -102,6 +104,7 @@ VisuGUI_Slider::VisuGUI_Slider( VisuGUI_Module* theModule, aSliderLayout->addWidget( myFirstTimeStamp ); mySlider = new QSlider( aWidget ); + mySlider->setMinValue( 0 ); mySlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); mySlider->setOrientation( QSlider::Horizontal ); mySlider->setTracking( false ); @@ -160,6 +163,7 @@ VisuGUI_Slider::VisuGUI_Slider( VisuGUI_Module* theModule, aButtonsLayout->addItem( new QSpacerItem( 24, 24, QSizePolicy::Expanding, QSizePolicy::Minimum ) ); +#ifdef SHOW_MEMORY_STATE // Memory state QLabel* aCacheMemoryLabel = new QLabel( tr( "CACHE_MEMORY" ), aWidget ); aCacheMemoryLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); @@ -178,6 +182,7 @@ VisuGUI_Slider::VisuGUI_Slider( VisuGUI_Module* theModule, aButtonsLayout->addWidget( myFreeMemory ); aButtonsLayout->addItem( new QSpacerItem( 24, 24, QSizePolicy::Expanding, QSizePolicy::Minimum ) ); +#endif // Speed QLabel* aSpeedLabel = new QLabel( tr( "SPEED" ), aWidget ); @@ -262,9 +267,6 @@ void VisuGUI_Slider::enableControls( bool on ) VISU::ColoredPrs3dHolder::BasicInput_var anInput = aHolder->GetBasicInput(); CORBA::Long aTimeStampNumber = anInput->myTimeStampNumber; - mySlider->setMinValue( 0 ); - mySlider->setMaxValue( aLength-1 ); - myFirstTimeStamp->setText( aTimeStampsRange[0].myTime.in() ); myLastTimeStamp->setText( aTimeStampsRange[aLength-1].myTime.in() ); myTimeStampsNumber->setText( QString("(") + QString::number( aLength ) + ")" ); @@ -291,6 +293,7 @@ void VisuGUI_Slider::enableControls( bool on ) myTimeStampStrings->setCurrentItem( a_current_index ); myTimeStampIndices->setCurrentItem( a_current_index ); + mySlider->setMaxValue( aLength-1 ); mySlider->setValue( a_current_index ); } else @@ -302,6 +305,7 @@ void VisuGUI_Slider::enableControls( bool on ) void VisuGUI_Slider::updateMemoryState() { +#ifdef SHOW_MEMORY_STATE if( checkHolderList() ) return; @@ -314,6 +318,7 @@ void VisuGUI_Slider::updateMemoryState() long aMb = 1024 * 1024; double aFreeMemory = (double)VISU_PipeLine::GetAvailableMemory( 2048 * aMb ) / (double)aMb; myFreeMemory->setText( QString::number( aFreeMemory ) + " Mb" ); +#endif } void VisuGUI_Slider::onSelectionChanged() diff --git a/src/VISU_I/VISU_ColoredPrs3dCache_i.cc b/src/VISU_I/VISU_ColoredPrs3dCache_i.cc index 3e7e61da..c1af621f 100644 --- a/src/VISU_I/VISU_ColoredPrs3dCache_i.cc +++ b/src/VISU_I/VISU_ColoredPrs3dCache_i.cc @@ -33,6 +33,9 @@ #include "VISU_View_i.hh" #include "VISU_Actor.h" +#include "VTKViewer_Algorithm.h" +#include "SVTK_Functor.h" + #include "VVTK_ViewWindow.h" #include "SUIT_ResourceMgr.h" @@ -111,7 +114,8 @@ VISU::ColoredPrs3dCache_i int VISU::ColoredPrs3dCache_i ::IsPossible(VISU::VISUType theType, - const VISU::ColoredPrs3dHolder::BasicInput& theInput) + const VISU::ColoredPrs3dHolder::BasicInput& theInput, + float& theMemoryToClear) { //cout << "VISU::ColoredPrs3dCache_i::IsPossible " << endl; size_t aMemory = VISU::CheckIsPossible(theType, theInput, true); @@ -122,7 +126,11 @@ VISU::ColoredPrs3dCache_i CORBA::Float aMemoryLimit = GetLimitedMemory(); CORBA::Float aMemoryNeeded = CORBA::Float(aMemory/(1024.0*1024.0)); // convert to Mb //cout<<"Memory needed: "< 0; @@ -163,9 +171,14 @@ VISU::ColoredPrs3dCache_i ::CreateHolder(VISU::VISUType theType, const VISU::ColoredPrs3dHolder::BasicInput& theInput) { - if(IsPossible(theType, theInput)) + float aMemoryToClear = 0.; + if(IsPossible(theType, theInput, aMemoryToClear)) { VISU::ColoredPrs3dHolder_i* aHolder = new VISU::ColoredPrs3dHolder_i(theType, theInput, *this); + + if( aMemoryToClear > 1 / VTK_LARGE_FLOAT ) + ClearMemory( aMemoryToClear, aHolder ); + VISU::ColoredPrs3d_i* aColoredPrs3d = CreateColoredPrs3d(theType, theInput); std::string aComment = std::string("myComment=") + aColoredPrs3d->GetComment(); std::string aName = aColoredPrs3d->GenerateName().latin1(); @@ -206,6 +219,7 @@ void VISU::ColoredPrs3dCache_i ::SetLimitedMemory(CORBA::Float theMemorySize) { + ClearCache(theMemorySize); myLimitedMemory = theMemorySize; } @@ -351,10 +365,13 @@ VISU::ColoredPrs3dCache_i TLastVisitedPrsList& aLastVisitedPrsList = GetLastVisitedPrsList(theHolder); TPrs3dPtr aPrs3d = FindPrsByInput(aLastVisitedPrsList, theInput); bool anIsCheckPossible = GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED; + float aMemoryToClear = 0.; if(aPrs3d.GetPointer()){ aLastVisitedPrsList.push_front(aPrs3d); //cout << "FindPrsByInput " << aPrs3d; - }else if(anIsCheckPossible && IsPossible(theHolder->GetPrsType(), theInput)){ + }else if(anIsCheckPossible && IsPossible(theHolder->GetPrsType(), theInput, aMemoryToClear)){ + if( aMemoryToClear > 1 / VTK_LARGE_FLOAT ) + ClearMemory(aMemoryToClear, theHolder); aPrs3d = CreatePrs(theHolder->GetPrsType(), theInput, theHolder); //cout << "Created " << aPrs3d; }else{ @@ -413,19 +430,101 @@ VISU::ColoredPrs3dCache_i //---------------------------------------------------------------------------- void VISU::ColoredPrs3dCache_i -::ClearCache() +::ClearCache(float theMemory) +{ + //cout << "VISU::ColoredPrs3dCache_i::ClearCache( " << theMemory << " )" << endl; + + float aCurrentMemory = GetMemorySize(); + ClearMemory( aCurrentMemory - theMemory, NULL ); +} + + +//---------------------------------------------------------------------------- +void +VISU::ColoredPrs3dCache_i +::ClearMemory(float theMemory, VISU::ColoredPrs3dHolder_i* theActiveHolder) +{ + if(MYDEBUG) cout << "VISU::ColoredPrs3dCache_i::ClearMemory( " << theMemory << " )" << endl; + + if( theMemory < 1 / VTK_LARGE_FLOAT ) + return; + + if(MYDEBUG) PrintCache(); + + TColoredPrs3dHolderMap::iterator aHolderIter = myHolderMap.begin(); + TColoredPrs3dHolderMap::iterator aHolderIterEnd = myHolderMap.end(); + + int anIteration = 0; + for( ; aHolderIter != aHolderIterEnd; aHolderIter++ ) + { + TLastVisitedPrsList& aPrsList = aHolderIter->second; + anIteration = QMAX( aPrsList.size() - 1, anIteration ); + } + if(MYDEBUG) cout << "Iteration - " << anIteration << endl; + + std::string anEntry; + if( theActiveHolder ) + anEntry = theActiveHolder->GetEntry(); + + float aMemory, aGatheredMemory = 0.; + while( anIteration > 0 ) + { + for( aHolderIter = myHolderMap.begin(); aHolderIter != aHolderIterEnd; aHolderIter++ ) + { + //if( aHolderIter->first == anEntry ) + //continue; + + TLastVisitedPrsList& aPrsList = aHolderIter->second; + if( anIteration < aPrsList.size() ) + { + TPrs3dPtr aPrs3d = aPrsList[anIteration]; + if(MYDEBUG) cout << aPrs3d.GetPointer() << " "; + if( !aPrs3d.GetPointer() ) + continue; + + aMemory = aPrs3d->GetMemorySize(); + aGatheredMemory += aMemory; + if(MYDEBUG) cout << aMemory << " >> " << aGatheredMemory; + + aPrsList.pop_back(); + + if( aGatheredMemory > theMemory ) + { + if(MYDEBUG) cout << " STOP" << endl; + goto stop; + } + if(MYDEBUG) cout << endl; + } + } + + anIteration--; + } + + stop: + if(MYDEBUG) PrintCache(); +} + + +//---------------------------------------------------------------------------- +void +VISU::ColoredPrs3dCache_i +::PrintCache() { + cout << "VISU::ColoredPrs3dCache_i::PrintCache()" << endl; + + cout << "Cache memory - " << GetMemorySize() << " Mb" << endl; TColoredPrs3dHolderMap::const_iterator aHolderIter = myHolderMap.begin(); TColoredPrs3dHolderMap::const_iterator aHolderIterEnd = myHolderMap.end(); for(; aHolderIter != aHolderIterEnd; aHolderIter++){ const TLastVisitedPrsList& aPrsList = aHolderIter->second; TLastVisitedPrsList::const_iterator aPrsIter = aPrsList.begin(); TLastVisitedPrsList::const_iterator aPrsIterEnd = aPrsList.end(); - for(; aPrsIter != aPrsIterEnd; aPrsIter++){ + + cout << "Holder - " << aHolderIter->first.c_str() << endl; + cout << "Size - " << aPrsList.size() << endl; + for(aPrsIter++; aPrsIter != aPrsIterEnd; aPrsIter++) if(TPrs3dPtr aPrs3d = *aPrsIter) - { - } - } + cout << aPrs3d.GetPointer() << " (" << aPrs3d->GetMemorySize() << " Mb)"<< endl; } } diff --git a/src/VISU_I/VISU_ColoredPrs3dCache_i.hh b/src/VISU_I/VISU_ColoredPrs3dCache_i.hh index ae7970d4..45f0ebc5 100644 --- a/src/VISU_I/VISU_ColoredPrs3dCache_i.hh +++ b/src/VISU_I/VISU_ColoredPrs3dCache_i.hh @@ -160,22 +160,31 @@ namespace VISU const VISU::ColoredPrs3dHolder::BasicInput& theInput, VISU::View3D_ptr theView3D); - // Unregister the holder instance from cache + //! Unregister the holder instance from cache void RemoveHolder(VISU::ColoredPrs3dHolder_i* theHolder); - - void - ClearCache(); - + protected: //---------------------------------------------------------------------------- virtual int IsPossible(VISU::VISUType theType, - const VISU::ColoredPrs3dHolder::BasicInput& theInput); + const VISU::ColoredPrs3dHolder::BasicInput& theInput, + float& theMemoryToClear); TPrs3dPtr FindPrsByInput(TLastVisitedPrsList& theLastVisitedPrsList, const VISU::ColoredPrs3dHolder::BasicInput& theInput); + //---------------------------------------------------------------------------- + void + ClearCache(float theMemory = 0); + + void + ClearMemory(float theMemory, + VISU::ColoredPrs3dHolder_i* theActiveHolder); + + void + PrintCache(); + private: CORBA::Float myLimitedMemory; VISU::ColoredPrs3dCache::MemoryMode myMemoryMode; -- 2.39.2