Skip to content

Commit

Permalink
Call MarkAndSweep only manually in apt-get for autoremove
Browse files Browse the repository at this point in the history
An interactive tool like aptitude needs these flags current far more
often than we do as a user can see them in apt only in one very well
defined place – the autoremove display block – so we don't need to run
it up to four times while a normal "apt install" is processed as that is
just busywork.

The effect on runtime is minimal, as a single run doesn't take too long
anyhow, but it cuts down tremendously on debug output at the expense of
requiring some manual handholding.

This is opt-in so that aptitude doesn't need to change nor do we need to
change our own tools like "apt list" where it is working correctly as
intended.

A special flag and co is needed as we want to prevent the ActionGroup
inside pkgDepCache::Init to be inhibited already so we need to insert
ourselves while the DepCache is still in the process of being built.
This is also the reason why the debug output in some tests changed to
all unmarked, but that is fine as the marking could have been already
obsoleted by the actions taken, just inhibited by a proper action group.
  • Loading branch information
DonKult committed Apr 26, 2021
1 parent 9a54e70 commit d6f3458
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 45 deletions.
7 changes: 7 additions & 0 deletions apt-pkg/cachefile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
struct pkgCacheFile::Private
{
bool WithLock = false;
bool InhibitActionGroups = false;
};

// CacheFile::CacheFile - Constructor /*{{{*/
Expand Down Expand Up @@ -199,6 +200,8 @@ bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
DCache.reset(new pkgDepCache(Cache,Policy));
if (_error->PendingError() == true)
return false;
if (d->InhibitActionGroups)
DCache->IncreaseActionGroupLevel();
if (DCache->Init(Progress) == false)
return false;

Expand Down Expand Up @@ -376,3 +379,7 @@ void pkgCacheFile::Close()
SrcList = NULL;
}
/*}}}*/
void pkgCacheFile::InhibitActionGroups(bool const yes)
{
d->InhibitActionGroups = yes;
}
2 changes: 2 additions & 0 deletions apt-pkg/cachefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class APT_PUBLIC pkgCacheFile
void Close();

bool AddIndexFile(pkgIndexFile * const File);
// Starts DepCache with a claim of one ActionGroup already active
void InhibitActionGroups(bool yes);

inline pkgCache* GetPkgCache() { BuildCaches(NULL, false); return Cache; };
inline pkgDepCache* GetDepCache() { BuildDepCache(); return DCache; };
Expand Down
34 changes: 19 additions & 15 deletions apt-pkg/depcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,34 @@ ConfigValueInSubTree(const char* SubTree, const char *needle)
pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) : /*{{{*/
d(NULL), cache(cache), released(false)
{
++cache.group_level;
cache.IncreaseActionGroupLevel();
}

void pkgDepCache::ActionGroup::release()
{
if(!released)
{
if(cache.group_level == 0)
std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
else
{
--cache.group_level;

if(cache.group_level == 0)
cache.MarkAndSweep();
}

released = true;
}
if(released)
return;
released = true;
if (cache.DecreaseActionGroupLevel() == 0)
cache.MarkAndSweep();
}

pkgDepCache::ActionGroup::~ActionGroup()
{
release();
}
int pkgDepCache::IncreaseActionGroupLevel()
{
return ++group_level;
}
int pkgDepCache::DecreaseActionGroupLevel()
{
if(group_level == 0)
{
std::cerr << "W: Unbalanced action groups, expect badness\n";
return -1;
}
return --group_level;
}
/*}}}*/
// DepCache::pkgDepCache - Constructors /*{{{*/
Expand Down
5 changes: 4 additions & 1 deletion apt-pkg/depcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ class APT_PUBLIC pkgDepCache : protected pkgCache::Namespace
int group_level;

friend class ActionGroup;

public:
int IncreaseActionGroupLevel();
int DecreaseActionGroupLevel();

protected:

