1 /* Copyright (c) 2009 Da SimGrid Team. All rights reserved. */
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. */
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.
12 #include "xbt/sysdep.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_workload,xbt, "Workload characterisation mecanisms");
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);
22 res->comment=NULL; /* it's not enough to memset for valgrind, apparently */
26 xbt_dynar_t w = xbt_str_split(line," ");
28 if (xbt_dynar_length(w) == 0) {
34 char **words = xbt_dynar_get_ptr(w,0);
36 if (words[i][0] == '[') {
37 sscanf(words[i]+1,"%lg",&(res->date));
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));
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));
51 } else if (!strcmp(words[i],"compute")) {
52 res->action = XBT_WORKLOAD_COMPUTE;
53 sscanf(words[++i],"%lg",&(res->d_arg));
55 xbt_die(bprintf("Unparsable command: %s (in %s)",words[i],line));
58 if (words[i] && words[i][0] == '#') {
59 res->comment = xbt_strdup(strchr(line,'#')+1);
65 void xbt_workload_elm_free(xbt_workload_elm_t cmd) {
76 void xbt_workload_elm_free_voidp(void*cmd) {
77 xbt_workload_elm_free(*(xbt_workload_elm_t*)cmd);
80 char *xbt_workload_elm_to_string(xbt_workload_elm_t cmd) {
85 return xbt_strdup("(null command)");
86 if (cmd->date != -1) {
87 addon=bprintf("[%f] ",cmd->date);
91 addon= bprintf("'%s' ",cmd->who);
92 strcat(res,addon);free(addon);
94 switch (cmd->action) {
95 case XBT_WORKLOAD_COMPUTE:
96 addon=bprintf("computed %f flops",cmd->d_arg);
97 strcat(res,addon);free(addon);
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);
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);
108 xbt_die(bprintf("Unknown command %d in '%s...'",cmd->action,res));
111 addon=bprintf(" (comment: %s)",cmd->comment);
112 strcat(res,addon);free(addon);
114 return xbt_strdup(res);
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;
124 int r = strcmp(c1->who,c2->who);
127 if (c1->date == c2->date)
129 if (c1->date < c2->date)
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);
136 xbt_dynar_t xbt_workload_parse_file(char *filename) {
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);
142 xbt_dynar_t in = xbt_str_split(str_in,"\n");
144 xbt_dynar_t cmds=xbt_dynar_new(sizeof(xbt_workload_elm_t),xbt_workload_elm_free_voidp);
148 xbt_dynar_foreach(in,cursor,line) {
149 xbt_workload_elm_t cmd = xbt_workload_elm_parse(line);
151 xbt_dynar_push(cmds,&cmd);
153 xbt_dynar_shrink(cmds,0);