diff --git a/src/applib/CMakeLists.txt b/src/applib/CMakeLists.txt index ba8b558..ae423e1 100644 --- a/src/applib/CMakeLists.txt +++ b/src/applib/CMakeLists.txt @@ -60,6 +60,8 @@ target_sources(applib PRIVATE storage_device.h storage_property.cpp storage_property.h + storage_property_data_amount.cpp + storage_property_data_amount.h storage_property_descr.cpp storage_property_descr.h storage_property_descr_ata_attribute.cpp diff --git a/src/applib/storage_property.cpp b/src/applib/storage_property.cpp index f5e202e..29a8e78 100644 --- a/src/applib/storage_property.cpp +++ b/src/applib/storage_property.cpp @@ -103,6 +103,9 @@ std::string AtaStorageAttribute::format_raw_value() const // something is wrong with system locale, can't do anything here. } ss << std::fixed << raw_value_int; + if (raw_value_bytes.has_value()) { + ss << " [" << hz::format_size(raw_value_bytes.value(), true) << "]"; + } return ss.str(); } return raw_value; @@ -143,6 +146,9 @@ std::string AtaStorageStatistic::format_value() const // something is wrong with system locale, can't do anything here. } ss << std::fixed << value_int; + if (value_bytes.has_value()) { + ss << " [" << hz::format_size(value_bytes.value(), true) << "]"; + } return ss.str(); } return value; diff --git a/src/applib/storage_property.h b/src/applib/storage_property.h index 536e785..50653f2 100644 --- a/src/applib/storage_property.h +++ b/src/applib/storage_property.h @@ -109,6 +109,7 @@ class AtaStorageAttribute { FailTime when_failed = FailTime::Unknown; ///< When-failed type std::string raw_value; ///< Raw value as a string, as presented by smartctl (formatted). std::int64_t raw_value_int = 0; ///< Same as raw_value, but parsed as int64. original value is 6 bytes I think. + std::optional raw_value_bytes; ///< Raw value converted to bytes, if the attribute reports an amount of data (e.g. Total_LBAs_Written). }; @@ -127,13 +128,14 @@ class AtaStorageStatistic { /// Whether the normalization flag is present [[nodiscard]] bool is_normalized() const; - /// Format value with commas (if it's a number) + /// Format value with commas (if it's a number), adding a human-readable data amount if available [[nodiscard]] std::string format_value() const; bool is_header = false; ///< If the line is a header std::string flags; ///< Flags in "NDC" / "---" format std::string value; ///< Value as a string, as presented by smartctl (formatted). std::int64_t value_int = 0; ///< Same as value, but parsed as int64. + std::optional value_bytes; ///< Value converted to bytes, if the statistic reports an amount of data (e.g. Logical Sectors Written). std::int64_t page = 0; ///< Page std::int64_t offset = 0; ///< Offset in page }; diff --git a/src/applib/storage_property_data_amount.cpp b/src/applib/storage_property_data_amount.cpp new file mode 100644 index 0000000..994e87c --- /dev/null +++ b/src/applib/storage_property_data_amount.cpp @@ -0,0 +1,145 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib +/// \weakgroup applib +/// @{ + +#include "storage_property_data_amount.h" + +#include // std::toupper +#include +#include + +#include "hz/string_num.h" // number_to_string_* +#include "hz/format_unit.h" // format_size +#include "app_regex.h" + + + +namespace { + + /// Size of a logical sector (LBA) assumed for ATA counters. + /// SATA SSDs use 512-byte logical sectors (4Kn SATA SSDs are practically nonexistent), + /// and the attributes counting LBAs use 512-byte units regardless. + constexpr std::uint64_t ata_sector_size = 512; + + /// NVMe data unit: 1000 units of 512 bytes (see NVMe specification, SMART / Health Information). + constexpr std::uint64_t nvme_data_unit_size = 512 * 1000; + +} + + + +std::optional ata_attribute_get_data_unit_size(const std::string& reported_name) +{ + // Attributes counting errors, retries, etc. of sectors are not data amounts. + if (app_regex_partial_match("/(Err|Fail|Retr|Realloc|Pend|Uncor|Bad)/i", reported_name)) { + return std::nullopt; + } + + // Explicit units: Host_Writes_32MiB, NAND_Writes_1GiB, Lifetime_Writes_GiB, Total_NAND_Prog_Ct_GiB, ... + std::string count_str, unit_str; + if (app_regex_partial_match("/_([0-9]*)([KMGT])iB$/i", reported_name, {&count_str, &unit_str})) { + std::uint64_t count = 1; + if (!count_str.empty() && (!hz::string_is_numeric_nolocale(count_str, count, false) || count == 0)) { + return std::nullopt; + } + std::uint64_t unit = 0; + switch (std::toupper(static_cast(unit_str.front()))) { + case 'K': unit = 1024ULL; break; + case 'M': unit = 1024ULL * 1024; break; + case 'G': unit = 1024ULL * 1024 * 1024; break; + case 'T': unit = 1024ULL * 1024 * 1024 * 1024; break; + default: return std::nullopt; + } + if (count > std::numeric_limits::max() / unit) { + return std::nullopt; + } + return count * unit; + } + + // Sector / LBA counts: Total_LBAs_Written, Total_LBAs_Read, Write_Sectors_Tot_Ct, Total_Host_Sector_Write, ... + if (app_regex_partial_match("/(^|_)(LBAs?|Sectors?)_(Write|Written|Read)(_|$)/i", reported_name) + || app_regex_partial_match("/(^|_)(Write|Written|Read)_(LBAs?|Sectors?)(_|$)/i", reported_name)) { + return ata_sector_size; + } + + return std::nullopt; +} + + + +std::optional ata_statistic_get_data_unit_size(const std::string& reported_name) +{ + if (reported_name == "Logical Sectors Written" || reported_name == "Logical Sectors Read") { + return ata_sector_size; + } + return std::nullopt; +} + + + +std::optional nvme_attribute_get_data_unit_size(const std::string& generic_name) +{ + if (generic_name == "nvme_smart_health_information_log/data_units_read" + || generic_name == "nvme_smart_health_information_log/data_units_written") { + return nvme_data_unit_size; + } + return std::nullopt; +} + + + +std::optional data_amount_to_bytes(std::int64_t count, std::uint64_t unit_size) +{ + if (count <= 0 || unit_size == 0) { + return std::nullopt; + } + const auto ucount = static_cast(count); + if (ucount > std::numeric_limits::max() / unit_size) { + return std::nullopt; // overflow + } + return ucount * unit_size; +} + + + +void storage_property_autoset_data_amount(StorageProperty& p) +{ + if (p.is_value_type()) { + auto& attr = std::get(p.value); + // Only plain numbers. Some raw values contain additional information, e.g. "37 (Min/Max 20/45)". + if (hz::number_to_string_nolocale(attr.raw_value_int) != attr.raw_value) { + return; + } + if (auto unit_size = ata_attribute_get_data_unit_size(p.reported_name)) { + attr.raw_value_bytes = data_amount_to_bytes(attr.raw_value_int, unit_size.value()); + } + + } else if (p.is_value_type()) { + auto& stat = std::get(p.value); + if (stat.is_header || hz::number_to_string_nolocale(stat.value_int) != stat.value) { + return; + } + if (auto unit_size = ata_statistic_get_data_unit_size(p.reported_name)) { + stat.value_bytes = data_amount_to_bytes(stat.value_int, unit_size.value()); + } + + } else if (p.is_value_type()) { + if (auto unit_size = nvme_attribute_get_data_unit_size(p.generic_name)) { + const auto count = p.get_value(); + if (auto bytes = data_amount_to_bytes(count, unit_size.value())) { + p.readable_value = hz::number_to_string_locale(count) + " [" + hz::format_size(bytes.value(), true) + "]"; + } + } + } +} + + + +/// @} diff --git a/src/applib/storage_property_data_amount.h b/src/applib/storage_property_data_amount.h new file mode 100644 index 0000000..872e852 --- /dev/null +++ b/src/applib/storage_property_data_amount.h @@ -0,0 +1,53 @@ +/****************************************************************************** +License: GNU General Public License v3.0 only +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib +/// \weakgroup applib +/// @{ + +#ifndef STORAGE_PROPERTY_DATA_AMOUNT_H +#define STORAGE_PROPERTY_DATA_AMOUNT_H + +#include +#include +#include + +#include "storage_property.h" + + + +/// Get the size (in bytes) of one unit of an ATA attribute raw value, if the attribute +/// reports an amount of data, judging by its name (as reported by smartctl). +/// For example, Total_LBAs_Written -> 512, Host_Writes_32MiB -> 32 MiB, NAND_Writes_1GiB -> 1 GiB. +[[nodiscard]] std::optional ata_attribute_get_data_unit_size(const std::string& reported_name); + + +/// Get the size (in bytes) of one unit of an ATA Device Statistics value, if the statistic +/// reports an amount of data (e.g. "Logical Sectors Written" -> 512). +[[nodiscard]] std::optional ata_statistic_get_data_unit_size(const std::string& reported_name); + + +/// Get the size (in bytes) of one unit of an NVMe health information value, if it reports +/// an amount of data (e.g. "Data Units Written" -> 512,000). +[[nodiscard]] std::optional nvme_attribute_get_data_unit_size(const std::string& generic_name); + + +/// Convert a count of units to bytes. +/// \return std::nullopt if count is not positive, or on overflow. +[[nodiscard]] std::optional data_amount_to_bytes(std::int64_t count, std::uint64_t unit_size); + + +/// If the property reports an amount of data, set its value in bytes, so that it's displayed +/// in human-readable form (e.g. "11,538,326,532 [5.91 TB]"). +/// This handles ATA attributes, ATA Device Statistics and NVMe health information. +void storage_property_autoset_data_amount(StorageProperty& p); + + + +#endif + +/// @} diff --git a/src/applib/storage_property_descr.cpp b/src/applib/storage_property_descr.cpp index 2418bfa..7036590 100644 --- a/src/applib/storage_property_descr.cpp +++ b/src/applib/storage_property_descr.cpp @@ -21,6 +21,7 @@ Copyright: #include "storage_property_descr.h" #include "warning_colors.h" #include "storage_property_descr_ata_attribute.h" +#include "storage_property_data_amount.h" #include "storage_property_descr_ata_statistic.h" #include "storage_property_descr_nvme_attribute.h" @@ -344,6 +345,7 @@ StoragePropertyRepository StoragePropertyProcessor::process_properties( StoragePropertyRepository properties, StorageDeviceDetectedType device_type) { for (auto& p : properties.get_properties_ref()) { + storage_property_autoset_data_amount(p); storage_property_autoset_description(p, device_type); storage_property_autoset_warning(p); storage_property_autoset_warning_descr(p); // append warning to description diff --git a/src/applib/tests/CMakeLists.txt b/src/applib/tests/CMakeLists.txt index 69ac7a5..c678276 100644 --- a/src/applib/tests/CMakeLists.txt +++ b/src/applib/tests/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(applib_tests PRIVATE test_selftest.cpp test_smartctl_parser.cpp test_smartctl_version_parser.cpp + test_storage_property_data_amount.cpp ) target_link_libraries(applib_tests PRIVATE applib diff --git a/src/applib/tests/test_storage_property_data_amount.cpp b/src/applib/tests/test_storage_property_data_amount.cpp new file mode 100644 index 0000000..b517ef2 --- /dev/null +++ b/src/applib/tests/test_storage_property_data_amount.cpp @@ -0,0 +1,158 @@ +/****************************************************************************** +License: BSD Zero Clause License +Copyright: + (C) 2026 Alexander Shaduri +******************************************************************************/ +/// \file +/// \author Alexander Shaduri +/// \ingroup applib_tests +/// \weakgroup applib_tests +/// @{ + +#include "catch2/catch.hpp" + +#include "applib/storage_property_data_amount.h" +#include "hz/string_num.h" +#include +#include +#include + + + +namespace { + + constexpr std::uint64_t mib = 1024ULL * 1024; + constexpr std::uint64_t gib = 1024ULL * 1024 * 1024; + + + StorageProperty make_attribute(const std::string& reported_name, std::int64_t raw, const std::string& raw_str = {}) + { + AtaStorageAttribute attr; + attr.id = 241; + attr.raw_value_int = raw; + attr.raw_value = raw_str.empty() ? hz::number_to_string_nolocale(raw) : raw_str; + StorageProperty p; + p.reported_name = reported_name; + p.section = StoragePropertySection::AtaAttributes; + p.value = attr; + return p; + } + +} + + + +TEST_CASE("AtaAttributeDataUnitSize", "[app][data_amount]") +{ + SECTION("Sector / LBA counts") + { + REQUIRE(ata_attribute_get_data_unit_size("Total_LBAs_Written") == 512); + REQUIRE(ata_attribute_get_data_unit_size("Total_LBAs_Read") == 512); + REQUIRE(ata_attribute_get_data_unit_size("Host_LBAs_Written") == 512); + REQUIRE(ata_attribute_get_data_unit_size("Write_Sectors_Tot_Ct") == 512); + REQUIRE(ata_attribute_get_data_unit_size("Read_Sectors_Tot_Ct") == 512); + REQUIRE(ata_attribute_get_data_unit_size("Total_Host_Sector_Write") == 512); + } + + SECTION("Explicit units") + { + REQUIRE(ata_attribute_get_data_unit_size("Host_Writes_32MiB") == 32 * mib); + REQUIRE(ata_attribute_get_data_unit_size("Host_Reads_32MiB") == 32 * mib); + REQUIRE(ata_attribute_get_data_unit_size("Flash_Writes_32MiB") == 32 * mib); + REQUIRE(ata_attribute_get_data_unit_size("NAND_Writes_1GiB") == gib); + REQUIRE(ata_attribute_get_data_unit_size("Lifetime_Writes_GiB") == gib); + REQUIRE(ata_attribute_get_data_unit_size("Total_NAND_Prog_Ct_GiB") == gib); + REQUIRE(ata_attribute_get_data_unit_size("Host_Writes_MiB") == mib); + } + + SECTION("Not data amounts") + { + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Reallocated_Sector_Ct").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Current_Pending_Sector").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Offline_Uncorrectable").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Raw_Read_Error_Rate").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Sector_Read_Errors").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Power_On_Hours").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Unknown_SSD_Attribute").has_value()); + REQUIRE_FALSE(ata_attribute_get_data_unit_size("Host_Writes_0MiB").has_value()); + } +} + + + +TEST_CASE("OtherDataUnitSizes", "[app][data_amount]") +{ + REQUIRE(ata_statistic_get_data_unit_size("Logical Sectors Written") == 512); + REQUIRE(ata_statistic_get_data_unit_size("Logical Sectors Read") == 512); + REQUIRE_FALSE(ata_statistic_get_data_unit_size("Number of Reallocated Logical Sectors").has_value()); + + REQUIRE(nvme_attribute_get_data_unit_size("nvme_smart_health_information_log/data_units_written") == 512000); + REQUIRE(nvme_attribute_get_data_unit_size("nvme_smart_health_information_log/data_units_read") == 512000); + REQUIRE_FALSE(nvme_attribute_get_data_unit_size("nvme_smart_health_information_log/host_reads").has_value()); +} + + + +TEST_CASE("DataAmountToBytes", "[app][data_amount]") +{ + REQUIRE(data_amount_to_bytes(10, 512) == 5120); + REQUIRE_FALSE(data_amount_to_bytes(0, 512).has_value()); + REQUIRE_FALSE(data_amount_to_bytes(-1, 512).has_value()); + REQUIRE_FALSE(data_amount_to_bytes(10, 0).has_value()); + REQUIRE_FALSE(data_amount_to_bytes(std::numeric_limits::max(), 512).has_value()); // overflow +} + + + +TEST_CASE("AutosetDataAmount", "[app][data_amount]") +{ + SECTION("ATA attribute") + { + auto p = make_attribute("Total_LBAs_Written", 11538326532); + storage_property_autoset_data_amount(p); + const auto& attr = p.get_value(); + REQUIRE(attr.raw_value_bytes == 11538326532ULL * 512); + REQUIRE(attr.format_raw_value().find("[5.91 TB]") != std::string::npos); + } + + SECTION("ATA attribute with non-numeric raw value is left alone") + { + auto p = make_attribute("Total_LBAs_Written", 100, "100 (some info)"); + storage_property_autoset_data_amount(p); + REQUIRE_FALSE(p.get_value().raw_value_bytes.has_value()); + } + + SECTION("Unrelated ATA attribute is left alone") + { + auto p = make_attribute("Power_On_Hours", 1000); + storage_property_autoset_data_amount(p); + REQUIRE_FALSE(p.get_value().raw_value_bytes.has_value()); + } + + SECTION("ATA statistic") + { + AtaStorageStatistic stat; + stat.value_int = 2000000000; + stat.value = "2000000000"; + StorageProperty p; + p.reported_name = "Logical Sectors Written"; + p.section = StoragePropertySection::Statistics; + p.value = stat; + storage_property_autoset_data_amount(p); + REQUIRE(p.get_value().value_bytes == 2000000000ULL * 512); + REQUIRE(p.get_value().format_value().find("[1.02 TB]") != std::string::npos); + } + + SECTION("NVMe data units") + { + StorageProperty p; + p.generic_name = "nvme_smart_health_information_log/data_units_written"; + p.value = std::int64_t(16174087); + storage_property_autoset_data_amount(p); + REQUIRE(p.readable_value.find("[8.28 TB]") != std::string::npos); + } +} + + + +/// @}