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 xbt_free(action->category);
149 surf_action_free(&action);
155 static void cpu_action_cancel(surf_action_t action)
157 surf_action_state_set(action, SURF_ACTION_FAILED);
161 static void cpu_action_state_set(surf_action_t action,
162 e_surf_action_state_t state)
164 /* if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
165 /* if(((surf_action_cpu_Cas01_t)action)->variable) { */
166 /* lmm_variable_disable(cpu_maxmin_system, ((surf_action_cpu_Cas01_t)action)->variable); */
167 /* ((surf_action_cpu_Cas01_t)action)->variable = NULL; */
170 surf_action_state_set(action, state);
174 static double cpu_share_resources(double now)
176 s_surf_action_cpu_Cas01_t action;
177 return generic_maxmin_share_resources(surf_cpu_model->
178 states.running_action_set,
179 xbt_swag_offset(action, variable),
180 cpu_maxmin_system, lmm_solve);
183 static void cpu_update_actions_state(double now, double delta)
185 surf_action_cpu_Cas01_t action = NULL;
186 surf_action_cpu_Cas01_t next_action = NULL;
187 xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
189 xbt_swag_foreach_safe(action, next_action, running_actions) {
191 if (TRACE_is_enabled()) {
193 lmm_constraint_id(lmm_get_cnst_from_var
194 (cpu_maxmin_system, action->variable, 0));
196 TRACE_surf_host_set_utilization(x->generic_resource.name,
197 action->generic_action.data,
198 (surf_action_t) action,
199 lmm_variable_getvalue
200 (action->variable), now - delta,
202 TRACE_last_timestamp_to_dump = now-delta;
205 double_update(&(action->generic_action.remains),
206 lmm_variable_getvalue(action->variable) * delta);
207 if (action->generic_action.max_duration != NO_MAX_DURATION)
208 double_update(&(action->generic_action.max_duration), delta);
209 if ((action->generic_action.remains <= 0) &&
210 (lmm_get_variable_weight(action->variable) > 0)) {
211 action->generic_action.finish = surf_get_clock();
212 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
213 } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
214 (action->generic_action.max_duration <= 0)) {
215 action->generic_action.finish = surf_get_clock();
216 cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
223 static void cpu_update_resource_state(void *id,
224 tmgr_trace_event_t event_type,
225 double value, double date)
227 cpu_Cas01_t cpu = id;
228 lmm_variable_t var = NULL;
229 lmm_element_t elem = NULL;
231 if (event_type == cpu->power_event) {
232 cpu->power_scale = value;
233 lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
234 cpu->core * cpu->power_scale * cpu->power_peak);
236 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
237 cpu->core * cpu->power_scale * cpu->power_peak);
239 while ((var = lmm_get_var_from_cnst
240 (cpu_maxmin_system, cpu->constraint, &elem))) {
241 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
242 lmm_update_variable_bound(cpu_maxmin_system, action->variable,
243 cpu->power_scale * cpu->power_peak);
245 if (tmgr_trace_event_free(event_type))
246 cpu->power_event = NULL;
247 } else if (event_type == cpu->state_event) {
249 cpu->state_current = SURF_RESOURCE_ON;
251 lmm_constraint_t cnst = cpu->constraint;
252 lmm_variable_t var = NULL;
253 lmm_element_t elem = NULL;
255 cpu->state_current = SURF_RESOURCE_OFF;
257 while ((var = lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
258 surf_action_t action = lmm_variable_id(var);
260 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
261 surf_action_state_get(action) == SURF_ACTION_READY ||
262 surf_action_state_get(action) ==
263 SURF_ACTION_NOT_IN_THE_SYSTEM) {
264 action->finish = date;
265 cpu_action_state_set(action, SURF_ACTION_FAILED);
269 if (tmgr_trace_event_free(event_type))
270 cpu->state_event = NULL;
272 XBT_CRITICAL("Unknown event ! \n");
279 static surf_action_t cpu_execute(void *cpu, double size)
281 surf_action_cpu_Cas01_t action = NULL;
282 cpu_Cas01_t CPU = cpu;
284 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
286 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
288 CPU->state_current != SURF_RESOURCE_ON);
290 action->suspended = 0; /* Should be useless because of the
291 calloc but it seems to help valgrind... */
293 action->variable = lmm_variable_new(cpu_maxmin_system, action,
294 action->generic_action.priority,
295 CPU->power_scale * CPU->power_peak, 1);
296 lmm_expand(cpu_maxmin_system, CPU->constraint, action->variable, 1.0);
298 return (surf_action_t) action;
301 static surf_action_t cpu_action_sleep(void *cpu, double duration)
303 surf_action_cpu_Cas01_t action = NULL;
306 duration = MAX(duration, MAXMIN_PRECISION);
308 XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
309 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
310 action->generic_action.max_duration = duration;
311 action->suspended = 2;
312 if (duration == NO_MAX_DURATION) {
313 /* Move to the *end* of the corresponding action set. This convention
314 is used to speed up update_resource_state */
315 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
316 ((surf_action_t) action)->state_set =
317 cpu_running_action_set_that_does_not_need_being_checked;
318 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
321 lmm_update_variable_weight(cpu_maxmin_system, action->variable, 0.0);
323 return (surf_action_t) action;
326 static void cpu_action_suspend(surf_action_t action)
328 XBT_IN("(%p)", action);
329 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
330 lmm_update_variable_weight(cpu_maxmin_system,
331 ((surf_action_cpu_Cas01_t)
332 action)->variable, 0.0);
333 ((surf_action_cpu_Cas01_t) action)->suspended = 1;
338 static void cpu_action_resume(surf_action_t action)
340 XBT_IN("(%p)", action);
341 if (((surf_action_cpu_Cas01_t) action)->suspended != 2) {
342 lmm_update_variable_weight(cpu_maxmin_system,
343 ((surf_action_cpu_Cas01_t)
344 action)->variable, action->priority);
345 ((surf_action_cpu_Cas01_t) action)->suspended = 0;
350 static int cpu_action_is_suspended(surf_action_t action)
352 return (((surf_action_cpu_Cas01_t) action)->suspended == 1);
355 static void cpu_action_set_max_duration(surf_action_t action,
358 XBT_IN("(%p,%g)", action, duration);
359 action->max_duration = duration;
363 static void cpu_action_set_priority(surf_action_t action, double priority)
365 XBT_IN("(%p,%g)", action, priority);
366 action->priority = priority;
367 lmm_update_variable_weight(cpu_maxmin_system,
368 ((surf_action_cpu_Cas01_t) action)->variable,
375 static void cpu_action_set_category(surf_action_t action, const char *category)
377 XBT_IN("(%p,%s)", action, category);
378 action->category = xbt_strdup (category);
383 static double cpu_action_get_remains(surf_action_t action)
385 XBT_IN("(%p)", action);
386 return action->remains;
390 static e_surf_resource_state_t cpu_get_state(void *cpu)
392 return ((cpu_Cas01_t) cpu)->state_current;
395 static double cpu_get_speed(void *cpu, double load)
397 return load * (((cpu_Cas01_t) cpu)->power_peak);
400 static double cpu_get_available_speed(void *cpu)
402 /* number between 0 and 1 */
403 return ((cpu_Cas01_t) cpu)->power_scale;
407 static void cpu_finalize(void)
409 lmm_system_free(cpu_maxmin_system);
410 cpu_maxmin_system = NULL;
412 surf_model_exit(surf_cpu_model);
413 surf_cpu_model = NULL;
415 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
416 cpu_running_action_set_that_does_not_need_being_checked = NULL;
419 static void surf_cpu_model_init_internal(void)
421 s_surf_action_t action;
423 surf_cpu_model = surf_model_init();
425 cpu_running_action_set_that_does_not_need_being_checked =
426 xbt_swag_new(xbt_swag_offset(action, state_hookup));
428 surf_cpu_model->name = "CPU";
430 surf_cpu_model->action_unref = cpu_action_unref;
431 surf_cpu_model->action_cancel = cpu_action_cancel;
432 surf_cpu_model->action_state_set = cpu_action_state_set;
434 surf_cpu_model->model_private->resource_used = cpu_resource_used;
435 surf_cpu_model->model_private->share_resources = cpu_share_resources;
436 surf_cpu_model->model_private->update_actions_state =
437 cpu_update_actions_state;
438 surf_cpu_model->model_private->update_resource_state =
439 cpu_update_resource_state;
440 surf_cpu_model->model_private->finalize = cpu_finalize;
442 surf_cpu_model->suspend = cpu_action_suspend;
443 surf_cpu_model->resume = cpu_action_resume;
444 surf_cpu_model->is_suspended = cpu_action_is_suspended;
445 surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
446 surf_cpu_model->set_priority = cpu_action_set_priority;
448 surf_cpu_model->set_category = cpu_action_set_category;
450 surf_cpu_model->get_remains = cpu_action_get_remains;
452 surf_cpu_model->extension.cpu.execute = cpu_execute;
453 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
455 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
456 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
457 surf_cpu_model->extension.cpu.get_available_speed =
458 cpu_get_available_speed;
459 surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
460 surf_cpu_model->extension.cpu.add_traces = add_traces_cpu;
462 if (!cpu_maxmin_system)
463 cpu_maxmin_system = lmm_system_new();
466 /*********************************************************************/
467 /* Basic sharing model for CPU: that is where all this started... ;) */
468 /*********************************************************************/
469 /* @InProceedings{casanova01simgrid, */
470 /* author = "H. Casanova", */
471 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
472 /* and the Grid (CCGrid'01)", */
473 /* publisher = "IEEE Computer Society", */
474 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
478 /* note = "Available at */
479 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
481 void surf_cpu_model_init_Cas01()
485 surf_cpu_model_init_internal();
486 cpu_define_callbacks();
487 xbt_dynar_push(model_list, &surf_cpu_model);