#! /usr/bin/python # -*- python -*- # -*- coding: utf-8 -*- import sys, errno from glob import glob def bitmasklist(line, nr_entries): fields = line.strip().split(",") bitmasklist = [] entry = 0 for i in range(len(fields) - 1, -1, -1): mask = int(fields[i], 16) while mask != 0: if mask & 1: bitmasklist.append(entry) mask >>= 1 entry += 1 if entry == nr_entries: break if entry == nr_entries: break return bitmasklist def get_file_line(filename): try: with open(filename) as f: txt = f.read().strip() except IOError as e: if e.errno != errno.ENOENT: print >>sys.stderr, '%s "%s"' % (e.strerror, e.filename) else: print >>sys.stderr, "This kernel does not properly support cpu topology reporting." raise SystemExit, -1 return txt topo = {} def uniq(inlist): uniques = [] for item in inlist: if item not in uniques: uniques.append(item) return uniques def show_threading(id): global topo pkg = topo[id] print 'Hyper-thread siblings in package ID', id, 'are:', uniq(pkg['thread_siblings'][id]) def show_core(id): global topo pkg = topo[id] print 'core siblings in package ID', id, 'are:', pkg['core_siblings'] def main(): global topo cpudirs = glob('/sys/devices/system/cpu/cpu[0-9]*/topology') thread_siblings = {} for dire in cpudirs: cpu = {} for item in ('physical_package_id', 'core_id', 'core_siblings', 'thread_siblings'): cpu[item] = get_file_line(dire + '/' + item) max_cpus = (len(cpu['core_siblings'].split(','))+1) * 32 core_siblings = bitmasklist(cpu['core_siblings'], max_cpus) threads = bitmasklist(cpu['thread_siblings'], max_cpus) try: thread_siblings[int(cpu['physical_package_id'])] += [threads] except: thread_siblings[int(cpu['physical_package_id'])] = [threads] pkg = {'core_id' : cpu['core_id'], 'core_siblings' : core_siblings, 'thread_siblings' : thread_siblings } topo[int(cpu['physical_package_id'])] = pkg for id in sorted(topo): pkg = topo[id] print "physical package ID:", id nts = len(pkg['thread_siblings'][id]) ncs = len(pkg['core_siblings']) if nts >= 2 and nts == ncs: print 'The physical package is Hyper-threading capable and has one cpu core' show_threading(id) elif ncs >= 2 and nts == 1: print 'The physical package is multi-core capable and is not Hyper-threading capable' show_core(id) elif ncs > nts and nts > 1: print 'The physical package is both Hyper-threading and multi-core capable' show_threading(id) show_core(id) else: print 'The physical package is not Hyper-threading or multi-core capable' if __name__ == '__main__': main()