#
# Copyright 2013-2013 Ghent University
# Copyright 2013-2013 Stijn De Weirdt
#
# This file is part of VSC-tools,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://vscentrum.be/nl/en),
# the Hercules foundation (http://www.herculesstichting.be/in_English)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# http://github.com/hpcugent/VSC-tools
#
# VSC-tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# VSC-tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with VSC-tools. If not, see <http://www.gnu.org/licenses/>.
#
"""
Implement Priority and Affinity classes using vsc.utils.affinity
"""

from .priority import Priority
from .affinity import Affinity
from vsc.utils.affinity import sched_getaffinity, sched_setaffinity, cpu_set_t, getpriority, setpriority

class VSCPriority(Priority):
    def get_priority(self):
        return getpriority(who=self.pid)

    def _set_priority(self,newprio):
        setpriority(newprio, who=self.pid)

@@ -1217,24 +1217,66 @@
 rt Affinity%0A
+from .algorithm import BasicCore%0Atry:%0A    
 from vsc.uti
@@ -1372,37 +1372,147 @@
 ity%0A
-%0Aclass VSCPriority(Priority):
+    valid = True%0Aexcept:%0A    vaild = False%0A%0AMODE_NAME = 'vsc'%0A%0Aclass VSCPriority(Priority):%0A    PRIORITY_MODE = MODE_NAME%0A    VALID = valid
 %0A   
@@ -1516,16 +1516,17 @@
     def 
+_
 get_prio
@@ -1549,14 +1549,23 @@
     
-return
+self.priority =
  get
@@ -1618,58 +1618,1332 @@
 self
-,newprio):%0A        setpriority(newprio, who=self.pid
+):%0A        setpriority(self.priority, who=self.pid)%0A%0Aclass VSCAffinity(Affinity):%0A    AFFINITY_MODE = MODE_NAME%0A    VALID = valid%0A    def _get_affinity(self):%0A        ctypes_cpuset_t = sched_getaffinity(pid=self.pid)%0A        self.cpusett.set_bits(cpus=ctypes_cpuset_t.cpus)%0A%0A    def _set_affinity(self):%0A        cs = cpu_set_t()%0A        cs.set_bits(cpus=self.cpusett.cpus)%0A        sched_setaffinity(cs, pid=self.pid)%0A%0Aclass VSCAffinityBasic(VSCAffinity):%0A    AFFINITY_ALGORITHM = 'basiccore'%0A%0A    def _algorithm(self, proc_nr, total_proc):%0A        self.get_affinity()%0A        allowed_cpus = self.cpusett.get_cpus_idx()  # takes into account cgroups/cpusets%0A        total_cpus = len(allowed_cpus)%0A%0A        alg = BasicCore()%0A        alg.create(total_cpus, total_proc)%0A        rel_cpus = alg.get_cpus(proc_nr)%0A%0A        # rel_cpus is a list relative to the usable cpus of cpusett.cpus%0A        for rel_idx, abs_idx in enumerate(allowed_cpus):%0A            self.cpusett.cpus%5Babs_idx%5D = 1 if rel_idx in rel_cpus else 0%0A%0Aif __name__ == '__main__':%0A    import time%0A    vp = VSCPriority()%0A    print vp.get_priority()%0A%0A    vp.set_priority(5)%0A    print vp.get_priority()%0A%0A    vab = VSCAffinityBasic()%0A    print vab.get_affinity()%0A    # process nr 1 out of 2 processes%0A    vab.algorithm(1, 2)%0A    print vab.get_affinity()%0A%0A%0A    time.sleep(5
 )%0A

add implementation based on vsc.util.affinity