Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make Process:cache_flags private
[simgrid.git] / src / mc / Process.hpp
index af42a12..2b53837 100644 (file)
@@ -7,56 +7,99 @@
 #ifndef SIMGRID_MC_PROCESS_H
 #define SIMGRID_MC_PROCESS_H
 
-#include <type_traits>
-
-#include <sys/types.h>
+#include <cstdint>
+#include <cstddef>
 
+#include <type_traits>
 #include <vector>
 #include <memory>
 
-#include "simgrid_config.h"
 #include <sys/types.h>
 
+#include <simgrid_config.h>
+
 #include <xbt/base.h>
+#include <xbt/dynar.h>
 #include <xbt/mmalloc.h>
 
-#ifdef HAVE_MC
-#include "xbt/mmalloc/mmprivate.h"
-#endif
+#include "src/xbt/mmalloc/mmprivate.h"
+#include "src/mc/Channel.hpp"
 
 #include <simgrid/simix.h>
-#include "simix/popping_private.h"
-#include "simix/smx_private.h"
-
-#include "mc_forward.hpp"
-#include "mc_base.h"
-#include "mc_mmalloc.h" // std_heap
-#include "mc_memory_map.h"
-#include "AddressSpace.hpp"
-#include "mc_protocol.h"
-
-// Those flags are used to track down which cached information
-// is still up to date and which information needs to be updated.
-typedef int mc_process_cache_flags_t;
-#define MC_PROCESS_CACHE_FLAG_NONE 0
-#define MC_PROCESS_CACHE_FLAG_HEAP 1
-#define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
-#define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
+#include "src/simix/popping_private.h"
+#include "src/simix/smx_private.h"
+
+#include "src/xbt/memory_map.hpp"
+
+#include "src/mc/mc_forward.hpp"
+#include "src/mc/mc_base.h"
+#include "src/mc/AddressSpace.hpp"
+#include "src/mc/mc_protocol.h"
+#include "src/mc/ObjectInformation.hpp"
+
 
 namespace simgrid {
 namespace mc {
 
+class SimixProcessInformation {
+public:
+  /** MCed address of the process */
+  void* address = nullptr;
+  union {
+    /** (Flat) Copy of the process data structure */
+    struct s_smx_process copy;
+  };
+  /** Hostname (owned by `mc_modelchecker->hostnames`) */
+  const char* hostname = nullptr;
+  std::string name;
+
+  void clear()
+  {
+    name.clear();
+    address = nullptr;
+    hostname = nullptr;
+  }
+};
+
 struct IgnoredRegion {
   std::uint64_t addr;
-  size_t size;
+  std::size_t size;
+};
+
+struct IgnoredHeapRegion {
+  int block;
+  int fragment;
+  void *address;
+  std::size_t size;
 };
 
 /** Representation of a process
+ *
+ *  This class is mixing a lot of differents responsabilities and is tied
+ *  to SIMIX. It should probably split into different classes.
+ *
+ *  Responsabilities:
+ *
+ *  - reading from the process memory (`AddressSpace`);
+ *  - accessing the system state of the porcess (heap, …);
+ *  - storing the SIMIX state of the process;
+ *  - privatization;
+ *  - communication with the model-checked process;
+ *  - stack unwinding;
+ *  - etc.
  */
 class Process final : public AddressSpace {
+private:
+  // Those flags are used to track down which cached information
+  // is still up to date and which information needs to be updated.
+  static constexpr int cache_none = 0;
+  static constexpr int cache_heap = 1;
+  static constexpr int cache_malloc = 2;
+  static constexpr int cache_simix_processes = 4;
 public:
   Process(pid_t pid, int sockfd);
   ~Process();
+  void init();
 
   Process(Process const&) = delete;
   Process(Process &&) = delete;
@@ -65,8 +108,8 @@ public:
 
   // Read memory:
   const void* read_bytes(void* buffer, std::size_t size,
-    remote_ptr<void> address, int process_index = ProcessIndexAny,
-    ReadMode mode = Normal) const override;
+    RemotePtr<void> address, int process_index = ProcessIndexAny,
+    ReadOptions options = ReadOptions::none()) const override;
   void read_variable(const char* name, void* target, size_t size) const;
   template<class T>
   T read_variable(const char *name) const
@@ -76,33 +119,41 @@ public:
     read_variable(name, &res, sizeof(T));
     return res;
   }
-  char* read_string(remote_ptr<void> address) const;
+  char* read_string(RemotePtr<void> address) const;
 
   // Write memory:
-  void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
-  void clear_bytes(remote_ptr<void> address, size_t len);
+  void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
+  void clear_bytes(RemotePtr<void> address, size_t len);
 
   // Debug information:
-  std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
-  std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
-  std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
-  simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
+  std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
+  std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
+  std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
+  simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
   simgrid::mc::Variable* find_variable(const char* name) const;
 
   // Heap access:
   xbt_mheap_t get_heap()
   {
-    if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
+    if (!(this->cache_flags_ & Process::cache_heap))
       this->refresh_heap();
-    return this->heap;
+    return this->heap.get();
   }
   malloc_info* get_malloc_info()
   {
-    if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
+    if (!(this->cache_flags_ & Process::cache_malloc))
       this->refresh_malloc_info();
-    return this->heap_info;
+    return this->heap_info.data();
+  }
+  
+  void clear_cache()
+  {
+    this->cache_flags_ = Process::cache_none;
   }
 
+  Channel const& getChannel() const { return channel_; }
+  Channel& getChannel() { return channel_; }
+
   std::vector<IgnoredRegion> const& ignored_regions() const
   {
     return ignored_regions_;
@@ -111,7 +162,7 @@ public:
 
   pid_t pid() const { return pid_; }
 
-  bool in_maestro_stack(remote_ptr<void> p) const
+  bool in_maestro_stack(RemotePtr<void> p) const
   {
     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
   }
@@ -121,54 +172,71 @@ public:
     return running_;
   }
 
-  void terminate(int status)
+  void terminate()
   {
-    status_ = status;
     running_ = false;
   }
 
-  int status() const
+  void reset_soft_dirty();
+  void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
+
+  bool privatized(ObjectInformation const& info) const
   {
-    return status_;
+    return privatized_ && info.executable();
   }
+  bool privatized() const
+  {
+    return privatized_;
+  }
+  void privatized(bool privatized) { privatized_ = privatized; }
 
-  template<class M>
-  typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
-  send_message(M const& m)
+  void ignore_global_variable(const char* name)
   {
-    return MC_protocol_send(this->socket_, &m, sizeof(M));
+    for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
+        this->object_infos)
+      info->remove_global_variable(name);
   }
 
