1 /* Copyright (c) 2017-2022. 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. */
8 #include "src/kernel/resource/Resource.hpp"
9 #include "src/kernel/resource/profile/Event.hpp"
10 #include "simgrid/kernel/ProfileBuilder.hpp"
11 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
12 #include "src/surf/surf_interface.hpp"
16 #include "xbt/random.hpp"
20 XBT_LOG_NEW_DEFAULT_CATEGORY(unit, "Unit tests of the Trace Manager");
22 class MockedResource : public simgrid::kernel::resource::Resource {
24 static double the_date;
26 explicit MockedResource() : Resource("fake"){}
27 void apply_event(simgrid::kernel::profile::Event* event, double value) override
29 XBT_VERB("t=%.1f: Change value to %lg (idx: %u)", the_date, value, event->idx);
30 tmgr_trace_event_unref(&event);
32 bool is_used() const override { return true; }
35 double MockedResource::the_date;
37 static std::vector<simgrid::kernel::profile::DatedValue> trace2vector(const char* str)
39 std::vector<simgrid::kernel::profile::DatedValue> res;
40 simgrid::kernel::profile::Profile* trace = simgrid::kernel::profile::ProfileBuilder::from_string("TheName", str, 0);
41 XBT_VERB("---------------------------------------------------------");
42 XBT_VERB("data>>\n%s<<data\n", str);
43 for (auto const& evt : trace->get_event_list())
44 XBT_VERB("event: d:%lg v:%lg", evt.date_, evt.value_);
46 MockedResource daResource;
47 simgrid::kernel::profile::FutureEvtSet fes;
48 simgrid::kernel::profile::Event* insertedIt = trace->schedule(&fes, &daResource);
50 while (fes.next_date() <= 20.0 && fes.next_date() >= 0) {
51 MockedResource::the_date = fes.next_date();
53 simgrid::kernel::resource::Resource* resource;
54 simgrid::kernel::profile::Event* it = fes.pop_leq(MockedResource::the_date, &value, &resource);
58 REQUIRE(it == insertedIt); // Check that we find what we've put
60 res.emplace_back(MockedResource::the_date, value);
62 XBT_DEBUG("%.1f: ignore an event (idx: %u)\n", MockedResource::the_date, it->idx);
64 resource->apply_event(it, value);
70 TEST_CASE("kernel::profile: Resource profiles, defining the external load", "kernel::profile")
72 SECTION("No event, no loop")
74 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("");
75 std::vector<simgrid::kernel::profile::DatedValue> want;
79 SECTION("One event no loop")
81 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("9.0 3.0\n");
83 std::vector<simgrid::kernel::profile::DatedValue> want;
84 want.emplace_back(9, 3);
88 SECTION("Two events, no loop")
90 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
93 std::vector<simgrid::kernel::profile::DatedValue> want;
94 want.emplace_back(3, 1);
95 want.emplace_back(9, 3);
100 SECTION("Three events, no loop")
102 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("3.0 1.0\n"
106 std::vector<simgrid::kernel::profile::DatedValue> want;
107 want.emplace_back(3, 1);
108 want.emplace_back(5, 2);
109 want.emplace_back(9, 3);
111 REQUIRE(want == got);
114 SECTION("Two events, looping")
116 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("1.0 1.0\n"
120 std::vector<simgrid::kernel::profile::DatedValue> want;
121 want.emplace_back(1, 1);
122 want.emplace_back(3, 3);
123 want.emplace_back(6, 1);
124 want.emplace_back(8, 3);
125 want.emplace_back(11, 1);
126 want.emplace_back(13, 3);
127 want.emplace_back(16, 1);
128 want.emplace_back(18, 3);
130 REQUIRE(want == got);
133 SECTION("Two events, looping, start at 0")
135 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("0.0 1\n"
139 std::vector<simgrid::kernel::profile::DatedValue> want;
140 want.emplace_back(0, 1);
141 want.emplace_back(5, 2);
142 want.emplace_back(10, 1);
143 want.emplace_back(15, 2);
144 want.emplace_back(20, 1);
146 REQUIRE(want == got);
149 SECTION("One stochastic event (parsing)")
151 using simgrid::kernel::profile::Distribution;
152 std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
155 std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
156 want.emplace_back(Distribution::DET, std::vector<double>{0}, Distribution::UNIF, std::vector<double>{10, 20});
158 REQUIRE(want == got);
161 SECTION("Several stochastic events (all possible parsing forms)")
163 using simgrid::kernel::profile::Distribution;
164 std::vector<simgrid::kernel::profile::StochasticDatedValue> got = trace2selist("STOCHASTIC\n"
166 "NORMAL 25 10 DET 3\n"
167 "UNIF 10 20 NORMAL 25 10\n"
170 std::vector<simgrid::kernel::profile::StochasticDatedValue> want;
171 want.emplace_back(Distribution::DET, std::vector<double>{0}, Distribution::DET, std::vector<double>{4});
172 want.emplace_back(Distribution::NORM, std::vector<double>{25, 10}, Distribution::DET, std::vector<double>{3});
173 want.emplace_back(Distribution::UNIF, std::vector<double>{10, 20}, Distribution::NORM, std::vector<double>{25, 10});
174 want.emplace_back(Distribution::DET, std::vector<double>{5}, Distribution::UNIF, std::vector<double>{5, 25});
176 REQUIRE(want == got);
179 SECTION("Two stochastic events (drawing each distribution)")
181 simgrid::xbt::random::set_implem_xbt();
182 simgrid::xbt::random::set_mersenne_seed(12345);
183 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC\n"
185 "EXP 0.05 NORMAL 15 5");
187 std::vector<simgrid::kernel::profile::DatedValue> want;
188 // The following values were drawn using the XBT_RNG_xbt method /outside/ the testcase.
189 want.emplace_back(0, 19.29616086867082813683);
190 want.emplace_back(2.32719992449416279712, 20.16807234800742065772);
192 REQUIRE(want == got);
195 SECTION("Two stochastic events, with a loop")
197 simgrid::xbt::random::set_implem_xbt();
198 simgrid::xbt::random::set_mersenne_seed(12345);
199 std::vector<simgrid::kernel::profile::DatedValue> got = trace2vector("STOCHASTIC LOOP\n"
201 "EXP 0.05 NORMAL 15 5\n"
204 // In this case, the main use of the last stochastic event is to set when the first event takes place.
206 std::vector<simgrid::kernel::profile::DatedValue> want;
207 want.emplace_back(0, 19.29616086867082813683);
208 want.emplace_back(2.32719992449416279712, 20.16807234800742065772);
209 want.emplace_back(3.51111873684917075167, 0);
210 want.emplace_back(3.51111873684917075167, 10.39759496468994726115);
212 REQUIRE(want == got);