From f1c6a8ca0511c623a16bb804ed2c5b6c26c83d78 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 8 Jul 2009 01:03:15 +0200 Subject: [contrib/mmap] implements a theoretical dynamic growing mmap based on Michael Vogts patch in #195018 this commit implements the use of mmap (as preferred) instead of a static char array. In theory this made it possible to grow the mmap as needed, but as it is currently impossible to move the mmap around in the memory the grow is likely to fail but it improve the memory usage a bit, so it is not totally useless for now - and maybe we can adjust the pointers in the future... --- apt-pkg/contrib/mmap.cc | 84 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 21 deletions(-) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 04a45811b..073bc9eb3 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -13,11 +13,6 @@ libc6 generates warnings -- which should be errors, g++ isn't properly strict. - The configure test notes that some OS's have broken private mmap's - so on those OS's we can't use mmap. This means we have to use - configure to test mmap and can't rely on the POSIX - _POSIX_MAPPED_FILES test. - ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ @@ -166,13 +161,23 @@ DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) /*}}}*/ // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/ // --------------------------------------------------------------------- -/* This is just a fancy malloc really.. */ +/* We try here to use mmap to reserve some space - this is much more + cooler than the fallback solution to simply allocate a char array + and could come in handy later than we are able to grow such an mmap */ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) : MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace) { if (_error->PendingError() == true) return; - + +#ifdef _POSIX_MAPPED_FILES + // use anonymous mmap() to get the memory + Base = (unsigned char*) mmap(0, WorkSpace, PROT_READ|PROT_WRITE, + MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + if(Base != MAP_FAILED) + return; +#endif + // fallback to a static allocated space Base = new unsigned char[WorkSpace]; memset(Base,0,WorkSpace); iSize = 0; @@ -185,7 +190,11 @@ DynamicMMap::~DynamicMMap() { if (Fd == 0) { +#ifdef _POSIX_MAPPED_FILES + munmap(Base, WorkSpace); +#else delete [] (unsigned char *)Base; +#endif return; } @@ -207,14 +216,16 @@ unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) iSize = Result + Size; - // Just in case error check - if (Result + Size > WorkSpace) + // try to grow the buffer + while(Result + Size > WorkSpace) { - _error->Error(_("Dynamic MMap ran out of room. Please increase the size " - "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); - return 0; + if(!Grow()) + { + _error->Error(_("Dynamic MMap ran out of room. Please increase the size " + "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); + return 0; + } } - return Result; } /*}}}*/ @@ -234,7 +245,6 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) if (I->ItemSize == ItemSize) break; } - // No pool is allocated, use an unallocated one if (I == Pools + PoolCount) { @@ -270,14 +280,17 @@ unsigned long DynamicMMap::WriteString(const char *String, unsigned long Len) { unsigned long Result = iSize; - // Just in case error check - if (Result + Len > WorkSpace) + // try to grow the buffer + while(Result + Len > WorkSpace) { - _error->Error(_("Dynamic MMap ran out of room. Please increase the size " - "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); - return 0; - } - + if(!Grow()) + { + _error->Error(_("Dynamic MMap ran out of room. Please increase the size " + "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); + return 0; + } + } + if (Len == (unsigned long)-1) Len = strlen(String); iSize += Len + 1; @@ -286,3 +299,32 @@ unsigned long DynamicMMap::WriteString(const char *String, return Result; } /*}}}*/ +// DynamicMMap::Grow - Grow the mmap /*{{{*/ +// --------------------------------------------------------------------- +/* This method will try to grow the mmap we currently use. This doesn't + work most of the time because we can't move the mmap around in the + memory for now as this would require to adjust quite a lot of pointers + but why we should not at least try to grow it before we give up? */ +bool DynamicMMap::Grow() +{ +#ifdef _POSIX_MAPPED_FILES + unsigned long newSize = WorkSpace + 1024*1024; + + if(Fd != 0) + { + Fd->Seek(newSize - 1); + char C = 0; + Fd->Write(&C,sizeof(C)); + } + + Base = mremap(Base, WorkSpace, newSize, 0); + if(Base == MAP_FAILED) + return false; + + WorkSpace = newSize; + return true; +#else + return false; +#endif +} + /*}}}*/ -- cgit v1.2.3 From 13eb93fcb6a03afd8fc05fa2b4c60046473e8507 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 8 Jul 2009 01:11:16 +0200 Subject: add a segfault handler to MMap to show the Cache-Limit message, which can be deactivated with MMap::SegfaultHandler=false (Closes: 535218) --- apt-pkg/contrib/mmap.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 073bc9eb3..5f56178f4 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -19,6 +19,7 @@ #define _BSD_SOURCE #include #include +#include #include @@ -26,6 +27,8 @@ #include #include #include +#include +#include #include /*}}}*/ @@ -136,6 +139,20 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) } /*}}}*/ +// DynamicMMapSegfaultHandler /*{{{*/ +// --------------------------------------------------------------------- +/* In theory, the mmap should never segfault because we check the available + size of our mmap before we use it, but there are a few reports out there + which state that the mmap segfaults without further notice. So this handler + will take care of all these segfaults which should never happen... */ +void DynamicMMapSegfaultHandler(int) +{ + _error->Error(_("Dynamic MMap segfaults, most likely because it ran out of room. " + "Please increase the size of APT::Cache-Limit. (man 5 apt.conf)")); + _error->DumpErrors(); + exit(EXIT_FAILURE); +} + /*}}}*/ // DynamicMMap::DynamicMMap - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -170,6 +187,13 @@ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) : if (_error->PendingError() == true) return; + if (_config->FindB("MMap::SegfaultHandler",true) == true) + { + struct sigaction sa; + sa.sa_handler = DynamicMMapSegfaultHandler; + sigaction(SIGSEGV, &sa, NULL); + } + #ifdef _POSIX_MAPPED_FILES // use anonymous mmap() to get the memory Base = (unsigned char*) mmap(0, WorkSpace, PROT_READ|PROT_WRITE, -- cgit v1.2.3 From c5f44afc2446d738e30ea4c6021d4b60915546b1 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 21 Jul 2009 16:54:28 +0200 Subject: eliminate (hopefully all) segfaults in pkgcachegen.cc and mmap.cc which can arise if cache doesn't fit into the mmap (Closes: #535218) This removes also the previously introduced SegfaultSignalHandler: The handler works, but is ugly by design... --- apt-pkg/contrib/mmap.cc | 68 +++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 45 deletions(-) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 5f56178f4..ba4482131 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -19,7 +19,6 @@ #define _BSD_SOURCE #include #include -#include #include @@ -28,7 +27,6 @@ #include #include #include -#include #include /*}}}*/ @@ -139,24 +137,11 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) } /*}}}*/ -// DynamicMMapSegfaultHandler /*{{{*/ -// --------------------------------------------------------------------- -/* In theory, the mmap should never segfault because we check the available - size of our mmap before we use it, but there are a few reports out there - which state that the mmap segfaults without further notice. So this handler - will take care of all these segfaults which should never happen... */ -void DynamicMMapSegfaultHandler(int) -{ - _error->Error(_("Dynamic MMap segfaults, most likely because it ran out of room. " - "Please increase the size of APT::Cache-Limit. (man 5 apt.conf)")); - _error->DumpErrors(); - exit(EXIT_FAILURE); -} /*}}}*/ // DynamicMMap::DynamicMMap - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ -DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) : +DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) : MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(WorkSpace) { if (_error->PendingError() == true) @@ -187,13 +172,6 @@ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) : if (_error->PendingError() == true) return; - if (_config->FindB("MMap::SegfaultHandler",true) == true) - { - struct sigaction sa; - sa.sa_handler = DynamicMMapSegfaultHandler; - sigaction(SIGSEGV, &sa, NULL); - } - #ifdef _POSIX_MAPPED_FILES // use anonymous mmap() to get the memory Base = (unsigned char*) mmap(0, WorkSpace, PROT_READ|PROT_WRITE, @@ -237,9 +215,9 @@ unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) unsigned long Result = iSize; if (Aln != 0) Result += Aln - (iSize%Aln); - + iSize = Result + Size; - + // try to grow the buffer while(Result + Size > WorkSpace) { @@ -258,7 +236,7 @@ unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) /* This allocates an Item of size ItemSize so that it is aligned to its size in the file. */ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) -{ +{ // Look for a matching pool entry Pool *I; Pool *Empty = 0; @@ -283,17 +261,24 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) I->ItemSize = ItemSize; I->Count = 0; } - + + unsigned long Result = 0; // Out of space, allocate some more if (I->Count == 0) { - I->Count = 20*1024/ItemSize; - I->Start = RawAllocate(I->Count*ItemSize,ItemSize); - } + const unsigned long size = 20*1024; + I->Count = size/ItemSize; + Result = RawAllocate(size,ItemSize); + // Does the allocation failed ? + if (Result == 0 && _error->PendingError()) + return 0; + I->Start = Result; + } + else + Result = I->Start; I->Count--; - unsigned long Result = I->Start; - I->Start += ItemSize; + I->Start += ItemSize; return Result/ItemSize; } /*}}}*/ @@ -303,21 +288,14 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) unsigned long DynamicMMap::WriteString(const char *String, unsigned long Len) { - unsigned long Result = iSize; - // try to grow the buffer - while(Result + Len > WorkSpace) - { - if(!Grow()) - { - _error->Error(_("Dynamic MMap ran out of room. Please increase the size " - "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); - return 0; - } - } - if (Len == (unsigned long)-1) Len = strlen(String); - iSize += Len + 1; + + unsigned long Result = RawAllocate(Len+1,0); + + if (Result == 0 && _error->PendingError()) + return 0; + memcpy((char *)Base + Result,String,Len); ((char *)Base)[Result + Len] = 0; return Result; -- cgit v1.2.3 From eb162ff79b93ea98380f4555e0fe3116993241fb Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 23 Jul 2009 14:05:41 +0200 Subject: [apt-pkg] yet another bit of mmap and pkgcachegen housekeeping * add mmap error message also to the dynamic mmap * remove some more {Ver,Desc} == 0 checks in for loops * try to respect the given flags to the dynamic mmap * open cached caches not as ReadOnly and not as Shared, so we always have a copy of the cache in the memory we can modify (e.g. set the hold state on-the-fly) --- apt-pkg/contrib/mmap.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index ba4482131..aa52b4c30 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -137,7 +137,6 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) } /*}}}*/ - /*}}}*/ // DynamicMMap::DynamicMMap - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -173,15 +172,24 @@ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) : return; #ifdef _POSIX_MAPPED_FILES + // Set the permissions. + int Prot = PROT_READ; + int Map = MAP_PRIVATE | MAP_ANONYMOUS; + if ((Flags & ReadOnly) != ReadOnly) + Prot |= PROT_WRITE; + if ((Flags & Public) == Public) + Map = MAP_SHARED | MAP_ANONYMOUS; + // use anonymous mmap() to get the memory - Base = (unsigned char*) mmap(0, WorkSpace, PROT_READ|PROT_WRITE, - MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - if(Base != MAP_FAILED) - return; -#endif + Base = (unsigned char*) mmap(0, WorkSpace, Prot, Map, -1, 0); + + if(Base == MAP_FAILED) + _error->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace); +#else // fallback to a static allocated space Base = new unsigned char[WorkSpace]; memset(Base,0,WorkSpace); +#endif iSize = 0; } /*}}}*/ -- cgit v1.2.3 From a67de73e4b66d67eeafe5d8c1f7fd5b031faeb3f Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 26 Jul 2009 01:11:53 +0200 Subject: merge with lp:apt/debian-sid Remove a bug (= an evil amok running if) introduced by the merge in 1817 which cause a segfault in the destructor for the dynamic mmap. --- apt-pkg/contrib/mmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib/mmap.cc') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 917466c2f..aa52b4c30 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -201,7 +201,7 @@ DynamicMMap::~DynamicMMap() if (Fd == 0) { #ifdef _POSIX_MAPPED_FILES - if(munmap(Base, WorkSpace) < 0) + munmap(Base, WorkSpace); #else delete [] (unsigned char *)Base; #endif -- cgit v1.2.3