-  int send_message(e_mc_message_type message_id)
+  std::vector<s_stack_region_t>& stack_areas()
+  {
+    return stack_areas_;
+  }
+  std::vector<s_stack_region_t> const& stack_areas() const
   {
-    return MC_protocol_send_simple_message(this->socket_, message_id);
+    return stack_areas_;
   }
 
-  template<class M>
-  typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
-  receive_message(M& m)
+  std::vector<IgnoredHeapRegion> const& ignored_heap() const
   {
-    return MC_receive_message(this->socket_, &m, sizeof(M), 0);
+    return ignored_heap_;
   }
+  void ignore_heap(IgnoredHeapRegion const& region);
+  void unignore_heap(void *address, size_t size);
 
-  void reset_soft_dirty();
-  void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
+  void ignore_local_variable(const char *var_name, const char *frame_name);
+  std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
+  std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
 
 private:
   void init_memory_map_info();
   void refresh_heap();
   void refresh_malloc_info();
+  void refresh_simix();
+
 private:
-  pid_t pid_;
-  int socket_;
-  int status_;
-  bool running_;
-  std::vector<VmMap> memory_map_;
-  remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
-  int memory_file;
+  pid_t pid_ = -1;
+  Channel channel_;
+  bool running_ = false;
+  std::vector<simgrid::xbt::VmMap> memory_map_;
+  RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
+  int memory_file = -1;
   std::vector<IgnoredRegion> ignored_regions_;
-  int clear_refs_fd_;
-  int pagemap_fd_;
+  int clear_refs_fd_ = -1;
+  int pagemap_fd_ = -1;
+  bool privatized_ = false;
+  std::vector<s_stack_region_t> stack_areas_;
+  std::vector<IgnoredHeapRegion> ignored_heap_;
+
 public: // object info
   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
@@ -180,17 +248,19 @@ public: // Copies of MCed SMX data structures
    *
    *  See mc_smx.c.
    */
-  xbt_dynar_t smx_process_infos;
+  std::vector<SimixProcessInformation> smx_process_infos;
 
   /** Copy of `simix_global->process_to_destroy`
    *
    *  See mc_smx.c.
    */
-  xbt_dynar_t smx_old_process_infos;
+  std::vector<SimixProcessInformation> smx_old_process_infos;
 
+private:
   /** State of the cache (which variables are up to date) */
-  mc_process_cache_flags_t cache_flags;
+  int cache_flags_ = Process::cache_none;
 
+public:
   /** Address of the heap structure in the MCed process. */
   void* heap_address;
 
@@ -200,7 +270,7 @@ public: // Copies of MCed SMX data structures
    *  This is not used if the process is the current one:
    *  use `get_heap_info()` in order to use it.
    */
-   xbt_mheap_t heap;
+   std::unique_ptr<s_xbt_mheap_t> heap;
 
   /** Copy of the allocation info structure
    *
@@ -208,7 +278,7 @@ public: // Copies of MCed SMX data structures
    *  This is not used if the process is the current one:
    *  use `get_malloc_info()` in order to use it.
    */
-  malloc_info* heap_info;
+  std::vector<malloc_info> heap_info;
 
 public: // Libunwind-data
 
@@ -240,10 +310,4 @@ XBT_PRIVATE int open_vm(pid_t pid, int flags);
 }
 }
 
-SG_BEGIN_DECL()
-
-XBT_PRIVATE void MC_invalidate_cache(void);
-
-SG_END_DECL()
-
 #endif