feat: add -f/--sendfile for zero-copy file transfer
- Add file_send_sendfile() using sendfile() syscall to send file content directly from fd to socket, bypassing userspace memory - Add use_sendfile field to Config struct (default false) - Add -f / --sendfile flag parsing in client.c - sendfile path in send_chunk() used when -f is set without -c or -s - send_files_multithreaded() falls back to single-threaded when sendfile is enabled (loader pipeline becomes unnecessary) - Add Sendfile (-f) test case to test.py
This commit is contained in:
@@ -49,6 +49,7 @@ The client-server communication uses the following status codes:
|
|||||||
| `-m` | Enable multithreading mode |
|
| `-m` | Enable multithreading mode |
|
||||||
| `-c [level]` | Enable compression with optional level (1-22, default: 5) |
|
| `-c [level]` | Enable compression with optional level (1-22, default: 5) |
|
||||||
| `-s` | Enable chunk serialization (batch-transfer all files per chunk) |
|
| `-s` | Enable chunk serialization (batch-transfer all files per chunk) |
|
||||||
|
| `-f` | Enable sendfile (zero-copy file transfer, bypasses userspace memory) |
|
||||||
| `--source-dir <path>` | Source directory to sync (overrides `FASTSYNC_SOURCE_DIR`) |
|
| `--source-dir <path>` | Source directory to sync (overrides `FASTSYNC_SOURCE_DIR`) |
|
||||||
| `--dest-dir <path>` | Server-side destination directory (overrides `FASTSYNC_DEST_DIR`) |
|
| `--dest-dir <path>` | Server-side destination directory (overrides `FASTSYNC_DEST_DIR`) |
|
||||||
| `--save-to-disk` | Persist received files to disk |
|
| `--save-to-disk` | Persist received files to disk |
|
||||||
@@ -118,6 +119,9 @@ make
|
|||||||
|
|
||||||
# Multithreaded with compressed chunk serialization
|
# Multithreaded with compressed chunk serialization
|
||||||
./build/client -m -s -c 3
|
./build/client -m -s -c 3
|
||||||
|
|
||||||
|
# Sendfile (zero-copy, bypasses userspace for large files)
|
||||||
|
./build/client -f
|
||||||
```
|
```
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ int send_chunk(Client *client, Chunk *chunk, Config *config) {
|
|||||||
}
|
}
|
||||||
send_data(client->file_descriptor, data->data, data->size);
|
send_data(client->file_descriptor, data->data, data->size);
|
||||||
data_destroy(data);
|
data_destroy(data);
|
||||||
|
} else if (config->use_sendfile && !config->use_compression) {
|
||||||
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
|
send_status(client->file_descriptor, STATUS_NEXT);
|
||||||
|
file_send_sendfile(chunk->items[i], client->file_descriptor);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < chunk->element_count; i++) {
|
for (int i = 0; i < chunk->element_count; i++) {
|
||||||
send_status(client->file_descriptor, STATUS_NEXT);
|
send_status(client->file_descriptor, STATUS_NEXT);
|
||||||
@@ -130,8 +135,10 @@ int send_files(Config *config) {
|
|||||||
DirectoryScanner *scanner = directory_scanner_create(config->send_directory);
|
DirectoryScanner *scanner = directory_scanner_create(config->send_directory);
|
||||||
Chunk *current_chunk;
|
Chunk *current_chunk;
|
||||||
while ((current_chunk = directory_scanner_next(scanner)) != NULL) {
|
while ((current_chunk = directory_scanner_next(scanner)) != NULL) {
|
||||||
|
if (!config->use_sendfile) {
|
||||||
for (int i = 0; i < current_chunk->element_count; i++)
|
for (int i = 0; i < current_chunk->element_count; i++)
|
||||||
file_load_data(current_chunk->items[i]);
|
file_load_data(current_chunk->items[i]);
|
||||||
|
}
|
||||||
send_chunk(client, current_chunk, config);
|
send_chunk(client, current_chunk, config);
|
||||||
chunk_destroy(current_chunk);
|
chunk_destroy(current_chunk);
|
||||||
}
|
}
|
||||||
@@ -145,6 +152,11 @@ int send_files(Config *config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int send_files_multithreaded(Config *config) {
|
int send_files_multithreaded(Config *config) {
|
||||||
|
if (config->use_sendfile) {
|
||||||
|
log_message(LOG_LEVEL_INFO, "Sendfile enabled, falling back to single-threaded");
|
||||||
|
return send_files(config);
|
||||||
|
}
|
||||||
|
|
||||||
PipelineContextSender *context =
|
PipelineContextSender *context =
|
||||||
pipeline_context_sender_create(config, queue_create(100, chunk_destroy),
|
pipeline_context_sender_create(config, queue_create(100, chunk_destroy),
|
||||||
queue_create(100, chunk_destroy));
|
queue_create(100, chunk_destroy));
|
||||||
@@ -215,6 +227,9 @@ int main(int argc, char *argv[]) {
|
|||||||
config->receive_root_directory = str_dup(argv[++i]);
|
config->receive_root_directory = str_dup(argv[++i]);
|
||||||
} else if (strcmp(argv[i], "--save-to-disk") == 0) {
|
} else if (strcmp(argv[i], "--save-to-disk") == 0) {
|
||||||
config->save_to_disk = true;
|
config->save_to_disk = true;
|
||||||
|
} else if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--sendfile") == 0) {
|
||||||
|
config->use_sendfile = true;
|
||||||
|
log_message(LOG_LEVEL_INFO, "Enabled sendfile");
|
||||||
} else {
|
} else {
|
||||||
handle_arg(argv[i], "-m", &config->use_multithreading,
|
handle_arg(argv[i], "-m", &config->use_multithreading,
|
||||||
"Enabled Multithreading");
|
"Enabled Multithreading");
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ Config *config_create(char *version, char *send_directory,
|
|||||||
config->use_compression = use_compression;
|
config->use_compression = use_compression;
|
||||||
config->compression_level = compression_level;
|
config->compression_level = compression_level;
|
||||||
config->num_connections = num_connections;
|
config->num_connections = num_connections;
|
||||||
|
config->use_sendfile = false;
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ typedef struct Config {
|
|||||||
bool use_multithreading;
|
bool use_multithreading;
|
||||||
bool use_chunk_serialization;
|
bool use_chunk_serialization;
|
||||||
bool use_compression;
|
bool use_compression;
|
||||||
|
bool use_sendfile;
|
||||||
bool use_single_send_per_file;
|
bool use_single_send_per_file;
|
||||||
int compression_level;
|
int compression_level;
|
||||||
int num_connections;
|
int num_connections;
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/sendfile.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <zstd.h>
|
#include <zstd.h>
|
||||||
|
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
@@ -68,6 +71,31 @@ void file_send_single_calls(File *file, int file_descriptor) {
|
|||||||
send_data(file_descriptor, file->data->data, file->data->size);
|
send_data(file_descriptor, file->data->data, file->data->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void file_send_sendfile(File *file, int file_descriptor) {
|
||||||
|
send_str(file_descriptor, file->path);
|
||||||
|
|
||||||
|
int fd = open(file->path, O_RDONLY);
|
||||||
|
if (fd == -1) {
|
||||||
|
perror("Could not open file for sendfile");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long file_size = file->stats.st_size;
|
||||||
|
send_n_data(file_descriptor, &file_size, sizeof(unsigned long long));
|
||||||
|
|
||||||
|
off_t offset = 0;
|
||||||
|
while (offset < file_size) {
|
||||||
|
ssize_t sent = sendfile(file_descriptor, fd, &offset, file_size - offset);
|
||||||
|
if (sent == -1) {
|
||||||
|
perror("sendfile failed");
|
||||||
|
close(fd);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
size_t file_content_to_buffer(File *file) {
|
size_t file_content_to_buffer(File *file) {
|
||||||
FILE *file_pointer = fopen(file->path, "rb");
|
FILE *file_pointer = fopen(file->path, "rb");
|
||||||
if (file_pointer == NULL) {
|
if (file_pointer == NULL) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ void file_destroy(void *item);
|
|||||||
void file_load_data(File *file);
|
void file_load_data(File *file);
|
||||||
void file_print(void *item);
|
void file_print(void *item);
|
||||||
void file_send_single_calls(File *file, int file_descriptor);
|
void file_send_single_calls(File *file, int file_descriptor);
|
||||||
|
void file_send_sendfile(File *file, int file_descriptor);
|
||||||
size_t file_content_to_buffer(File *file);
|
size_t file_content_to_buffer(File *file);
|
||||||
|
|
||||||
FileReceive *file_receive_create(char *path, Data *data);
|
FileReceive *file_receive_create(char *path, Data *data);
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ TEST_CASES = [
|
|||||||
"name": "Multithreading + Compression + Chunk Serialization (-m -c -s)",
|
"name": "Multithreading + Compression + Chunk Serialization (-m -c -s)",
|
||||||
"flags": ["-m", "-c", "-s"],
|
"flags": ["-m", "-c", "-s"],
|
||||||
},
|
},
|
||||||
|
{"name": "Sendfile (-f)", "flags": ["-f"]},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user