X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/8840f8ed2dc9084b3c7a041105ffa8d633178b5a..50c46c12da0939b796b69af632cde52a8513338a:/src/simix/smx_network.cpp diff --git a/src/simix/smx_network.cpp b/src/simix/smx_network.cpp index dd209ff180..a8a5866f1a 100644 --- a/src/simix/smx_network.cpp +++ b/src/simix/smx_network.cpp @@ -1,5 +1,4 @@ -/* Copyright (c) 2009-2015. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -10,31 +9,23 @@ #include "mc/mc.h" #include "src/mc/mc_replay.h" #include "xbt/dict.h" +#include "simgrid/s4u/mailbox.hpp" XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix, "SIMIX network-related synchronization"); -static xbt_dict_t mailboxes = NULL; -XBT_EXPORT_NO_IMPORT(unsigned long int) smx_total_comms = 0; +static void SIMIX_mbox_free(void *data); +static xbt_dict_t mailboxes = xbt_dict_new_homogeneous(SIMIX_mbox_free); static void SIMIX_waitany_remove_simcall_from_actions(smx_simcall_t simcall); static void SIMIX_comm_copy_data(smx_synchro_t comm); static smx_synchro_t SIMIX_comm_new(e_smx_comm_type_t type); static inline void SIMIX_mbox_push(smx_mailbox_t mbox, smx_synchro_t comm); -static smx_synchro_t SIMIX_fifo_probe_comm(xbt_fifo_t fifo, e_smx_comm_type_t type, - int (*match_fun)(void *, void *,smx_synchro_t), - void *user_data, smx_synchro_t my_synchro); -static smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type, +static smx_synchro_t _find_matching_comm(std::deque *deque, e_smx_comm_type_t type, int (*match_fun)(void *, void *,smx_synchro_t), - void *user_data, smx_synchro_t my_synchro); -static void SIMIX_mbox_free(void *data); + void *user_data, smx_synchro_t my_synchro,int extract); static void SIMIX_comm_start(smx_synchro_t synchro); -void SIMIX_network_init(void) -{ - mailboxes = xbt_dict_new_homogeneous(SIMIX_mbox_free); -} - -void SIMIX_network_exit(void) +void SIMIX_mailbox_exit(void) { xbt_dict_free(&mailboxes); } @@ -45,37 +36,30 @@ void SIMIX_network_exit(void) smx_mailbox_t SIMIX_mbox_create(const char *name) { + xbt_assert(name, "Mailboxes must have a name"); /* two processes may have pushed the same mbox_create simcall at the same time */ - smx_mailbox_t mbox = name ? (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name) : NULL; + smx_mailbox_t mbox = (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name); if (!mbox) { mbox = xbt_new0(s_smx_mailbox_t, 1); - mbox->name = name ? xbt_strdup(name) : NULL; - mbox->comm_fifo = xbt_fifo_new(); - mbox->done_comm_fifo = xbt_fifo_new(); + mbox->name = xbt_strdup(name); + mbox->comm_queue = new std::deque(); + mbox->done_comm_queue = nullptr; // Allocated on need only mbox->permanent_receiver=NULL; XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name); - - if (mbox->name) - xbt_dict_set(mailboxes, mbox->name, mbox, NULL); + xbt_dict_set(mailboxes, mbox->name, mbox, NULL); } return mbox; } -void SIMIX_mbox_destroy(smx_mailbox_t mbox) -{ - if (mbox->name) - xbt_dict_remove(mailboxes, mbox->name); -} - void SIMIX_mbox_free(void *data) { XBT_DEBUG("mbox free %p", data); smx_mailbox_t mbox = (smx_mailbox_t) data; xbt_free(mbox->name); - xbt_fifo_free(mbox->comm_fifo); - xbt_fifo_free(mbox->done_comm_fifo); + delete mbox->comm_queue; + delete mbox->done_comm_queue; xbt_free(mbox); } @@ -85,24 +69,9 @@ smx_mailbox_t SIMIX_mbox_get_by_name(const char *name) return (smx_mailbox_t) xbt_dict_get_or_null(mailboxes, name); } -int SIMIX_mbox_comm_count_by_host(smx_mailbox_t mbox, sg_host_t host) -{ - smx_synchro_t comm = NULL; - xbt_fifo_item_t item = NULL; - int count = 0; - - xbt_fifo_foreach(mbox->comm_fifo, item, comm, smx_synchro_t) { - if (comm->comm.src_proc->host == host) - count++; - } - - return count; -} - smx_synchro_t SIMIX_mbox_get_head(smx_mailbox_t mbox) { - return (smx_synchro_t) xbt_fifo_get_item_content( - xbt_fifo_get_first_item(mbox->comm_fifo)); + return mbox->comm_queue->front(); } /** @@ -123,6 +92,8 @@ smx_process_t SIMIX_mbox_get_receiver(smx_mailbox_t mbox) void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_process_t process) { mbox->permanent_receiver=process; + if (mbox->done_comm_queue == nullptr) + mbox->done_comm_queue = new std::deque(); } /** @@ -132,7 +103,7 @@ void SIMIX_mbox_set_receiver(smx_mailbox_t mbox, smx_process_t process) */ static inline void SIMIX_mbox_push(smx_mailbox_t mbox, smx_synchro_t comm) { - xbt_fifo_push(mbox->comm_fifo, comm); + mbox->comm_queue->push_back(comm); comm->comm.mbox = mbox; } @@ -143,35 +114,37 @@ static inline void SIMIX_mbox_push(smx_mailbox_t mbox, smx_synchro_t comm) */ void SIMIX_mbox_remove(smx_mailbox_t mbox, smx_synchro_t comm) { - xbt_fifo_remove(mbox->comm_fifo, comm); comm->comm.mbox = NULL; + for (auto it = mbox->comm_queue->begin(); it != mbox->comm_queue->end(); it++) + if (*it == comm) { + mbox->comm_queue->erase(it); + return; + } + xbt_die("Cannot remove this comm that is not part of the mailbox"); } /** - * \brief Checks if there is a communication synchro queued in a fifo matching our needs + * \brief Checks if there is a communication synchro queued in a deque matching our needs * \param type The type of communication we are looking for (comm_send, comm_recv) * \return The communication synchro if found, NULL otherwise */ -smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type, - int (*match_fun)(void *, void *,smx_synchro_t), - void *this_user_data, smx_synchro_t my_synchro) +static smx_synchro_t _find_matching_comm(std::deque *deque, e_smx_comm_type_t type, + int (*match_fun)(void *, void *,smx_synchro_t), void *this_user_data, smx_synchro_t my_synchro, int extract) { - smx_synchro_t synchro; - xbt_fifo_item_t item; void* other_user_data = NULL; - xbt_fifo_foreach(fifo, item, synchro, smx_synchro_t) { + for(auto it = deque->begin(); it != deque->end(); it++){ + smx_synchro_t synchro = *it; if (synchro->comm.type == SIMIX_COMM_SEND) { other_user_data = synchro->comm.src_data; } else if (synchro->comm.type == SIMIX_COMM_RECEIVE) { other_user_data = synchro->comm.dst_data; } if (synchro->comm.type == type && - (!match_fun || match_fun(this_user_data, other_user_data, synchro)) && + (!match_fun || match_fun(this_user_data, other_user_data, synchro)) && (!synchro->comm.match_fun || synchro->comm.match_fun(other_user_data, this_user_data, my_synchro))) { XBT_DEBUG("Found a matching communication synchro %p", synchro); - xbt_fifo_remove_item(fifo, item); - xbt_fifo_free_item(item); + if(extract!=0) deque->erase(it); synchro->comm.refcount++; #if HAVE_MC synchro->comm.mbox_cpy = synchro->comm.mbox; @@ -187,41 +160,6 @@ smx_synchro_t SIMIX_fifo_get_comm(xbt_fifo_t fifo, e_smx_comm_type_t type, return NULL; } - -/** - * \brief Checks if there is a communication synchro queued in a fifo matching our needs, but leave it there - * \param type The type of communication we are looking for (comm_send, comm_recv) - * \return The communication synchro if found, NULL otherwise - */ -smx_synchro_t SIMIX_fifo_probe_comm(xbt_fifo_t fifo, e_smx_comm_type_t type, - int (*match_fun)(void *, void *,smx_synchro_t), - void *this_user_data, smx_synchro_t my_synchro) -{ - smx_synchro_t synchro; - xbt_fifo_item_t item; - void* other_user_data = NULL; - - xbt_fifo_foreach(fifo, item, synchro, smx_synchro_t) { - if (synchro->comm.type == SIMIX_COMM_SEND) { - other_user_data = synchro->comm.src_data; - } else if (synchro->comm.type == SIMIX_COMM_RECEIVE) { - other_user_data = synchro->comm.dst_data; - } - if (synchro->comm.type == type && - (!match_fun || match_fun(this_user_data, other_user_data, synchro)) && - (!synchro->comm.match_fun || synchro->comm.match_fun(other_user_data, this_user_data, my_synchro))) { - XBT_DEBUG("Found a matching communication synchro %p", synchro); - synchro->comm.refcount++; - - return synchro; - } - XBT_DEBUG("Sorry, communication synchro %p does not match our needs:" - " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)", - synchro, (int)synchro->comm.type, (int)type); - } - XBT_DEBUG("No matching communication synchro found"); - return NULL; -} /******************************************************************************/ /* Communication synchros */ /******************************************************************************/ @@ -250,7 +188,6 @@ smx_synchro_t SIMIX_comm_new(e_smx_comm_type_t type) synchro->category = NULL; XBT_DEBUG("Create communicate synchro %p", synchro); - ++smx_total_comms; return synchro; } @@ -340,7 +277,7 @@ smx_synchro_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_process_t sr * ourself so that the other side also gets a chance of choosing if it wants to match with us. * * If it is not found then push our communication into the rendez-vous point */ - smx_synchro_t other_synchro = SIMIX_fifo_get_comm(mbox->comm_fifo, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro); + smx_synchro_t other_synchro = _find_matching_comm(mbox->comm_queue, SIMIX_COMM_RECEIVE, match_fun, data, this_synchro,1); if (!other_synchro) { other_synchro = this_synchro; @@ -350,7 +287,7 @@ smx_synchro_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_process_t sr other_synchro->state = SIMIX_READY; other_synchro->comm.dst_proc=mbox->permanent_receiver; other_synchro->comm.refcount++; - xbt_fifo_push(mbox->done_comm_fifo,other_synchro); + mbox->done_comm_queue->push_back(other_synchro); other_synchro->comm.mbox=mbox; XBT_DEBUG("pushing a message into the permanent receive fifo %p, comm %p", mbox, &(other_synchro->comm)); @@ -361,7 +298,6 @@ smx_synchro_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx_process_t sr XBT_DEBUG("Receive already pushed"); SIMIX_comm_destroy(this_synchro); - --smx_total_comms; // this creation was a pure waste other_synchro->state = SIMIX_READY; other_synchro->comm.type = SIMIX_COMM_READY; @@ -426,16 +362,16 @@ smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t mbox, void void (*copy_data_fun)(smx_synchro_t, void*, size_t), // used to copy data if not default one void *data, double rate) { - XBT_DEBUG("recv from %p %p", mbox, mbox->comm_fifo); + XBT_DEBUG("recv from %p %p", mbox, mbox->comm_queue); smx_synchro_t this_synchro = SIMIX_comm_new(SIMIX_COMM_RECEIVE); smx_synchro_t other_synchro; //communication already done, get it inside the fifo of completed comms - if (mbox->permanent_receiver && xbt_fifo_size(mbox->done_comm_fifo)!=0) { + if (mbox->permanent_receiver && ! mbox->done_comm_queue->empty()) { XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication"); //find a match in the already received fifo - other_synchro = SIMIX_fifo_get_comm(mbox->done_comm_fifo, SIMIX_COMM_SEND, match_fun, data, this_synchro); + other_synchro = _find_matching_comm(mbox->done_comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,1); //if not found, assume the receiver came first, register it to the mailbox in the classical way if (!other_synchro) { XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into fifo"); @@ -450,7 +386,6 @@ smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t mbox, void } other_synchro->comm.refcount--; SIMIX_comm_destroy(this_synchro); - --smx_total_comms; // this creation was a pure waste } } else { /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */ @@ -459,15 +394,14 @@ smx_synchro_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_mailbox_t mbox, void * ourself so that the other side also gets a chance of choosing if it wants to match with us. * * If it is not found then push our communication into the rendez-vous point */ - other_synchro = SIMIX_fifo_get_comm(mbox->comm_fifo, SIMIX_COMM_SEND, match_fun, data, this_synchro); + other_synchro = _find_matching_comm(mbox->comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,1); if (!other_synchro) { - XBT_DEBUG("Receive pushed first %d", xbt_fifo_size(mbox->comm_fifo)); + XBT_DEBUG("Receive pushed first %zu", mbox->comm_queue->size()); other_synchro = this_synchro; SIMIX_mbox_push(mbox, this_synchro); } else { SIMIX_comm_destroy(this_synchro); - --smx_total_comms; // this creation was a pure waste other_synchro->state = SIMIX_READY; other_synchro->comm.type = SIMIX_COMM_READY; //other_synchro->comm.refcount--; @@ -506,7 +440,7 @@ smx_synchro_t simcall_HANDLER_comm_iprobe(smx_simcall_t simcall, smx_mailbox_t m smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_mailbox_t mbox, int type, int src, int tag, int (*match_fun)(void *, void *, smx_synchro_t), void *data) { - XBT_DEBUG("iprobe from %p %p", mbox, mbox->comm_fifo); + XBT_DEBUG("iprobe from %p %p", mbox, mbox->comm_queue); smx_synchro_t this_synchro; int smx_type; if(type == 1){ @@ -517,26 +451,18 @@ smx_synchro_t SIMIX_comm_iprobe(smx_process_t dst_proc, smx_mailbox_t mbox, int smx_type = SIMIX_COMM_SEND; } smx_synchro_t other_synchro=NULL; - if(mbox->permanent_receiver && xbt_fifo_size(mbox->done_comm_fifo)!=0){ - //find a match in the already received fifo - XBT_DEBUG("first try in the perm recv mailbox"); - - other_synchro = SIMIX_fifo_probe_comm( - mbox->done_comm_fifo, (e_smx_comm_type_t) smx_type, - match_fun, data, this_synchro); + if(mbox->permanent_receiver && ! mbox->done_comm_queue->empty()){ + XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something"); + other_synchro = _find_matching_comm(mbox->done_comm_queue, (e_smx_comm_type_t) smx_type, match_fun, data, this_synchro,0); } - // }else{ - if(!other_synchro){ - XBT_DEBUG("try in the normal mailbox"); - other_synchro = SIMIX_fifo_probe_comm( - mbox->comm_fifo, (e_smx_comm_type_t) smx_type, - match_fun, data, this_synchro); - } -// } - if(other_synchro)other_synchro->comm.refcount--; + if (!other_synchro){ + XBT_DEBUG("check if we have more luck in the normal mailbox"); + other_synchro = _find_matching_comm(mbox->comm_queue, (e_smx_comm_type_t) smx_type, match_fun, data, this_synchro,0); + } + if(other_synchro) + other_synchro->comm.refcount--; SIMIX_comm_destroy(this_synchro); - --smx_total_comms; return other_synchro; } @@ -941,12 +867,10 @@ void SIMIX_comm_resume(smx_synchro_t synchro) */ double SIMIX_comm_get_remains(smx_synchro_t synchro) { - double remains; - - if(!synchro){ + if(!synchro) return 0; - } + double remains; switch (synchro->state) { case SIMIX_RUNNING: @@ -1014,8 +938,7 @@ SIMIX_comm_set_copy_data_callback(void (*callback) (smx_synchro_t, void*, size_t void SIMIX_comm_copy_pointer_callback(smx_synchro_t comm, void* buff, size_t buff_size) { - xbt_assert((buff_size == sizeof(void *)), - "Cannot copy %zu bytes: must be sizeof(void*)", buff_size); + xbt_assert((buff_size == sizeof(void *)), "Cannot copy %zu bytes: must be sizeof(void*)", buff_size); *(void **) (comm->comm.dst_buff) = buff; }