Skip to content

Commit

Permalink
Simplify collecting statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 5, 2023
1 parent 8fddfc4 commit 41717d0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 27 deletions.
16 changes: 12 additions & 4 deletions cpp/devices/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,20 @@ bool Disk::SetConfiguredSectorSize(const DeviceFactory& device_factory, uint32_t
return true;
}

statistics_map Disk::GetStatistics()
vector<PbStatistics> Disk::GetStatistics()
{
statistics_map statistics;
vector<PbStatistics> 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;
}
2 changes: 1 addition & 1 deletion cpp/devices/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PbStatistics> GetStatistics();

private:

Expand Down
16 changes: 12 additions & 4 deletions cpp/devices/disk_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,20 @@ void DiskCache::UpdateSerialNumber()
}
}

statistics_map DiskCache::GetStatistics()
vector<PbStatistics> DiskCache::GetStatistics()
{
statistics_map statistics;
vector<PbStatistics> 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;
}
4 changes: 3 additions & 1 deletion cpp/devices/disk_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

#pragma once

#include "generated/piscsi_interface.pb.h"
#include <span>
#include <array>
#include <memory>
#include <string>

using namespace std;
using namespace piscsi_interface;

class DiskCache
{
Expand Down Expand Up @@ -50,7 +52,7 @@ class DiskCache
bool ReadSector(span<uint8_t>, uint32_t); // Sector Read
bool WriteSector(span<const uint8_t>, uint32_t); // Sector Write

static statistics_map GetStatistics();
static vector<PbStatistics> GetStatistics();

private:

Expand Down
16 changes: 12 additions & 4 deletions cpp/devices/disk_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,20 @@ bool DiskTrack::WriteSector(span<const uint8_t> buf, int sec)
return true;
}

statistics_map DiskTrack::GetStatistics()
vector<PbStatistics> DiskTrack::GetStatistics()
{
statistics_map statistics;
vector<PbStatistics> 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;
}
4 changes: 1 addition & 3 deletions cpp/devices/disk_track.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
using namespace std;
using namespace piscsi_interface;

using statistics_map = unordered_multimap<PbStatisticsCategory, pair<string, uint32_t>>;

class DiskTrack
{
struct {
Expand Down Expand Up @@ -55,7 +53,7 @@ class DiskTrack
DiskTrack(DiskTrack&) = delete;
DiskTrack& operator=(const DiskTrack&) = delete;

static statistics_map GetStatistics();
static vector<PbStatistics> GetStatistics();

private:

Expand Down
10 changes: 5 additions & 5 deletions cpp/piscsi/piscsi_response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
10 changes: 6 additions & 4 deletions cpp/piscsi/statistics_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

#include "statistics_collector.h"

statistics_map StatisticsCollector::GetStatistics() const
vector<PbStatistics> 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;
}
4 changes: 3 additions & 1 deletion cpp/piscsi/statistics_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

#include "devices/disk.h"

using namespace piscsi_interface;

class StatisticsCollector
{
friend class PiscsiResponse;

StatisticsCollector() = default;
~StatisticsCollector() = default;

statistics_map GetStatistics() const;
vector<PbStatistics> GetStatistics() const;
};

0 comments on commit 41717d0

Please sign in to comment.