Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6b07413fa124df0a01652d369816b8b653c7b9c7
[simgrid.git] / tools / MSG_visualization / trace2fig.pl
1 #!/usr/bin/perl -w
2 use strict;
3 #use Data::Dumper;
4 use XFig;
5
6 my($grid_Y_size)=225; # xfig 
7 my($grid_X_size)=550; # xfig 
8
9 sub read_cat {
10     my(%Cat);
11     my($filename)=@_;
12     my($line);
13
14     open INPUT, $filename;
15
16     while (defined($line=<INPUT>)) {
17         chomp $line;
18         if($line =~ /^7\s/) {
19             my($event,$date,$id,$type,$father,@name) = split(/\s+/,$line);
20
21             $Cat{$id}{name}="@name ";
22             $Cat{$id}{name}=~s/\"//g;
23             $Cat{$id}{father}=$father;
24             $Cat{$id}{type}=$type;
25             $Cat{$id}{date}=$date;
26         }
27     }
28     close INPUT;
29     return \%Cat;
30 }
31
32
33 sub read_event {
34     my($filename,$Cat)=@_;
35     my($line);
36
37     open INPUT, $filename;
38
39     while (defined($line=<INPUT>)) {
40         chomp $line;
41         if($line =~ /^11\s/) {
42             my($event,$date,$type,$id,$state) = split(/\s+/,$line);
43             push @{$$Cat{$id}{state}}, [$date,$state];
44         }
45         if($line =~ /^12\s/) {
46             my($event,$date,$type,$id) = split(/\s+/,$line);
47             push @{$$Cat{$id}{state}}, [$date];
48         }
49     }
50     close INPUT;
51 }
52
53 sub read_link {
54     my($filename)=@_;
55     my($line);
56     my(%link);
57
58     open INPUT, $filename;
59
60     while (defined($line=<INPUT>)) {
61         chomp $line;
62         if($line =~ /^16\s/) {
63             my($event,$date,$type,$father,$channel,$src,$key) = split(/\s+/,$line);
64             $link{$key}{src}=$src;
65             $link{$key}{src_date}=$date;
66         }
67         if($line =~ /^17\s/) {
68             my($event,$date,$type,$father,$channel,$dst,$key) = split(/\s+/,$line);
69             $link{$key}{dst}=$dst;
70             $link{$key}{dst_date}=$date;
71         }
72     }
73     close INPUT;
74     return \%link;
75 }
76
77
78 sub build_cat_tree {
79     my($root,$Cat)=@_;
80     my(@childs)=();
81     my($cat);
82
83     foreach $cat (keys %$Cat) {
84         if($$Cat{$cat}{father} eq $root) {
85             push @childs, build_cat_tree($cat,$Cat);
86         }
87 #       print "$$Cat{$cat}{name}\t\t $Cat{$cat}{father}\n";
88     }
89     
90     return [$root,@childs];
91 }
92
93 sub build_cat_list {
94     my($tree,$cat_list)=@_;
95     my($root) = shift @$tree;
96     my($u);
97     
98     push @$cat_list,$root;
99
100     foreach $u (@$tree) {
101         build_cat_list($u,$cat_list);
102     }
103     unshift @$tree, $root;
104 }
105
106
107 sub set_cat_position {
108     my($Cat,$cat_list)=@_;
109     my($i)=0;
110     my($cat);
111     foreach $cat (@$cat_list) {
112         if(defined($$Cat{$cat}{state})) {
113             $$Cat{$cat}{Y_min} = $i;
114             $$Cat{$cat}{Y_max} = $i+1;
115             $i++;
116         }
117     }
118 }
119
120 sub create_fig {
121     my($filename)=shift;
122     my($fig)=new XFig;
123     $fig->{object} = 'compound'; # Compound
124     $fig->{elements} = [];
125     $fig->{version} = 3.2;
126     $fig->{orientation}   = 'Landscape';
127     $fig->{justification} = 'Center';
128     $fig->{units}         = 'Metric';
129     $fig->{papersize}     = 'A4';
130     $fig->{magnification} = '100.00';
131     $fig->{multiplepage}  = 'Single';
132     $fig->{transparent}   = '-2';
133     $fig->{resolution}    = '1200';
134     $fig->{coordsystem}   = '2';
135     $fig->{filename}   = $filename;
136     return $fig;
137 }
138
139 sub draw_cat {
140     my($fig,$Cat,$Link)=@_;
141     my($cat,$e,$link);
142     foreach $cat (keys %$Cat) {
143         next unless (defined($$Cat{$cat}{Y_min}) && 
144                      defined($$Cat{$cat}{Y_max}));
145         my($text) = new XFig ('text');
146         $text->{'text'} = $$Cat{$cat}{name};
147         $text->{'y'} = ($$Cat{$cat}{Y_min}+$$Cat{$cat}{Y_max})/2*$grid_Y_size+68;
148         $fig->add ($text);
149     }
150     foreach $cat (keys %$Cat) {
151         next unless (defined($$Cat{$cat}{Y_min}) && 
152                      defined($$Cat{$cat}{Y_max}));
153         my(@states)=();
154         my($e);
155         foreach $e (@{$$Cat{$cat}{state}}) {
156             my($new_date,$state) = ($$e[0],$$e[1]);
157             if(defined($state)) {
158                 push @states, $e;
159             } else {
160                 my($old_event) = pop @states;
161                 my($old_date) = $$old_event[0];
162                 $state = $$old_event[1];
163                 
164                 my($line) = new XFig ('polyline');
165
166                 $line->{'subtype'} = 1;  # line
167                 $line->{'points'} = [ [$old_date*$grid_X_size, $$Cat{$cat}{Y_min}*$grid_Y_size],
168                                       [$new_date*$grid_X_size, $$Cat{$cat}{Y_min}*$grid_Y_size],
169                                       [$new_date*$grid_X_size, $$Cat{$cat}{Y_max}*$grid_Y_size],
170                                       [$old_date*$grid_X_size, $$Cat{$cat}{Y_max}*$grid_Y_size] ];
171                 $line->{'areafill'} = 20;
172                 if($state eq "S") {
173                     $line->{'fillcolor'} = 1;
174                 } elsif ($state eq "E") {
175                     $line->{'fillcolor'} = 2;
176                 } elsif ($state eq "B") {
177                     $line->{'fillcolor'} = 3;
178                 } elsif ($state eq "C") {
179                     $line->{'fillcolor'} = 4;
180                 }
181                 $fig->add ($line);
182             }
183         }
184     }
185
186     foreach $link (keys %$Link) {
187         my($line) = new XFig ('polyline');
188         my($src_date)=$$Link{$link}{src_date};
189         my($src)=$$Link{$link}{src};
190         my($dst_date)=$$Link{$link}{dst_date};
191         my($dst)=$$Link{$link}{dst};
192         $line->{'subtype'} = 1;  # line
193
194         $line->{'points'} = [ [$src_date*$grid_X_size, 
195                                ($$Cat{$src}{Y_min}+$$Cat{$src}{Y_max})/2*$grid_Y_size],
196                               [$dst_date*$grid_X_size, 
197                                ($$Cat{$dst}{Y_min}+$$Cat{$dst}{Y_max})/2*$grid_Y_size] ];
198         $line->{'forwardarrow'} = ['1', '1', '1.00', '60.00', '120.00'];
199         $fig->add ($line);
200     }
201 }
202
203 sub main {
204     my($Cat) = read_cat($ARGV[0]);
205     my($cat_tree)=build_cat_tree("0",$Cat);
206     read_event($ARGV[0],$Cat);
207     my($Link)=read_link($ARGV[0]);
208 #    print Dumper($cat_tree);
209 #    print Dumper($Cat);
210     my($cat_list)=[];
211     build_cat_list($cat_tree,$cat_list);
212     shift @$cat_list;
213     shift @$cat_list;
214     print "@$cat_list \n";
215
216     set_cat_position($Cat,$cat_list);
217     
218     my($fig)=create_fig("toto.fig");
219     draw_cat($fig,$Cat,$Link);
220     $fig->writefile ();
221     system "fig2dev -L pdf toto.fig toto.pdf";
222 }
223
224 main;