Ejemplo n.º 1
0
/** \ingroup m_task_management
 * \brief Changes the CPU affinity of a computation task.
 *
 * When pinning the given task to the first CPU core of the given host, use
 * 0x01 for the mask value. Each bit of the mask value corresponds to each CPU
 * core. See taskset(1) on Linux.
 *
 * \param task a target task
 * \param host the host having a multi-core CPU
 * \param mask the bit mask of a new CPU affinity setting for the task
 *
 *
 * Usage:
 * 0. Define a host with multiple cores.
 *    \<host id="PM0" power="1E8" core="2"/\>
 *
 * 1. Pin a given task to the first CPU core of a host.
 *   MSG_task_set_affinity(task, pm0, 0x01);
 *
 * 2. Pin a given task to the third CPU core of a host. Turn on the third bit of the mask.
 *   MSG_task_set_affinity(task, pm0, 0x04); // 0x04 == 100B
 *
 * 3. Pin a given VM to the first CPU core of a host.
 *   MSG_vm_set_affinity(vm, pm0, 0x01);
 *
 * See examples/msg/cloud/multicore.c for more information.
 *
 *
 * Note:
 * 1. The current code does not allow an affinity of a task to multiple cores.
 * The mask value 0x03 (i.e., a given task will be executed on the first core
 * or the second core) is not allowed. The mask value 0x01 or 0x02 works. See
 * cpu_cas01.c for details.
 *
 * 2. It is recommended to first compare simulation results in both the Lazy
 * and Full calculation modes (using --cfg=cpu/optim:Full or not). Fix
 * cpu_cas01.c if you find wrong results in the Lazy mode.
 *
 */
void MSG_task_set_affinity(msg_task_t task, msg_host_t host, unsigned long mask)
{
  xbt_assert(task, "Invalid parameter");
  xbt_assert(task->simdata, "Invalid parameter");

  if (mask == 0) {
    /* 0 means clear */
    {
      /* We need remove_ext() not throwing exception. */
      void *ret = xbt_dict_get_or_null_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(msg_host_t));
      if (ret != NULL)
        xbt_dict_remove_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(host));
    }
  } else
    xbt_dict_set_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(host), (void *) mask, NULL);

  /* We set affinity data of this task. If the task is being executed, we
   * actually change the affinity setting of the task. Otherwise, this change
   * will be applied when the task is executed. */

  if (!task->simdata->compute) {
    /* task is not yet executed */
    XBT_INFO("set affinity(0x%04lx@%s) for %s (not active now)", mask, MSG_host_get_name(host), MSG_task_get_name(task));
    return;
  }

  {
    smx_synchro_t compute = task->simdata->compute;
    msg_host_t host_now = compute->execution.host;  // simix_private.h is necessary
    if (host_now != host) {
      /* task is not yet executed on this host */
      XBT_INFO("set affinity(0x%04lx@%s) for %s (not active now)", mask, MSG_host_get_name(host), MSG_task_get_name(task));
      return;
    }

    /* task is being executed on this host. so change the affinity now */
    {
      /* check it works. remove me if it works. */
      xbt_assert((unsigned long) xbt_dict_get_or_null_ext(task->simdata->affinity_mask_db, (char *) host, sizeof(msg_host_t)) == mask);
    }

    XBT_INFO("set affinity(0x%04lx@%s) for %s", mask, MSG_host_get_name(host), MSG_task_get_name(task));
    simcall_process_execution_set_affinity(task->simdata->compute, host, mask);
  }
}
Ejemplo n.º 2
0
/*
 * \brief Destroys a host (internal call only)
 */
