Skip to content

Commit

Permalink
Add pressure type parameter
Browse files Browse the repository at this point in the history
Summary: Allow plugins to consume pressure.some or pressure.full as they wish

Reviewed By: danobi

Differential Revision: D14758562

fbshipit-source-id: f18c0848b6f6b3e7e28e6a061f6e2633e46b0f02
  • Loading branch information
dschatzberg authored and facebook-github-bot committed Apr 4, 2019
1 parent 5227c2a commit 198f248
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
43 changes: 34 additions & 9 deletions util/Fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,38 @@ std::vector<int> Fs::getPids(const std::string& path, bool recursive) {
return pids;
}

ResourcePressure Fs::readRespressure(const std::string& path) {
std::string Fs::pressureTypeToString(PressureType type) {
switch (type) {
case PressureType::SOME:
return "some";
case PressureType::FULL:
return "full";
}
}

ResourcePressure Fs::readRespressure(
const std::string& path,
PressureType type) {
auto lines = readFileByLine(path);

auto type_name = pressureTypeToString(type);
size_t pressure_line_index = 0;
switch (type) {
case PressureType::SOME:
pressure_line_index = 0;
break;
case PressureType::FULL:
pressure_line_index = 1;
break;
}
if (lines.size() == 2) {
// Upstream v4.16+ format
//
// some avg10=0.22 avg60=0.17 avg300=1.11 total=58761459
// full avg10=0.22 avg60=0.16 avg300=1.08 total=58464525
std::vector<std::string> toks = split(lines[1], ' ');
std::vector<std::string> toks = split(lines[pressure_line_index], ' ');
OCHECK_EXCEPT(
toks[0] == "full", bad_control_file(path + ": invalid format"));
toks[0] == type_name, bad_control_file(path + ": invalid format"));
std::vector<std::string> avg10 = split(toks[1], '=');
OCHECK_EXCEPT(
avg10[0] == "avg10", bad_control_file(path + ": invalid format"));
Expand All @@ -260,9 +281,9 @@ ResourcePressure Fs::readRespressure(const std::string& path) {
// aggr 316016073
// some 0.00 0.03 0.05
// full 0.00 0.03 0.05
std::vector<std::string> toks = split(lines[2], ' ');
std::vector<std::string> toks = split(lines[pressure_line_index + 1], ' ');
OCHECK_EXCEPT(
toks[0] == "full", bad_control_file(path + ": invalid format"));
toks[0] == type_name, bad_control_file(path + ": invalid format"));

return ResourcePressure{
std::stof(toks[1]),
Expand Down Expand Up @@ -294,8 +315,10 @@ int64_t Fs::readMemcurrentWildcard(const std::string& path) {
return total;
}

ResourcePressure Fs::readMempressure(const std::string& path) {
return readRespressure(path + "/" + kMemPressureFile);
ResourcePressure Fs::readMempressure(
const std::string& path,
PressureType type) {
return readRespressure(path + "/" + kMemPressureFile, type);
}

int64_t Fs::readMinMaxLowHigh(
Expand Down Expand Up @@ -388,15 +411,17 @@ std::unordered_map<std::string, int64_t> Fs::getMemstat(
return map;
}

ResourcePressure Fs::readIopressure(const std::string& path) {
ResourcePressure Fs::readIopressure(
const std::string& path,
PressureType type) {
const std::string ioPressurePath = path + "/" + kIoPressureFile;

// earlier kernels had only mempressure, throw an exception if missing
std::ifstream f(ioPressurePath, std::ios::in);
if (!f.is_open()) {
throw std::system_error(ENOENT, std::system_category());
}
return readRespressure(ioPressurePath);
return readRespressure(ioPressurePath, type);
}

void Fs::writeMemhigh(const std::string& path, int64_t value) {
Expand Down
18 changes: 15 additions & 3 deletions util/Fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class Fs {
DIRECTORY,
};

enum class PressureType {
SOME = 0,
FULL,
};

/*
* Reads a directory and returns the names of the requested entry types
* Won't return any dotfiles (including ./ and ../)
Expand Down Expand Up @@ -92,19 +97,26 @@ class Fs {
const std::string& path,
bool recursive = false);

static std::string pressureTypeToString(PressureType type);
/* Helpers to read PSI files */
static ResourcePressure readRespressure(const std::string& path);
static ResourcePressure readRespressure(
const std::string& path,
PressureType type = PressureType::FULL);
static int64_t readMemcurrent(const std::string& path);
static int64_t readMemcurrentWildcard(const std::string& path);
static ResourcePressure readMempressure(const std::string& path);
static ResourcePressure readMempressure(
const std::string& path,
PressureType type = PressureType::FULL);
static int64_t readMinMaxLowHigh(
const std::string& path,
const std::string& file);
static int64_t readMemlow(const std::string& path);
static int64_t readMemhigh(const std::string& path);
static int64_t readMemmin(const std::string& path);
static int64_t readSwapCurrent(const std::string& path);
static ResourcePressure readIopressure(const std::string& path);
static ResourcePressure readIopressure(
const std::string& path,
PressureType type = PressureType::FULL);

static void writeMemhigh(const std::string& path, int64_t value);

Expand Down
27 changes: 27 additions & 0 deletions util/FsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,24 @@ TEST_F(FsTest, ReadMemoryPressure) {
EXPECT_FLOAT_EQ(pressure2.sec_600, 6.66);
}

TEST_F(FsTest, ReadMemoryPressureSome) {
// v4.16+ upstream format
std::string dir(kCgroupDataDir);
auto pressure = Fs::readMempressure(dir, Fs::PressureType::SOME);

EXPECT_FLOAT_EQ(pressure.sec_10, 1.11);
EXPECT_FLOAT_EQ(pressure.sec_60, 2.22);
EXPECT_FLOAT_EQ(pressure.sec_600, 3.33);

// old experimental format
auto dir2 = dir + "/service2.service";
auto pressure2 = Fs::readMempressure(dir2, Fs::PressureType::SOME);

EXPECT_FLOAT_EQ(pressure2.sec_10, 1.11);
EXPECT_FLOAT_EQ(pressure2.sec_60, 2.22);
EXPECT_FLOAT_EQ(pressure2.sec_600, 3.33);
}

TEST_F(FsTest, GetVmstat) {
std::string vmstatfile(kFsVmstatFile);
auto vmstat = Fs::getVmstat(vmstatfile);
Expand Down Expand Up @@ -292,3 +310,12 @@ TEST_F(FsTest, ReadIoPressure) {
EXPECT_FLOAT_EQ(pressure.sec_60, 5.56);
EXPECT_FLOAT_EQ(pressure.sec_600, 6.67);
}

TEST_F(FsTest, ReadIoPressureSome) {
std::string dir(kCgroupDataDir);
auto pressure = Fs::readIopressure(dir, Fs::PressureType::SOME);

EXPECT_FLOAT_EQ(pressure.sec_10, 1.12);
EXPECT_FLOAT_EQ(pressure.sec_60, 2.23);
EXPECT_FLOAT_EQ(pressure.sec_600, 3.34);
}

0 comments on commit 198f248

Please sign in to comment.