Rework the portable support

This commit is contained in:
Andrea Mazzoleni 2026-03-02 01:13:19 +01:00
parent 34fc79464b
commit b3879f7f8e
9 changed files with 139 additions and 128 deletions

View file

@ -23,7 +23,7 @@ void bw_init(struct snapraid_bw* bw, uint64_t limit)
{
bw->limit = limit;
bw->total = 0;
bw->start = tick_ms();
bw->start = os_tick_ms();
}
void bw_limit(struct snapraid_bw* bw, uint64_t bytes)
@ -31,7 +31,7 @@ void bw_limit(struct snapraid_bw* bw, uint64_t bytes)
if (!bw || bw->limit == 0)
return;
uint64_t elapsed = tick_ms() - bw->start;
uint64_t elapsed = os_tick_ms() - bw->start;
uint64_t done;
uint64_t eta;

View file

@ -71,7 +71,7 @@ static ULONGLONG(WINAPI * ptr_GetTickCount64)(void);
static windows_key_t last_error;
/**
* Monotone tick counter
* Monotone os_tick counter
*/
static windows_mutex_t tick_lock;
static uint64_t tick_last;
@ -2210,13 +2210,13 @@ size_t windows_direct_size(void)
return si.dwPageSize;
}
uint64_t tick(void)
uint64_t os_tick(void)
{
LARGE_INTEGER t;
uint64_t r;
/*
* Ensure to return a strict monotone tick counter.
* Ensure to return a strict monotone os_tick counter.
*
* We had reports of invalid stats due faulty High Precision Event Timer.
* See: https://sourceforge.net/p/snapraid/discussion/1677233/thread/a2122fd6/
@ -2241,7 +2241,7 @@ uint64_t tick(void)
return r;
}
uint64_t tick_ms(void)
uint64_t os_tick_ms(void)
{
/* GetTickCount64() isn't supported in Windows XP */
if (ptr_GetTickCount64 != 0)
@ -3006,7 +3006,7 @@ static void* thread_spinup(void* arg)
devinfo_t* devinfo = arg;
uint64_t start;
start = tick_ms();
start = os_tick_ms();
if (devup(devinfo->device, devinfo->name, devinfo->wfile) != 0) {
/* LCOV_EXCL_START */
@ -3014,7 +3014,7 @@ static void* thread_spinup(void* arg)
/* LCOV_EXCL_STOP */
}
msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
/* after the spin up, get SMART info */
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
@ -3042,7 +3042,7 @@ static void* thread_spindown(void* arg)
devinfo_t* devinfo = arg;
uint64_t start;
start = tick_ms();
start = os_tick_ms();
if (devdown(devinfo->device, devinfo->name, devinfo->smartctl) != 0) {
/* LCOV_EXCL_START */
@ -3050,7 +3050,7 @@ static void* thread_spindown(void* arg)
/* LCOV_EXCL_STOP */
}
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
return 0;
}
@ -3064,7 +3064,7 @@ static void* thread_spindownifup(void* arg)
uint64_t start;
int power;
start = tick_ms();
start = os_tick_ms();
if (devdownifup(devinfo->device, devinfo->name, devinfo->smartctl, &power) != 0) {
/* LCOV_EXCL_START */
@ -3073,7 +3073,7 @@ static void* thread_spindownifup(void* arg)
}
if (power == POWER_ACTIVE)
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
return 0;
}

View file

@ -398,6 +398,27 @@ size_t windows_direct_size(void);
/****************************************************************************/
/* thread */
#define HAVE_THREAD 1
typedef void* windows_thread_t;
typedef CRITICAL_SECTION windows_mutex_t;
typedef CONDITION_VARIABLE windows_cond_t;
typedef void* windows_key_t;
/* remap to pthread */
#define thread_id_t windows_thread_t
#define thread_mutex_t windows_mutex_t
#define thread_cond_t windows_cond_t
#define pthread_mutex_init windows_mutex_init
#define pthread_mutex_destroy windows_mutex_destroy
#define pthread_mutex_lock windows_mutex_lock
#define pthread_mutex_unlock windows_mutex_unlock
#define pthread_cond_init windows_cond_init
#define pthread_cond_destroy windows_cond_destroy
#define pthread_cond_signal windows_cond_signal
#define pthread_cond_broadcast windows_cond_broadcast
#define pthread_cond_wait windows_cond_wait
#define pthread_create windows_create
#define pthread_join windows_join
/**
* Like the pthread_* equivalent.
*/

