2024 π Daylatest newsbuy art
This love loves love. It's a strange love, strange love.Liz Fraserfind a way to lovemore quotes
very clickable
data + munging

The Perl Journal

Volumes 1–6 (1996–2002)

Code tarballs available for issues 1–21.

I reformatted the CD-ROM contents. Some things may still be a little wonky — oh, why hello there <FONT> tag. Syntax highlighting is iffy. Please report any glaring issues.

The Perl Journal
#8
Winter 1997
vol 2
num 4
Unreal Numbers
Why 21.09 doesn't always equal 21.09.
Torture Testing Web Servers and CGI Scripts
Throw random input at your servers and scripts.
JPL: The Java-Perl Library
A glue interface linking Perl and Java.
Perl and EBCDIC
Before Unicode, before ASCII, there was EBCDIC.
Interprocess Communication with MacPerl
Using AppleEvents from Perl.
Perl News
What's new in the Perl Community.
SWIG
Generate Perl wrappers around C/C++ code - automatically.
Telnetting with Perl
How Net::Telnet lets your programs talk to other computers.
B-Trees
An extremely efficient tree data structure.
NT Administration with Perl
Common sysadmin tasks, automated with Perl.
The Great Internet Mersenne Prime Search
A highly distributed computing effort to find new primes.
The Perl Wizard's Quiz
Not for the timid.
The Perl Journal One-Liners
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.

SWIG

Generate Perl wrappers around C/C++ code - automatically.

Scott Bolte


Tired of the "which interpreter is best for the job" debates? Want something that makes the speed and power of C and C++ available to several common interpreters - automatically? There's a new tool that does exactly this: SWIG, the Simplified Wrapper and Interface Generator.

SWIG is a freely available compiler developed by Dave Beazley, now at the University of Utah. It works on many Unix variants, as well as NT, Win95, and MacOS, although the Win95 and MacOS versions are less polished. It converts C and C++ files into interface code (called a "wrapper") that makes the contents available to Perl, Tcl, Guile, and Python - all automatically. Like Perl's own XS language, SWIG creates Perl wrappers around C code . Unlike XS, SWIG requires little or no programming effort.

I'm not going to describe the mechanics of SWIG in detail; a more thorough treatment would address function prototypes, complex data structures, multiple inheritance, Perl classes, Tcl 8 modules, exception handling, makefile generation, strict type checking in the interpreter, conditional compilation, and automatic generation of documentation. There's an excellent user's guide bundled with SWIG that covers all of these topics; in this article, I'll just show you the basics: how to take an existing application and create a Perl wrapper around it.

This tutorial is divided into three sections. First, in "Take a Sip," I'll show you a simple use of SWIG: creating a Perl wrapper around a lone C function. Second, you'll see how to make C data structures available to Perl via what SWIG calls an interface file, in "Take Another Sip." Finally, in "Take a SWIG," I'll take the source code for an entire utility and "port" it to Perl.

Along the way, you'll see examples of how SWIG is invoked, how shared libraries are built, and how the resulting Perl modules are used. Finally, I'll demonstrate how to write a Perl script that does a great job of imitating top - a widely-used process monitor - because it uses the actual top source code.

Hooks by Hand

I first started using Perl and Tcl in the early 1990's when both were new. Perl was an immediate hit and I happily used a2p and s2p to convert all my awk and sed scripts to Perl. Then, looking at the generated Perl for clues, I converted my sh scripts too. Tcl, while not nearly as powerful as Perl, had the nifty feature of allowing user-defined C functions to be exported to the interpreter. While Perl would eventually do that as well, it was still years away. (I was using Perl 3, after all.)

I spent an inordinate amount of time trying to simplify the writing of Tcl glue code. Eventually an entire library was developed that used a C template structure to convert a vector of Tcl string arguments into C data structures. This made it much easier to call C functions without an argc/argv style interface. Even so, the linkage between C and Tcl was still a hassle.