void __MSG_host_destroy(msg_host_t host)
{
  const char *name = MSG_host_get_name(host);
  /* TODO:
   * What happens if VMs still remain on this host?
   * Revisit here after the surf layer gets stable.
   **/

  xbt_lib_unset(host_lib, name, MSG_HOST_LEVEL, 1);
}
Ejemplo n.º 3
0
static int receiver_fun(int argc, char *argv[])
{
  XBT_INFO("Receiving");
  msg_task_t task = NULL;
  MSG_task_receive_with_timeout(&task, MSG_host_get_name(MSG_host_self()), DBL_MAX);
  xbt_assert(MSG_task_get_sender(task), "No sender received");
  XBT_INFO("Got a message sent by '%s'", MSG_process_get_name(MSG_task_get_sender(task)));
  MSG_task_destroy(task);
  return 0;
}
Ejemplo n.º 4
0
static void vm_migrate(msg_vm_t vm, msg_host_t dst_pm)
{
  msg_host_t src_pm = MSG_vm_get_pm(vm);
  double mig_sta    = MSG_get_clock();
  MSG_vm_migrate(vm, dst_pm);
  double mig_end = MSG_get_clock();

  XBT_INFO("%s migrated: %s->%s in %g s", MSG_vm_get_name(vm), MSG_host_get_name(src_pm), MSG_host_get_name(dst_pm),
           mig_end - mig_sta);
}
Ejemplo n.º 5
0
static sg_size_t write_local_file(const char *dest, sg_size_t file_size)
{
  sg_size_t written;
  msg_file_t file = MSG_file_open(dest, NULL);
  written = MSG_file_write(file, file_size);
  XBT_INFO("%llu bytes on %llu bytes have been written by %s on /sd1",written, file_size,
           MSG_host_get_name(MSG_host_self()));
  MSG_file_close(file);
  return written;
}
Ejemplo n.º 6
0
// Read src file on local disk and send a put message to remote host (size of message = size of src file)
static int hsm_put(const char *remote_host, const char *src, const char *dest){
  // Read local src file, and return the size that was actually read
  sg_size_t read_size = read_local_file(src);

  // Send file
  XBT_INFO("%s sends %llu to %s",MSG_host_get_name(MSG_host_self()),read_size,remote_host);
  msg_task_t to_execute = MSG_task_create((const char*)"hsm_put", 0, (double) read_size, (void*)dest);
  MSG_task_send(to_execute, remote_host);
  MSG_process_sleep(.4);
  return 1;
}
Ejemplo n.º 7
0
/** \ingroup msg_file_management
 * \brief Write into a file (local or remote)
 *
 * \param size of the file to write
 * \param fd is a the file descriptor
 * \return the number of bytes successfully write or -1 if an error occurred
 */
sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
{
  msg_file_priv_t file_priv = MSG_file_priv(fd);
  sg_size_t write_size, offset;

  /* Find the host where the file is physically located (remote or local)*/
  msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
  msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
  msg_host_t attached_host = MSG_get_host_by_name(storage_priv_src->hostname);

  if(strcmp(storage_priv_src->hostname, MSG_host_get_name(MSG_host_self()))){
    /* the file is hosted on a remote host, initiate a communication between src and dest hosts for data transfer */
    XBT_DEBUG("File is on %s remote host, initiate data transfer of %llu bytes.", storage_priv_src->hostname, size);
    msg_host_t *m_host_list = NULL;
    m_host_list = calloc(2, sizeof(msg_host_t));

    m_host_list[0] = MSG_host_self();
    m_host_list[1] = attached_host;
    double flops_amount[] = { 0, 0 };
    double bytes_amount[] = { 0, (double)size, 0, 0 };

    msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL);
    msg_error_t transfer = MSG_parallel_task_execute(task);
    MSG_task_destroy(task);
    free(m_host_list);
    if(transfer != MSG_OK){
      if (transfer == MSG_HOST_FAILURE)
        XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
      if (transfer == MSG_TASK_CANCELED)
        XBT_WARN("Transfer error, task has been canceled!");

      return -1;
    }
  }
  /* Write file on local or remote host */
  offset = simcall_file_tell(file_priv->simdata->smx_file);
  write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
  file_priv->size = offset+write_size;

  return write_size;
}
Ejemplo n.º 8
0
static sg_size_t read_local_file(const char *src)
{
  sg_size_t read, file_size;
  msg_file_t file = MSG_file_open(src, NULL);
  file_size = MSG_file_get_size(file);

  read = MSG_file_read(file, file_size);
  XBT_INFO("%s has read %llu on %s",MSG_host_get_name(MSG_host_self()),read,src);
  MSG_file_close(file);

  return read;
}
Ejemplo n.º 9
0
/** master */
int master(int argc, char *argv[])
{
  char *slavename = NULL;
  double task_comm_size = 0;
  msg_task_t todo;
  char id_alias[10];
  //unique id to control statistics
  int id = -1;

  xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);

  XBT_DEBUG ("Master started");

  /* data size */
  int read;
  read = sscanf(argv[1], "%lg", &task_comm_size);
  xbt_assert(read, "Invalid argument %s\n", argv[1]);

  /* slave name */
  slavename = argv[2];
  id = atoi(argv[3]);
  sprintf(id_alias, "flow_%d", id);
  slavenames[id] = slavename;
  TRACE_category(id_alias);

  masternames[id] = MSG_host_get_name(MSG_host_self());

  {                             /*  Task creation.  */
    char sprintf_buffer[64] = "Task_0";
    todo = MSG_task_create(sprintf_buffer, 100*task_comm_size, task_comm_size, NULL);
    MSG_task_set_category(todo, id_alias);
    //keep track of running tasks
    gl_task_array[id] = todo;
    gl_data_size[id] = task_comm_size;
  }

  {                             /* Process organisation */
    MSG_get_host_by_name(slavename);
  }

  count_finished++;
  timer_start = 1 ;

  /* time measurement */
  sprintf(id_alias, "%d", id);
  start_time = MSG_get_clock();
  //MSG_task_execute(todo);
  MSG_task_send(todo, id_alias);
  end_time = MSG_get_clock();

  XBT_DEBUG ("Finished");
  return 0;
}                               /* end_of_master */
Ejemplo n.º 10
0
static int runner(int argc, char *argv[])
{
  /* Retrieve the list of all hosts as an array of hosts */
  int hosts_count = MSG_get_host_number();
  msg_host_t *hosts = xbt_dynar_to_array(MSG_hosts_as_dynar());

  XBT_INFO("First, build a classical parallel task, with 1 Gflop to execute on each node, "
           "and 10MB to exchange between each pair");
  double *computation_amounts = xbt_new0(double, hosts_count);
  double *communication_amounts = xbt_new0(double, hosts_count * hosts_count);

  for (int i = 0; i < hosts_count; i++)
    computation_amounts[i] = 1e9; // 1 Gflop

  for (int i = 0; i < hosts_count; i++)
    for (int j = i + 1; j < hosts_count; j++)
      communication_amounts[i * hosts_count + j] = 1e7; // 10 MB

  msg_task_t ptask =
    MSG_parallel_task_create("parallel task", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
  MSG_parallel_task_execute(ptask);
  MSG_task_destroy(ptask);
  /* The arrays communication_amounts and computation_amounts are not to be freed manually */

  XBT_INFO("Then, build a parallel task involving only computations and no communication (1 Gflop per node)");
  computation_amounts = xbt_new0(double, hosts_count);
  for (int i = 0; i < hosts_count; i++)
    computation_amounts[i] = 1e9; // 1 Gflop
  ptask = MSG_parallel_task_create("parallel exec", hosts_count, hosts, computation_amounts, NULL/* no comm */, NULL);
  MSG_parallel_task_execute(ptask);
  MSG_task_destroy(ptask);

  XBT_INFO("Then, build a parallel task with no computation nor communication (synchro only)");
  computation_amounts = xbt_new0(double, hosts_count);
  communication_amounts = xbt_new0(double, hosts_count * hosts_count); /* memset to 0 by xbt_new0 */
  ptask = MSG_parallel_task_create("parallel sync", hosts_count, hosts, computation_amounts, communication_amounts, NULL);
  MSG_parallel_task_execute(ptask);
  MSG_task_destroy(ptask);

   XBT_INFO("Finally, trick the ptask to do a 'remote execution', on host %s", MSG_host_get_name(hosts[1]));
  computation_amounts = xbt_new0(double, 1);
  computation_amounts[0] = 1e9; // 1 Gflop
  msg_host_t *remote = xbt_new(msg_host_t,1);
  remote[0] = hosts[1];
  ptask = MSG_parallel_task_create("remote exec", 1, remote, computation_amounts, NULL/* no comm */, NULL);
  MSG_parallel_task_execute(ptask);
  MSG_task_destroy(ptask);
  free(remote);

  XBT_INFO("Goodbye now!");
  free(hosts);
  return 0;
}
Ejemplo n.º 11
0
/**
 * @brief  Send a task to a worker.
 * @param  phase     The current job phase.
 * @param  tid       The task ID.
 * @param  data_src  The ID of the DataNode that owns the task data.
 * @param  wid       The destination worker id.
 */
static void send_task (enum phase_e phase, size_t tid, size_t data_src, size_t wid)
{
    char         mailbox[MAILBOX_ALIAS_SIZE];
    int          i;
    double       cpu_required = 0.0;
    msg_task_t   task = NULL;
    task_info_t  task_info;

    cpu_required = user.task_cost_f (phase, tid, wid);

    task_info = xbt_new (struct task_info_s, 1);
    task = MSG_task_create (SMS_TASK, cpu_required, 0.0, (void*) task_info);

    task_info->phase = phase;
    task_info->id = tid;
    task_info->src = data_src;
    task_info->wid = wid;
    task_info->task = task;
    task_info->shuffle_end = 0.0;
    task_info->start_time = MSG_get_clock();
    task_info->finished_time = 0.0;
    task_info->elapsed_time = 0.0;
    task_info->cpu_time = 0.0;

    // for tracing purposes...
    MSG_task_set_category (task, (phase==MAP?"MAP":"REDUCE"));

    if (job.task_status[phase][tid] != T_STATUS_TIP_SLOW)
	job.task_status[phase][tid] = T_STATUS_TIP;

    job.heartbeats[wid].slots_av[phase]--;

    for (i = 0; i < MAX_SPECULATIVE_COPIES; i++)
    {
	if (job.task_list[phase][tid][i] == NULL)
	{
	    job.task_list[phase][tid][i] = task;
	    break;
	}
    }

    fprintf (tasks_log, "%d_%zu_%d,%s,%zu,%.3f,START,\n", phase, tid, i, (phase==MAP?"MAP":"REDUCE"), wid, MSG_get_clock ());

#ifdef VERBOSE
    XBT_INFO ("TX: %s > %s", SMS_TASK, MSG_host_get_name (config.workers[wid]));
#endif

    sprintf (mailbox, TASKTRACKER_MAILBOX, wid);
    xbt_assert (MSG_task_send (task, mailbox) == MSG_OK, "ERROR SENDING MESSAGE");

    job.task_instances[phase][tid]++;
}
Ejemplo n.º 12
0
static int worker(int argc, char *argv[])
{
  msg_task_t task = NULL;

  TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_worker", 1);
  TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_computation", 0);
  while (1) {
    MSG_task_receive(&(task), "master_mailbox");

    if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
      MSG_task_destroy(task);
      break;
    }
    //adding the value returned by MSG_task_get_compute_duration(task)
    //to the variable "task_computation"
    TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()), "task_computation", MSG_task_get_flops_amount(task));
    MSG_task_execute(task);
    MSG_task_destroy(task);
    task = NULL;
  }
  return 0;
}
Ejemplo n.º 13
0
/*
 * \brief Destroys a host (internal call only)
 */
