Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First steps for making a tutorial on How to create a new API in Simgrid
authornavarro <navarro@caraja.(none)>
Fri, 12 Oct 2012 09:09:02 +0000 (11:09 +0200)
committernavarro <navarro@caraja.(none)>
Fri, 12 Oct 2012 09:49:12 +0000 (11:49 +0200)
buildtools/Cmake/DefinePackages.cmake
include/msg/msg.h
src/msg/msg_new_api.c [new file with mode: 0644]
src/simix/smx_new_api.c [new file with mode: 0644]
src/simix/smx_new_api_private.h [new file with mode: 0644]
src/simix/smx_private.h
src/simix/smx_smurf.c
src/simix/smx_smurf_private.h
src/simix/smx_user.c

index 0a48719..da66cbd 100644 (file)
@@ -272,6 +272,23 @@ set(MSG_SRC
   src/msg/msg_vm.c
   )
 
   src/msg/msg_vm.c
   )
 
+#* ****************************************************************************************** *#
+#* TUTORIAL: New API                                                                        *#
+
+set(MSG_SRC
+  ${MSG_SRC}
+  src/msg/msg_new_api.c
+  )
+set(EXTRA_DIST
+  ${EXTRA_DIST}
+  src/simix/smx_new_api_private.h
+  )
+set(SIMIX_SRC
+  ${SIMIX_SRC}
+  src/simix/smx_new_api.c
+)
+#* ****************************************************************************************** *#
+
 set(PLATFGEN_SRC
   include/simgrid/platf_generator.h
   src/surf/platf_generator.c
 set(PLATFGEN_SRC
   include/simgrid/platf_generator.h
   src/surf/platf_generator.c
index cfcffee..19f733a 100644 (file)
@@ -383,5 +383,11 @@ xbt_dynar_t<msg_vm_t> MSG_vm_get_list_from_hosts(msg_dynar_t<msg_host_t>)
 /* Used only by the bindings -- unclean pimple, please ignore if you're not writing a binding */
 XBT_PUBLIC(smx_context_t) MSG_process_get_smx_ctx(msg_process_t process);
 
 /* Used only by the bindings -- unclean pimple, please ignore if you're not writing a binding */
 XBT_PUBLIC(smx_context_t) MSG_process_get_smx_ctx(msg_process_t process);
 
+/* ****************************************************************************************** */
+/* TUTORIAL: New API                                                                        */
+/* Declare all functions for the API                                                          */
+/* ****************************************************************************************** */
+XBT_PUBLIC(int) MSG_new_API_fct(const char* param1, double param2);
+
 SG_END_DECL()
 #endif
 SG_END_DECL()
 #endif
diff --git a/src/msg/msg_new_api.c b/src/msg/msg_new_api.c
new file mode 100644 (file)
index 0000000..b760f44
--- /dev/null
@@ -0,0 +1,22 @@
+/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012. 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 "msg_private.h"
+#include "xbt/log.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_new_API, msg,
+                                "Logging specific to MSG (new_API)");
+
+
+/* ****************************************************************************************** */
+/* TUTORIAL: New API                                                                        */
+/* All functions for the API                                                                  */
+/* ****************************************************************************************** */
+int MSG_new_API_fct(const char* param1, double param2)
+{
+  int result = simcall_new_api_fct(param1, param2);
+  return result;
+}
diff --git a/src/simix/smx_new_api.c b/src/simix/smx_new_api.c
new file mode 100644 (file)
index 0000000..e2c2890
--- /dev/null
@@ -0,0 +1,136 @@
+/* Copyright (c) 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. */
+
+/* ****************************************************************************************** */
+/* TUTORIAL: New API                                                                        */
+/* ****************************************************************************************** */
+#include "smx_private.h"
+#include "xbt/sysdep.h"
+#include "xbt/log.h"
+#include "xbt/dict.h"
+#include "mc/mc.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_new_api, simix,
+                                "Logging specific to SIMIX (new_api)");
+
+
+//SIMIX NEW MODEL INIT
+void SIMIX_pre_new_api_fct(smx_simcall_t simcall)
+{
+  smx_action_t action = SIMIX_new_api_fct(simcall->issuer,
+      simcall->new_api.param1,
+      simcall->new_api.param2);
+  xbt_fifo_push(action->simcalls, simcall);
+  simcall->issuer->waiting_action = action;
+}
+
+void SIMIX_post_new_api(smx_action_t action)
+{
+  xbt_fifo_item_t i;
+  smx_simcall_t simcall;
+
+  xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
+    switch (simcall->call) {
+    case SIMCALL_NEW_API_INIT:
+      simcall->new_api.result = 0;
+      break;
+
+    default:
+      break;
+    }
+  }
+
+  switch (surf_workstation_model->action_state_get(action->new_api.surf_new_api)) {
+
+    case SURF_ACTION_FAILED:
+      action->state = SIMIX_FAILED;
+      break;
+
+    case SURF_ACTION_DONE:
+      action->state = SIMIX_DONE;
+      break;
+
+    default:
+      THROW_IMPOSSIBLE;
+      break;
+  }
+
+  SIMIX_new_api_finish(action);
+}
+
+smx_action_t SIMIX_new_api_fct(smx_process_t process, const char* param1, double param2)
+{
+  smx_action_t action;
+  smx_host_t host = process->smx_host;
+
+  /* check if the host is active */
+  if (surf_workstation_model->extension.
+      workstation.get_state(host->host) != SURF_RESOURCE_ON) {
+    THROWF(host_error, 0, "Host %s failed, you cannot call this function",
+           host->name);
+  }
+
+  action = xbt_mallocator_get(simix_global->action_mallocator);
+  action->type = SIMIX_ACTION_NEW_API;
+  action->name = NULL;
+#ifdef HAVE_TRACING
+  action->category = NULL;
+#endif
+
+  action->new_api.surf_new_api = NULL;
+
+  surf_workstation_model->action_data_set(action->new_api.surf_new_api, action);
+  XBT_DEBUG("Create NEW MODEL action %p", action);
+
+  return action;
+}
+
+void SIMIX_new_api_destroy(smx_action_t action)
+{
+  XBT_DEBUG("Destroy action %p", action);
+  if (action->new_api.surf_new_api)
+    action->new_api.surf_new_api->model_type->action_unref(action->new_api.surf_new_api);
+  xbt_mallocator_release(simix_global->action_mallocator, action);
+}
+
+void SIMIX_new_api_finish(smx_action_t action)
+{
+  xbt_fifo_item_t item;
+  smx_simcall_t simcall;
+
+  xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
+
+    switch (action->state) {
+
+      case SIMIX_DONE:
+        /* do nothing, action done */
+        break;
+
+      case SIMIX_FAILED:
+        SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
+        break;
+
+      case SIMIX_CANCELED:
+        SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
+        break;
+
+      default:
+        xbt_die("Internal error in SIMIX_NEW_MODEL_finish: unexpected action state %d",
+            (int)action->state);
+    }
+
+    if (surf_workstation_model->extension.
+        workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
+      simcall->issuer->context->iwannadie = 1;
+    }
+
+    simcall->issuer->waiting_action = NULL;
+    SIMIX_simcall_answer(simcall);
+  }
+
+  /* We no longer need it */
+  SIMIX_new_api_destroy(action);
+}
diff --git a/src/simix/smx_new_api_private.h b/src/simix/smx_new_api_private.h
new file mode 100644 (file)
index 0000000..72ab3e0
--- /dev/null
@@ -0,0 +1,23 @@
+/* Copyright (c) 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. */
+
+/* ****************************************************************************************** */
+/* TUTORIAL: New API                                                                        */
+/* ****************************************************************************************** */
+#ifndef _SIMIX_NEW_API_PRIVATE_H
+#define _SIMIX_NEW_API_PRIVATE_H
+
+#include "simgrid/simix.h"
+#include "smx_smurf_private.h"
+
+void SIMIX_pre_new_api_fct(smx_simcall_t simcall);
+smx_action_t SIMIX_new_api_fct(smx_process_t process, const char* param1, double param2);
+
+void SIMIX_post_new_api(smx_action_t action);
+void SIMIX_new_api_destroy(smx_action_t action);
+void SIMIX_new_api_finish(smx_action_t action);
+
+#endif
index 8d6f5f5..6e151d7 100644 (file)
 #include "smx_network_private.h"
 #include "smx_smurf_private.h"
 #include "smx_synchro_private.h"
 #include "smx_network_private.h"
 #include "smx_smurf_private.h"
 #include "smx_synchro_private.h"
