-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
90 lines (81 loc) · 2.85 KB
/
server.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
#!/usr/local/bin/python3
import sys
import argparse
import os
import subprocess
import asyncio
from aiohttp import web
import signal
import time
import socket
import multiprocessing
if f"{os.environ['DYLINX_HOME']}/python" not in sys.path:
sys.path.append(f"{os.environ['DYLINX_HOME']}/python")
from Dylinx import BaseSubject
class DylinxSubject(BaseSubject):
def __init__(self, cc_path):
super().__init__(cc_path)
self.repo = os.path.abspath(os.path.dirname(self.cc_path))
self.xray_options = "patch_premain=true xray_mode=xray-basic xray_logfile_base=xray-log/ "
self.basic_options = "func_duration_threshold_us=0"
def build_repo(self, id2type):
super().configure_type(id2type)
cmd = f"cd repo; make -f dylinx.mk memcached-dlx"
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.DEVNULL, shell=True)
err_msg = proc.stderr.read().decode("utf-8")
if len(err_msg) != 0:
print(err_msg)
def execute_repo(self, active_xray):
os.environ["LD_LIBRARY_PATH"] = ":".join([
f"{self.repo}/.dylinx/lib",
f"{os.environ['DYLINX_HOME']}/build/lib"
])
if not active_xray:
self.task = subprocess.Popen(
[f"{self.repo}/memcached-dlx", "-c", "100000", "-l", "0.0.0.0", "-t", f"{multiprocessing.cpu_count()}"],
stdout=subprocess.DEVNULL
)
else:
os.environ["XRAY_OPTIONS"] = self.xray_options
os.environ["XRAY_BASIC_OPTIONS"] = self.basic_options
print(os.environ["XRAY_OPTIONS"])
print(os.environ["XRAY_BASIC_OPTIONS"])
self.task = subprocess.Popen(
[f"{self.repo}/memcached-dlx", "-c", "100000", "-l", "0.0.0.0", "-t", f"{multiprocessing.cpu_count()}"],
stdout=subprocess.DEVNULL, env=os.environ
)
time.sleep(3)
def stop_repo(self):
self.task.kill()
async def init_subject(request):
global subject
subject = DylinxSubject("./repo/compiler_commands.json")
return web.json_response({
"err": 0,
"sites": subject.get_pluggable(),
"hostname": socket.gethostname(),
})
async def set_subject(request):
global subject
print("set memcached...")
param = await request.json()
subject.build_repo(param["slots"])
subject.execute_repo(param["active_xray"])
return web.json_response({
"err": 0,
})
async def stop_subject(request):
global subject
subject.stop_repo()
print("stop memcached...")
return web.json_response({
"err": 0
})
if __name__ == "__main__":
app = web.Application()
app.add_routes([
web.get('/init', init_subject),
web.post('/set', set_subject),
web.get('/stop', stop_subject),
])
web.run_app(app, host="0.0.0.0", port=8787)