fix atomic

This commit is contained in:
2025-12-10 21:15:39 +01:00
parent a4f15460d3
commit 4b0ec380b1
6 changed files with 134 additions and 19 deletions

View File

@ -33,16 +33,18 @@ public:
NVME
};
struct
struct ShredSpeed
{
time_t u32ShredTimeDelta;
std::chrono::time_point<std::chrono::system_clock> chronoShredTimestamp;
std::chrono::time_point<std::chrono::system_clock>
chronoShredTimestamp;
unsigned long ulWrittenBytes;
unsigned long ulSpeedMetricBytesWritten;
} sShredSpeed;
};
std::atomic<TaskState> state;
std::atomic<ConnectionType> connectionType;
std::atomic<ShredSpeed> sShredSpeed;
bool bWasShredded = false; // all shred iterations done
bool bWasShredStarted = false; // shred was atleast once started
@ -76,6 +78,18 @@ private:
protected:
public:
// Copy constructor
Drive(const Drive &other);
// Copy assignment operator
Drive &operator=(const Drive &other);
// Move constructor
Drive(Drive &&other) noexcept;
// Move assignment operator
Drive &operator=(Drive &&other) noexcept;
Drive(std::string path)
{
this->sPath = path;

View File

@ -18,7 +18,7 @@ DCOMPILE_FLAGS = -D DEBUG
# Add additional include paths
INCLUDES = include
# General linker settings
LINK_FLAGS = -Llib -lpthread -lncurses -ltfng
LINK_FLAGS = -Llib -lpthread -lncurses -ltfng -latomic
# Doc
DOCDIR = doc

View File

@ -8,6 +8,94 @@
#include "../include/reHDD.h"
using namespace std;
// Copy constructor
Drive::Drive(const Drive &other)
: state(other.state.load()),
connectionType(other.connectionType.load()),
sShredSpeed(other.sShredSpeed.load()),
bWasShredded(other.bWasShredded),
bWasShredStarted(other.bWasShredStarted),
bWasChecked(other.bWasChecked),
bWasDeleted(other.bWasDeleted),
bIsOffline(other.bIsOffline),
u32DriveChecksumAfterShredding(other.u32DriveChecksumAfterShredding),
sPath(other.sPath),
u32Timestamp(other.u32Timestamp),
d32TaskPercentage(other.d32TaskPercentage),
u32TimestampTaskStart(other.u32TimestampTaskStart),
u32TaskDuration(other.u32TaskDuration),
sSmartData(other.sSmartData)
{
}
// Copy assignment operator
Drive &Drive::operator=(const Drive &other)
{
if (this != &other)
{
state = other.state.load();
connectionType = other.connectionType.load();
sShredSpeed = other.sShredSpeed.load();
bWasShredded = other.bWasShredded;
bWasShredStarted = other.bWasShredStarted;
bWasChecked = other.bWasChecked;
bWasDeleted = other.bWasDeleted;
bIsOffline = other.bIsOffline;
u32DriveChecksumAfterShredding = other.u32DriveChecksumAfterShredding;
sPath = other.sPath;
u32Timestamp = other.u32Timestamp;
d32TaskPercentage = other.d32TaskPercentage;
u32TimestampTaskStart = other.u32TimestampTaskStart;
u32TaskDuration = other.u32TaskDuration;
sSmartData = other.sSmartData;
}
return *this;
}
// Move constructor
Drive::Drive(Drive &&other) noexcept
: state(other.state.load()),
connectionType(other.connectionType.load()),
sShredSpeed(other.sShredSpeed.load()),
bWasShredded(other.bWasShredded),
bWasShredStarted(other.bWasShredStarted),
bWasChecked(other.bWasChecked),
bWasDeleted(other.bWasDeleted),
bIsOffline(other.bIsOffline),
u32DriveChecksumAfterShredding(other.u32DriveChecksumAfterShredding),
sPath(std::move(other.sPath)),
u32Timestamp(other.u32Timestamp),
d32TaskPercentage(other.d32TaskPercentage),
u32TimestampTaskStart(other.u32TimestampTaskStart),
u32TaskDuration(other.u32TaskDuration),
sSmartData(std::move(other.sSmartData))
{
}
// Move assignment operator
Drive &Drive::operator=(Drive &&other) noexcept
{
if (this != &other)
{
state = other.state.load();
connectionType = other.connectionType.load();
sShredSpeed = other.sShredSpeed.load();
bWasShredded = other.bWasShredded;
bWasShredStarted = other.bWasShredStarted;
bWasChecked = other.bWasChecked;
bWasDeleted = other.bWasDeleted;
bIsOffline = other.bIsOffline;
u32DriveChecksumAfterShredding = other.u32DriveChecksumAfterShredding;
sPath = std::move(other.sPath);
u32Timestamp = other.u32Timestamp;
d32TaskPercentage = other.d32TaskPercentage;
u32TimestampTaskStart = other.u32TimestampTaskStart;
u32TaskDuration = other.u32TaskDuration;
sSmartData = std::move(other.sSmartData);
}
return *this;
}
string Drive::getPath(void)
{
return sPath;

View File

@ -359,8 +359,8 @@ void reHDD::searchDrives(std::list<Drive> *plistDrives)
"Drive found: " + tmpDrive.getPath() +
" (type: " +
(tmpDrive.connectionType == Drive::ConnectionType::USB ? "USB" : tmpDrive.connectionType == Drive::ConnectionType::SATA ? "SATA"
: tmpDrive.connectionType == Drive::ConnectionType::NVME ? "NVME"
: "UNKNOWN") +
: tmpDrive.connectionType == Drive::ConnectionType::NVME ? "NVME"
: "UNKNOWN") +
")");
}
@ -571,13 +571,15 @@ void reHDD::updateShredMetrics(list<Drive> *plistDrives)
Drive *pTmpDrive = iterator_to_pointer<Drive, std::list<Drive>::iterator>(it);
// set metrics for calculating shred speed
std::chrono::time_point<std::chrono::system_clock> chronoCurrentTimestamp = std::chrono::system_clock::now();
time_t u32ShredTimeDelta = (chronoCurrentTimestamp - pTmpDrive->sShredSpeed.chronoShredTimestamp).count();
auto shredSpeed = pTmpDrive->sShredSpeed.load();
time_t u32ShredTimeDelta = (chronoCurrentTimestamp - shredSpeed.chronoShredTimestamp).count();
if (u32ShredTimeDelta > METRIC_THRESHOLD)
{
pTmpDrive->sShredSpeed.u32ShredTimeDelta = u32ShredTimeDelta;
pTmpDrive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();
pTmpDrive->sShredSpeed.ulWrittenBytes = pTmpDrive->sShredSpeed.ulSpeedMetricBytesWritten;
pTmpDrive->sShredSpeed.ulSpeedMetricBytesWritten = 0U;
shredSpeed.u32ShredTimeDelta = u32ShredTimeDelta;
shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();
shredSpeed.ulWrittenBytes = shredSpeed.ulSpeedMetricBytesWritten;
shredSpeed.ulSpeedMetricBytesWritten = 0U;
pTmpDrive->sShredSpeed.store(shredSpeed);
}
}
}

