-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3465 from canonical/parallelize-info-intra-vm
Parallelize info intra vm
- Loading branch information
Showing
7 changed files
with
173 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (C) Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "runtime_instance_info_helper.h" | ||
|
||
#include <multipass/format.h> | ||
#include <multipass/rpc/multipass.grpc.pb.h> | ||
#include <multipass/utils.h> | ||
#include <multipass/virtual_machine.h> | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
#include <array> | ||
|
||
namespace mp = multipass; | ||
|
||
namespace | ||
{ | ||
|
||
struct Keys | ||
{ | ||
public: | ||
static constexpr auto loadavg_key = "loadavg"; | ||
static constexpr auto mem_usage_key = "mem_usage"; | ||
static constexpr auto mem_total_key = "mem_total"; | ||
static constexpr auto disk_usage_key = "disk_usage"; | ||
static constexpr auto disk_total_key = "disk_total"; | ||
static constexpr auto cpus_key = "cpus"; | ||
static constexpr auto current_release_key = "current_release"; | ||
}; | ||
|
||
struct Cmds | ||
{ | ||
private: | ||
static constexpr auto key_val_cmd = R"(echo {}: \"$(eval "{}")\")"; | ||
static constexpr std::array key_cmds_pairs{ | ||
std::pair{Keys::loadavg_key, "cat /proc/loadavg | cut -d ' ' -f1-3"}, | ||
std::pair{Keys::mem_usage_key, R"(free -b | grep 'Mem:' | awk '{printf \$3}')"}, | ||
std::pair{Keys::mem_total_key, R"(free -b | grep 'Mem:' | awk '{printf \$2}')"}, | ||
std::pair{Keys::disk_usage_key, "df -t ext4 -t vfat --total -B1 --output=used | tail -n 1"}, | ||
std::pair{Keys::disk_total_key, "df -t ext4 -t vfat --total -B1 --output=size | tail -n 1"}, | ||
std::pair{Keys::cpus_key, "nproc"}, | ||
std::pair{Keys::current_release_key, R"(cat /etc/os-release | grep 'PRETTY_NAME' | cut -d \\\" -f2)"}}; | ||
|
||
inline static const std::array cmds = [] { | ||
constexpr auto n = key_cmds_pairs.size(); | ||
std::array<std::string, key_cmds_pairs.size()> ret; | ||
for (std::size_t i = 0; i < n; ++i) | ||
{ | ||
const auto [key, cmd] = key_cmds_pairs[i]; | ||
ret[i] = fmt::format(key_val_cmd, key, cmd); | ||
} | ||
|
||
return ret; | ||
}(); | ||
|
||
public: | ||
inline static const std::string sequential_composite_cmd = fmt::to_string(fmt::join(cmds, "; ")); | ||
inline static const std::string parallel_composite_cmd = fmt::format("{} & wait", fmt::join(cmds, "& ")); | ||
}; | ||
} // namespace | ||
|
||
void mp::RuntimeInstanceInfoHelper::populate_runtime_info(mp::VirtualMachine& vm, | ||
mp::DetailedInfoItem* info, | ||
mp::InstanceDetails* instance_info, | ||
const std::string& original_release, | ||
bool parallelize) | ||
{ | ||
const auto& cmd = parallelize ? Cmds::parallel_composite_cmd : Cmds::sequential_composite_cmd; | ||
auto results = YAML::Load(vm.ssh_exec(cmd)); | ||
|
||
instance_info->set_load(results[Keys::loadavg_key].as<std::string>()); | ||
instance_info->set_memory_usage(results[Keys::mem_usage_key].as<std::string>()); | ||
info->set_memory_total(results[Keys::mem_total_key].as<std::string>()); | ||
instance_info->set_disk_usage(results[Keys::disk_usage_key].as<std::string>()); | ||
info->set_disk_total(results[Keys::disk_total_key].as<std::string>()); | ||
info->set_cpu_count(results[Keys::cpus_key].as<std::string>()); | ||
|
||
auto current_release = results[Keys::current_release_key].as<std::string>(); | ||
instance_info->set_current_release(!current_release.empty() ? current_release : original_release); | ||
|
||
std::string management_ip = vm.management_ipv4(); | ||
auto all_ipv4 = vm.get_all_ipv4(); | ||
|
||
if (MP_UTILS.is_ipv4_valid(management_ip)) | ||
instance_info->add_ipv4(management_ip); | ||
else if (all_ipv4.empty()) | ||
instance_info->add_ipv4("N/A"); | ||
|
||
for (const auto& extra_ipv4 : all_ipv4) | ||
if (extra_ipv4 != management_ip) | ||
instance_info->add_ipv4(extra_ipv4); | ||
} |
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,41 @@ | ||
/* | ||
* Copyright (C) Canonical, Ltd. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MULTIPASS_RUNTIME_INSTANCE_INFO_HELPER_H | ||
#define MULTIPASS_RUNTIME_INSTANCE_INFO_HELPER_H | ||
|
||
#include <string> | ||
|
||
namespace multipass | ||
{ | ||
class VirtualMachine; | ||
class DetailedInfoItem; | ||
class InstanceDetails; | ||
|
||
// Note: we could extract other code to info/list populating code here, but that is left as a future improvement | ||
struct RuntimeInstanceInfoHelper | ||
{ | ||
static void populate_runtime_info(VirtualMachine& vm, | ||
DetailedInfoItem* info, | ||
InstanceDetails* instance_info, | ||
const std::string& original_release, | ||
bool parallelize); | ||
}; | ||
|
||
} // namespace multipass | ||
|
||
#endif // MULTIPASS_RUNTIME_INSTANCE_INFO_HELPER_H |
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