From: Gautham R S. <eg...@in...> - 2010-01-11 04:50:52
|
On Thu, Dec 24, 2009 at 11:45:51PM +0530, Poornima Nayak wrote: > +def get_hyper_thread_count(): > + ''' Return number of threads in cpu0 > + ''' > + try: > + file_cpuinfo = open("/sys/devices/system/cpu/cpu0/topology/thread_siblings", 'r') > + threads_count = 0 > + for line in file_cpuinfo: > + core_grps = line.split(",") > + for i in range(0, len(core_grps)): > + cpumask=int(core_grps[i]) > + while cpumask > 0: > + threads_count = threads_count + 1 > + cpumask = cpumask >> 1 > + return threads_count > + except Exception, details: > + print "INFO: Failed to get threaded siblings count", details > + sys.exit(1) > + > +def get_core_sibling_count(): > + ''' Return number of threads in cpu0 > + ''' > + try: > + file_cpuinfo = open("/sys/devices/system/cpu/cpu0/topology/core_siblings", 'r') > + cores_count = 0 > + for line in file_cpuinfo: > + core_grps = line.split(",") > + for i in range(0, len(core_grps)): > + cpumask=int(core_grps[i]) > + while cpumask > 0: > + cores_count = cores_count + 1 > + cpumask = cpumask >> 1 > + return cores_count > + except Exception, details: > + print "INFO: Failed to get core siblings count", details > + sys.exit(1) > + We could avoid the code repetition above. def get_entity_count(entity): try: file_cpuinfo = open("/sys/devices/system/cpu/cpu0/topology/" + entity + "siblings", 'r) . . . except . . . def get_hyper_thread_count(): return get_entity_count("thread") def get_core_sibling_count(): return get_entity_count("core") -- Thanks and Regards gautham |