Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a1e5f865814e5984a8dd10bb329f4a3c81498507
[simgrid.git] / examples / s4u / plugin-hostload / s4u-plugin-hostload.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/s4u.hpp"
7 #include "simgrid/plugins/load.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10
11 static void execute_load_test()
12 {
13   s4u_Host* host = simgrid::s4u::Host::by_name("MyHost1");
14
15   XBT_INFO("Initial peak speed: %.0E flop/s; number of flops computed so far: %.0E (should be 0) and current average "
16            "load: %.5f (should be 0)",
17            host->get_speed(), sg_host_get_computed_flops(host), sg_host_get_avg_load(host));
18
19   double start = simgrid::s4u::Engine::get_clock();
20   XBT_INFO("Sleep for 10 seconds");
21   simgrid::s4u::this_actor::sleep_for(10);
22
23   double speed = host->get_speed();
24   XBT_INFO("Done sleeping %.2fs; peak speed: %.0E flop/s; number of flops computed so far: %.0E (nothing should have "
25            "changed)",
26            simgrid::s4u::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
27
28   // Run a task
29   start = simgrid::s4u::Engine::get_clock();
30   XBT_INFO("Run a task of %.0E flops at current speed of %.0E flop/s", 200E6, host->get_speed());
31   simgrid::s4u::this_actor::execute(200E6);
32
33   XBT_INFO("Done working on my task; this took %.2fs; current peak speed: %.0E flop/s (when I started the computation, "
34            "the speed was set to %.0E flop/s); number of flops computed so "
35            "far: %.2E, average load as reported by the HostLoad plugin: %.5f (should be %.5f)",
36            simgrid::s4u::Engine::get_clock() - start, host->get_speed(), speed, sg_host_get_computed_flops(host),
37            sg_host_get_avg_load(host),
38            static_cast<double>(200E6) /
39                (10.5 * speed * host->get_core_count() +
40                 (simgrid::s4u::Engine::get_clock() - start - 0.5) * host->get_speed() * host->get_core_count()));
41
42   // ========= Change power peak =========
43   int pstate = 1;
44   host->set_pstate(pstate);
45   XBT_INFO(
46       "========= Requesting pstate %d (speed should be of %.0E flop/s and is of %.0E flop/s, average load is %.5f)",
47       pstate, host->get_pstate_speed(pstate), host->get_speed(), sg_host_get_avg_load(host));
48
49   // Run a second task
50   start = simgrid::s4u::Engine::get_clock();
51   XBT_INFO("Run a task of %.0E flops", 100E6);
52   simgrid::s4u::this_actor::execute(100E6);
53   XBT_INFO("Done working on my task; this took %.2fs; current peak speed: %.0E flop/s; number of flops computed so "
54            "far: %.2E",
55            simgrid::s4u::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
56
57   start = simgrid::s4u::Engine::get_clock();
58   XBT_INFO("========= Requesting a reset of the computation and load counters");
59   sg_host_load_reset(host);
60   XBT_INFO("After reset: %.0E flops computed; load is %.5f", sg_host_get_computed_flops(host), sg_host_get_avg_load(host));
61   XBT_INFO("Sleep for 4 seconds");
62   simgrid::s4u::this_actor::sleep_for(4);
63   XBT_INFO("Done sleeping %.2f s; peak speed: %.0E flop/s; number of flops computed so far: %.0E",
64            simgrid::s4u::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
65
66   // =========== Turn the other host off ==========
67   s4u_Host* host2 = simgrid::s4u::Host::by_name("MyHost2");
68   XBT_INFO("Turning MyHost2 off, and sleeping another 10 seconds. MyHost2 computed %.0f flops so far and has an average load of %.5f.",
69            sg_host_get_computed_flops(host2), sg_host_get_avg_load(host2));
70   host2->turn_off();
71   start = simgrid::s4u::Engine::get_clock();
72   simgrid::s4u::this_actor::sleep_for(10);
73   XBT_INFO("Done sleeping %.2f s; peak speed: %.0E flop/s; number of flops computed so far: %.0E",
74            simgrid::s4u::Engine::get_clock() - start, host->get_speed(), sg_host_get_computed_flops(host));
75 }
76
77 static void change_speed()
78 {
79   s4u_Host* host = simgrid::s4u::Host::by_name("MyHost1");
80   simgrid::s4u::this_actor::sleep_for(10.5);
81   XBT_INFO("I slept until now, but now I'll change the speed of this host "
82       "while the other process is still computing! This should slow the computation down.");
83   host->set_pstate(2);
84 }
85
86 int main(int argc, char* argv[])
87 {
88   sg_host_load_plugin_init();
89   simgrid::s4u::Engine e(&argc, argv);
90
91   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s ../platforms/energy_platform.xml\n", argv[0], argv[0]);
92   e.load_platform(argv[1]);
93
94   simgrid::s4u::Actor::create("load_test", simgrid::s4u::Host::by_name("MyHost1"), execute_load_test);
95   simgrid::s4u::Actor::create("change_speed", simgrid::s4u::Host::by_name("MyHost1"), change_speed);
96
97   e.run();
98
99   XBT_INFO("Total simulation time: %.2f", simgrid::s4u::Engine::get_clock());
100
101   return 0;
102 }