void __MSG_host_destroy(msg_host_t host) {

#ifdef MSG_USE_DEPRECATED
  if (msg_global->max_channel > 0)
    free(host->mailboxes);
#endif
  if (xbt_swag_size(host->vms) > 0 ) {
    XBT_VERB("Host %s shut down, but it still hosts %d VMs. They will be leaked.",
        MSG_host_get_name(host),xbt_swag_size(host->vms));
  }
  xbt_swag_free(host->vms);
  free(host);
}
Ejemplo n.º 14
0
/** \ingroup msg_file_management
 * \brief Close the file
 *
 * \param fd is the file to close
 * \return 0 on success or 1 on error
 */
int MSG_file_close(msg_file_t fd)
{
  char *name;
  msg_file_priv_t priv = MSG_file_priv(fd);
  if (priv->data)
    xbt_free(priv->data);

  int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
  name = bprintf("%s:%i:%s",MSG_host_get_name(MSG_host_self()),MSG_process_self_PID(),priv->fullpath);
  xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
  xbt_free(name);
  return res;
}
Ejemplo n.º 15
0
static int dvfs(int argc, char* argv[])
{
  double workload = 100E6;
  msg_host_t host = MSG_host_self();

  int nb = MSG_host_get_nb_pstates(host);
  XBT_INFO("Count of Processor states=%d", nb);

  double current_peak = MSG_host_get_speed(host);
  XBT_INFO("Current power peak=%f", current_peak);

  // Run a task
  msg_task_t task1 = MSG_task_create("t1", workload, 0, NULL);
  MSG_task_execute(task1);
  MSG_task_destroy(task1);

  double task_time = MSG_get_clock();
  XBT_INFO("Task1 simulation time: %e", task_time);

  // Change power peak
  int new_pstate = 2;
  xbt_assert(new_pstate < nb, "Cannot set the host %s at pstate %d because it only provides %d pstates.",
             MSG_host_get_name(host), new_pstate, nb);

  double peak_at = MSG_host_get_power_peak_at(host, new_pstate);
  XBT_INFO("Changing power peak value to %f (at index %d)", peak_at, new_pstate);

  MSG_host_set_pstate(host, new_pstate);

  current_peak = MSG_host_get_speed(host);
  XBT_INFO("Current power peak=%f", current_peak);

  // Run a second task
  task1 = MSG_task_create("t1", workload, 0, NULL);
  MSG_task_execute(task1);
  MSG_task_destroy(task1);

  task_time = MSG_get_clock() - task_time;
  XBT_INFO("Task2 simulation time: %e", task_time);

  // Verify the default pstate is set to 0
  host    = MSG_host_by_name("MyHost2");
  int nb2 = MSG_host_get_nb_pstates(host);
  XBT_INFO("Count of Processor states=%d", nb2);

  double current_peak2 = MSG_host_get_speed(host);
  XBT_INFO("Current power peak=%f", current_peak2);
  return 0;
}
int slave(int argc, char *argv[])
{
  while (1) {
    msg_task_t task = NULL;
    int a;
    double time1, time2;

    time1 = MSG_get_clock();
    a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
    time2 = MSG_get_clock();
    if (a == MSG_OK) {
      XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
      if (MSG_task_get_data(task) == FINALIZE) {
        MSG_task_destroy(task);
        break;
      }
      if (time1 < *((double *) task->data))
        time1 = *((double *) task->data);
      XBT_INFO("Communication time : \"%f\"", time2 - time1);
      XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
      a = MSG_task_execute(task);
      if (a == MSG_OK) {
        XBT_INFO("\"%s\" done", MSG_task_get_name(task));
        free(task->data);
        MSG_task_destroy(task);
      } else if (a == MSG_HOST_FAILURE) {
        XBT_INFO
            ("Gloups. The cpu on which I'm running just turned off!. See you!");
        return 0;
      } else {
        XBT_INFO("Hey ?! What's up ? ");
        xbt_die("Unexpected behavior");
      }
    } else if (a == MSG_HOST_FAILURE) {
      XBT_INFO
          ("Gloups. The cpu on which I'm running just turned off!. See you!");
      return 0;
    } else if (a == MSG_TRANSFER_FAILURE) {
      XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
    } else if (a == MSG_TIMEOUT) {
      XBT_INFO("Mmh. Got a timeout. Nevermind. Let's keep going!");
    } else {
      XBT_INFO("Hey ?! What's up ? ");
      xbt_die("Unexpected behavior");
    }
  }
  XBT_INFO("I'm done. See you!");
  return 0;
}                               /* end_of_slave */
Ejemplo n.º 17
0
static void vm_migrate_async(msg_vm_t vm, msg_host_t dst_pm)
{
  const char *vm_name = MSG_vm_get_name(vm);
  const char *dst_pm_name = MSG_host_get_name(dst_pm);
  msg_host_t host = MSG_host_self();

  const char *pr_name = "mig_wrk";
  char **argv = xbt_new(char *, 4);
  argv[0] = xbt_strdup(pr_name);
  argv[1] = xbt_strdup(vm_name);
  argv[2] = xbt_strdup(dst_pm_name);
  argv[3] = NULL;

  MSG_process_create_with_arguments(pr_name, migration_worker_main, NULL, host, 3, argv);
}
Ejemplo n.º 18
0
/** \ingroup msg_file_management
 * \brief Opens the file whose name is the string pointed to by path
 *
 * \param fullpath is the file location on the storage
 * \param data user data to attach to the file
 *
 * \return An #msg_file_t associated to the file
 */
