Month: June 2011

Building a physical CPU load meter

I built this analog CPU load meter for my dev workstation:

Physical CPU load meter

All I did was drill a few holes into the CPU and probe the power supply lines…

Okay, I lied. This is actually a fun project that would make a great intro to embedded electronics, or a quick afternoon hack for someone with a bit of experience.

The parts

The main components are:

  • Current meter: I got this at MIT Swapfest. The scale printed on the face is misleading: the meter itself measures only about 600 microamps in each direction. (It’s designed for use with a circuit like this one). We can determine the actual current scale by connecting (in series) the analog meter, a variable resistor, and a digital multimeter, and driving them from a 5 volt power supply. This lets us adjust and reliably measure the current flowing through the analog meter.

  • Arduino: This little board has a 16 MHz processor, with direct control of a few dozen input/output pins, and a USB serial interface to a computer. In our project, it will take commands over USB and produce the appropriate amount of current to drive the meter. We’re not even using most of the capabilities of the Arduino, but it’s hard to beat as a platform for rapid development.

  • Resistor: The Arduino board is powered over USB; its output pins produce 5 volts for a logic ‘high’. We want this 5 volt potential to push 600 microamps of current through the meter, according to the earlier measurement. Using Ohm’s law we can calculate that we’ll need a resistance of about 8.3 kilohms. Or you can just measure the variable resistor from earlier.

We’ll also use some wire, solder, and tape.

Building it

The resistor goes in series with the meter. I just soldered it directly to the back:

Back of the meter

Some tape over these components prevents them from shorting against any of the various junk on my desk. Those wires run to the Arduino, hidden behind my monitor, which is connected to the computer by USB:

The Arduino

That’s it for hardware!

Code for the Arduino

The Arduino IDE will compile code written in a C-like language and upload it to the Arduino board over USB. Here’s our program:

#define DIRECTION 2
#define MAGNITUDE 3

void setup() {
    Serial.begin(57600);
    pinMode(DIRECTION, OUTPUT);
    pinMode(MAGNITUDE, OUTPUT);
}

void loop() {
    int x = Serial.read();
    if (x == -1)
        return;

    if (x < 128) {
        digitalWrite(DIRECTION, LOW);
        analogWrite (MAGNITUDE, 2*(127 - x));
    } else {
        digitalWrite(DIRECTION, HIGH);
        analogWrite (MAGNITUDE, 255 - 2*(x - 128));
    }
}

When it turns on, the Arduino will execute setup() once, and then call loop() over and over, forever. On each iteration, we try to read a byte from the serial port. A value of -1 indicates that no byte is available, so we return and try again a moment later. Otherwise, we translate a byte value between 0 to 255 into a meter deflection between −600 and 600 microamps.

Pins 0 and 1 are used for serial communication, so I connected the meter to pins 2 and 3, and named them DIRECTION and MAGNITUDE respectively. When we call analogWrite on the MAGNITUDE pin with a value between 0 and 255, we get a proportional voltage between 0 and 5 volts. Actually, the Arduino fakes this by alternating between 0 and 5 volts very rapidly, but our meter is a slow mechanical object and won’t know the difference.

Suppose the MAGNITUDE pin is at some intermediate voltage between 0 and 5 volts. If the DIRECTION pin is low (0 V), conventional current will flow from MAGNITUDE to DIRECTION through the meter. If we set DIRECTION high (5 V), current will flow from DIRECTION to MAGNITUDE. So we can send current through the meter in either direction, and we can control the amount of current by controlling the effective voltage at MAGNITUDE. This is all we need to make the meter display whatever reading we want.

Code for the Linux host

On Linux we can get CPU load information from the proc special filesystem:

keegan@lyle$ head -n 1 /proc/stat
cpu  965348 22839 479136 88577774 104454 5259 24633 0 0

These numbers tell us how much time the system’s CPUs have spent in each of several states:

  1. user: running normal user processes
  2. nice: running user processes of low priority
  3. system: running kernel code, often on behalf of user processes
  4. idle: doing nothing because all processes are sleeping
  5. iowait: doing nothing because all runnable processes are waiting on I/O devices
  6. irq: handling asynchronous events from hardware
  7. softirq: performing tasks deferred by irq handlers
  8. steal: not running, because we’re in a virtual machine and some other VM is using the physical CPU
  9. guest: acting as the host for a running virtual machine

The numbers in /proc/stat are cumulative totals since boot, measured in arbitrary time units. We can read the file twice and subtract, in order to get a measure of where CPU time was spent recently. Then we’ll use the fraction of time spent in states other than idle as a measure of CPU load, and send this to the Arduino.

We’ll do all this with a small Python script. The pySerial library lets us talk to the Arduino over USB serial. We’ll configure it for 57,600 bits per second, the same rate specified in the Arduino’s setup() function. Here’s the code:

#!/usr/bin/env python

