Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove gras from the main documentation
[simgrid.git] / examples / gras / replay / xbt_workload.c
1 /* Copyright (c) 2009, 2010. 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 /* This datatype stores a a trace as produced by examples/simdag/dax, or other means
8  * It can be replayed in simulation with examples/msg/actions or on a real platform with
9  * examples/gras/replay.
10  */
11
12 #include "xbt/log.h"
13 #include "xbt/sysdep.h"
14 #include "xbt/str.h"
15 #include "workload.h"
16 #include "gras/datadesc.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_workload, xbt,
19                                 "Workload characterisation mecanisms");
20
21
22 xbt_workload_elm_t xbt_workload_elm_parse(char *line)
23 {
24   xbt_workload_elm_t res = xbt_new(s_xbt_workload_elm_t, 1);
25   res->date = -1;
26   res->comment = NULL;          /* it's not enough to memset for valgrind, apparently */
27   res->who = NULL;
28   res->str_arg = NULL;
29
30   xbt_dynar_t w = xbt_str_split(line, " ");
31
32   if (xbt_dynar_is_empty(w)) {
33     free(res);
34     xbt_dynar_free(&w);
35     return NULL;
36   }
37
38   char **words = xbt_dynar_get_ptr(w, 0);
39   int i = 0;
40   if (words[i][0] == '[') {
41     sscanf(words[i] + 1, "%lg", &(res->date));
42     i++;
43   }
44   res->who = xbt_strdup(words[i++]);
45   if (!strcmp(words[i], "recv")) {
46     res->action = XBT_WORKLOAD_RECV;
47     res->str_arg = xbt_strdup(words[++i]);
48     sscanf(words[++i], "%lg", &(res->d_arg));
49
50   } else if (!strcmp(words[i], "send")) {
51     res->action = XBT_WORKLOAD_SEND;
52     res->str_arg = xbt_strdup(words[++i]);
53     sscanf(words[++i], "%lg", &(res->d_arg));
54
55   } else if (!strcmp(words[i], "compute")) {
56     res->action = XBT_WORKLOAD_COMPUTE;
57     sscanf(words[++i], "%lg", &(res->d_arg));
58   } else {
59     xbt_die("Unparsable command: %s (in %s)", words[i], line);
60   }
61   i++;
62   if (words[i] && words[i][0] == '#') {
63     res->comment = xbt_strdup(strchr(line, '#') + 1);
64   }
65
66   xbt_dynar_free(&w);
67   return res;
68 }
69
70 void xbt_workload_elm_free(xbt_workload_elm_t cmd)
71 {
72   if (!cmd)
73     return;
74   free(cmd->who);
75   free(cmd->comment);
76   free(cmd->str_arg);
77   free(cmd);
78 }
79
80 void xbt_workload_elm_free_voidp(void *cmd)
81 {
82   xbt_workload_elm_free(*(xbt_workload_elm_t *) cmd);
83 }
84
85 char *xbt_workload_elm_to_string(xbt_workload_elm_t cmd)
86 {
87   char res[2048];
88   char *addon;
89   res[0] = '\0';
90   if (cmd == NULL)
91     return xbt_strdup("(null command)");
92   if (cmd->date != -1) {
93     addon = bprintf("[%f] ", cmd->date);
94     strcat(res, addon);
95     free(addon);
96   }
97   addon = bprintf("'%s' ", cmd->who);
98   strcat(res, addon);
99   free(addon);
100
101   switch (cmd->action) {
102   case XBT_WORKLOAD_COMPUTE:
103     addon = bprintf("computed %f flops", cmd->d_arg);
104     strcat(res, addon);
105     free(addon);
106     break;
107   case XBT_WORKLOAD_SEND:
108     addon = bprintf("sent %f bytes to '%s'", cmd->d_arg, cmd->str_arg);
109     strcat(res, addon);
110     free(addon);
111     break;
112   case XBT_WORKLOAD_RECV:
113     addon =
114         bprintf("received %f bytes from '%s'", cmd->d_arg, cmd->str_arg);
115     strcat(res, addon);
116     free(addon);
117     break;
118   default:
119     xbt_die("Unknown command %d in '%s...'", cmd->action, res);
120   }
121   if (cmd->comment) {
122     addon = bprintf(" (comment: %s)", cmd->comment);
123     strcat(res, addon);
124     free(addon);
125   }
126   return xbt_strdup(res);
127 }
128
129 int xbt_workload_elm_cmp_who_date(const void *_c1, const void *_c2)
130 {
131   xbt_workload_elm_t c1 = *(xbt_workload_elm_t *) _c1;
132   xbt_workload_elm_t c2 = *(xbt_workload_elm_t *) _c2;
133   if (!c1 || !c1->who)
134     return -1;
135   if (!c2 || !c2->who)
136     return 1;
137   int r = strcmp(c1->who, c2->who);
138   if (r)
139     return r;
140   if (c1->date == c2->date)
141     return 0;
142   if (c1->date < c2->date)
143     return -1;
144   return 1;
145 }
146
147 void xbt_workload_sort_who_date(xbt_dynar_t c)
148 {
149   qsort(xbt_dynar_get_ptr(c, 0), xbt_dynar_length(c),
150         sizeof(xbt_workload_elm_t), xbt_workload_elm_cmp_who_date);
151 }
152
153 xbt_dynar_t xbt_workload_parse_file(char *filename)
154 {
155   FILE *file_in;
156   file_in = fopen(filename, "r");
157   xbt_assert(file_in, "cannot open tracefile '%s'", filename);
158   char *str_in = xbt_str_from_file(file_in);
159   fclose(file_in);
160   xbt_dynar_t in = xbt_str_split(str_in, "\n");
161   free(str_in);
162   xbt_dynar_t cmds =
163       xbt_dynar_new(sizeof(xbt_workload_elm_t),
164                     xbt_workload_elm_free_voidp);
165
166   unsigned int cursor;
167   char *line;
168   xbt_dynar_foreach(in, cursor, line) {
169     xbt_workload_elm_t cmd = xbt_workload_elm_parse(line);
170     if (cmd)
171       xbt_dynar_push(cmds, &cmd);
172   }
173   xbt_dynar_shrink(cmds, 0);
174   xbt_dynar_free(&in);
175   return cmds;
176 }
177
178
179 void xbt_workload_declare_datadesc(void)
180 {
181   xbt_datadesc_type_t ddt;
182
183   ddt = xbt_datadesc_struct("s_xbt_workload_elm_t");
184   xbt_datadesc_struct_append(ddt, "who", xbt_datadesc_by_name("string"));
185   xbt_datadesc_struct_append(ddt, "comment",
186                               xbt_datadesc_by_name("string"));
187   xbt_datadesc_struct_append(ddt, "action", xbt_datadesc_by_name("int"));
188   xbt_datadesc_struct_append(ddt, "date",
189                               xbt_datadesc_by_name("double"));
190   xbt_datadesc_struct_append(ddt, "d_arg",
191                               xbt_datadesc_by_name("double"));
192   xbt_datadesc_struct_append(ddt, "str_arg",
193                               xbt_datadesc_by_name("string"));
194   xbt_datadesc_struct_close(ddt);
195
196   xbt_datadesc_ref("xbt_workload_elm_t", ddt);
197
198   ddt = xbt_datadesc_struct("s_xbt_workload_data_chunk_t");
199   xbt_datadesc_struct_append(ddt, "size", xbt_datadesc_by_name("int"));
200   xbt_datadesc_cb_field_push(ddt, "size");
201   xbt_datadesc_struct_append(ddt, "chunk",
202                               xbt_datadesc_ref_pop_arr
203                               (xbt_datadesc_by_name("char")));
204   xbt_datadesc_struct_close(ddt);
205
206   xbt_datadesc_ref("xbt_workload_data_chunk_t", ddt);
207 }
208
209
210
211 xbt_workload_data_chunk_t xbt_workload_data_chunk_new(int size)
212 {
213   xbt_workload_data_chunk_t res = xbt_new0(s_xbt_workload_data_chunk_t, 1);
214   res->size = size - sizeof(res) - sizeof(int);
215   res->chunk = xbt_new(char, res->size);
216   return res;
217 }
218
219 void xbt_workload_data_chunk_free(xbt_workload_data_chunk_t c)
220 {
221   free(c->chunk);
222   free(c);
223 }