Exemplo n.º 1
0
void thorium_balancer_update_actor_production(struct thorium_balancer *self, struct thorium_actor *actor)
{
    int messages;
    int name;

    if (actor == NULL) {
        return;
    }

    messages = thorium_actor_get_sum_of_received_messages(actor);
    name = thorium_actor_name(actor);

    if (!core_map_update_value(&self->last_actor_received_messages, &name, &messages)) {
        core_map_add_value(&self->last_actor_received_messages, &name, &messages);
    }
}
Exemplo n.º 2
0
void biosal_input_controller_receive(struct thorium_actor *actor, struct thorium_message *message)
{
    int tag;
    int count;
    char *file;
    void *buffer;
    struct biosal_input_controller *controller;
    struct biosal_input_controller *concrete_actor;
    int destination;
    int script;
    int stream;
    char *local_file;
    int i;
    int name;
    int source;
    int destination_index;
    struct thorium_message new_message;
    int error;
    int stream_index;
    int64_t entries;
    int64_t *bucket;
    int *int_bucket;
    int spawner;
    int command_name;
    int stream_name;
    int consumer;
    int consumer_index;
    int *bucket_for_requests;
    char *new_buffer;
    int new_count;
    int file_index;
    struct core_vector mega_blocks;
    struct core_vector_iterator vector_iterator;
    struct biosal_mega_block *mega_block;
    struct core_vector *vector_bucket;
    struct core_vector block_counts;
    uint64_t block_entries;
    int mega_block_index;
    uint64_t offset;
    struct biosal_mega_block *block;
    int acquaintance_index;

    if (thorium_actor_take_action(actor, message)) {
        return;
    }

    thorium_message_get_all(message, &tag, &count, &buffer, &source);

    name = thorium_actor_name(actor);
    controller = (struct biosal_input_controller *)thorium_actor_concrete_actor(actor);
    concrete_actor = controller;

    if (tag == ACTION_START) {

        core_vector_init(&concrete_actor->spawners, 0);
        core_vector_unpack(&concrete_actor->spawners, buffer);

        core_vector_resize(&concrete_actor->stores_per_spawner,
                        core_vector_size(&concrete_actor->spawners));

        for (i = 0; i < core_vector_size(&concrete_actor->spawners); i++) {
            int_bucket = (int *)core_vector_at(&concrete_actor->stores_per_spawner, i);
            *int_bucket = 0;

            spawner = core_vector_at_as_int(&concrete_actor->spawners, i);

            core_queue_enqueue(&concrete_actor->unprepared_spawners, &spawner);
        }

        concrete_actor->state = BIOSAL_INPUT_CONTROLLER_STATE_PREPARE_SPAWNERS;

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL
        printf("DEBUG preparing first spawner\n");
#endif

        thorium_actor_send_to_self_empty(actor, ACTION_INPUT_CONTROLLER_PREPARE_SPAWNERS);

        /*
        thorium_dispatcher_print(thorium_actor_dispatcher(actor));
        */

    } else if (tag == ACTION_ADD_FILE) {

        file = (char *)buffer;

        local_file = core_memory_allocate(strlen(file) + 1, MEMORY_CONTROLLER);
        strcpy(local_file, file);

        printf("controller %d ACTION_ADD_FILE %s\n",
                        thorium_actor_name(actor),
                        local_file);

        core_vector_push_back(&concrete_actor->files, &local_file);

        bucket = core_vector_at(&concrete_actor->files, core_vector_size(&concrete_actor->files) - 1);
        local_file = *(char **)bucket;

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
        printf("DEBUG11 ACTION_ADD_FILE %s %p bucket %p index %d\n",
                        local_file, local_file, (void *)bucket, core_vector_size(&concrete_actor->files) - 1);
#endif

        thorium_actor_send_reply_empty(actor, ACTION_ADD_FILE_REPLY);

    } else if (tag == ACTION_SPAWN_REPLY) {

        if (concrete_actor->state == BIOSAL_INPUT_CONTROLLER_STATE_SPAWN_STORES) {

            biosal_input_controller_add_store(actor, message);
            return;

        } else if (concrete_actor->state == BIOSAL_INPUT_CONTROLLER_STATE_PREPARE_SPAWNERS) {

            concrete_actor->ready_spawners++;
            thorium_message_unpack_int(message, 0, &name);
            thorium_actor_send_empty(actor, name, ACTION_ASK_TO_STOP);
            thorium_actor_send_to_self_empty(actor, ACTION_INPUT_CONTROLLER_PREPARE_SPAWNERS);

            if (concrete_actor->ready_spawners == (int)core_vector_size(&concrete_actor->spawners)) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
                printf("DEBUG all spawners are prepared\n");
#endif
                thorium_actor_send_to_supervisor_empty(actor, ACTION_START_REPLY);
            }

            return;

        } else if (concrete_actor->state == BIOSAL_INPUT_CONTROLLER_STATE_SPAWN_PARTITIONER) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
            printf("DEBUG received spawn reply, state is spawn_partitioner\n");
#endif

            thorium_message_unpack_int(message, 0, &concrete_actor->partitioner);

            /* configure the partitioner
             */
            destination = concrete_actor->partitioner;
            thorium_actor_send_int(actor, destination,
                            ACTION_SEQUENCE_PARTITIONER_SET_BLOCK_SIZE,
                            concrete_actor->block_size);
            thorium_actor_send_int(actor, destination,
                            ACTION_SEQUENCE_PARTITIONER_SET_ACTOR_COUNT,
                            core_vector_size(&concrete_actor->consumers));

            core_vector_init(&block_counts, sizeof(uint64_t));

            for (i = 0; i < core_vector_size(&concrete_actor->mega_block_vector); i++) {

                block = (struct biosal_mega_block *)core_vector_at(&concrete_actor->mega_block_vector, i);
                block_entries = biosal_mega_block_get_entries(block);

                core_vector_push_back_uint64_t(&block_counts, block_entries);
            }

            new_count = core_vector_pack_size(&block_counts);
            new_buffer = thorium_actor_allocate(actor, new_count);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
            printf("DEBUG packed counts, %d bytes\n", count);
#endif

            core_vector_pack(&block_counts, new_buffer);
            thorium_message_init(&new_message, ACTION_SEQUENCE_PARTITIONER_SET_ENTRY_VECTOR,
                            new_count, new_buffer);
            thorium_actor_send(actor, destination, &new_message);
            core_vector_destroy(&block_counts);

            return;

        } else if (concrete_actor->state == BIOSAL_INPUT_CONTROLLER_STATE_SPAWN_READING_STREAMS) {

            thorium_message_unpack_int(message, 0, &stream);

            stream_index = stream;

            mega_block_index = core_vector_size(&concrete_actor->reading_streams);

            core_vector_push_back_int(&concrete_actor->reading_streams, stream_index);
            core_vector_push_back_int(&concrete_actor->partition_commands, -1);
            core_vector_push_back_int(&concrete_actor->stream_consumers, -1);

            stream_index = core_vector_size(&concrete_actor->reading_streams) - 1;
            mega_block = (struct biosal_mega_block *)core_vector_at(&concrete_actor->mega_block_vector,
                            mega_block_index);

            offset = biosal_mega_block_get_offset(mega_block);

            core_map_add_value(&concrete_actor->assigned_blocks,
                            &stream_index, &mega_block_index);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_READING_STREAMS
            printf("DEBUG setting offset to %" PRIu64 " for stream/%d\n",
                            offset, stream);
#endif

            thorium_actor_send_uint64_t(actor, stream, ACTION_INPUT_STREAM_SET_START_OFFSET, offset);

            return;
        }

        stream = *(int *)buffer;

        file_index = core_vector_size(&concrete_actor->counting_streams);
        local_file = *(char **)core_vector_at(&concrete_actor->files, file_index);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_READING_STREAMS
        printf("DEBUG actor %d receives stream %d from spawner %d for file %s\n",
                        name, stream, source,
                        local_file);
