1 /* Copyright (c) 2016. 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 struct SimulationClock {
42 using period = std::ratio<1>;
43 using duration = std::chrono::duration<rep, period>;
44 using time_point = std::chrono::time_point<SimulationClock, duration>;
45 static constexpr bool is_steady = true;
46 static time_point now()
48 return time_point(duration(SIMIX_get_clock()));
52 /** Default duration for simulated time */
53 using SimulationClockDuration = SimulationClock::duration;
55 /** Default time point for simulated time */
56 using SimulationClockTimePoint = SimulationClock::time_point;
58 // Durations based on doubles:
59 using nanoseconds = std::chrono::duration<double, std::nano>;
60 using microseconds = std::chrono::duration<double, std::micro>;
61 using milliseconds = std::chrono::duration<double, std::milli>;
62 using seconds = std::chrono::duration<double>;
63 using minutes = std::chrono::duration<double, std::ratio<60>>;
64 using hours = std::chrono::duration<double, std::ratio<3600>>;
66 /** A time point in the simulated time */
67 template<class Duration>
68 using SimulationTimePoint = std::chrono::time_point<SimulationClock, Duration>;