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

Dynamic allocation memory (part 2) #131

Merged
merged 4 commits into from
Mar 14, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
steps:
- cli_check: |
YANET_FORMAT_COLUMNS=group,maximum memory group
group maximum
------------------------------ ----------
acl.network.ht 1048576
acl.transport.ht 67108864
acl.total.ht 67108864
acl.network.v4.source.lpm 71303168
acl.network.v4.destination.lpm 71303168
acl.network.v6.source.lpm 1073741824
acl.network.v6.destination.ht 262144
acl.network.v6.destination.lpm 1073741824
acl.network 157286400
acl 277872640
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"modules": {
"lp0.100": {
"type": "logicalPort",
"physicalPort": "kni0",
"vlanId": "100",
"macAddress": "00:00:00:11:11:11",
"nextModule": "acl0"
},
"lp0.200": {
"type": "logicalPort",
"physicalPort": "kni0",
"vlanId": "200",
"macAddress": "00:00:00:22:22:22",
"nextModule": "acl0"
},
"acl0": {
"type": "acl",
"nextModules": [
"route0"
]
},
"route0": {
"type": "route",
"interfaces": {
"kni0.100": {
"ipAddresses": [
"200.0.0.100/24"
],
"nextModule": "lp0.100"
},
"kni0.200": {
"ipAddresses": [
"fe80::200/96"
],
"nextModule": "lp0.200"
}
}
}
},
"memory_groups": [
{
"name": "acl",
"limit": "265M",
"memory_groups": [
{
"name": "acl.network.ht",
"limit": "1048576"
},
{
"name": "acl.transport.ht",
"limit": "64M"
},
{
"name": "acl.total.ht",
"limit": "64M"
},
{
"name": "acl.network",
"limit": "150M",
"memory_groups": [
{
"name": "acl.network.v4.source.lpm",
"limit": "68M"
},
{
"name": "acl.network.v4.destination.lpm",
"limit": "68M"
},
{
"name": "acl.network.v6.source.lpm",
"limit": "1G"
},
{
"name": "acl.network.v6.destination.ht",
"limit": "256K"
},
{
"name": "acl.network.v6.destination.lpm",
"limit": "1G"
}
]
}
]
}
]
}
17 changes: 15 additions & 2 deletions cli/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
#include <array>
#include <cstdio>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>
#include <vector>

#include <nlohmann/json.hpp>

#include "converter.h"

template<typename type>
static std::string to_percent(const type current, const type maximum)
{
double percent = 0.0;
if (maximum)
{
percent = (double)current / (double)maximum;
percent *= (double)100;
}

std::stringstream stream;
stream << std::fixed << std::setprecision(2) << percent;
return stream.str();
}