#endif

        core_vector_push_back(&concrete_actor->counting_streams, &stream);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_READING_STREAMS
        printf("asking stream/%d to open %s\n", stream, local_file);
#endif
        thorium_message_init(&new_message, ACTION_INPUT_OPEN, strlen(local_file) + 1, local_file);

#ifdef DEBUG_ISSUE_594
        thorium_message_print(&new_message);
        printf("SEND Buffer %s\n", local_file);
#endif

        thorium_actor_send(actor, stream, &new_message);
        thorium_message_destroy(&new_message);

        if (core_vector_size(&concrete_actor->counting_streams) != core_vector_size(&concrete_actor->files)) {

            thorium_actor_send_to_self_empty(actor, ACTION_INPUT_SPAWN);

        }

#ifdef DEBUG_ISSUE_594
        printf("EXIT Buffer %s\n", local_file);
#endif

    } else if (tag == ACTION_INPUT_OPEN_REPLY) {

        if (concrete_actor->state == BIOSAL_INPUT_CONTROLLER_STATE_SPAWN_READING_STREAMS) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_READING_STREAMS
            printf("DEBUG receives open.reply for reading stream/%d\n", source);
#endif
            concrete_actor->opened_streams++;

            if (concrete_actor->opened_streams == core_vector_size(&concrete_actor->mega_block_vector)) {
                thorium_actor_send_to_self_empty(actor, ACTION_INPUT_CONTROLLER_CREATE_STORES);
            }

            return;
        }

        concrete_actor->opened_streams++;

        stream = source;
        thorium_message_unpack_int(message, 0, &error);

        if (error == BIOSAL_INPUT_ERROR_NO_ERROR) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
            printf("DEBUG actor %d asks %d ACTION_INPUT_COUNT_IN_PARALLEL\n", name, stream);
