Skip to content

Commit

Permalink
Implement memory manager for Tile allocation in Tramontana.
Browse files Browse the repository at this point in the history
* Tiles are removed as soon as they reach a null refcount *and* that
  their occurrence has been merge into an equipotential. Groundwork
  for windowed extraction.
  • Loading branch information
jpc-lip6 committed Aug 14, 2024
1 parent d923e48 commit ab23025
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 111 deletions.
8 changes: 4 additions & 4 deletions tramontana/src/QueryTiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ namespace Tramontana {
if (not component->getLayer()->getMask().intersect(layer->getMask())) continue;
Tile* tile = Tile::create( occurrence
, layer
, nullptr
, rootTile
, _sweepLine );
if (not rootTile) rootTile = tile;
else rootTile->merge( tile );
//else rootTile->merge( tile );
}

BasicLayer* cutLayer = component->getLayer()->getBasicLayers().getFirst();
Expand All @@ -94,11 +94,11 @@ namespace Tramontana {
for ( const BasicLayer* connexLayer : connexSet ) {
Tile* tile = Tile::create( occurrence
, connexLayer
, nullptr
, rootTile
, _sweepLine
, Tile::ForceLayer );
if (not rootTile) rootTile = tile;
else rootTile->merge( tile );
//else rootTile->merge( tile );
}
}

