Wednesday, December 17, 2008

getting cpu usage of a specific Linux process

Couldn't find any good documentation online, so scribbling something here.
Running Debian/Lenny - Linux 2.6.24-1-686 i686 GNU/Linux

/proc/[pid]/stat has the total cpu usage of a process, along with various other data.
You can find the print statement in the source near the end of this file:
[linux_src]/linux-2.6.24/fs/proc/array.c :: static int do_task_stat([...])

For example, if you determine the pid of firefox to be 3428
I ran watch "cat /proc/3428/stat | sed 's/ /\n/g'" and just lined up the values with their c-variable names.

cputime_to_clock_t(utime), --> user time
cputime_to_clock_t(stime), --> system time
cputime_to_clock_t(cutime), --> child process user time
cputime_to_clock_t(cstime), --> child process system time


A little bit of counting, and we can retrieve these values via cat /proc/[pid]/stat | cut -d " " -f 14-17
For my firefox process, this command gave me numbers like: 4627014 49642 891 91


And to close it out, a little psuedo-code (python, rather) to collect the information, given a pid file from /var/run, easily modified to use an exact, or feed in from command line, etc.

#!/usr/bin/python

import subprocess
import time

class CPUData():
pid = "`cat /var/run/pidfile.pid`" # pid from file
# pid = 1234 # static pid
delay = 1

def reap(self):
command = 'cat /proc/`cat %s`/stat | cut -d " " -f 14-17' % self.noxpid
# eg result: 4627014 49642 891 91
# utime stime cu cs
usage = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
pcpu = sum([int(v) for v in usage.stdout.read().split()])
return pcpu

def harvest(self):
v1,t1 = self.reap(),time.time()
time.sleep(self.delay)
v2,t2 = self.reap(),time.time()
return (v2-v1)/(t2-t1)

if __name__ == '__main__':
x = CPUData()
print "% CPU Usage:", x.harvest()


Sorry for the parsimonious nature of the post, first draft got wiped, should be some errors hiding somewhere (unsolicited exercise for the reader?)

Sunday, November 30, 2008

adding machines to french wireless

The vague recollection goes as follows:

Setup
1 wanadoo wireless router - dsl (apparently some pun on wan + zanadoo?)
1 iMac (new giant thick screen type, not bubble grid + color)
1 macbook G4 laptop (A's)
1 pc laptop - vista (J's)

The two macs are already connected to the network, no particular problems there, besides the fact that the wireless was not going to the third floor.

Found the 26-digit (hex) WEP password on the body of the router, but when attempting to connect to the router via the pc, encountered a 'connection rejected/refused/dismissed' type of message. From the g4, went to 192.168.1.1 to check the network settings, would only allow language selection. Attempted the same from the imac, selecting language (used french as a test, then proceeded in english) proceeded to a management page (admin/admin login). Went to the wireless tab, there was a whitelist of non-blocked mac addresses, ran [cmd] on the pc, then 'ifconfig /all', found the wireless device near the top along with its corresponding 10-digit hex mac address, entered the address and applied the new settings. On the pc, the wireless disappeared from the discovered list briefly, when it reappeared, the connection attempt gave a password prompt, which accepted the 26 digit one previously obtained - and voila, internet for all.

A wireless repeater [http://en.wikipedia.org/wiki/Wireless_Repeater] or an additional router (properly configured) on the first or second floor should solve the third-floor issue.

Tuesday, October 14, 2008

meta-portrait of a dragon*

Ah, I have neglected to post the obligatory** picture-of-self, and so will rectify this***.  I really don't have much to say beyond that, but I suspect that the asterisks**** shall keep me entertained for some time.

A1.  Here 'dragon' does not refer to my western (sea-goat?) or eastern (rat) astrological manifestation, but rather to the remarkable purpality (violence, if you will) of the shirt, usually reserved for the dinosaur or dragon-like natured beasts of this world.

Asterick two.  In my recently-compiled experience*****, there are several types of blog:  educational; editorial; emo(tional); engineering; elucidating ... to enumerate some ellipse-ended elements.  The vamaj~ of said blogs will include no such self-portrayal, and will in fact (as well as deed) deliberately avoid said pictures.  The need for imagery to associate with the text is easily/commonly/usually (causal/relational?) filled by photographs of said author's feline companion.  On one other end of the spectrum are those who (willingly or not) have been striken with photogenaity, and are thus free to place themselves willy-nilly about their pages.  In reflection, I found myself respecting the latter more than the former, not for their inherently symmetrical faces, but rather for the ethics of the matter - there is enough ambiguously constructed persona (grata or non-) without my contributions.  However, I am less than enamored with my infrequent attempts at self-portraiture, and do not particularly care for the literal and metaphorical posings of photoshopped or reflected glory, so I suppose I am left with one final cowardice:  the 'look at this cool thing oh and i am in it too lol what a coincidence i totally forgot' technique.  Which brings me in a asymptotically smooth fashion to

Asthreerick.  The caption of this photo might accurately read 'this is a picture of me', but it is not a picture of me itself.  This was a free glamour (not my british) shot, courtesy of some photographs I needed for a foreign gym membership that I never used.  I figured that misr wouldn't mind if I decided to watch over the valley of kings from this cairn eternally (in-that-sense-meaning-for-a-very-short-time-before-it-is-blown-away).

*:IV. Once I get a decent image-editing package, perhaps I will be able to rectify that spelling to astrices
 ()******

Five-stars.  Thinly-veiled euphemism for "I'm making this up as I type it, but I seem to remember thinking about it once before"

Hexasterisk.  Found this well-crafted, though probably other-sourced, image at a since-deleted (good thing google has backups of the internet [who capitalizes that? though I do suppose it is good to distinguish from the general-purpose word internet]) page with base (http://www.windowsgames.co.uk/)

The tilde!  Eh, not really worth the exclamation point:  vast-majority

Percentage sign - unlikely to be used for a footnote, you say?  That's why I did not.  I hope to gain some insight into my run-on sentence slash hyphen slash slash slash parenthesis abuse when I have the time, by making a little force-node graph of this posting - though hyphen is perhaps less graphable than dashes are ... oh well.  I'm really not that fond of footnotes anyways.

and we're off, with a whimper

Shortly after creating this blog, I was fortunate enough to receive a lovely note of welcome from blogspot/google:
Your blog has been identified as a potential spam blog.  To correct this, please request a review by filling out the form at ...
An inauspicious start, to say the least - and one which delayed this first post by a week or two.  I suppose that's what you get for editing their .css files?  I utilized a little scientific method to identify what specifics caused this sudden disapproval, but I'll err on the side of non-disclosure for now, and let the spambot writers do their own researching.

I suppose I'll make an attempt at modularization as well, and contain myself to single thoughts per post - it's unlikely to hold up, but whatever populates the list will do for now =)