Clean up from my last commit to the trace stuff.
[gem5.git] / src / python / m5 / convert.py
1 # Copyright (c) 2005 The Regents of The University of Michigan
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met: redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer;
8 # redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution;
11 # neither the name of the copyright holders nor the names of its
12 # contributors may be used to endorse or promote products derived from
13 # this software without specific prior written permission.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #
27 # Authors: Nathan Binkert
28
29 # metric prefixes
30 exa = 1.0e18
31 peta = 1.0e15
32 tera = 1.0e12
33 giga = 1.0e9
34 mega = 1.0e6
35 kilo = 1.0e3
36
37 milli = 1.0e-3
38 micro = 1.0e-6
39 nano = 1.0e-9
40 pico = 1.0e-12
41 femto = 1.0e-15
42 atto = 1.0e-18
43
44 # power of 2 prefixes
45 kibi = 1024
46 mebi = kibi * 1024
47 gibi = mebi * 1024
48 tebi = gibi * 1024
49 pebi = tebi * 1024
50 exbi = pebi * 1024
51
52 # memory size configuration stuff
53 def toFloat(value):
54 if not isinstance(value, str):
55 raise TypeError, "wrong type '%s' should be str" % type(value)
56
57 if value.endswith('Ei'):
58 return float(value[:-2]) * exbi
59 elif value.endswith('Pi'):
60 return float(value[:-2]) * pebi
61 elif value.endswith('Ti'):
62 return float(value[:-2]) * tebi
63 elif value.endswith('Gi'):
64 return float(value[:-2]) * gibi
65 elif value.endswith('Mi'):
66 return float(value[:-2]) * mebi
67 elif value.endswith('ki'):
68 return float(value[:-2]) * kibi
69 elif value.endswith('E'):
70 return float(value[:-1]) * exa
71 elif value.endswith('P'):
72 return float(value[:-1]) * peta
73 elif value.endswith('T'):
74 return float(value[:-1]) * tera
75 elif value.endswith('G'):
76 return float(value[:-1]) * giga
77 elif value.endswith('M'):
78 return float(value[:-1]) * mega
79 elif value.endswith('k'):
80 return float(value[:-1]) * kilo
81 elif value.endswith('m'):
82 return float(value[:-1]) * milli
83 elif value.endswith('u'):
84 return float(value[:-1]) * micro
85 elif value.endswith('n'):
86 return float(value[:-1]) * nano
87 elif value.endswith('p'):
88 return float(value[:-1]) * pico
89 elif value.endswith('f'):
90 return float(value[:-1]) * femto
91 else:
92 return float(value)
93
94 def toInteger(value):
95 value = toFloat(value)
96 result = long(value)
97 if value != result:
98 raise ValueError, "cannot convert '%s' to integer" % value
99
100 return result
101
102 _bool_dict = {
103 'true' : True, 't' : True, 'yes' : True, 'y' : True, '1' : True,
104 'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False
105 }
106
107 def toBool(value):
108 if not isinstance(value, str):
109 raise TypeError, "wrong type '%s' should be str" % type(value)
110
111 value = value.lower()
112 result = _bool_dict.get(value, None)
113 if result == None:
114 raise ValueError, "cannot convert '%s' to bool" % value
115 return result
116
117 def toFrequency(value):
118 if not isinstance(value, str):
119 raise TypeError, "wrong type '%s' should be str" % type(value)
120
121 if value.endswith('THz'):
122 return float(value[:-3]) * tera
123 elif value.endswith('GHz'):
124 return float(value[:-3]) * giga
125 elif value.endswith('MHz'):
126 return float(value[:-3]) * mega
127 elif value.endswith('kHz'):
128 return float(value[:-3]) * kilo
129 elif value.endswith('Hz'):
130 return float(value[:-2])
131
132 raise ValueError, "cannot convert '%s' to frequency" % value
133
134 def toLatency(value):
135 if not isinstance(value, str):
136 raise TypeError, "wrong type '%s' should be str" % type(value)
137
138 if value.endswith('ps'):
139 return float(value[:-2]) * pico
140 elif value.endswith('ns'):
141 return float(value[:-2]) * nano
142 elif value.endswith('us'):
143 return float(value[:-2]) * micro
144 elif value.endswith('ms'):
145 return float(value[:-2]) * milli
146 elif value.endswith('s'):
147 return float(value[:-1])
148
149 raise ValueError, "cannot convert '%s' to latency" % value
150
151 def toClockPeriod(value):
152 """result is a clock period"""
153
154 if not isinstance(value, str):
155 raise TypeError, "wrong type '%s' should be str" % type(value)
156
157 try:
158 val = toFrequency(value)
159 if val != 0:
160 val = 1 / val
161 return val
162 except ValueError:
163 pass
164
165 try:
166 val = toLatency(value)
167 return val
168 except ValueError:
169 pass
170
171 raise ValueError, "cannot convert '%s' to clock period" % value
172
173
174 def toNetworkBandwidth(value):
175 if not isinstance(value, str):
176 raise TypeError, "wrong type '%s' should be str" % type(value)
177
178 if value.endswith('Tbps'):
179 return float(value[:-4]) * tera
180 elif value.endswith('Gbps'):
181 return float(value[:-4]) * giga
182 elif value.endswith('Mbps'):
183 return float(value[:-4]) * mega
184 elif value.endswith('kbps'):
185 return float(value[:-4]) * kilo
186 elif value.endswith('bps'):
187 return float(value[:-3])
188 else:
189 return float(value)
190
191 raise ValueError, "cannot convert '%s' to network bandwidth" % value
192
193 def toMemoryBandwidth(value):
194 if not isinstance(value, str):
195 raise TypeError, "wrong type '%s' should be str" % type(value)
196
197 if value.endswith('PB/s'):
198 return float(value[:-4]) * pebi
199 elif value.endswith('TB/s'):
200 return float(value[:-4]) * tebi
201 elif value.endswith('GB/s'):
202 return float(value[:-4]) * gibi
203 elif value.endswith('MB/s'):
204 return float(value[:-4]) * mebi
205 elif value.endswith('kB/s'):
206 return float(value[:-4]) * kibi
207 elif value.endswith('B/s'):
208 return float(value[:-3])
209
210 raise ValueError, "cannot convert '%s' to memory bandwidth" % value
211
212 def toMemorySize(value):
213 if not isinstance(value, str):
214 raise TypeError, "wrong type '%s' should be str" % type(value)
215
216 if value.endswith('PB'):
217 return long(value[:-2]) * pebi
218 elif value.endswith('TB'):
219 return long(value[:-2]) * tebi
220 elif value.endswith('GB'):
221 return long(value[:-2]) * gibi
222 elif value.endswith('MB'):
223 return long(value[:-2]) * mebi
224 elif value.endswith('kB'):
225 return long(value[:-2]) * kibi
226 elif value.endswith('B'):
227 return long(value[:-1])
228
229 raise ValueError, "cannot convert '%s' to memory size" % value