#endif

            thorium_actor_send_vector(actor, stream, ACTION_INPUT_COUNT_IN_PARALLEL,
                            &concrete_actor->spawners);
        } else {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
            printf("DEBUG actor %d received error %d from %d\n", name, error, stream);
#endif
            concrete_actor->counted++;
        }

	/* if all streams failed, notice supervisor */
        if (concrete_actor->counted == core_vector_size(&concrete_actor->files)) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
#endif
            printf("DEBUG %d: Error all streams failed.\n",
                            thorium_actor_name(actor));
            thorium_actor_send_to_supervisor_empty(actor, ACTION_INPUT_DISTRIBUTE_REPLY);
        }

/*
        if (concrete_actor->opened_streams == core_vector_size(&concrete_actor->files)) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
            printf("DEBUG controller %d sends ACTION_INPUT_DISTRIBUTE_REPLY to supervisor %d [%d/%d]\n",
                            name, thorium_actor_supervisor(actor),
                            concrete_actor->opened_streams, core_vector_size(&concrete_actor->files));
#endif

        }
*/

    } else if (tag == ACTION_INPUT_COUNT_PROGRESS) {

        stream_index = core_vector_index_of(&concrete_actor->counting_streams, &source);
        local_file = core_vector_at_as_char_pointer(&concrete_actor->files, stream_index);
        thorium_message_unpack_int64_t(message, 0, &entries);

        bucket = (int64_t *)core_vector_at(&concrete_actor->counts, stream_index);

        printf("controller/%d receives progress from stream/%d file %s %" PRIu64 " entries so far\n",
                        name, source, local_file, entries);
        *bucket = entries;

    } else if (tag == ACTION_INPUT_COUNT_IN_PARALLEL_REPLY) {

        stream_index = core_vector_index_of(&concrete_actor->counting_streams, &source);
        local_file = core_vector_at_as_char_pointer(&concrete_actor->files, stream_index);

        core_vector_init(&mega_blocks, 0);
        core_vector_unpack(&mega_blocks, buffer);

        printf("DEBUG receive mega blocks from %d\n", source);
        /*
         * Update the file index for every mega block.
         */
        core_vector_iterator_init(&vector_iterator, &mega_blocks);

        bucket = (int64_t*)core_vector_at(&concrete_actor->counts, stream_index);
        (*bucket) = 0;

        while (core_vector_iterator_has_next(&vector_iterator)) {
            core_vector_iterator_next(&vector_iterator, (void **)&mega_block);

            printf("SETTING setting file to %d for mega block\n", stream_index);
            biosal_mega_block_set_file(mega_block, stream_index);

            entries = biosal_mega_block_get_entries_from_start(mega_block);

            printf("Cataloging %d ENTRIES\n", (int)entries);

            (*bucket) = entries;

            biosal_mega_block_print(mega_block);
        }

        core_vector_iterator_destroy(&vector_iterator);

        vector_bucket = (struct core_vector *)core_map_add(&concrete_actor->mega_blocks, &stream_index);
        core_vector_init_copy(vector_bucket, &mega_blocks);

        core_vector_destroy(&mega_blocks);

        concrete_actor->counted++;

        printf("controller/%d received from stream/%d for file %s %" PRIu64 " entries (final) %d/%d\n",
                        name, source, local_file, entries,
                        concrete_actor->counted, (int)core_vector_size(&concrete_actor->files));

        thorium_actor_send_reply_empty(actor, ACTION_INPUT_CLOSE);

        /* continue work here, tell supervisor about it */
        if (concrete_actor->counted == core_vector_size(&concrete_actor->files)) {
            thorium_actor_send_to_self_empty(actor, ACTION_INPUT_CONTROLLER_SPAWN_READING_STREAMS);
        }


    } else if (tag == ACTION_INPUT_DISTRIBUTE) {

        core_timer_start(&concrete_actor->input_timer);
        core_timer_start(&concrete_actor->counting_timer);

        /* for each file, spawn a stream to count */

        /* no files, return immediately
         */
        if (core_vector_size(&concrete_actor->files) == 0) {

            printf("Error: no file to distribute...\n");
            thorium_actor_send_reply_empty(actor, ACTION_INPUT_DISTRIBUTE_REPLY);
            return;
        }

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
        printf("DEBUG actor %d receives ACTION_INPUT_DISTRIBUTE\n", name);
#endif

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
        printf("DEBUG send ACTION_INPUT_SPAWN to self\n");
#endif

        thorium_actor_send_to_self_empty(actor, ACTION_INPUT_SPAWN);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
        printf("DEBUG resizing counts to %d\n", core_vector_size(&concrete_actor->files));
#endif

        core_vector_resize(&concrete_actor->counts, core_vector_size(&concrete_actor->files));

        for (i = 0; i < core_vector_size(&concrete_actor->counts); i++) {
            bucket = (int64_t*)core_vector_at(&concrete_actor->counts, i);
            *bucket = 0;
        }

    } else if (tag == ACTION_INPUT_SPAWN && source == name) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
        printf("DEBUG ACTION_INPUT_SPAWN\n");
