1 /* Copyright (c) 2016-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_CHRONO_HPP
7 #define SIMGRID_CHRONO_HPP
9 /** @file chrono.hpp Time support
11 * Define clock, duration types, time point types compatible with the standard
18 #include <simgrid/simix.h>
22 /** @brief A C++ compatible [TrivialClock](http://en.cppreference.com/w/cpp/concept/TrivialClock) working with simulated-time.
24 * SimGrid uses `double` for representing the simulated time, where *durations* are expressed in seconds
25 * (with *infinite duration* expressed as a negative value) and
26 * *timepoints* are expressed as seconds from the beginning of the simulation.
27 * In contrast, all the C++ APIs use the much more sensible `std::chrono::duration` and`std::chrono::time_point`.
29 * This class can be used to build `std::chrono` objects that use simulated time,
30 * using #SimulationClockDuration and #SimulationClockTimePoint.
32 * This means it is possible to use (since C++14):
35 * using namespace std::chrono_literals;
36 * simgrid::s4u::actor::sleep_for(42s);
40 class SimulationClock {
43 using period = std::ratio<1>;
44 using duration = std::chrono::duration<rep, period>;
45 using time_point = std::chrono::time_point<SimulationClock, duration>;
46 static constexpr bool is_steady = true;
47 static time_point now()
49 return time_point(duration(SIMIX_get_clock()));
53 /** Default duration for simulated time */
54 using SimulationClockDuration = SimulationClock::duration;
56 /** Default time point for simulated time */
57 using SimulationClockTimePoint = SimulationClock::time_point;
59 // Durations based on doubles:
60 using nanoseconds = std::chrono::duration<double, std::nano>;
61 using microseconds = std::chrono::duration<double, std::micro>;
62 using milliseconds = std::chrono::duration<double, std::milli>;
63 using seconds = std::chrono::duration<double>;
64 using minutes = std::chrono::duration<double, std::ratio<60>>;
65 using hours = std::chrono::duration<double, std::ratio<3600>>;
67 /** A time point in the simulated time */
68 template<class Duration>
69 using SimulationTimePoint = std::chrono::time_point<SimulationClock, Duration>;