File: getCpuSetRange.py

package info (click to toggle)
pyhst2 2020c-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 12,532 kB
  • sloc: ansic: 11,807; python: 9,663; cpp: 6,786; makefile: 147; sh: 31
file content (130 lines) | stat: -rw-r--r-- 4,622 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#/*##########################################################################
# Copyright (C) 2001-2013 European Synchrotron Radiation Facility
#
#              PyHST2  
#  European Synchrotron Radiation Facility, Grenoble,France
#
# PyHST2 is  developed at
# the ESRF by the Scientific Software  staff.
# Principal author for PyHST2: Alessandro Mirone.
#
# This program 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; either version 2 of the License, or (at your option) 
# any later version.
#
# PyHST2 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
# PyHST2; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# PyHST2 follows the dual licensing model of Trolltech's Qt and Riverbank's PyQt
# and cannot be used as a free plugin for a non-free program. 
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license 
# is a problem for you.
#############################################################################*/

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import subprocess as sub
import sys

import string
    ## in un futuro in cui potessero esserci piu di due cpu i cores potrebbero essere
    ## sur intervalli non contigui. In questo caso non bastera avere il numero totale di core
    ## e il primo numero. Ci vorranno piu range , ognuno individuato dall'inizio 
    ## e dal numero di cores  0,5,7,9-11

from . import string_six


    
def getCpuSetRange(cpuset_string, first=True):
    pos1 = cpuset_string.find( ":")
    pos2 = max(cpuset_string.rfind( "-"), cpuset_string.rfind( ","))
    if pos1 == -1 :
        raise Exception( "ERROR : wrong  output  in  cpuset_string " )
    if pos2 == -1 :
        if first:
            print( " **********************************!!!!!!!!!!!!!!!!!!!!!!!!!!\n"*10)
            print( " ERROR : you must request several OAR cores. PyHst cannot run on one core only , cpuset_string was  "+cpuset_string)
            raise Exception( "ERROR : you must request several OAR cores. PyHst cannot run on one core only , cpuset_string was "+cpuset_string )
        else:
            return []
    result = []
    msg = cpuset_string[pos1+1:]
    msgs = msg.split(",")
    for tok in msgs:
        ## print( tok)
        if "-" not in tok:
            result.append(  string.atoi(tok)  )
        else:
            a,b = map(string.atoi, tok.split("-"))
            result.extend( range(a,b+1))
    return result

def getCoresOrdered():
    
 
    comando = 'lscpu'
    if (sys.version_info >= (3, 7)):
        p1 = sub.Popen(args=comando.split( " ") ,stdin=sub.PIPE,stdout=sub.PIPE,stderr=sub.PIPE, text=True)
    elif (sys.version_info > (3, 0)):
        p1 = sub.Popen(args=comando.split( " ") ,stdin=sub.PIPE,stdout=sub.PIPE,stderr=sub.PIPE, universal_newlines=True)
    else:
        p1 = sub.Popen(args=comando.split( " ") ,stdin=sub.PIPE,stdout=sub.PIPE,stderr=sub.PIPE)
    # comando = 'grep node'
    # p2 = sub.Popen(args=comando.split( " ") ,stdin=p1.stdout,stdout=sub.PIPE,stderr=sub.PIPE)
    # comando = 'grep CPU'
    # p3 = sub.Popen(args=comando.split( " ") ,stdin=p2.stdout,stdout=sub.PIPE,stderr=sub.PIPE)
    # msg,errors=  p3.communicate()

    msg,errors=  p1.communicate()

    ## print( "  lscpu :  ",  msg)
    res = getCoresOrdered_fromstring(msg)
    return res

def     getCoresOrdered_fromstring(msg):
    msgs = msg.split("\n")
    count=0
    for l in msgs:
        count+=1
        # print( l)
        if "NUMA" in l:
            ncpus = string.atoi(l.split(":")[1])
            break
    else:
        raise Exception( "cannot understand output from lscpu ")



    result = []
    first= True
    for t in msgs[count:]:
        # if "CPU" in t:
        if "NUMA" in t:
            res = getCpuSetRange(t, first = first)
            first=False
            print ( " RES " , res)
            if(len(res)):
                result = result + res
                ncores4cpu = len(res)

    return ncores4cpu, result

if __name__=="__main__":
    print( getCpuSetRange(" asdasdas : 0,5,7,9-11"))
    s="""
NUMA node0 CPU(s):     0,2,4,6,8,10
NUMA node1 CPU(s):     1,3,5,7,9,11
"""
    print( getCoresOrdered_fromstring(s))