newspaint

Documenting Problems That Were Difficult To Find The Answer To

Monthly Archives: October 2013

Monitoring Statistics for a Single Process in Linux

Some useful tools exist in Linux for monitoring a system. Both top and htop provide a quick view as to processor utilisation, memory utilisation.

But what if you need to know some more details about an individual process, such as user CPU vs system CPU usage?

The following Perl script reads from the /proc filesystem to get the stat file for the particular process and, when run with a delay, calculates the rate over time for particular parameters.

#!/usr/bin/perl -w

use Time::HiRes;

use strict;

my $pid = shift;
my $interval = ( shift || 0 );

if ( ! $pid ) {
  die( "Must provide process ID to monitor" );
}

# from "man proc" under "/proc/[pid]/stat"
my @vars = qw(
  pid comm state ppid pgrp session tty_nr tpgid flags minflt cminflt majflt
  cmajflt utime stime cutime cstime priority nice num_threads itrealvalue
  starttime vsize rss rlim startcode endcode startstack kstkesp kstkeip
  signal blocked sigignore sigcatch wchan nswap cnswap exit_signal
  processor rt_priority policy delayacct_blkio_ticks
);

my %prevdata = ();
my $last = Time::HiRes::time();
my $first = 1;
while ( 1 ) {
  my $now = ( $interval == 0 ) ? $last + 1 : Time::HiRes::time();
  if ( ! open( FIN, "</proc/$pid/stat" ) ) {
    die( "Cannot open stat for PID $pid: $!" );
  }
  my $line = <FIN>;
  close( FIN );

  my $period = $now - $last;
  $last = $now;
 
  my @values = split( /\s+/, $line );
  my %data = ();<span style="font-family:monospace;">
  for ( my $i = 0; $i < keys( @vars ); $i++ ) {
    $data{$vars[$i]} = $values[$i];
  }

  my %rates = ();
  foreach ( keys %data ) {
    next if ( $data{$_} !~ m{^(\d+(?:\.\d+)?)$} );
    $rates{$_} = ( $data{$_} - ($prevdata{$_} || 0) ) / $period;
  }

  if ( ( ! $first ) || ( $interval == 0 ) ) {
    printf( "user=%3d kernel=%3d RSS=%8d thds=%d\n", 
      int( $rates{utime} ),
      int( $rates{stime} ),
      $data{rss},
      $data{num_threads},
    );
  } else {
    $first = 0;
  }

  last if ( $interval == 0 );
  select( undef, undef, undef, $interval );
  %prevdata = %data;
}

The script can be used as follows:

user@host:~$ perl -w procstat.pl 3663 3
user=  0 kernel=  0 RSS=  158818 thds=34
user=  7 kernel=  0 RSS=  159014 thds=34
user=  7 kernel=  0 RSS=  158750 thds=34
user=  0 kernel=  0 RSS=  158750 thds=34
user=  1 kernel=  0 RSS=  158870 thds=34
user=  0 kernel=  0 RSS=  158870 thds=34
user=  1 kernel=  0 RSS=  159200 thds=34
user=  0 kernel=  0 RSS=  159200 thds=34
user=  0 kernel=  0 RSS=  158804 thds=34
user= 28 kernel=  1 RSS=  159751 thds=35
user=  7 kernel=  0 RSS=  160081 thds=35

Usage is: perl procstat.pl PID interval

Where interval is seconds.

Companies Should Not Hide Caller ID

Lately I’ve been getting irritated at representatives of companies calling my mobile phone without showing caller ID.

There is, frankly, no excuse for hiding caller identity today by companies. In fact the only ones that engage in this socially unacceptable practice are those who rely on nuisance calls to generate business: primarily cold callers from recruitment agencies.

I’ve had enough; I’ll answer such calls with an immediate demand for identity, followed by a polite “I’d rather an e-mail, something with an identity, thanks very much, goodbye”.

If you work at a company that routinely hides caller ID then have a sharp word with them. By denying your customers the choice of answering your call or not based on your identity you are abusing them. It’s that simple.

U Speed 7-Port H7928-U3 USB3 Hub Problems

Update 2016-03-28: I truly believe the problem was my motherboard with a fast USB initialisation setting in the BIOS. By disabling the “quick” USB option (reverting to a more thorough boot process) hubs are more reliably detected by the motherboard. But I haven’t thoroughly tested this theory – I’ve just not had a problem with a more recent USB3 hub that I bought that exhibited a similar problem before I changed the BIOS.


Twelve months ago I bought a 7 port USB hub from Amazon UK. It was the U Speed 7-port H7928-U3 hub (now labelled Anker I suspect).

U Speed H7928-U3 USB Hub

U Speed H7928-U3 USB Hub

It’s a great little hub – when it is functioning. But there are a couple of situations where it doesn’t.

Will Not Let Motherboard Boot When Plugged In

I have an Asus (ASUSTeK) P8H77-V LE motherboard (according to CPU-Z).

If the USB3 hub is powered on and connected to a USB3 port on my Asus motherboard it will boot but not recognise my USB2 keyboard. That prevents me from booting because I need to enter a password to boot.

The only solution is to unplug the USB3 cable from the USB3 hub before booting. As soon as the mainboard (motherboard) has finished its POST and begun running whatever code is in the boot sector of the chosen boot device you can re-attach the USB3 cable to the USB3 hub.

Windows 7 Doesn’t Recognise Any More Devices or Hard Drives Attached To The Hub

On occasion Windows 7 will not recognise a USB3 drive (or any devices) attached to the hub. Unplugging the device/drive and plugging it into a different port doesn’t help. Although the blue light goes on Windows 7 doesn’t appear to notice a new device has been attached.

Unplugging and re-plugging the USB3 hub itself from the mainboard (motherboard) doesn’t help.

The solution is to unplug the DC power from the USB hub, unplug the USB3 cable from the hub to the mainboard (motherboard), then count slowly to ten. Then re-attach the DC power to the hub, re-attach the USB3 cable to the hub from the mainboard (motherboard). Then re-attach your USB3 device (or drive). Windows 7 should recognise it now.

Cyanogenmod – Can’t Talk Now. Call Me Later?

By accident I somehow sent a text (SMS) message to a friend:

Can’t talk now. Call me later?

Turns out this is a quick response that can be selected from the incoming call screen if the number is recognised (but not if the number is unknown).

Instead of swiping to the right (to accept the call) or to the left (to reject the call) you swipe up to the message icon as per the following screenshot:

Swipe Up To Send A Rejection SMS

Swipe Up To Send A Rejection SMS

The settings for these pre-defined SMSes can be found by opening the call application, then pressing the “menu” key, then selecting “Settings”, and under “OTHER CALL SETTINGS” choose “Quick responses”.

The default list in CyanogenMod 10.1.3 are:

  • Can’t talk now. What’s up?
  • I’ll call you right back.
  • I’ll call you later.
  • Can’t talk now. Call me later?

See Also