#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <folly/io/async/test/TimeUtil.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cerrno>
#ifdef __linux__
#include <sys/utsname.h>
#endif
#include <chrono>
#include <ostream>
#include <stdexcept>
#include <glog/logging.h>
#include <folly/Conv.h>
#include <folly/Portability.h>
#include <folly/ScopeGuard.h>
#include <folly/String.h>
#include <folly/portability/Unistd.h>
#include <folly/system/ThreadId.h>
string;
usingnamespacestd::chrono;
namespace folly {
#ifdef __linux__
static int getLinuxVersion(StringPiece release) {
auto dot1 = release.find('.');
if (dot1 == StringPiece::npos) {
throw std::invalid_argument("could not find first dot");
}
auto v1 = folly::to<int>(release.subpiece(0, dot1));
auto dot2 = release.find('.', dot1 + 1);
if (dot2 == StringPiece::npos) {
throw std::invalid_argument("could not find second dot");
}
auto v2 = folly::to<int>(release.subpiece(dot1 + 1, dot2 - (dot1 + 1)));
int dash = release.find('-', dot2 + 1);
auto v3 = folly::to<int>(release.subpiece(dot2 + 1, dash - (dot2 + 1)));
return ((v1 * 1000 + v2) * 1000) + v3;
}
static int64_t determineSchedstatUnits() {
struct utsname unameInfo;
if (uname(&unameInfo) != 0) {
LOG(ERROR) << "unable to determine jiffies/second: uname failed: %s"
<< errnoStr(errno);
return -1;
}
int linuxVersion;
try {
linuxVersion = getLinuxVersion(unameInfo.release);
} catch (const std::exception&) {
LOG(ERROR) << "unable to determine jiffies/second: failed to parse "
<< "kernel release string \"" << unameInfo.release << "\"";
return -1;
}
if (linuxVersion >= 2006023) {
return 1;
}
char configPath[256];
snprintf(
configPath, sizeof(configPath), "/boot/config-%s", unameInfo.release);
FILE* f = fopen(configPath, "r");
if (f == nullptr) {
LOG(ERROR) << "unable to determine jiffies/second: "
"cannot open kernel config file %s"
<< configPath;
return -1;
}
SCOPE_EXIT {
fclose(f);
};
int64_t hz = -1;
char buf[1024];
while (fgets(buf, sizeof(buf), f) != nullptr) {
if (strcmp(buf, "CONFIG_NO_HZ=y\n") == 0) {
LOG(ERROR) << "unable to determine jiffies/second: tickless kernel";
return -1;
} else if (strcmp(buf, "CONFIG_HZ=1000\n") == 0) {
hz = 1000;
} else if (strcmp(buf, "CONFIG_HZ=300\n") == 0) {
hz = 300;
} else if (strcmp(buf, "CONFIG_HZ=250\n") == 0) {
hz = 250;
} else if (strcmp(buf, "CONFIG_HZ=100\n") == 0) {
hz = 100;
}
}
if (hz == -1) {
LOG(ERROR) << "unable to determine jiffies/second: no CONFIG_HZ setting "
"found in %s"
<< configPath;
return -1;
}
return hz;
}
#endif
static nanoseconds getSchedTimeWaiting(pid_t tid) { … }
void TimePoint::reset() { … }
std::ostream& operator<<(std::ostream& os, const TimePoint& timePoint) { … }
bool checkTimeout(
const TimePoint& start,
const TimePoint& end,
nanoseconds expected,
bool allowSmaller,
nanoseconds tolerance) { … }
}