-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc-example.py
157 lines (132 loc) · 4.87 KB
/
rpc-example.py
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
# ----------------------------------------------------------------------------
# File: <rpc-example>.py
#
# Description:
# <Example usage of the gEMA rpc API>.
#
# Contact:
# For inquiries, please contact Alex Manley ([email protected]).
#
# License:
# This project is licensed under the MIT License. See the LICENSE file
# in the repository root for more information.
# ----------------------------------------------------------------------------
import readline
from xmlrpc.client import ServerProxy
def create_config_1(gema_server: ServerProxy):
"""Creates config 1 using gema RPC methods."""
gema_server.add_config(1)
gema_server.set_board(1, "SimpleBoard", 3.5)
gema_server.set_processor(1, "x86", "SimpleProcessor", "timing", 1)
gema_server.set_memory(1, "SingleChannelDDR3_1600", 1024)
gema_server.set_cache(1, "PrivateL1PrivateL2CacheHierarchy", 64, 64, 256)
gema_server.set_resource(1, "x86-hello64-static")
def create_config_2(gema_server: ServerProxy):
"""Creates config 2 using gema RPC methods and a predefined dictionary."""
example_cfg = {
"board": {"type": "SimpleBoard", "clk": 3.0},
"processor": {
"isa": "x86",
"type": "SimpleProcessor",
"cpu": "timing",
"ncores": 2,
},
"memory": {"type": "SingleChannelDDR3_1600", "size": 2048},
"cache": {
"type": "PrivateL1CacheHierarchy",
"l1d_size": 64,
"l1i_size": 64,
"l2_size": 0,
"l1d_assoc": 0,
"l1i_assoc": 0,
"l2_assoc": 0,
},
"resource": {"name": "x86-npb-cg-size-s"},
}
gema_server.add_config(2, example_cfg)
def convert_arg(arg):
"""Try to convert the argument to an appropriate type (int, float, or leave as str)."""
try:
return int(arg)
except ValueError:
try:
return float(arg)
except ValueError:
return arg
def interactive_terminal(server):
"""Interactive terminal to execute remote commands."""
print(
"Type 'exit' to quit. Type 'history' or use the arrow keys to access command history."
)
print("For help, call the 'get_endpoints' method or consult the README.")
while True:
try:
user_input = input("\nEnter command: ").strip()
if user_input == "exit":
print("Exiting the terminal.")
break
if user_input == "history":
print("\nCommand History:")
for i in range(readline.get_current_history_length()):
print(f"{i+1}: {readline.get_history_item(i+1)}")
continue
command_parts = user_input.split()
method_name = command_parts[0]
args = command_parts[1:]
converted_args = [convert_arg(arg) for arg in args]
result = getattr(server, method_name)(*converted_args)
print(result)
except Exception as e:
print(f"Error: {e}")
def setup():
port = input(
"Enter the port number to connect to the server (default is 8000): "
).strip()
if not port:
port = 8000
else:
try:
port = int(port)
except ValueError:
print("Invalid port number. Using default port 8000.")
port = 8000
host_ip = f"http://localhost:{port}"
server = ServerProxy(host_ip)
print("RPC Terminal connected to:", host_ip)
return server
def main(server):
# Ask the user which mode to operate in
print("\nChoose the operation mode:")
print("1. Predefined Configuration Setup")
print("2. Interactive RPC Command Terminal")
mode_choice = input("Choice (1 or 2): ").strip()
if mode_choice == "1":
# Ask the user which configuration to create
print(
"\nThis example demonstrates running simulations in a simple (gem5-like) script."
)
print("Choose the configuration method to try: ")
print("1. RPC Functions")
print("2. Pre-defined Dictionary Data")
config_choice = input("Choice (1 or 2): ").strip()
if config_choice == "1":
create_config_1(server)
print("Config 1 created successfully.")
print("Starting simulation for Config 1...")
print(server.run_simulation(1))
print(server.manage_sim(1, "status"))
elif config_choice == "2":
create_config_2(server)
print("Config 2 created successfully.")
print("Starting simulation for Config 2...")
print(server.run_simulation(2))
else:
print("Invalid configuration choice. Exiting program.")
elif mode_choice == "2":
interactive_terminal(server)
else:
print("Invalid mode choice. Exiting program.")
if __name__ == "__main__":
server = setup()
while True:
main(server)