Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert "Revert "cast once for all at surf level and not in the APIs""
[simgrid.git] / src / msg / msg_io.cpp
index 510ac65..3b06812 100644 (file)
@@ -1,16 +1,17 @@
-/* Copyright (c) 2004-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2004-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. */
 
-#include <simgrid/s4u/host.hpp>
-
-#include "msg_private.h"
-#include "xbt/log.h"
+#include "simgrid/s4u/host.hpp"
+#include "src/msg/msg_private.h"
+#include "src/surf/storage_interface.hpp"
+#include <numeric>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg, "Logging specific to MSG (io)");
 
+SG_BEGIN_DECL()
+
 /** @addtogroup msg_file
  * (#msg_file_t) and the functions for managing it.
  *
@@ -34,6 +35,24 @@ void __MSG_file_get_info(msg_file_t fd){
   xbt_dynar_free_container(&info);
 }
 
+static int MSG_host_get_file_descriptor_id(msg_host_t host)
+{
+  simgrid::MsgHostExt* priv = host->extension<simgrid::MsgHostExt>();
+  if (priv->file_descriptor_table == nullptr) {
+    priv->file_descriptor_table = new std::vector<int>(sg_storage_max_file_descriptors);
+    std::iota(priv->file_descriptor_table->rbegin(), priv->file_descriptor_table->rend(), 0); // Fill with ..., 1, 0.
+  }
+  xbt_assert(!priv->file_descriptor_table->empty(), "Too much files are opened! Some have to be closed.");
+  int desc = priv->file_descriptor_table->back();
+  priv->file_descriptor_table->pop_back();
+  return desc;
+}
+
+static void MSG_host_release_file_descriptor_id(msg_host_t host, int id)
+{
+  host->extension<simgrid::MsgHostExt>()->file_descriptor_table->push_back(id);
+}
+
 /** \ingroup msg_file
  *
  * \brief Set the user data of a #msg_file_t.
@@ -103,7 +122,7 @@ sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
   read_size = simcall_file_read(file_priv->simdata->smx_file, size, attached_host);
 
-  if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
+  if (strcmp(storage_priv_src->hostname, MSG_host_self()->cname())) {
     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, read_size);
     msg_host_t *m_host_list = nullptr;
@@ -121,7 +140,7 @@ sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
     xbt_free(m_host_list);
     if(transfer != MSG_OK){
       if (transfer == MSG_HOST_FAILURE)
-        XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
+        XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
       if (transfer == MSG_TASK_CANCELED)
         XBT_WARN("Transfer error, task has been canceled!");
 
@@ -150,7 +169,7 @@ sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
   msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
   msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
 
-  if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
+  if (strcmp(storage_priv_src->hostname, MSG_host_self()->cname())) {
     /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
     XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
     msg_host_t *m_host_list = nullptr;
@@ -168,7 +187,7 @@ sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
     free(m_host_list);
     if(transfer != MSG_OK){
       if (transfer == MSG_HOST_FAILURE)
-        XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
+        XBT_WARN("Transfer error, %s remote host just turned off!", attached_host->cname());
       if (transfer == MSG_TASK_CANCELED)
         XBT_WARN("Transfer error, task has been canceled!");
 
@@ -199,9 +218,9 @@ msg_file_t MSG_file_open(const char* fullpath, void* data)
   priv->fullpath = xbt_strdup(fullpath);
   priv->simdata = xbt_new0(s_simdata_file_t,1);
   priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
-  priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
+  priv->desc_id           = MSG_host_get_file_descriptor_id(MSG_host_self());
 
-  name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
+  name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_self()->cname(), priv->desc_id);
 
   xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
   msg_file_t fd = static_cast<msg_file_t>(xbt_lib_get_elm_or_null(file_lib, name));
@@ -225,8 +244,8 @@ int MSG_file_close(msg_file_t fd)
     xbt_free(priv->data);
 
   int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
-  name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()), priv->desc_id);
-  __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
+  name    = bprintf("%s:%s:%d", priv->fullpath, MSG_host_self()->cname(), priv->desc_id);
+  MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
   xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
   xbt_free(name);
   return res;
@@ -359,7 +378,7 @@ msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpa
     host_name_dest = (char*)storage_dest_priv->hostname;
     host_dest = MSG_host_by_name(host_name_dest);
   }else{
-    XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, sg_host_get_name(host));
+    XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->cname());
     return MSG_TASK_CANCELED;
   }
 
@@ -425,12 +444,14 @@ void __MSG_file_destroy(msg_file_priv_t file) {
 
 msg_storage_t __MSG_storage_create(smx_storage_t storage)
 {
-  const char *name = SIMIX_storage_get_name(storage);
-  const char *host = SIMIX_storage_get_host(storage);
   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
-  storage_private->hostname = host;
-  xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
-  return xbt_lib_get_elm_or_null(storage_lib, name);
+
+  storage_private->name     = SIMIX_storage_get_name(storage);
+  storage_private->hostname = surf_storage_get_host(storage);
+  storage_private->size     = surf_storage_get_size(storage);
+
+  xbt_lib_set(storage_lib, storage_private->name, MSG_STORAGE_LEVEL, storage_private);
+  return xbt_lib_get_elm_or_null(storage_lib, storage_private->name);
 }
 
 /**
@@ -448,7 +469,8 @@ void __MSG_storage_destroy(msg_storage_priv_t storage) {
  */
 const char *MSG_storage_get_name(msg_storage_t storage) {
   xbt_assert((storage != nullptr), "Invalid parameters");
-  return SIMIX_storage_get_name(storage);
+  msg_storage_priv_t priv = MSG_storage_priv(storage);
+  return priv->name;
 }
 
 /** \ingroup msg_storage_management
@@ -486,10 +508,10 @@ xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
  * \param storage a storage
  * \param name a property name
  * \param value what to change the property to
- * \param free_ctn the freeing function to use to kill the value on need
  */
-void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
-  xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
+void MSG_storage_set_property_value(msg_storage_t storage, const char* name, char* value)
+{
+  xbt_dict_set(MSG_storage_get_properties(storage), name, value, nullptr);
 }
 
 /** \ingroup m_storage_management
@@ -565,7 +587,13 @@ void *MSG_storage_get_data(msg_storage_t storage)
  */
 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
 {
-  return SIMIX_storage_get_content(storage);
+  std::map<std::string, sg_size_t*>* content = surf_storage_resource_priv(storage)->getContent();
+  xbt_dict_t content_dict = xbt_dict_new_homogeneous(nullptr);
+
+  for (auto entry : *content) {
+    xbt_dict_set(content_dict, entry.first.c_str(), entry.second, nullptr);
+  }
+  return content_dict;
 }
 
 /** \ingroup msg_storage_management
@@ -576,7 +604,8 @@ xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
  */
 sg_size_t MSG_storage_get_size(msg_storage_t storage)
 {
-  return SIMIX_storage_get_size(storage);
+  msg_storage_priv_t priv = MSG_storage_priv(storage);
+  return priv->size;
 }
 
 /** \ingroup msg_storage_management
@@ -590,3 +619,5 @@ const char *MSG_storage_get_host(msg_storage_t storage) {
   msg_storage_priv_t priv = MSG_storage_priv(storage);
   return priv->hostname;
 }
+
+SG_END_DECL()