void thorium_balancer_init(struct thorium_balancer *self, struct thorium_worker_pool *pool) { self->pool = pool; core_map_init(&self->last_actor_received_messages, sizeof(int), sizeof(int)); self->worker_for_work = 0; self->last_migrations = 0; self->last_killed_actors = -1; self->last_spawned_actors = -1; core_map_init(&self->current_script_workers, sizeof(int), sizeof(int)); self->first_worker = 0; }
void core_map_clear(struct core_map *self) { int key_size; int value_size; key_size = core_map_get_key_size(self); value_size = core_map_get_value_size(self); core_map_destroy(self); core_map_init(self, key_size, value_size); /*core_dynamic_hash_table_clear(&self->table);*/ }
void biosal_coverage_distribution_init(struct thorium_actor *self) { struct biosal_coverage_distribution *concrete_actor; concrete_actor = (struct biosal_coverage_distribution *)thorium_actor_concrete_actor(self); core_map_init(&concrete_actor->distribution, sizeof(int), sizeof(uint64_t)); #ifdef BIOSAL_COVERAGE_DISTRIBUTION_DEBUG thorium_actor_log(self, "DISTRIBUTION IS READY\n"); #endif concrete_actor->actual = 0; concrete_actor->expected = 0; thorium_actor_log(self, "%s/%d is ready\n", thorium_actor_script_name(self), thorium_actor_name(self)); }
void biosal_sequence_partitioner_init(struct thorium_actor *actor) { struct biosal_sequence_partitioner *concrete_actor; concrete_actor = (struct biosal_sequence_partitioner *)thorium_actor_concrete_actor(actor); core_vector_init(&concrete_actor->stream_entries, sizeof(uint64_t)); core_vector_init(&concrete_actor->stream_positions, sizeof(uint64_t)); core_vector_init(&concrete_actor->stream_global_positions, sizeof(uint64_t)); core_vector_init(&concrete_actor->store_entries, sizeof(uint64_t)); core_queue_init(&concrete_actor->available_commands, sizeof(struct biosal_partition_command)); core_map_init(&concrete_actor->active_commands, sizeof(int), sizeof(struct biosal_partition_command)); concrete_actor->store_count = -1; concrete_actor->block_size = -1; concrete_actor->command_number = 0; concrete_actor->last_progress = -1; }
void biosal_assembly_graph_store_push_data(struct thorium_actor *self, struct thorium_message *message) { struct biosal_assembly_graph_store *concrete_self; int name; int source; concrete_self = thorium_actor_concrete_actor(self); source = thorium_message_source(message); concrete_self->source = source; name = thorium_actor_name(self); core_map_init(&concrete_self->coverage_distribution, sizeof(int), sizeof(uint64_t)); printf("%s/%d: local table has %" PRIu64" canonical kmers (%" PRIu64 " kmers)\n", thorium_actor_script_name(self), name, core_map_size(&concrete_self->table), 2 * core_map_size(&concrete_self->table)); core_memory_pool_examine(&concrete_self->persistent_memory); core_map_iterator_init(&concrete_self->iterator, &concrete_self->table); thorium_actor_send_to_self_empty(self, ACTION_YIELD); }
void core_map_clear(struct core_map *self) { int key_size; int value_size; struct core_memory_pool *pool; /* * Save key_size, value_size, and memory pool. */ key_size = core_map_get_key_size(self); value_size = core_map_get_value_size(self); pool = core_map_memory_pool(self); core_map_destroy(self); core_map_init(self, key_size, value_size); core_map_set_memory_pool(self, pool); /* * TODO: implement the clear operation directly inside * the lower layer (core_dynamic_hash_table + core_hash_table) */ /*core_dynamic_hash_table_clear(&self->table);*/ }
void biosal_input_controller_init(struct thorium_actor *actor) { struct biosal_input_controller *concrete_actor; concrete_actor = (struct biosal_input_controller *)thorium_actor_concrete_actor(actor); core_map_init(&concrete_actor->mega_blocks, sizeof(int), sizeof(struct core_vector)); core_map_init(&concrete_actor->assigned_blocks, sizeof(int), sizeof(int)); core_vector_init(&concrete_actor->mega_block_vector, sizeof(struct biosal_mega_block)); core_vector_init(&concrete_actor->counting_streams, sizeof(int)); core_vector_init(&concrete_actor->reading_streams, sizeof(int)); core_vector_init(&concrete_actor->partition_commands, sizeof(int)); core_vector_init(&concrete_actor->stream_consumers, sizeof(int)); core_vector_init(&concrete_actor->consumer_active_requests, sizeof(int)); core_vector_init(&concrete_actor->files, sizeof(char *)); core_vector_init(&concrete_actor->spawners, sizeof(int)); core_vector_init(&concrete_actor->counts, sizeof(int64_t)); core_vector_init(&concrete_actor->consumers, sizeof(int)); core_vector_init(&concrete_actor->stores_per_spawner, sizeof(int)); core_timer_init(&concrete_actor->input_timer); core_timer_init(&concrete_actor->counting_timer); core_timer_init(&concrete_actor->distribution_timer); biosal_dna_codec_init(&concrete_actor->codec); if (biosal_dna_codec_must_use_two_bit_encoding(&concrete_actor->codec, thorium_actor_get_node_count(actor))) { biosal_dna_codec_enable_two_bit_encoding(&concrete_actor->codec); } core_queue_init(&concrete_actor->unprepared_spawners, sizeof(int)); concrete_actor->opened_streams = 0; concrete_actor->state = BIOSAL_INPUT_CONTROLLER_STATE_NONE; #ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_10355 printf("DEBUG actor %d register ACTION_INPUT_CONTROLLER_CREATE_STORES\n", thorium_actor_name(actor)); #endif thorium_actor_add_action(actor, ACTION_INPUT_CONTROLLER_CREATE_STORES, biosal_input_controller_create_stores); thorium_actor_add_action(actor, ACTION_GET_NODE_NAME_REPLY, biosal_input_controller_get_node_name_reply); thorium_actor_add_action(actor, ACTION_GET_NODE_WORKER_COUNT_REPLY, biosal_input_controller_get_node_worker_count_reply); thorium_actor_add_action(actor, ACTION_INPUT_CONTROLLER_PREPARE_SPAWNERS, biosal_input_controller_prepare_spawners); thorium_actor_add_action(actor, ACTION_INPUT_CONTROLLER_SPAWN_READING_STREAMS, biosal_input_controller_spawn_streams); thorium_actor_add_action(actor, ACTION_INPUT_STREAM_SET_START_OFFSET_REPLY, biosal_input_controller_set_offset_reply); thorium_actor_add_script(actor, SCRIPT_INPUT_STREAM, &biosal_input_stream_script); thorium_actor_add_script(actor, SCRIPT_SEQUENCE_STORE, &biosal_sequence_store_script); thorium_actor_add_script(actor, SCRIPT_SEQUENCE_PARTITIONER, &biosal_sequence_partitioner_script); /* configuration for the input controller * other values for block size: 512, 1024, 2048, 4096, 8192 * / */ concrete_actor->block_size = 4096; concrete_actor->stores_per_worker_per_spawner = 0; #ifdef BIOSAL_INPUT_CONTROLLER_DEBUG printf("DEBUG %d init controller\n", thorium_actor_name(actor)); #endif concrete_actor->ready_spawners = 0; concrete_actor->ready_consumers = 0; concrete_actor->partitioner = THORIUM_ACTOR_NOBODY; concrete_actor->filled_consumers = 0; concrete_actor->counted = 0; }
void biosal_assembly_graph_store_receive(struct thorium_actor *self, struct thorium_message *message) { int tag; /*void *buffer;*/ struct biosal_assembly_graph_store *concrete_self; double value; struct biosal_dna_kmer kmer; /*struct core_memory_pool *ephemeral_memory;*/ int customer; int big_key_size; int big_value_size; if (thorium_actor_take_action(self, message)) { return; } /*ephemeral_memory = thorium_actor_get_ephemeral_memory(self);*/ concrete_self = thorium_actor_concrete_actor(self); tag = thorium_message_action(message); /*buffer = thorium_message_buffer(message);*/ if (tag == ACTION_SET_KMER_LENGTH) { thorium_message_unpack_int(message, 0, &concrete_self->kmer_length); biosal_dna_kmer_init_mock(&kmer, concrete_self->kmer_length, &concrete_self->storage_codec, thorium_actor_get_ephemeral_memory(self)); concrete_self->key_length_in_bytes = biosal_dna_kmer_pack_size(&kmer, concrete_self->kmer_length, &concrete_self->storage_codec); biosal_dna_kmer_destroy(&kmer, thorium_actor_get_ephemeral_memory(self)); big_key_size = concrete_self->key_length_in_bytes; big_value_size = sizeof(struct biosal_assembly_vertex); core_map_init(&concrete_self->table, big_key_size, big_value_size); core_map_set_memory_pool(&concrete_self->table, &concrete_self->persistent_memory); printf("DEBUG big_key_size %d big_value_size %d\n", big_key_size, big_value_size); /* * Configure the map for better performance. */ core_map_disable_deletion_support(&concrete_self->table); /* * The threshold of the map is not very important because * requests that hit the map have to first arrive as messages, * which are slow. */ core_map_set_threshold(&concrete_self->table, 0.95); thorium_actor_send_reply_empty(self, ACTION_SET_KMER_LENGTH_REPLY); } else if (tag == ACTION_ASSEMBLY_GET_KMER_LENGTH) { thorium_actor_send_reply_int(self, ACTION_ASSEMBLY_GET_KMER_LENGTH_REPLY, concrete_self->kmer_length); } else if (tag == ACTION_RESET) { /* * Reset the iterator. */ core_map_iterator_init(&concrete_self->iterator, &concrete_self->table); printf("DEBUG unitig_vertex_count %d\n", concrete_self->unitig_vertex_count); thorium_actor_send_reply_empty(self, ACTION_RESET_REPLY); } else if (tag == ACTION_SEQUENCE_STORE_REQUEST_PROGRESS_REPLY) { thorium_message_unpack_double(message, 0, &value); core_map_set_current_size_estimate(&concrete_self->table, value); } else if (tag == ACTION_ASK_TO_STOP) { printf("%s/%d received %d arc blocks\n", thorium_actor_script_name(self), thorium_actor_name(self), concrete_self->received_arc_block_count); thorium_actor_ask_to_stop(self, message); } else if (tag == ACTION_SET_CONSUMER) { thorium_message_unpack_int(message, 0, &customer); printf("%s/%d will use coverage distribution %d\n", thorium_actor_script_name(self), thorium_actor_name(self), customer); concrete_self->customer = customer; thorium_actor_send_reply_empty(self, ACTION_SET_CONSUMER_REPLY); } else if (tag == ACTION_PUSH_DATA) { printf("%s/%d receives ACTION_PUSH_DATA\n", thorium_actor_script_name(self), thorium_actor_name(self)); biosal_assembly_graph_store_push_data(self, message); } else if (tag == ACTION_STORE_GET_ENTRY_COUNT) { thorium_actor_send_reply_uint64_t(self, ACTION_STORE_GET_ENTRY_COUNT_REPLY, concrete_self->received); } else if (tag == ACTION_GET_RECEIVED_ARC_COUNT) { thorium_actor_send_reply_uint64_t(self, ACTION_GET_RECEIVED_ARC_COUNT_REPLY, concrete_self->received_arc_count); } }
int main(int argc, char **argv) { BEGIN_TESTS(); { struct core_map big_map; int kmer_length = 43; struct biosal_dna_kmer kmer; int count; int run_test; int coverage; void *key; int key_length; int *bucket; int i; struct biosal_dna_codec codec; struct core_memory_pool memory; core_memory_pool_init(&memory, 1048576, -1); biosal_dna_codec_init(&codec); run_test = 1; count = 100000000; printf("STRESS TEST\n"); biosal_dna_kmer_init_mock(&kmer, kmer_length, &codec, &memory); key_length = biosal_dna_kmer_pack_size(&kmer, kmer_length, &codec); biosal_dna_kmer_destroy(&kmer, &memory); core_map_init(&big_map, key_length, sizeof(coverage)); key = core_memory_allocate(key_length, -1); i = 0; while (i < count && run_test) { biosal_dna_kmer_init_random(&kmer, kmer_length, &codec, &memory); biosal_dna_kmer_pack_store_key(&kmer, key, kmer_length, &codec, &memory); bucket = core_map_add(&big_map, key); coverage = 99; (*bucket) = coverage; biosal_dna_kmer_destroy(&kmer, &memory); if (i % 100000 == 0) { printf("ADD %d/%d %" PRIu64 "\n", i, count, core_map_size(&big_map)); } i++; } core_map_destroy(&big_map); core_memory_free(key, -1); biosal_dna_codec_destroy(&codec); core_memory_pool_destroy(&memory); } END_TESTS(); return 0; }
void biosal_coverage_distribution_receive(struct thorium_actor *self, struct thorium_message *message) { int tag; struct core_map map; struct core_map_iterator iterator; int *coverage_from_message; uint64_t *count_from_message; uint64_t *frequency; int count; void *buffer; struct biosal_coverage_distribution *concrete_actor; int name; int source; struct core_memory_pool *ephemeral_memory; ephemeral_memory = thorium_actor_get_ephemeral_memory(self); name = thorium_actor_name(self); source = thorium_message_source(message); concrete_actor = (struct biosal_coverage_distribution *)thorium_actor_concrete_actor(self); tag = thorium_message_action(message); count = thorium_message_count(message); buffer = thorium_message_buffer(message); if (tag == ACTION_PUSH_DATA) { core_map_init(&map, 0, 0); core_map_set_memory_pool(&map, ephemeral_memory); core_map_unpack(&map, buffer); core_map_iterator_init(&iterator, &map); while (core_map_iterator_has_next(&iterator)) { core_map_iterator_next(&iterator, (void **)&coverage_from_message, (void **)&count_from_message); #ifdef BIOSAL_COVERAGE_DISTRIBUTION_DEBUG thorium_actor_log(self, "DEBUG DATA %d %d\n", (int)*coverage_from_message, (int)*count_from_message); #endif frequency = core_map_get(&concrete_actor->distribution, coverage_from_message); if (frequency == NULL) { frequency = core_map_add(&concrete_actor->distribution, coverage_from_message); (*frequency) = 0; } (*frequency) += (*count_from_message); } core_map_iterator_destroy(&iterator); thorium_actor_send_reply_empty(self, ACTION_PUSH_DATA_REPLY); concrete_actor->actual++; thorium_actor_log(self, "distribution/%d receives coverage data from producer/%d, %d entries / %d bytes %d/%d\n", name, source, (int)core_map_size(&map), count, concrete_actor->actual, concrete_actor->expected); if (concrete_actor->expected != 0 && concrete_actor->expected == concrete_actor->actual) { thorium_actor_log(self, "received everything %d/%d\n", concrete_actor->actual, concrete_actor->expected); biosal_coverage_distribution_write_distribution(self); thorium_actor_send_empty(self, concrete_actor->source, ACTION_NOTIFY); } core_map_destroy(&map); } else if (tag == ACTION_ASK_TO_STOP) { biosal_coverage_distribution_ask_to_stop(self, message); } else if (tag == ACTION_SET_EXPECTED_MESSAGE_COUNT) { concrete_actor->source = source; thorium_message_unpack_int(message, 0, &concrete_actor->expected); thorium_actor_log(self, "distribution %d expects %d messages\n", thorium_actor_name(self), concrete_actor->expected); thorium_actor_send_reply_empty(self, ACTION_SET_EXPECTED_MESSAGE_COUNT_REPLY); } }
void thorium_worker_init(struct thorium_worker *worker, int name, struct thorium_node *node) { int capacity; int ephemeral_memory_block_size; int injected_buffer_ring_size; int argc; char **argv; worker->tick_count = 0; thorium_load_profiler_init(&worker->profiler); argc = thorium_node_argc(node); argv = thorium_node_argv(node); #ifdef THORIUM_WORKER_DEBUG_INJECTION worker->counter_allocated_outbound_buffers = 0; worker->counter_freed_outbound_buffers_from_self = 0; worker->counter_freed_outbound_buffers_from_other_workers = 0; worker->counter_injected_outbound_buffers_other_local_workers= 0; worker->counter_injected_inbound_buffers_from_thorium_core = 0; #endif core_map_init(&worker->actor_received_messages, sizeof(int), sizeof(int)); worker->waiting_is_enabled = 0; worker->waiting_start_time = 0; core_timer_init(&worker->timer); capacity = THORIUM_WORKER_RING_CAPACITY; /*worker->work_queue = work_queue;*/ worker->node = node; worker->name = name; core_bitmap_clear_bit_uint32_t(&worker->flags, FLAG_DEAD); worker->last_warning = 0; worker->last_wake_up_count = 0; /*worker->work_queue = &worker->works;*/ /* There are two options: * 1. enable atomic operations for change visibility * 2. Use volatile head and tail. */ core_fast_ring_init(&worker->actors_to_schedule, capacity, sizeof(struct thorium_actor *)); #ifdef THORIUM_NODE_INJECT_CLEAN_WORKER_BUFFERS injected_buffer_ring_size = capacity; core_fast_ring_init(&worker->injected_clean_outbound_buffers, injected_buffer_ring_size, sizeof(void *)); core_fast_ring_init(&worker->clean_message_ring_for_triage, injected_buffer_ring_size, sizeof(struct thorium_message)); core_fast_queue_init(&worker->clean_message_queue_for_triage, sizeof(struct thorium_message)); #endif thorium_scheduler_init(&worker->scheduler, thorium_node_name(worker->node), worker->name); core_map_init(&worker->actors, sizeof(int), sizeof(int)); core_map_iterator_init(&worker->actor_iterator, &worker->actors); core_fast_ring_init(&worker->outbound_message_queue, capacity, sizeof(struct thorium_message)); core_fast_queue_init(&worker->outbound_message_queue_buffer, sizeof(struct thorium_message)); core_bitmap_clear_bit_uint32_t(&worker->flags, FLAG_DEBUG); core_bitmap_clear_bit_uint32_t(&worker->flags, FLAG_BUSY); core_bitmap_clear_bit_uint32_t(&node->flags, FLAG_ENABLE_ACTOR_LOAD_PROFILER); worker->flags = 0; core_bitmap_clear_bit_uint32_t(&worker->flags, FLAG_DEBUG_ACTORS); if (core_command_has_argument(argc, argv, DEBUG_WORKER_OPTION)) { #if 0 printf("DEBUG has option %s\n", DEBUG_WORKER_OPTION); #endif if (thorium_node_name(worker->node) == 0 && thorium_worker_name(worker) == 0) { #if 0 printf("DEBUG setting bit FLAG_DEBUG_ACTORS because %s\n", DEBUG_WORKER_OPTION); #endif core_bitmap_set_bit_uint32_t(&worker->flags, FLAG_DEBUG_ACTORS); } } worker->epoch_used_nanoseconds = 0; worker->loop_used_nanoseconds = 0; worker->scheduling_epoch_used_nanoseconds = 0; worker->started_in_thread = 0; /* 2 MiB is the default size for Linux huge pages. * \see https://wiki.debian.org/Hugepages * \see http://lwn.net/Articles/376606/ */ /* * 8 MiB */ ephemeral_memory_block_size = 8388608; /*ephemeral_memory_block_size = 16777216;*/ core_memory_pool_init(&worker->ephemeral_memory, ephemeral_memory_block_size, MEMORY_POOL_NAME_WORKER_EPHEMERAL); core_memory_pool_disable_tracking(&worker->ephemeral_memory); core_memory_pool_enable_ephemeral_mode(&worker->ephemeral_memory); #ifdef THORIUM_WORKER_ENABLE_LOCK core_lock_init(&worker->lock); #endif core_set_init(&worker->evicted_actors, sizeof(int)); core_memory_pool_init(&worker->outbound_message_memory_pool, CORE_MEMORY_POOL_MESSAGE_BUFFER_BLOCK_SIZE, MEMORY_POOL_NAME_WORKER_OUTBOUND); /* * Disable the pool so that it uses allocate and free * directly. */ #ifdef CORE_MEMORY_POOL_DISABLE_MESSAGE_BUFFER_POOL core_memory_pool_disable(&worker->outbound_message_memory_pool); #endif /* * Transport message buffers are fancy objects. */ core_memory_pool_enable_normalization(&worker->outbound_message_memory_pool); core_memory_pool_enable_alignment(&worker->outbound_message_memory_pool); worker->ticks_without_production = 0; thorium_priority_assigner_init(&worker->assigner, thorium_worker_name(worker)); /* * This variables should be set in * thorium_worker_start, but when running on 1 process with 1 thread, * thorium_worker_start is never called... */ worker->last_report = time(NULL); worker->epoch_start_in_nanoseconds = core_timer_get_nanoseconds(&worker->timer); worker->loop_start_in_nanoseconds = worker->epoch_start_in_nanoseconds; worker->loop_end_in_nanoseconds = worker->loop_start_in_nanoseconds; worker->scheduling_epoch_start_in_nanoseconds = worker->epoch_start_in_nanoseconds; /* * Avoid valgrind warnings. */ worker->epoch_load = 0; }
void thorium_worker_print_actors(struct thorium_worker *worker, struct thorium_balancer *scheduler) { struct core_map_iterator iterator; int name; int count; struct thorium_actor *actor; int producers; int consumers; int received; int difference; int script; struct core_map distribution; int frequency; struct thorium_script *script_object; int dead; int node_name; int worker_name; int previous_amount; node_name = thorium_node_name(worker->node); worker_name = worker->name; core_map_iterator_init(&iterator, &worker->actors); printf("node/%d worker/%d %d queued messages, received: %d busy: %d load: %f ring: %d scheduled actors: %d/%d\n", node_name, worker_name, thorium_worker_get_scheduled_message_count(worker), thorium_worker_get_sum_of_received_actor_messages(worker), thorium_worker_is_busy(worker), thorium_worker_get_scheduling_epoch_load(worker), core_fast_ring_size_from_producer(&worker->actors_to_schedule), thorium_scheduler_size(&worker->scheduler), (int)core_map_size(&worker->actors)); core_map_init(&distribution, sizeof(int), sizeof(int)); while (core_map_iterator_get_next_key_and_value(&iterator, &name, NULL)) { actor = thorium_node_get_actor_from_name(worker->node, name); if (actor == NULL) { continue; } dead = thorium_actor_dead(actor); if (dead) { continue; } count = thorium_actor_get_mailbox_size(actor); received = thorium_actor_get_sum_of_received_messages(actor); producers = core_map_size(thorium_actor_get_received_messages(actor)); consumers = core_map_size(thorium_actor_get_sent_messages(actor)); previous_amount = 0; core_map_get_value(&worker->actor_received_messages, &name, &previous_amount); difference = received - previous_amount;; if (!core_map_update_value(&worker->actor_received_messages, &name, &received)) { core_map_add_value(&worker->actor_received_messages, &name, &received); } printf(" [%s/%d] mailbox: %d received: %d (+%d) producers: %d consumers: %d\n", thorium_actor_script_name(actor), name, count, received, difference, producers, consumers); script = thorium_actor_script(actor); if (core_map_get_value(&distribution, &script, &frequency)) { ++frequency; core_map_update_value(&distribution, &script, &frequency); } else { frequency = 1; core_map_add_value(&distribution, &script, &frequency); } } /*printf("\n");*/ core_map_iterator_destroy(&iterator); core_map_iterator_init(&iterator, &distribution); printf("node/%d worker/%d Frequency list\n", node_name, worker_name); while (core_map_iterator_get_next_key_and_value(&iterator, &script, &frequency)) { script_object = thorium_node_find_script(worker->node, script); CORE_DEBUGGER_ASSERT(script_object != NULL); printf("node/%d worker/%d Frequency %s => %d\n", node_name, worker->name, thorium_script_name(script_object), frequency); } core_map_iterator_destroy(&iterator); core_map_destroy(&distribution); }
void thorium_balancer_generate_symmetric_migrations(struct thorium_balancer *self, struct core_map *symmetric_actor_scripts, struct core_vector *migrations) { int i; int worker_count; struct thorium_worker *worker; struct core_map *set; struct core_map_iterator iterator; struct thorium_migration migration; struct core_map script_current_worker; struct core_map script_current_worker_actor_count; int frequency; int current_worker; int current_worker_actor_count; int old_worker; #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY struct thorium_script *actual_script; #endif struct thorium_node *node; int actor_name; int script; int new_worker; struct thorium_actor *actor; int enabled; /* Gather symmetric actors: */ #ifdef THORIUM_SCHEDULER_ENABLE_SYMMETRIC_SCHEDULING enabled = 1; #else enabled = 0; #endif core_map_init(&script_current_worker, sizeof(int), sizeof(int)); core_map_init(&script_current_worker_actor_count, sizeof(int), sizeof(int)); node = thorium_worker_pool_get_node(self->pool); worker_count = thorium_worker_pool_worker_count(self->pool); for (i = 0; i < worker_count; i++) { worker = thorium_worker_pool_get_worker(self->pool, i); set = thorium_worker_get_actors(worker); core_map_iterator_init(&iterator, set); while (core_map_iterator_get_next_key_and_value(&iterator, &actor_name, NULL)) { actor = thorium_node_get_actor_from_name(node, actor_name); if (actor == NULL) { continue; } script = thorium_actor_script(actor); /* * Check if the actor is symmetric */ if (core_map_get_value(symmetric_actor_scripts, &script, &frequency)) { current_worker = 0; if (!core_map_get_value(&script_current_worker, &script, ¤t_worker)) { core_map_add_value(&script_current_worker, &script, ¤t_worker); } current_worker_actor_count = 0; if (!core_map_get_value(&script_current_worker_actor_count, &script, ¤t_worker_actor_count)) { core_map_add_value(&script_current_worker_actor_count, &script, ¤t_worker_actor_count); } /* * Emit migration instruction */ old_worker = thorium_balancer_get_actor_worker(self, actor_name); new_worker = current_worker; #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY actual_script = thorium_node_find_script(node, script); #endif if (enabled && old_worker != new_worker) { thorium_migration_init(&migration, actor_name, old_worker, new_worker); core_vector_push_back(migrations, &migration); thorium_migration_destroy(&migration); #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("[EMIT] "); #endif } else { #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("[MOCK] "); #endif } #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("SCHEDULER -> symmetric placement... %s/%d scheduled for execution on worker/%d of node/%d\n", thorium_script_description(actual_script), actor_name, new_worker, thorium_node_name(node)); #endif ++current_worker_actor_count; core_map_update_value(&script_current_worker_actor_count, &script, ¤t_worker_actor_count); /* The current worker is full. * Increment the current worker and set the * worker actor count to 0. */ if (current_worker_actor_count == frequency) { ++current_worker; core_map_update_value(&script_current_worker, &script, ¤t_worker); current_worker_actor_count = 0; core_map_update_value(&script_current_worker_actor_count, &script, ¤t_worker_actor_count); } } } core_map_iterator_destroy(&iterator); } core_map_destroy(&script_current_worker); core_map_destroy(&script_current_worker_actor_count); }
void thorium_balancer_detect_symmetric_scripts(struct thorium_balancer *self, struct core_map *symmetric_actor_scripts) { int i; struct thorium_worker *worker; struct thorium_actor *actor; struct core_map_iterator iterator; struct core_map *set; int actor_name; struct thorium_node *node; int script; int frequency; struct core_map frequencies; int worker_count; int population_per_worker; #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY struct thorium_script *actual_script; #endif worker_count = thorium_worker_pool_worker_count(self->pool); core_map_init(&frequencies, sizeof(int), sizeof(int)); node = thorium_worker_pool_get_node(self->pool); /* Gather frequencies */ for (i = 0; i < worker_count; i++) { worker = thorium_worker_pool_get_worker(self->pool, i); set = thorium_worker_get_actors(worker); core_map_iterator_init(&iterator, set); while (core_map_iterator_get_next_key_and_value(&iterator, &actor_name, NULL)) { actor = thorium_node_get_actor_from_name(node, actor_name); if (actor == NULL) { continue; } script = thorium_actor_script(actor); frequency = 0; if (!core_map_get_value(&frequencies, &script, &frequency)) { core_map_add_value(&frequencies, &script, &frequency); } ++frequency; core_map_update_value(&frequencies, &script, &frequency); } core_map_iterator_destroy(&iterator); } /* * Detect symmetric scripts */ core_map_iterator_init(&iterator, &frequencies); while (core_map_iterator_get_next_key_and_value(&iterator, &script, &frequency)) { #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY actual_script = thorium_node_find_script(node, script); #endif #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("SCHEDULER test symmetry %s %d\n", thorium_script_description(actual_script), frequency); #endif if (frequency % worker_count == 0) { population_per_worker = frequency / worker_count; core_map_add_value(symmetric_actor_scripts, &script, &population_per_worker); #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("SCHEDULER: script %s is symmetric, worker_count: %d, population_per_worker: %d\n", thorium_script_description(actual_script), worker_count, population_per_worker); #endif } } core_map_iterator_destroy(&iterator); core_map_destroy(&frequencies); }
void thorium_balancer_balance(struct thorium_balancer *self) { /* * The 95th percentile is useful: * \see http://en.wikipedia.org/wiki/Burstable_billing * \see http://www.init7.net/en/backbone/95-percent-rule */ int load_percentile_50; struct core_timer timer; int i; struct core_vector loads; struct core_vector loads_unsorted; struct core_vector burdened_workers; struct core_vector stalled_workers; struct thorium_worker *worker; struct thorium_node *node; /*struct core_set *set;*/ struct core_pair pair; struct core_vector_iterator vector_iterator; int old_worker; int actor_name; int messages; int maximum; int with_maximum; struct core_map *set; struct core_map_iterator set_iterator; int stalled_index; int stalled_count; int new_worker_index; struct core_vector migrations; struct thorium_migration migration; struct thorium_migration *migration_to_do; struct thorium_actor *actor; int candidates; int load_value; int remaining_load; int projected_load; struct core_vector actors_to_migrate; int total; int with_messages; int stalled_percentile; int burdened_percentile; int old_total; int old_load; int new_load; int predicted_new_load; struct core_pair *pair_pointer; struct thorium_worker *new_worker; /*int new_total;*/ int actor_load; int test_stalled_index; int tests; int found_match; int spawned_actors; int killed_actors; int perfect; #ifdef THORIUM_SCHEDULER_ENABLE_SYMMETRIC_SCHEDULING struct core_map symmetric_actor_scripts; int script; #endif node = thorium_worker_pool_get_node(self->pool); spawned_actors = thorium_node_get_counter(node, CORE_COUNTER_SPAWNED_ACTORS); /* There is nothing to balance... */ if (spawned_actors == 0) { return; } killed_actors = thorium_node_get_counter(node, CORE_COUNTER_KILLED_ACTORS); /* * The system can probably not be balanced to get in * a better shape anyway. */ if (spawned_actors == self->last_spawned_actors && killed_actors == self->last_killed_actors && self->last_migrations == 0) { printf("SCHEDULER: balance can not be improved because nothing changed.\n"); return; } /* Check if we have perfection */ perfect = 1; for (i = 0; i < thorium_worker_pool_worker_count(self->pool); i++) { worker = thorium_worker_pool_get_worker(self->pool, i); load_value = thorium_worker_get_epoch_load(worker) * 100; if (load_value != 100) { perfect = 0; break; } } if (perfect) { printf("SCHEDULER: perfect balance can not be improved.\n"); return; } /* update counters */ self->last_spawned_actors = spawned_actors; self->last_killed_actors = killed_actors; /* Otherwise, try to balance things */ core_timer_init(&timer); core_timer_start(&timer); #ifdef THORIUM_SCHEDULER_ENABLE_SYMMETRIC_SCHEDULING core_map_init(&symmetric_actor_scripts, sizeof(int), sizeof(int)); thorium_balancer_detect_symmetric_scripts(self, &symmetric_actor_scripts); #endif #ifdef THORIUM_WORKER_ENABLE_LOCK /* Lock all workers first */ for (i = 0; i < thorium_worker_pool_worker_count(self->pool); i++) { worker = thorium_worker_pool_get_worker(self->pool, i); thorium_worker_lock(worker); } #endif core_vector_init(&migrations, sizeof(struct thorium_migration)); #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("BALANCING\n"); #endif core_vector_init(&loads, sizeof(int)); core_vector_init(&loads_unsorted, sizeof(int)); core_vector_init(&burdened_workers, sizeof(struct core_pair)); core_vector_init(&stalled_workers, sizeof(struct core_pair)); core_vector_init(&actors_to_migrate, sizeof(struct core_pair)); for (i = 0; i < thorium_worker_pool_worker_count(self->pool); i++) { worker = thorium_worker_pool_get_worker(self->pool, i); load_value = thorium_worker_get_scheduling_epoch_load(worker) * SCHEDULER_PRECISION; #if 0 printf("DEBUG LOAD %d %d\n", i, load_value); #endif core_vector_push_back(&loads, &load_value); core_vector_push_back(&loads_unsorted, &load_value); } core_vector_sort_int(&loads); stalled_percentile = core_statistics_get_percentile_int(&loads, SCHEDULER_WINDOW); /*load_percentile_25 = core_statistics_get_percentile_int(&loads, 25);*/ load_percentile_50 = core_statistics_get_percentile_int(&loads, 50); /*load_percentile_75 = core_statistics_get_percentile_int(&loads, 75);*/ burdened_percentile = core_statistics_get_percentile_int(&loads, 100 - SCHEDULER_WINDOW); #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("Percentiles for epoch loads: "); core_statistics_print_percentiles_int(&loads); #endif for (i = 0; i < thorium_worker_pool_worker_count(self->pool); i++) { worker = thorium_worker_pool_get_worker(self->pool, i); load_value = core_vector_at_as_int(&loads_unsorted, i); set = thorium_worker_get_actors(worker); if (stalled_percentile == burdened_percentile) { #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("scheduling_class:%s ", THORIUM_CLASS_NORMAL_STRING); #endif } else if (load_value <= stalled_percentile) { #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("scheduling_class:%s ", THORIUM_CLASS_STALLED_STRING); #endif core_pair_init(&pair, load_value, i); core_vector_push_back(&stalled_workers, &pair); } else if (load_value >= burdened_percentile) { #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("scheduling_class:%s ", THORIUM_CLASS_BURDENED_STRING); #endif core_pair_init(&pair, load_value, i); core_vector_push_back(&burdened_workers, &pair); } else { #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("scheduling_class:%s ", THORIUM_CLASS_NORMAL_STRING); #endif } #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY thorium_worker_print_actors(worker, self); #endif } core_vector_sort_int_reverse(&burdened_workers); core_vector_sort_int(&stalled_workers); stalled_count = core_vector_size(&stalled_workers); #ifdef THORIUM_SCHEDULER_ENABLE_VERBOSITY printf("MIGRATIONS (stalled: %d, burdened: %d)\n", (int)core_vector_size(&stalled_workers), (int)core_vector_size(&burdened_workers)); #endif stalled_index = 0; core_vector_iterator_init(&vector_iterator, &burdened_workers); while (stalled_count > 0 && core_vector_iterator_get_next_value(&vector_iterator, &pair)) { old_worker = core_pair_get_second(&pair); worker = thorium_worker_pool_get_worker(self->pool, old_worker); set = thorium_worker_get_actors(worker); /* thorium_worker_print_actors(worker); printf("\n"); */ /* * Lock the worker and try to select actors for migration */ core_map_iterator_init(&set_iterator, set); maximum = -1; with_maximum = 0; total = 0; with_messages = 0; while (core_map_iterator_get_next_key_and_value(&set_iterator, &actor_name, NULL)) { actor = thorium_node_get_actor_from_name(thorium_worker_pool_get_node(self->pool), actor_name); messages = thorium_balancer_get_actor_production(self, actor); if (maximum == -1 || messages > maximum) { maximum = messages; with_maximum = 1; } else if (messages == maximum) { with_maximum++; } if (messages > 0) { ++with_messages; } total += messages; } core_map_iterator_destroy(&set_iterator); core_map_iterator_init(&set_iterator, set); --with_maximum; candidates = 0; load_value = thorium_worker_get_scheduling_epoch_load(worker) * SCHEDULER_PRECISION; remaining_load = load_value; #if 0 printf("maximum %d with_maximum %d\n", maximum, with_maximum); #endif while (core_map_iterator_get_next_key_and_value(&set_iterator, &actor_name, NULL)) { actor = thorium_node_get_actor_from_name(thorium_worker_pool_get_node(self->pool), actor_name); if (actor == NULL) { continue; } messages = thorium_balancer_get_actor_production(self, actor); #ifdef THORIUM_SCHEDULER_ENABLE_SYMMETRIC_SCHEDULING script = thorium_actor_script(actor); /* symmetric actors are migrated elsewhere. */ if (core_map_get_value(&symmetric_actor_scripts, &script, NULL)) { continue; } #endif /* Simulate the remaining load */ projected_load = remaining_load; projected_load -= ((0.0 + messages) / total) * load_value; #ifdef THORIUM_SCHEDULER_DEBUG printf(" TESTING actor %d, production was %d, projected_load is %d (- %d * (1 - %d/%d)\n", actor_name, messages, projected_load, load_value, messages, total); #endif /* An actor without any queued messages should not be migrated */ if (messages > 0 && ((with_maximum > 0 && messages == maximum) || messages < maximum) /* * Avoid removing too many actors because * generating a stalled one is not desired */ && (projected_load >= load_percentile_50 /* * The previous rule does not apply when there * are 2 actors. */ || with_messages == 2) ) { remaining_load = projected_load; candidates++; if (messages == maximum) { --with_maximum; } core_pair_init(&pair, messages, actor_name); core_vector_push_back(&actors_to_migrate, &pair); #ifdef THORIUM_SCHEDULER_DEBUG printf("early CANDIDATE for migration: actor %d, worker %d\n", actor_name, old_worker); #endif } } core_map_iterator_destroy(&set_iterator); } core_vector_iterator_destroy(&vector_iterator); /* Sort the candidates */ /* core_vector_sort_int(&actors_to_migrate); printf("Percentiles for production: "); core_statistics_print_percentiles_int(&actors_to_migrate); */ /* Sort them in reverse order. */ core_vector_sort_int_reverse(&actors_to_migrate); core_vector_iterator_init(&vector_iterator, &actors_to_migrate); /* For each highly active actor, * try to match it with a stalled worker */ while (core_vector_iterator_get_next_value(&vector_iterator, &pair)) { actor_name = core_pair_get_second(&pair); actor = thorium_node_get_actor_from_name(thorium_worker_pool_get_node(self->pool), actor_name); if (actor == NULL) { continue; } messages = thorium_balancer_get_actor_production(self, actor); old_worker = thorium_actor_assigned_worker(actor); worker = thorium_worker_pool_get_worker(self->pool, old_worker); /* old_total can not be 0 because otherwise the would not * be burdened. */ old_total = thorium_worker_get_production(worker, self); with_messages = thorium_worker_get_producer_count(worker, self); old_load = thorium_worker_get_scheduling_epoch_load(worker) * SCHEDULER_PRECISION; actor_load = ((0.0 + messages) / old_total) * old_load; /* Try to find a stalled worker that can take it. */ test_stalled_index = stalled_index; tests = 0; predicted_new_load = 0; found_match = 0; while (tests < stalled_count) { core_vector_get_value(&stalled_workers, test_stalled_index, &pair); new_worker_index = core_pair_get_second(&pair); new_worker = thorium_worker_pool_get_worker(self->pool, new_worker_index); new_load = thorium_worker_get_scheduling_epoch_load(new_worker) * SCHEDULER_PRECISION; /*new_total = thorium_worker_get_production(new_worker);*/ predicted_new_load = new_load + actor_load; if (predicted_new_load > SCHEDULER_PRECISION /* && with_messages != 2 */) { #ifdef THORIUM_SCHEDULER_DEBUG printf("Scheduler: skipping actor %d, predicted load is %d >= 100\n", actor_name, predicted_new_load); #endif ++tests; ++test_stalled_index; if (test_stalled_index == stalled_count) { test_stalled_index = 0; } continue; } /* Otherwise, this stalled worker is fine... */ stalled_index = test_stalled_index; found_match = 1; break; } /* This actor can not be migrated to any stalled worker. */ if (!found_match) { continue; } /* Otherwise, update the load of the stalled one and go forward with the change. */ pair_pointer = (struct core_pair *)core_vector_at(&stalled_workers, stalled_index); core_pair_set_first(pair_pointer, predicted_new_load); ++stalled_index; if (stalled_index == stalled_count) { stalled_index = 0; } #if 0 new_worker = thorium_worker_pool_get_worker(pool, new_worker_index); printf(" CANDIDATE: actor %d old worker %d (%d - %d = %d) new worker %d (%d + %d = %d)\n", actor_name, old_worker, value, messages, 2new_score, new_worker_index, new_worker_old_score, messages, new_worker_new_score); #endif thorium_migration_init(&migration, actor_name, old_worker, new_worker_index); core_vector_push_back(&migrations, &migration); thorium_migration_destroy(&migration); } core_vector_iterator_destroy(&vector_iterator); core_vector_destroy(&stalled_workers); core_vector_destroy(&burdened_workers); core_vector_destroy(&loads); core_vector_destroy(&loads_unsorted); core_vector_destroy(&actors_to_migrate); /* Update the last values */ for (i = 0; i < thorium_worker_pool_worker_count(self->pool); i++) { worker = thorium_worker_pool_get_worker(self->pool, i); set = thorium_worker_get_actors(worker); core_map_iterator_init(&set_iterator, set); while (core_map_iterator_get_next_key_and_value(&set_iterator, &actor_name, NULL)) { actor = thorium_node_get_actor_from_name(thorium_worker_pool_get_node(self->pool), actor_name); thorium_balancer_update_actor_production(self, actor); } core_map_iterator_destroy(&set_iterator); thorium_worker_reset_scheduling_epoch(worker); } #ifdef THORIUM_SCHEDULER_ENABLE_SYMMETRIC_SCHEDULING /* Generate migrations for symmetric actors. */ thorium_balancer_generate_symmetric_migrations(self, &symmetric_actor_scripts, &migrations); #endif /* Actually do the migrations */ core_vector_iterator_init(&vector_iterator, &migrations); while (core_vector_iterator_next(&vector_iterator, (void **)&migration_to_do)) { thorium_balancer_migrate(self, migration_to_do); } core_vector_iterator_destroy(&vector_iterator); self->last_migrations = core_vector_size(&migrations); core_vector_destroy(&migrations); #ifdef THORIUM_WORKER_ENABLE_LOCK /* Unlock all workers */ for (i = 0; i < thorium_worker_pool_worker_count(self->pool); i++) { worker = thorium_worker_pool_get_worker(self->pool, i); thorium_worker_unlock(worker); } #endif #ifdef THORIUM_SCHEDULER_ENABLE_SYMMETRIC_SCHEDULING core_map_destroy(&symmetric_actor_scripts); #endif core_timer_stop(&timer); printf("SCHEDULER: elapsed time for balancing: %d us, %d migrations performed\n", (int)(core_timer_get_elapsed_nanoseconds(&timer) / 1000), self->last_migrations); }