import serial
import time

port = serial.Serial('/dev/ttyUSB0', 57600)

old = None
while True:
    with open('/proc/stat') as stat:
        new = map(float, stat.readline().strip().split()[1:])
    if old is not None:
        diff = [n - o for n, o in zip(new, old)]
        idle = diff[3] / sum(diff)
        port.write(chr(int(255 * (1 - idle))))
    old = new
    time.sleep(0.25)

That’s it!

That’s all it takes to make a physical, analog CPU meter. It’s been done before and will be done again, but we’re interested in what you’d do (or have already done!) with the concept. You could measure website hits, or load across a whole cluster, or your profits from trading Bitcoins. One standard Arduino can run at least six meters of this type (being the number of pins which support analogWrite), and a number of switches, knobs, buzzers, and blinky lights besides. If your server room has a sweet control panel, we’d love to see a picture!

~keegan

Building a physical CPU load meter

I built this analog CPU load meter for my dev workstation:

Physical CPU load meter

All I did was drill a few holes into the CPU and probe the power supply lines…

Okay, I lied. This is actually a fun project that would make a great intro to embedded electronics, or a quick afternoon hack for someone with a bit of experience.

The parts

The main components are:

  • Current meter: I got this at MIT Swapfest. The scale printed on the face is misleading: the meter itself measures only about 600 microamps in each direction. (It’s designed for use with a circuit like this one). We can determine the actual current scale by connecting (in series) the analog meter, a variable resistor, and a digital multimeter, and driving them from a 5 volt power supply. This lets us adjust and reliably measure the current flowing through the analog meter.

  • Arduino: This little board has a 16 MHz processor, with direct control of a few dozen input/output pins, and a USB serial interface to a computer. In our project, it will take commands over USB and produce the appropriate amount of current to drive the meter. We’re not even using most of the capabilities of the Arduino, but it’s hard to beat as a platform for rapid development.

  • Resistor: The Arduino board is powered over USB; its output pins produce 5 volts for a logic ‘high’. We want this 5 volt potential to push 600 microamps of current through the meter, according to the earlier measurement. Using Ohm’s law we can calculate that we’ll need a resistance of about 8.3 kilohms. Or you can just measure the variable resistor from earlier.

We’ll also use some wire, solder, and tape.

Building it

The resistor goes in series with the meter. I just soldered it directly to the back:

Back of the meter

Some tape over these components prevents them from shorting against any of the various junk on my desk. Those wires run to the Arduino, hidden behind my monitor, which is connected to the computer by USB:

The Arduino

That’s it for hardware!

Code for the Arduino

The Arduino IDE will compile code written in a C-like language and upload it to the Arduino board over USB. Here’s our program:

#define DIRECTION 2#define MAGNITUDE 3void setup() { Serial.begin(57600); pinMode(DIRECTION, OUTPUT); pinMode(MAGNITUDE, OUTPUT);}void loop() { int x = Serial.read(); if (x == 1) return; if (x < 128) { digitalWrite(DIRECTION, LOW); analogWrite (MAGNITUDE, 2*(127 x)); } else { digitalWrite(DIRECTION, HIGH); analogWrite (MAGNITUDE, 255 2*(x 128)); }}

When it turns on, the Arduino will execute setup() once, and then call loop() over and over, forever. On each iteration, we try to read a byte from the serial port. A value of -1 indicates that no byte is available, so we return and try again a moment later. Otherwise, we translate a byte value between 0 to 255 into a meter deflection between −600 and 600 microamps.

Pins 0 and 1 are used for serial communication, so I connected the meter to pins 2 and 3, and named them DIRECTION and MAGNITUDE respectively. When we call analogWrite on the MAGNITUDE pin with a value between 0 and 255, we get a proportional voltage between 0 and 5 volts. Actually, the Arduino fakes this by alternating between 0 and 5 volts very rapidly, but our meter is a slow mechanical object and won’t know the difference.

Suppose the MAGNITUDE pin is at some intermediate voltage between 0 and 5 volts. If the DIRECTION pin is low (0 V), conventional current will flow from MAGNITUDE to DIRECTION through the meter. If we set DIRECTION high (5 V), current will flow from DIRECTION to MAGNITUDE. So we can send current through the meter in either direction, and we can control the amount of current by controlling the effective voltage at MAGNITUDE. This is all we need to make the meter display whatever reading we want.

Code for the Linux host

On Linux we can get CPU load information from the proc special filesystem:

keegan@lyle$ head -n 1 /proc/statcpu 965348 22839 479136 88577774 104454 5259 24633 0 0

