Skip to content

Commit

Permalink
Update handling of cache statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 14, 2025
1 parent 0a79a6a commit c9b2d21
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 52 deletions.
4 changes: 2 additions & 2 deletions cpp/base/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class Device // NOSONAR The number of fields and methods is justified, the compl

logger& GetLogger() const;

void EnrichStatistics(vector<PbStatistics>&, PbStatisticsCategory, const string&, uint64_t) const;

protected:

Device(PbDeviceType type, int lun) : type(type), lun(lun)
Expand Down Expand Up @@ -168,8 +170,6 @@ class Device // NOSONAR The number of fields and methods is justified, the compl
void LogWarn(const string&) const;
void LogError(const string&) const;

void EnrichStatistics(vector<PbStatistics>&, PbStatisticsCategory, const string&, uint64_t) const;

private:

const PbDeviceType type;
Expand Down
5 changes: 3 additions & 2 deletions cpp/devices/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//
// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi
//
// Copyright (C) 2024 Uwe Seimet
// Copyright (C) 2024-2025 Uwe Seimet
//
//---------------------------------------------------------------------------

#pragma once

#include "base/device.h"
#include "shared/s2p_defs.h"
#include "generated/s2p_interface.pb.h"

Expand All @@ -28,7 +29,7 @@ class Cache

virtual bool Init() = 0;

virtual vector<PbStatistics> GetStatistics(bool) const = 0;
virtual vector<PbStatistics> GetStatistics(const Device&, bool) const = 0;

protected:

Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ vector<PbStatistics> Disk::GetStatistics() const

// Enrich cache statistics with device information before adding them to device statistics
if (cache) {
for (auto &s : cache->GetStatistics(IsReadOnly())) {
for (auto &s : cache->GetStatistics(*this, IsReadOnly())) {
s.set_id(GetId());
s.set_unit(GetLun());
statistics.push_back(s);
Expand Down
29 changes: 5 additions & 24 deletions cpp/devices/disk_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,15 @@ void DiskCache::UpdateSerial()
}
}

vector<PbStatistics> DiskCache::GetStatistics(bool is_read_only) const
vector<PbStatistics> DiskCache::GetStatistics(const Device &device, bool is_read_only) const
{
vector<PbStatistics> statistics;

PbStatistics s;

s.set_category(PbStatisticsCategory::CATEGORY_INFO);

s.set_key(CACHE_MISS_READ_COUNT);
s.set_value(cache_miss_read_count);
statistics.push_back(s);

if (!is_read_only) {
s.set_key(CACHE_MISS_WRITE_COUNT);
s.set_value(cache_miss_write_count);
statistics.push_back(s);
}

s.set_category(PbStatisticsCategory::CATEGORY_ERROR);

s.set_key(READ_ERROR_COUNT);
s.set_value(read_error_count);
statistics.push_back(s);

device.EnrichStatistics(statistics, CATEGORY_INFO, CACHE_MISS_READ_COUNT, cache_miss_read_count);
device.EnrichStatistics(statistics, CATEGORY_ERROR, READ_ERROR_COUNT, read_error_count);
if (!is_read_only) {
s.set_key(WRITE_ERROR_COUNT);
s.set_value(write_error_count);
statistics.push_back(s);
device.EnrichStatistics(statistics, CATEGORY_INFO, CACHE_MISS_WRITE_COUNT, cache_miss_write_count);
device.EnrichStatistics(statistics, CATEGORY_ERROR, WRITE_ERROR_COUNT, write_error_count);
}

return statistics;
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/disk_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DiskCache : public Cache
int ReadSectors(data_in_t, uint64_t, uint32_t) override;
int WriteSectors(data_out_t, uint64_t, uint32_t) override;

vector<PbStatistics> GetStatistics(bool) const override;
vector<PbStatistics> GetStatistics(const Device&, bool) const override;

private:

Expand Down
17 changes: 4 additions & 13 deletions cpp/devices/linux_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi
//
// Copyright (C) 2024 Uwe Seimet
// Copyright (C) 2024-2025 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand Down Expand Up @@ -84,22 +84,13 @@ bool LinuxCache::Flush()
return true;
}

vector<PbStatistics> LinuxCache::GetStatistics(bool is_read_only) const
vector<PbStatistics> LinuxCache::GetStatistics(const Device &device, bool is_read_only) const
{
vector<PbStatistics> statistics;

PbStatistics s;

s.set_category(PbStatisticsCategory::CATEGORY_ERROR);

s.set_key(READ_ERROR_COUNT);
s.set_value(read_error_count);
statistics.push_back(s);

device.EnrichStatistics(statistics, CATEGORY_ERROR, READ_ERROR_COUNT, read_error_count);
if (!is_read_only) {
s.set_key(WRITE_ERROR_COUNT);
s.set_value(write_error_count);
statistics.push_back(s);
device.EnrichStatistics(statistics, CATEGORY_ERROR, WRITE_ERROR_COUNT, write_error_count);
}

return statistics;
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/linux_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LinuxCache : public Cache

bool Flush() override;

vector<PbStatistics> GetStatistics(bool) const override;
vector<PbStatistics> GetStatistics(const Device&, bool) const override;

private:

Expand Down
9 changes: 5 additions & 4 deletions cpp/test/disk_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//
// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi
//
// Copyright (C) 2024 Uwe Seimet
// Copyright (C) 2024-2025 Uwe Seimet
//
//---------------------------------------------------------------------------

#include <gtest/gtest.h>
#include "mocks.h"
#include "devices/disk_cache.h"
#include "test_shared.h"

Expand Down Expand Up @@ -40,8 +40,9 @@ TEST(DiskCache, ReadWriteSectors)

TEST(DiskCache, GetStatistics)
{
MockDevice device(0);
DiskCache cache("", 512, 0);

EXPECT_EQ(2U, cache.GetStatistics(true).size());
EXPECT_EQ(4U, cache.GetStatistics(false).size());
EXPECT_EQ(2U, cache.GetStatistics(device, true).size());
EXPECT_EQ(4U, cache.GetStatistics(device, false).size());
}
9 changes: 5 additions & 4 deletions cpp/test/linux_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//
// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi
//
// Copyright (C) 2024 Uwe Seimet
// Copyright (C) 2024-2025 Uwe Seimet
//
//---------------------------------------------------------------------------

#include <gtest/gtest.h>
#include "mocks.h"
#include "devices/linux_cache.h"
#include "test_shared.h"

Expand Down Expand Up @@ -75,8 +75,9 @@ TEST(LinuxCache, Flush)

TEST(LinuxCache, GetStatistics)
{
MockDevice device(0);
LinuxCache cache("", 0, 0, false);

EXPECT_EQ(1U, cache.GetStatistics(true).size());
EXPECT_EQ(2U, cache.GetStatistics(false).size());
EXPECT_EQ(1U, cache.GetStatistics(device, true).size());
EXPECT_EQ(2U, cache.GetStatistics(device, false).size());
}

0 comments on commit c9b2d21

Please sign in to comment.