Skip to content

Commit

Permalink
Remove TimeValue usage from llvm/Support
Browse files Browse the repository at this point in the history
Summary:
This is a follow-up to D25416. It removes all usages of TimeValue from
llvm/Support library (except for the actual TimeValue declaration), and replaces
them with appropriate usages of std::chrono. To facilitate this, I have added
small utility functions for converting time points and durations into appropriate
OS-specific types (FILETIME, struct timespec, ...).

Reviewers: zturner, mehdi_amini

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D25730

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284966 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
labath committed Oct 24, 2016
1 parent 7577cb9 commit 2864c2a
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 114 deletions.
3 changes: 2 additions & 1 deletion include/llvm/Object/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Object/Binary.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TimeValue.h"

namespace llvm {
namespace object {
Expand Down
11 changes: 6 additions & 5 deletions include/llvm/Support/CachePruning.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define LLVM_SUPPORT_CACHE_PRUNING_H

#include "llvm/ADT/StringRef.h"
#include <chrono>

namespace llvm {

Expand All @@ -29,15 +30,15 @@ class CachePruning {
/// Define the pruning interval. This is intended to be used to avoid scanning
/// the directory too often. It does not impact the decision of which file to
/// prune. A value of 0 forces the scan to occurs.
CachePruning &setPruningInterval(int PruningInterval) {
CachePruning &setPruningInterval(std::chrono::seconds PruningInterval) {
Interval = PruningInterval;
return *this;
}

/// Define the expiration for a file. When a file hasn't been accessed for
/// \p ExpireAfter seconds, it is removed from the cache. A value of 0 disable
/// the expiration-based pruning.
CachePruning &setEntryExpiration(unsigned ExpireAfter) {
CachePruning &setEntryExpiration(std::chrono::seconds ExpireAfter) {
Expiration = ExpireAfter;
return *this;
}
Expand All @@ -59,11 +60,11 @@ class CachePruning {
private:
// Options that matches the setters above.
std::string Path;
unsigned Expiration = 0;
unsigned Interval = 0;
std::chrono::seconds Expiration;
std::chrono::seconds Interval;
unsigned PercentageOfAvailableSpace = 0;
};

} // namespace llvm

#endif
#endif
8 changes: 4 additions & 4 deletions include/llvm/Support/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/TimeValue.h"
#include <cassert>
#include <cstdint>
#include <ctime>
Expand Down Expand Up @@ -209,8 +209,8 @@ class file_status
// getters
file_type type() const { return Type; }
perms permissions() const { return Perms; }
TimeValue getLastAccessedTime() const;
TimeValue getLastModificationTime() const;
TimePoint<> getLastAccessedTime() const;
TimePoint<> getLastModificationTime() const;
UniqueID getUniqueID() const;

#if defined(LLVM_ON_UNIX)
Expand Down Expand Up @@ -540,7 +540,7 @@ inline std::error_code file_size(const Twine &Path, uint64_t &Result) {
/// @returns errc::success if the file times were successfully set, otherwise a
/// platform-specific error_code or errc::function_not_supported on
/// platforms where the functionality isn't available.
std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time);
std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time);

/// @brief Is status available?
///
Expand Down
11 changes: 6 additions & 5 deletions include/llvm/Support/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#include "llvm/ADT/Optional.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/TimeValue.h"
#include <system_error>

namespace llvm {
Expand All @@ -55,13 +55,14 @@ class Process {
/// This static function will set \p user_time to the amount of CPU time
/// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
/// time spent in system (kernel) mode. If the operating system does not
/// support collection of these metrics, a zero TimeValue will be for both
/// support collection of these metrics, a zero duration will be for both
/// values.
/// \param elapsed Returns the TimeValue::now() giving current time
/// \param elapsed Returns the system_clock::now() giving current time
/// \param user_time Returns the current amount of user time for the process
/// \param sys_time Returns the current amount of system time for the process
static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
TimeValue &sys_time);
static void GetTimeUsage(TimePoint<> &elapsed,
std::chrono::nanoseconds &user_time,
std::chrono::nanoseconds &sys_time);

/// This function makes the necessary calls to the operating system to
/// prevent core files or any other kind of large memory dumps that can
Expand Down
4 changes: 2 additions & 2 deletions lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,8 @@ void ThinLTOCodeGenerator::run() {
}

CachePruning(CacheOptions.Path)
.setPruningInterval(CacheOptions.PruningInterval)
.setEntryExpiration(CacheOptions.Expiration)
.setPruningInterval(std::chrono::seconds(CacheOptions.PruningInterval))
.setEntryExpiration(std::chrono::seconds(CacheOptions.Expiration))
.setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
.prune();

Expand Down
25 changes: 13 additions & 12 deletions lib/Support/CachePruning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static void writeTimestampFile(StringRef TimestampFile) {

/// Prune the cache of files that haven't been accessed in a long time.
bool CachePruning::prune() {
using namespace std::chrono;

if (Path.empty())
return false;

Expand All @@ -45,7 +47,7 @@ bool CachePruning::prune() {
if (!isPathDir)
return false;

if (Expiration == 0 && PercentageOfAvailableSpace == 0) {
if (Expiration == seconds(0) && PercentageOfAvailableSpace == 0) {
DEBUG(dbgs() << "No pruning settings set, exit early\n");
// Nothing will be pruned, early exit
return false;
Expand All @@ -55,7 +57,7 @@ bool CachePruning::prune() {
SmallString<128> TimestampFile(Path);
sys::path::append(TimestampFile, "llvmcache.timestamp");
sys::fs::file_status FileStatus;
sys::TimeValue CurrentTime = sys::TimeValue::now();
const auto CurrentTime = system_clock::now();
if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
if (EC == errc::no_such_file_or_directory) {
// If the timestamp file wasn't there, create one now.
Expand All @@ -65,14 +67,14 @@ bool CachePruning::prune() {
return false;
}
} else {
if (Interval) {
if (Interval == seconds(0)) {
// Check whether the time stamp is older than our pruning interval.
// If not, do nothing.
sys::TimeValue TimeStampModTime = FileStatus.getLastModificationTime();
auto TimeInterval = sys::TimeValue(sys::TimeValue::SecondsType(Interval));
const auto TimeStampModTime = FileStatus.getLastModificationTime();
auto TimeStampAge = CurrentTime - TimeStampModTime;
if (TimeStampAge <= TimeInterval) {
DEBUG(dbgs() << "Timestamp file too recent (" << TimeStampAge.seconds()
if (TimeStampAge <= Interval) {
DEBUG(dbgs() << "Timestamp file too recent ("
<< duration_cast<seconds>(TimeStampAge).count()
<< "s old), do not prune.\n");
return false;
}
Expand Down Expand Up @@ -103,7 +105,6 @@ bool CachePruning::prune() {
std::error_code EC;
SmallString<128> CachePathNative;
sys::path::native(Path, CachePathNative);
auto TimeExpiration = sys::TimeValue(sys::TimeValue::SecondsType(Expiration));
// Walk all of the files within this directory.
for (sys::fs::directory_iterator File(CachePathNative, EC), FileEnd;
File != FileEnd && !EC; File.increment(EC)) {
Expand All @@ -119,11 +120,11 @@ bool CachePruning::prune() {
}

// If the file hasn't been used recently enough, delete it
sys::TimeValue FileAccessTime = FileStatus.getLastAccessedTime();
const auto FileAccessTime = FileStatus.getLastAccessedTime();
auto FileAge = CurrentTime - FileAccessTime;
if (FileAge > TimeExpiration) {
DEBUG(dbgs() << "Remove " << File->path() << " (" << FileAge.seconds()
<< "s old)\n");
if (FileAge > Expiration) {
DEBUG(dbgs() << "Remove " << File->path() << " ("
<< duration_cast<seconds>(FileAge).count() << "s old)\n");
sys::fs::remove(File->path());
continue;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/Support/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ static inline size_t getMemUsage() {
}

TimeRecord TimeRecord::getCurrentTime(bool Start) {
using Seconds = std::chrono::duration<double, std::ratio<1>>;
TimeRecord Result;
sys::TimeValue now(0,0), user(0,0), sys(0,0);
sys::TimePoint<> now;
std::chrono::nanoseconds user, sys;

if (Start) {
Result.MemUsed = getMemUsage();
Expand All @@ -127,9 +129,9 @@ TimeRecord TimeRecord::getCurrentTime(bool Start) {
Result.MemUsed = getMemUsage();
}

Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Result.WallTime = Seconds(now.time_since_epoch()).count();
Result.UserTime = Seconds(user).count();
Result.SystemTime = Seconds(sys).count();
return Result;
}

Expand Down
22 changes: 7 additions & 15 deletions lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,12 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
return "";
}

TimeValue file_status::getLastAccessedTime() const {
TimeValue Ret;
Ret.fromEpochTime(fs_st_atime);
return Ret;
TimePoint<> file_status::getLastAccessedTime() const {
return toTimePoint(fs_st_atime);
}

TimeValue file_status::getLastModificationTime() const {
TimeValue Ret;
Ret.fromEpochTime(fs_st_mtime);
return Ret;
TimePoint<> file_status::getLastModificationTime() const {
return toTimePoint(fs_st_mtime);
}

UniqueID file_status::getUniqueID() const {
Expand Down Expand Up @@ -446,20 +442,16 @@ std::error_code status(int FD, file_status &Result) {
return fillStatus(StatRet, Status, Result);
}

std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
#if defined(HAVE_FUTIMENS)
timespec Times[2];
Times[0].tv_sec = Time.toEpochTime();
Times[0].tv_nsec = 0;
Times[1] = Times[0];
Times[0] = Times[1] = sys::toTimeSpec(Time);
if (::futimens(FD, Times))
return std::error_code(errno, std::generic_category());
return std::error_code();
#elif defined(HAVE_FUTIMES)
timeval Times[2];
Times[0].tv_sec = Time.toEpochTime();
Times[0].tv_usec = 0;
Times[1] = Times[0];
Times[0] = Times[1] = sys::toTimeVal(Time);
if (::futimes(FD, Times))
return std::error_code(errno, std::generic_category());
return std::error_code();
Expand Down
25 changes: 8 additions & 17 deletions lib/Support/Unix/Process.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/MutexGuard.h"
#include "llvm/Support/TimeValue.h"
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
Expand Down Expand Up @@ -60,22 +59,14 @@
using namespace llvm;
using namespace sys;

static std::pair<TimeValue, TimeValue> getRUsageTimes() {
static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsageTimes() {
#if defined(HAVE_GETRUSAGE)
struct rusage RU;
::getrusage(RUSAGE_SELF, &RU);
return std::make_pair(
TimeValue(
static_cast<TimeValue::SecondsType>(RU.ru_utime.tv_sec),
static_cast<TimeValue::NanoSecondsType>(
RU.ru_utime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND)),
TimeValue(
static_cast<TimeValue::SecondsType>(RU.ru_stime.tv_sec),
static_cast<TimeValue::NanoSecondsType>(
RU.ru_stime.tv_usec * TimeValue::NANOSECONDS_PER_MICROSECOND)));
return { toDuration(RU.ru_utime), toDuration(RU.ru_stime) };
#else
#warning Cannot get usage times on this platform
return std::make_pair(TimeValue(), TimeValue());
return {};
#endif
}

Expand Down Expand Up @@ -121,9 +112,9 @@ size_t Process::GetMallocUsage() {
#endif
}

void Process::GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
TimeValue &sys_time) {
elapsed = TimeValue::now();
void Process::GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time,
std::chrono::nanoseconds &sys_time) {
elapsed = std::chrono::system_clock::now();
std::tie(user_time, sys_time) = getRUsageTimes();
}

Expand Down Expand Up @@ -449,8 +440,8 @@ static unsigned GetRandomNumberSeed() {

// Otherwise, swizzle the current time and the process ID to form a reasonable
// seed.
TimeValue Now = TimeValue::now();
return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid());
const auto Now = std::chrono::high_resolution_clock::now();
return hash_combine(Now.time_since_epoch().count(), ::getpid());
}
#endif

Expand Down
36 changes: 35 additions & 1 deletion lib/Support/Unix/Unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
//=== is guaranteed to work on all UNIX variants.
//===----------------------------------------------------------------------===//

#include "llvm/Config/config.h" // Get autoconf configuration settings
#include "llvm/Config/config.h" // Get autoconf configuration settings
#include "llvm/Support/Chrono.h"
#include "llvm/Support/Errno.h"
#include <algorithm>
#include <assert.h>
Expand Down Expand Up @@ -69,4 +70,37 @@ static inline bool MakeErrMsg(
return true;
}

namespace llvm {
namespace sys {

/// Convert a struct timeval to a duration. Note that timeval can be used both
/// as a time point and a duration. Be sure to check what the input represents.
inline std::chrono::microseconds toDuration(const struct timeval &TV) {
return std::chrono::seconds(TV.tv_sec) +
std::chrono::microseconds(TV.tv_usec);
}

/// Convert a time point to struct timespec.
inline struct timespec toTimeSpec(TimePoint<> TP) {
using namespace std::chrono;

struct timespec RetVal;
RetVal.tv_sec = toTimeT(TP);
RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
return RetVal;
}

/// Convert a time point to struct timeval.
inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
using namespace std::chrono;

struct timeval RetVal;
RetVal.tv_sec = toTimeT(TP);
RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
return RetVal;
}

} // namespace sys
} // namespace llvm

#endif
Loading

0 comments on commit 2864c2a

Please sign in to comment.