+/* ****************************************************************************************** */
+/* TUTORIAL: New API                                                                        */
+/* ****************************************************************************************** */
+#include "smx_new_api_private.h"
 
 /* Define only for SimGrid benchmarking purposes */
 //#define TIME_BENCH_PER_SR /* this aims at measuring the time spent in each scheduling round per each thread. The code is thus run in sequential to bench separately each SSR */
 
 /* Define only for SimGrid benchmarking purposes */
 //#define TIME_BENCH_PER_SR /* this aims at measuring the time spent in each scheduling round per each thread. The code is thus run in sequential to bench separately each SSR */
@@ -92,7 +96,11 @@ typedef enum {
   SIMIX_ACTION_COMMUNICATE,
   SIMIX_ACTION_SLEEP,
   SIMIX_ACTION_SYNCHRO,
   SIMIX_ACTION_COMMUNICATE,
   SIMIX_ACTION_SLEEP,
   SIMIX_ACTION_SYNCHRO,
-  SIMIX_ACTION_IO
+  SIMIX_ACTION_IO,
+  /* ****************************************************************************************** */
+  /* TUTORIAL: New API                                                                        */
+  /* ****************************************************************************************** */
+  SIMIX_ACTION_NEW_API
 } e_smx_action_type_t;
 
 typedef enum {
 } e_smx_action_type_t;
 
 typedef enum {
@@ -169,6 +177,13 @@ typedef struct s_smx_action {
       smx_host_t host;
       surf_action_t surf_io;
     } io;
       smx_host_t host;
       surf_action_t surf_io;
     } io;