These numbers tell us how much time the system’s CPUs have spent in each of several states:

  1. user: running normal user processes
  2. nice: running user processes of low priority
  3. system: running kernel code, often on behalf of user processes
  4. idle: doing nothing because all processes are sleeping
  5. iowait: doing nothing because all runnable processes are waiting on I/O devices
  6. irq: handling asynchronous events from hardware
  7. softirq: performing tasks deferred by irq handlers
  8. steal: not running, because we’re in a virtual machine and some other VM is using the physical CPU
  9. guest: acting as the host for a running virtual machine

The numbers in /proc/stat are cumulative totals since boot, measured in arbitrary time units. We can read the file twice and subtract, in order to get a measure of where CPU time was spent recently. Then we’ll use the fraction of time spent in states other than idle as a measure of CPU load, and send this to the Arduino.

We’ll do all this with a small Python script. The pySerial library lets us talk to the Arduino over USB serial. We’ll configure it for 57,600 bits per second, the same rate specified in the Arduino’s setup() function. Here’s the code:

#!/usr/bin/env pythonimport serialimport timeport = serial.Serial(‘/dev/ttyUSB0’, 57600)old = Nonewhile True: with open(‘/proc/stat’) as stat: new = map(float, stat.readline().strip().split()[1:]) if old is not None: diff = [n o for n, o in zip(new, old)] idle = diff[3] / sum(diff) port.write(chr(int(255 * (1 idle)))) old = new time.sleep(0.25)

That’s it!

That’s all it takes to make a physical, analog CPU meter. It’s been done before and will be done again, but we’re interested in what you’d do (or have already done!) with the concept. You could measure website hits, or load across a whole cluster, or your profits from trading Bitcoins. One standard Arduino can run at least six meters of this type (being the number of pins which support analogWrite), and a number of switches, knobs, buzzers, and blinky lights besides. If your server room has a sweet control panel, we’d love to see a picture!

~keegan

Deferred Segment Creation in PeopleSoft

This note has been in my to do folder for a while since I found these two excellent blog entries about Deferred Segment Creation by Chistian Antognini.

They made me think about the use of this feature in PeopleSoft. In most PeopleSoft systems there are lots of empty tables, sometimes because not all the modules delivered in the database are in use, but also because not all the temporary table instances have ever been used. This blog entry from Tom Kyte feels very close to home

Deferred segment creation is available from Oracle 11gR1. From Oracle 11.2.0.2 this becomes the default, so there is no need to reconfigure anything in PeopleSoft in order use this Oracle feature. You may want to drop empty segments created under previous versions of the database.

Are you the smartest 2011

It’s been a long time since I’ve written something here, but I reckon the following is worth a post.
For those of you who don’t know what Oracle ‘Are you the smartest’ is (or ayts short), have a look at this post I wrote a couple of years back.
Basically it’s a competition for Oracle partners in the Benelux – I’ve entered the first time in 2008 and since then I’ve won it twice and became second once. 
Have a look at this year’s topics (http://www.ayts.be/)
All Benelux based Oracle partners can enter, and there is a winner per partner. 
This year, AXI was the smartest partner in Benelux, meaning, on average, we had the highest scores on the exams… again.
Anyhow – I’ve won it this year for AXI – and the first price is a trip to Oracle HQ, followed by a quick stop in Vegas. Now that’s a cool incentive !
 
Have a look at Marcel’s blog for a more detailed overlook of the trip.
I’ve been to SF and Vegas numerous times, but it never gets old ! 
Although this year, I must it admit, I was very tired before I even started the trip.
I’ve become a father 6months ago and this sort of starts to work on your sleep patterns Cool
So I was looking forward to some rest, but these were 4days packed full of action and this tired me down even more. 
Also the group was quit large (25people), and people who know me, know I don’t thrive that well in big groups.
This meant that by the end of the trip I hadn’t talk to all the people I wanted to talk to !
The trip was a blast tho – I teamed up with Geert Depaep (the guy from DBA-Village)  as a room-mate. He also won in 2008, the same year I last won.
Two high lights stand out 
– the visit to Oracle HQ, were we had 2 presentations.
One from Wim Coeckaerts on Oracle virtualisations and one from Jeff Davis on SOA.
I can’t really say much about it since we had to sign an NDA, other than, OracleVM 3.0 does exist.
(it has been in beta so long, I started to wonder if it acutally existed !)
Also, have a look at Oracle Virtual Assembly Builder.
I had never heard about this tool but it looks pretty cool if you want to deploy VM images and customize them.
wim coeckaert 
– my poker session at the Venetian
I didn’t enter a tournament (allthough those deep stack tournaments look mighty tempting) – just played cash games 4h straight.
Most of the guys went to the V club after 1h of gambling – apperently I was the only one not there … but when I’m at the tables
I lose time and interests in anything else … and I made some money as a bonus.
Anyway, overall excellent trip, a great incentive from Oracle, and a big thanks to Yves Van Looy.
Now if only I could get this song out of my head they were playing at the Paris Chateau Nightclub.
TEL/電話+86 13764045638
Email service@parnassusdata.com
QQ 47079569