Perl caught up with Tcl when XS was introduced. Not only could developers specify how to convert between the compiled and interpreted worlds, but the compiled code could be dynamically loaded via a shared library. On the surface, this seemed a wonderful solution.

The problem with interface code is that, well, you have to write it. That might be acceptable if you're creating entirely new code to be made available via a Perl wrapper, but what about legacy code? Wouldn't you really rather develop new programs than spend your time creating wrappers around old code?

Take a Sip: Wrapping a C Function

Let's start out with a simple example. Many people who post to the Usenet newsgroup comp.lang.perl.misc ask how to determine the amount of time used by chunks of their Perl programs. The usual solution is to use one of the timing modules, such as Benchmark.pm. To demonstrate SWIG, we'll create and use a C function instead.

At the top of the next page you'll see elapsed.c, containing nothing but the elapsed_seconds() function.

Because elapsed.c: a C Function to be Wrappered by SWIG is so simple we can feed it directly to SWIG:

% swig -perl5 -shadow -module Elapsed elapsed.c
Generating wrappers for Perl 5

This generates three files: the Perl modules in Elapsed.pm, XS wrapper code in elapsed_wrap.c, and documentation in elapsed_wrap.doc.

I won't discuss elapsed_wrap.doc. It's enough to say that SWIG's surprisingly rich documentation generation can create plain ASCII, HTML, or LaTeX. There are options to locate, extract, and format comments from the source code. As with most configuration preferences, the options can be selected either by the SWIG command line or via directives embedded in the source code.

SWIG isn't a full C/C++ parser; some snippets of code will give it fits. So instead of throwing full-fledged C programs at it, it's more common to process merely a .h (header) file. If you're going to be intermixing C/C++ source code with SWIG directives, then an interface file (ending in .i by convention) would be a better choice. SWIG defines the SWIG preprocessor token, so you can make a portion of the C source code visible only to SWIG by enclosing it between #ifdef SWIG and #endif lines, or render it invisible to SWIG with #ifndef SWIG and #endif.

In addition to providing the name of the source module, we need to tell SWIG a few other things. In the command line of elapsed.c we set the output language to Perl, request shadow classes (more on those later), and set the module name to Elapsed.

The Elapsed.pm and elapsed_wrap.c files generated by SWIG constitute a full-fledged Perl extension. They verify function arguments, translate them into C data structures, invoke the functions, and translate return values into a form palatable by Perl. It handles not only functions, but global variables and read-only constants as well. The wrapper, along with the original source file, is compiled and turned into a shared library.

One of the obstacles to using SWIG (and nearly everything else) on different platforms is the variety of ways to create shared libraries. Here's the particular incantation for FreeBSD:

% gcc -DPIC -fpic -I/opt/perl5.004/lib/ \
i386-freebsd/5.00403/CORE -c elapsed_wrap.c 
% gcc -DPIC -fpic -Wall -c elapsed.c 
% ld -Bshareable  -L/usr/local/lib -o \
Elapsed.so elapsed_wrap.o elapsed.o 

You'll need to figure out what commands and options your platform requires. This information should be available from your Perl configuration; here's an ugly but effective make rule that appends the pertinent variables to the Makefile. Whenever I move to a new platform, make localvars deletes the old variables and adds the new ones. It's not pretty, but it works.

 localvars:
 perl -MConfig -e '                                         \
 printf("LD = %s %s\n", $$Config{ld}, $$Config{lddlflags}); \
 printf("CC = %s %s\n", $$Config{cc}, $$Config{cccdlflags});\
 printf("PERLINC =-I%s/CORE\n", $$INC[0]);'                 \
 >> Makefile

Our test of elapsed_seconds() is encapsulated in a Perl program called elapsed. This program computes the Fibonacci sequence and the ratio between successive numbers in the sequence, which converges to the Golden Mean. We'll use elapsed_seconds() to time our program. (Remember, this is only so that we can demonstrate SWIG; to time Perl code you can always just use Perl's own Benchmark module, bundled with the standard distribution.)

