Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
python: add exec-dvfs example
[simgrid.git] / tools / sg_xml_unit_converter.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2013-2019. The SimGrid Team.
5 # All rights reserved.
6
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the license (GNU LGPL) which comes with this package.
9
10 # grep -ohrI 'bw=".*"' . | sort | uniq
11
12 import sys
13 import fnmatch
14 import os
15 from decimal import Decimal, DecimalException
16 import re
17
18
19 def to_str(dec):
20     return re.sub(r"(\.\d*?)0*$", r"\1", dec.to_eng_string()).rstrip(".")
21
22
23 def convert(xml, formats, attrib):
24     res = []
25     m = re.search(r'%s="(.*?)"' % attrib, xml)
26     while m:
27         b, e = m.span(1)
28         res.append(xml[:b])
29         val = xml[b:e]
30         xml = xml[e:]
31         try:
32             power = Decimal(val)
33             tmp = to_str(power)
34             for p, f in formats:
35                 d = power / p
36                 if d >= 1.0:
37                     tmp = "%s%s" % (to_str(d), f)
38                     break
39             res.append(tmp)
40         except DecimalException:
41             print "Error with:", val
42             res.append(val)
43         m = re.search(r'%s="(.*?)"' % attrib, xml)
44
45     res.append(xml)
46     return "".join(res)
47
48
49 def formats(ll):
50     return sorted(((Decimal(i), j) for i, j in ll), key=lambda x: x[0], reverse=True)
51
52
53 for root, dirnames, filenames in os.walk(sys.argv[1]):
54     for filename in fnmatch.filter(filenames, '*.xml'):
55         print root, dirnames, filename
56         path = os.path.join(root, filename)
57         xml = open(path).read()
58
59         power_formats = formats([("1E0", "f"),
60                                  ("1E3", "kf"),
61                                  ("1E6", "Mf"),
62                                  ("1E9", "Gf"),
63                                  ("1E12", "Tf"),
64                                  ("1E15", "Pt"),
65                                  ("1E18", "Ef"),
66                                  ("1E21", "Zf")])
67         xml = convert(xml, power_formats, "power")
68
69         bandwidth_formats = formats([("1E0", "Bps"),
70                                      ("1E3", "kBps"),
71                                      ("1E6", "MBps"),
72                                      ("1E9", "GBps"),
73                                      ("1E12", "TBps")])
74         xml = convert(xml, bandwidth_formats, "bandwidth")
75         xml = convert(xml, bandwidth_formats, "bw")
76         xml = convert(xml, bandwidth_formats, "bb_bw")
77         xml = convert(xml, bandwidth_formats, "bw_in")
78         xml = convert(xml, bandwidth_formats, "bw_out")
79
80         time_formats = formats([("1E-12", "ps"),
81                                 ("1E-9", "ns"),
82                                 ("1E-6", "us"),
83                                 ("1E-3", "ms"),
84                                 ("1E0", "s"),
85                                 ("60E0", "m"),
86                                 ("3600E0", "h"),
87                                 ("86400E0", "d"),
88                                 ("604800E0", "w")])
89         xml = convert(xml, time_formats, "latency")
90         xml = convert(xml, time_formats, "lat")
91         xml = convert(xml, time_formats, "bb_lat")
92
93         # print xml
94         outfile = open(path, "w")
95         outfile.write(xml)
96         outfile.close()