View File

@ -286,7 +286,9 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
ulDriveByteCounter += iReadBytes;
ulDriveByteOverallCount += iReadBytes;
d32Percent = this->calcProgress();
drive->sShredSpeed.ulSpeedMetricBytesWritten += iReadBytes;
auto shredSpeed = drive->sShredSpeed.load();
shredSpeed.ulSpeedMetricBytesWritten += iReadBytes;
drive->sShredSpeed.store(shredSpeed);
#ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Shred-Task (Checksum): ByteCount: " + to_string(ulDriveByteCounter) + " - progress: " + to_string(d32Percent) + " - Drive: " + drive->getSerial());

View File

@ -99,9 +99,9 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
string sSpeed = " ";
string sTime = " ";
string sTemp = it->sTemperatureToText();
string sConnection = (it->connectionType == Drive::USB ? "USB" : it->connectionType == Drive::SATA ? "SATA"
: it->connectionType == Drive::NVME ? "NVME"
: "");
string sConnection = (it->connectionType == Drive::ConnectionType::USB ? "USB" : it->connectionType == Drive::ConnectionType::SATA ? "SATA"
: it->connectionType == Drive::ConnectionType::NVME ? "NVME"
: "");
bool bSelectedEntry = false;
@ -123,29 +123,38 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
switch (it->state)
{
case Drive::TaskState::SHRED_ACTIVE:
{
stream << fixed << setprecision(3) << (it->getTaskPercentage());
sState = "Shredding: " + stream.str() + "%";
it->calculateTaskDuration();
sTime = this->formatTimeDuration(it->getTaskDuration());
sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes);
auto shredSpeed = it->sShredSpeed.load();
sSpeed = this->formatSpeed(shredSpeed.u32ShredTimeDelta, shredSpeed.ulWrittenBytes);
break;
}
case Drive::TaskState::CHECK_ACTIVE:
{
stream << fixed << setprecision(3) << (it->getTaskPercentage());
sState = "Checking: " + stream.str() + "%";
it->calculateTaskDuration();
sTime = this->formatTimeDuration(it->getTaskDuration());
sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes);
auto shredSpeed = it->sShredSpeed.load();
sSpeed = this->formatSpeed(shredSpeed.u32ShredTimeDelta, shredSpeed.ulWrittenBytes);
break;
}
case Drive::TaskState::DELETE_ACTIVE:
{
sState = "Deleting ...";
it->calculateTaskDuration();
sTime = this->formatTimeDuration(it->getTaskDuration());
break;
}
case Drive::TaskState::NONE:
case Drive::TaskState::SHRED_SELECTED:
case Drive::TaskState::DELETE_SELECTED:
{
if (it->bWasDeleted)
{
sState = "DELETED"; // mark drive as deleted previously
@ -172,8 +181,8 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
wrefresh(dialog);
}
#endif
break;
}
case Drive::TaskState::FROZEN:
stream << fixed << setprecision(3) << (it->getTaskPercentage());
#ifdef FROZEN_ALERT