msg_file_t MSG_file_open(const char* fullpath, void* data)
{
  char *name;
  msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
  priv->data = data;
  priv->fullpath = xbt_strdup(fullpath);
  priv->simdata = xbt_new0(s_simdata_file_t,1);
  priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
  name = bprintf("%s:%i:%s",MSG_host_get_name(MSG_host_self()),MSG_process_self_PID(),fullpath);
  xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
  msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
  __MSG_file_get_info(fd);
  xbt_free(name);
  return fd;
}
Ejemplo n.º 19
0
Archivo: peer.c Proyecto: R7R8/simgrid
void peer_init(peer_t p, int argc, char *argv[])
{
  p->init = 0;
  p->prev = NULL;
  p->next = NULL;
  p->pieces = 0;
  p->bytes = 0;
  p->pending_recvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
  p->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
  /* Set mailbox name: use host number from argv or hostname if no argument given */
  if (argc > 1) {
    p->me = bprintf("host%s", argv[1]);
  } else {
    p->me = xbt_strdup(MSG_host_get_name(MSG_host_self()));
  }
}
Ejemplo n.º 20
0
static int computation_fun(int argc, char *argv[])
{
  const char *pr_name = MSG_process_get_name(MSG_process_self());
  const char *host_name = MSG_host_get_name(MSG_host_self());

  msg_task_t task = MSG_task_create("Task", 1000000, 1000000, NULL);

  double clock_sta = MSG_get_clock();
  MSG_task_execute(task);
  double clock_end = MSG_get_clock();

  XBT_INFO("%s:%s task executed %g", host_name, pr_name, clock_end - clock_sta);

  MSG_task_destroy(task);

  return 0;
}
Ejemplo n.º 21
0
/** Bittorrent example launcher */
int main(int argc, char* argv[])
{
  msg_host_t host;
  unsigned i;

  MSG_init(&argc, argv);

  /* Check the arguments */
  xbt_assert(argc > 2, "Usage: %s platform_file deployment_file", argv[0]);

  MSG_create_environment(argv[1]);

  xbt_dynar_t host_list = MSG_hosts_as_dynar();
  xbt_dynar_foreach (host_list, i, host) {
    char descr[512];
    snprintf(descr, sizeof descr, "RngSream<%s>", MSG_host_get_name(host));
    RngStream stream = RngStream_CreateStream(descr);
    MSG_host_set_data(host, stream);
  }
Ejemplo n.º 22
0
/** Create a Java org.simgrid.msg.Process with the arguments and run it */
static int java_main(int argc, char *argv[])
{
  JNIEnv *env = get_current_thread_env();
  simgrid::java::JavaContext* context = (simgrid::java::JavaContext*) SIMIX_context_self();

  //Change the "." in class name for "/".
  xbt_str_subst(argv[0],'.','/',0);
  jclass class_Process = env->FindClass(argv[0]);
  xbt_str_subst(argv[0],'/','.',0);
  //Retrieve the methodID for the constructor
  xbt_assert((class_Process != nullptr), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]);
  jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
  xbt_assert((constructor_Process != nullptr), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);

  //Retrieve the name of the process.
  jstring jname = env->NewStringUTF(argv[0]);
  //Build the arguments
  jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
    env->FindClass("java/lang/String"),
    env->NewStringUTF(""));
  int i;
  for (i = 1; i < argc; i++)
      env->SetObjectArrayElement(args,i - 1, env->NewStringUTF(argv[i]));
  //Retrieve the host for the process.
  jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
  jobject jhost = Java_org_simgrid_msg_Host_getByName(env, nullptr, jhostName);
  //creates the process
  jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
  xbt_assert((jprocess != nullptr), "Process allocation failed.");
  jprocess = env->NewGlobalRef(jprocess);
  //bind the process to the context
  msg_process_t process = MSG_process_self();

  context->jprocess = jprocess;
  /* sets the PID and the PPID of the process */
  env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
  env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
  jprocess_bind(jprocess, process, env);

  run_jprocess(env, context->jprocess);
  return 0;
}
Ejemplo n.º 23
0
JNIEXPORT jobjectArray JNICALL
Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
{
  int index;
  jobjectArray jtable;
  jobject jhost;
  jstring jname;
  msg_host_t host;

  xbt_dynar_t table =  MSG_hosts_as_dynar();
  int count = xbt_dynar_length(table);

  jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");

  if (!cls) {
    return NULL;
  }

  jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);

  if (!jtable) {
    jxbt_throw_jni(env, "Hosts table allocation failed");
    return NULL;
  }

  for (index = 0; index < count; index++) {
    host = xbt_dynar_get_as(table,index,msg_host_t);
    jhost = (jobject) (MSG_host_get_data(host));

    if (!jhost) {
      jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));

      jhost =
      		Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
      /* FIXME: leak of jname ? */
    }

    (*env)->SetObjectArrayElement(env, jtable, index, jhost);
  }
  xbt_dynar_free(&table);
  return jtable;
}
Ejemplo n.º 24
0
/** Worker does not expect any argument from XML deployment file. */
static int worker(int argc, char* argv[])
{
  while (1) {
    msg_task_t task = NULL;
    int res         = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
    xbt_assert(res == MSG_OK, "MSG_task_receive failed");

    XBT_INFO("Received '%s'", MSG_task_get_name(task));
    if (!strcmp(MSG_task_get_name(task), "finalize")) {
      MSG_task_destroy(task);
      break;
    }

    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
    MSG_task_execute(task);
    XBT_INFO("'%s' done", MSG_task_get_name(task));
    MSG_task_destroy(task);
  }
  XBT_INFO("I'm done. See you!");
  return 0;
} /* end_of_worker */
Ejemplo n.º 25
0
JNIEXPORT jobject JNICALL
Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
  jobject jhost;

  msg_host_t host = MSG_host_self();

  if (!MSG_host_get_data(host)) {
    /* the native host not yet associated with the java host instance */

    /* instanciate a new java host instance */
    jhost = jhost_new_instance(env);

    if (!jhost) {
      jxbt_throw_jni(env, "java host instantiation failed");
      return NULL;
    }

    /* get a global reference to the newly created host */
    jhost = jhost_ref(env, jhost);

    if (!jhost) {
      jxbt_throw_jni(env, "global ref allocation failed");
      return NULL;
    }
    /* Sets the host name */
    const char *name = MSG_host_get_name(host);
    jobject jname = (*env)->NewStringUTF(env,name);
    (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
    /* Bind & store it */
    jhost_bind(jhost, host, env);
    MSG_host_set_data(host, (void *) jhost);
  } else {
    jhost = (jobject) MSG_host_get_data(host);
  }

  return jhost;
}
Ejemplo n.º 26
0
/** Receiver function  */
int slave(int argc, char *argv[])
{
  while (1) {
    msg_task_t task = NULL;
    int a;
    double time1, time2;

    time1 = MSG_get_clock();
    a = MSG_task_receive( &(task), MSG_host_get_name(MSG_host_self()) );
    time2 = MSG_get_clock();
    if (a == MSG_OK) {
      XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
      if (MSG_task_get_data(task) == FINALIZE) {
        MSG_task_destroy(task);
        break;
      }
      if (time1 < *((double *) task->data))
        time1 = *((double *) task->data);
      XBT_INFO("Communication time : \"%f\"", time2 - time1);
      XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
      a = MSG_task_execute(task);
      if (a == MSG_OK) {
        XBT_INFO("\"%s\" done", MSG_task_get_name(task));
        free(task->data);
        MSG_task_destroy(task);
      } else {
        XBT_INFO("Hey ?! What's up ? ");
        xbt_die("Unexpected behavior");
      }
    } else {
      XBT_INFO("Hey ?! What's up ? error %d", a);
      xbt_die("Unexpected behavior");
    }
  }
  XBT_INFO("I'm done. See you!");
  return 0;
}
Ejemplo n.º 27
0
  /* - Attach some user data to disk1 */
  XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);

  data = MSG_storage_get_data(storage);

  XBT_INFO("Get storage data: '%s'", data);

  MSG_storage_set_data(storage, xbt_strdup("Some user data"));
  data = MSG_storage_get_data(storage);
  XBT_INFO("Set and get data: '%s'", data);
  xbt_free(data);
  xbt_free(storage_name);

  /* - Finally dump disks contents */
  XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
  xbt_dict_t contents = NULL;
  contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
  xbt_dict_cursor_t curs, curs2 = NULL;
  char* mountname;
  xbt_dict_t content;
  char* path;
  sg_size_t *size;
  xbt_dict_foreach(contents, curs, mountname, content){
    XBT_INFO("Print the content of mount point: %s",mountname);
    xbt_dict_foreach(content,curs2,path,size){
       XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
    }
  xbt_dict_free(&content);
  }
  xbt_dict_free(&contents);
