Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 29 Feb 2016 16:30:27 +0000 (17:30 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Mon, 29 Feb 2016 16:30:27 +0000 (17:30 +0100)
include/xbt/hash.h [deleted file]
src/mc/mc_private.h
src/smpi/smpi_bench.cpp
src/xbt/xbt_sha.c [deleted file]
tools/cmake/DefinePackages.cmake
tools/cmake/Distrib.cmake
tools/cmake/UnitTesting.cmake

diff --git a/include/xbt/hash.h b/include/xbt/hash.h
deleted file mode 100644 (file)
index 7bcea4f..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/* hash.h - Various hashing functions.                                      */
-
-/* Copyright (c) 2008-2011, 2013-2014. 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. */
-
-#ifndef XBT_HASH_H
-#define XBT_HASH_H
-
-#include "xbt/misc.h"
-
-SG_BEGIN_DECL()
-
-/* The classical SHA1 algorithm */
-typedef struct s_xbt_sha_ s_xbt_sha_t, *xbt_sha_t;
-
-XBT_PUBLIC(xbt_sha_t) xbt_sha_new(void);
-XBT_PUBLIC(void) xbt_sha_free(xbt_sha_t sha);
-
-XBT_PUBLIC(void) xbt_sha_feed(xbt_sha_t sha, const unsigned char *data,
-                              size_t len);
-XBT_PUBLIC(void) xbt_sha_reset(xbt_sha_t sha);
-
-XBT_PUBLIC(void) xbt_sha_print(xbt_sha_t sha, char *hash);
-XBT_PUBLIC(char *) xbt_sha_read(xbt_sha_t sha);
-
-XBT_PUBLIC(void) xbt_sha(const char *data, char *hash);
-
-SG_END_DECL()
-
-#endif                          /* XBT_HASH_H */
index a5ded6a..de35d20 100644 (file)
@@ -33,7 +33,6 @@
 #include "src/simix/smx_private.h"
 #include "src/xbt/mmalloc/mmprivate.h"
 #include "xbt/automaton.h"
-#include "xbt/hash.h"
 #include <simgrid/msg.h>
 #include "xbt/strbuff.h"
 #include "xbt/parmap.h"
index 695615d..c7c1dc1 100644 (file)
@@ -4,12 +4,16 @@
 /* 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 <cstring>
+
+#include <unordered_map>
+#include <utility>
+
 #include "src/internal_config.h"
 #include "private.h"
 #include "xbt/dict.h"
 #include "xbt/sysdep.h"
 #include "xbt/ex.h"
-#include "xbt/hash.h"
 #include "surf/surf.h"
 #include "simgrid/sg_config.h"
 #include "simgrid/modelchecker.h"
@@ -67,8 +71,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
 
 #define PTR_STRLEN (2 + 2 * sizeof(void*) + 1)
 
-xbt_dict_t allocs = NULL;          /* Allocated on first use */
-xbt_dict_t allocs_metadata = NULL; /* Allocated on first use */
 xbt_dict_t samples = NULL;         /* Allocated on first use */
 xbt_dict_t calls = NULL;           /* Allocated on first use */
 
@@ -82,17 +84,72 @@ int smpi_privatize_global_variables;
 double smpi_total_benched_time = 0;
 smpi_privatisation_region_t smpi_privatisation_regions;
 
+namespace {
+
+/** Some location in the source code
+ *
+ *  This information is used by SMPI_SHARED_MALLOC to allocate
+ *  some shared memory for all simulated processes.
+ */
+class smpi_source_location {
+public:
+  smpi_source_location(const char* filename, int line)
+    : filename(filename), filename_length(strlen(filename)), line(line) {}
+
+  /** Pointer to a static string containing the file name */
+  const char* filename = nullptr;
+  int filename_length = 0;
+  int line = 0;
+
+  bool operator==(smpi_source_location const& that) const
+  {
+    return filename_length == that.filename_length
+      && line == that.line
+      && std::memcmp(filename, that.filename, filename_length) == 0;
+  }
+  bool operator!=(smpi_source_location const& that) const
+  {
+    return !(*this == that);
+  }
+};
+
+}
+
+namespace std {
+
+template<>
+class hash<smpi_source_location> {
+public:
+  typedef smpi_source_location argument_type;
+  typedef std::size_t result_type;
+  result_type operator()(smpi_source_location const& loc) const
+  {
+    return xbt_str_hash_ext(loc.filename, loc.filename_length)
+      ^ xbt_str_hash_ext((const char*) &loc.line, sizeof(loc.line));
+  }
+};
+
+}
+
+namespace {
+
 typedef struct {
-  int fd;
-  int count;
-  char* loc;
+  int fd = -1;
+  int count = 0;
 } shared_data_t;
 
+std::unordered_map<smpi_source_location, shared_data_t> allocs;
+typedef std::unordered_map<smpi_source_location, shared_data_t>::value_type shared_data_key_type;
+
 typedef struct  {
   size_t size;
-  shared_data_t* data;
+  shared_data_key_type* data;
 } shared_metadata_t;
 
+std::unordered_map<void*, shared_metadata_t> allocs_metadata;
+
+}
+
 static size_t shm_size(int fd) {
   struct stat st;
 
@@ -103,10 +160,10 @@ static size_t shm_size(int fd) {
 }
 
 #ifndef WIN32
-static void* shm_map(int fd, size_t size, shared_data_t* data) {
+static void* shm_map(int fd, size_t size, shared_data_key_type* data) {
   void* mem;
   char loc[PTR_STRLEN];
-  shared_metadata_t* meta;
+  shared_metadata_t meta;
 
   if(size > shm_size(fd)) {
     if(ftruncate(fd, (off_t)size) < 0) {
@@ -118,14 +175,10 @@ static void* shm_map(int fd, size_t size, shared_data_t* data) {
   if(mem == MAP_FAILED) {
     xbt_die("Could not map fd %d: %s", fd, strerror(errno));
   }
-  if(!allocs_metadata) {
-    allocs_metadata = xbt_dict_new_homogeneous(xbt_free_f);
-  }
   snprintf(loc, PTR_STRLEN, "%p", mem);
-  meta = xbt_new(shared_metadata_t, 1);
-  meta->size = size;
-  meta->data = data;
-  xbt_dict_set(allocs_metadata, loc, meta, NULL);
+  meta.size = size;
+  meta.data = data;
+  allocs_metadata[mem] = meta;
   XBT_DEBUG("MMAP %zu to %p", size, mem);
   return mem;
 }
@@ -133,8 +186,8 @@ static void* shm_map(int fd, size_t size, shared_data_t* data) {
 
 void smpi_bench_destroy(void)
 {
-  xbt_dict_free(&allocs);
-  xbt_dict_free(&allocs_metadata);
+  allocs.clear();
+  allocs_metadata.clear();
   xbt_dict_free(&samples);
   xbt_dict_free(&calls);
 }
@@ -424,78 +477,42 @@ void smpi_sample_3(int global, const char *file, int line)
 }
 
 #ifndef WIN32
-static void smpi_shared_alloc_free(void *p)
-{
-  shared_data_t *data = static_cast<shared_data_t *>(p);
-  xbt_free(data->loc);
-  xbt_free(data);
-}
-
-static char *smpi_shared_alloc_hash(char *loc)
-{
-  char hash[42];
-  char s[7];
-  unsigned val;
-  int i, j;
-
-  xbt_sha(loc, hash);
-  hash[41] = '\0';
-  s[6] = '\0';
-  loc = static_cast<char *>(xbt_realloc(loc, 30));
-  loc[0] = '/';
-  for (i = 0; i < 40; i += 6) { /* base64 encode */
-    memcpy(s, hash + i, 6);
-    val = strtoul(s, NULL, 16);
-    for (j = 0; j < 4; j++) {
-      unsigned char x = (val >> (18 - 3 * j)) & 0x3f;
-      loc[1 + 4 * i / 6 + j] =
-        "ABCDEFGHIJKLMNOPQRSTUVZXYZabcdefghijklmnopqrstuvzxyz0123456789-_"[x];
-    }
-  }
-  loc[29] = '\0';
-  return loc;
-}
 
 void *smpi_shared_malloc(size_t size, const char *file, int line)
 {
   void* mem;
   if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
-    char *loc = bprintf("/%zu_%s_%d", (size_t)getpid(), file, line);
     int fd;
-    shared_data_t *data;
-    loc = smpi_shared_alloc_hash(loc); /* hash loc, in order to have something
-                                        * not too long */
-    if (!allocs) {
-      allocs = xbt_dict_new_homogeneous(smpi_shared_alloc_free);
-    }
-    data = static_cast<shared_data_t *>(xbt_dict_get_or_null(allocs, loc));
-    if (!data) {
-      fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL,
+    smpi_source_location loc(file, line);
+    auto res = allocs.insert(std::make_pair(loc, shared_data_t()));
+    auto data = res.first;
+    if (res.second) {
+      // The insertion did not take place.
+      // Generate a shared memory name from the address of the shared_data:
+      char shmname[256];
+      sprintf(shmname, "smpi_shared_malloc_%p", &*data);
+      fd = shm_open(shmname, O_RDWR | O_CREAT | O_EXCL,
                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
       if (fd < 0) {
         switch(errno) {
           case EEXIST:
-            xbt_die("Please cleanup /dev/shm/%s", loc);
+            xbt_die("Please cleanup /dev/shm/%s", shmname);
           default:
-            xbt_die("An unhandled error occured while opening %s. shm_open: %s", loc, strerror(errno));
+            xbt_die("An unhandled error occured while opening %s. shm_open: %s", shmname, strerror(errno));
         }
       }
-      data = xbt_new(shared_data_t, 1);
-      data->fd = fd;
-      data->count = 1;
-      data->loc = loc;
-      mem = shm_map(fd, size, data);
-      if (shm_unlink(loc) < 0) {
-        XBT_WARN("Could not early unlink %s. shm_unlink: %s", loc, strerror(errno));
+      data->second.fd = fd;
+      data->second.count = 1;
+      mem = shm_map(fd, size, &*data);
+      if (shm_unlink(shmname) < 0) {
+        XBT_WARN("Could not early unlink %s. shm_unlink: %s", shmname, strerror(errno));
       }
-      xbt_dict_set(allocs, loc, data, NULL);
-      XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
+      XBT_DEBUG("Mapping %s at %p through %d", shmname, mem, fd);
     } else {
-      xbt_free(loc);
-      mem = shm_map(data->fd, size, data);
-      data->count++;
+      mem = shm_map(data->second.fd, size, &*data);
+      data->second.count++;
     }
-    XBT_DEBUG("Shared malloc %zu in %p (metadata at %p)", size, mem, data);
+    XBT_DEBUG("Shared malloc %zu in %p (metadata at %p)", size, mem, &*data);
   } else {
     mem = xbt_malloc(size);
     XBT_DEBUG("Classic malloc %zu in %p", size, mem);
@@ -503,39 +520,27 @@ void *smpi_shared_malloc(size_t size, const char *file, int line)
 
   return mem;
 }
+
 void smpi_shared_free(void *ptr)
 {
   char loc[PTR_STRLEN];
-  shared_metadata_t* meta;
-  shared_data_t* data;
-  if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
 
-    if (!allocs) {
-      XBT_WARN("Cannot free: nothing was allocated");
-      return;
-    }
-    if(!allocs_metadata) {
-      XBT_WARN("Cannot free: no metadata was allocated");
-    }
+  if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
     snprintf(loc, PTR_STRLEN, "%p", ptr);
-    meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
-    if (!meta) {
+    auto meta = allocs_metadata.find(ptr);
+    if (meta == allocs_metadata.end()) {
       XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
       return;
     }
-    data = meta->data;
-    if(!data) {
-      XBT_WARN("Cannot free: something is broken in the metadata link");
-      return;
-    }
-    if(munmap(ptr, meta->size) < 0) {
+    shared_data_t* data = &meta->second.data->second;
+    if (munmap(ptr, meta->second.size) < 0) {
       XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
     }
     data->count--;
     XBT_DEBUG("Shared free - no removal - of %p, count = %d", ptr, data->count);
     if (data->count <= 0) {
       close(data->fd);
-      xbt_dict_remove(allocs, data->loc);
+      allocs.erase(allocs.find(meta->second.data->first));
       XBT_DEBUG("Shared free - with removal - of %p", ptr);
     }
   }else{
diff --git a/src/xbt/xbt_sha.c b/src/xbt/xbt_sha.c
deleted file mode 100644 (file)
index aa3dd12..0000000
+++ /dev/null
@@ -1,218 +0,0 @@
-/* xbt_sha.c - SHA1 hash function */
-
-/* Copyright (c) 2008-2014. 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. */
-
-/* Initial version part of iksemel (XML parser for Jabber)
- *   Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>. All right reserved.
- *   Distributed under LGPL v2.1, February 1999.
- */
-
-/* Later adapted to fit into SimGrid. Distributed under LGPL v2.1, Feb 1999.*/
-
-#include "xbt/sysdep.h"
-#include "xbt/hash.h"
-#include <stdio.h> /* sprintf */
-
-struct s_xbt_sha_ {
-  unsigned int hash[5];
-  unsigned int buf[80];
-  int blen;
-  unsigned int lenhi, lenlo;
-};
-static void sha_calculate(xbt_sha_t sha);
-
-/* ************** */
-/* User Interface */
-/* ************** */
-
-/** @brief constructor */
-xbt_sha_t xbt_sha_new(void)
-{
-  xbt_sha_t sha = xbt_new(s_xbt_sha_t, 1);
-  xbt_sha_reset(sha);
-  return sha;
-}
-
-/** @brief destructor */
-void xbt_sha_free(xbt_sha_t sha)
-{
-  free(sha);
-}
-
-void xbt_sha_reset(xbt_sha_t sha)
-{
-  memset(sha, 0, sizeof(s_xbt_sha_t));
-  sha->hash[0] = 0x67452301;
-  sha->hash[1] = 0xefcdab89;
-  sha->hash[2] = 0x98badcfe;
-  sha->hash[3] = 0x10325476;
-  sha->hash[4] = 0xc3d2e1f0;
-}
-
-/* @brief Add some more data to the buffer */
-void xbt_sha_feed(xbt_sha_t sha, const unsigned char *data, size_t len)
-{
-  unsigned int i;
-
-  for (i = 0; i < len; i++) {
-    sha->buf[sha->blen / 4] <<= 8;
-    sha->buf[sha->blen / 4] |= (unsigned int) data[i];
-    if ((++sha->blen) % 64 == 0) {
-      sha_calculate(sha);
-      sha->blen = 0;
-    }
-    sha->lenlo += 8;
-    sha->lenhi += (sha->lenlo < 8);
-  }
-}
-
-/* finalize computation before displaying the result */
-static void xbt_sha_finalize(xbt_sha_t sha)
-{
-  unsigned char pad[8];
-  unsigned char padc;
-
-  pad[0] = (unsigned char) ((sha->lenhi >> 24) & 0xff);
-  pad[1] = (unsigned char) ((sha->lenhi >> 16) & 0xff);
-  pad[2] = (unsigned char) ((sha->lenhi >> 8) & 0xff);
-  pad[3] = (unsigned char) (sha->lenhi & 0xff);
-  pad[4] = (unsigned char) ((sha->lenlo >> 24) & 0xff);
-  pad[5] = (unsigned char) ((sha->lenlo >> 16) & 0xff);
-  pad[6] = (unsigned char) ((sha->lenlo >> 8) & 0xff);
-  pad[7] = (unsigned char) (sha->lenlo & 255);
-
-  padc = 0x80;
-  xbt_sha_feed(sha, &padc, 1);
-
-  padc = 0x00;
-  while (sha->blen != 56)
-    xbt_sha_feed(sha, &padc, 1);
-
-  xbt_sha_feed(sha, pad, 8);
-}
-
-/** @brief returns the sha hash into a newly allocated buffer (+ reset sha object) */
-char *xbt_sha_read(xbt_sha_t sha)
-{
-  char *res = xbt_malloc(41);
-  xbt_sha_print(sha, res);
-  return res;
-}
-
-/** @brief copy the content sha hash into the @a hash pre-allocated string (and reset buffer) */
-void xbt_sha_print(xbt_sha_t sha, char *hash)
-{
-  int i;
-
-  xbt_sha_finalize(sha);
-  for (i = 0; i < 5; i++) {
-    sprintf(hash, "%08x", sha->hash[i]);
-    hash += 8;
-  }
-}
-
-
-/** @brief simply compute a SHA1 hash and copy it to the provided buffer */
-void xbt_sha(const char *data, char *hash)
-{
-  s_xbt_sha_t sha;
-
-  xbt_sha_reset(&sha);
-  xbt_sha_feed(&sha, (const unsigned char *) data, strlen(data));
-
-  xbt_sha_print(&sha, hash);
-}
-
-/* ********************* */
-/* Core of the algorithm */
-/* ********************* */
-
-#define SRL(x,y) (((x) << (y)) | ((x) >> (32-(y))))
-#define SHA(a,b,f,c) \
-  for (i= (a) ; i<= (b) ; i++) { \
-    TMP = SRL(A,5) + ( (f) ) + E + sha->buf[i] + (c) ; \
-    E = D; \
-    D = C; \
-    C = SRL(B,30); \
-    B = A; \
-    A = TMP; \
-  }
-
-static void sha_calculate(xbt_sha_t sha)
-{
-  int i;
-  unsigned int A, B, C, D, E, TMP;
-
-  for (i = 16; i < 80; i++)
-    sha->buf[i] =
-        SRL(sha->buf[i - 3] ^ sha->buf[i - 8] ^ sha->
-            buf[i - 14] ^ sha->buf[i - 16], 1);
-
-  A = sha->hash[0];
-  B = sha->hash[1];
-  C = sha->hash[2];
-  D = sha->hash[3];
-  E = sha->hash[4];
-
-  SHA(0, 19, ((C ^ D) & B) ^ D, 0x5a827999);
-  SHA(20, 39, B ^ C ^ D, 0x6ed9eba1);
-  SHA(40, 59, (B & C) | (D & (B | C)), 0x8f1bbcdc);
-  SHA(60, 79, B ^ C ^ D, 0xca62c1d6);
-
-  sha->hash[0] += A;
-  sha->hash[1] += B;
-  sha->hash[2] += C;
-  sha->hash[3] += D;
-  sha->hash[4] += E;
-}
-
-/* ************* */
-/* Testing stuff */
-/* ************* */
-#ifdef SIMGRID_TEST
-#include "xbt/hash.h"
-#include "src/portable.h"           /* hexa_str */
-
-static char *mycmp(const char *p1, const char *p2, size_t n)
-{
-  int i;
-
-  for (i = 0; i < n; i++) {
-    if (p1[i] != p2[i]) {
-      return bprintf("Differs on %d -- Ox%x", i, p1[i]);
-    }
-  }
-  return xbt_strdup("");
-}
-
-static void test_sha(const char *clear, const char *hashed)
-{
-  char hash[41];
-  xbt_sha(clear, hash);
-
-  xbt_test_add("==== Test with '%s'", clear);
-  xbt_test_assert(!memcmp(hash, hashed, 40), "Wrong sha: %40s!=%40s (%s)",
-                   hash, hashed, mycmp(hash, hashed, 40));
-}
-
-XBT_TEST_SUITE("hash", "Various hash functions");
-
-XBT_TEST_UNIT("sha", test_crypto_sha, "Test of the sha algorithm")
-{
-  /* Empty string as test vector */
-  test_sha("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
-
-  /* Some pangram as test vector */
-  test_sha("The quick brown fox jumps over the lazy dog",
-           "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
-  test_sha("Woven silk pyjamas exchanged for blue quartz",
-           "da3aff337c810c6470db4dbf0f205c8afc31c442");
-  test_sha("Pack my box with five dozen liquor jugs",
-           "373ba8be29d4d95708bf7cd43038f4e409dcb439");
-
-}
-#endif                          /* SIMGRID_TEST */
index bc04b32..37233cd 100644 (file)
@@ -270,7 +270,6 @@ set(XBT_SRC
   src/xbt/xbt_os_synchro.c
   src/xbt/xbt_os_time.c
   src/xbt/xbt_replay.c
-  src/xbt/xbt_sha.c
   src/xbt/xbt_str.c
   src/xbt/xbt_strbuff.c
   src/xbt/xbt_virtu.c
@@ -679,7 +678,6 @@ set(headers_to_install
   include/xbt/file.h
   include/xbt/function_types.h
   include/xbt/graph.h
-  include/xbt/hash.h
   include/xbt/heap.h
   include/xbt/lib.h
   include/xbt/Extendable.hpp
index fbcc115..e1cbe9c 100644 (file)
@@ -330,7 +330,6 @@ add_custom_target(maintainer-clean
   COMMAND ${CMAKE_COMMAND} -E remove -f src/set_unit.c
   COMMAND ${CMAKE_COMMAND} -E remove -f src/simgrid_units_main.c
   COMMAND ${CMAKE_COMMAND} -E remove -f src/swag_unit.c
-  COMMAND ${CMAKE_COMMAND} -E remove -f src/xbt_sha_unit.c
   COMMAND ${CMAKE_COMMAND} -E remove -f src/xbt_str_unit.c
   COMMAND ${CMAKE_COMMAND} -E remove -f src/xbt_strbuff_unit.c
   COMMAND ${CMAKE_COMMAND} -E remove -f src/xbt_synchro_unit.c
index cbbd15c..c7dc531 100644 (file)
@@ -9,7 +9,6 @@ set(TEST_CFILES
   src/xbt/swag.c
   src/xbt/xbt_str.c
   src/xbt/xbt_strbuff.c
-  src/xbt/xbt_sha.c
   src/xbt/config.c
   )
 set(TEST_UNITS
@@ -20,7 +19,6 @@ set(TEST_UNITS
   ${CMAKE_CURRENT_BINARY_DIR}/src/swag_unit.c
   ${CMAKE_CURRENT_BINARY_DIR}/src/xbt_str_unit.c
   ${CMAKE_CURRENT_BINARY_DIR}/src/xbt_strbuff_unit.c
-  ${CMAKE_CURRENT_BINARY_DIR}/src/xbt_sha_unit.c
   ${CMAKE_CURRENT_BINARY_DIR}/src/config_unit.c
 
   ${CMAKE_CURRENT_BINARY_DIR}/src/simgrid_units_main.c