Expand Down
33 changes: 28 additions & 5 deletions tramontana/src/SweepLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ namespace Tramontana {
//DebugSession::open( 160, 169 );
cdebug_log(160,1) << "SweepLine::run()" << endl;
loadTiles();
Box ab = getCell()->getAbutmentBox();
Interval sweepSpan = Interval( ab.getXMin(), ab.getXMax() );
//bool debugOn = false;
//bool written = false;
size_t processedTiles = 0;
DbU::Unit xSweepLine = DbU::Min;
DbU::Unit xSweepLine = sweepSpan.getVMin();
for ( Element& element : _tiles ) {
processedTiles++;
Tile* tile = element.getTile();
Expand Down Expand Up @@ -148,6 +150,21 @@ namespace Tramontana {
// }
// }
// }

if (element.isLeftEdge()) {
if (tile->getLeftEdge() != xSweepLine) {
if (tty::enabled() and (xSweepLine != DbU::Min)) {
xSweepLine = tile->getLeftEdge();
DbU::Unit progress = ((xSweepLine - sweepSpan.getVMin()) * 100) / sweepSpan.getSize();
cmess2 << " <SweepLine @" << tty::bold
<< right << setw(12) << DbU::getValueString(xSweepLine,DbU::Physical) << " | "
<< right << setw( 3) << progress << "%"
<< tty::reset
<< setfill(' ') << tty::reset << ">" << tty::cr;
cmess2.flush ();
}
}
}

cdebug_log(160,1) << "X@ + " << DbU::getValueString(element.getX()) << " " << tile << endl;
auto intvTree = _intervalTrees.find( element.getMask() );
Expand Down Expand Up @@ -238,6 +255,7 @@ namespace Tramontana {
// if (tile->getOccurrence().getEntity()->getId() == 3348) {
// DebugSession::close();
// }
tile->decRefCount();
cdebug_tabw(160,-1);
}
//if (debugOn) DebugSession::close();
Expand All @@ -262,7 +280,7 @@ namespace Tramontana {
query.setBasicLayer( layer );
query.doQuery();
}
cmess2 << " - Loaded " << _tiles.size() << " tiles (from "
cmess2 << " - Loaded " << (_tiles.size()/2) << " tiles (from "
<< query.getGoMatchCount() << " gos)." << endl;

// for ( Occurrence occurrence : getCell()->getOccurrencesUnder( getCell()->getBoundingBox() ) ) {
Expand Down Expand Up @@ -294,14 +312,19 @@ namespace Tramontana {

void SweepLine::mergeEquipotentials ()
{
cout.flush();
cerr.flush();
//DebugSession::open( 160, 169 );
cdebug_log(160,1) << "SweepLine::mergeEquipotentials()" << endl;
//cerr << "SweepLine::mergeEquipotentials()" << endl;
Tile::timeTick();
for ( Tile* tile : Tile::getAllTiles() ) {
tile->getRoot( Tile::MergeEqui|Tile::MakeLeafEqui );
const vector<Tile*>& tiles = Tile::getAllTiles();
for ( size_t i=0 ; i<tiles.size() ; ++i ) {
if (not tiles[i]) continue;
tiles[i]->getRoot( Tile::Compress|Tile::MergeEqui|Tile::MakeLeafEqui );
Tile::destroyQueued();
}
cdebug_tabw(160,-1);
//Tile::showStats();
//DebugSession::close();
}

Expand Down
138 changes: 123 additions & 15 deletions tramontana/src/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ namespace Tramontana {
// Class : "Tramontana::Tile".


uint32_t Tile::_idCounter = 0;
uint32_t Tile::_time = 0;
vector<Tile*> Tile::_allocateds;
uint32_t Tile::_time = 0;
vector<Tile*> Tile::_allocateds;
vector<Tile*> Tile::_destroyQueue;
vector<size_t> Tile::_freeds;


Tile::Tile ( Occurrence occurrence
, Occurrence deepOccurrence
, const BasicLayer* layer
, const Box& boundingBox
, Tile* parent )
: _id (_idCounter++)
: _id (0)
, _refCount (0)
, _occurrence (occurrence)
, _deepOccurrence(deepOccurrence)
, _layer (layer)
Expand All @@ -106,14 +108,23 @@ namespace Tramontana {
, _rank (0)
, _timeStamp (0)
{
cdebug_log(165,0) << "Tile::Tile() " << this << endl;
_allocateds.push_back( this );
if (not _freeds.empty()) {
_id = _freeds.back();
_freeds.pop_back();
_allocateds[ _id ] = this;
} else {
_id = _allocateds.size();
_allocateds.push_back( this );
}
if (_parent) _parent->incRefCount();

if (occurrence.getPath().isEmpty()) {
if (not occurrence.getEntity()) {
cerr << Error( "Tile::Tile(): Cannot build on an empty Occurrence." ) << endl;
}
_flags |= TopLevel;
}
cdebug_log(165,0) << "Tile::Tile() " << this << endl;
}


Expand Down Expand Up @@ -203,14 +214,32 @@ namespace Tramontana {
Tile* tile = new Tile ( childEqui, occurrence, layer, bb, rootTile );
sweepLine->add( tile );

//cerr << "Tile::create() " << (void*)tile << ":" << tile << endl;

return tile;
}


void Tile::destroy ()
void Tile::queuedDestroy ()
{
cdebug_log(165,1) << "Tile::destroy() " << this << endl;
cdebug_tabw(165,-1);
if (isFreed()) return;
if (_parent) _parent->decRefCount();
_flags |= Freed;
cdebug_log(165,0) << "Tile::destroy() " << this << endl;
_destroyQueue.push_back( this );
}


void Tile::destroyQueued ()
{

for ( Tile* tile : _destroyQueue ) {
_allocateds[ tile->getId() ] = nullptr;
_freeds.push_back( tile->getId() );
//cerr << "Tile::destroyQueued() " << (void*)tile << ":" << tile << endl;
delete tile;
}
_destroyQueue.clear();
}


Expand All @@ -220,9 +249,58 @@ namespace Tramontana {

void Tile::deleteAllTiles ()
{
for ( Tile* tile : _allocateds) delete tile;
int64_t delCount = 0;
for ( Tile* tile : _allocateds) {
if (tile) delCount++;
delete tile;
}
int64_t delta = (int64_t)_allocateds.size() - (int64_t)_freeds.size() - delCount;
if (delta < 0) {
cerr << Error( "Tile::deleteAllTiles(): Delete more tiles than allocated, brace for a crash.\n"
" Has allocateds %lu, freed %lu in excess of %ld."
, _allocateds.size(), _freeds.size(), delCount-_freeds.size()
) << endl;
}
if (delta > 0) {
cerr << Error( "Tile::deleteAllTiles(): Delete less tiles than allocated, memory leak.\n"
" Has allocateds %lu, freed %lu short of %ld."
, _allocateds.size(), _freeds.size(), _freeds.size()-delCount
) << endl;
}
_allocateds.clear();
_idCounter = 0;
_freeds.clear();
}


void Tile::showStats ()
{
size_t roots = 0;
size_t childs = 0;
size_t mergedChilds = 0;
size_t nullRefCount = 0;
size_t nonFreeds = 0;
for ( Tile* tile : _allocateds ) {
if (not tile) continue;
if (tile->getParent()) {
childs++;
if (tile->isOccMerged())
mergedChilds++;
} else
roots++;
if (tile->getRefCount() == 0) {
nullRefCount++;
if (not tile->isFreed() and tile->isOccMerged())
nonFreeds++;
}
}
cerr << "\n o Tile statistics:" << endl;
cerr << Dots::asUInt(" - Roots" , roots ) << endl;
cerr << Dots::asUInt(" - Childs" , childs ) << endl;
cerr << Dots::asUInt(" - Merged childs" , mergedChilds ) << endl;
cerr << Dots::asUInt(" - Null refcount" , nullRefCount ) << endl;
cerr << Dots::asUInt(" - Non freeds" , nonFreeds ) << endl;
cerr << Dots::asUInt(" - Total allocateds", _allocateds.size()) << endl;
cerr << Dots::asUInt(" - Freed" , _freeds.size() ) << endl;
}


Expand All @@ -234,7 +312,7 @@ namespace Tramontana {
if ((flags & MakeLeafEqui) and not getEquipotential()) {
newEquipotential();
}
//cdebug_tabw(165,-1);
cdebug_tabw(165,-1);
return this;
}

Expand All @@ -251,11 +329,16 @@ namespace Tramontana {
// }
root = root->getParent();
cdebug_log(165,0) << "| parent " << root << endl;
if (root->isFreed())
cerr << Error( "Tile::getRoot(): On %s,\n"
" parent tile is already freed %s."
, getString(this).c_str()
, getString(root).c_str()
) << endl;
}
cdebug_log(165,0) << "> root " << root << " "
<< (root->getEquipotential() ? getString(root->getEquipotential()) : "equi=NULL") << endl;


if (flags & MergeEqui) {
Equipotential* rootEqui = root->getEquipotential();
if (not rootEqui) {
Expand All @@ -282,7 +365,7 @@ namespace Tramontana {
}
}

//cdebug_tabw(165,-1);
cdebug_tabw(165,-1);
return root;
}

Expand Down Expand Up @@ -342,6 +425,7 @@ namespace Tramontana {
<< " child tid:" << root2->getId() << endl;
// Fuse root2 into root1 here!

destroyQueued();
cdebug_tabw(160,-1);
return root1;
}
Expand Down Expand Up @@ -380,6 +464,24 @@ namespace Tramontana {
}


void Tile::check ( string tag ) const
{
if (_id != 205594) return;

cerr << tag << endl;
cerr << " Tile::check() " << this << endl;
size_t childCount = 0;
for ( const Tile* tile : _allocateds ) {
if (not tile) continue;
if (tile->getParent() and (tile->getParent() == this)) {
cerr << " | child " << tile << endl;
childCount++;
}
}
cerr << " childs:" << childCount << " vs. ref:" << _refCount << endl;
}


string Tile::_getTypeName () const
{ return "Tramontana::Tile"; }

Expand All @@ -388,7 +490,11 @@ namespace Tramontana {
{
ostringstream os;
os << "<Tile tid:" << _id
<< " " << (getEquipotential() ? "E" : "-")
<< " ref=" << _refCount
<< " " << (_parent ? "p" : "-")
<< (getEquipotential() ? "E" : "-")
<< (isOccMerged () ? "m" : "-")
<< (isFreed () ? "F" : "-")
<< " " << _boundingBox
<< " " << _layer->getName()
<< " " << _occurrence << ">";
Expand All @@ -400,6 +506,8 @@ namespace Tramontana {
{
Record* record = new Record ( _getString() );
if (record) {
record->add( getSlot( "_refCount" , _refCount ) );
record->add( getSlot( "_parent" , _parent ) );
record->add( getSlot( "_occurrence" , &_occurrence ) );
record->add( getSlot( "_layer" , _layer ) );
record->add( getSlot( "_boundingBox", &_boundingBox ) );
Expand Down
1 change: 1 addition & 0 deletions tramontana/src/tramontana/SweepLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ namespace Tramontana {

inline void SweepLine::add ( Tile* tile )
{
tile->incRefCount( 2 );
_tiles.push_back( Element( tile, Tile::LeftEdge ) );
_tiles.push_back( Element( tile, Tile::RightEdge ) );
}
Expand Down
Loading

0 comments on commit ab23025

Please sign in to comment.