si.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:Starfish 作者: BillWang139967 项目源码 文件源码
def cpustat(self, fullstat=False):
        cpustat = {}
        # REF: http://www.kernel.org/doc/Documentation/filesystems/proc.txt
        fname = ('used', 'idle')
        full_fname = ('user', 'nice', 'system', 'idle', 'iowait', 'irq',
                'softirq', 'steal', 'guest', 'guest_nice')
        cpustat['cpus'] = []
        with open('/proc/stat', 'r') as f:
            for line in f:
                if line.startswith('cpu'):
                    fields = line.strip().split()
                    name = fields[0]
                    if not fullstat and name != 'cpu': continue;
                    stat = fields[1:]
                    stat = [int(i) for i in stat]
                    statall = sum(stat)
                    if fullstat:
                        while len(stat) < 10: stat.append(0)
                        stat = dict(zip(full_fname, stat))
                    else:
                        stat = [statall-stat[3], stat[3]]
                        stat = dict(zip(fname, stat))
                    stat['all'] = statall
                    if name == 'cpu':
                        cpustat['total'] = stat
                    else:
                        cpustat['cpus'].append(stat)
                elif line.startswith('btime'):
                    btime = int(line.strip().split()[1])
                    cpustat['btime'] = time.strftime('%Y-%m-%d %X %Z',
                                                    time.localtime(btime))
        return cpustat
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号