Downloading SWIG

SWIG's home page is https://www.cs.utah.edu/~beazley/SWIG/swig.html.

You can download it from the CPAN site nearest you in CPAN/authors/Dave_Beazley, or from ftp://ftp.cs.utah.edu/pub/beazley/SWIG.

To join the SWIG mailing list, send a message saying subscribe swig to Majordomo@cs.utah.edu. The message volume is low and the signal-to-noise ratio is high, because Dave Beazley responds to most questions himself.

As you'd hope, the calls to the C function elapsed_seconds() and the assignments of $before and $after look like regular Perl. Once you include the C function with use Elapsed, you can treat it like any other Perl subroutine; the fact that the underlying code happens to be written in C makes no difference.

Here's the output of our elapsed program:

1:            1 
2:            1  1 
3:            2  2 
4:            3  1.5 
5:            5  1.666666667 
6:            8  1.6 
7:           13  1.625 
8:           21  1.615384615 
9:           34  1.619047619 
10:          55  1.617647059 
11:          89  1.618181818 
12:         144  1.617977528 
13:         233  1.618055556 
14:         377  1.618025751 
15:         610  1.618037135 
16:         987  1.618032787 
17:        1597  1.618034448 
18:        2584  1.618033813 
19:        4181  1.618034056 
20:        6765  1.618033963 
Elapsed time is 0.001777 seconds. 

Take another Sip: Interface Files

Now we'll provide SWIG with an interface file, which lets us use SWIG pragmas. We'll skip over compilation and linking, and look at a few complex data structures instead.

In gettime.i, shown below, we set the module name, make the global variable errno read-only, and ask for default structure constructors. We also define two time structures and provide the prototypes for gettimeofday() and settimeofday().

%module Gettime;    // Alternative to command line
                    // arguments (for naming) 
%readonly           // Make all variables read only. 
int errno; 
%readwrite          // Restore default behavior. 
%pragma make_default;  // Generate default constructors. 
struct timeval { 
        long    tv_sec;		 // seconds 
        long    tv_usec;	 // and microseconds 
}; 
struct timezone { 
     int  tz_minuteswest;  // minutes west of Greenwich
     int  tz_dsttime;      // type of dst correction
}; 
int gettimeofday(struct timeval * tp, 
                 struct timezone * tzp); 
int settimeofday(const struct timeval * tp, 
                 const struct timezone * tzp); 

We can now build the module much as before. Now we don't need the -module option since the name of the module is set in the interface file with the statement %module Gettime.

% swig -perl5 -shadow gettime.i 
Generating wrappers for Perl 5 
% gcc -DPIC -fpic -I/opt/perl5.004/lib/ \
i386-freebsd/5.00403/CORE -c gettime_wrap.c 
% ld -Bshareable  -L/usr/local/lib -o   \
                   Gettime.so gettime_wrap.o 

Now using gettimeofday() is easy:

#!/usr/bin/perl -w
use Gettime;
my $tv  = new timeval(); # Allocate a timeval structure
# Below, undef maps to a null pointer
Gettime::gettimeofday($tv, undef) 
 && warn("gettimeofday() failed, errno = $Gettime::errno.\n");
# The shadow option is what allows these symbolic references
# to structure fields.
printf("Time is %d.%06d\n",
       $tv->{tv_sec}, 
       $tv->{tv_usec});

The script's output:

Time is 877438914.248738

Let's look at a program that doesn't work. This script tries to set the time, but fails because it wasn't run by the superuser. Then it fails again because $errno is read-only.