+
+    /* ****************************************************************************************** */
+    /* TUTORIAL: New API                                                                        */
+    /* ****************************************************************************************** */
+    struct {
+      surf_action_t surf_new_api;
+    } new_api;
   };
 
 #ifdef HAVE_LATENCY_BOUND_TRACKING
   };
 
 #ifdef HAVE_LATENCY_BOUND_TRACKING
index 0a8fbca..aed0958 100644 (file)
@@ -563,6 +563,13 @@ void SIMIX_simcall_pre(smx_simcall_t simcall, int value)
           SIMIX_host_get_name(SIMIX_process_get_host(simcall->issuer))
           );
       break;
           SIMIX_host_get_name(SIMIX_process_get_host(simcall->issuer))
           );
       break;
+
+    /* ****************************************************************************************** */
+    /* TUTORIAL: New API                                                                        */
+    /* ****************************************************************************************** */
+    case SIMCALL_NEW_API_INIT:
+      SIMIX_pre_new_api_fct(simcall);
+      break;
   }
 }
 
   }
 }
 
@@ -590,5 +597,12 @@ void SIMIX_simcall_post(smx_action_t action)
     case SIMIX_ACTION_IO:
       SIMIX_post_io(action);
       break;
     case SIMIX_ACTION_IO:
       SIMIX_post_io(action);
       break;
+
+    /* ****************************************************************************************** */
+    /* TUTORIAL: New API                                                                        */
+    /* ****************************************************************************************** */
+    case SIMIX_ACTION_NEW_API:
+      SIMIX_post_new_model(action);
+      break;
   }
 }
   }
 }
index d111650..adff451 100644 (file)
@@ -97,7 +97,11 @@ SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_CLOSE),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_STAT), \
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_UNLINK),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_LS),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_STAT), \
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_UNLINK),\
 SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_LS),\
-SIMCALL_ENUM_ELEMENT(SIMCALL_ASR_GET_PROPERTIES)
+SIMCALL_ENUM_ELEMENT(SIMCALL_ASR_GET_PROPERTIES), \
+/* ****************************************************************************************** */ \
+/* TUTORIAL: New API                                                                        */ \
+/* ****************************************************************************************** */ \
+SIMCALL_ENUM_ELEMENT(SIMCALL_NEW_API_INIT)
 
 
 /* SIMCALL_COMM_IS_LATENCY_BOUNDED and SIMCALL_SET_CATEGORY make things complicated
 
 
 /* SIMCALL_COMM_IS_LATENCY_BOUNDED and SIMCALL_SET_CATEGORY make things complicated
@@ -603,6 +607,15 @@ typedef struct s_smx_simcall {
       xbt_dict_t result;
     } asr_get_properties;
 
       xbt_dict_t result;
     } asr_get_properties;
 
+    /* ****************************************************************************************** */
+    /* TUTORIAL: New API                                                                        */
+    /* ****************************************************************************************** */
+    struct {
+      const char* param1;
+      double param2;
+      int result;
+    } new_api;
+
   };
 } s_smx_simcall_t, *smx_simcall_t;
 
   };
 } s_smx_simcall_t, *smx_simcall_t;
 
index 2ad612d..7f48359 100644 (file)
@@ -1651,6 +1651,20 @@ xbt_dict_t simcall_file_ls(const char* mount, const char* path)
   return simcall->file_ls.result;
 }
 
   return simcall->file_ls.result;
 }
 
+/* ****************************************************************************************** */
+/* TUTORIAL: New API                                                                          */
+/* All functions for simcall                                                                  */
+/* ****************************************************************************************** */
+int simcall_new_api_fct(const char* param1, double param2){
+  smx_simcall_t simcall = SIMIX_simcall_mine();
+  simcall->call = SIMCALL_NEW_API_INIT;
+  simcall->new_api.param1 = param1;
+  simcall->new_api.param2 = param2;
+
+  SIMIX_simcall_push(simcall->issuer);
+  return simcall->new_api.result;
+}
+
 /* ************************************************************************** */
 
 /** @brief returns a printable string representing a simcall */
 /* ************************************************************************** */
 
 /** @brief returns a printable string representing a simcall */