Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a426e74407ed51a97ee2249287e7cf4fc5dc9d75
[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
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, sg_host_get_name(*((sg_host_t *) n1)));
61   strcpy(name2, sg_host_get_name(*((sg_host_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 sg_host_t *ws_list = sg_host_list();
72   int totalHosts = sg_host_count();
73   qsort((void *) ws_list, totalHosts, sizeof(sg_host_t),
74         name_compare_hosts);
75
76   //fprintf(stdout, "No. workstations: %d, %d\n", totalHosts, (dax != NULL));
77
78   xbt_dynar_foreach(dax, cursor, task) {
79     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
80       if (!strcmp(SD_task_get_name(task), "end")
81           || !strcmp(SD_task_get_name(task), "root")) {
82         fprintf(stdout, "Scheduling %s to node: %s\n", SD_task_get_name(task),
83                 sg_host_get_name(ws_list[0]));
84         SD_task_schedulel(task, 1, ws_list[0]);
85       } else {
86         fprintf(stdout, "Scheduling %s to node: %s\n", SD_task_get_name(task),
87                 sg_host_get_name(ws_list[(cursor) % totalHosts]));
88         SD_task_schedulel(task, 1, ws_list[(cursor) % totalHosts]);
89       }
90     }
91   }
92 }
93
94 /* static void printTasks(xbt_dynar_t completedTasks) */
95 /* { */
96 /*      unsigned int cursor; */
97 /*      SD_task_t task; */
98
99 /*      xbt_dynar_foreach(completedTasks, cursor, task) */
100 /*      { */
101 /*              if(SD_task_get_state(task) == SD_DONE) */
102 /*              { */
103 /*                      fprintf(stdout, "Task done: %s, %f, %f\n", */
104 /*                                              SD_task_get_name(task), SD_task_get_start_time(task), SD_task_get_finish_time(task)); */
105 /*              } */
106 /*      } */
107 /* } */
108
109
110 /* void createDottyFile(xbt_dynar_t dax, char *filename) */
111 /* { */
112 /*      char filename2[1000]; */
113 /*      unsigned int cursor; */
114 /*      SD_task_t task; */
115
116 /*      sprintf(filename2, "%s.dot", filename); */
117 /*      FILE *dotout = fopen(filename2, "w"); */
118 /*      fprintf(dotout, "digraph A {\n"); */
119 /*      xbt_dynar_foreach(dax, cursor, task) */
120 /*      { */
121 /*              SD_task_dotty(task, dotout); */
122 /*      } */
123 /*      fprintf(dotout, "}\n"); */
124 /*      fclose(dotout); */
125 /* } */
126
127 static xbt_dynar_t initDynamicThrottling(int *argc, char *argv[])
128 {
129   /* Initialize SD */
130   SD_init(argc, argv);
131
132   /* Check parameters */
133   checkParameters(*argc,argv);
134
135   /* Create environment */
136   SD_create_environment(argv[1]);
137   /* Load DAX file */
138   xbt_dynar_t dax = SD_daxload(argv[2]);
139
140   //  createDottyFile(dax, argv[2]);
141
142   // Schedule DAX
143   fprintf(stdout, "Scheduling DAX...\n");
144   scheduleDAX(dax);
145   fprintf(stdout, "DAX scheduled\n");
146   SD_simulate(-1);
147   fprintf(stdout, "Simulation end. Time: %f\n", SD_get_clock());
148
149   return dax;
150 }
151
152 /**
153  * Garbage collector :D
154  */
155 static void garbageCollector(xbt_dynar_t dax)
156 {
157   while (!xbt_dynar_is_empty(dax)) {
158     SD_task_t task = xbt_dynar_pop_as(dax, SD_task_t);
159     SD_task_destroy(task);
160   }
161   xbt_dynar_free(&dax);
162   SD_exit();
163 }
164
165
166
167 /**
168  * Main procedure
169  * @param argc
170  * @param argv
171  * @return
172  */
173 int main(int argc, char *argv[])
174 {
175
176   /* Start process... */
177   xbt_dynar_t dax = initDynamicThrottling(&argc, argv);
178
179   // Free memory
180   garbageCollector(dax);
181   return 0;
182 }