std::vector<std::string> split(const char* string,
const char delimiter)
{
Expand Down
3 changes: 3 additions & 0 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "helper.h"
#include "latch.h"
#include "limit.h"
#include "memory_manager.h"
#include "nat46clat.h"
#include "nat64stateful.h"
#include "neighbor.h"
Expand Down Expand Up @@ -91,6 +92,8 @@ std::vector<std::tuple<std::string,
{"limit", "", [](const auto& args) { call(limit::summary, args); }},
{"values", "", [](const auto& args) { call(show::values, args); }},
{"durations", "", [](const auto& args) { call(show::durations, args); }},
{"memory show", "", [](const auto& args) { call(memory_manager::show, args); }},
{"memory group", "", [](const auto& args) { call(memory_manager::group, args); }},
{"dump", "[in|out|drop] [interface_name] [enable|disable]", [](const auto& args) { call(show::physical_port_dump, args); }},
{},
{"show errors", "", [](const auto& args) { call(show::errors, args); }},
Expand Down
94 changes: 94 additions & 0 deletions cli/memory_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#pragma once

#include "common/idataplane.h"
#include "helper.h"

namespace memory_manager
{

void show()
{
interface::dataPlane dataplane;

const auto response = dataplane.memory_manager_stats();
const auto& [response_memory_group, response_objects] = response;
(void)response_memory_group;

table_t table;
table.insert("name",
"socket_id",
"current");

uint64_t total = 0;
for (const auto& [name, socket_id, current] : response_objects)
{
total += current;

table.insert(name,
socket_id,
current);
}

table.insert("total",
"n/s",
total);

table.print();
}

void group()
{
interface::dataPlane dataplane;

const auto response = dataplane.memory_manager_stats();
const auto& [response_memory_group, response_objects] = response;

table_t table;
table.insert("group",
"current",
"maximum",
"percent");

std::map<std::string, ///< object_name
common::uint64> ///< current
currents;

for (const auto& [name, socket_id, current] : response_objects)
{
(void)socket_id;

currents[name] = std::max(currents[name].value,
current);
}

response_memory_group.for_each([&](const auto& memory_group,
const std::set<std::string>& object_names) {
if (memory_group.name.empty())
{
return;
}

uint64_t group_total = 0;
for (const auto& object_name : object_names)
{
group_total += currents[object_name];
}

std::optional<uint64_t> maximum;
std::optional<std::string> percent;
if (memory_group.limit)
{
maximum = memory_group.limit;
percent = to_percent(group_total, memory_group.limit);
}

table.insert(memory_group.name,
group_total,
maximum,
percent);
});

table.print();
}

}
52 changes: 52 additions & 0 deletions cli/telegraf.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ void unsafe()

const auto static_counters = dataplane.getCounters(vector_range(0, (tCounterId)common::globalBase::static_counter_type::size));
const auto neighbor_stats = dataplane.neighbor_stats();
const auto memory_stats = dataplane.memory_manager_stats();
const auto& [memory_groups, memory_objects] = memory_stats;

const auto durations = controlplane.controlplane_durations();

Expand Down Expand Up @@ -371,6 +373,56 @@ void unsafe()
{"hashtable_remove_error", neighbor_stats.hashtable_remove_error},
{"netlink_neighbor_update", neighbor_stats.netlink_neighbor_update},
{"resolve", neighbor_stats.resolve}});

/// memory
{
std::map<std::string, ///< object_name
common::uint64> ///< current
currents;

uint64_t total = 0;
for (const auto& [name, socket_id, current] : memory_objects)
{
total += current;

currents[name] = std::max(currents[name].value,
current);

influxdb_format::print("memory",
{{"name", name},
{"socket_id", socket_id}},
{{"current", current}});
}

influxdb_format::print("memory",
{{"name", "total"}},
{{"current", total}});

memory_groups.for_each([&](const auto& memory_group,
const std::set<std::string>& object_names) {
if (memory_group.name.empty())
{
return;
}

uint64_t group_total = 0;
for (const auto& object_name : object_names)
{
group_total += currents[object_name];
}

uint64_t maximum = 0;
if (memory_group.limit)
{
maximum = memory_group.limit;
}

influxdb_format::print("memory",
{{"group", memory_group.name}},
{{"current", group_total},
{"maximum", maximum}});
});
}
}

void dregress()
Expand Down
30 changes: 30 additions & 0 deletions common/memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ inline uint64_t convert_string_to_bytes(std::string string)
class memory_group
{
public:
memory_group() :
limit(0)
{
}

template<typename callback_t>
std::set<std::string> for_each(const callback_t& callback) const
{
std::set<std::string> object_names;

if (memory_groups.empty())
{
object_names.emplace(name);
}
else
{
for (const auto& memory_group_next : memory_groups)
{
auto object_names_next = memory_group_next->for_each(callback);
for (const auto& object_name : object_names_next)
{
object_names.emplace(object_name);
}
}
}

callback(*this, object_names);
return object_names;
}

void pop(common::stream_in_t& stream)
{
stream.pop(name);
Expand Down
4 changes: 4 additions & 0 deletions dataplane/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ void cBus::clientThread(int clientSocket)
{
response = dataPlane->memory_manager.memory_manager_update(std::get<common::idp::memory_manager_update::request>(std::get<1>(request)));
}
else if (type == common::idp::requestType::memory_manager_stats)
{
response = dataPlane->memory_manager.memory_manager_stats();
}
else
{
stats.errors[(uint32_t)common::idp::errorType::busParse]++;
Expand Down
Loading
Loading