From 41717d0628497ce6d6095bc12526f80db9e4c311 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Thu, 5 Oct 2023 23:18:02 +0200 Subject: [PATCH] Simplify collecting statistics --- cpp/devices/disk.cpp | 16 ++++++++++++---- cpp/devices/disk.h | 2 +- cpp/devices/disk_cache.cpp | 16 ++++++++++++---- cpp/devices/disk_cache.h | 4 +++- cpp/devices/disk_track.cpp | 16 ++++++++++++---- cpp/devices/disk_track.h | 4 +--- cpp/piscsi/piscsi_response.cpp | 10 +++++----- cpp/piscsi/statistics_collector.cpp | 10 ++++++---- cpp/piscsi/statistics_collector.h | 4 +++- 9 files changed, 55 insertions(+), 27 deletions(-) diff --git a/cpp/devices/disk.cpp b/cpp/devices/disk.cpp index 796fe3c70e..646d033f8c 100644 --- a/cpp/devices/disk.cpp +++ b/cpp/devices/disk.cpp @@ -716,12 +716,20 @@ bool Disk::SetConfiguredSectorSize(const DeviceFactory& device_factory, uint32_t return true; } -statistics_map Disk::GetStatistics() +vector Disk::GetStatistics() { - statistics_map statistics; + vector statistics; - statistics.emplace(PbStatisticsCategory::INFO, make_pair(SECTOR_READ_COUNT, sector_read_count)); - statistics.emplace(PbStatisticsCategory::INFO, make_pair(SECTOR_WRITE_COUNT, sector_write_count)); + PbStatistics s; + s.set_category(PbStatisticsCategory::INFO); + + s.set_key(SECTOR_READ_COUNT); + s.set_value(sector_read_count); + statistics.push_back(s); + + s.set_key(SECTOR_WRITE_COUNT); + s.set_value(sector_write_count); + statistics.push_back(s); return statistics; } diff --git a/cpp/devices/disk.h b/cpp/devices/disk.h index 512d34eede..a8eb64ba8f 100644 --- a/cpp/devices/disk.h +++ b/cpp/devices/disk.h @@ -68,7 +68,7 @@ class Disk : public StorageDevice, private ScsiBlockCommands bool SetConfiguredSectorSize(const DeviceFactory&, uint32_t); void FlushCache() override; - static statistics_map GetStatistics(); + static vector GetStatistics(); private: diff --git a/cpp/devices/disk_cache.cpp b/cpp/devices/disk_cache.cpp index a06a16b3fb..bea9d2e319 100644 --- a/cpp/devices/disk_cache.cpp +++ b/cpp/devices/disk_cache.cpp @@ -189,12 +189,20 @@ void DiskCache::UpdateSerialNumber() } } -statistics_map DiskCache::GetStatistics() +vector DiskCache::GetStatistics() { - statistics_map statistics; + vector statistics; - statistics.emplace(PbStatisticsCategory::ERROR, make_pair(READ_ERROR_COUNT, read_error_count)); - statistics.emplace(PbStatisticsCategory::ERROR, make_pair(WRITE_ERROR_COUNT, write_error_count)); + PbStatistics s; + s.set_category(PbStatisticsCategory::ERROR); + + s.set_key(READ_ERROR_COUNT); + s.set_value(read_error_count); + statistics.push_back(s); + + s.set_key(WRITE_ERROR_COUNT); + s.set_value(write_error_count); + statistics.push_back(s); return statistics; } diff --git a/cpp/devices/disk_cache.h b/cpp/devices/disk_cache.h index 165adefd03..15048e2bfc 100644 --- a/cpp/devices/disk_cache.h +++ b/cpp/devices/disk_cache.h @@ -15,12 +15,14 @@ #pragma once +#include "generated/piscsi_interface.pb.h" #include #include #include #include using namespace std; +using namespace piscsi_interface; class DiskCache { @@ -50,7 +52,7 @@ class DiskCache bool ReadSector(span, uint32_t); // Sector Read bool WriteSector(span, uint32_t); // Sector Write - static statistics_map GetStatistics(); + static vector GetStatistics(); private: diff --git a/cpp/devices/disk_track.cpp b/cpp/devices/disk_track.cpp index 052d3330c9..1fede9b81f 100644 --- a/cpp/devices/disk_track.cpp +++ b/cpp/devices/disk_track.cpp @@ -276,12 +276,20 @@ bool DiskTrack::WriteSector(span buf, int sec) return true; } -statistics_map DiskTrack::GetStatistics() +vector DiskTrack::GetStatistics() { - statistics_map statistics; + vector statistics; - statistics.emplace(PbStatisticsCategory::INFO, make_pair(CACHE_MISS_READ_COUNT, cache_miss_read_count)); - statistics.emplace(PbStatisticsCategory::INFO, make_pair(CACHE_MISS_WRITE_COUNT, cache_miss_write_count)); + PbStatistics s; + s.set_category(PbStatisticsCategory::INFO); + + s.set_key(CACHE_MISS_READ_COUNT); + s.set_value(cache_miss_read_count); + statistics.push_back(s); + + s.set_key(CACHE_MISS_WRITE_COUNT); + s.set_value(cache_miss_write_count); + statistics.push_back(s); return statistics; } diff --git a/cpp/devices/disk_track.h b/cpp/devices/disk_track.h index 626bebe7c3..df49f7b767 100644 --- a/cpp/devices/disk_track.h +++ b/cpp/devices/disk_track.h @@ -25,8 +25,6 @@ using namespace std; using namespace piscsi_interface; -using statistics_map = unordered_multimap>; - class DiskTrack { struct { @@ -55,7 +53,7 @@ class DiskTrack DiskTrack(DiskTrack&) = delete; DiskTrack& operator=(const DiskTrack&) = delete; - static statistics_map GetStatistics(); + static vector GetStatistics(); private: diff --git a/cpp/piscsi/piscsi_response.cpp b/cpp/piscsi/piscsi_response.cpp index b9ab09000e..f1797a715d 100644 --- a/cpp/piscsi/piscsi_response.cpp +++ b/cpp/piscsi/piscsi_response.cpp @@ -275,11 +275,11 @@ void PiscsiResponse::GetMappingInfo(PbMappingInfo& mapping_info) const void PiscsiResponse::GetStatisticsInfo(PbStatisticsInfo& statistics_info) const { - for (const auto& [category, item] : collector.GetStatistics()) { - auto statistics = statistics_info.add_statistics(); - statistics->set_category(category); - statistics->set_key(item.first); - statistics->set_value(item.second); + for (const auto& statistics : collector.GetStatistics()) { + auto s = statistics_info.add_statistics(); + s->set_category(statistics.category()); + s->set_key(statistics.key()); + s->set_value(statistics.value()); } } diff --git a/cpp/piscsi/statistics_collector.cpp b/cpp/piscsi/statistics_collector.cpp index 4226c23375..ad01665bf4 100644 --- a/cpp/piscsi/statistics_collector.cpp +++ b/cpp/piscsi/statistics_collector.cpp @@ -9,11 +9,13 @@ #include "statistics_collector.h" -statistics_map StatisticsCollector::GetStatistics() const +vector StatisticsCollector::GetStatistics() const { - statistics_map statistics = DiskTrack::GetStatistics(); - statistics.merge(DiskCache::GetStatistics()); - statistics.merge(Disk::GetStatistics()); + auto statistics = DiskTrack::GetStatistics(); + auto s = DiskCache::GetStatistics(); + statistics.insert(statistics.end(), s.begin(), s.end()); + s = Disk::GetStatistics(); + statistics.insert(statistics.end(), s.begin(), s.end()); return statistics; } diff --git a/cpp/piscsi/statistics_collector.h b/cpp/piscsi/statistics_collector.h index d5d7c0ec31..5676d11b0d 100644 --- a/cpp/piscsi/statistics_collector.h +++ b/cpp/piscsi/statistics_collector.h @@ -11,6 +11,8 @@ #include "devices/disk.h" +using namespace piscsi_interface; + class StatisticsCollector { friend class PiscsiResponse; @@ -18,5 +20,5 @@ class StatisticsCollector StatisticsCollector() = default; ~StatisticsCollector() = default; - statistics_map GetStatistics() const; + vector GetStatistics() const; };