#endif

        script = SCRIPT_INPUT_STREAM;

        concrete_actor->state = BIOSAL_INPUT_CONTROLLER_STATE_SPAWN_STREAMS;

        /* the next file name to send is the current number of streams */
        i = core_vector_size(&concrete_actor->counting_streams);

        destination_index = i % core_vector_size(&concrete_actor->spawners);
        destination = *(int *)core_vector_at(&concrete_actor->spawners, destination_index);

        thorium_message_init(message, ACTION_SPAWN, sizeof(script), &script);
        thorium_actor_send(actor, destination, message);

        bucket = core_vector_at(&concrete_actor->files, i);
        local_file = *(char **)core_vector_at(&concrete_actor->files, i);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
        printf("DEBUG890 local_file %p bucket %p index %d\n", local_file, (void *)bucket,
                        i);
#endif

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
        printf("DEBUG actor %d spawns a stream for file %d/%d via spawner %d\n",
                        name, i, core_vector_size(&concrete_actor->files), destination);
#endif

        /* also, spawn 4 stores on each node */

    } else if (tag == ACTION_ASK_TO_STOP && ( source == thorium_actor_supervisor(actor)
                            || source == thorium_actor_name(actor))) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_LEVEL_2
#endif

        /* stop streams
         */
        for (i = 0; i < core_vector_size(&concrete_actor->counting_streams); i++) {
            stream = *(int *)core_vector_at(&concrete_actor->counting_streams, i);

            thorium_actor_send_empty(actor, stream, ACTION_ASK_TO_STOP);
        }
        for (i = 0; i < core_vector_size(&concrete_actor->reading_streams); i++) {
            stream = *(int *)core_vector_at(&concrete_actor->reading_streams, i);

            thorium_actor_send_empty(actor, stream, ACTION_ASK_TO_STOP);
        }



