1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| #define _GNU_SOURCE #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/stat.h> #include <time.h> #include <sys/resource.h>
/** * splice性能测试配置 */ typedef struct { size_t buffer_size; size_t chunk_size; int use_splice; int use_flags; const char *description; } splice_test_config_t;
/** * 创建测试数据文件 */ int create_test_data_file(const char *filename, size_t size) { int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) { perror("创建测试文件失败"); return -1; } // 使用随机数据填充文件 char *buffer = malloc(1024 * 1024); if (!buffer) { perror("分配缓冲区失败"); close(fd); return -1; } srand(time(NULL)); for (size_t i = 0; i < size; i += 1024) { for (int j = 0; j < 1024 && i + j < size; j++) { buffer[j] = rand() % 256; } write(fd, buffer, (size - i < 1024) ? size - i : 1024); } free(buffer); close(fd); printf("创建测试文件: %s (%.1f MB)\n", filename, size / (1024.0 * 1024.0)); return 0; }
/** * 使用splice进行数据传输 */ double test_splice_performance(const char *src_file, const char *dst_file, size_t chunk_size) { int src_fd, dst_fd, pipefd[2]; struct timespec start, end; ssize_t bytes_transferred, total_transferred = 0; struct stat file_stat; // 获取文件大小 if (stat(src_file, &file_stat) == -1) { perror("获取文件状态失败"); return -1; } // 打开文件 src_fd = open(src_file, O_RDONLY); dst_fd = open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (src_fd == -1 || dst_fd == -1) { perror("打开文件失败"); if (src_fd != -1) close(src_fd); if (dst_fd != -1) close(dst_fd); return -1; } // 创建管道 if (pipe(pipefd) == -1) { perror("创建管道失败"); close(src_fd); close(dst_fd); return -1; } // 开始计时 clock_gettime(CLOCK_MONOTONIC, &start); // 执行splice传输 while (total_transferred < file_stat.st_size) { size_t remaining = file_stat.st_size - total_transferred; size_t to_transfer = (remaining < chunk_size) ? remaining : chunk_size; // 读取到管道 bytes_transferred = splice(src_fd, NULL, pipefd[1], NULL, to_transfer, SPLICE_F_MOVE | SPLICE_F_MORE); if (bytes_transferred <= 0) { if (bytes_transferred == -1) { perror("splice读取失败"); } break; } // 写入到目标文件 ssize_t bytes_written = splice(pipefd[0], NULL, dst_fd, NULL, bytes_transferred, SPLICE_F_MOVE); if (bytes_written <= 0) { if (bytes_written == -1) { perror("splice写入失败"); } break; } total_transferred += bytes_written; } // 结束计时 clock_gettime(CLOCK_MONOTONIC, &end); // 清理资源 close(pipefd[0]); close(pipefd[1]); close(src_fd); close(dst_fd); double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; return elapsed; }
/** * 使用传统read/write进行数据传输 */ double test_traditional_performance(const char *src_file, const char *dst_file, size_t buffer_size) { int src_fd, dst_fd; struct timespec start, end; char *buffer; ssize_t bytes_read, bytes_written; ssize_t total_transferred = 0; struct stat file_stat; // 获取文件大小 if (stat(src_file, &file_stat) == -1) { perror("获取文件状态失败"); return -1; } // 分配缓冲区 buffer = malloc(buffer_size); if (!buffer) { perror("分配缓冲区失败"); return -1; } // 打开文件 src_fd = open(src_file, O_RDONLY); dst_fd = open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (src_fd == -1 || dst_fd == -1) { perror("打开文件失败"); free(buffer); if (src_fd != -1) close(src_fd); if (dst_fd != -1) close(dst_fd); return -1; } // 开始计时 clock_gettime(CLOCK_MONOTONIC, &start); // 执行传统传输 while ((bytes_read = read(src_fd, buffer, buffer_size)) > 0) { bytes_written = write(dst_fd, buffer, bytes_read); if (bytes_written == -1) { perror("写入失败"); break; } total_transferred += bytes_written; } // 结束计时 clock_gettime(CLOCK_MONOTONIC, &end); // 清理资源 free(buffer); close(src_fd); close(dst_fd); double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; return elapsed; }
/** * 演示splice性能优化和最佳实践 */ int demo_splice_optimization() { const char *src_file = "performance_test_src.dat"; const char *dst_file_splice = "performance_test_dst_splice.dat"; const char *dst_file_traditional = "performance_test_dst_traditional.dat"; const size_t file_size = 200 * 1024 * 1024; // 200MB const size_t chunk_sizes[] = {4096, 16384, 65536, 262144, 1048576}; // 4KB到1MB const int num_chunk_sizes = sizeof(chunk_sizes) / sizeof(chunk_sizes[0]); printf("=== splice性能优化和最佳实践演示 ===\n"); // 检查系统是否支持splice printf("检查splice支持:\n"); int test_pipe[2]; if (pipe(test_pipe) == 0) { printf(" ✓ 系统支持管道操作\n"); close(test_pipe[0]); close(test_pipe[1]); } else { printf(" ✗ 系统不支持管道操作\n"); return -1; } // 创建测试文件 printf("\n创建测试文件...\n"); if (create_test_data_file(src_file, file_size) != 0) { return -1; } printf("\n=== 性能测试结果 ===\n"); printf("%-12s %-12s %-15s %-15s %-10s\n", "传输方式", "块大小", "传输时间(秒)", "传输速度(MB/s)", "性能提升"); printf("%-12s %-12s %-15s %-15s %-10s\n", "--------", "--------", "------------", "------------", "--------"); // 测试不同块大小的splice性能 for (int i = 0; i < num_chunk_sizes; i++) { char dst_file[256]; snprintf(dst_file, sizeof(dst_file), "splice_chunk_%zu.dat", chunk_sizes[i]); double elapsed = test_splice_performance(src_file, dst_file, chunk_sizes[i]); if (elapsed > 0) { double speed = file_size / (1024.0 * 1024.0) / elapsed; printf("%-12s %-12zu %-15.3f %-15.2f %-10s\n", "splice", chunk_sizes[i], elapsed, speed, "N/A"); } unlink(dst_file); } // 测试传统方法性能(使用不同缓冲区大小) const size_t buffer_sizes[] = {4096, 16384, 65536, 262144}; const int num_buffer_sizes = sizeof(buffer_sizes) / sizeof(buffer_sizes[0]); for (int i = 0; i < num_buffer_sizes; i++) { char dst_file[256]; snprintf(dst_file, sizeof(dst_file), "traditional_buf_%zu.dat", buffer_sizes[i]); double elapsed = test_traditional_performance(src_file, dst_file, buffer_sizes[i]); if (elapsed > 0) { double speed = file_size / (1024.0 * 1024.0) / elapsed; printf("%-12s %-12zu %-15.3f %-15.2f %-10s\n", "传统方法", buffer_sizes[i], elapsed, speed, "N/A"); } unlink(dst_file); } // 最佳实践建议 printf("\n=== splice使用最佳实践 ===\n"); printf("1. 块大小选择:\n"); printf(" - 小文件: 4KB-16KB\n"); printf(" - 大文件: 64KB-1MB\n"); printf(" - 根据系统和硬件调整\n"); printf("\n2. 标志使用:\n"); printf(" - SPLICE_F_MOVE: 移动页面而不是复制\n"); printf(" - SPLICE_F_MORE: 提示还有更多数据\n"); printf(" - SPLICE_F_GIFT: 释放用户页面\n"); printf("\n3. 性能优化:\n"); printf(" - 使用合适的管道大小\n"); printf(" - 避免频繁的小块传输\n"); printf(" - 结合非阻塞I/O使用\n"); printf(" - 监控系统资源使用\n"); printf("\n4. 错误处理:\n"); printf(" - 检查返回值和errno\n"); printf(" - 处理部分传输情况\n"); printf(" - 优雅降级到传统方法\n"); // 清理测试文件 unlink(src_file); unlink(dst_file_splice); unlink(dst_file_traditional); return 0; }
int main() { return demo_splice_optimization(); }
|