Logo AND Algorithmique Numérique Distribuée

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