Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / gras / Virtu / sg_process.c
index 018225f..743734c 100644 (file)
-/* $Id$ */
-
 /* process_sg - GRAS process handling on simulator                          */
 
-/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
+/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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. */
 
-#include "gras_modinter.h" /* module initialization interface */
+#include "xbt/ex.h"
+#include "xbt/dict.h"
+#include "gras_modinter.h"      /* module initialization interface */
 #include "gras/Virtu/virtu_sg.h"
-#include "gras/Msg/msg_interface.h" /* For some checks at simulation end */
+#include "gras/Msg/msg_interface.h"     /* For some checks at simulation end */
 #include "gras/Transport/transport_interface.h" /* For some checks at simulation end */
+#if HAVE_LUA
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#endif
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
+
+static long int PID = 1;
+
+
+void gras_agent_spawn(const char *name, void *data,
+                      xbt_main_func_t code, int argc, char *argv[],
+                      xbt_dict_t properties)
+{
 
-XBT_LOG_EXTERNAL_CATEGORY(process);
-XBT_LOG_DEFAULT_CATEGORY(process);
+  SIMIX_process_create(name, code,
+                       data, gras_os_myname(), argc, argv, properties);
+}
+
+/* **************************************************************************
+ * Process constructor/destructor (semi-public interface)
+ * **************************************************************************/
 
-xbt_error_t
-gras_process_init() {
-  xbt_error_t errcode;
-  gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
-  gras_procdata_t *pd=xbt_new(gras_procdata_t,1);
+void gras_process_init()
+{
+  gras_hostdata_t *hd =
+      (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
+  gras_procdata_t *pd = xbt_new0(gras_procdata_t, 1);
   gras_trp_procdata_t trp_pd;
-  gras_sg_portrec_t prraw,pr;
-  int i;
-  
-  if (MSG_process_set_data(MSG_process_self(),(void*)pd) != MSG_OK)
-    return unknown_error;
-   
+
+  SIMIX_process_set_data(SIMIX_process_self(), (void *) pd);
+
+
   gras_procdata_init();
 
   if (!hd) {
     /* First process on this host */
-    hd=xbt_new(gras_hostdata_t,1);
-    hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t),NULL);
+    hd = xbt_new(gras_hostdata_t, 1);
+    hd->refcount = 1;
+    hd->ports = xbt_dynar_new(sizeof(gras_sg_portrec_t), NULL);
+    SIMIX_host_set_data(SIMIX_host_self(), (void *) hd);
+  } else {
+    hd->refcount++;
+  }
 
-    memset(hd->proc, 0, sizeof(hd->proc[0]) * XBT_MAX_CHANNEL); 
+  trp_pd = (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
+  pd->pid = PID++;
 
-    if (MSG_host_set_data(MSG_host_self(),(void*)hd) != MSG_OK)
-      return unknown_error;
-  }
-  
-  /* take a free channel for this process */
-  trp_pd = (gras_trp_procdata_t)gras_libdata_get("gras_trp");
-  for (i=0; i<XBT_MAX_CHANNEL && hd->proc[i]; i++);
-  if (i == XBT_MAX_CHANNEL) 
-    RAISE2(system_error,
-          "GRAS: Can't add a new process on %s, because all channel are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS\n.",
-           MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL);
-
-  trp_pd->chan = i;
-  hd->proc[ i ] = MSG_process_self_PID();
-
-  /* regiter it to the ports structure */
-  pr.port = -1;
-  pr.tochan = i;
-  pr.raw = 0;
-  xbt_dynar_push(hd->ports,&pr);
-
-  /* take a free RAW channel for this process */
-  for (i=0; i<XBT_MAX_CHANNEL && hd->proc[i]; i++);
-  if (i == XBT_MAX_CHANNEL) {
-    RAISE2(system_error,
-          "GRAS: Can't add a new process on %s, because all channel are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS\n.",
-           MSG_host_get_name(MSG_host_self()),XBT_MAX_CHANNEL);
-  }
-  trp_pd->rawChan = i;
+  if (SIMIX_process_self() != NULL) {
+    pd->ppid = gras_os_getpid();
+  } else
+    pd->ppid = -1;
 
-  hd->proc[ i ] = MSG_process_self_PID();
+  trp_pd->msg_selectable_sockets = xbt_queue_new(0, sizeof(gras_socket_t));
 
-  /* register it to the ports structure */
-  prraw.port = -1;
-  prraw.tochan = i;
-  prraw.raw = 1;
-  xbt_dynar_push(hd->ports,&prraw);
+  trp_pd->meas_selectable_sockets =
+      xbt_queue_new(0, sizeof(gras_socket_t));
 
   VERB2("Creating process '%s' (%d)",
-          MSG_process_get_name(MSG_process_self()),
-          MSG_process_self_PID());
-  return no_error;
+        SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
 }
 