// State information
Expand Down
21 changes: 15 additions & 6 deletions apt-private/private-install.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ bool CheckNothingBroken(CacheFile &Cache) /*{{{*/
// ---------------------------------------------------------------------
/* This displays the informative messages describing what is going to
happen and then calls the download routines */
class SimulateWithActionGroupInhibited : public pkgSimulate
{
public:
SimulateWithActionGroupInhibited(CacheFile &Cache) : pkgSimulate(Cache) { Sim.IncreaseActionGroupLevel(); }
SimulateWithActionGroupInhibited(SimulateWithActionGroupInhibited const &Cache) = delete;
SimulateWithActionGroupInhibited(SimulateWithActionGroupInhibited &&Cache) = delete;
SimulateWithActionGroupInhibited& operator=(SimulateWithActionGroupInhibited const &Cache) = delete;
SimulateWithActionGroupInhibited& operator=(SimulateWithActionGroupInhibited &&Cache) = delete;
~SimulateWithActionGroupInhibited() = default;
};
static void RemoveDownloadNeedingItemsFromFetcher(pkgAcquire &Fetcher, bool &Transient)
{
for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I < Fetcher.ItemsEnd();)
Expand Down Expand Up @@ -208,7 +218,7 @@ bool InstallPackages(CacheFile &Cache, bool ShwKept, bool Ask, bool Safety, std:
// Run the simulator ..
if (_config->FindB("APT::Get::Simulate") == true)
{
pkgSimulate PM(Cache);
SimulateWithActionGroupInhibited PM(Cache);

APT::Progress::PackageManager *progress = APT::Progress::PackageManagerProgressFactory();
pkgPackageManager::OrderResult Res = PM.DoInstall(progress);
Expand Down Expand Up @@ -420,7 +430,6 @@ bool DoAutomaticRemove(CacheFile &Cache)
kernelAutoremovalMatcher = APT::KernelAutoRemoveHelper::GetProtectedKernelsFilter(Cache, true);
}

pkgDepCache::ActionGroup group(*Cache);
if(Debug)
std::cout << "DoAutomaticRemove()" << std::endl;

Expand All @@ -431,6 +440,7 @@ bool DoAutomaticRemove(CacheFile &Cache)
"AutoRemover") << std::endl;
return false;
}
Cache->MarkAndSweep();

bool purgePkgs = _config->FindB("APT::Get::Purge", false);
bool smallList = (hideAutoRemove == false &&
Expand Down Expand Up @@ -529,8 +539,6 @@ bool DoAutomaticRemove(CacheFile &Cache)
}
} while (Changed == true);
}
// trigger marking now so that the package list below is correct
group.release();

