千家信息网

python如何计算节点创建cgroups绑定虚拟核心

发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,这篇文章给大家分享的是有关python如何计算节点创建cgroups绑定虚拟核心的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 import MySQLdbimport o
千家信息网最后更新 2024年10月22日python如何计算节点创建cgroups绑定虚拟核心

这篇文章给大家分享的是有关python如何计算节点创建cgroups绑定虚拟核心的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

 import MySQLdbimport osimport signalimport shleximport subprocessfrom eventlet.green import subprocess as green_subprocessfrom eventlet import greenthreadimport loggingimport stringimport timemysql_host = "10.160.0.120"mysql_db = "nova"mysql_user = "nova"mysql_passwd = "87da3417bb3a42ee"mysql_port = 3306mysql_charset = "utf8"cgroups_hierarchy = "/home/cgroups/cpu"LOG = logging.getLogger(__name__)def create_process(cmd, root_helper=None, addl_env=None):    if root_helper:        cmd = shlex.split(root_helper) + cmd    cmd = map(str, cmd)    LOG.debug(("Running command: %s"), cmd)    env = os.environ.copy()    if addl_env:        env.update(addl_env)        obj = subprocess_popen(cmd, shell=False,                            stdin=subprocess.PIPE,                            stdout=subprocess.PIPE,                            stderr=subprocess.PIPE,                            env=env)        return obj, cmddef execute(cmd, root_helper=None, process_input=None, addl_env=None,            check_exit_code=True, return_stderr=False):    try:        obj, cmd = create_process(cmd, root_helper=root_helper,                                  addl_env=addl_env)        _stdout, _stderr = (process_input and                            obj.communicate(process_input) or                            obj.communicate())        obj.stdin.close()        m = ("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n"              "Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode,                                       'stdout': _stdout, 'stderr': _stderr}        LOG.debug(m)        if obj.returncode and check_exit_code:            raise RuntimeError(m)    finally:        greenthread.sleep(0)        return return_stderr and (_stdout, _stderr) or _stdoutdef _subprocess_setup():    signal.signal(signal.SIGPIPE, signal.SIG_DFL)def subprocess_popen(args, stdin=None, stdout=None, stderr=None, shell=False,                     env=None):    return green_subprocess.Popen(args, shell=shell, stdin=stdin, stdout=stdout,                            stderr=stderr, preexec_fn=_subprocess_setup,                            close_fds=True, env=env)def query_db_of_local_vms():    """ query controller node db for information about vms running on local host """    conn = None    try:        conn = MySQLdb.connect(host=mysql_host,                               user=mysql_user,                               passwd=mysql_passwd,                               db=mysql_db,                               port=mysql_port,                               charset=mysql_charset)    except Exception as e:        #LOG.error("Fail to connect mysql .")                raise e    else:        LOG.info("Connect to mysql .")        local_compute_name = get_host()        sql = "SELECT vcpus,uuid FROM instances WHERE host='%s' AND deleted=0"%local_compute_name        vms = {}        try:        cursor = conn.cursor()        cursor.execute(sql)        result = cursor.fetchall()        cursor.close()        conn.close()                for item in result:            vms.update({item[1]:item[0]})    except Exception as ex:        #LOG.error("Exception happens while querying mysql.")                raise ex    else:        return vmsdef get_host():    return os.environ['HOSTNAME']def get_ip():    """ parse /etc/hosts to get local ip"""    df = open("/etc/hosts")    hosts = df.readlines()    hostname = get_host()    host_rec = [line for line in hosts if hostname in line and "#" not in line][0]    return host_rec.split()[0].strip()def check_cglib():    """ check required cglibs"""    check_pkgs = "rpm -qa"    cglib_kw = "libcgroup"    cmd = check_pkgs.split()    pkgs = execute(cmd,root_helper=None)        cglibs = [pkg for pkg in pkgs.split("\n") if cglib_kw in pkg]        if len(cglibs)==0:        print "libcgroup-x.xx-x.xx.x86_64 not installed.Exit."        exit(1)def init_cgroups():        """ensure cgrouplib installed"""    check_cglib()        """ create cgroups base architecture """    """clear old cgroups settings mounted on cgroups_hierarchy """    cg_clear()        """create base cgroups hierarchy"""    create_hierarchy = ("mkdir -p %s"%cgroups_hierarchy).split()    execute(create_hierarchy,root_helper=None)        """mount target subsystem,that's cpuset,to established hierarchy."""    mount_cg = ("mount -t cgroup -o cpuset cpuset2015  %s"%cgroups_hierarchy).split()    execute(mount_cg,root_helper=None)def cpuinfo():    """ get cpu counts and memory nodes """    cpuinfo = "lscpu"    cmd = cpuinfo.split()    retv = execute(cmd,root_helper=None)    memNodes = retv.count("NUMA node")-1    cpus = string.atoi([line for line in retv.split("\n") if "CPU(s)" in line][0].split(":")[1].strip())    return {"cpu_count":cpus,"memNodes":memNodes}def assign_cores():    """ cal out schemes of core binding for instances running on local host"""    vmDetails = query_db_of_local_vms()    cpuinfos = cpuinfo()    def assign(require,all):        tmp = {}        i=0        for k,v in require.iteritems():                    cores = [p%all for p in xrange(i,i+v)]                    i=i+v                    tmp.update({k:{"cores":cores,"memNodes":"0-%d"%(cpuinfos["memNodes"]-1)}})        return tmp    vmDetails = assign(vmDetails,cpuinfos["cpu_count"])    return vmDetailsdef get_pids(vmDetails):    query_pids = "ps aux"    cmd = query_pids.split()    retv = execute(cmd,root_helper=None)    qemu_kvm_processes =[p for p in retv.split("\n") if "qemu-kvm" in p and len(p)>100]    tmp = {}     for vmDetail in vmDetails:        tmp.update({vmDetail:vmDetails[vmDetail]})        for qkp in qemu_kvm_processes:            if vmDetail not in qkp:                continue                            else:                pid = string.atoi(qkp.split()[1])                 tmp[vmDetail]["pid"] = pid    return tmp def create_cgs_for_instances(vmDetails):    for item in vmDetails:        detail = vmDetails[item]        if "pid" not in detail or "cores" not in detail or "memNodes" not in detail:            print "Instance %s invalid"%item            continue        else:            """ create private group for each instance"""            instance_cg = "%s/%s"%(cgroups_hierarchy,item)            create_cg_for_instance = ("mkdir -p %s"%instance_cg).split()            execute(create_cg_for_instance,root_helper=None)            setcpu_txt = "%s"%detail["cores"][0]            if len(detail["cores"]) > 1:                for core_num in detail["cores"][1:]:                    setcpu_txt = "%s,%s"%(setcpu_txt,core_num)                 setmem_txt = detail["memNodes"]                        fd_setcpu = open("%s/cpuset.cpus"%instance_cg,"w")            fd_setcpu.write(setcpu_txt)            fd_setcpu.close()            fd_setmem = open("%s/cpuset.mems"%instance_cg,"w")            fd_setmem.write(setmem_txt)            fd_setmem.close()                        print detail["pid"]            fd_task = open("%s/tasks"%instance_cg,"w")            fd_task.write("%s"%detail["pid"])            fd_task.close()           def cg_clear():    """ destroy existing cgroups """    cgclear = "umount -t cgroup %s"%cgroups_hierarchy    cmd = cgclear.split()    try:        execute(cmd,root_helper=None)    except Exception as e:        passdef periodic_task():    init_cgroups()    hosted_vms = assign_cores()    hosted_vms = get_pids(hosted_vms)    create_cgs_for_instances(hosted_vms) if __name__ == "__main__":    periodic_task()     while True:        time.sleep(3*60)        print "Loop Job."        periodic_task()

该脚本作用是:连接数据库查询出运行在当前宿主下的客户机的cpu配额,然后再根据当前宿主的CPU信息,做出虚拟核心的分配;创建cgroups 绑定虚拟核心,实现资源隔离.

该脚本后台运行:

nohup python /path/to/this/scripy  &

感谢各位的阅读!关于"python如何计算节点创建cgroups绑定虚拟核心"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

核心 节点 内容 宿主 更多 篇文章 脚本 运行 不错 实用 作用 信息 后台 客户 客户机 数据 数据库 文章 看吧 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 大连诺德网络技术 数据库怎样把角色的权限授予用户 管理软件开发好处 什么行业可以接触软件开发 苏州网络无纸化会议系统服务器 网络安全法未经授权 健全网络安全管理规章制度 军人如何确保信息网络安全 人力资源方面数据库 凯里电商软件开发公司 男生学会计还是网络技术好 同关系型数据库交互机制 山西通信软件开发过程品质保障 学校网络安全教育的重要性 社区开展网络安全进家庭宣传活动 ftp已经从服务器断开 世界五大疾病数据库 天骐服务器小游戏死神跑酷 阿里云桌面服务器配置 我们社会的网络安全吗 入侵网游服务器 平谷区信息化软件开发介绍 网络安全设计基本原则 苏州程序软件开发服务费 食物数据库营养 第一篇mysql数据库详解重点 程序员与软件开发 太原代驾软件开发 如何选购数据库的好处 微生物多样性数据库有哪些
0