#if 0
        /* stop data stores
         */
        for (i = 0; i < core_vector_size(&concrete_actor->consumers); i++) {
            store = core_vector_at_as_int(&concrete_actor->consumers, i);

            thorium_actor_send_empty(actor, store, ACTION_ASK_TO_STOP);
        }
#endif
        /* stop partitioner
         */

        if (concrete_actor->partitioner != THORIUM_ACTOR_NOBODY) {
            thorium_actor_send_empty(actor,
                                concrete_actor->partitioner,
                        ACTION_ASK_TO_STOP);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
            printf("DEBUG controller %d sends ACTION_ASK_TO_STOP_REPLY to %d\n",
                        thorium_actor_name(actor),
                        thorium_message_source(message));
#endif

        }

        thorium_actor_send_reply_empty(actor, ACTION_ASK_TO_STOP_REPLY);

        /* stop self
         */
        thorium_actor_send_to_self_empty(actor, ACTION_STOP);

        thorium_actor_ask_to_stop(actor, message);

        printf("DEBUG controller %d dies\n", name);
#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
#endif

    } else if (tag == ACTION_INPUT_CONTROLLER_CREATE_PARTITION && source == name) {

        spawner = *(int *)core_vector_at(&concrete_actor->spawners,
                        core_vector_size(&concrete_actor->spawners) / 2);

        thorium_actor_send_int(actor, spawner, ACTION_SPAWN,
                        SCRIPT_SEQUENCE_PARTITIONER);
        concrete_actor->state = BIOSAL_INPUT_CONTROLLER_STATE_SPAWN_PARTITIONER;

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
        printf("DEBUG input controller %d spawns a partitioner via spawner %d\n",
                        name,  spawner);
#endif

    } else if (tag == ACTION_SEQUENCE_PARTITIONER_COMMAND_IS_READY) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
        printf("DEBUG controller receives ACTION_SEQUENCE_PARTITIONER_COMMAND_IS_READY, asks for command\n");
