From 79b61ae7673eb6213493e2cb202f0d70c390932d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 26 May 2018 17:36:08 +0200 Subject: Use a steady clock source for progress reporting Clock changes while apt is running can result in strange reports confusing (and amusing) users. Sadly, to keep the ABI for now the code is a bit more ugly than it would need to be. --- ftparchive/apt-ftparchive.cc | 70 ++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 32 deletions(-) (limited to 'ftparchive') diff --git a/ftparchive/apt-ftparchive.cc b/ftparchive/apt-ftparchive.cc index a3fdf2391..077701cc0 100644 --- a/ftparchive/apt-ftparchive.cc +++ b/ftparchive/apt-ftparchive.cc @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,7 @@ #include #include #include +#include #include "apt-ftparchive.h" #include "cachedb.h" @@ -46,6 +48,15 @@ using namespace std; unsigned Quiet = 0; +static struct timeval GetTimevalFromSteadyClock() /*{{{*/ +{ + auto const Time = std::chrono::steady_clock::now().time_since_epoch(); + auto const Time_sec = std::chrono::duration_cast(Time); + auto const Time_usec = std::chrono::duration_cast(Time - Time_sec); + return { Time_sec.count(), Time_usec.count() }; +} + /*}}}*/ + // struct PackageMap - List of all package files in the config file /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -173,9 +184,8 @@ bool PackageMap::GenPackages(Configuration &Setup,struct CacheDB::Stats &Stats) string OverrideDir = Setup.FindDir("Dir::OverrideDir"); string CacheDir = Setup.FindDir("Dir::CacheDir"); - struct timeval StartTime; - gettimeofday(&StartTime,0); - + struct timeval StartTime = GetTimevalFromSteadyClock(); + PkgDone = true; // Create a package writer object. @@ -232,15 +242,15 @@ bool PackageMap::GenPackages(Configuration &Setup,struct CacheDB::Stats &Stats) else c0out << ' '; - struct timeval NewTime; - gettimeofday(&NewTime,0); - double Delta = NewTime.tv_sec - StartTime.tv_sec + - (NewTime.tv_usec - StartTime.tv_usec)/1000000.0; + struct timeval NewTime = GetTimevalFromSteadyClock(); + std::chrono::duration Delta = + std::chrono::seconds(NewTime.tv_sec - StartTime.tv_sec) + + std::chrono::microseconds(NewTime.tv_sec - StartTime.tv_usec); c0out << Packages.Stats.Packages << " files " << /* SizeToStr(Packages.Stats.MD5Bytes) << "B/" << */ SizeToStr(Packages.Stats.Bytes) << "B " << - TimeToStr((long)Delta) << endl; + TimeToStr(llround(Delta.count())) << endl; if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true) c0out << " Misses in Cache: " << Packages.Stats.Misses<< endl; @@ -264,9 +274,7 @@ bool PackageMap::GenSources(Configuration &Setup,struct CacheDB::Stats &Stats) string OverrideDir = Setup.FindDir("Dir::OverrideDir"); string CacheDir = Setup.FindDir("Dir::CacheDir"); - struct timeval StartTime; - gettimeofday(&StartTime,0); - + struct timeval StartTime = GetTimevalFromSteadyClock(); SrcDone = true; // Create a package writer object. @@ -320,13 +328,13 @@ bool PackageMap::GenSources(Configuration &Setup,struct CacheDB::Stats &Stats) else c0out << ' '; - struct timeval NewTime; - gettimeofday(&NewTime,0); - double Delta = NewTime.tv_sec - StartTime.tv_sec + - (NewTime.tv_usec - StartTime.tv_usec)/1000000.0; + struct timeval NewTime = GetTimevalFromSteadyClock(); + std::chrono::duration Delta = + std::chrono::seconds(NewTime.tv_sec - StartTime.tv_sec) + + std::chrono::microseconds(NewTime.tv_sec - StartTime.tv_usec); c0out << Sources.Stats.Packages << " pkgs in " << - TimeToStr((long)Delta) << endl; + TimeToStr(llround(Delta.count())) << endl; if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true) c0out << " Misses in Cache: " << Sources.Stats.Misses << endl; @@ -356,10 +364,9 @@ bool PackageMap::GenContents(Configuration &Setup, string ArchiveDir = Setup.FindDir("Dir::ArchiveDir"); string CacheDir = Setup.FindDir("Dir::CacheDir"); string OverrideDir = Setup.FindDir("Dir::OverrideDir"); - - struct timeval StartTime; - gettimeofday(&StartTime,0); - + + struct timeval StartTime = GetTimevalFromSteadyClock(); + // Create a package writer object. MultiCompress Comp(flCombine(ArchiveDir,this->Contents), CntCompress,Permissions); @@ -437,17 +444,17 @@ bool PackageMap::GenContents(Configuration &Setup, else c0out << ' '; - struct timeval NewTime; - gettimeofday(&NewTime,0); - double Delta = NewTime.tv_sec - StartTime.tv_sec + - (NewTime.tv_usec - StartTime.tv_usec)/1000000.0; + struct timeval NewTime = GetTimevalFromSteadyClock(); + std::chrono::duration Delta = + std::chrono::seconds(NewTime.tv_sec - StartTime.tv_sec) + + std::chrono::microseconds(NewTime.tv_sec - StartTime.tv_usec); if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true) c0out << " Misses in Cache: " << Contents.Stats.Misses<< endl; c0out << Contents.Stats.Packages << " files " << SizeToStr(Contents.Stats.Bytes) << "B " << - TimeToStr((long)Delta) << endl; + TimeToStr(llround(Delta.count())) << endl; return true; } @@ -926,8 +933,7 @@ static bool Generate(CommandLine &CmdL) if (CmdL.FileSize() < 2) return ShowHelp(CmdL); - struct timeval StartTime; - gettimeofday(&StartTime,0); + struct timeval StartTime = GetTimevalFromSteadyClock(); struct CacheDB::Stats Stats; // Read the configuration file. @@ -964,12 +970,12 @@ static bool Generate(CommandLine &CmdL) return false; } - struct timeval NewTime; - gettimeofday(&NewTime,0); - double Delta = NewTime.tv_sec - StartTime.tv_sec + - (NewTime.tv_usec - StartTime.tv_usec)/1000000.0; + struct timeval NewTime = GetTimevalFromSteadyClock(); + std::chrono::duration Delta = + std::chrono::seconds(NewTime.tv_sec - StartTime.tv_sec) + + std::chrono::microseconds(NewTime.tv_sec - StartTime.tv_usec); c1out << "Done. " << SizeToStr(Stats.Bytes) << "B in " << Stats.Packages - << " archives. Took " << TimeToStr((long)Delta) << endl; + << " archives. Took " << TimeToStr(llround(Delta.count())) << endl; UnloadTree(TransList); return true; -- cgit v1.2.3