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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
| #include <linux/aio_abi.h> #include <sys/syscall.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <pthread.h> #include <signal.h> #include <sys/time.h>
/** * 生产环境AIO上下文管理器 */ typedef struct { aio_context_t ctx; int is_initialized; int is_destroyed; time_t create_time; time_t destroy_time; unsigned long operations_submitted; unsigned long operations_completed; unsigned long operations_cancelled; pthread_mutex_t mutex; volatile int shutdown_requested; } production_aio_context_t;
/** * 系统调用包装函数 */ static inline int io_setup(unsigned nr_events, aio_context_t *ctxp) { return syscall(__NR_io_setup, nr_events, ctxp); }
static inline int io_destroy(aio_context_t ctx) { return syscall(__NR_io_destroy, ctx); }
static inline int io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp) { return syscall(__NR_io_submit, ctx, nr, iocbsp); }
static inline int io_cancel(aio_context_t ctx, struct iocb *iocb, struct io_event *result) { return syscall(__NR_io_cancel, ctx, iocb, result); }
static inline int io_getevents(aio_context_t ctx, long min_nr, long nr, struct io_event *events, struct timespec *timeout) { return syscall(__NR_io_getevents, ctx, min_nr, nr, events, timeout); }
/** * 初始化生产环境AIO上下文 */ int init_production_aio_context(production_aio_context_t *ctx, unsigned nr_events) { // 初始化结构体 memset(ctx, 0, sizeof(production_aio_context_t)); ctx->create_time = time(NULL); ctx->is_initialized = 0; ctx->is_destroyed = 0; ctx->shutdown_requested = 0; // 初始化互斥锁 if (pthread_mutex_init(&ctx->mutex, NULL) != 0) { printf("初始化互斥锁失败\n"); return -1; } // 创建AIO上下文 ctx->ctx = 0; int ret = io_setup(nr_events, &ctx->ctx); if (ret != 0) { printf("创建AIO上下文失败: %s\n", strerror(-ret)); pthread_mutex_destroy(&ctx->mutex); return -1; } ctx->is_initialized = 1; printf("生产环境AIO上下文初始化成功:\n"); printf(" 上下文ID: %llu\n", (unsigned long long)ctx->ctx); printf(" 缓冲区大小: %u 事件\n", nr_events); printf(" 创建时间: %s", ctime(&ctx->create_time)); return 0; }
/** * 安全销毁生产环境AIO上下文 */ int destroy_production_aio_context(production_aio_context_t *ctx) { if (!ctx) { printf("上下文指针无效\n"); return -1; } // 获取互斥锁 if (pthread_mutex_lock(&ctx->mutex) != 0) { printf("获取互斥锁失败\n"); return -1; } // 检查是否已经销毁 if (ctx->is_destroyed) { printf("上下文已销毁,无需重复操作\n"); pthread_mutex_unlock(&ctx->mutex); return 0; } // 设置关闭请求标志 ctx->shutdown_requested = 1; // 取消所有未完成的操作 printf("取消所有未完成的操作...\n"); // 这里应该实现取消未完成操作的逻辑 // 销毁AIO上下文 printf("销毁AIO上下文: %llu\n", (unsigned long long)ctx->ctx); int ret = io_destroy(ctx->ctx); if (ret != 0) { printf("销毁AIO上下文失败: %s\n", strerror(-ret)); if (ret == -EAGAIN) { printf(" 原因:上下文中仍有未完成的操作\n"); } else if (ret == -EINVAL) { printf(" 原因:无效的上下文ID\n"); } // 即使销毁失败也要清理资源 ctx->is_destroyed = 1; ctx->destroy_time = time(NULL); pthread_mutex_unlock(&ctx->mutex); pthread_mutex_destroy(&ctx->mutex); return -1; } ctx->is_destroyed = 1; ctx->destroy_time = time(NULL); // 显示统计信息 printf("AIO上下文销毁成功:\n"); printf(" 销毁时间: %s", ctime(&ctx->destroy_time)); printf(" 运行时间: %ld 秒\n", (long)difftime(ctx->destroy_time, ctx->create_time)); printf(" 提交操作数: %lu\n", ctx->operations_submitted); printf(" 完成操作数: %lu\n", ctx->operations_completed); printf(" 取消操作数: %lu\n", ctx->operations_cancelled); pthread_mutex_unlock(&ctx->mutex); pthread_mutex_destroy(&ctx->mutex); return 0; }
/** * 优雅关闭信号处理 */ void graceful_shutdown_handler(int sig) { printf("\n收到关闭信号 %d,开始优雅关闭...\n", sig); // 这里应该通知所有AIO上下文管理器开始关闭 }
/** * 演示生产环境使用模式 */ int demo_production_usage_pattern() { production_aio_context_t aio_ctx; struct sigaction sa; printf("=== 生产环境使用模式演示 ===\n"); // 设置信号处理 printf("1. 设置信号处理:\n"); memset(&sa, 0, sizeof(sa)); sa.sa_handler = graceful_shutdown_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGINT, &sa, NULL) == 0) { printf(" ✓ SIGINT信号处理设置成功\n"); } else { printf(" ✗ SIGINT信号处理设置失败: %s\n", strerror(errno)); } if (sigaction(SIGTERM, &sa, NULL) == 0) { printf(" ✓ SIGTERM信号处理设置成功\n"); } else { printf(" ✗ SIGTERM信号处理设置失败: %s\n", strerror(errno)); } // 初始化AIO上下文 printf("\n2. 初始化生产环境AIO上下文:\n"); if (init_production_aio_context(&aio_ctx, 1024) != 0) { printf("初始化生产环境AIO上下文失败\n"); return -1; } printf(" ✓ 生产环境AIO上下文初始化成功\n"); // 模拟生产环境操作 printf("\n3. 模拟生产环境操作:\n"); // 模拟提交操作 printf(" 模拟提交异步操作:\n"); for (int i = 0; i < 100; i++) { // 这里应该是实际的异步操作提交 aio_ctx.operations_submitted++; if (i % 20 == 0) { printf(" 已提交 %d 个操作\n", i); } // 模拟操作完成 if (i % 3 == 0) { aio_ctx.operations_completed++; } // 模拟操作取消 if (i % 7 == 0) { aio_ctx.operations_cancelled++; } } printf(" ✓ 模拟操作完成\n"); printf(" 总提交: %lu\n", aio_ctx.operations_submitted); printf(" 总完成: %lu\n", aio_ctx.operations_completed); printf(" 总取消: %lu\n", aio_ctx.operations_cancelled); // 模拟运行期间的状态监控 printf("\n4. 模拟运行期间状态监控:\n"); // 显示当前状态 printf(" 当前AIO上下文状态:\n"); printf(" 已初始化: %s\n", aio_ctx.is_initialized ? "是" : "否"); printf(" 已销毁: %s\n", aio_ctx.is_destroyed ? "是" : "否"); printf(" 关闭请求: %s\n", aio_ctx.shutdown_requested ? "是" : "否"); printf(" 运行时间: %ld 秒\n", (long)difftime(time(NULL), aio_ctx.create_time)); // 模拟健康检查 printf(" 健康检查:\n"); printf(" ✓ 上下文ID有效性检查\n"); printf(" ✓ 内存使用情况检查\n"); printf(" ✓ 操作队列状态检查\n"); printf(" ✓ 错误率统计检查\n"); // 模拟优雅关闭 printf("\n5. 模拟优雅关闭:\n"); // 设置关闭标志 printf(" 设置关闭标志:\n"); aio_ctx.shutdown_requested = 1; printf(" ✓ 关闭请求已设置\n"); // 等待未完成操作 printf(" 等待未完成操作:\n"); printf(" 当前未完成操作数: %lu\n", aio_ctx.operations_submitted - aio_ctx.operations_completed - aio_ctx.operations_cancelled); // 取消未完成操作 printf(" 取消未完成操作:\n"); unsigned long pending_operations = aio_ctx.operations_submitted - aio_ctx.operations_completed - aio_ctx.operations_cancelled; printf(" 取消 %lu 个未完成操作\n", pending_operations); aio_ctx.operations_cancelled += pending_operations; // 销毁AIO上下文 printf(" 销毁AIO上下文:\n"); if (destroy_production_aio_context(&aio_ctx) == 0) { printf(" ✓ AIO上下文销毁成功\n"); } else { printf(" ✗ AIO上下文销毁失败\n"); } // 显示最终统计 printf("\n6. 最终统计:\n"); printf(" 生命周期统计:\n"); printf(" 创建时间: %s", ctime(&aio_ctx.create_time)); if (aio_ctx.is_destroyed) { printf(" 销毁时间: %s", ctime(&aio_ctx.destroy_time)); printf(" 运行时间: %ld 秒\n", (long)difftime(aio_ctx.destroy_time, aio_ctx.create_time)); } printf(" 操作统计:\n"); printf(" 总提交操作: %lu\n", aio_ctx.operations_submitted); printf(" 总完成操作: %lu\n", aio_ctx.operations_completed); printf(" 总取消操作: %lu\n", aio_ctx.operations_cancelled); // 显示生产环境最佳实践 printf("\n=== 生产环境最佳实践 ===\n"); printf("1. 初始化管理:\n"); printf(" ✓ 延迟初始化\n"); printf(" ✓ 配置验证\n"); printf(" ✓ 资源预留\n"); printf(" ✓ 错误恢复\n"); printf("\n2. 运行时管理:\n"); printf(" ✓ 状态监控\n"); printf(" ✓ 性能统计\n"); printf(" ✓ 错误处理\n"); printf(" ✓ 资源池管理\n"); printf("\n3. 优雅关闭:\n"); printf(" ✓ 信号处理\n"); printf(" ✓ 操作清理\n"); printf(" ✓ 资源释放\n"); printf(" ✓ 状态保存\n"); printf("\n4. 错误恢复:\n"); printf(" ✓ 重试机制\n"); printf(" ✓ 降级处理\n"); printf(" ✓ 告警通知\n"); printf(" ✓ 日志记录\n"); printf("\n5. 监控告警:\n"); printf(" ✓ 性能指标\n"); printf(" ✓ 错误率统计\n"); printf(" ✓ 资源使用率\n"); printf(" ✓ 健康检查\n"); return 0; }
int main() { return demo_production_usage_pattern(); }
|