View file

@ -46,6 +46,11 @@
*/
#define _WIN32_WINNT 0x600
/**
* Undef as it clashes with windows.h declarations
*/
#undef DATADIR
#include <windows.h>
#endif
@ -208,12 +213,6 @@
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifndef WEXITSTATUS
#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif
#ifndef WIFEXITED
#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif
#if HAVE_GETOPT_H
#include <getopt.h>
@ -232,42 +231,6 @@
#include <DiskArbitration/DiskArbitration.h>
#endif
/**
* Enable thread use.
*/
#ifdef _WIN32
#define HAVE_THREAD 1
typedef void* windows_thread_t;
typedef CRITICAL_SECTION windows_mutex_t;
typedef CONDITION_VARIABLE windows_cond_t;
typedef void* windows_key_t;
/* remap to pthread */
#define thread_id_t windows_thread_t
#define thread_mutex_t windows_mutex_t
#define thread_cond_t windows_cond_t
#define pthread_mutex_init windows_mutex_init
#define pthread_mutex_destroy windows_mutex_destroy
#define pthread_mutex_lock windows_mutex_lock
#define pthread_mutex_unlock windows_mutex_unlock
#define pthread_cond_init windows_cond_init
#define pthread_cond_destroy windows_cond_destroy
#define pthread_cond_signal windows_cond_signal
#define pthread_cond_broadcast windows_cond_broadcast
#define pthread_cond_wait windows_cond_wait
#define pthread_create windows_create
#define pthread_join windows_join
#else
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#if HAVE_PTHREAD_CREATE
#define HAVE_THREAD 1
typedef pthread_t thread_id_t;
typedef pthread_mutex_t thread_mutex_t;
typedef pthread_cond_t thread_cond_t;
#endif
#endif
#if HAVE_IO_H
#include <io.h>
#endif
@ -285,6 +248,64 @@ typedef pthread_cond_t thread_cond_t;
#define HAVE_LOCKFILE 1
#endif
/**
* Includes specific support for Windows or Linux.
*/
#ifdef __MINGW32__
#include "mingw.h"
#else
#include "unix.h"
#endif
/****************************************************************************/
/* os */
/**
* Get the os_tick counter value.
*
* Note that the frequency is unspecified, because the time measure
* is meant to be used to compare the ratio between usage times.
*/
uint64_t os_tick(void);
/**
* Get the os_tick counter value in millisecond.
*/
uint64_t os_tick_ms(void);
/**
* Initializes the system.
*/
void os_init(int opt);
/**
* Deinitialize the system.
*/
void os_done(void);
/**
* Abort the process with a stacktrace.
*/
void os_abort(void) __noreturn;
/**
* Clear the screen.
*/
void os_clear(void);
/**
* Global variable to identify if Ctrl+C is pressed.
*/
extern volatile int global_interrupt;
/****************************************************************************/
/* app */
/**
* Include list support to have tommy_node.
*/
#include "tommyds/tommylist.h"
/**
* Basic block position type.
* With 32 bits and 128k blocks you can address 256 TB.
@ -297,20 +318,6 @@ typedef uint32_t block_off_t;
*/
typedef int64_t data_off_t;
/**
* Includes specific support for Windows or Linux.
*/
#ifdef __MINGW32__
#include "mingw.h"
#else
#include "unix.h"
#endif
/**
* Include list support to have tommy_node.
*/
#include "tommyds/tommylist.h"
/**
* Another name for link() to avoid confusion with local variables called "link".
*/
@ -359,39 +366,6 @@ int filephy(const char* path, uint64_t size, uint64_t* physical);
*/
int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_hardlinks, uint64_t* total_space, uint64_t* free_space, char* fstype, size_t fstype_size, char* fslabel, size_t fslabel_size);
/**
* Get the tick counter value.
*
* Note that the frequency is unspecified, because the time measure
* is meant to be used to compare the ratio between usage times.
*/
uint64_t tick(void);
/**
* Get the tick counter value in millisecond.
*/
uint64_t tick_ms(void);
/**
* Initializes the system.
*/
void os_init(int opt);
/**
* Deinitialize the system.
*/
void os_done(void);
/**
* Abort the process with a stacktrace.
*/
void os_abort(void) __noreturn;
/**
* Clear the screen.
*/
void os_clear(void);
/**
* Log file.
*
@ -610,11 +584,6 @@ int devtest(tommy_list* high, tommy_list* low, int operation);
*/
int ambient_temperature(void);
/**
* Global variable to identify if Ctrl+C is pressed.
*/
extern volatile int global_interrupt;
/**
* Size of the spaceholder file for Windows to avoid the message of low disk space
*/

