1 /* Copyright (c) 2004-2011. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "surf_private.h"
8 #include "surf/surf_resource.h"
11 typedef s_surf_action_lmm_t s_surf_action_cpu_Cas01_t,
12 *surf_action_cpu_Cas01_t;
14 typedef struct cpu_Cas01 {
15 s_surf_resource_t generic_resource;
19 tmgr_trace_event_t power_event;
20 e_surf_resource_state_t state_current;
21 tmgr_trace_event_t state_event;
22 lmm_constraint_t constraint;
23 } s_cpu_Cas01_t, *cpu_Cas01_t;
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
26 "Logging specific to the SURF CPU module");
30 surf_model_t surf_cpu_model = NULL;
31 lmm_system_t cpu_maxmin_system = NULL;
34 static xbt_swag_t cpu_running_action_set_that_does_not_need_being_checked =
37 static void* cpu_create_resource(const char *name, double power_peak,
39 tmgr_trace_t power_trace,
41 e_surf_resource_state_t state_initial,
42 tmgr_trace_t state_trace,
43 xbt_dict_t cpu_properties)
46 cpu_Cas01_t cpu = NULL;
47 xbt_assert(!surf_cpu_resource_by_name(name),
48 "Host '%s' declared several times in the platform file",
50 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
51 surf_cpu_model, name,cpu_properties);
52 cpu->power_peak = power_peak;
53 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
54 cpu->power_scale = power_scale;
56 xbt_assert(core>0,"Invalid number of cores %d",core);
59 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
61 cpu->state_current = state_initial;
64 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
67 lmm_constraint_new(cpu_maxmin_system, cpu,
68 cpu->core * cpu->power_scale * cpu->power_peak);
70 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
76 static void parse_cpu_init(sg_platf_host_cbarg_t host)
78 if(strcmp(host->coord,"")) xbt_die("Coordinates not implemented yet!");
80 cpu_create_resource(host->id,
90 static void add_traces_cpu(void)
92 xbt_dict_cursor_t cursor = NULL;
93 char *trace_name, *elm;
95 static int called = 0;
102 /* connect all traces relative to hosts */
103 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
104 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
105 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
107 xbt_assert(host, "Host %s undefined", elm);
108 xbt_assert(trace, "Trace %s undefined", trace_name);
111 tmgr_history_add_trace(history, trace, 0.0, 0, host);
114 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
115 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
116 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
118 xbt_assert(host, "Host %s undefined", elm);
119 xbt_assert(trace, "Trace %s undefined", trace_name);
122 tmgr_history_add_trace(history, trace, 0.0, 0, host);
126 static void cpu_define_callbacks(void)
128 sg_platf_host_add_cb(parse_cpu_init);
129 sg_platf_postparse_add_cb(add_traces_cpu);
132 static int cpu_resource_used(void *resource_id)
134 return lmm_constraint_used(cpu_maxmin_system,
135 ((cpu_Cas01_t) resource_id)->constraint);
138 static int cpu_action_unref(surf_action_t action)
141 if (!action->refcount) {
142 xbt_swag_remove(action, action->state_set);
143 if (((surf_action_cpu_Cas01_t) action)->variable)
144 lmm_variable_free(cpu_maxmin_system,
145 ((surf_action_cpu_Cas01_t) action)->variable);
147 if (action->category)
148 xbt_free(action->category);
150 surf_action_free(&action);
156 static void cpu_action_cancel(surf_action_t action)
158 surf_action_state_set(action, SURF_ACTION_FAILED);
162 static void cpu_action_state_set(surf_action_t action,
163 e_surf_action_state_t state)
165 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
166 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
167 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
168 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
171 surf_action_state_set(action, state);
175 static double cpu_share_resources(double now)
177 s_surf_action_cpu_Cas01_t action;
178 return generic_maxmin_share_resources(surf_cpu_model->
179 states.running_action_set,
180 xbt_swag_offset(action, variable),
181 cpu_maxmin_system, lmm_solve);
184 static void cpu_update_actions_state(double now, double delta)
186 surf_action_cpu_Cas01_t action = NULL;
187 surf_action_cpu_Cas01_t next_action = NULL;
188 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
190 xbt_swag_foreach_safe(action, next_action, running_actions) {
192 if (TRACE_is_enabled()) {
194 lmm_constraint_id(lmm_get_cnst_from_var
195 (cpu_maxmin_system, action->variable, 0));
197 TRACE_surf_host_set_utilization(x->generic_resource.name,
198 action->generic_action.data,
199 (surf_action_t) action,
200 lmm_variable_getvalue
201 (action->variable), now - delta,
203 TRACE_last_timestamp_to_dump = now-delta;
206 double_update(&(action->generic_action.remains),
207 lmm_variable_getvalue(action->variable) * delta);
208 if (action->generic_action.max_duration != NO_MAX_DURATION)
209 double_update(&(action->generic_action.max_duration), delta);
210 if ((action->generic_action.remains <= 0) &&
211 (lmm_get_variable_weight(action->variable) > 0)) {
212 action->generic_action.finish = surf_get_clock();
213 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
214 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
215 (action->generic_action.max_duration <= 0)) {
216 action->generic_action.finish = surf_get_clock();
217 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
224 static void cpu_update_resource_state(void *id,
225 tmgr_trace_event_t event_type,
226 double value, double date)
228 cpu_Cas01_t cpu = id;
229 lmm_variable_t var = NULL;
230 lmm_element_t elem = NULL;
232 if (event_type == cpu->power_event) {
233 cpu->power_scale = value;
234 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
235 cpu->core * cpu->power_scale * cpu->power_peak);
237 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
238 cpu->core * cpu->power_scale * cpu->power_peak);
240 while ((var = lmm_get_var_from_cnst
241 (cpu_maxmin_system, cpu->constraint, &elem))) {
242 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
243 lmm_update_variable_bound(cpu_maxmin_system, action->variable,
244 cpu->power_scale * cpu->power_peak);
246 if (tmgr_trace_event_free(event_type))
247 cpu->power_event = NULL;
248 } else if (event_type == cpu->state_event) {
250 cpu->state_current = SURF_RESOURCE_ON;
252 lmm_constraint_t cnst = cpu->constraint;
253 lmm_variable_t var = NULL;
254 lmm_element_t elem = NULL;
256 cpu->state_current = SURF_RESOURCE_OFF;
258 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
259 surf_action_t action = lmm_variable_id(var);
261 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
262 surf_action_state_get(action) == SURF_ACTION_READY ||
263 surf_action_state_get(action) ==
264 SURF_ACTION_NOT_IN_THE_SYSTEM) {
265 action->finish = date;
266 cpu_action_state_set(action, SURF_ACTION_FAILED);
270 if (tmgr_trace_event_free(event_type))
271 cpu->state_event = NULL;
273 XBT_CRITICAL("Unknown event ! \n");
280 static surf_action_t cpu_execute(void *cpu, double size)
282 surf_action_cpu_Cas01_t action = NULL;
283 cpu_Cas01_t CPU = cpu;
285 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
287 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
289 CPU->state_current != SURF_RESOURCE_ON);
291 action->suspended = 0; /* Should be useless because of the
292 calloc but it seems to help valgrind... */
294 action->variable = lmm_variable_new(cpu_maxmin_system, action,
295 action->generic_action.priority,
296 CPU->power_scale * CPU->power_peak, 1);
297 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
299 return (surf_action_t) action;
302 static surf_action_t cpu_action_sleep(void *cpu, double duration)
304 surf_action_cpu_Cas01_t action = NULL;
307 duration = MAX(duration, MAXMIN_PRECISION);
309 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
310 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
311 action->generic_action.max_duration = duration;
312 action->suspended = 2;
313 if (duration == NO_MAX_DURATION) {
314 /* Move to the *end* of the corresponding action set. This convention
315 is used to speed up update_resource_state */
316 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
317 ((surf_action_t) action)->state_set =
318 cpu_running_action_set_that_does_not_need_being_checked;
319 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
322 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
324 return (surf_action_t) action;
327 static void cpu_action_suspend(surf_action_t action)
329 XBT_IN("(%p)", action);
330 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
331 lmm_update_variable_weight(cpu_maxmin_system,
332 ((surf_action_cpu_Cas01_t)
333 action)->variable, 0.0);
334 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
339 static void cpu_action_resume(surf_action_t action)
341 XBT_IN("(%p)", action);
342 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
343 lmm_update_variable_weight(cpu_maxmin_system,
344 ((surf_action_cpu_Cas01_t)
345 action)->variable, action->priority);
346 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
351 static int cpu_action_is_suspended(surf_action_t action)
353 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
356 static void cpu_action_set_max_duration(surf_action_t action,
359 XBT_IN("(%p,%g)", action, duration);
360 action->max_duration = duration;
364 static void cpu_action_set_priority(surf_action_t action, double priority)
366 XBT_IN("(%p,%g)", action, priority);
367 action->priority = priority;
368 lmm_update_variable_weight(cpu_maxmin_system,
369 ((surf_action_cpu_Cas01_t) action)->variable,
376 static void cpu_action_set_category(surf_action_t action, const char *category)
378 XBT_IN("(%p,%s)", action, category);
379 action->category = xbt_strdup (category);
384 static double cpu_action_get_remains(surf_action_t action)
386 XBT_IN("(%p)", action);
387 return action->remains;
391 static e_surf_resource_state_t cpu_get_state(void *cpu)
393 return ((cpu_Cas01_t) cpu)->state_current;
396 static double cpu_get_speed(void *cpu, double load)
398 return load * (((cpu_Cas01_t) cpu)->power_peak);
401 static double cpu_get_available_speed(void *cpu)
403 /* number between 0 and 1 */
404 return ((cpu_Cas01_t) cpu)->power_scale;
408 static void cpu_finalize(void)
410 lmm_system_free(cpu_maxmin_system);
411 cpu_maxmin_system = NULL;
413 surf_model_exit(surf_cpu_model);
414 surf_cpu_model = NULL;
416 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
417 cpu_running_action_set_that_does_not_need_being_checked = NULL;
420 static void surf_cpu_model_init_internal(void)
422 s_surf_action_t action;
424 surf_cpu_model = surf_model_init();
426 cpu_running_action_set_that_does_not_need_being_checked =
427 xbt_swag_new(xbt_swag_offset(action, state_hookup));
429 surf_cpu_model->name = "CPU";
431 surf_cpu_model->action_unref = cpu_action_unref;
432 surf_cpu_model->action_cancel = cpu_action_cancel;
433 surf_cpu_model->action_state_set = cpu_action_state_set;
435 surf_cpu_model->model_private->resource_used = cpu_resource_used;
436 surf_cpu_model->model_private->share_resources = cpu_share_resources;
437 surf_cpu_model->model_private->update_actions_state =
438 cpu_update_actions_state;
439 surf_cpu_model->model_private->update_resource_state =
440 cpu_update_resource_state;
441 surf_cpu_model->model_private->finalize = cpu_finalize;
443 surf_cpu_model->suspend = cpu_action_suspend;
444 surf_cpu_model->resume = cpu_action_resume;
445 surf_cpu_model->is_suspended = cpu_action_is_suspended;
446 surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
447 surf_cpu_model->set_priority = cpu_action_set_priority;
449 surf_cpu_model->set_category = cpu_action_set_category;
451 surf_cpu_model->get_remains = cpu_action_get_remains;
453 surf_cpu_model->extension.cpu.execute = cpu_execute;
454 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
456 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
457 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
458 surf_cpu_model->extension.cpu.get_available_speed =
459 cpu_get_available_speed;
460 surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
461 surf_cpu_model->extension.cpu.add_traces = add_traces_cpu;
463 if (!cpu_maxmin_system)
464 cpu_maxmin_system = lmm_system_new();
467 /*********************************************************************/
468 /* Basic sharing model for CPU: that is where all this started... ;) */
469 /*********************************************************************/
470 /* @InProceedings{casanova01simgrid, */
471 /* author = "H. Casanova", */
472 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
473 /* and the Grid (CCGrid'01)", */
474 /* publisher = "IEEE Computer Society", */
475 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
479 /* note = "Available at */
480 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
482 void surf_cpu_model_init_Cas01()
486 surf_cpu_model_init_internal();
487 cpu_define_callbacks();
488 xbt_dynar_push(model_list, &surf_cpu_model);