Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /proc/smaps parser #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions include/pfs/parsers/mem_map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

#ifndef PFS_PARSERS_MEM_MAP_HPP
#define PFS_PARSERS_MEM_MAP_HPP

#include <string>
#include <vector>

#include "pfs/types.hpp"

namespace pfs {
namespace impl {
namespace parsers {

std::vector<mem_map> parse_mem_map(const std::string& path);

} // namespace parsers
} // namespace impl
} // namespace pfs

#endif // PFS_PARSERS_MEM_MAP_HPP
1 change: 0 additions & 1 deletion include/pfs/parsers/meminfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef PFS_PARSERS_MEMINFO_HPP
#define PFS_PARSERS_MEMINFO_HPP

#include <functional>
#include <string>

namespace pfs {
Expand Down
2 changes: 2 additions & 0 deletions include/pfs/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class task final

std::vector<mem_region> get_maps() const;

std::vector<mem_map> get_smaps() const;

mem get_mem() const;

std::vector<mount> get_mountinfo() const;
Expand Down
32 changes: 31 additions & 1 deletion include/pfs/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <chrono>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

namespace pfs {
Expand Down Expand Up @@ -340,6 +339,37 @@ struct mem_region
}
};

struct mem_map
{
mem_region region;

size_t size = 0; // In kB
size_t kernel_page_size = 0; // In kB
size_t mmu_page_size = 0; // In kB
size_t rss = 0; // In kB
size_t pss = 0; // In kB
size_t pss_dirty = 0; // In kB
size_t shared_clean = 0; // In kB
size_t shared_dirty = 0; // In kB
size_t private_clean = 0; // In kB
size_t private_dirty = 0; // In kB
size_t referenced = 0; // In kB
size_t anonymous = 0; // In kB
size_t ksm = 0; // In kB
size_t lazy_free = 0; // In kB
size_t anon_huge_pages = 0; // In kB
size_t shmem_pmd_mapped = 0; // In kB
size_t file_pmd_mapped = 0; // In kB
size_t shared_hugetlb = 0; // In kB
size_t private_hugetlb = 0; // In kB
size_t swap = 0; // In kB
size_t swap_pss = 0; // In kB
size_t locked = 0; // In kB

bool thp_eligible = false;
std::string vm_flags;
};

struct module
{
enum class state
Expand Down
3 changes: 3 additions & 0 deletions include/pfs/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ ip parse_ipv6_address(const std::string& ip_address_hex);
// Figure out ip version (IPv4/IPv6), parse it and return it as a ip struct
std::pair<ip, uint16_t> parse_address(const std::string& address_str);

// Parses a memory size line (e.g. VmRSS: 4488 kB)
void parse_memory_size(const std::string& value, size_t& out);

} // namespace utils
} // namespace impl
} // namespace pfs
Expand Down
248 changes: 248 additions & 0 deletions src/mem_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/*
* 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 <fstream>
#include <functional>
#include <optional>
#include <string>
#include <unordered_map>

#include "pfs/parser_error.hpp"
#include "pfs/parsers/maps.hpp"
#include "pfs/parsers/mem_map.hpp"
#include "pfs/types.hpp"
#include "pfs/utils.hpp"

namespace pfs {
namespace impl {
namespace parsers {

namespace {

void parse_size(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.size);
}

void parse_kernel_page_size(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.kernel_page_size);
}

void parse_mmu_page_size(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.mmu_page_size);
}

void parse_rss(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.rss);
}

void parse_pss(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.pss);
}

void parse_pss_dirty(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.pss_dirty);
}

void parse_shared_clean(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.shared_clean);
}

void parse_shared_dirty(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.shared_dirty);
}

void parse_private_clean(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.private_clean);
}

void parse_private_dirty(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.private_dirty);
}

void parse_referenced(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.referenced);
}

void parse_anonymous(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.anonymous);
}

void parse_ksm(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.ksm);
}

void parse_lazy_free(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.lazy_free);
}

void parse_anon_huge_pages(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.anon_huge_pages);
}

void parse_shmem_pmd_mapped(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.shmem_pmd_mapped);
}

void parse_file_pmd_mapped(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.file_pmd_mapped);
}

void parse_shared_hugetlb(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.shared_hugetlb);
}

void parse_private_hugetlb(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.private_hugetlb);
}

void parse_swap(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.swap);
}

void parse_swap_pss(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.swap_pss);
}

void parse_locked(const std::string& value, mem_map& out)
{
utils::parse_memory_size(value, out.locked);
}

void parse_thp_eligible(const std::string& value, mem_map& out)
{
int thp_eligible = 0;
utils::stot(value, thp_eligible);
out.thp_eligible = thp_eligible != 0;
}

using value_parser =
std::function<void(const std::string& value, mem_map& out)>;
using value_parsers = std::unordered_map<std::string, value_parser>;

const value_parsers parsers = {{"Size", parse_size},
{"KernelPageSize", parse_kernel_page_size},
{"MMUPageSize", parse_mmu_page_size},
{"Rss", parse_rss},
{"Pss", parse_pss},
{"Pss_Dirty", parse_pss_dirty},
{"Shared_Clean", parse_shared_clean},
{"Shared_Dirty", parse_shared_dirty},
{"Private_Clean", parse_private_clean},
{"Private_Dirty", parse_private_dirty},
{"Referenced", parse_referenced},
{"Anonymous", parse_anonymous},
{"KSM", parse_ksm},
{"LazyFree", parse_lazy_free},
{"AnonHugePages", parse_anon_huge_pages},
{"ShmemPmdMapped", parse_shmem_pmd_mapped},
{"FilePmdMapped", parse_file_pmd_mapped},
{"Shared_Hugetlb", parse_shared_hugetlb},
{"Private_Hugetlb", parse_private_hugetlb},
{"Swap", parse_swap},
{"SwapPss", parse_swap_pss},
{"Locked", parse_locked},
{"THPeligible", parse_thp_eligible}};

} // namespace

std::vector<mem_map> parse_mem_map(const std::string& path)
{
std::ifstream in(path);
if (!in)
{
throw parser_error("Couldn't open file", path);
}

std::vector<mem_map> mem_maps;
std::optional<mem_map> current;
std::string line;
while (std::getline(in, line))
{
auto tokens = utils::split(line);
if (tokens.size() >= 5 && tokens[0] != "VmFlags:")
{
// Header line
if (current)
{
mem_maps.push_back(std::move(*current));
}

current.emplace();
current->region = parse_maps_line(line);

continue;
}

if (!current)
{
throw parser_error("Corrupted block - Missing header", line);
}

auto [key, value] = utils::split_once(line, ':');
if (key.empty())
{
throw parser_error("Corrupted line - Missing key", line);
}

utils::rtrim(key);
utils::ltrim(value);
if (key == "VmFlags")
{
// TODO: Split this into tokens and parse them?
current->vm_flags = value;
continue;
}

auto iter = parsers.find(key);
if (iter != parsers.end())
{
auto& parser = iter->second;
parser(value, *current);
}
}

if (current)
{
mem_maps.push_back(std::move(*current));
}

return mem_maps;
}

} // namespace parsers
} // namespace impl
} // namespace pfs
Loading