View file

@ -1612,12 +1612,12 @@ static void* scan_disk(void* arg)
}
}
start = tick_ms();
start = os_tick_ms();
scan_dir(scan, 0, scan->is_diff, disk->dir, "");
if (!scan->is_diff)
msg_progress("Scanned %s in %" PRIu64 " seconds\n", disk->name, (tick_ms() - start) / 1000);
msg_progress("Scanned %s in %" PRIu64 " seconds\n", disk->name, (os_tick_ms() - start) / 1000);
return 0;
}

View file

@ -171,7 +171,7 @@ void state_init(struct snapraid_state* state)
state->tick_sched = 0;
state->tick_raid = 0;
state->tick_hash = 0;
state->tick_last = tick();
state->tick_last = os_tick();
state->share[0] = 0;
state->pool[0] = 0;
state->pool_device = 0;
@ -4224,7 +4224,7 @@ static void* state_verify_thread(void* arg)
uint32_t crc_computed;
uint64_t start;
start = tick_ms();
start = os_tick_ms();
if (sdeplete(f, buf) != 0) {
/* LCOV_EXCL_START */
@ -4256,7 +4256,7 @@ static void* state_verify_thread(void* arg)
/* LCOV_EXCL_STOP */
}
msg_progress("Verified %s in %" PRIu64 " seconds\n", content->content, (tick_ms() - start) / 1000);
msg_progress("Verified %s in %" PRIu64 " seconds\n", content->content, (os_tick_ms() - start) / 1000);
return 0;
}
@ -4652,7 +4652,7 @@ void state_progress_restart(struct snapraid_state* state)
}
/**
* Set the latest tick data in the progress vector.
* Set the latest os_tick data in the progress vector.
*/
static void state_progress_latest(struct snapraid_state* state)
{
@ -5118,14 +5118,14 @@ int state_progress(struct snapraid_state* state, struct snapraid_io* io, block_o
void state_usage_waste(struct snapraid_state* state)
{
uint64_t now = tick();
uint64_t now = os_tick();
state->tick_last = now;
}
void state_usage_misc(struct snapraid_state* state)
{
uint64_t now = tick();
uint64_t now = os_tick();
uint64_t delta = now - state->tick_last;
/* increment the time spent in computations */
@ -5136,7 +5136,7 @@ void state_usage_misc(struct snapraid_state* state)
void state_usage_sched(struct snapraid_state* state)
{
uint64_t now = tick();
uint64_t now = os_tick();
uint64_t delta = now - state->tick_last;
/* increment the time spent in computations */
@ -5147,7 +5147,7 @@ void state_usage_sched(struct snapraid_state* state)
void state_usage_raid(struct snapraid_state* state)
{
uint64_t now = tick();
uint64_t now = os_tick();
uint64_t delta = now - state->tick_last;
/* increment the time spent in computations */
@ -5158,7 +5158,7 @@ void state_usage_raid(struct snapraid_state* state)
void state_usage_hash(struct snapraid_state* state)
{
uint64_t now = tick();
uint64_t now = os_tick();
uint64_t delta = now - state->tick_last;
/* increment the time spent in computations */
@ -5176,7 +5176,7 @@ void state_usage_file(struct snapraid_state* state, struct snapraid_disk* disk,
void state_usage_disk(struct snapraid_state* state, struct snapraid_handle* handle_map, unsigned* waiting_map, unsigned waiting_mac)
{
uint64_t now = tick();
uint64_t now = os_tick();
uint64_t delta = now - state->tick_last;
unsigned i;
@ -5196,7 +5196,7 @@ void state_usage_disk(struct snapraid_state* state, struct snapraid_handle* hand
void state_usage_parity(struct snapraid_state* state, unsigned* waiting_map, unsigned waiting_mac)
{
uint64_t now = tick();
uint64_t now = os_tick();
uint64_t delta = now - state->tick_last;
unsigned i;

View file

@ -105,7 +105,7 @@ void random_seed(uint64_t seed)
void random_reseed(void)
{
random_state = tick();
random_state = os_tick();
}
unsigned char random_u8(void)

View file

@ -1175,7 +1175,7 @@ int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_har
return 0;
}
uint64_t tick(void)
uint64_t os_tick(void)
{
#if HAVE_MACH_ABSOLUTE_TIME
/* for Mac OS X */
@ -1207,7 +1207,7 @@ uint64_t tick(void)
#endif
}
uint64_t tick_ms(void)
uint64_t os_tick_ms(void)
{
struct timeval tv;
@ -2010,7 +2010,7 @@ static void* thread_spinup(void* arg)
devinfo_t* devinfo = arg;
uint64_t start;
start = tick_ms();
start = os_tick_ms();
if (devup(devinfo->device, devinfo->name) != 0) {
/* LCOV_EXCL_START */
@ -2018,7 +2018,7 @@ static void* thread_spinup(void* arg)
/* LCOV_EXCL_STOP */
}
msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
msg_status("Spunup device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
/* after the spin up, get SMART info */
if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->info, devinfo->serial, devinfo->family, devinfo->model, devinfo->interf) != 0) {
@ -2051,7 +2051,7 @@ static void* thread_spindown(void* arg)
devinfo_t* devinfo = arg;
uint64_t start;
start = tick_ms();
start = os_tick_ms();
if (devdown(devinfo->device, devinfo->name, devinfo->smartctl) != 0) {
/* LCOV_EXCL_START */
@ -2059,7 +2059,7 @@ static void* thread_spindown(void* arg)
/* LCOV_EXCL_STOP */
}
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
return 0;
#else
@ -2078,7 +2078,7 @@ static void* thread_spindownifup(void* arg)
uint64_t start;
int power;
start = tick_ms();
start = os_tick_ms();
if (devdownifup(devinfo->device, devinfo->name, devinfo->smartctl, &power) != 0) {
/* LCOV_EXCL_START */
@ -2087,7 +2087,7 @@ static void* thread_spindownifup(void* arg)
}
if (power == POWER_ACTIVE)
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, os_tick_ms() - start);
return 0;
#else

View file

@ -18,6 +18,13 @@
#ifndef __UNIX_H
#define __UNIX_H
#ifndef WEXITSTATUS
#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif
#ifndef WIFEXITED
#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif
#ifdef __linux__
#define HAVE_LINUX_DEVICE 1 /**< In Linux enables special device support. */
#define HAVE_DIRECT_IO 1 /**< Support O_DIRECT in open(). */
@ -63,5 +70,19 @@ const char* stat_desc(struct stat* st);
*/
size_t direct_size(void);
/****************************************************************************/
/* thread */
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#if HAVE_PTHREAD_CREATE
#define HAVE_THREAD 1
typedef pthread_t thread_id_t;
typedef pthread_mutex_t thread_mutex_t;
typedef pthread_cond_t thread_cond_t;
#endif
#endif