#!/usr/bin/perl -w
use Gettime;
my $tv  = new timeval();  # Allocate timeval structure.
$tv->{tv_sec} = $tv->{tv_usec} = 0; # Turn back the clock.
# This if statement will fail unless you're root
if (Gettime::settimeofday($tv, undef)) {
       warn("settimeofday() failed, errno =$Gettime::errno.\n");
       # This fails since it's read-only
	   $Gettime::errno = 0;
}

The output:

 
settimeofday() failed, errno = 1.
Value is read-only. at ./Gettime-test.pl line 9.

A Top Notch Utility

The remainder of this article shows how the power of top can be made available to Perl. The top utility, developed by William LeFebvre and a cast of dozens for more than a decade, is a great system utility similar to ps: It displays a system summary followed by a listing of processes. Unlike ps, the display is updated at regular intervals. There are various other nifty features, but the kicker is that top is portable. Source code is available from ftp://ftp.groupsys.com/pub/top.

Before we get started I'll point out three reasons why I choose top. First, since I didn't write it, it serves as a good test of adapting legacy code to a new environment. Second, top was written with portability in mind, and that makes our job easier. Version 3.4 runs on over two dozen Unix variants - pretty unusual for a program so sensitive to internal kernel structures. top's portability makes it an ideal candidate for SWIG. Finally, I've always wanted to have access to top's information in my system monitoring scripts without having to decode the internal structures of yet another operating system.

One of top's header files, machine.h, contains three structure definitions and a few function prototypes. That's it. The operating system-specific code for each port need only populate an array of those structures. Those three structures and a few functions are all SWIG needs to know about.

My interface file, top.i, has two sections. The first section, delimited by %{ and %}, is literal source code that will be needed by the Perl extension generated by SWIG. This section is opaque to SWIG and can be as complex as necessary. The code after the %} is almost straight C too; in fact, if the special %include statements weren't necessary, the entire interface file could be nothing but C code.

From %{ to %}. This section contains three functions to be included in the Perl extension. The printable() function mimics a function in the top source code. The original is in a file that, if compiled and linked, introduces many more platform dependencies. So to keep the number of dependencies to a minimum, I just duplicated what I needed.

The two functions after printable(), full_format_header() and full_format_next_process(), are only needed in the interface file for their complete prototype definitions. As you can tell, the original prototypes in machine.h lack arguments, so we have to provide them here. (As LeFebvre points out, this code predates ANSI C and is due for an overhaul.)

Complete prototypes are required, and for a good reason. SWIG isn't as permissive as Perl. It doesn't have Perl's anything-goes attitude, and in fact performs extensive type checking of function arguments. This should be reassuring to people leery of integrating a low-level application into a typeless language.

After the %{... %} block. The next lines in top.i are a few %include statements. The first two import a couple of SWIG's built-in interface files: pointer.i and typemaps.i. SWIG supports basic data types, but more sophisticated structures (structures, arrays, complex pointers, and the like) require additional help. These built-in interface files handle common C constructs, such as a null terminated array of strings represented as a char **.

The next two include statements pull in two top header files: top.h and machine.h. The first contains some constants, and the second, as you've already seen, contains the portable structures used by top.

Finally, the five externs at the bottom of top.i are function prototypes. The first two should look familiar; they need to be included so SWIG will know to generate wrappers. (Remember, the earlier code block was opaque to SWIG.) The last three statements are prototypes for the top functions we'll be calling from our Perl program.

If this interface file seems overly complex, it's due to my desire to leave the legacy top sources untouched. Were this new code, the interface file might have been just a few include statements - or the .h files might even have been used directly.

