Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / resource / profile / ProfileBuilder.cpp
index ba7a225..83c587d 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2004-2023. 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. */
@@ -7,18 +7,18 @@
 #include "simgrid/forward.h"
 #include "src/kernel/resource/profile/Profile.hpp"
 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
-#include "src/surf/surf_interface.hpp"
+#include "xbt/asserts.h"
+#include "xbt/file.hpp"
 
 #include <boost/algorithm/string.hpp>
 #include <boost/intrusive/options.hpp>
 #include <cstddef>
 #include <fstream>
+#include <math.h>
 #include <sstream>
+#include <string_view>
 
-
-namespace simgrid {
-namespace kernel {
-namespace profile {
+namespace simgrid::kernel::profile {
 
 bool DatedValue::operator==(DatedValue const& e2) const
 {
@@ -72,19 +72,19 @@ class LegacyUpdateCb {
   bool loop;
   double loop_delay = 0.0;
 
-  static bool is_comment_or_empty_line(const std::string& val)
+  static bool is_comment_or_empty_line(std::string_view val)
   {
-    return (val[0] == '#' || val[0] == '\0' || val[0] == '%');
+    return (val.empty() || val.front() == '#' || val.front() == '%');
   }
 
-  static bool is_normal_distribution(const std::string& val)
+  static bool is_normal_distribution(std::string_view val)
   {
     return (val == "NORM" || val == "NORMAL" || val == "GAUSS" || val == "GAUSSIAN");
   }
 
-  static bool is_exponential_distribution(const std::string& val) { return (val == "EXP" || val == "EXPONENTIAL"); }
+  static bool is_exponential_distribution(std::string_view val) { return (val == "EXP" || val == "EXPONENTIAL"); }
 
-  static bool is_uniform_distribution(const std::string& val) { return (val == "UNIF" || val == "UNIFORM"); }
+  static bool is_uniform_distribution(std::string_view val) { return (val == "UNIF" || val == "UNIFORM"); }
 
 public:
   LegacyUpdateCb(const std::string& input, double periodicity) : loop(periodicity > 0)
@@ -214,6 +214,8 @@ public:
         event_list.at(initial_size).date_ += loop_delay;
     }
   }
+
+  std::vector<StochasticDatedValue> get_pattern() const { return pattern; }
 };
 
 Profile* ProfileBuilder::from_string(const std::string& name, const std::string& input, double periodicity)
@@ -222,30 +224,36 @@ Profile* ProfileBuilder::from_string(const std::string& name, const std::string&
   return new Profile(name,cb,cb.get_repeat_delay());
 }
 
-Profile* ProfileBuilder::from_file(const std::string& path)
+Profile* ProfileBuilder::from_file(const std::string& filename)
 {
-  xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
-  auto f = std::unique_ptr<std::ifstream>(surf_ifsopen(path));
-  xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
+  xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename");
+  auto f = std::unique_ptr<std::ifstream>(simgrid::xbt::path_ifsopen(filename));
+  xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
+             simgrid::xbt::path_to_string().c_str());
 
   std::stringstream buffer;
   buffer << f->rdbuf();
 
   LegacyUpdateCb cb(buffer.str(), -1);
-  return new Profile(path,cb,cb.get_repeat_delay());
+  return new Profile(filename, cb, cb.get_repeat_delay());
 }
 
 
 Profile* ProfileBuilder::from_void() {
-  static Profile void_profile("__void__",[](std::vector<DatedValue>&){},-1.0);
-  return &void_profile;
+  static auto* void_profile = new Profile("__void__", nullptr, -1.0);
+  return void_profile;
 }
 
 Profile* ProfileBuilder::from_callback(const std::string& name, const std::function<UpdateCb>& cb, double repeat_delay) {
   return new Profile(name, cb, repeat_delay);
 }
 
+} // namespace simgrid::kernel::profile
+
+std::vector<simgrid::kernel::profile::StochasticDatedValue> trace2selist( const char*  c_str) {
+  std::string str(c_str);
+  simgrid::kernel::profile::LegacyUpdateCb cb(str,0);
+  return cb.get_pattern();
+}
+
 
-} // namespace profile
-} // namespace kernel
-} // namespace simgrid