diff --git a/ML/eval_proc.py b/ML/eval_proc.py index 1bb743c..10fbe00 100644 --- a/ML/eval_proc.py +++ b/ML/eval_proc.py @@ -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") diff --git a/cmd/monitor b/cmd/monitor index 2a808c7..41af10e 100755 --- a/cmd/monitor +++ b/cmd/monitor @@ -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" @@ -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 @@ -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 diff --git a/kernel_module/Makefile b/kernel_module/Makefile index ddb0ccd..64bd3b0 100644 --- a/kernel_module/Makefile +++ b/kernel_module/Makefile @@ -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 diff --git a/kernel_module/src/f_list.c b/kernel_module/src/f_list.c index 1fd9c12..fdfa283 100644 --- a/kernel_module/src/f_list.c +++ b/kernel_module/src/f_list.c @@ -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