1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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"
9 typedef s_surf_action_lmm_t s_surf_action_cpu_Cas01_t,
10 *surf_action_cpu_Cas01_t;
12 typedef struct cpu_Cas01 {
13 s_surf_resource_t generic_resource;
17 tmgr_trace_event_t power_event;
18 e_surf_resource_state_t state_current;
19 tmgr_trace_event_t state_event;
20 lmm_constraint_t constraint;
21 } s_cpu_Cas01_t, *cpu_Cas01_t;
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
24 "Logging specific to the SURF CPU module");
28 surf_model_t surf_cpu_model = NULL;
29 lmm_system_t cpu_maxmin_system = NULL;
32 static xbt_swag_t cpu_running_action_set_that_does_not_need_being_checked =
35 static void* cpu_create_resource(const char *name, double power_peak,
37 tmgr_trace_t power_trace,
39 e_surf_resource_state_t state_initial,
40 tmgr_trace_t state_trace,
41 xbt_dict_t cpu_properties)
44 cpu_Cas01_t cpu = NULL;
45 xbt_assert(!surf_cpu_resource_by_name(name),
46 "Host '%s' declared several times in the platform file",
48 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
49 surf_cpu_model, name,cpu_properties);
50 cpu->power_peak = power_peak;
51 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
52 cpu->power_scale = power_scale;
54 xbt_assert(core>0,"Invalid number of cores %d",core);
57 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
59 cpu->state_current = state_initial;
62 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
65 lmm_constraint_new(cpu_maxmin_system, cpu,
66 cpu->core * cpu->power_scale * cpu->power_peak);
68 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
74 static void parse_cpu_init(sg_platf_host_cbarg_t host)
76 if(strcmp(host->V_host_coord,"")) xbt_die("Coordinates not implemented yet!");
78 cpu_create_resource(host->V_host_id,
79 host->V_host_power_peak,
80 host->V_host_power_scale,
81 host->V_host_power_trace,
83 host->V_host_state_initial,
84 host->V_host_state_trace,
88 static void add_traces_cpu(void)
90 xbt_dict_cursor_t cursor = NULL;
91 char *trace_name, *elm;
93 static int called = 0;
100 /* connect all traces relative to hosts */
101 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
102 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
103 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
105 xbt_assert(host, "Host %s undefined", elm);
106 xbt_assert(trace, "Trace %s undefined", trace_name);
109 tmgr_history_add_trace(history, trace, 0.0, 0, host);
112 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
113 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
114 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
116 xbt_assert(host, "Host %s undefined", elm);
117 xbt_assert(trace, "Trace %s undefined", trace_name);
120 tmgr_history_add_trace(history, trace, 0.0, 0, host);
124 static void cpu_define_callbacks(void)
126 sg_platf_host_add_cb(parse_cpu_init);
127 surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces_cpu);
130 static int cpu_resource_used(void *resource_id)
132 return lmm_constraint_used(cpu_maxmin_system,
133 ((cpu_Cas01_t) resource_id)->constraint);
136 static int cpu_action_unref(surf_action_t action)
139 if (!action->refcount) {
140 xbt_swag_remove(action, action->state_set);
141 if (((surf_action_cpu_Cas01_t) action)->variable)
142 lmm_variable_free(cpu_maxmin_system,
143 ((surf_action_cpu_Cas01_t) action)->variable);
145 if (action->category)
146 xbt_free(action->category);
148 surf_action_free(&action);
154 static void cpu_action_cancel(surf_action_t action)
156 surf_action_state_set(action, SURF_ACTION_FAILED);
160 static void cpu_action_state_set(surf_action_t action,
161 e_surf_action_state_t state)
163 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
164 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
165 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
166 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
169 surf_action_state_set(action, state);
173 static double cpu_share_resources(double now)
175 s_surf_action_cpu_Cas01_t action;
176 return generic_maxmin_share_resources(surf_cpu_model->
177 states.running_action_set,
178 xbt_swag_offset(action, variable),
179 cpu_maxmin_system, lmm_solve);
182 static void cpu_update_actions_state(double now, double delta)
184 surf_action_cpu_Cas01_t action = NULL;
185 surf_action_cpu_Cas01_t next_action = NULL;
186 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
188 xbt_swag_foreach_safe(action, next_action, running_actions) {
190 if (TRACE_is_enabled()) {
192 lmm_constraint_id(lmm_get_cnst_from_var
193 (cpu_maxmin_system, action->variable, 0));
195 TRACE_surf_host_set_utilization(x->generic_resource.name,
196 action->generic_action.data,
197 (surf_action_t) action,
198 lmm_variable_getvalue
199 (action->variable), now - delta,
201 TRACE_last_timestamp_to_dump = now-delta;
204 double_update(&(action->generic_action.remains),
205 lmm_variable_getvalue(action->variable) * delta);
206 if (action->generic_action.max_duration != NO_MAX_DURATION)
207 double_update(&(action->generic_action.max_duration), delta);
208 if ((action->generic_action.remains <= 0) &&
209 (lmm_get_variable_weight(action->variable) > 0)) {
210 action->generic_action.finish = surf_get_clock();
211 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
212 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
213 (action->generic_action.max_duration <= 0)) {
214 action->generic_action.finish = surf_get_clock();
215 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
222 static void cpu_update_resource_state(void *id,
223 tmgr_trace_event_t event_type,
224 double value, double date)
226 cpu_Cas01_t cpu = id;
227 lmm_variable_t var = NULL;
228 lmm_element_t elem = NULL;
230 if (event_type == cpu->power_event) {
231 cpu->power_scale = value;
232 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
233 cpu->core * cpu->power_scale * cpu->power_peak);
235 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
236 cpu->core * cpu->power_scale * cpu->power_peak);
238 while ((var = lmm_get_var_from_cnst
239 (cpu_maxmin_system, cpu->constraint, &elem))) {
240 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
241 lmm_update_variable_bound(cpu_maxmin_system, action->variable,
242 cpu->power_scale * cpu->power_peak);
244 if (tmgr_trace_event_free(event_type))
245 cpu->power_event = NULL;
246 } else if (event_type == cpu->state_event) {
248 cpu->state_current = SURF_RESOURCE_ON;
250 lmm_constraint_t cnst = cpu->constraint;
251 lmm_variable_t var = NULL;
252 lmm_element_t elem = NULL;
254 cpu->state_current = SURF_RESOURCE_OFF;
256 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
257 surf_action_t action = lmm_variable_id(var);
259 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
260 surf_action_state_get(action) == SURF_ACTION_READY ||
261 surf_action_state_get(action) ==
262 SURF_ACTION_NOT_IN_THE_SYSTEM) {
263 action->finish = date;
264 cpu_action_state_set(action, SURF_ACTION_FAILED);
268 if (tmgr_trace_event_free(event_type))
269 cpu->state_event = NULL;
271 XBT_CRITICAL("Unknown event ! \n");
278 static surf_action_t cpu_execute(void *cpu, double size)
280 surf_action_cpu_Cas01_t action = NULL;
281 cpu_Cas01_t CPU = cpu;
283 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
285 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
287 CPU->state_current != SURF_RESOURCE_ON);
289 action->suspended = 0; /* Should be useless because of the
290 calloc but it seems to help valgrind... */
292 action->variable = lmm_variable_new(cpu_maxmin_system, action,
293 action->generic_action.priority,
294 CPU->power_scale * CPU->power_peak, 1);
295 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
297 return (surf_action_t) action;
300 static surf_action_t cpu_action_sleep(void *cpu, double duration)
302 surf_action_cpu_Cas01_t action = NULL;
305 duration = MAX(duration, MAXMIN_PRECISION);
307 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
308 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
309 action->generic_action.max_duration = duration;
310 action->suspended = 2;
311 if (duration == NO_MAX_DURATION) {
312 /* Move to the *end* of the corresponding action set. This convention
313 is used to speed up update_resource_state */
314 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
315 ((surf_action_t) action)->state_set =
316 cpu_running_action_set_that_does_not_need_being_checked;
317 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
320 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
322 return (surf_action_t) action;
325 static void cpu_action_suspend(surf_action_t action)
327 XBT_IN("(%p)", action);
328 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
329 lmm_update_variable_weight(cpu_maxmin_system,
330 ((surf_action_cpu_Cas01_t)
331 action)->variable, 0.0);
332 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
337 static void cpu_action_resume(surf_action_t action)
339 XBT_IN("(%p)", action);
340 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
341 lmm_update_variable_weight(cpu_maxmin_system,
342 ((surf_action_cpu_Cas01_t)
343 action)->variable, action->priority);
344 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
349 static int cpu_action_is_suspended(surf_action_t action)
351 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
354 static void cpu_action_set_max_duration(surf_action_t action,
357 XBT_IN("(%p,%g)", action, duration);
358 action->max_duration = duration;
362 static void cpu_action_set_priority(surf_action_t action, double priority)
364 XBT_IN("(%p,%g)", action, priority);
365 action->priority = priority;
366 lmm_update_variable_weight(cpu_maxmin_system,
367 ((surf_action_cpu_Cas01_t) action)->variable,
374 static void cpu_action_set_category(surf_action_t action, const char *category)
376 XBT_IN("(%p,%s)", action, category);
377 action->category = xbt_strdup (category);
382 static double cpu_action_get_remains(surf_action_t action)
384 XBT_IN("(%p)", action);
385 return action->remains;
389 static e_surf_resource_state_t cpu_get_state(void *cpu)
391 return ((cpu_Cas01_t) cpu)->state_current;
394 static double cpu_get_speed(void *cpu, double load)
396 return load * (((cpu_Cas01_t) cpu)->power_peak);
399 static double cpu_get_available_speed(void *cpu)
401 /* number between 0 and 1 */
402 return ((cpu_Cas01_t) cpu)->power_scale;
406 static void cpu_finalize(void)
408 lmm_system_free(cpu_maxmin_system);
409 cpu_maxmin_system = NULL;
411 surf_model_exit(surf_cpu_model);
412 surf_cpu_model = NULL;
414 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
415 cpu_running_action_set_that_does_not_need_being_checked = NULL;
418 static void surf_cpu_model_init_internal(void)
420 s_surf_action_t action;
422 surf_cpu_model = surf_model_init();
424 cpu_running_action_set_that_does_not_need_being_checked =
425 xbt_swag_new(xbt_swag_offset(action, state_hookup));
427 surf_cpu_model->name = "CPU";
429 surf_cpu_model->action_unref = cpu_action_unref;
430 surf_cpu_model->action_cancel = cpu_action_cancel;
431 surf_cpu_model->action_state_set = cpu_action_state_set;
433 surf_cpu_model->model_private->resource_used = cpu_resource_used;
434 surf_cpu_model->model_private->share_resources = cpu_share_resources;
435 surf_cpu_model->model_private->update_actions_state =
436 cpu_update_actions_state;
437 surf_cpu_model->model_private->update_resource_state =
438 cpu_update_resource_state;
439 surf_cpu_model->model_private->finalize = cpu_finalize;
441 surf_cpu_model->suspend = cpu_action_suspend;
442 surf_cpu_model->resume = cpu_action_resume;
443 surf_cpu_model->is_suspended = cpu_action_is_suspended;
444 surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
445 surf_cpu_model->set_priority = cpu_action_set_priority;
447 surf_cpu_model->set_category = cpu_action_set_category;
449 surf_cpu_model->get_remains = cpu_action_get_remains;
451 surf_cpu_model->extension.cpu.execute = cpu_execute;
452 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
454 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
455 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
456 surf_cpu_model->extension.cpu.get_available_speed =
457 cpu_get_available_speed;
458 surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
459 surf_cpu_model->extension.cpu.add_traces = add_traces_cpu;
461 if (!cpu_maxmin_system)
462 cpu_maxmin_system = lmm_system_new();
465 /*********************************************************************/
466 /* Basic sharing model for CPU: that is where all this started... ;) */
467 /*********************************************************************/
468 /* @InProceedings{casanova01simgrid, */
469 /* author = "H. Casanova", */
470 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
471 /* and the Grid (CCGrid'01)", */
472 /* publisher = "IEEE Computer Society", */
473 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
477 /* note = "Available at */
478 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
480 void surf_cpu_model_init_Cas01()
484 surf_cpu_model_init_internal();
485 cpu_define_callbacks();
486 xbt_dynar_push(model_list, &surf_cpu_model);