Ejemplo n.º 28
0
/** Emitter function  */
int master(int argc, char *argv[])
{
  int workers_count = 0;
  msg_host_t *workers = NULL;
  msg_task_t *todo = NULL;
  msg_host_t host_self = MSG_host_self();
  char *master_name = (char *) MSG_host_get_name(host_self);
  double task_comp_size = 0;
  double task_comm_size = 0;
  char channel[1024];
  double timeout = -1;

  int i;

  TRACE_category(master_name);

  _XBT_GNUC_UNUSED int res = sscanf(argv[1], "%lg", &timeout);
  xbt_assert(res,"Invalid argument %s\n", argv[1]);
  res = sscanf(argv[2], "%lg", &task_comp_size);
  xbt_assert(res, "Invalid argument %s\n", argv[2]);
  res = sscanf(argv[3], "%lg", &task_comm_size);
  xbt_assert(res, "Invalid argument %s\n", argv[3]);

  {                             /* Process organisation */
    workers_count = MSG_get_host_number();
    workers = xbt_dynar_to_array(MSG_hosts_as_dynar());
    
    for (i = 0; i < workers_count; i++)
      if(host_self == workers[i]) {
	workers[i] = workers[workers_count-1];
	workers_count--;
	break;
      }

    for (i = 0; i < workers_count; i++)
	MSG_process_create("worker", worker, master_name, workers[i]);
  }

  XBT_INFO("Got %d workers and will send tasks for %g seconds!", 
	   workers_count, timeout);

  for (i = 0; 1; i++) {
    char sprintf_buffer[64];
    msg_task_t task = NULL;

    if(MSG_get_clock()>timeout) break;

    sprintf(sprintf_buffer, "Task_%d", i);
    task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
			   NULL);
    MSG_task_set_category(task, master_name);

    build_channel_name(channel,master_name,
		       MSG_host_get_name(workers[i % workers_count]));
    
    XBT_DEBUG("Sending \"%s\" to channel \"%s\"", task->name, channel);
    MSG_task_send(task, channel);
    XBT_DEBUG("Sent");
  }

  int task_num = i;

  XBT_DEBUG
      ("All tasks have been dispatched. Let's tell everybody the computation is over.");
  for (i = 0; i < workers_count; i++) {
    msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
    MSG_task_send(finalize, build_channel_name(channel,master_name,
		    MSG_host_get_name(workers[i % workers_count])));
  }

  XBT_INFO("Sent %d tasks in total!", task_num);
  free(workers);
  free(todo);
  return 0;
}                               /* end_of_master */
Ejemplo n.º 29
0
const char *jhost_get_name(jobject jhost, JNIEnv * env) {
  msg_host_t host = jhost_get_native(env, jhost);
  return MSG_host_get_name(host);
}
Ejemplo n.º 30
0
/** Emitter function  */
int master(int argc, char *argv[])
{
  int slaves_count = 0;
  msg_host_t *slaves = NULL;
  msg_task_t *todo = NULL;
  int number_of_tasks = 0;
  double task_comp_size = 0;
  double task_comm_size = 0;
  int i;
  _XBT_GNUC_UNUSED int read;

  read = sscanf(argv[1], "%d", &number_of_tasks);
  xbt_assert(read, "Invalid argument %s\n", argv[1]);
  read = sscanf(argv[2], "%lg", &task_comp_size);
  xbt_assert(read, "Invalid argument %s\n", argv[2]);
  read = sscanf(argv[3], "%lg", &task_comm_size);
  xbt_assert(read, "Invalid argument %s\n", argv[3]);

  {                             /*  Task creation */
    char sprintf_buffer[64];

    todo = xbt_new0(msg_task_t, number_of_tasks);

    for (i = 0; i < number_of_tasks; i++) {
      sprintf(sprintf_buffer, "Task_%d", i);
      todo[i] =
          MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
                          NULL);
    }
  }

  {                             /* Process organization */
    slaves_count = argc - 4;
    slaves = xbt_new0(msg_host_t, slaves_count);

    for (i = 4; i < argc; i++) {
      slaves[i - 4] = MSG_host_by_name(argv[i]);
      if (slaves[i - 4] == NULL) {
        XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
        abort();
      }
    }
  }

  XBT_INFO("Got %d slave(s) :", slaves_count);
  for (i = 0; i < slaves_count; i++)
    XBT_INFO("\t %s", MSG_host_get_name(slaves[i]));

  XBT_INFO("Got %d task to process :", number_of_tasks);

  for (i = 0; i < number_of_tasks; i++)
    XBT_INFO("\t\"%s\"", todo[i]->name);

  for (i = 0; i < number_of_tasks; i++) {
    XBT_INFO("Sending \"%s\" to \"%s\"",
          todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
    if (MSG_host_self() == slaves[i % slaves_count]) {
      XBT_INFO("Hey ! It's me ! :)");
    }
    MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
    XBT_INFO("Send completed");
  }

  XBT_INFO
      ("All tasks have been dispatched. Let's tell everybody the computation is over.");
  for (i = 0; i < slaves_count; i++)
    MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
        MSG_host_get_name(slaves[i]));

  XBT_INFO("Goodbye now!");
  free(slaves);
  free(todo);
  return 0;
}                               /* end_of_master */