Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the affinity control function in the CPU model
[simgrid.git] / src / surf / cpu_cas01.c
index 9b7d5c4c17eb46e160020e0c81e3863b96dbfa79..0692c1bcc31c17270de10d595a75478644048179 100644 (file)
@@ -68,6 +68,26 @@ void *cpu_cas01_create_resource(const char *name, double power_peak,
       lmm_constraint_new(cpu_model->model_private->maxmin_system, cpu,
                          cpu->core * cpu->power_scale * cpu->power_peak);
 
+  /* Note (hypervisor): we create a constraint object for each CPU core, which
+   * is used for making a contraint problem of CPU affinity.
+   **/
+  {
+    /* At now, we assume that a VM does not have a multicore CPU. */
+    if (core > 1)
+      xbt_assert(cpu_model == surf_cpu_model_pm);
+
+    cpu->constraint_core = xbt_new(lmm_constraint_t, core);
+
+    unsigned long i;
+    for (i = 0; i < core; i++) {
+      /* just for a unique id, never used as a string. */
+      void *cnst_id = bprintf("%s:%lu", name, i);
+      cpu->constraint_core[i] =
+        lmm_constraint_new(cpu_model->model_private->maxmin_system, cnst_id,
+            cpu->power_scale * cpu->power_peak);
+    }
+  }
+
   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
 
   return xbt_lib_get_elm_or_null(host_lib, name);;
@@ -132,6 +152,11 @@ static int cpu_resource_used(void *resource)
 {
   surf_model_t cpu_model = ((surf_resource_t) resource)->model;
 
+  /* Note (hypervisor): we do not need to look up constraint_core[i] here. Even
+   * when a task is pinned or not, its variable object is always linked to the
+   * basic contraint object.
+   **/
+
   return lmm_constraint_used(cpu_model->model_private->maxmin_system,
                              ((cpu_Cas01_t) resource)->constraint);
 }
@@ -174,6 +199,10 @@ static void cpu_update_resource_state(void *id,
   surf_watched_hosts();
 
   if (event_type == cpu->power_event) {
+    /* TODO (Hypervisor): do the same thing for constraint_core[i] */
+    XBT_CRITICAL("FIXME: add power scaling code also for constraint_core[i]");
+    xbt_abort();
+
     cpu->power_scale = value;
     lmm_update_constraint_bound(cpu_model->model_private->maxmin_system, cpu->constraint,
                                 cpu->core * cpu->power_scale *
@@ -193,6 +222,10 @@ static void cpu_update_resource_state(void *id,
     if (tmgr_trace_event_free(event_type))
       cpu->power_event = NULL;
   } else if (event_type == cpu->state_event) {
+    /* TODO (Hypervisor): do the same thing for constraint_core[i] */
+    XBT_CRITICAL("FIXME: add state change code also for constraint_core[i]");
+    xbt_abort();
+
     if (value > 0)
       cpu->state_current = SURF_RESOURCE_ON;
     else {
@@ -222,6 +255,36 @@ static void cpu_update_resource_state(void *id,
   return;
 }
 
+static void cpu_action_set_affinity(surf_action_t action, void *cpu, unsigned long mask)
+{
+  lmm_variable_t var_obj = ((surf_action_lmm_t) action)->variable;
+
+  surf_model_t cpu_model = action->model_obj;
+  xbt_assert(cpu_model->type == SURF_MODEL_TYPE_CPU);
+  cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
+
+  XBT_IN("(%p,%lx)", action, mask);
+
+
+  unsigned long i;
+  for (i = 0; i < CPU->core; i++) {
+    unsigned long has_affinity = (1UL << i) & mask;
+    if (has_affinity) {
+      XBT_INFO("set affinity %p to cpu-%lu@%s", action, i, CPU->generic_resource.name);
+      lmm_expand(cpu_model->model_private->maxmin_system, CPU->constraint_core[i], var_obj, 1.0);
+    } else {
+      XBT_INFO("clear affinity %p to cpu-%lu@%s", action, i, CPU->generic_resource.name);
+      lmm_shrink(cpu_model->model_private->maxmin_system, CPU->constraint_core[i], var_obj);
+    }
+  }
+
+  if (cpu_model->model_private->update_mechanism == UM_LAZY) {
+    XBT_WARN("FIXME (hypervisor): Do we need to do something for the LAZY mode?");
+  }
+
+  XBT_OUT();
+}
+
 static surf_action_t cpu_execute(void *cpu, double size)
 {
   surf_action_cpu_Cas01_t action = NULL;
@@ -237,10 +300,16 @@ static surf_action_t cpu_execute(void *cpu, double size)
   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
                                                    calloc but it seems to help valgrind... */
 
+  /* Note (hypervisor): here, the bound value of the variable is set to the
+   * capacity of a CPU core. But, after MSG_{task/vm}_set_bound() were added to
+   * the hypervisor branch, this bound value is overwritten in
+   * SIMIX_host_execute().
+   * TODO: cleanup this.
+   */
   GENERIC_LMM_ACTION(action).variable =
       lmm_variable_new(cpu_model->model_private->maxmin_system, action,
                        GENERIC_ACTION(action).priority,
-                       CPU->power_scale * CPU->power_peak, 1);
+                       CPU->power_scale * CPU->power_peak, 1 + CPU->core); // the basic constraint plus core-specific constraints
   if (cpu_model->model_private->update_mechanism == UM_LAZY) {
     GENERIC_LMM_ACTION(action).index_heap = -1;
     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
@@ -294,11 +363,20 @@ static e_surf_resource_state_t cpu_get_state(void *cpu)
   return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
 }
 
+static void cpu_set_state(void *cpu, e_surf_resource_state_t state)
+{
+  ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current = state;
+}
+
 static double cpu_get_speed(void *cpu, double load)
 {
   return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
 }
 
+static int cpu_get_core(void *cpu)
+{
+  return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->core;
+}
 static double cpu_get_available_speed(void *cpu)
 {
   /* number between 0 and 1 */
@@ -328,7 +406,7 @@ static surf_model_t surf_cpu_model_init_cas01(void)
 
   char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
   int select =
-      xbt_cfg_get_int(_sg_cfg_set, "cpu/maxmin_selective_update");
+      xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
 
   surf_model_t cpu_model = surf_model_init();
 
@@ -381,6 +459,8 @@ static surf_model_t surf_cpu_model_init_cas01(void)
   cpu_model->is_suspended = surf_action_is_suspended;
   cpu_model->set_max_duration = surf_action_set_max_duration;
   cpu_model->set_priority = surf_action_set_priority;
+  cpu_model->set_bound = surf_action_set_bound;
+  cpu_model->set_affinity = cpu_action_set_affinity;
 #ifdef HAVE_TRACING
   cpu_model->set_category = surf_action_set_category;
 #endif
@@ -390,6 +470,8 @@ static surf_model_t surf_cpu_model_init_cas01(void)
   cpu_model->extension.cpu.sleep = cpu_action_sleep;
 
   cpu_model->extension.cpu.get_state = cpu_get_state;
+  cpu_model->extension.cpu.set_state = cpu_set_state;
+  cpu_model->extension.cpu.get_core = cpu_get_core;
   cpu_model->extension.cpu.get_speed = cpu_get_speed;
   cpu_model->extension.cpu.get_available_speed =
       cpu_get_available_speed;