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/Event.hpp"
8 #include "src/kernel/resource/profile/Profile.hpp"
9 #include "src/surf/surf_interface.hpp"
10 #include "surf/surf.hpp"
14 constexpr double EPSILON = 0.000000001;
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_ti, surf_cpu, "Logging specific to the SURF CPU TRACE INTEGRATION module");
26 CpuTiProfile::CpuTiProfile(profile::Profile* profile)
30 unsigned nb_points = profile->event_list.size() + 1;
31 time_points_.reserve(nb_points);
32 integral_.reserve(nb_points);
33 for (auto const& val : profile->event_list) {
34 time_points_.push_back(time);
35 integral_.push_back(integral);
37 integral += val.date_ * val.value_;
39 time_points_.push_back(time);
40 integral_.push_back(integral);
44 * @brief Integrate trace
46 * Wrapper around surf_cpu_integrate_trace_simple() to get
49 * @param a Begin of interval
50 * @param b End of interval
51 * @return the integrate value. -1 if an error occurs.
53 double CpuTiTmgr::integrate(double a, double b)
55 if ((a < 0.0) || (a > b)) {
56 xbt_die("Error, invalid integration interval [%.2f,%.2f]. "
57 "You probably have a task executing with negative computation amount. Check your code.", a, b);
59 if (fabs(a - b) < EPSILON)
62 if (type_ == Type::FIXED) {
63 return (b - a) * value_;
67 if (fabs(ceil(a / last_time_) - a / last_time_) < EPSILON)
68 a_index = 1 + static_cast<int>(ceil(a / last_time_));
70 a_index = static_cast<int>(ceil(a / last_time_));
72 int b_index = static_cast<int>(floor(b / last_time_));
74 if (a_index > b_index) { /* Same chunk */
75 return profile_->integrate_simple(a - (a_index - 1) * last_time_, b - (b_index)*last_time_);
78 double first_chunk = profile_->integrate_simple(a - (a_index - 1) * last_time_, last_time_);
79 double middle_chunk = (b_index - a_index) * total_;
80 double last_chunk = profile_->integrate_simple(0.0, b - (b_index)*last_time_);
82 XBT_DEBUG("first_chunk=%.2f middle_chunk=%.2f last_chunk=%.2f\n", first_chunk, middle_chunk, last_chunk);
84 return (first_chunk + middle_chunk + last_chunk);
88 * @brief Auxiliary function to compute the integral between a and b.
89 * It simply computes the integrals at point a and b and returns the difference between them.
90 * @param a Initial point
91 * @param b Final point
93 double CpuTiProfile::integrate_simple(double a, double b)
95 return integrate_simple_point(b) - integrate_simple_point(a);
99 * @brief Auxiliary function to compute the integral at point a.
102 double CpuTiProfile::integrate_simple_point(double a)
106 int ind = binary_search(time_points_, a);
107 integral += integral_[ind];
109 XBT_DEBUG("a %f ind %d integral %f ind + 1 %f ind %f time +1 %f time %f", a, ind, integral, integral_[ind + 1],
110 integral_[ind], time_points_[ind + 1], time_points_[ind]);
111 double_update(&a_aux, time_points_[ind], sg_maxmin_precision * sg_surf_precision);
114 ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind])) * (a - time_points_[ind]);
115 XBT_DEBUG("Integral a %f = %f", a, integral);
121 * @brief Computes the time needed to execute "amount" on cpu.
123 * Here, amount can span multiple trace periods
125 * @param a Initial time
126 * @param amount Amount to be executed
129 double CpuTiTmgr::solve(double a, double amount)
131 /* Fix very small negative numbers */
132 if ((a < 0.0) && (a > -EPSILON)) {
135 if ((amount < 0.0) && (amount > -EPSILON)) {
140 if ((a < 0.0) || (amount < 0.0)) {
141 XBT_CRITICAL ("Error, invalid parameters [a = %.2f, amount = %.2f]. "
142 "You probably have a task executing with negative computation amount. Check your code.", a, amount);
146 /* At this point, a and amount are positive */
147 if (amount < EPSILON)
150 /* Is the trace fixed ? */
151 if (type_ == Type::FIXED) {
152 return (a + (amount / value_));
155 XBT_DEBUG("amount %f total %f", amount, total_);
156 /* Reduce the problem to one where amount <= trace_total */
157 int quotient = static_cast<int>(floor(amount / total_));
158 double reduced_amount = (total_) * ((amount / total_) - floor(amount / total_));
159 double reduced_a = a - (last_time_) * static_cast<int>(floor(a / last_time_));
161 XBT_DEBUG("Quotient: %d reduced_amount: %f reduced_a: %f", quotient, reduced_amount, reduced_a);
163 /* Now solve for new_amount which is <= trace_total */
165 XBT_DEBUG("Solve integral: [%.2f, amount=%.2f]", reduced_a, reduced_amount);
166 double amount_till_end = integrate(reduced_a, last_time_);
168 if (amount_till_end > reduced_amount) {
169 reduced_b = profile_->solve_simple(reduced_a, reduced_amount);
171 reduced_b = last_time_ + profile_->solve_simple(0.0, reduced_amount - amount_till_end);
174 /* Re-map to the original b and amount */
175 return (last_time_) * static_cast<int>(floor(a / last_time_)) + (quotient * last_time_) + reduced_b;
179 * @brief Auxiliary function to solve integral.
180 * It returns the date when the requested amount of flops is available
181 * @param a Initial point
182 * @param amount Amount of flops
183 * @return The date when amount is available.
185 double CpuTiProfile::solve_simple(double a, double amount)
187 double integral_a = integrate_simple_point(a);
188 int ind = binary_search(integral_, integral_a + amount);
189 double time = time_points_[ind];
190 time += (integral_a + amount - integral_[ind]) /
191 ((integral_[ind + 1] - integral_[ind]) / (time_points_[ind + 1] - time_points_[ind]));
197 * @brief Auxiliary function to update the CPU speed scale.
199 * This function uses the trace structure to return the speed scale at the determined time a.
201 * @return CPU speed scale
203 double CpuTiTmgr::get_power_scale(double a)
205 double reduced_a = a - floor(a / last_time_) * last_time_;
206 int point = CpuTiProfile::binary_search(profile_->time_points_, reduced_a);
207 kernel::profile::DatedValue val = speed_profile_->event_list.at(point);
212 * @brief Creates a new integration trace from a tmgr_trace_t
214 * @param speed_trace CPU availability trace
215 * @param value Percentage of CPU speed available (useful to fixed tracing)
216 * @return Integration trace structure
218 CpuTiTmgr::CpuTiTmgr(kernel::profile::Profile* speed_profile, double value) : speed_profile_(speed_profile)
220 double total_time = 0.0;
221 profile_.reset(nullptr);
223 /* no availability file, fixed trace */
224 if (not speed_profile) {
227 XBT_DEBUG("No availability trace. Constant value = %f", value);
231 /* only one point available, fixed trace */
232 if (speed_profile->event_list.size() == 1) {
234 value_ = speed_profile->event_list.front().value_;
238 type_ = Type::DYNAMIC;
240 /* count the total time of trace file */
241 for (auto const& val : speed_profile->event_list)
242 total_time += val.date_;
244 profile_.reset(new CpuTiProfile(speed_profile));
245 last_time_ = total_time;
246 total_ = profile_->integrate_simple(0, total_time);
248 XBT_DEBUG("Total integral %f, last_time %f ", total_, last_time_);
252 * @brief Binary search in array.
253 * It returns the last point of the interval in which "a" is.
255 * @param a Value to search
256 * @return Index of point
258 int CpuTiProfile::binary_search(const std::vector<double>& array, double a)
262 auto pos = std::upper_bound(begin(array), end(array), a);
263 return std::distance(begin(array), pos) - 1;
270 void CpuTiModel::create_pm_vm_models()
272 xbt_assert(surf_cpu_model_pm == nullptr, "CPU model already initialized. This should not happen.");
273 xbt_assert(surf_cpu_model_vm == nullptr, "CPU model already initialized. This should not happen.");
275 surf_cpu_model_pm = new simgrid::kernel::resource::CpuTiModel();
276 surf_cpu_model_vm = new simgrid::kernel::resource::CpuTiModel();
279 CpuTiModel::CpuTiModel() : CpuModel(Model::UpdateAlgo::FULL)
281 all_existing_models.push_back(this);
284 CpuTiModel::~CpuTiModel()
286 surf_cpu_model_pm = nullptr;
289 kernel::resource::Cpu* CpuTiModel::create_cpu(s4u::Host* host, const std::vector<double>& speed_per_pstate, int core)
291 return new CpuTi(this, host, speed_per_pstate, core);
294 double CpuTiModel::next_occuring_event(double now)
296 double min_action_duration = -1;
298 /* iterates over modified cpus to update share resources */
299 for (auto it = std::begin(modified_cpus_); it != std::end(modified_cpus_);) {
301 ++it; // increment iterator here since the following call to ti.update_actions_finish_time() may invalidate it
302 cpu.update_actions_finish_time(now);
305 /* get the min next event if heap not empty */
306 if (not get_action_heap().empty())
307 min_action_duration = get_action_heap().top_date() - now;
309 XBT_DEBUG("Share resources, min next event date: %f", min_action_duration);
311 return min_action_duration;
314 void CpuTiModel::update_actions_state(double now, double /*delta*/)
316 while (not get_action_heap().empty() && double_equals(get_action_heap().top_date(), now, sg_surf_precision)) {
317 auto* action = static_cast<CpuTiAction*>(get_action_heap().pop());
318 XBT_DEBUG("Action %p: finish", action);
319 action->finish(kernel::resource::Action::State::FINISHED);
320 /* update remaining amount of all actions */
321 action->cpu_->update_remaining_amount(surf_get_clock());
328 CpuTi::CpuTi(CpuTiModel* model, simgrid::s4u::Host* host, const std::vector<double>& speed_per_pstate, int core)
329 : Cpu(model, host, speed_per_pstate, core)
331 xbt_assert(core == 1, "Multi-core not handled by this model yet");
333 speed_.peak = speed_per_pstate.front();
334 XBT_DEBUG("CPU create: peak=%f", speed_.peak);
336 speed_integrated_trace_ = new CpuTiTmgr(nullptr, 1 /*scale*/);
342 delete speed_integrated_trace_;
344 void CpuTi::set_speed_profile(kernel::profile::Profile* profile)
346 delete speed_integrated_trace_;
347 speed_integrated_trace_ = new CpuTiTmgr(profile, speed_.scale);
349 /* add a fake trace event if periodicity == 0 */
350 if (profile && profile->event_list.size() > 1) {
351 kernel::profile::DatedValue val = profile->event_list.back();
352 if (val.date_ < 1e-12) {
353 auto* prof = new simgrid::kernel::profile::Profile();
354 speed_.event = prof->schedule(&profile::future_evt_set, this);
359 void CpuTi::apply_event(kernel::profile::Event* event, double value)
361 if (event == speed_.event) {
362 XBT_DEBUG("Speed changed in trace! New fixed value: %f", value);
364 /* update remaining of actions and put in modified cpu list */
365 update_remaining_amount(surf_get_clock());
369 delete speed_integrated_trace_;
370 speed_integrated_trace_ = new CpuTiTmgr(value);
372 speed_.scale = value;
373 tmgr_trace_event_unref(&speed_.event);
375 } else if (event == state_event_) {
378 XBT_VERB("Restart processes on host %s", get_host()->get_cname());
379 get_host()->turn_on();
382 get_host()->turn_off();
383 double date = surf_get_clock();
385 /* put all action running on cpu to failed */
386 for (CpuTiAction& action : action_set_) {
387 if (action.get_state() == kernel::resource::Action::State::INITED ||
388 action.get_state() == kernel::resource::Action::State::STARTED ||
389 action.get_state() == kernel::resource::Action::State::IGNORED) {
390 action.set_finish_time(date);
391 action.set_state(kernel::resource::Action::State::FAILED);
392 get_model()->get_action_heap().remove(&action);
396 tmgr_trace_event_unref(&state_event_);
399 xbt_die("Unknown event!\n");
403 /** Update the actions that are running on this CPU (which was modified recently) */
404 void CpuTi::update_actions_finish_time(double now)
406 /* update remaining amount of actions */
407 update_remaining_amount(now);
409 /* Compute the sum of priorities for the actions running on that CPU */
411 for (CpuTiAction const& action : action_set_) {
412 /* action not running, skip it */
413 if (action.get_state_set() != surf_cpu_model_pm->get_started_action_set())
416 /* bogus priority, skip it */
417 if (action.get_sharing_penalty() <= 0)
420 /* action suspended, skip it */
421 if (not action.is_running())
424 sum_priority_ += 1.0 / action.get_sharing_penalty();
427 for (CpuTiAction& action : action_set_) {
428 double min_finish = -1;
429 /* action not running, skip it */
430 if (action.get_state_set() != surf_cpu_model_pm->get_started_action_set())
433 /* verify if the action is really running on cpu */
434 if (action.is_running() && action.get_sharing_penalty() > 0) {
435 /* total area needed to finish the action. Used in trace integration */
436 double total_area = (action.get_remains() * sum_priority_ * action.get_sharing_penalty()) / speed_.peak;
438 action.set_finish_time(speed_integrated_trace_->solve(now, total_area));
439 /* verify which event will happen before (max_duration or finish time) */
440 if (action.get_max_duration() != NO_MAX_DURATION &&
441 action.get_start_time() + action.get_max_duration() < action.get_finish_time())
442 min_finish = action.get_start_time() + action.get_max_duration();
444 min_finish = action.get_finish_time();
446 /* put the max duration time on heap */
447 if (action.get_max_duration() != NO_MAX_DURATION)
448 min_finish = action.get_start_time() + action.get_max_duration();
450 /* add in action heap */
451 if (min_finish != NO_MAX_DURATION)
452 get_model()->get_action_heap().update(&action, min_finish, kernel::resource::ActionHeap::Type::unset);
454 get_model()->get_action_heap().remove(&action);
456 XBT_DEBUG("Update finish time: Cpu(%s) Action: %p, Start Time: %f Finish Time: %f Max duration %f", get_cname(),
457 &action, action.get_start_time(), action.get_finish_time(), action.get_max_duration());
459 /* remove from modified cpu */
463 bool CpuTi::is_used()
465 return not action_set_.empty();
468 double CpuTi::get_speed_ratio()
470 speed_.scale = speed_integrated_trace_->get_power_scale(surf_get_clock());
471 return Cpu::get_speed_ratio();
474 /** @brief Update the remaining amount of actions */
475 void CpuTi::update_remaining_amount(double now)
477 /* already up to date */
478 if (last_update_ >= now)
481 /* compute the integration area */
482 double area_total = speed_integrated_trace_->integrate(last_update_, now) * speed_.peak;
483 XBT_DEBUG("Flops total: %f, Last update %f", area_total, last_update_);
484 for (CpuTiAction& action : action_set_) {
485 /* action not running, skip it */
486 if (action.get_state_set() != get_model()->get_started_action_set())
489 /* bogus priority, skip it */
490 if (action.get_sharing_penalty() <= 0)
493 /* action suspended, skip it */
494 if (not action.is_running())
497 /* action don't need update */
498 if (action.get_start_time() >= now)
501 /* skip action that are finishing now */
502 if (action.get_finish_time() >= 0 && action.get_finish_time() <= now)
505 /* update remaining */
506 action.update_remains(area_total / (sum_priority_ * action.get_sharing_penalty()));
507 XBT_DEBUG("Update remaining action(%p) remaining %f", &action, action.get_remains_no_update());
512 kernel::resource::CpuAction* CpuTi::execution_start(double size)
514 XBT_IN("(%s,%g)", get_cname(), size);
515 auto* action = new CpuTiAction(this, size);
517 action_set_.push_back(*action); // Actually start the action
523 kernel::resource::CpuAction* CpuTi::sleep(double duration)
526 duration = std::max(duration, sg_surf_precision);
528 XBT_IN("(%s,%g)", get_cname(), duration);
529 auto* action = new CpuTiAction(this, 1.0);
531 action->set_max_duration(duration);
532 action->set_suspend_state(kernel::resource::Action::SuspendStates::SLEEPING);
533 if (duration == NO_MAX_DURATION)
534 action->set_state(simgrid::kernel::resource::Action::State::IGNORED);
536 action_set_.push_back(*action);
542 void CpuTi::set_modified(bool modified)
544 CpuTiList& modified_cpus = static_cast<CpuTiModel*>(get_model())->modified_cpus_;
546 if (not cpu_ti_hook.is_linked()) {
547 modified_cpus.push_back(*this);
550 if (cpu_ti_hook.is_linked())
551 simgrid::xbt::intrusive_erase(modified_cpus, *this);
559 CpuTiAction::CpuTiAction(CpuTi* cpu, double cost) : CpuAction(cpu->get_model(), cost, not cpu->is_on()), cpu_(cpu)
561 cpu_->set_modified(true);
563 CpuTiAction::~CpuTiAction()
565 /* remove from action_set */
566 if (action_ti_hook.is_linked())
567 simgrid::xbt::intrusive_erase(cpu_->action_set_, *this);
568 /* remove from heap */
569 get_model()->get_action_heap().remove(this);
570 cpu_->set_modified(true);
573 void CpuTiAction::set_state(Action::State state)
575 CpuAction::set_state(state);
576 cpu_->set_modified(true);
579 void CpuTiAction::cancel()
581 this->set_state(Action::State::FAILED);
582 get_model()->get_action_heap().remove(this);
583 cpu_->set_modified(true);
586 void CpuTiAction::suspend()
588 XBT_IN("(%p)", this);
590 set_suspend_state(Action::SuspendStates::SUSPENDED);
591 get_model()->get_action_heap().remove(this);
592 cpu_->set_modified(true);
597 void CpuTiAction::resume()
599 XBT_IN("(%p)", this);
600 if (is_suspended()) {
601 set_suspend_state(Action::SuspendStates::RUNNING);
602 cpu_->set_modified(true);
607 void CpuTiAction::set_max_duration(double duration)
611 XBT_IN("(%p,%g)", this, duration);
613 Action::set_max_duration(duration);
616 min_finish = (get_start_time() + get_max_duration()) < get_finish_time() ? (get_start_time() + get_max_duration())
619 min_finish = get_finish_time();
621 /* add in action heap */
622 get_model()->get_action_heap().update(this, min_finish, kernel::resource::ActionHeap::Type::unset);
627 void CpuTiAction::set_sharing_penalty(double sharing_penalty)
629 XBT_IN("(%p,%g)", this, sharing_penalty);
630 set_sharing_penalty_no_update(sharing_penalty);
631 cpu_->set_modified(true);
635 double CpuTiAction::get_remains()
637 XBT_IN("(%p)", this);
638 cpu_->update_remaining_amount(surf_get_clock());
640 return get_remains_no_update();
643 } // namespace resource
644 } // namespace kernel
645 } // namespace simgrid