-xbt_error_t
-gras_process_exit() {
-  gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
-  gras_msg_procdata_t msg_pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
-  gras_trp_procdata_t trp_pd=(gras_trp_procdata_t)gras_libdata_get("gras_trp");
-  int myPID=MSG_process_self_PID();
-  int cpt;
-  gras_sg_portrec_t pr;
+void gras_process_exit()
+{
+  xbt_dynar_t sockets =
+      ((gras_trp_procdata_t) gras_libdata_by_name("gras_trp"))->sockets;
+  gras_socket_t sock_iter;
+  unsigned int cursor;
+  gras_hostdata_t *hd =
+      (gras_hostdata_t *) SIMIX_host_get_data(SIMIX_host_self());
+  gras_procdata_t *pd =
+      (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
+
+  gras_msg_procdata_t msg_pd =
+      (gras_msg_procdata_t) gras_libdata_by_name("gras_msg");
+  gras_trp_procdata_t trp_pd =
+      (gras_trp_procdata_t) gras_libdata_by_name("gras_trp");
+
+  xbt_queue_free(&trp_pd->msg_selectable_sockets);
 
-  xbt_assert0(hd,"Run gras_process_init (ie, gras_init)!!");
+  xbt_queue_free(&trp_pd->meas_selectable_sockets);
 
-  INFO2("GRAS: Finalizing process '%s' (%d)",
-       MSG_process_get_name(MSG_process_self()),MSG_process_self_PID());
 
-  if (xbt_dynar_length(msg_pd->msg_queue))
-    WARN1("process %d terminated, but some messages are still queued",
-         MSG_process_self_PID());
+  xbt_assert0(hd, "Run gras_process_init (ie, gras_init)!!");
 
-  for (cpt=0; cpt< XBT_MAX_CHANNEL; cpt++)
-    if (myPID == hd->proc[cpt])
-      hd->proc[cpt] = 0;
+  VERB2("GRAS: Finalizing process '%s' (%d)",
+        SIMIX_process_get_name(SIMIX_process_self()), gras_os_getpid());
 
-  xbt_dynar_foreach(hd->ports, cpt, pr) {
-    if (pr.port == trp_pd->chan || pr.port == trp_pd->rawChan) {
-      xbt_dynar_cursor_rm(hd->ports, &cpt);
+  if (xbt_dynar_length(msg_pd->msg_queue)) {
+    unsigned int cpt;
+    s_gras_msg_t msg;
+    WARN2
+        ("process %d terminated, but %ld messages are still queued. Message list:",
+         gras_os_getpid(), xbt_dynar_length(msg_pd->msg_queue));
+    xbt_dynar_foreach(msg_pd->msg_queue, cpt, msg) {
+      WARN5("   Message %s (%s) from %s@%s:%d", msg.type->name,
+            e_gras_msg_kind_names[msg.kind],
+            gras_socket_peer_proc(msg.expe),
+            gras_socket_peer_name(msg.expe),
+            gras_socket_peer_port(msg.expe));
     }
   }
 
-  return no_error;
+  /* if each process has its sockets list, we need to close them when the
+     process finish */
+  xbt_dynar_foreach(sockets, cursor, sock_iter) {
+    VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
+          sock_iter);
+    gras_socket_close(sock_iter);
+  }
+  if (!--(hd->refcount)) {
+    xbt_dynar_free(&hd->ports);
+    free(hd);
+  }
+  gras_procdata_exit();
+  free(pd);
 }
 
 /* **************************************************************************
- * Process data
+ * Process data (public interface)
  * **************************************************************************/
 
-gras_procdata_t *gras_procdata_get(void) {
-  gras_procdata_t *pd=
-    (gras_procdata_t *)MSG_process_get_data(MSG_process_self());
+gras_procdata_t *gras_procdata_get(void)
+{
+  gras_procdata_t *pd =
+      (gras_procdata_t *) SIMIX_process_get_data(SIMIX_process_self());
 
-  xbt_assert0(pd,"Run gras_process_init! (ie, gras_init)");
+  xbt_assert0(pd, "Run gras_process_init! (ie, gras_init)");
 
   return pd;
 }
 
-const char* xbt_procname(void) {
-  const char *res = NULL;
-  m_process_t process = MSG_process_self();
-  if ((process != NULL) && (process->simdata))
-    res = MSG_process_get_name(process);
-  if (res) 
-    return res;
-  else
-    return "";
+void *gras_libdata_by_name_from_remote(const char *name, smx_process_t p)
+{
+  gras_procdata_t *pd = (gras_procdata_t *) SIMIX_process_get_data(p);
+
+  xbt_assert2(pd,
+              "process '%s' on '%s' didn't run gras_process_init! (ie, gras_init)",
+              SIMIX_process_get_name(p),
+              SIMIX_host_get_name(SIMIX_process_get_host(p)));
+
+  return gras_libdata_by_name_from_procdata(name, pd);
+}
+
+/** @brief retrieve the value of a given process property (or NULL if not defined) */
+const char *gras_process_property_value(const char *name)
+{
+  return xbt_dict_get_or_null(
+               SIMIX_process_get_properties(SIMIX_process_self()), 
+                                            name);
+}
+
+/** @brief retrieve the process properties dictionnary
+ *  @warning it's the original one, not a copy. Don't mess with it
+ */
+xbt_dict_t gras_process_properties(void)
+{
+  return SIMIX_process_get_properties(SIMIX_process_self());
+}
+
+/* **************************************************************************
+ * OS virtualization function
+ * **************************************************************************/
+
+const char *xbt_procname(void)
+{
+  smx_process_t process = SIMIX_process_self();
+  /*FIXME: maestro used not have a simix process, now it does so 
+     SIMIX_process_self will return something different to NULL. This breaks
+     the old xbt_log logic that assumed that NULL was equivalent to maestro,
+     thus when printing it searches for maestro host name (which doesn't exists)
+     and breaks the logging.
+     As a hack we check for maestro by looking to the assigned host, if it is
+     NULL then we are sure is maestro
+   */
+  if (process != NULL && SIMIX_process_get_host(process))
+    return SIMIX_process_get_name(process);
+
+  return "";
+}
+
+int gras_os_getpid(void)
+{
+  gras_procdata_t *data;
+  smx_process_t process = SIMIX_process_self();
+
+  if (process != NULL) {
+    data = (gras_procdata_t *) SIMIX_process_get_data(process);
+    if (data != NULL)
+      return data->pid;
+  }
+
+  return 0;
+}
+
+/** @brief retrieve the value of a given host property (or NULL if not defined) */
+const char *gras_os_host_property_value(const char *name)
+{
+  return
+      xbt_dict_get_or_null(SIMIX_host_get_properties
+                           (SIMIX_process_get_host(SIMIX_process_self())),
+                           name);
+}
+
+/** @brief retrieve the host properties dictionnary
+ *  @warning it's the original one, not a copy. Don't mess with it
+ */
+xbt_dict_t gras_os_host_properties(void)
+{
+  return
+      SIMIX_host_get_properties(SIMIX_process_get_host
+                                (SIMIX_process_self()));
+}
+
+/* **************************************************************************
+ * Interface with SIMIX
+ * (these functions are called by the stuff generated by gras_stub_generator)
+ * **************************************************************************/
+
+XBT_LOG_EXTERNAL_CATEGORY(gras_trp);
+XBT_LOG_EXTERNAL_CATEGORY(gras_trp_sg);
+
+void gras_global_init(int *argc, char **argv)
+{
+  XBT_LOG_CONNECT(gras_trp_sg, gras_trp);
+  SIMIX_global_init(argc, argv);
+}
+
+void gras_create_environment(const char *file)
+{
+  SIMIX_create_environment(file);
+}
+
+void gras_function_register(const char *name, xbt_main_func_t code)
+{
+  SIMIX_function_register(name, code);
+}
+
+void gras_function_register_default(xbt_main_func_t code)
+{
+  SIMIX_function_register_default(code);
+}
+
+void gras_main()
+{
+  /* Clean IO before the run */
+  fflush(stdout);
+  fflush(stderr);
+  SIMIX_init();
+
+  while (SIMIX_solve(NULL, NULL) != -1.0);
+
+  return;
+}
+
+void gras_launch_application(const char *file)
+{
+  SIMIX_launch_application(file);
+}
+
+void gras_load_environment_script(const char *script_file)
+{
+#ifdef HAVE_LUA
+  lua_State *L = lua_open();
+  luaL_openlibs(L);
+
+  if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
+    printf("error: %s\n", lua_tostring(L, -1));
+    return;
+  }
+#else
+  xbt_die
+      ("Lua is not available!! to call gras_load_environment_script, lua should be available...");
+#endif
+  return;
 }
 
-int gras_os_getpid(void) {
-  m_process_t process = MSG_process_self();
-  if ((process != NULL) && (process->simdata))
-    return MSG_process_get_PID(MSG_process_self());
-  else
-    return 0;
+void gras_clean()
+{
+  SIMIX_clean();
 }