Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Mallocator on the message contextes
[simgrid.git] / src / gras / Msg / msg.c
index aa45bad..a041dba 100644 (file)
@@ -8,6 +8,7 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "xbt/ex.h"
+#include "xbt/ex_interface.h"
 #include "gras/Msg/msg_private.h"
 #include "gras/Virtu/virtu_interface.h"
 #include "gras/DataDesc/datadesc_interface.h"
@@ -78,6 +79,12 @@ void gras_msg_init(void) {
   memcpy(_GRAS_header,"GRAS", 4);
   _GRAS_header[4]=GRAS_PROTOCOL_VERSION;
   _GRAS_header[5]=(char)GRAS_THISARCH;
+   
+  gras_msg_ctx_mallocator = 
+     xbt_mallocator_new(1000,
+                       gras_msg_ctx_mallocator_new_f,
+                       gras_msg_ctx_mallocator_free_f,
+                       gras_msg_ctx_mallocator_reset_f);
 }
 
 /*
@@ -87,6 +94,8 @@ void
 gras_msg_exit(void) {
   VERB0("Exiting Msg");
   xbt_set_free(&_gras_msgtype_set);
+
+  xbt_mallocator_free(gras_msg_ctx_mallocator);
 }
 
 /*
@@ -99,6 +108,23 @@ void gras_msgtype_free(void *t) {
     free(msgtype);
   }
 }
+/**
+ * Dump all declared message types (debugging purpose)
+ */
+void gras_msgtype_dumpall(void) {   
+  xbt_set_cursor_t cursor;
+  gras_msgtype_t msgtype=NULL;
+   
+  INFO0("Dump of all registered messages:");
+  xbt_set_foreach(_gras_msgtype_set, cursor, msgtype) {
+    INFO6("  Message name: %s (v%d) %s; %s%s%s", 
+         msgtype->name, msgtype->version, e_gras_msg_kind_names[msgtype->kind],
+         gras_datadesc_get_name(msgtype->ctn_type),
+         (msgtype->kind==e_gras_msg_kind_rpccall ? " -> ":""),
+         (msgtype->kind==e_gras_msg_kind_rpccall ? gras_datadesc_get_name(msgtype->answer_type) : ""));
+  }   
+}
+
 
 /**
  * make_namev:
@@ -213,30 +239,47 @@ gras_msgtype_declare_v(const char           *name,
                            e_gras_msg_kind_oneway, payload, NULL);
 }
 
-/** @brief retrive an existing message type from its name. */
+/** @brief retrieve an existing message type from its name (raises an exception if it does not exist). */
 gras_msgtype_t gras_msgtype_by_name (const char *name) {
   return gras_msgtype_by_namev(name,0);
 }
+/** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
+gras_msgtype_t gras_msgtype_by_name_or_null (const char *name) {
+  xbt_ex_t e;
+  gras_msgtype_t res = NULL;
+   
+  TRY {
+     res = gras_msgtype_by_namev(name,0);
+  } CATCH(e) {
+     res = NULL;
+     xbt_ex_free(e);
+  }
+  return res;
+}
 
-/** @brief retrive an existing message type from its name and version. */
+/** @brief retrieve an existing message type from its name and version. */
 gras_msgtype_t gras_msgtype_by_namev(const char      *name,
                                     short int        version) {
   gras_msgtype_t res = NULL;
-  char *namev = make_namev(name,version); 
+  char *namev = make_namev(name,version);
+  volatile int found=0;
   xbt_ex_t e;
 
   TRY {
     res = (gras_msgtype_t)xbt_set_get_by_name(_gras_msgtype_set, namev);
+    found=1;
   } CATCH(e) {
     xbt_ex_free(e);
-    THROW1(not_found_error,0,"No registred message of that name: %s",name);
   }
+  if (!found)
+    THROW1(not_found_error,0,"No registred message of that name: %s",name);
+
   if (name != namev) 
     free(namev);
   
   return res;
 }
