OpenVZ Forum


Home » General » Support » Measuring CPU usage
Measuring CPU usage [message #34870] Mon, 09 February 2009 16:39 Go to next message
gurtaj is currently offline  gurtaj
Messages: 12
Registered: September 2008
Location: London, Ontario, Canada
Junior Member
Hello, all. I've written a couple of Python methods to gather CPU usage statistics of the containers running in a Hardware Node.

I'm working with Centos 5.2, with this OpenVZ kernel: 2.6.18-92.1.13.el5.028stab059.6

If you find it useful, good. If you feel something is wrong, let me know please. =)

def gatherCpuStats():
	dict = {}
	stats = file('/proc/vz/vestat')

	# should check if the file exists and the operation was successful

	# read the first line (version number) and the second one (parameter names)
        stats.readline()
        stats.readline()

	# now, for each line create a ContainerStats
	while True:
		line = stats.readline()
		if len(line) == 0:
			break
		
		# create a ContainerStats for each line and place them inside a dictionary
		elems = line.split()
		
		# (parameter names) - second line, already read
		# VEID   user   nice   system   uptime   idle   strv   uptime   used   maxlat   totlat   numsched
		cont = ContainerStats(int(elems[0]))
		cont.setUptime(int(elems[7]))
		cont.setUsedtime(int(elems[8]))
		
		dict[cont.getVeid()] = cont
	stats.close()
	return dict


def calculateCpuUsage():
	global oldSet
	global newSet

	newSet = gatherCpuStats()

	for veid, newCont in newSet.iteritems():
		if veid in oldSet:
			oldCont = oldSet[veid]
			oldUptime = oldCont.getUptime()
			oldUsedtime = oldCont.getUsedtime()
		else:
			oldUptime = 0
			oldUsedtime = 0
		
		# CPU_USAGE_% = [(NEW_USED - OLD_USED) / CPU_MHZ] / [(NEW_UPTIME - OLD_UPTIME) / CPU_MHZ]
		cpuUsage = (float(newCont.getUsedtime() - oldUsedtime) / cpuCycles) / (float(newCont.getUptime() - oldUptime) / cpuCycles)

		# in order to store cpuUsage as a percentage value (xx.xx%)
		# I multiply cpuUsage by 100 and truncate it
		newCont.setCpuUsage(int(cpuUsage * 100))



# the following is a simple loop to test the previous methods

i = 0
while (i < 10):
	print '+ + + + + + + + + + + + + + + + + + + +'
	print 'Start:', time.strftime('%H%M%S')
	print
	
	calculateCpuUsage()
	oldSet = newSet		# the current info becomes the _old_set_ of info

	for veid, cont in oldSet.iteritems():
		print 'VEID:', cont.getVeid(), '- CPU usage:', cont.getCpuUsage()
	print
	i += 1
	time.sleep(10)

Re: Measuring CPU usage [message #35194 is a reply to message #34870] Mon, 09 March 2009 15:34 Go to previous message
gurtaj is currently offline  gurtaj
Messages: 12
Registered: September 2008
Location: London, Ontario, Canada
Junior Member
# CPU_USAGE_% = [(NEW_USED - OLD_USED) / CPU_MHZ] / [(NEW_UPTIME - OLD_UPTIME) / CPU_MHZ]
cpuUsage = (float(newCont.getUsedtime() - oldUsedtime) / cpuCycles) / (float(newCont.getUptime() - oldUptime) / cpuCycles)


The other day I was doing some refactoring of the code and suddenly realized that it was kind of stupid what I did in that formula: dividing both terms by CPU_MHZ.

I believe the rest remained the same.
Previous Topic: RTNETLINK answers: File exists for veth interface
Next Topic: Accessing host directory/parition from VPS
Goto Forum:
  


Current Time: Thu Oct 17 23:41:22 GMT 2024

Total time taken to generate the page: 0.05295 seconds