// Now see if we had destroyed anything (if we had done anything)
if (Cache->BrokenCount() != 0)
Expand All @@ -550,6 +558,8 @@ bool DoAutomaticRemove(CacheFile &Cache)
{
if (smallList == false)
{
// trigger marking now so that the package list is correct
Cache->MarkAndSweep();
SortedPackageUniverse Universe(Cache);
ShowList(c1out, P_("The following package was automatically installed and is no longer required:",
"The following packages were automatically installed and are no longer required:",
Expand Down Expand Up @@ -664,9 +674,7 @@ bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, std::vector<PseudoPkg
TryToInstall InstallAction(Cache, Fix.get(), BrokenFix);
TryToRemove RemoveAction(Cache, Fix.get());

// new scope for the ActionGroup
{
pkgDepCache::ActionGroup group(Cache);
unsigned short const order[] = { MOD_REMOVE, MOD_INSTALL, 0 };

for (unsigned short i = 0; order[i] != 0; ++i)
Expand Down Expand Up @@ -828,6 +836,7 @@ struct PkgIsExtraInstalled {
bool DoInstall(CommandLine &CmdL)
{
CacheFile Cache;
Cache.InhibitActionGroups(true);
auto VolatileCmdL = GetPseudoPackages(Cache.GetSourceList(), CmdL, AddVolatileBinaryFile, "");

// then open the cache
Expand Down
3 changes: 1 addition & 2 deletions apt-private/private-source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ bool DoBuildDep(CommandLine &CmdL)
std::string const pseudoArch = hostArch.empty() ? nativeArch : hostArch;

CacheFile Cache;
Cache.InhibitActionGroups(true);
auto VolatileCmdL = GetPseudoPackages(Cache.GetSourceList(), CmdL, AddVolatileSourceFile, pseudoArch);
auto AreDoingSatisfy = strcasecmp(CmdL.FileList[0], "satisfy") == 0;

Expand Down Expand Up @@ -820,7 +821,6 @@ bool DoBuildDep(CommandLine &CmdL)

APT::PackageVector removeAgain;
{
pkgDepCache::ActionGroup group(Cache);
TryToInstall InstallAction(Cache, &Fix, false);
std::list<std::pair<pkgCache::VerIterator, std::string>> candSwitch;
for (auto const &pkg: pseudoPkgs)
Expand Down Expand Up @@ -856,7 +856,6 @@ bool DoBuildDep(CommandLine &CmdL)
return false;

{
pkgDepCache::ActionGroup group(Cache);
if (_config->FindB(AreDoingSatisfy ? "APT::Get::Satisfy-Automatic" : "APT::Get::Build-Dep-Automatic", false) == false)
{
for (auto const &pkg: removeAgain)
Expand Down
2 changes: 2 additions & 0 deletions cmdline/apt-internal-solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ int main(int argc,const char *argv[]) /*{{{*/
EDSP::WriteProgress(5, "Read scenario…", output);

pkgCacheFile CacheFile;
CacheFile.InhibitActionGroups(true);
if (CacheFile.Open(NULL, false) == false)
DIE("Failed to open CacheFile!");

Expand Down Expand Up @@ -202,6 +203,7 @@ int main(int argc,const char *argv[]) /*{{{*/

EDSP::WriteProgress(95, "Write solution…", output);

CacheFile->MarkAndSweep();
if (WriteSolution(CacheFile, output) == false)
DIE("Failed to output the solution!");

Expand Down
2 changes: 0 additions & 2 deletions test/integration/test-bug-604222-new-and-autoremove
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ Building dependency tree...
MarkInstall libopenal-dev:i386 < none -> 1:1.12.854-2 @un puN > FU=0
Ignore MarkGarbage of libopenal-dev:i386 < none -> 1:1.12.854-2 @un puN > as its mode (Install) is protected
Ignore MarkGarbage of libavcodec52:i386 < none -> 4:0.5.2-6 @un puN > as its mode (Install) is protected
Ignore MarkGarbage of libopenal-dev:i386 < none -> 1:1.12.854-2 @un puN > as its mode (Install) is protected
Ignore MarkGarbage of libavcodec52:i386 < none -> 4:0.5.2-6 @un puN > as its mode (Install) is protected
The following additional packages will be installed:
libavcodec52 libopenal-dev
The following NEW packages will be installed:
Expand Down
10 changes: 5 additions & 5 deletions test/integration/test-bug-618848-always-respect-user-requests
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ setupaptarchive

testsuccessequal "Reading package lists...
Building dependency tree...
MarkDelete libdb4.8:i386 < 1.0 @ii pmK > FU=1
MarkDelete exim4-daemon-light:i386 < 1.0 @ii mK Ib > FU=0
MarkInstall exim4-daemon-heavy:i386 < none -> 1.0 @un umN Ib > FU=0
exim4-daemon-heavy:i386 Depends on libdb4.8:i386 < 1.0 @ii pmR > can't be satisfied! (dep)
MarkDelete exim4:i386 < 1.0 @ii mK Ib > FU=0
MarkDelete libdb4.8:i386 < 1.0 @ii pK > FU=1
MarkDelete exim4-daemon-light:i386 < 1.0 @ii K Ib > FU=0
MarkInstall exim4-daemon-heavy:i386 < none -> 1.0 @un uN Ib > FU=0
exim4-daemon-heavy:i386 Depends on libdb4.8:i386 < 1.0 @ii pR > can't be satisfied! (dep)
MarkDelete exim4:i386 < 1.0 @ii K Ib > FU=0
The following packages will be REMOVED:
exim4 exim4-daemon-light libdb4.8
MarkDelete exim4:i386 < 1.0 @ii K > FU=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ setupaptarchive
testsuccessequal "Reading package lists...
Building dependency tree...
Removing: systemd-sysv:amd64 as upgrade is not an option for runit-init:amd64 (1)
MarkDelete systemd-sysv:amd64 < 1 @ii mK > FU=0
MarkDelete systemd-sysv:amd64 < 1 @ii K > FU=0
MarkInstall runit-init:amd64 < none -> 1 @un puN > FU=1
Starting pkgProblemResolver with broken count: 1
Starting 2 pkgProblemResolver with broken count: 1
Investigating (0) init:amd64 < 1 @ii mK Ib >
Broken init:amd64 PreDepends on systemd-sysv:amd64 < 1 @ii pmR >
Investigating (0) init:amd64 < 1 @ii K Ib >
Broken init:amd64 PreDepends on systemd-sysv:amd64 < 1 @ii pR >
Considering systemd-sysv:amd64 0 as a solution to init:amd64 5100
Added systemd-sysv:amd64 to the remove list
Broken init:amd64 PreDepends on sysvinit-core:amd64 < none @un pmH >
Broken init:amd64 PreDepends on sysvinit-core:amd64 < none @un pH >
Considering sysvinit-core:amd64 0 as a solution to init:amd64 5100
Ignore MarkKeep of systemd-sysv:amd64 < 1 @ii pmR > as its mode (Delete) is protected
Investigating (1) init:amd64 < 1 @ii mK Ib >
Broken init:amd64 PreDepends on systemd-sysv:amd64 < 1 @ii pmR >
Ignore MarkKeep of systemd-sysv:amd64 < 1 @ii pR > as its mode (Delete) is protected
Investigating (1) init:amd64 < 1 @ii K Ib >
Broken init:amd64 PreDepends on systemd-sysv:amd64 < 1 @ii pR >
Considering systemd-sysv:amd64 5100 as a solution to init:amd64 5100
Broken init:amd64 PreDepends on sysvinit-core:amd64 < none @un pmH >
Broken init:amd64 PreDepends on sysvinit-core:amd64 < none @un pH >
Considering sysvinit-core:amd64 0 as a solution to init:amd64 5100
Or group remove for init:amd64
MarkDelete init:amd64 < 1 @ii mK Ib > FU=0
MarkDelete init:amd64 < 1 @ii K Ib > FU=0
Done
The following packages will be REMOVED:
init systemd-sysv
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test-dpkg-i-apt-install-fix-broken
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ setupaptarchive
testfailure dpkg -i incoming/autopkgtest-*.deb
testsuccessequal 'Reading package lists...
Building dependency tree...
Correcting dependencies... MarkInstall autopkgtest-satdep:amd64 < 1 @iU mK Nb Ib > FU=0
Correcting dependencies... MarkInstall autopkgtest-satdep:amd64 < 1 @iU K Nb Ib > FU=0
MarkInstall debhelper:amd64 < none -> 1 @un uN > FU=0
Starting pkgProblemResolver with broken count: 0
Starting 2 pkgProblemResolver with broken count: 0
Expand Down
7 changes: 3 additions & 4 deletions test/integration/test-resolver-delays-remove-decisions
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Building dependency tree...
MarkInstall foo:amd64 < none -> 1 @un uN Ib > FU=0
Installing foo-dep:amd64 as Depends of foo:amd64
MarkInstall foo-dep:amd64 < none -> 1 @un uN > FU=0
MarkDelete stuff:amd64 < 1 @ii mK > FU=0
MarkDelete stuff:amd64 < 1 @ii K > FU=0
Starting pkgProblemResolver with broken count: 0
Starting 2 pkgProblemResolver with broken count: 0
Done
Expand All @@ -79,16 +79,15 @@ Building dependency tree...
MarkInstall foobar:amd64 < none -> 1 @un puN Ib > FU=1
Installing foo:amd64 as Depends of foobar:amd64
Removing: stuff:amd64 as upgrade is not an option for foo:amd64 (1)
MarkDelete stuff:amd64 < 1 @ii mK > FU=0
MarkDelete stuff:amd64 < 1 @ii K > FU=0
MarkInstall foo:amd64 < none -> 1 @un puN Ib > FU=0
Installing foo-dep:amd64 as Depends of foo:amd64
MarkInstall foo-dep:amd64 < none -> 1 @un puN > FU=0
MarkInstall uninstallable:amd64 < 1 @ii pmK > FU=0
MarkInstall uninstallable:amd64 < 1 @ii pK > FU=0
Starting pkgProblemResolver with broken count: 0
Starting 2 pkgProblemResolver with broken count: 0
Done
Ignore MarkGarbage of foo:amd64 < none -> 1 @un puN > as its mode (Install) is protected
Ignore MarkGarbage of foo:amd64 < none -> 1 @un puN > as its mode (Install) is protected
Package 'bar' is not installed, so not removed
The following additional packages will be installed:
foo foo-dep
Expand Down

0 comments on commit d6f3458

Please sign in to comment.