Skip to content

Commit

Permalink
Change to monitor command line and python evaluation process
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneDutto committed Sep 4, 2020
1 parent ed6d5b0 commit e6c9ec1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 84 deletions.
145 changes: 82 additions & 63 deletions ML/eval_proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,101 @@

import socket
import torch
import sys
import torch.nn as nn
import numpy as np

PATH= "model_NN50_20.pth"
PATH= "/home/sgundo/Desktop/Tesi/OS_FaultMonitor/ML/"
len_index = 20

class BinaryClassification(nn.Module):
def __init__(self):
super(BinaryClassification, self).__init__()
def __init__(self):
super(BinaryClassification, self).__init__()

self.features = nn.Sequential(
nn.Linear(len_index-1, 32),
nn.BatchNorm1d(32),
nn.Dropout(0.5), #50 % probability
nn.LeakyReLU(),
torch.nn.Linear(32, 64),
nn.BatchNorm1d(64),
torch.nn.Dropout(0.2), #20% probability
torch.nn.LeakyReLU(),
torch.nn.Linear(64, 22),
nn.BatchNorm1d(22),
torch.nn.LeakyReLU())
self.classifier = torch.nn.Linear(22, 2)
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x
self.features = nn.Sequential(
nn.Linear(len_index-1, 32),
nn.BatchNorm1d(32),
nn.Dropout(0.5), #50 % probability
nn.LeakyReLU(),
torch.nn.Linear(32, 64),
nn.BatchNorm1d(64),
torch.nn.Dropout(0.2), #20% probability
torch.nn.LeakyReLU(),
torch.nn.Linear(64, 22),
nn.BatchNorm1d(22),
torch.nn.LeakyReLU())
self.classifier = torch.nn.Linear(22, 2)
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x

def eval(feat):
X = np.array(feat)
X = torch.from_numpy(X).float()
X = X.unsqueeze(0)
y = model(X)
y = torch.log_softmax(y, dim=1)
_, y = torch.max(y, dim = 1)
return y.item()
X = np.array(feat)
X = torch.from_numpy(X).float()
X = X.unsqueeze(0)
y = model(X)
y = torch.log_softmax(y, dim=1)
_, y = torch.max(y, dim = 1)
return y.item()

f_s = True # read metrics from file or receive it from socket
flip = 0
cmd = sys.argv[1]
bi = sys.argv[2]
model = BinaryClassification()
model.load_state_dict(torch.load(PATH, map_location=torch.device('cpu')))
model.load_state_dict(torch.load(PATH+bi+".pth", map_location=torch.device('cpu')))
model.eval()
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 2325 # Port to listen on (non-privileged ports are > 1023)
i = 0
num_feat = 19
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
i = 0
feat = list()
for i in range(0, num_feat+1):
data = conn.recv(4)
num = int.from_bytes(data, byteorder='little', signed=False)
if i==0:
bina=num
continue
feat.append(num)
if not data:
break
print("Evaluate process bin: "+str(bina))
result = eval(feat);
if flip == 0:
result=1
flip=1
else:
flip=0
result=0
if result == 0:
conn.sendall(b"0")
print("Sent not fault valuation")
else:
conn.sendall(b"1")
print("Sent fault valuation")
num_feat = 7

if f_s:
with open(cmd+".out", "r") as file:
first_line = file.readline()
for last_line in file:
pass
print (last_line)
attr = last_line.split()
attr = attr[3:]
print(attr)
attr_int = [int(i) for i in attr]
if len(attr_int) == 7:
eval(attr_int)

else:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
i = 0
feat = list()
for i in range(0, num_feat+1):
data = conn.recv(4)
num = int.from_bytes(data, byteorder='little', signed=False)
if i==0:
bina=num
continue
feat.append(num)
if not data:
break
print("Evaluate process bin: "+str(bina))
result = eval(feat)
if flip == 0:
result=1
flip=1
else:
flip=0
result=0
if result == 0:
conn.sendall(b"0")
print("Sent not fault valuation")
else:
conn.sendall(b"1")
print("Sent fault valuation")
31 changes: 17 additions & 14 deletions cmd/monitor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Command line utility to start a program and signal PID to kernel monitor
# Command line utility to start a program and monitor it
cmd=""
i="0"

Expand All @@ -8,14 +8,14 @@ while getopts ":b:" opt; do
b )
target=$OPTARG
if [ "$target" == "bitcount" ];then
bin=0
elif [ "$target" == "basicmath" ];then
bin=1
elif [ "$target" =="qsort" ];then
bin=2
else
bin=-1
fi
bin=0
elif [ "$target" == "basicmath" ];then
bin=1
elif [ "$target" =="qsort" ];then
bin=2
else
bin=-1
fi
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
Expand All @@ -39,8 +39,11 @@ do

done
echo "comando lanciato $cmd"
nohup $cmd &
pid=$!
echo $pid
echo "monitor lanciato"
echo "$pid $bin" > /proc/monitor

conf="instr_retired,unhalted_ref_cycles_fixed,cycles,instr,llc_references,llc_misses,branch_instr_retired"
nohup pmctrack -T 1 -c $conf $cmd > $cmd.out
python3 /home/sgundo/Desktop/Tesi/OS_FaultMonitor/ML/eval_proc.py $cmd $target
# pid=$!
# echo $pid
# echo "monitor lanciato"
#echo "$pid $bin" > /proc/monitor
2 changes: 2 additions & 0 deletions kernel_module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ obj-m += monitor.o
monitor-objs := src/monitor.o src/connt.o src/m_pfile.o src/f_list.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
install:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
20 changes: 13 additions & 7 deletions kernel_module/src/f_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,26 @@ static LIST_HEAD(f_list);
static unsigned int* get_randoms(unsigned int id){
int i = 0;
unsigned int *fts = NULL;
uint64_t value;
uint64_t value=0;

fts = kzalloc(NUM*sizeof(unsigned int), GFP_KERNEL);
if(fts == NULL) return NULL;


//pmctrack_hwmon_export_device(NULL, 0, NULL);
for(i=0;i<NUM;i++){
//get_random_bytes(&fts[i], sizeof(unsigned int));
pmcs_get_current_metric_value(get_pid_task(find_get_pid(id),PIDTYPE_PID),\
0, &value);
value = -1;
//get_random_bytes(&fts[i], sizeof(unsigned int));
struct task_struct* task = get_pid_task(find_get_pid(id), PIDTYPE_PID);
if(task == NULL){
pr_err("ERROR: task not found\n");
return NULL;
}
pmcs_get_current_metric_value(task, 0, &value);
if(value != -1)
pr_info("PMC value: %llu", value);
fts[i] = (unsigned int) value;

}

return fts;
}

Expand Down
1 change: 1 addition & 0 deletions kernel_module/src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
MODULE_AUTHOR("Simone Dutto");
MODULE_DESCRIPTION("module to evaluate faulty proc");
MODULE_LICENSE("GPL");
MODULE_SOFTDEP("pre: mchw_intel_core");

static void proc_eval_handler(struct work_struct *w);
static void feat_updater(struct work_struct *w);
Expand Down

0 comments on commit e6c9ec1

Please sign in to comment.