1 /* Copyright (c) 2013-2019. 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. */
7 #include "src/kernel/resource/profile/trace_mgr.hpp"
8 #include "src/surf/surf_interface.hpp"
9 #include "surf/surf.hpp"
11 #define EPSILON 0.000000001
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf_cpu, "Logging specific to the SURF CPU TRACE INTEGRATION module");
22 CpuTiProfile::CpuTiProfile(kernel::profile::Profile* profile)
27 nb_points_ = profile->event_list.size() + 1;
28 time_points_ = new double[nb_points_];
29 integral_ = new double[nb_points_];
30 for (auto const& val : profile->event_list) {
31 time_points_[i] = time;
32 integral_[i] = integral;
33 integral += val.date_ * val.value_;
37 time_points_[i] = time;
38 integral_[i] = integral;
41 CpuTiProfile::~CpuTiProfile()
43 delete[] time_points_;
47 CpuTiTmgr::~CpuTiTmgr()
53 * @brief Integrate trace
55 * Wrapper around surf_cpu_integrate_trace_simple() to get
58 * @param a Begin of interval
59 * @param b End of interval
60 * @return the integrate value. -1 if an error occurs.
62 double CpuTiTmgr::integrate(double a, double b)
64 if ((a < 0.0) || (a > b)) {
65 xbt_die("Error, invalid integration interval [%.2f,%.2f]. "
66 "You probably have a task executing with negative computation amount. Check your code.", a, b);
68 if (fabs(a - b) < EPSILON)
71 if (type_ == Type::FIXED) {
72 return (b - a) * value_;
76 if (fabs(ceil(a / last_time_) - a / last_time_) < EPSILON)
77 a_index = 1 + static_cast<int>(ceil(a / last_time_));
79 a_index = static_cast<int>(ceil(a / last_time_));
81 int b_index = static_cast<int>(floor(b / last_time_));
83 if (a_index > b_index) { /* Same chunk */
84 return profile_->integrate_simple(a - (a_index - 1) * last_time_, b - (b_index)*last_time_);
87 double first_chunk = profile_->integrate_simple(a - (a_index - 1) * last_time_, last_time_);
88 double middle_chunk = (b_index - a_index) * total_;
89 double last_chunk = profile_->integrate_simple(0.0, b - (b_index)*last_time_);
91 XBT_DEBUG("first_chunk=%.2f middle_chunk=%.2f last_chunk=%.2f\n", first_chunk, middle_chunk, last_chunk);
93 return (first_chunk + middle_chunk + last_chunk);
97 * @brief Auxiliary function to compute the integral between a and b.
98 * It simply computes the integrals at point a and b and returns the difference between them.
99 * @param a Initial point
100 * @param b Final point
102 double CpuTiProfile::integrate_simple(double a, double b)
104 return integrate_simple_point(b) - integrate_simple_point(a);
108 * @brief Auxiliary function to compute the integral at point a.
111 double CpuTiProfile::integrate_simple_point(double a)
115 int ind = binary_search(time_points_, a, 0, nb_points_ - 1);
116 integral += integral_[ind];
118 XBT_DEBUG("a %f ind %d integral %f ind + 1 %f ind %f time +1 %f time %f", a, ind, integral, integral_[ind + 1],
119 integral_[ind], time_points_[ind + 1], time_points_[ind]);
120 double_update(&a_aux, time_points_[ind], sg_maxmin_precision * sg_surf_precision);
123 ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind])) * (a - time_points_[ind]);
124 XBT_DEBUG("Integral a %f = %f", a, integral);
130 * @brief Computes the time needed to execute "amount" on cpu.
132 * Here, amount can span multiple trace periods
134 * @param a Initial time
135 * @param amount Amount to be executed
138 double CpuTiTmgr::solve(double a, double amount)
140 /* Fix very small negative numbers */
141 if ((a < 0.0) && (a > -EPSILON)) {
144 if ((amount < 0.0) && (amount > -EPSILON)) {
149 if ((a < 0.0) || (amount < 0.0)) {
150 XBT_CRITICAL ("Error, invalid parameters [a = %.2f, amount = %.2f]. "
151 "You probably have a task executing with negative computation amount. Check your code.", a, amount);
155 /* At this point, a and amount are positive */
156 if (amount < EPSILON)
159 /* Is the trace fixed ? */
160 if (type_ == Type::FIXED) {
161 return (a + (amount / value_));
164 XBT_DEBUG("amount %f total %f", amount, total_);
165 /* Reduce the problem to one where amount <= trace_total */
166 int quotient = static_cast<int>(floor(amount / total_));
167 double reduced_amount = (total_) * ((amount / total_) - floor(amount / total_));
168 double reduced_a = a - (last_time_) * static_cast<int>(floor(a / last_time_));
170 XBT_DEBUG("Quotient: %d reduced_amount: %f reduced_a: %f", quotient, reduced_amount, reduced_a);
172 /* Now solve for new_amount which is <= trace_total */
174 XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", reduced_a, reduced_amount);
175 double amount_till_end = integrate(reduced_a, last_time_);
177 if (amount_till_end > reduced_amount) {
178 reduced_b = profile_->solve_simple(reduced_a, reduced_amount);
180 reduced_b = last_time_ + profile_->solve_simple(0.0, reduced_amount - amount_till_end);
183 /* Re-map to the original b and amount */
184 return (last_time_) * static_cast<int>(floor(a / last_time_)) + (quotient * last_time_) + reduced_b;
188 * @brief Auxiliary function to solve integral.
189 * It returns the date when the requested amount of flops is available
190 * @param a Initial point
191 * @param amount Amount of flops
192 * @return The date when amount is available.
194 double CpuTiProfile::solve_simple(double a, double amount)
196 double integral_a = integrate_simple_point(a);
197 int ind = binary_search(integral_, integral_a + amount, 0, nb_points_ - 1);
198 double time = time_points_[ind];
199 time += (integral_a + amount - integral_[ind]) /
200 ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind]));
206 * @brief Auxiliary function to update the CPU speed scale.
208 * This function uses the trace structure to return the speed scale at the determined time a.
210 * @return CPU speed scale
212 double CpuTiTmgr::get_power_scale(double a)
214 double reduced_a = a - floor(a / last_time_) * last_time_;
215 int point = profile_->binary_search(profile_->time_points_, reduced_a, 0, profile_->nb_points_ - 1);
216 kernel::profile::DatedValue val = speed_profile_->event_list.at(point);
221 * @brief Creates a new integration trace from a tmgr_trace_t
223 * @param speed_trace CPU availability trace
224 * @param value Percentage of CPU speed available (useful to fixed tracing)
225 * @return Integration trace structure
227 CpuTiTmgr::CpuTiTmgr(kernel::profile::Profile* speed_profile, double value) : speed_profile_(speed_profile)
229 double total_time = 0.0;
232 /* no availability file, fixed trace */
233 if (not speed_profile) {
236 XBT_DEBUG("No availability trace. Constant value = %f", value);
240 /* only one point available, fixed trace */
241 if (speed_profile->event_list.size() == 1) {
243 value_ = speed_profile->event_list.front().value_;
247 type_ = Type::DYNAMIC;
249 /* count the total time of trace file */
250 for (auto const& val : speed_profile->event_list)
251 total_time += val.date_;
253 profile_ = new CpuTiProfile(speed_profile);
254 last_time_ = total_time;
255 total_ = profile_->integrate_simple(0, total_time);
257 XBT_DEBUG("Total integral %f, last_time %f ", total_, last_time_);
261 * @brief Binary search in array.
262 * It returns the first point of the interval in which "a" is.
264 * @param a Value to search
265 * @param low Low bound to search in array
266 * @param high Upper bound to search in array
267 * @return Index of point
269 int CpuTiProfile::binary_search(double* array, double a, int low, int high)
271 xbt_assert(low < high, "Wrong parameters: low (%d) should be smaller than high (%d)", low, high);
274 int mid = low + (high - low) / 2;
275 XBT_DEBUG("a %f low %d high %d mid %d value %f", a, low, high, mid, array[mid]);
282 while (low < high - 1);
296 void CpuTiModel::create_pm_vm_models()
298 xbt_assert(surf_cpu_model_pm == nullptr, "CPU model already initialized. This should not happen.");
299 xbt_assert(surf_cpu_model_vm == nullptr, "CPU model already initialized. This should not happen.");
301 surf_cpu_model_pm = new simgrid::surf::CpuTiModel();
302 surf_cpu_model_vm = new simgrid::surf::CpuTiModel();
305 CpuTiModel::CpuTiModel() : CpuModel(Model::UpdateAlgo::FULL)
307 all_existing_models.push_back(this);
310 CpuTiModel::~CpuTiModel()
312 surf_cpu_model_pm = nullptr;
315 Cpu* CpuTiModel::create_cpu(simgrid::s4u::Host* host, std::vector<double>* speed_per_pstate, int core)
317 return new CpuTi(this, host, speed_per_pstate, core);
320 double CpuTiModel::next_occuring_event(double now)
322 double min_action_duration = -1;
324 /* iterates over modified cpus to update share resources */
325 for (auto it = std::begin(modified_cpus_); it != std::end(modified_cpus_);) {
327 ++it; // increment iterator here since the following call to ti.update_actions_finish_time() may invalidate it
328 cpu.update_actions_finish_time(now);
331 /* get the min next event if heap not empty */
332 if (not get_action_heap().empty())
333 min_action_duration = get_action_heap().top_date() - now;
335 XBT_DEBUG("Share resources, min next event date: %f", min_action_duration);
337 return min_action_duration;
340 void CpuTiModel::update_actions_state(double now, double /*delta*/)
342 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
343 CpuTiAction* action = static_cast<CpuTiAction*>(get_action_heap().pop());
344 XBT_DEBUG("Action %p: finish", action);
345 action->finish(kernel::resource::Action::State::FINISHED);
346 /* update remaining amount of all actions */
347 action->cpu_->update_remaining_amount(surf_get_clock());
354 CpuTi::CpuTi(CpuTiModel *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
355 : Cpu(model, host, speedPerPstate, core)
357 xbt_assert(core == 1, "Multi-core not handled by this model yet");
359 speed_.peak = speedPerPstate->front();
360 XBT_DEBUG("CPU create: peak=%f", speed_.peak);
362 speed_integrated_trace_ = new CpuTiTmgr(nullptr, 1 /*scale*/);
368 delete speed_integrated_trace_;
370 void CpuTi::set_speed_profile(kernel::profile::Profile* profile)
372 delete speed_integrated_trace_;
373 speed_integrated_trace_ = new CpuTiTmgr(profile, speed_.scale);
375 /* add a fake trace event if periodicity == 0 */
376 if (profile && profile->event_list.size() > 1) {
377 kernel::profile::DatedValue val = profile->event_list.back();
378 if (val.date_ < 1e-12) {
379 simgrid::kernel::profile::Profile* prof = new simgrid::kernel::profile::Profile();
380 speed_.event = prof->schedule(&future_evt_set, this);
385 void CpuTi::apply_event(kernel::profile::Event* event, double value)
387 if (event == speed_.event) {
388 XBT_DEBUG("Speed changed in trace! New fixed value: %f", value);
390 /* update remaining of actions and put in modified cpu list */
391 update_remaining_amount(surf_get_clock());
395 delete speed_integrated_trace_;
396 speed_integrated_trace_ = new CpuTiTmgr(value);
398 speed_.scale = value;
399 tmgr_trace_event_unref(&speed_.event);
401 } else if (event == state_event_) {
404 XBT_VERB("Restart processes on host %s", get_host()->get_cname());
405 get_host()->turn_on();
408 get_host()->turn_off();
409 double date = surf_get_clock();
411 /* put all action running on cpu to failed */
412 for (CpuTiAction& action : action_set_) {
413 if (action.get_state() == kernel::resource::Action::State::INITED ||
414 action.get_state() == kernel::resource::Action::State::STARTED ||
415 action.get_state() == kernel::resource::Action::State::IGNORED) {
416 action.set_finish_time(date);
417 action.set_state(kernel::resource::Action::State::FAILED);
418 get_model()->get_action_heap().remove(&action);
422 tmgr_trace_event_unref(&state_event_);
425 xbt_die("Unknown event!\n");
429 /** Update the actions that are running on this CPU (which was modified recently) */
430 void CpuTi::update_actions_finish_time(double now)
432 /* update remaining amount of actions */
433 update_remaining_amount(now);
435 /* Compute the sum of priorities for the actions running on that CPU */
437 for (CpuTiAction const& action : action_set_) {
438 /* action not running, skip it */
439 if (action.get_state_set() != surf_cpu_model_pm->get_started_action_set())
442 /* bogus priority, skip it */
443 if (action.get_priority() <= 0)
446 /* action suspended, skip it */
447 if (action.suspended_ != kernel::resource::Action::SuspendStates::not_suspended)
450 sum_priority_ += 1.0 / action.get_priority();
453 for (CpuTiAction& action : action_set_) {
454 double min_finish = -1;
455 /* action not running, skip it */
456 if (action.get_state_set() != surf_cpu_model_pm->get_started_action_set())
459 /* verify if the action is really running on cpu */
460 if (action.suspended_ == kernel::resource::Action::SuspendStates::not_suspended && action.get_priority() > 0) {
461 /* total area needed to finish the action. Used in trace integration */
462 double total_area = (action.get_remains() * sum_priority_ * action.get_priority()) / speed_.peak;
464 action.set_finish_time(speed_integrated_trace_->solve(now, total_area));
465 /* verify which event will happen before (max_duration or finish time) */
466 if (action.get_max_duration() > NO_MAX_DURATION &&
467 action.get_start_time() + action.get_max_duration() < action.get_finish_time())
468 min_finish = action.get_start_time() + action.get_max_duration();
470 min_finish = action.get_finish_time();
472 /* put the max duration time on heap */
473 if (action.get_max_duration() > NO_MAX_DURATION)
474 min_finish = action.get_start_time() + action.get_max_duration();
476 /* add in action heap */
477 if (min_finish > NO_MAX_DURATION)
478 get_model()->get_action_heap().update(&action, min_finish, kernel::resource::ActionHeap::Type::unset);
480 get_model()->get_action_heap().remove(&action);
482 XBT_DEBUG("Update finish time: Cpu(%s) Action: %p, Start Time: %f Finish Time: %f Max duration %f", get_cname(),
483 &action, action.get_start_time(), action.get_finish_time(), action.get_max_duration());
485 /* remove from modified cpu */
489 bool CpuTi::is_used()
491 return not action_set_.empty();
494 double CpuTi::get_speed_ratio()
496 speed_.scale = speed_integrated_trace_->get_power_scale(surf_get_clock());
497 return Cpu::get_speed_ratio();
500 /** @brief Update the remaining amount of actions */
501 void CpuTi::update_remaining_amount(double now)
503 /* already up to date */
504 if (last_update_ >= now)
507 /* compute the integration area */
508 double area_total = speed_integrated_trace_->integrate(last_update_, now) * speed_.peak;
509 XBT_DEBUG("Flops total: %f, Last update %f", area_total, last_update_);
510 for (CpuTiAction& action : action_set_) {
511 /* action not running, skip it */
512 if (action.get_state_set() != get_model()->get_started_action_set())
515 /* bogus priority, skip it */
516 if (action.get_priority() <= 0)
519 /* action suspended, skip it */
520 if (action.suspended_ != kernel::resource::Action::SuspendStates::not_suspended)
523 /* action don't need update */
524 if (action.get_start_time() >= now)
527 /* skip action that are finishing now */
528 if (action.get_finish_time() >= 0 && action.get_finish_time() <= now)
531 /* update remaining */
532 action.update_remains(area_total / (sum_priority_ * action.get_priority()));
533 XBT_DEBUG("Update remaining action(%p) remaining %f", &action, action.get_remains_no_update());
538 CpuAction *CpuTi::execution_start(double size)
540 XBT_IN("(%s,%g)", get_cname(), size);
541 CpuTiAction* action = new CpuTiAction(this, size);
543 action_set_.push_back(*action); // Actually start the action
550 CpuAction *CpuTi::sleep(double duration)
553 duration = std::max(duration, sg_surf_precision);
555 XBT_IN("(%s,%g)", get_cname(), duration);
556 CpuTiAction* action = new CpuTiAction(this, 1.0);
558 action->set_max_duration(duration);
559 action->suspended_ = kernel::resource::Action::SuspendStates::sleeping;
560 if (duration < 0) // NO_MAX_DURATION
561 action->set_state(simgrid::kernel::resource::Action::State::IGNORED);
563 action_set_.push_back(*action);
569 void CpuTi::set_modified(bool modified)
571 CpuTiList& modified_cpus = static_cast<CpuTiModel*>(get_model())->modified_cpus_;
573 if (not cpu_ti_hook.is_linked()) {
574 modified_cpus.push_back(*this);
577 if (cpu_ti_hook.is_linked())
578 simgrid::xbt::intrusive_erase(modified_cpus, *this);
586 CpuTiAction::CpuTiAction(CpuTi* cpu, double cost) : CpuAction(cpu->get_model(), cost, not cpu->is_on()), cpu_(cpu)
588 cpu_->set_modified(true);
590 CpuTiAction::~CpuTiAction()
592 /* remove from action_set */
593 if (action_ti_hook.is_linked())
594 simgrid::xbt::intrusive_erase(cpu_->action_set_, *this);
595 /* remove from heap */
596 get_model()->get_action_heap().remove(this);
597 cpu_->set_modified(true);
600 void CpuTiAction::set_state(Action::State state)
602 CpuAction::set_state(state);
603 cpu_->set_modified(true);
606 void CpuTiAction::cancel()
608 this->set_state(Action::State::FAILED);
609 get_model()->get_action_heap().remove(this);
610 cpu_->set_modified(true);
613 void CpuTiAction::suspend()
615 XBT_IN("(%p)", this);
616 if (suspended_ != Action::SuspendStates::sleeping) {
617 suspended_ = Action::SuspendStates::suspended;
618 get_model()->get_action_heap().remove(this);
619 cpu_->set_modified(true);
624 void CpuTiAction::resume()
626 XBT_IN("(%p)", this);
627 if (suspended_ != Action::SuspendStates::sleeping) {
628 suspended_ = Action::SuspendStates::not_suspended;
629 cpu_->set_modified(true);
634 void CpuTiAction::set_max_duration(double duration)
638 XBT_IN("(%p,%g)", this, duration);
640 Action::set_max_duration(duration);
643 min_finish = (get_start_time() + get_max_duration()) < get_finish_time() ? (get_start_time() + get_max_duration())
646 min_finish = get_finish_time();
648 /* add in action heap */
649 get_model()->get_action_heap().update(this, min_finish, kernel::resource::ActionHeap::Type::unset);
654 void CpuTiAction::set_priority(double priority)
656 XBT_IN("(%p,%g)", this, priority);
657 set_priority_no_update(priority);
658 cpu_->set_modified(true);
662 double CpuTiAction::get_remains()
664 XBT_IN("(%p)", this);
665 cpu_->update_remaining_amount(surf_get_clock());
667 return get_remains_no_update();