Skip to content

Commit

Permalink
stat: Use float to represent storage percentage
Browse files Browse the repository at this point in the history
If a storage size is relatively big while an update size is small it's
more convenient to see/print more accurate percentage instead of
rounding it.

Signed-off-by: Mike Sul <[email protected]>
  • Loading branch information
mike-sul committed Sep 5, 2023
1 parent 92963a6 commit e3e1d8e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/storage/stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ Volume::UsageInfo Volume::getUsageInfo(const std::string& path, unsigned int res
return UsageInfo{.err = err};
}

UsageInfo::Type free{
stat.blockSize * stat.freeBlockNumber,
static_cast<unsigned int>(std::floor((static_cast<double>(stat.freeBlockNumber) / stat.blockNumb) * 100))};
UsageInfo::Type free{stat.blockSize * stat.freeBlockNumber,
(static_cast<double>(stat.freeBlockNumber) / stat.blockNumb) * 100};
UsageInfo::Type reserved{
stat.blockSize * static_cast<uint64_t>(std::ceil(stat.blockNumb * (reserved_percentage / 100.0))),
reserved_percentage};
Expand All @@ -64,7 +63,7 @@ Volume::UsageInfo Volume::getUsageInfo(const std::string& path, unsigned int res

Volume::UsageInfo& Volume::UsageInfo::withRequired(const uint64_t& val) {
if (isOk() && size.first > 0) {
required = {val, std::ceil((static_cast<double>(val) / size.first) * 100)};
required = {val, (static_cast<double>(val) / size.first) * 100};
} else {
required = {val, 0};
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace storage {
struct Volume {
struct UsageInfo {
// <bytes, percentage of overall volume capacity>
using Type = std::pair<uint64_t, unsigned int>;
using Type = std::pair<uint64_t, float>;

std::string path;
Type size;
Expand Down
3 changes: 3 additions & 0 deletions tests/nospace_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ TEST(StorageStat, UsageInfo) {
SetBlockSize(block_size);
SetFreeBlockNumb(std::ceil(block_numb * (free_percentage / 100.0)), block_numb);
storage::Volume::UsageInfo usage_info{storage::Volume::getUsageInfo("./", reserved_percentage)};
usage_info.free.second = std::round(usage_info.free.second);
usage_info.available.second = std::round(usage_info.available.second);
ASSERT_TRUE(usage_info.isOk());
ASSERT_EQ(free, usage_info.free) << usage_info.free.first;
ASSERT_EQ(reserved, usage_info.reserved) << usage_info.reserved.first;
Expand All @@ -194,6 +196,7 @@ TEST(StorageStat, UsageInfo) {
SetBlockSize(block_size);
SetFreeBlockNumb(std::ceil(block_numb * (free_percentage / 100.0)), block_numb);
storage::Volume::UsageInfo usage_info{storage::Volume::getUsageInfo("./", reserved_percentage)};
usage_info.free.second = std::round(usage_info.free.second);
ASSERT_TRUE(usage_info.isOk());
ASSERT_EQ(free, usage_info.free) << usage_info.free.first;
ASSERT_EQ(reserved, usage_info.reserved) << usage_info.reserved.first;
Expand Down

0 comments on commit e3e1d8e

Please sign in to comment.