Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a few memory leaks.
[simgrid.git] / teshsuite / simdag / availability / availability_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stddef.h>
5 #include <unistd.h>
6 #include <simdag/simdag.h>
7 #include <xbt/log.h>
8 #include <xbt/ex.h>
9 #include <signal.h>
10
11
12 typedef struct {
13   FILE *daxFile;
14   FILE *envFile;
15 } XMLfiles;
16
17
18 static void usage(char *name)
19 {
20   fprintf(stdout, "Error on parameters.\n");
21   fprintf(stdout, "usage: %s <XML environment file>  <DAX file>\n", name);
22 }
23
24 static void checkParameters(int argc, char *argv[])
25 {
26   if (argc != 3) {
27     int i;
28     printf("====%d===\n",argc);
29     for(i=0; i<argc; i++) {
30       printf("\t%s\n",argv[i]);
31     }
32     usage(argv[0]);
33     exit(-1);
34   }
35  
36  /* Check that files exist */
37   XMLfiles xmlFiles;
38   if ((xmlFiles.envFile = fopen(argv[1], "r")) == NULL) {
39     fprintf(stderr, "Error while opening XML file %s.\n", argv[1]);
40     exit(-1);
41   }
42   fclose(xmlFiles.envFile);
43
44   if ((xmlFiles.daxFile = fopen(argv[2], "r")) == NULL) {
45     fprintf(stderr, "Error while opening DAX file %s.\n", argv[2]);
46     exit(-1);
47   }
48   fclose(xmlFiles.daxFile);
49 }
50
51 static int name_compare_hosts(const void *n1, const void *n2)
52 {
53   char name1[80], name2[80];
54   strcpy(name1, SD_workstation_get_name(*((SD_workstation_t *) n1)));
55   strcpy(name2, SD_workstation_get_name(*((SD_workstation_t *) n2)));
56
57   return strcmp(name1, name2);
58 }
59
60 static void scheduleDAX(xbt_dynar_t dax)
61 {
62   unsigned int cursor;
63   SD_task_t task;
64
65   const SD_workstation_t *ws_list = SD_workstation_get_list();
66   int totalHosts = SD_workstation_get_number();
67   qsort((void *) ws_list, totalHosts, sizeof(SD_workstation_t),
68         name_compare_hosts);
69
70   int count = SD_workstation_get_number();
71   //fprintf(stdout, "No. workstations: %d, %d\n", count, (dax != NULL));
72
73   xbt_dynar_foreach(dax, cursor, task) {
74     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
75       if (!strcmp(SD_task_get_name(task), "end")
76           || !strcmp(SD_task_get_name(task), "root")) {
77         fprintf(stdout, "Scheduling %s to node: %s\n", SD_task_get_name(task),
78                 SD_workstation_get_name(ws_list[0]));
79         SD_task_schedulel(task, 1, ws_list[0]);
80       } else {
81         fprintf(stdout, "Scheduling %s to node: %s\n", SD_task_get_name(task),
82                 SD_workstation_get_name(ws_list[(cursor) % count]));
83         SD_task_schedulel(task, 1, ws_list[(cursor) % count]);
84       }
85     }
86   }
87 }
88
89 /* static void printTasks(xbt_dynar_t completedTasks) */
90 /* { */
91 /*      unsigned int cursor; */
92 /*      SD_task_t task; */
93
94 /*      xbt_dynar_foreach(completedTasks, cursor, task) */
95 /*      { */
96 /*              if(SD_task_get_state(task) == SD_DONE) */
97 /*              { */
98 /*                      fprintf(stdout, "Task done: %s, %f, %f\n", */
99 /*                                              SD_task_get_name(task), SD_task_get_start_time(task), SD_task_get_finish_time(task)); */
100 /*              } */
101 /*      } */
102 /* } */
103
104
105 /* void createDottyFile(xbt_dynar_t dax, char *filename) */
106 /* { */
107 /*      char filename2[1000]; */
108 /*      unsigned int cursor; */
109 /*      SD_task_t task; */
110
111 /*      sprintf(filename2, "%s.dot", filename); */
112 /*      FILE *dotout = fopen(filename2, "w"); */
113 /*      fprintf(dotout, "digraph A {\n"); */
114 /*      xbt_dynar_foreach(dax, cursor, task) */
115 /*      { */
116 /*              SD_task_dotty(task, dotout); */
117 /*      } */
118 /*      fprintf(dotout, "}\n"); */
119 /*      fclose(dotout); */
120 /* } */
121
122 static xbt_dynar_t initDynamicThrottling(int *argc, char *argv[])
123 {
124   /* Initialize SD */
125   SD_init(argc, argv);
126
127   /* Check parameters */
128   checkParameters(*argc,argv);
129
130   /* Create environment */
131   SD_create_environment(argv[1]);
132   /* Load DAX file */
133   xbt_dynar_t dax = SD_daxload(argv[2]);
134
135   //  createDottyFile(dax, argv[2]);
136
137   // Schedule DAX
138   fprintf(stdout, "Scheduling DAX...\n");
139   scheduleDAX(dax);
140   fprintf(stdout, "DAX scheduled\n");
141   xbt_dynar_t ret = SD_simulate(-1);
142   xbt_dynar_free(&ret);
143   fprintf(stdout, "Simulation end. Time: %f\n", SD_get_clock());
144
145   return dax;
146 }
147
148 /**
149  * Garbage collector :D
150  */
151 static void garbageCollector(xbt_dynar_t dax)
152 {
153   while (!xbt_dynar_is_empty(dax)) {
154     SD_task_t task = xbt_dynar_pop_as(dax, SD_task_t);
155     SD_task_destroy(task);
156   }
157   xbt_dynar_free(&dax);
158   SD_exit();
159 }
160
161
162
163 /**
164  * Main procedure
165  * @param argc
166  * @param argv
167  * @return
168  */
169 int main(int argc, char *argv[])
170 {
171
172   /* Start process... */
173   xbt_dynar_t dax = initDynamicThrottling(&argc, argv);
174
175   // Free memory
176   garbageCollector(dax);
177   return 0;
178 }