From 7718dd83af39fa831dfa2dfb75cddbf867444aba Mon Sep 17 00:00:00 2001
From: Georges Da Costa <dacosta@irit.fr>
Date: Fri, 2 Jul 2021 21:58:33 +0200
Subject: [PATCH] Corrects C0 leverage

---
 expetator/leverages/nodeepsleep.py | 18 ++++++++++++++----
 setup.py                           |  2 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/expetator/leverages/nodeepsleep.py b/expetator/leverages/nodeepsleep.py
index e78c12a..10c6e6d 100644
--- a/expetator/leverages/nodeepsleep.py
+++ b/expetator/leverages/nodeepsleep.py
@@ -9,26 +9,35 @@ def signal_handler(signal_number, frame):
     os.close(fd)
 
 fd = os.open("/dev/cpu_dma_latency", os.O_RDWR)
-os.write(fd, b'\0\0\0\0')
+os.write(fd, b'\\0\\0\\0\\0')
 
 signal.signal(signal.SIGINT, signal_handler)
 signal.pause()
 """
 
+terminate_script = """#!/bin/bash
+ps aux | grep script_nodeepsleep.py | grep sudo | awk '{print $2}' | xargs sudo-g5k kill
+"""
+
 class Nodeepsleep:
     'Activates or deactivates CX'
     def __init__(self, modes = {True, False}):
         self.modes = modes
         self.binary = '/tmp/bin/script_nodeepsleep.py'
+        self.terminate = '/tmp/bin/terminate_nodeepsleep.py'
         self.cur_mode = False
         
     def build(self, executor):
         'Builds the software'
         self.executor = executor
         with open(self.binary, 'w') as out_file:
-            out_file.write(script)
+            out_file.write(source_script)
         os.chmod(self.binary, 0o777)
 
+        with open(self.terminate, 'w') as out_file:
+            out_file.write(terminate_script)
+        os.chmod(self.terminate, 0o777)
+        
     def available_states(self):
         'Returns all the modes'
         return self.modes
@@ -40,8 +49,9 @@ class Nodeepsleep:
             self.executor.hosts(self.binary+' &', root=True)
 
     def stop(self, output_file=None):
-        'Reverts to the maximum frequency'
-        self.executor.hosts('killall script_nodeepsleep.py', root=True)
+        'Stops the script'
+        if self.curr_mode:
+            self.executor.hosts(self.terminate, root=True)
         self.curr_mode = False
             
     def get_state(self):
diff --git a/setup.py b/setup.py
index 6f28977..eded41c 100644
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
 
 setuptools.setup(
     name="expetator",
-    version="0.2.3",
+    version="0.2.5",
     author="Georges Da Costa",
     author_email="georges.da-costa@irit.fr",
     description="A framework for monitoring HPC applications using DVFS",
-- 
GitLab