mirror of
https://github.com/ashaduri/gsmartcontrol.git
synced 2026-11-24 23:06:45 +00:00
Show data amounts (written / read) in human-readable form
Attributes, Device Statistics and NVMe health values that count amounts of data are now shown with a human-readable size, e.g. "11,538,326,532 [5.91 TB]". - ATA attributes: detected by the smartctl-reported name, which encodes the unit: LBA / sector counts (Total_LBAs_Written, Write_Sectors_Tot_Ct, Total_Host_Sector_Write, ...) and explicit units (Host_Writes_32MiB, NAND_Writes_1GiB, Lifetime_Writes_GiB, ...). Error / reallocation counters are excluded. - Device Statistics: Logical Sectors Written / Read. - NVMe: Data Units Read / Written (units of 512,000 bytes). - Decimal units (TB) are used, matching drive endurance (TBW) ratings and smartctl's own NVMe output. Fixes #28 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fed68f60e9
commit
e5f2b349f9
8 files changed
+370
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<std::uint64_t> 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<std::uint64_t> 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
|
||||
};
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
/******************************************************************************
|
||||
License: GNU General Public License v3.0 only
|
||||
Copyright:
|
||||
(C) 2026 Alexander Shaduri <ashaduri@gmail.com>
|
||||
******************************************************************************/
|
||||
/// \file
|
||||
/// \author Alexander Shaduri
|
||||
/// \ingroup applib
|
||||
/// \weakgroup applib
|
||||
/// @{
|
||||
|
||||
#include "storage_property_data_amount.h"
|
||||
|
||||
#include <cctype> // std::toupper
|
||||
#include <limits>
|
||||
#include <variant>
|
||||
|
||||
#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<std::uint64_t> 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<unsigned char>(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<std::uint64_t>::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<std::uint64_t> 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<std::uint64_t> 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<std::uint64_t> 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<std::uint64_t>(count);
|
||||
if (ucount > std::numeric_limits<std::uint64_t>::max() / unit_size) {
|
||||
return std::nullopt; // overflow
|
||||
}
|
||||
return ucount * unit_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void storage_property_autoset_data_amount(StorageProperty& p)
|
||||
{
|
||||
if (p.is_value_type<AtaStorageAttribute>()) {
|
||||
auto& attr = std::get<AtaStorageAttribute>(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<AtaStorageStatistic>()) {
|
||||
auto& stat = std::get<AtaStorageStatistic>(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<int64_t>()) {
|
||||
if (auto unit_size = nvme_attribute_get_data_unit_size(p.generic_name)) {
|
||||
const auto count = p.get_value<int64_t>();
|
||||
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) + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// @}
|
||||
@@ -0,0 +1,53 @@
|
||||
/******************************************************************************
|
||||
License: GNU General Public License v3.0 only
|
||||
Copyright:
|
||||
(C) 2026 Alexander Shaduri <ashaduri@gmail.com>
|
||||
******************************************************************************/
|
||||
/// \file
|
||||
/// \author Alexander Shaduri
|
||||
/// \ingroup applib
|
||||
/// \weakgroup applib
|
||||
/// @{
|
||||
|
||||
#ifndef STORAGE_PROPERTY_DATA_AMOUNT_H
|
||||
#define STORAGE_PROPERTY_DATA_AMOUNT_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#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<std::uint64_t> 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<std::uint64_t> 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<std::uint64_t> 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<std::uint64_t> 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
|
||||
|
||||
/// @}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
/******************************************************************************
|
||||
License: BSD Zero Clause License
|
||||
Copyright:
|
||||
(C) 2026 Alexander Shaduri <ashaduri@gmail.com>
|
||||
******************************************************************************/
|
||||
/// \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 <cstdint>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
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<std::int64_t>::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<AtaStorageAttribute>();
|
||||
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<AtaStorageAttribute>().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<AtaStorageAttribute>().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<AtaStorageStatistic>().value_bytes == 2000000000ULL * 512);
|
||||
REQUIRE(p.get_value<AtaStorageStatistic>().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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// @}
|
||||
Reference in new issue
Block a user