100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
/**
|
|
* @file shred.h
|
|
* @brief shred drive
|
|
* @author hendrik schutter
|
|
* @date 03.05.2020
|
|
*/
|
|
|
|
#ifndef SHRED_H_
|
|
#define SHRED_H_
|
|
|
|
#include "reHDD.h"
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <chrono>
|
|
|
|
// Adaptive chunk size optimization - uncomment to enable
|
|
#define ADAPTIVE_CHUNK_SIZE
|
|
|
|
// Chunk size configuration
|
|
#define CHUNK_SIZE_START 1024 * 1024 * 32 // Starting chunk size: 32MB
|
|
#define CHUNK_SIZE_MIN 1024 * 1024 * 4 // Minimum chunk size: 4MB
|
|
#define CHUNK_SIZE_MAX 1024 * 1024 * 128 // Maximum chunk size: 128MB
|
|
#define CHUNK_SIZE_STEP_UP 1024 * 1024 * 2 // Increase step: 2MB
|
|
#define CHUNK_SIZE_STEP_DOWN 1024 * 1024 * 4 // Decrease step: 4MB
|
|
#define CHUNK_MEASURE_INTERVAL 64 // Measure performance every 64 chunks
|
|
|
|
#ifdef ADAPTIVE_CHUNK_SIZE
|
|
// Use max buffer size when adaptive mode is enabled
|
|
#define CHUNK_SIZE CHUNK_SIZE_MAX
|
|
#define TFNG_DATA_SIZE CHUNK_SIZE_MAX
|
|
#else
|
|
// Use fixed chunk size when adaptive mode is disabled
|
|
#define CHUNK_SIZE CHUNK_SIZE_START
|
|
#define TFNG_DATA_SIZE CHUNK_SIZE
|
|
#endif
|
|
|
|
// #define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB
|
|
// #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB
|
|
// #define DEMO_DRIVE_SIZE 5*1024*1024*1024L // 5GB
|
|
// #define DEMO_DRIVE_SIZE 1024*1024*1024*10L // 10GB
|
|
|
|
typedef int fileDescriptor;
|
|
|
|
class Shred
|
|
{
|
|
protected:
|
|
public:
|
|
Shred();
|
|
~Shred();
|
|
int shredDrive(Drive* drive, int* ipSignalFd);
|
|
|
|
private:
|
|
fileDescriptor randomSrcFileDiscr;
|
|
fileDescriptor driveFileDiscr;
|
|
|
|
#ifdef ADAPTIVE_CHUNK_SIZE
|
|
unsigned char* caTfngData; // Dynamic buffer allocation for adaptive mode
|
|
unsigned char* caReadBuffer; // Dynamic buffer allocation for adaptive mode
|
|
#else
|
|
unsigned char caTfngData[TFNG_DATA_SIZE];
|
|
unsigned char caReadBuffer[CHUNK_SIZE];
|
|
#endif
|
|
|
|
unsigned long ulDriveByteSize;
|
|
unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation
|
|
double d32Percent = 0.0;
|
|
double d32TmpPercent = 0.0;
|
|
|
|
#ifdef ADAPTIVE_CHUNK_SIZE
|
|
// Adaptive chunk size optimization members
|
|
size_t currentChunkSize;
|
|
size_t bestChunkSize;
|
|
unsigned int chunkCounter;
|
|
std::chrono::high_resolution_clock::time_point measurementStartTime;
|
|
double bestThroughputMBps;
|
|
double lastThroughputMBps;
|
|
unsigned long bytesWrittenInMeasurement;
|
|
bool throughputIncreasing;
|
|
|
|
// Adaptive methods
|
|
void startMeasurement();
|
|
void evaluateThroughput(Drive* drive);
|
|
void adjustChunkSize(Drive* drive);
|
|
size_t getCurrentChunkSize() const;
|
|
#endif
|
|
|
|
inline double calcProgress();
|
|
int iRewindDrive(fileDescriptor file);
|
|
long getDriveSizeInBytes(fileDescriptor file);
|
|
unsigned int uiCalcChecksum(fileDescriptor file, Drive* drive, int* ipSignalFd);
|
|
void cleanup();
|
|
};
|
|
|
|
#endif // SHRED_H_
|