forked from tokenspice/tokenspice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp
executable file
·260 lines (198 loc) · 7.23 KB
/
tsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
import cProfile
import importlib
import logging
import pstats
import os
import sys
import brownie
# ========================================================================
# tsp help
HELP_MAIN = """
TokenSPICE - EVM agent-based token simulator
Usage: tsp compile|ganache|run|plot|showstats
tsp compile -- compile Solidity code
tsp ganache -- run local chain
tsp run -- run simulation
tsp plot -- plot results
tsp showstats -- see run stats
"""
def do_help():
print(HELP_MAIN)
# ========================================================================
# tsp ganache
def do_ganache():
os.system("./ganache.py")
# ========================================================================
# tsp compile
def do_compile():
os.system("./compile.sh")
# ========================================================================
# tsp run
HELP_RUN = """
Usage: tsp run NETLIST OUTPUT_DIR [DO_PROFILE]
NETLIST -- string -- pathname for netlist
OUTPUT_DIR -- string -- output directory for csv file.
DO_PROFILE -- bool -- if True, profile. Otherwise don't. Default=False.
"""
def do_run():
if len(sys.argv) not in [4, 5]:
print(HELP_RUN)
sys.exit(0)
# extract inputs
assert sys.argv[1] == "run"
netlist_str = sys.argv[2]
output_dir = sys.argv[3]
do_profile = False
if len(sys.argv) == 5 and sys.argv[4] == "True":
do_profile = True
print(
f"Arguments: NETLIST={netlist_str}, OUTPUT_DIR={output_dir},"
f" DO_PROFILE={do_profile}"
)
# handle corner cases
if os.path.exists(output_dir):
print(f"\nOutput path '{output_dir}' already exists. Exiting.\n")
sys.exit(0)
# make directory
os.mkdir(output_dir)
# go
netlist_module = _importNetlistModule(netlist_str)
netlist_state = netlist_module.SimState()
netlist_log_func = netlist_module.netlist_createLogData
from engine.SimEngine import SimEngine #pylint: disable=import-outside-toplevel
engine = SimEngine(netlist_state, output_dir, netlist_log_func)
if not do_profile:
engine.run()
else:
stats_filename = os.path.join(output_dir, "stats")
cProfile.run("engine.run()", stats_filename)
print(
f"Output stats file: {stats_filename}. To see: tsp showstats "
f"outdir_csv/stats 20 cumulative"
)
print(f"Output directory: {output_dir}")
if brownie.network.is_connected():
brownie.network.disconnect()
def _importNetlistModule(netlist_str: str):
module_str = netlist_str.replace("/", ".").replace(".py", "")
netlist_module = importlib.import_module(module_str)
return netlist_module
# ==========================================================================
# tsp plot
HELP_PLOT = """
Usage: tsp plot NETLIST INPUT_CSV_DIR OUTPUT_PNG_DIR
NETLIST -- string -- pathname for netlist
INPUT_CSV_DIR -- string -- input directory for csv file.
OUTPUT_PNG_DIR -- string -- output directory for png files. Can't exist yet.
"""
def do_plot():
# got the right number of args? If not, output help
if len(sys.argv) not in [5]:
print(HELP_PLOT)
sys.exit(0)
# extract inputs
assert sys.argv[1] == "plot"
netlist_str = sys.argv[2]
input_csv_dir = sys.argv[3]
output_png_dir = sys.argv[4]
#
base_input_csv_filename = "data.csv" # magic number. Set in engine/SimEngine.py
input_csv_filename = os.path.join(input_csv_dir, base_input_csv_filename)
print(
f"Arguments: NETLIST={netlist_str}, INPUT_CSV_DIR={input_csv_dir}, "
f"OUTPUT_PNG_DIR={output_png_dir}"
)
print(f"Base input filename: '{base_input_csv_filename}' (hardcoded)")
print(f"Full input filename: '{input_csv_filename}'")
print()
# corner cases
if not os.path.exists(input_csv_dir):
print(f"Input directory '{input_csv_dir}' does not exist. Exiting.")
sys.exit(0)
if not os.path.exists(input_csv_filename):
print(f"Input filename '{input_csv_filename}' does not exist. Exiting.")
sys.exit(0)
if os.path.exists(output_png_dir):
print(f"Output path '{output_png_dir}' already exists. Exiting.")
sys.exit(0)
# get netlist module
netlist_module = _importNetlistModule(netlist_str)
netlist_plot_instrs_func = netlist_module.netlist_plotInstructions
# main work
from util.plotutil import csvToPngs #pylint: disable=import-outside-toplevel
csvToPngs(input_csv_filename, output_png_dir, netlist_plot_instrs_func)
print("Done")
# ========================================================================
# tsp showstats
HELP_SHOWSTATS = """
Usage: tsp showstats FILENAME NUM_SHOW SORT_BY
FILENAME -- string -- stats filename (including path)
NUM_SHOW -- int -- only show the top 'NUM_SHOW' functions
SORT_BY -- string -- one of 'cumulative', 'time'
cumulative -- cumulative time by a function and its callees. To understand what functions take the most time
internal -- time spent *within* a function, but not callees. To understand what functions were looping a lot, and taking a lot of time.
internal_callers -- which funcs called the ones above
Example: tsp showstats outdir_csv/stats 20 cumulative
"""
def do_showstats():
if len(sys.argv) not in [5]:
print(HELP_SHOWSTATS)
sys.exit(0)
# extract inputs
assert sys.argv[1] == "showstats"
filename = sys.argv[2]
num_show = int(eval(sys.argv[3])) # pylint: disable=eval-used
sort_by = sys.argv[4]
print(f"Argument FILENAME: '{filename}'")
print(f"Argument NUM_SHOW: '{num_show}'")
print(f"Argument SORT_BY: '{sort_by}'")
print()
# corner cases
if not os.path.exists(filename):
print(f"Input file '{filename}' does not exist. Exiting.")
sys.exit(0)
if num_show < 1:
print("Input NUM_SHOW is invalid. Exiting.")
sys.exit(0)
# do work
# Note: assumes python3.6. Different in 3.7+.
p = pstats.Stats(filename)
print("=" * 80)
if sort_by == "cumulative":
print("Highest-impact by cumulative time in a function and its callees")
print("=" * 80)
p.sort_stats("cumulative").print_stats(num_show)
elif sort_by == "internal":
print("Highest-impact by time spent *within* a function")
print("=" * 80)
p.sort_stats("time", "cumulative").print_stats(num_show)
elif sort_by == "internal_callers":
print("Highest-impact by functions looping a lot _and_ taking time")
print("=" * 80)
p.sort_stats("time").print_stats(num_show).print_callers(num_show)
else:
print("Input sort_by is invalid. Exiting.")
sys.exit(0)
print("Done")
# ========================================================================
# main
def do_main():
logging.basicConfig()
logging.getLogger("master").setLevel(logging.INFO)
if len(sys.argv) == 1:
do_help()
elif sys.argv[1] == "compile":
do_compile()
elif sys.argv[1] == "ganache":
do_ganache()
elif sys.argv[1] == "run":
do_run()
elif sys.argv[1] == "plot":
do_plot()
elif sys.argv[1] == "showstats":
do_showstats()
else:
do_help()
if __name__ == "__main__":
do_main()