Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'coverity_scan' of github.com:mquinson/simgrid
[simgrid.git] / teshsuite / simdag / availability / availability_test.c
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stddef.h>
11 #include <unistd.h>
12 #include <simgrid/simdag.h>
13 #include <xbt/log.h>
14 #include <xbt/ex.h>
15 #include <signal.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging for the current example");
18
19 static int name_compare_hosts(const void *n1, const void *n2)
20 {
21   return strcmp(
22       sg_host_get_name(*(sg_host_t *) n1),
23       sg_host_get_name(*(sg_host_t *) n2)
24   );
25 }
26
27 static void scheduleDAX(xbt_dynar_t dax)
28 {
29   unsigned int cursor;
30   SD_task_t task;
31
32   const sg_host_t *ws_list = sg_host_list();
33   int totalHosts = sg_host_count();
34   qsort((void *) ws_list, totalHosts, sizeof(sg_host_t), name_compare_hosts);
35
36
37   xbt_dynar_foreach(dax, cursor, task) {
38     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
39       if (!strcmp(SD_task_get_name(task), "end")
40           || !strcmp(SD_task_get_name(task), "root")) {
41         XBT_INFO("Scheduling %s to node: %s", SD_task_get_name(task),
42                 sg_host_get_name(ws_list[0]));
43         SD_task_schedulel(task, 1, ws_list[0]);
44       } else {
45         XBT_INFO("Scheduling %s to node: %s", SD_task_get_name(task),
46                 sg_host_get_name(ws_list[(cursor) % totalHosts]));
47         SD_task_schedulel(task, 1, ws_list[(cursor) % totalHosts]);
48       }
49     }
50   }
51 }
52
53 static xbt_dynar_t initDynamicThrottling(int argc, char *argv[])
54 {
55   SD_init(&argc, argv);
56   xbt_assert(argc == 3,
57       "Error on parameters.\n"
58       "Usage: %s <XML platform file>  <DAX file>\n", argv[0]);
59
60   FILE *testFile = fopen(argv[1], "r");
61   xbt_assert(testFile, "Cannot open platform file %s.\n", argv[1]);
62   testFile = fopen(argv[2], "r");
63   xbt_assert(testFile, "Cannot open DAX file %s.\n", argv[2]);
64
65   SD_create_environment(argv[1]);
66   xbt_dynar_t dax = SD_daxload(argv[2]);
67
68   // Schedule DAX
69   XBT_INFO("Scheduling DAX...");
70   scheduleDAX(dax);
71   XBT_INFO("DAX scheduled");
72   SD_simulate(-1);
73   XBT_INFO("Simulation done.");
74
75   return dax;
76 }
77
78 int main(int argc, char *argv[])
79 {
80
81   /* Start process... */
82   xbt_dynar_t dax = initDynamicThrottling(argc, argv);
83
84   // Free memory
85   while (!xbt_dynar_is_empty(dax)) {
86     SD_task_t task = xbt_dynar_pop_as(dax, SD_task_t);
87     SD_task_destroy(task);
88   }
89   xbt_dynar_free(&dax);
90   SD_exit();
91   return 0;
92 }