-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "pfs/parsers.hpp" | ||
#include "pfs/utils.hpp" | ||
|
||
namespace pfs { | ||
namespace impl { | ||
namespace parsers { | ||
|
||
cgroup_controller parse_cgroup_controller_line(const std::string& line) | ||
{ | ||
// Some examples: | ||
// clang-format off | ||
// #subsys_name hierarchy num_cgroups enabled | ||
// cpuset 6 1 1 | ||
// cpu 5 33 1 | ||
// cpuacct 5 33 1 | ||
// blkio 7 33 1 | ||
// memory 8 76 1 | ||
// devices 12 33 1 | ||
// freezer 3 1 1 | ||
// net_cls 2 1 1 | ||
// perf_event 11 1 1 | ||
// net_prio 2 1 1 | ||
// hugetlb 10 1 1 | ||
// pids 4 38 1 | ||
// rdma 9 1 1 | ||
// clang-format on | ||
|
||
enum token | ||
{ | ||
SUBSYS_NAME = 0, | ||
HIERARCHY = 1, | ||
NUM_CGROUPS = 2, | ||
ENABLED = 3, | ||
COUNT | ||
}; | ||
|
||
static const char DELIM = '\t'; | ||
|
||
auto tokens = utils::split(line, DELIM); | ||
if (tokens.size() != COUNT) | ||
{ | ||
throw parser_error( | ||
"Corrupted cgroup controller line - Unexpected tokens count", line); | ||
} | ||
|
||
try | ||
{ | ||
cgroup_controller controller; | ||
|
||
controller.subsys_name = tokens[SUBSYS_NAME]; | ||
|
||
utils::stot(tokens[HIERARCHY], controller.hierarchy); | ||
|
||
utils::stot(tokens[NUM_CGROUPS], controller.num_cgroups); | ||
|
||
if (tokens[ENABLED] == "0") | ||
{ | ||
controller.enabled = false; | ||
} | ||
else if (tokens[ENABLED] == "1") | ||
{ | ||
controller.enabled = true; | ||
} | ||
else | ||
{ | ||
throw parser_error( | ||
"Corrupted cgroup controller line - Unexpected enabled value", | ||
tokens[ENABLED]); | ||
} | ||
|
||
return controller; | ||
} | ||
catch (const std::invalid_argument& ex) | ||
{ | ||
throw parser_error("Corrupted cgroup controller - Invalid argument", | ||
line); | ||
} | ||
catch (const std::out_of_range& ex) | ||
{ | ||
throw parser_error("Corrupted cgroup controller - Out of range", line); | ||
} | ||
} | ||
|
||
} // namespace parsers | ||
} // namespace impl | ||
} // namespace pfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <sstream> | ||
|
||
#include "catch.hpp" | ||
#include "test_utils.hpp" | ||
|
||
#include "pfs/parsers.hpp" | ||
|
||
using namespace pfs::impl::parsers; | ||
|
||
TEST_CASE("Parse corrupted cgroup controller", "[procfs][cgroup]") | ||
{ | ||
// Missing last token (enabled) | ||
std::string line = "devices 12 33"; | ||
|
||
REQUIRE_THROWS_AS(parse_cgroup_controller_line(line), pfs::parser_error); | ||
} | ||
|
||
TEST_CASE("Parse cgroup controller", "[procfs][cgroup]") | ||
{ | ||
pfs::cgroup_controller expected; | ||
|
||
SECTION("Enabled") | ||
{ | ||
expected.subsys_name = "perf_event"; | ||
expected.hierarchy = 11; | ||
expected.num_cgroups = 1; | ||
expected.enabled = true; | ||
} | ||
|
||
SECTION("Disabled") | ||
{ | ||
expected.subsys_name = "hugetlb"; | ||
expected.hierarchy = 0; | ||
expected.num_cgroups = 1; | ||
expected.enabled = false; | ||
} | ||
|
||
std::ostringstream line; | ||
line << expected.subsys_name << '\t'; | ||
line << expected.hierarchy << '\t'; | ||
line << expected.num_cgroups << '\t'; | ||
line << (expected.enabled ? '1' : '0'); | ||
|
||
auto controller = parse_cgroup_controller_line(line.str()); | ||
REQUIRE(controller.subsys_name == expected.subsys_name); | ||
REQUIRE(controller.hierarchy == expected.hierarchy); | ||
REQUIRE(controller.num_cgroups == expected.num_cgroups); | ||
REQUIRE(controller.enabled == expected.enabled); | ||
} |