查询OSD运行在哪些cpu上



前言

在看CPU相关的文章的时候,想起来之前有文章讨论是否要做CPU绑定,这个有说绑定的也有说不绑定的,然后就想到一个问题,有去观测这些OSD到底运行在哪些CPU上面么,有问题就好解决了,现在就是要查下机器上的OSD运行在哪些CPU上

代码

这里直接上代码了,最近学习python在,就用python来实现

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
import json
import psutil
import commands
from prettytable import PrettyTable

def main():
if len(sys.argv) == 1:
printosdcputable("process")
elif sys.argv[1] == 't':
printosdcputable("thread")

def printosdcputable(choose):
print choose
row = PrettyTable()
row.header = True
cpulist = ["OSD\CPU"]
corelist=["Core ID"]
phylist = ["Physical ID"]
emplist=["-----------"]
for cpupro in range(psutil.cpu_count()):
cpulist.append("%s" %cpupro )

coreid=commands.getoutput('egrep \'processor|physical id|core id\' /proc/cpuinfo | cut -d : -f 2 | paste - - - | awk \'$1==%s {print $3 }\'' %cpupro)
corelist.append("%s" %coreid)

phyid = commands.getoutput('egrep \'processor|physical id|core id\' /proc/cpuinfo | cut -d : -f 2 | paste - - - | awk \'$1==%s {print $2 }\'' % cpupro)
phylist.append("%s" %phyid)
emplist.append("--")

row.field_names = cpulist
row.add_row(corelist)
row.add_row(phylist)
row.add_row(emplist)

for root, dirs, files in os.walk('/var/run/ceph/'):
for name in files:
if "osd" in name and "pid" in name :
osdlist = []
osdthlist=[]
for osdcpu in range(psutil.cpu_count()):
osdlist.append(" ")
osdthlist.append("0")
pidfile=root+ name
osdid=commands.getoutput('ls %s|cut -d "." -f 2 2>/dev/null' %pidfile )
osdpid = commands.getoutput('cat %s 2>/dev/null' %pidfile)
osd_runcpu = commands.getoutput('ps -o psr -p %s |grep -v PSR 2>/dev/null' %osdpid)
th_list = commands.getoutput('ps -o psr -L -p %s |grep -v PSR|awk \'gsub(/^ *| *$/,"")\' 2>/dev/null' % osdpid)

osdname="osd."+osdid
osdlist[int(osd_runcpu)]="+"
for osdth in th_list.split('\n'):
osdthlist[int(osdth)] = int(osdthlist[int(osdth)])+1
osdlist.insert(0,osdname)
osdthlist.insert(0,osdname)
if choose == "process":
row.add_row(osdlist)
elif choose == "thread":
row.add_row(osdthlist)
print row

if __name__ == '__main__':
main()

运行脚本:

watch python getosdcpu.py

或者监控线程

watch python getosdcpu.py t

运行效果如下:



线程的情况



看上去确实有些CPU上面运行了多个OSD,这里不讨论CPU绑定的好坏,只是展示现象,具体有什么效果,是需要用数据取分析的,这个以后再看下

补充

如果你发现你运行的脚本没有结果,这个不是脚本的问题,是因为没有生成pid文件,在配置文件/etc/ceph/ceph.conf当中增加:

pid_file = /var/run/ceph/$type.$id.pid

然后重启osd进程,检查生成了pid没有

ll /var/run/ceph/*.pid

变更记录

Why Who When
创建 武汉-运维-磨渣 2016-11-16
解决无pid 武汉-运维-磨渣 2017-02-21