-/** @brief retrive an existing message type from its name and version. */
+/** @brief retrieve an existing message type from its name and version. */
 gras_msgtype_t gras_msgtype_by_id(int id) {
   return (gras_msgtype_t)xbt_set_get_by_id(_gras_msgtype_set, id);
 }
@@ -246,11 +289,9 @@ gras_msgtype_t gras_msgtype_by_id(int id) {
  * @param timeout: How long should we wait for this message.
  * @param msgt_want: type of awaited msg (or NULL if I'm enclined to accept any message)
  * @param expe_want: awaited expeditot (match on hostname, not port; NULL if not relevant)
- * @param payl_filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant)
+ * @param filter: function returning true or false when passed a payload. Messages for which it returns false are not selected. (NULL if not relevant)
  * @param filter_ctx: context passed as second argument of the filter (a pattern to match?)
- * @param[out] msgt_got: where to write the descriptor of the message we got
- * @param[out] expe_got: where to create a socket to answer the incomming message
- * @param[out] payl_got: where to write the payload of the incomming message
+ * @param[out] msg_got: where to write the message we got
  *
  * Every message of another type received before the one waited will be queued
  * and used by subsequent call to this function or gras_msg_handle().
@@ -271,12 +312,10 @@ gras_msg_wait_ext(double           timeout,
   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
   int cpt;
 
-  xbt_assert0(msgt_want,"Cannot wait for the NULL message");
   xbt_assert0(msg_got,"msg_got is an output parameter");
 
-  VERB1("Waiting for message '%s'",msgt_want->name);
-
-  start = now = gras_os_time();
+  start = gras_os_time();
+  VERB1("Waiting for message '%s'",msgt_want?msgt_want->name:"(any)");
 
   xbt_dynar_foreach(pd->msg_queue,cpt,msg){
     if ( (   !msgt_want || (msg.type->code == msgt_want->code)) 
@@ -292,11 +331,31 @@ gras_msg_wait_ext(double           timeout,
   }
 
   while (1) {
+    int need_restart;
+    xbt_ex_t e;
+
+  restart_receive: /* Goto here when the receive of a message failed */
+    need_restart=0;
+    now=gras_os_time();
     memset(&msg,sizeof(msg),0);
 
-    msg.expe = gras_trp_select(timeout - now + start);
-    gras_msg_recv(msg.expe, &msg);
-    DEBUG0("Here");
+    TRY {
+      msg.expe = gras_trp_select(timeout ? timeout - now + start : 0);
+      gras_msg_recv(msg.expe, &msg);
+    } CATCH(e) {
+      if (e.category == system_error &&
+         !strncmp("Socket closed by remote side",e.msg,
+                 strlen("Socket closed by remote side"))) {
+       xbt_ex_free(e);
+       need_restart=1;
+      }        else {
+       RETHROW;
+      }
+    }
+    if (need_restart)
+      goto restart_receive;
+
+    DEBUG0("Got a message from the socket");
 
     if ( (   !msgt_want || (msg.type->code == msgt_want->code)) 
         && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
@@ -304,16 +363,19 @@ gras_msg_wait_ext(double           timeout,
         && (!filter || filter(&msg,filter_ctx))) {
 
       memcpy(msg_got,&msg,sizeof(s_gras_msg_t));
+      DEBUG0("Message matches expectations. Use it.");
       return;
     }
+    DEBUG0("Message does not match expectations. Queue it.");
 
     /* not expected msg type. Queue it for later */
     xbt_dynar_push(pd->msg_queue,&msg);
     
     now=gras_os_time();
-    if (now - start + 0.001 < timeout) {
+    if (now - start + 0.001 > timeout) {
       THROW1(timeout_error,  now-start+0.001-timeout,
-            "Timeout while waiting for msg %s",msgt_want->name);
+            "Timeout while waiting for msg '%s'",
+            msgt_want?msgt_want->name:"(any)");
     }
   }
 
@@ -340,9 +402,80 @@ gras_msg_wait(double           timeout,
   gras_msg_wait_ext(timeout,
                    msgt_want, NULL,      NULL, NULL,
                    &msg);
-  memcpy(payload,msg.payl,msg.payl_size);
-  free(msg.payl);
-  *expeditor = msg.expe;
+
+  if (msgt_want->ctn_type) {
+    xbt_assert1(payload,
+               "Message type '%s' convey a payload you must accept",
+               msgt_want->name);
+  } else {
+    xbt_assert1(!payload,
+               "No payload was declared for message type '%s'",
+               msgt_want->name);
+  }
+
+  if (payload) {
+    memcpy(payload,msg.payl,msg.payl_size);
+    free(msg.payl);
+  }
+
+  if (expeditor)
+    *expeditor = msg.expe;
+}
+
+static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx) {
+  xbt_dynar_t dyn=(xbt_dynar_t)ctx;
+  int res =  xbt_dynar_member(dyn,msg->type);
+  if (res)
+    VERB1("Got matching message (type=%s)",msg->type->name);
+  else
+    VERB0("Got message not matching our expectations");
+  return res;
+}
+/** \brief Waits for a message to come in over a given socket. 
+ *
+ * @param timeout: How long should we wait for this message.
+ * @param msgt_want: a dynar containing all accepted message type
+ * @param[out] ctx: the context of received message (in case it's a RPC call we want to answer to)
+ * @param[out] msgt_got: indice in the dynar of the type of the received message 
+ * @param[out] payload: where to write the payload of the incomming message
+ * @return the error code (or no_error).
+ *
+ * Every message of a type not in the accepted list received before the one
+ * waited will be queued and used by subsequent call to this function or
+ * gras_msg_handle().
+ *
+ * If you are interested in the context, pass the address of a s_gras_msg_cb_ctx_t variable.
+ */
+void gras_msg_wait_or(double         timeout,
+                      xbt_dynar_t    msgt_want,
+                     gras_msg_cb_ctx_t *ctx,
+                      int           *msgt_got,
+                      void          *payload) {
+  s_gras_msg_t msg;
+
+  VERB1("Wait %f seconds for several message types",timeout);
+  gras_msg_wait_ext(timeout,
+                   NULL, NULL,      
+                   &gras_msg_wait_or_filter, (void*)msgt_want,
+                   &msg);
+
+  if (msg.type->ctn_type) {
+    xbt_assert1(payload,
+               "Message type '%s' convey a payload you must accept",
+               msg.type->name);
+  } /* don't check the other side since some of the types may have a payload */
+
+  if (payload && msg.type->ctn_type) {
+    memcpy(payload,msg.payl,msg.payl_size);
+    free(msg.payl);
+  }
+
+  if (ctx) 
+    *ctx=gras_msg_cb_ctx_new(msg.expe, msg.type, msg.ID,
+                            (msg.kind == e_gras_msg_kind_rpccall), 60);
+
+  if (msgt_got)
+    *msgt_got = xbt_dynar_search(msgt_want,msg.type);
 }
 
 
@@ -353,7 +486,49 @@ gras_msg_send(gras_socket_t   sock,
              gras_msgtype_t  msgtype,
              void           *payload) {
 
+  if (msgtype->ctn_type) {
+    xbt_assert1(payload,
+               "Message type '%s' convey a payload you must provide",
+               msgtype->name);
+  } else {
+    xbt_assert1(!payload,
+               "No payload was declared for message type '%s'",
+               msgtype->name);
+  }
+
+  DEBUG2("Send a oneway message of type '%s'. Payload=%p",
+        msgtype->name,payload);
   gras_msg_send_ext(sock, e_gras_msg_kind_oneway,0, msgtype, payload);
+  VERB2("Sent a oneway message of type '%s'. Payload=%p",
+       msgtype->name,payload);
+}
+
+/** @brief Handle all messages arriving within the given period
+ *
+ * @param period: How long to wait for incoming messages (in seconds)
+ *
+ * Messages are dealed with just like gras_msg_handle() would do. The
+ * difference is that gras_msg_handle() handles at most one message (or wait up
+ * to timeout second when no message arrives) while this function handles any
+ * amount of messages, and lasts the given period in any case.
+ */
+void 
+gras_msg_handleall(double period) {
+  xbt_ex_t e;
+  double begin=gras_os_time();
+  double now;
+
+  do {
+    now=gras_os_time();
+    TRY{
+      if (period - now + begin > 0)
+       gras_msg_handle(period - now + begin);
+    } CATCH(e) {
+      if (e.category != timeout_error) 
+       RETHROW0("Error while waiting for messages: %s");
+      xbt_ex_free(e);
+    }
+  } while (now - begin < period);
 }
 
 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
@@ -361,7 +536,7 @@ gras_msg_send(gras_socket_t   sock,
  * @param timeOut: How long to wait for incoming messages (in seconds)
  * @return the error code (or no_error).
  *
- * Messages are passed to the callbacks.
+ * Messages are passed to the callbacks. See also gras_msg_handleall().
  */
 void
 gras_msg_handle(double timeOut) {
@@ -369,6 +544,7 @@ gras_msg_handle(double timeOut) {
   double          untiltimer;
    
   int             cpt;
+  int volatile ran_ok;
 
   s_gras_msg_t    msg;
 
@@ -383,7 +559,7 @@ gras_msg_handle(double timeOut) {
   VERB1("Handling message within the next %.2fs",timeOut);
   
   untiltimer = gras_msg_timer_handle();
-  DEBUG2("[%.0f] Next timer in %f sec", gras_os_time(), untiltimer);
+  DEBUG1("Next timer in %f sec", untiltimer);
   if (untiltimer == 0.0) {
      /* A timer was already elapsed and handled */
      return;
@@ -414,11 +590,14 @@ gras_msg_handle(double timeOut) {
       TRY {
        /* FIXME: if not the right kind, queue it and recall ourself or goto >:-) */
        gras_msg_recv(msg.expe, &msg);
-       DEBUG0("Here");
+       DEBUG1("Received a msg from the socket kind:%s",
+              e_gras_msg_kind_names[msg.kind]);
     
       } CATCH(e) {
-       RETHROW1("Error caught while receiving a message on select()ed socket %p: %s",
-                msg.expe);
+       RETHROW4("Error while receiving a message on select()ed socket %p to [%s]%s:%d: %s",
+                msg.expe,
+                gras_socket_peer_proc(msg.expe),gras_socket_peer_name(msg.expe),
+                gras_socket_peer_port(msg.expe));
       }
     }
   }
@@ -442,7 +621,8 @@ gras_msg_handle(double timeOut) {
        
      } else {
        /* select timeouted, and no timer elapsed. Nothing to do */
-       THROW0(timeout_error, 0, "No new message or timer");
+       THROW1(timeout_error, 0, "No new message or timer (delay was %f)",
+             timeOut);
      }
      
   }
@@ -456,8 +636,9 @@ gras_msg_handle(double timeOut) {
     }
   }
   if (!list) {
-    INFO1("No callback for the incomming '%s' message. Discarded.", 
-         msg.type->name);
+    INFO3("No callback for the incomming '%s' message (from %s:%d). Discarded.", 
+         msg.type->name,
+         gras_socket_peer_name(msg.expe),gras_socket_peer_port(msg.expe));
     WARN0("FIXME: gras_datadesc_free not implemented => leaking the payload");
     return;
   }
@@ -465,39 +646,54 @@ gras_msg_handle(double timeOut) {
   ctx.expeditor = msg.expe;
   ctx.ID = msg.ID;
   ctx.msgtype = msg.type;
+  ctx.answer_due = (msg.kind == e_gras_msg_kind_rpccall);
 
-  switch (msg.type->kind) {
+  switch (msg.kind) {
   case e_gras_msg_kind_oneway:
   case e_gras_msg_kind_rpccall:
+    ran_ok=0;
     TRY {
       xbt_dynar_foreach(list->cbs,cpt,cb) { 
-       VERB3("Use the callback #%d (@%p) for incomming msg %s",
-             cpt+1,cb,msg.type->name);
-       if ((*cb)(&ctx,msg.payl)) {
-         /* cb handled the message */
-         free(msg.payl);
-         return;
+       if (!ran_ok) {
+         DEBUG4("Use the callback #%d (@%p) for incomming msg %s (payload_size=%d)",
+               cpt+1,cb,msg.type->name,msg.payl_size);
+         if ((*cb)(&ctx,msg.payl)) {
+           /* cb handled the message */
+           free(msg.payl);
+           ran_ok = 1;
+         }
        }
       }
     } CATCH(e) {
+      free(msg.payl);
       if (msg.type->kind == e_gras_msg_kind_rpccall) {
        /* The callback raised an exception, propagate it on the network */
-       e.host = (char*)gras_os_myname();
-#ifdef HAVE_EXECINFO_H
-       e.bt_strings = backtrace_symbols (e.bt, e.used);
-#endif
-       gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror , msg.ID, msg.type, &e);
-       e.host = NULL;
-       INFO2("RPC callback raised an exception, which were propagated back to %s:%d",
-             gras_socket_peer_name(msg.expe),  gras_socket_peer_port(msg.expe));
+       if (!e.remote) { /* the exception is born on this machine */
+         e.host = (char*)gras_os_myname();
+         xbt_ex_setup_backtrace(&e);
+       } 
+       VERB5("Propagate %s exception ('%s') from '%s' RPC cb back to %s:%d",
+             (e.remote ? "remote" : "local"),
+             e.msg,
+             msg.type->name,
+             gras_socket_peer_name(msg.expe),
+             gras_socket_peer_port(msg.expe));
+       gras_msg_send_ext(msg.expe, e_gras_msg_kind_rpcerror,
+                         msg.ID, msg.type, &e);
        xbt_ex_free(e);
-       return;
+       ctx.answer_due = 0;
+       ran_ok=1;
+      } else {
+       RETHROW0("Callback raised an exception: %s");
       }
-      RETHROW;
     }
+    xbt_assert0(!(ctx.answer_due),
+               "RPC callback didn't call gras_msg_rpcreturn");
+
+    if (!ran_ok)
+      THROW1(mismatch_error,0,
+            "Message '%s' refused by all registered callbacks", msg.type->name);
     /* FIXME: gras_datadesc_free not implemented => leaking the payload */
-    THROW1(mismatch_error,0,
-          "Message '%s' refused by all registered callbacks", msg.type->name);
     break;
 
 
@@ -513,7 +709,7 @@ gras_msg_handle(double timeOut) {
 
   default:
     THROW1(unknown_error,0,
-          "Cannot handle messages of kind %d yet",msg.type->kind);
+          "Cannot handle messages of kind %d yet",msg.type->kind);
   }
 
 }
@@ -600,3 +796,25 @@ gras_cb_unregister(gras_msgtype_t msgtype,
 gras_socket_t gras_msg_cb_ctx_from(gras_msg_cb_ctx_t ctx) {
   return ctx->expeditor;
 }
+/* \brief Creates a new message exchange context (user should never have to) */
+gras_msg_cb_ctx_t gras_msg_cb_ctx_new(gras_socket_t expe, 
+                                     gras_msgtype_t msgtype,
+                                     unsigned long int ID,
+                                     int answer_due,
+                                     double timeout) {
+  gras_msg_cb_ctx_t res=xbt_new(s_gras_msg_cb_ctx_t,1);
+  res->expeditor = expe;
+  res->msgtype = msgtype;
+  res->ID = ID;
+  res->timeout = timeout;
+  res->answer_due = answer_due;
+
+  return res;
+}
+/* \brief Frees a message exchange context 
+ *
+ * This function is mainly useful with \ref gras_msg_wait_or, ie seldom.
+ */
+void gras_msg_cb_ctx_free(gras_msg_cb_ctx_t ctx) {
+  free(ctx);
+}