#endif

        thorium_actor_send_reply_empty(actor, ACTION_SEQUENCE_PARTITIONER_GET_COMMAND);

    } else if (tag == ACTION_SEQUENCE_PARTITIONER_GET_COMMAND_REPLY) {

        biosal_input_controller_receive_command(actor, message);

    } else if (tag == ACTION_SEQUENCE_PARTITIONER_FINISHED) {

        thorium_actor_send_empty(actor,
                                concrete_actor->partitioner,
                        ACTION_ASK_TO_STOP);

        biosal_input_controller_verify_requests(actor, message);

    } else if (tag == ACTION_SEQUENCE_PARTITIONER_PROVIDE_STORE_ENTRY_COUNTS) {

        biosal_input_controller_receive_store_entry_counts(actor, message);

    } else if (tag == ACTION_RESERVE_REPLY) {

        concrete_actor->ready_consumers++;

        printf("DEBUG marker ACTION_RESERVE_REPLY %d/%d\n",
                        concrete_actor->ready_consumers,
                        (int)core_vector_size(&concrete_actor->consumers));

        if (concrete_actor->ready_consumers == core_vector_size(&concrete_actor->consumers)) {

            concrete_actor->ready_consumers = 0;
            printf("DEBUG all consumers are ready\n");
            thorium_actor_send_empty(actor,
                            concrete_actor->partitioner,
                            ACTION_SEQUENCE_PARTITIONER_PROVIDE_STORE_ENTRY_COUNTS_REPLY);
        }

    } else if (tag == ACTION_INPUT_PUSH_SEQUENCES_READY) {

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
        printf("DEBUG biosal_input_controller_receive received ACTION_INPUT_PUSH_SEQUENCES_READY\n");
#endif

        stream_name = source;

        acquaintance_index = stream_name;
        stream_index = core_vector_index_of(&concrete_actor->reading_streams, &acquaintance_index);
        command_name = *(int *)core_vector_at(&concrete_actor->partition_commands,
                        stream_index);

        thorium_actor_send_int(actor,
                                concrete_actor->partitioner,
                        ACTION_SEQUENCE_PARTITIONER_GET_COMMAND_REPLY_REPLY,
                        command_name);

    } else if (tag == ACTION_INPUT_PUSH_SEQUENCES_REPLY) {

        stream_name = source;

        thorium_message_unpack_int(message, 0, &consumer);

        consumer_index = core_vector_index_of(&concrete_actor->consumers,
                        &consumer);

        bucket_for_requests = (int *)core_vector_at(&concrete_actor->consumer_active_requests, consumer_index);

        (*bucket_for_requests)--;

        biosal_input_controller_verify_requests(actor, message);

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG_CONSUMERS
        printf("DEBUG consumer # %d has %d active requests\n",
                        consumer_index, *bucket_for_requests);
#endif


    } else if (tag == ACTION_SET_CONSUMERS) {

        core_vector_init(&concrete_actor->consumers, 0);
        core_vector_unpack(&concrete_actor->consumers, buffer);

        printf("controller %d receives %d consumers\n",
                        thorium_actor_name(actor),
                        (int)core_vector_size(&concrete_actor->consumers));

        for (i = 0; i < core_vector_size(&concrete_actor->consumers); i++) {
            core_vector_push_back_int(&concrete_actor->consumer_active_requests, 0);
        }

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
        core_vector_print_int(&concrete_actor->consumers);
        printf("\n");
#endif
        thorium_actor_send_reply_empty(actor, ACTION_SET_CONSUMERS_REPLY);

    } else if (tag == ACTION_SET_BLOCK_SIZE) {

        thorium_message_unpack_int(message, 0, &concrete_actor->block_size);
        thorium_actor_send_reply_empty(actor, ACTION_SET_BLOCK_SIZE_REPLY);

    } else if (tag == ACTION_SEQUENCE_STORE_READY) {

        concrete_actor->filled_consumers++;

#ifdef BIOSAL_INPUT_CONTROLLER_DEBUG
        printf("DEBUG ACTION_SEQUENCE_STORE_READY %d/%d\n", concrete_actor->filled_consumers,
                        (int)core_vector_size(&concrete_actor->consumers));
#endif

        if (concrete_actor->filled_consumers == core_vector_size(&concrete_actor->consumers)) {
            concrete_actor->filled_consumers = 0;

            printf("DEBUG: all consumers are filled,  sending ACTION_INPUT_DISTRIBUTE_REPLY\n");

            core_timer_stop(&concrete_actor->input_timer);
            core_timer_stop(&concrete_actor->distribution_timer);

            core_timer_print_with_description(&concrete_actor->distribution_timer,
                            "Load input / Distribute input data");

            core_timer_print_with_description(&concrete_actor->input_timer,
                            "Load input");

            thorium_actor_send_to_supervisor_empty(actor, ACTION_INPUT_DISTRIBUTE_REPLY);
        }
    }
}
Exemplo n.º 3
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);
}
Exemplo n.º 4
0
/* This can only be called from the CONSUMER
 */