You can have SWIG process the interface file as follows (assuming you're in the same directory as the code). The only new option, -Itop-3.4, indicates where to look for include files. The other options should all look familiar:

% swig -Itop-3.4 -perl5 -shadow -module Top top.i

SWIG then creates three files: top_wrap.c, Top.pm, and top_wrap.doc. The wrapper source file contains all the Perl to C interfaces - well over 2000 lines of code that you, thankfully, don't have to write. This may seem like a lot; it's because of SWIG's type checking and its tests for end cases. You might be able to do as good a job by hand, but I doubt it.

Top.pm is the Perl module proper. It uses Perl's built-in DynaLoader module to load the top shared library dynamically (assuming your system supports shared libraries). This makes the functions and C constants in top.h available to the module.

The final step is to compile the C code. top_wrap.c is compiled along with three files from the top sources. They're linked together into a shared library (Top.so on my system) and we're ready to go.

% swig -Itop-3.4 -perl5 -shadow -module Top top.i
top-3.4/machine.h : Line 28. Warning. Array member will be
read-only. Generating wrappers for Perl 5
% gcc -DPIC -fpic -Itop-3.4 -I/opt/perl5.004/lib/
                 \ i386-freebsd/5.00403/CORE -c top_wrap.c
% gcc -DPIC -fpic -Itop-3.4 -c top-3.4/machine.c
% gcc -DPIC -fpic -Itop-3.4 -c top-3.4/utils.c
% gcc -DPIC -fpic -Itop-3.4 -c top-3.4/username.c
% ld -Bshareable  -L/usr/local/lib -o Top.so \
             top_wrap.o machine.o utils.o username.o -lkvm

The warning from line 28 about the read-only array member (double load_avg[NUM_AVERAGES]) is telling - the distinction between an array and a pointer is subtle in C. To avoid such problems, SWIG treats references as read-only by default. You can usually create unambiguous types with typedef if you need to.

The last line above uses ld to link the four .o files with the KVM library. KVM is the kernel memory interface for FreeBSD; it will be loaded along with the top library at the first call to the top module. Since the module opens sensitive kernel structures, you'll most likely need to run it as the superuser.)

Output of our top emulator

load averages:  0.14,  0.03,  0.01                                   22:44:41
30 processes: 1 running, 29 sleeping, 
CPU states:  5.3% user,  0.0% nice,  1.5% system,  0.8% interrupt, 92.4% idle
Mem: 27M Active, 8M Inact, 14M Wired, 5060K Cache, 7640K Buf, 6952K Free
  PID USERNAME PRI NICE SIZE    RES STATE    TIME   WCPU    CPU COMMAND
15121 root     28   0  1276K  1748K RUN      0:00  2.13%  0.84% perl
14499 root     18   0   684K   896K pause    0:00  0.00%  0.00% tcsh
14498 scott     2   0   608K  1828K select   0:00  0.04%  0.04% xterm
13917 scott     3   0  1448K  1652K ttyin    0:02  0.00%  0.00% vi
  242 scott    18   0   828K  1016K pause    0:04  0.00%  0.00% tcsh
  241 scott     3   0   652K   944K ttyin    0:00  0.00%  0.00% tcsh
  240 scott     3   0   664K   952K ttyin    0:00  0.00%  0.00% tcsh
  239 root      2   0  1244K  1380K select   1:08  0.00%  0.00% perl
  238 scott     3   0   772K   980K ttyin    0:05  0.00%  0.00% tcsh
  234 scott     2   0   532K  1340K select   0:00  0.00%  0.00% xterm
  233 scott     2   0   532K  1408K select   0:19  0.00%  0.00% xterm
  232 scott     2   0   208K  1172K select   0:02  0.00%  0.00% xclock

Take a Swig: The top Emulator

A Perl program that emulates top is A SWIG-enabled Top Emulator. There are a few new aspects to it, notably the ptrvalue calls, but most of the program is straightforward.

If you look back at machine.h you'll see that the statics structure has pointers to character arrays. Normally, the potential ambiguity of pointers to pointers would cause SWIG to punt unless it had explicit directions for what to do. However, the array-of-strings construct is so common that SWIG provides support via the pointer.i interface file.

The names() subroutine steps through the char ** array, pulling off the strings one by one until NULL is reached. ptrvalue() is a standard SWIG function that requiring an array reference and an index; it returns the element at that index. The assembled array is then returned by names(). The memfix() routine is more mundane. It just converts a number from kilobytes to megabytes.

The initialization section of the script creates three structures ($statics, $si, and $ps) via calls to new(). SWIG's %make_default pragma (back in top.i) automatically allocated the structures and created the new() methods for you.

The ps structure is initialized by hand so that its values make sense the first time they are used, and the Top::machine_init() method populates the statics structure. The three calls to names() extract the respective names into arrays.

The remainder of the script is just a loop that repeats sixty times. On each iteration, the script pauses, gathers current statistics, and prints a top-style report. The only feature missing is the sorting of processes according to their CPU usage.

This script is simple minded; it merely emulates top rather than extends it. Still, it runs equally well on all top-ready operating systems. More sophisticated scripts might extend top in different ways: data trending, event triggers, real time plots of system data, and so on.

A Toast

SWIG's 300 page user manual goes into great detail about features not covered in this article: pointers, input constraints, typemaps for complex data types, exception handling, and further customization. It also covers C++ and Objective C.

Other common uses of SWIG include rapid prototyping, interactive debugging, script-based testing of systems, and optimizing existing scripts by implementing slow portions in C or C++. SWIG makes it simple to embed C and C++ code in your favorite interpreter. In addition to Perl, SWIG can just as easily generate interface code for Tcl, Guile, and Python. Best of all, SWIG is portable and free. A toast is in order.


Scott Bolte (bolte@niss.com) recently joined GE Medical Systems in Milwaukee as a Lead Software Architect for the Unix Foundation Group.

listing 1

elapsed c: a C function to be wrappered by SWIG
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
#include <sys/time.h>
/* 
   Return the number of seconds since the first time 
   elapsed_seconds() was called.
 */
float
elapsed_seconds(void)
{
    	static struct timeval then = {0, 0};
    	struct timeval  now;
    	/* The first time we're called note the time 
           and then return 0.
    	 */
    	if (then.tv_sec == 0) {
               (void) gettimeofday(&then, 0);
               return (0);
    	}
    	/*
           Return the elapsed time as a floating point 
           number on subsequent calls.
    	 */
    	(void) gettimeofday(&now, 0);
    	return (now.tv_sec - then.tv_sec
             + (now.tv_usec - then.tv_usec) / 1000000.0);
}

listing 2

elapsed: Using the Wrappered C Function
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
elapsed: Using the Wrappered C Function

#!/usr/bin/perl -w
# Use the first N elements of the Fibonacci sequence 
# to approximate the Golden Mean.
use strict;
use Elapsed;
my $before = Elapsed::elapsed_seconds();
my $N = shift || 20;	 # Take either first argument or 20.
my ($n1, $n2) = (1, 1);
printf("%2d:  %10d\n", 1, $n1);
printf("%2d:  %10d  %.10g\n", 2, $n2, $n2/$n1);
for (3..$N) {
    	($n1, $n2) = ($n2, $n1 + $n2);
    	printf("%2d:  %10d  %.10g\n", $_, $n2, $n2/$n1);
}
my $after = Elapsed::elapsed_seconds();
printf("Elapsed time is %g seconds.\n", 
                                 $after - $before);

listing 3

Downloading SWIG
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
Downloading SWIG

SWIG's home page is https://www.cs.utah.edu/~beazley/SWIG/swig.html.

You can download it from the CPAN site nearest you in CPAN/authors/Dave_Beazley, or from ftp://ftp.cs.utah.edu/pub/beazley/SWIG.

To join the SWIG mailing list, send a message saying subscribe swig to Majordomo@cs.utah.edu. The message volume is low and the signal-to-noise ratio is high, because Dave Beazley responds to most questions himself.

listing 4

machine.h: one of top's header files
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
machine.h: one of top's header files

/*
 *  This file defines the interface between top and the machine-dependent
 *  module.  It is NOT machine dependent and should not need to be changed
 *  for any specific machine.
 */
/*
 * the statics struct is filled in by machine_init
 */
struct statics
{
    char **procstate_names;
    char **cpustate_names;
    char **memory_names;
};
/* The system_info struct is filled in by a machine-dependent routine. */

struct system_info
{
    int    last_pid;
    double load_avg[NUM_AVERAGES];
    int    p_total;
    int    p_active;     /* number of procs considered "active" */

    int    *procstates;
    int    *cpustates;
    int    *memory;
};
/* cpu_states is an array of percentages * 10.  For example, 
   the (integer) value 105 is 10.5% (or .105).
 */
/*
 * the process_select struct tells get_process_info what processes we
 * are interested in seeing
 */
struct process_select
{
    int idle;           /* show idle processes */

    int system;         /* show system processes */

    int uid;            /* only this uid (unless uid == -1) */

    char *command;      /* only this command (unless == NULL) */

};
/* routines defined by the machine dependent module */

char *format_header();
char *format_next_process();
/* non-int routines typically used by the 
   machine dependent module */
char *printable();

listing 5

top.i: a SWIG interface file
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
top.i: a SWIG interface file

%{
#include "top.h"
#include "machine.h"
char *printable(str)
    	char *str;
{
    	int c;
    	for (c = 0; str[c] != '\0'; c++) {
        		if (!isprint(str[c]))
            			str[c] = '?';
    }
    	return (str);
}
char *full_format_header(char *uname_field)
{
    	return (format_header(uname_field));
}
char *full_format_next_process(caddr_t handle)
{
    	extern char    *username(int uid);
    	return (format_next_process(handle, username));
}
%}
%include pointer.i
%include typemaps.i
%include "top.h"
%pragma make_default
%include "machine.h"
extern char   *full_format_header(char *uname_field);
extern char   *full_format_next_process(caddr_t handle);
extern int    machine_init(struct statics * statics);
extern void   get_system_info(struct system_info * si);
extern caddr_t  get_process_info(struct system_info * si,
    struct process_select * sel,
    int fake = 0);

listing 6

Output of our top emulator
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
Output of our top emulator

load averages:  0.14,  0.03,  0.01                                     22:44:41
30 processes: 1 running, 29 sleeping, 
CPU states:  5.3% user,  0.0% nice,  1.5% system,  0.8% interrupt, 92.4% idle
Mem: 27M Active, 8M Inact, 14M Wired, 5060K Cache, 7640K Buf, 6952K Free
  PID USERNAME PRI NICE SIZE    RES STATE    TIME   WCPU    CPU COMMAND
15121 root     28   0  1276K  1748K RUN      0:00  2.13%  0.84% perl
14499 root     18   0   684K   896K pause    0:00  0.00%  0.00% tcsh
14498 scott     2   0   608K  1828K select   0:00  0.04%  0.04% xterm
13917 scott     3   0  1448K  1652K ttyin    0:02  0.00%  0.00% vi
  242 scott    18   0   828K  1016K pause    0:04  0.00%  0.00% tcsh
  241 scott     3   0   652K   944K ttyin    0:00  0.00%  0.00% tcsh
  240 scott     3   0   664K   952K ttyin    0:00  0.00%  0.00% tcsh
  239 root      2   0  1244K  1380K select   1:08  0.00%  0.00% perl
  238 scott     3   0   772K   980K ttyin    0:05  0.00%  0.00% tcsh
  234 scott     2   0   532K  1340K select   0:00  0.00%  0.00% xterm
  233 scott     2   0   532K  1408K select   0:19  0.00%  0.00% xterm
  232 scott     2   0   208K  1172K select   0:02  0.00%  0.00% xclock

listing 7

A SWIG-enabled top emulator
Scott Bolte (1997) SWIG. The Perl Journal, vol 2(4), issue #8, Winter 1997.
A SWIG-enabled top emulator

#!/usr/bin/perl -w
use strict;
use Top;	       # treated like any other module
sub memfix ($) {
    	my $label = shift;
    	if ($label =~ m/(\d+)K/ && $1 > 8192) {
        		my $M = int($1 / 1024);
        		$label =~ s#$1K#${M}M#;
    	}
    	return $label;
}
sub names ($) {
    	my $ref	  = shift;
    	my @names = ();
    	for(my $i = 0; $val ne "NULL"; $i++) {
        		my $val = Top::ptrvalue($ref, $i);
        		push(@names, $val);
    	}
    	return(@names);
}
my $clear = 'clear';
# contains the OS specific field names
my($statics) = new statics();
# contains the raw system information
my($si)	     = new system_info();
# used to store process information
my($ps)	     = new process_select(); 
$ps->{idle}	     =  1;
$ps->{"system"} =  0;
$ps->{uid}	      = -1;
# Extract field names from their respective null-terminated lists.
Top::machine_init($statics);
my(@procstates) = names($statics->{procstate_names});
my(@cpustates)  = names($statics->{cpustate_names});
my(@memory)     = names($statics->{memory_names});
# We'll emulate top's basic display sixty times.
Top::get_system_info($si);
for (1 .. 60) {
    	sleep(1);
    	# Get the info and clear the screen
    	Top::get_system_info($si);
    	my $handle = Top::get_process_info($si, $ps);
    	print $clear;
    	# "load averages:  0.02,  0.05,	 0.01  20:43:09"
    	print("load averages");
    	for my $i (0 .. 2) {
            my $value =Top::ptrvalue($si->{load_avg},$i);
        	printf("%s %5.2f", $i == 0 ? ":" : ",", $value);
    	}
    	printf("\t\t\t\t      %2d:%02d:%02d\n", 
                              reverse((localtime())[0..2]));
    	# "34 processes: 1 running, 32 sleeping, 1 stopped"
    	printf("%d processes: ", $si->{p_total});
    	for my $i (0 .. $#procstates) {
           my $value = Top::ptrvalue($si->{procstates},$i);
            next 	unless $value;
            printf("%d%s", $value, $procstates[$i]);
    	}
    	print("\n");
    	# "CPU states:	2.3% user,  0.0% nice,	1.5% system,  
        # 0.0% interrupt, 96.2% idle"
    	my $sum = 0;
    	for my $i (0 .. $#cpustates) { 	
		    $sum += Top::ptrvalue($si->{cpustates}, $i) }
    	$sum /= 100.0;
    	for my $i (0 .. $#cpustates) {
            my $percent = Top::ptrvalue($si->{cpustates},$i)/$sum;
               my $value  = $percent == 100.0 	? "100" : 
			                 sprintf("%4.1f", $percent);  
               printf("%s %4s%% %s", $i == 0 ? "CPU states:" : ",", 
                      $value, $cpustates[$i]);
    	}
    	print("\n");
    	# "Mem: 25M Active, 3752K Inact, 14M Wired, 8M Cache, 
		#  7323K Buf, 8M Free"
    	print("Mem: ");
    	for my $i (0 .. $#memory) {
        		my $value = Top::ptrvalue($si->{memory},$i);
        		next 	if $value == 0;
        		print(memfix("$value$memory[$i]"));
    	}
    	print("\n");
    	# "29938 root   28   0  1204K  1648K RUN   
        #  0:00 20.31%  0.99% perl"
    	print("\n");
    	print(Top::full_format_header("USERNAME"), "\n");
    	for my $p (1 .. $si->{p_total}) {
            print(Top::full_format_next_process($handle), "\n");
    	}
}
Martin Krzywinski | contact | Canada's Michael Smith Genome Sciences CentreBC Cancer Research CenterBC CancerPHSA
Google whack “vicissitudinal corporealization”
{ 10.9.234.152 }