Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More verbose error messages
[simgrid.git] / examples / gras / replay / xbt_workload.c
1 /* Copyright (c) 2009 Da SimGrid Team.  All rights reserved.                */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* This datatype stores a a trace as produced by examples/simdag/dax, or other means
7  * It can be replayed in simulation with examples/msg/actions or on a real platform with
8  * examples/gras/replay.
9  */
10
11 #include "xbt/log.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/str.h"
14 #include "workload.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_workload,xbt, "Workload characterisation mecanisms");
17
18
19 xbt_workload_elm_t xbt_workload_elm_parse(char *line) {
20   xbt_workload_elm_t res = xbt_new(s_xbt_workload_elm_t,1);
21   res->date=-1;
22   res->comment=NULL; /* it's not enough to memset for valgrind, apparently */
23   res->who=NULL;
24   res->str_arg=NULL;
25
26   xbt_dynar_t w = xbt_str_split(line," ");
27
28   if (xbt_dynar_length(w) == 0) {
29     free(res);
30     xbt_dynar_free(&w);
31     return NULL;
32   }
33
34   char **words = xbt_dynar_get_ptr(w,0);
35   int i=0;
36   if (words[i][0] == '[') {
37     sscanf(words[i]+1,"%lg",&(res->date));
38     i++;
39   }
40   res->who = xbt_strdup(words[i++]);
41   if (!strcmp(words[i],"recv")) {
42     res->action = XBT_WORKLOAD_RECV;
43     res->str_arg = xbt_strdup(words[++i]);
44     sscanf(words[++i],"%lg",&(res->d_arg));
45
46   } else if (!strcmp(words[i],"send")) {
47     res->action = XBT_WORKLOAD_SEND;
48     res->str_arg = xbt_strdup(words[++i]);
49     sscanf(words[++i],"%lg",&(res->d_arg));
50
51   } else if (!strcmp(words[i],"compute")) {
52     res->action = XBT_WORKLOAD_COMPUTE;
53     sscanf(words[++i],"%lg",&(res->d_arg));
54   } else {
55     xbt_die(bprintf("Unparsable command: %s (in %s)",words[i],line));
56   }
57   i++;
58   if (words[i] && words[i][0] == '#') {
59     res->comment = xbt_strdup(strchr(line,'#')+1);
60   }
61
62   xbt_dynar_free(&w);
63   return res;
64 }
65 void xbt_workload_elm_free(xbt_workload_elm_t cmd) {
66   if (!cmd)
67     return;
68   if (cmd->who)
69     free(cmd->who);
70   if (cmd->comment)
71     free(cmd->comment);
72   if (cmd->str_arg)
73     free(cmd->str_arg);
74   free(cmd);
75 }
76 void xbt_workload_elm_free_voidp(void*cmd) {
77   xbt_workload_elm_free(*(xbt_workload_elm_t*)cmd);
78 }
79
80 char *xbt_workload_elm_to_string(xbt_workload_elm_t cmd) {
81   char res[2048];
82   char *addon;
83   res[0]='\0';
84   if (cmd==NULL)
85     return xbt_strdup("(null command)");
86   if (cmd->date != -1) {
87     addon=bprintf("[%f] ",cmd->date);
88     strcat(res,addon);
89     free(addon);
90   }
91   addon= bprintf("'%s' ",cmd->who);
92   strcat(res,addon);free(addon);
93
94   switch (cmd->action) {
95   case XBT_WORKLOAD_COMPUTE:
96     addon=bprintf("computed %f flops",cmd->d_arg);
97     strcat(res,addon);free(addon);
98     break;
99   case XBT_WORKLOAD_SEND:
100     addon=bprintf("sent %f bytes to '%s'",cmd->d_arg,cmd->str_arg);
101     strcat(res,addon);free(addon);
102     break;
103   case XBT_WORKLOAD_RECV:
104     addon=bprintf("received %f bytes from '%s'",cmd->d_arg,cmd->str_arg);
105     strcat(res,addon);free(addon);
106     break;
107   default:
108     xbt_die(bprintf("Unknown command %d in '%s...'",cmd->action,res));
109   }
110   if (cmd->comment) {
111     addon=bprintf(" (comment: %s)",cmd->comment);
112     strcat(res,addon);free(addon);
113   }
114   return xbt_strdup(res);
115 }
116
117 int xbt_workload_elm_cmp_who_date(const void* _c1, const void* _c2) {
118   xbt_workload_elm_t c1=*(xbt_workload_elm_t*)_c1;
119   xbt_workload_elm_t c2=*(xbt_workload_elm_t*)_c2;
120   if (!c1 || !c1->who)
121     return -1;
122   if (!c2 || !c2->who)
123     return 1;
124   int r = strcmp(c1->who,c2->who);
125   if (r)
126     return r;
127   if (c1->date == c2->date)
128     return 0;
129   if (c1->date < c2->date)
130     return -1;
131   return 1;
132 }
133 void xbt_workload_sort_who_date(xbt_dynar_t c) {
134   qsort(xbt_dynar_get_ptr(c,0),xbt_dynar_length(c),sizeof(xbt_workload_elm_t),xbt_workload_elm_cmp_who_date);
135 }
136 xbt_dynar_t xbt_workload_parse_file(char *filename) {
137   FILE *file_in;
138   file_in = fopen(filename,"r");
139   xbt_assert1(file_in, "cannot open tracefile '%s'",filename);
140   char *str_in = xbt_str_from_file(file_in);
141   fclose(file_in);
142   xbt_dynar_t in = xbt_str_split(str_in,"\n");
143   free(str_in);
144   xbt_dynar_t cmds=xbt_dynar_new(sizeof(xbt_workload_elm_t),xbt_workload_elm_free_voidp);
145
146   unsigned int cursor;
147   char *line;
148   xbt_dynar_foreach(in,cursor,line) {
149     xbt_workload_elm_t cmd = xbt_workload_elm_parse(line);
150     if (cmd)
151       xbt_dynar_push(cmds,&cmd);
152   }
153   xbt_dynar_shrink(cmds,0);
154   xbt_dynar_free(&in);
155   return cmds;
156 }