int thorium_worker_dequeue_actor(struct thorium_worker *worker, struct thorium_actor **actor)
{
    int value;
    int name;
    struct thorium_actor *other_actor;
    int other_name;
    int operations;
    int status;
    int mailbox_size;

    operations = 4;
    other_actor = NULL;

    /* Move an actor from the ring to the real actor scheduling queue
     */
    while (operations--
                    && core_fast_ring_pop_from_consumer(&worker->actors_to_schedule, &other_actor)) {

#ifdef CORE_DEBUGGER_ENABLE_ASSERT
        if (other_actor == NULL) {
            printf("NULL pointer pulled from ring, operations %d ring size %d\n",
                            operations, core_fast_ring_size_from_consumer(&worker->actors_to_schedule));
        }
#endif

        CORE_DEBUGGER_ASSERT(other_actor != NULL);

        other_name = thorium_actor_name(other_actor);

#ifdef THORIUM_WORKER_DEBUG_SCHEDULER
        printf("ring.DEQUEUE %d\n", other_name);
#endif

        if (core_set_find(&worker->evicted_actors, &other_name)) {

#ifdef THORIUM_WORKER_DEBUG_SCHEDULER
            printf("ALREADY EVICTED\n");
#endif
            continue;
        }

        if (!core_map_get_value(&worker->actors, &other_name, &status)) {
            /* Add the actor to the list of actors.
             * This does nothing if it is already in the list.
             */

            status = STATUS_IDLE;
            core_map_add_value(&worker->actors, &other_name, &status);

            core_map_iterator_destroy(&worker->actor_iterator);
            core_map_iterator_init(&worker->actor_iterator, &worker->actors);
        }

        /* If the actor is not queued, queue it
         */
        if (status == STATUS_IDLE) {
            status = STATUS_QUEUED;

            core_map_update_value(&worker->actors, &other_name, &status);

            thorium_scheduler_enqueue(&worker->scheduler, other_actor);
        } else {

#ifdef THORIUM_WORKER_DEBUG_SCHEDULER
            printf("SCHEDULER %d already scheduled to run, scheduled: %d\n", other_name,
                            (int)core_set_size(&worker->queued_actors));
#endif
        }
    }

    /* Now, dequeue an actor from the real queue.
     * If it has more than 1 message, re-enqueue it
     */
    value = thorium_scheduler_dequeue(&worker->scheduler, actor);

    /* Setting name to nobody;
     * check_production at the end uses the value and the name;
     * if value is false, check_production is not using name anyway.
     */
    name = THORIUM_ACTOR_NOBODY;

    /* an actor is ready to be run and it was dequeued from the scheduling queue.
     */
    if (value) {
        name = thorium_actor_name(*actor);

#ifdef THORIUM_WORKER_DEBUG_SCHEDULER
        printf("scheduler.DEQUEUE actor %d, removed from queued actors...\n", name);
#endif

        mailbox_size = thorium_actor_get_mailbox_size(*actor);

        /* The actor has only one message and it is going to
         * be processed now.
         */
        if (mailbox_size == 1) {
#ifdef THORIUM_WORKER_DEBUG_SCHEDULER
            printf("SCHEDULER %d has no message to schedule...\n", name);
#endif
            /* Set the status of the worker to STATUS_IDLE
             *
             * TODO: the ring new tail might not be visible too.
             * That could possibly be a problem...
             */
            status = STATUS_IDLE;
            core_map_update_value(&worker->actors, &name, &status);

        /* The actor still has a lot of messages
         * to process. Keep them coming.
         */
        } else if (mailbox_size >= 2) {

            /* Add the actor to the scheduling queue if it
             * still has messages
             */

#ifdef THORIUM_WORKER_DEBUG_SCHEDULER
            printf("Scheduling actor %d again, messages: %d\n",
                        name,
                        thorium_actor_get_mailbox_size(*actor));
#endif

            /* The status is still STATUS_QUEUED
             */
            thorium_scheduler_enqueue(&worker->scheduler, *actor);


        /* The actor is scheduled to run, but the new tail is not
         * yet visible apparently.
         *
         * Solution, push back the actor in the scheduler queue, it can take a few cycles to see cache changes across cores. (MESIF protocol)
         *
         * This is done below.
         */
        } else /* if (mailbox_size == 0) */ {

            status = STATUS_IDLE;
            core_map_update_value(&worker->actors, &name, &status);

            value = 0;
        }

    }

    thorium_worker_check_production(worker, value, name);

    return value;
}
Exemplo n.º 5
0
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, &current_worker)) {
                    core_map_add_value(&script_current_worker, &script, &current_worker);
                }
                current_worker_actor_count = 0;
                if (!core_map_get_value(&script_current_worker_actor_count, &script, &current_worker_actor_count)) {
                    core_map_add_value(&script_current_worker_actor_count, &script, &current_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, &current_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, &current_worker);
                    current_worker_actor_count = 0;
                    core_map_update_value(&script_current_worker_actor_count, &script, &current_worker_actor_count);
                }
            }

        }

        core_map_iterator_destroy(&iterator);
    }

    core_map_destroy(&script_current_worker);
    core_map_destroy(&script_current_worker_actor_count);
}
Exemplo n.º 6
0
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);
}