int sched_start_ex(ScheduleArray *pScheduleArray, pthread_t *ptid, const int stack_size, bool * volatile pcontinue_flag, ScheduleContext **ppContext) { int result; pthread_attr_t thread_attr; ScheduleContext *pContext; pContext = (ScheduleContext *)malloc(sizeof(ScheduleContext)); if (pContext == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes failed, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(ScheduleContext), \ result, STRERROR(result)); return result; } memset(pContext, 0, sizeof(ScheduleContext)); if ((result=init_pthread_attr(&thread_attr, stack_size)) != 0) { free(pContext); return result; } if ((result=sched_dup_array(pScheduleArray, \ &(pContext->scheduleArray))) != 0) { free(pContext); return result; } if (timer_slot_count > 0) { if ((result=fast_mblock_init(&pContext->mblock, sizeof(FastDelayTask), mblock_alloc_once)) != 0) { free(pContext); return result; } g_current_time = time(NULL); if ((result=fast_timer_init(&pContext->timer, timer_slot_count, g_current_time)) != 0) { free(pContext); return result; } if ((result=init_pthread_lock(&pContext->delay_queue.lock)) != 0) { free(pContext); return result; } pContext->timer_init = true; } pContext->pcontinue_flag = pcontinue_flag; if ((result=pthread_create(ptid, &thread_attr, \ sched_thread_entrance, pContext)) != 0) { free(pContext); logError("file: "__FILE__", line: %d, " \ "create thread failed, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); } *ppContext = pContext; pthread_attr_destroy(&thread_attr); return result; }
int storage_trunk_init() { int result; int i; int count; if (!g_if_trunker_self) { logError("file: "__FILE__", line: %d, " \ "I am not trunk server!", __LINE__); return 0; } if (trunk_init_flag != STORAGE_TRUNK_INIT_FLAG_NONE) { logWarning("file: "__FILE__", line: %d, " \ "trunk already inited!", __LINE__); return 0; } logDebug("file: "__FILE__", line: %d, " \ "storage trunk init ...", __LINE__); g_trunk_server.sock = -1; g_trunk_server.port = g_server_port; if ((result=init_pthread_lock(&trunk_file_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } if ((result=init_pthread_lock(&trunk_mem_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } if ((result=fast_mblock_init(&free_blocks_man, \ sizeof(FDFSTrunkNode), 0)) != 0) { return result; } if ((result=fast_mblock_init(&tree_nodes_man, \ sizeof(FDFSTrunkSlot), 0)) != 0) { return result; } tree_info_by_sizes = (AVLTreeInfo *)malloc(sizeof(AVLTreeInfo) * \ g_fdfs_store_paths.count); if (tree_info_by_sizes == NULL) { result = errno != 0 ? errno : ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, errno: %d, error info: %s", \ __LINE__, (int)(sizeof(AVLTreeInfo) * \ g_fdfs_store_paths.count), result, STRERROR(result)); return result; } for (i=0; i<g_fdfs_store_paths.count; i++) { if ((result=avl_tree_init(tree_info_by_sizes + i, NULL, \ storage_trunk_node_compare_size)) != 0) { logError("file: "__FILE__", line: %d, " \ "avl_tree_init fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } } if ((result=trunk_free_block_checker_init()) != 0) { return result; } if ((result=storage_trunk_load()) != 0) { return result; } count = 0; for (i=0; i<g_fdfs_store_paths.count; i++) { count += avl_tree_count(tree_info_by_sizes + i); } logInfo("file: "__FILE__", line: %d, " \ "tree by space size node count: %d, tree by trunk file id " \ "node count: %d, free block count: %d, " \ "trunk_total_free_space: %"PRId64, __LINE__, \ count, trunk_free_block_tree_node_count(), \ trunk_free_block_total_count(), \ g_trunk_total_free_space); /* { char filename[MAX_PATH_SIZE]; sprintf(filename, "%s/logs/tttt.dat", g_fdfs_base_path); trunk_free_block_tree_print(filename); } */ trunk_init_flag = STORAGE_TRUNK_INIT_FLAG_DONE; return 0; }
int storage_trunk_init() { int result; if (!g_if_trunker_self) { logError("file: "__FILE__", line: %d, " \ "I am not trunk server!", __LINE__); return 0; } if (trunk_init_flag != STORAGE_TRUNK_INIT_FLAG_NONE) { logWarning("file: "__FILE__", line: %d, " \ "trunk already inited!", __LINE__); return 0; } logDebug("file: "__FILE__", line: %d, " \ "storage trunk init ...", __LINE__); g_trunk_server.sock = -1; g_trunk_server.port = g_server_port; if ((result=init_pthread_lock(&trunk_file_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } if ((result=init_pthread_lock(&trunk_mem_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "init_pthread_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } if ((result=fast_mblock_init(&free_blocks_man, \ sizeof(FDFSTrunkNode), 0)) != 0) { return result; } if ((result=fast_mblock_init(&tree_nodes_man, \ sizeof(FDFSTrunkSlot), 0)) != 0) { return result; } if ((result=avl_tree_init(&tree_info_by_size, NULL, \ storage_trunk_node_compare_size)) != 0) { logError("file: "__FILE__", line: %d, " \ "avl_tree_init fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } if ((result=trunk_free_block_checker_init()) != 0) { return result; } if ((result=storage_trunk_load()) != 0) { return result; } logInfo("file: "__FILE__", line: %d, " \ "tree by space size node count: %d, tree by trunk file id " \ "node count: %d, free block count: %d, " \ "trunk_total_free_space: "INT64_PRINTF_FORMAT, __LINE__, \ avl_tree_count(&tree_info_by_size), \ trunk_free_block_tree_node_count(), \ trunk_free_block_total_count(), \ g_trunk_total_free_space); /* { char filename[MAX_PATH_SIZE]; sprintf(filename, "%s/logs/tttt.dat", g_fdfs_base_path); trunk_free_block_tree_print(filename); } */ trunk_init_flag = STORAGE_TRUNK_INIT_FLAG_DONE; return 0; }