Graphical Applications Using MetaCard
A top program is a very useful thing to have on a multi-tasking operating system like Linux. You can use it to keep track of the CPU and memory usage of all programs running on the system and to detect and kill runaway processes. The character-based top program that comes with Linux systems can be improved. Because it isn't aware of the windowing system, it doesn't go to sleep when the window it is running in is iconified. It would also be nice if you could select a process to kill by clicking on it instead of having top type in its process ID.
Fortunately, Linux has a very simple and elegant way to get process information, and so it's easy to develop a graphical application to display this information using a tool like MetaCard. This article will show you how it's done using MetaCard's scripting language MetaTalk. MetaTalk is a very high level language (VHLL) that supports building complete applications with very little effort. The MetaTalk language has an English-like syntax and is very concise. This makes it easy to learn, yet doesn't sacrifice the power and compactness found in other HVLLs such as Perl. It is very readable, so it's easy for non-expert users to figure out what a script does (which can be much more difficult with languages like Perl).
Each process that is running on a Linux system has a directory in /proc. In that directory are several files containing information on the process. To implement a graphical top program, we're most interested in the information in the file named stat, which contains process run time and memory usage information. We'll also use the file named cmdline that contains the command line used to start up that process.
The stat file contains a single line of information with the fields separated by spaces. Details on the format can be found in the proc man page. Information on this page indicates that on a Linux 1.2 system, words 14 and 15 are the user and system run times, respectively, for a particular process. Word 23 is the process size in bytes, and word 24 is the number of 4KB pages currently in RAM for that process.
There are also files in the /proc system that contain information about the whole system. We'll use the /proc/stat file which contains overall system resource usage. We'll need that information to compute the percentage of CPU usage for each process.
Each time the display is to be updated, a program must do the following:
Read the stat file in the /proc directory.
Find all of the subdirectories in the /proc directory.
Read the stat and cmdline files in each of these directories.
Compute the CPU time for the process by subtracting from the last time.
Save the current CPU usage.
Convert the CPU usage into a percentage of total usage.
Build the list of processes and display it.
Schedule a time to redo the update.
The MetaTalk handler that does all of these things is called updatelist and is shown in Listing 1. This handler, like all MetaTalk message handlers, starts with the word on and the name of the message to be handled. The first few lines of this handler declare all the local variables used in this handler. While not strictly necessary, it's a good idea to declare variables to avoid bugs caused by misspelling variable names. To check your scripts, you can set the MetaCard property explicitVariables, which will flag as an error any variable used before it was declared.
The handler then gets the global system time statistics from the file /proc/stat and subtracts the values from the last time the handler was called. The time statistics are then stored in a local variable declared outside the handler, which works like a “static” variable in C. That is, it retains its value like a global, but can only be referred to within the script, so it doesn't pollute the global name space. This variable, like all MetaTalk variables, can be used as an associative array without special declarations. All you have to do is put an alphanumeric string between the [] (square brackets).
Note the expressions of the form “word x of y”. These are called “chunk” expressions and are a very powerful feature of MetaTalk. With them you can access elements of a string individually without having to split up the whole string into an array first. Also note that you can add words together without having to explicitly convert them into numbers first. This saves development time and makes scripts smaller than they would be in lower-level languages.
The readfile function used in the updatelist handler is shown in Listing 2. Like all function handlers, it starts with the keyword function and is used in expressions just like MetaCard built-in functions.
There is one unusual thing about this function. Normally in MetaCard, one would use:
read from file x until eof
because when you specify eof as the terminating condition, MetaCard speeds things up by getting the file size and reading the whole file with a single system call. This doesn't work for /proc files since the stat() system call returns the file lengths as 0 bytes, even though they contain data. Therefore, we must force MetaCard to read the file a byte at a time by specifying empty as the terminating condition.
The readfile function also has to handle the case where the file specified doesn't exist, which can happen if the process exits after the ls command that obtains the directory contents is executed. It also needs to convert nulls (ASCII 0 characters) in the strings to spaces, since some of the files use this character as a delimiter. Note that this requires a scripting language that can handle binary data. MetaTalk has no problem with this, but many other scripting languages do.
The rest of the updatelist handler does steps 2 through 7 in the above recipe. The resulting listing is sorted twice to remedy one of the more annoying characteristics of the character-based top program: the individual processes bounce around the listing unpredictably if they're not using any CPU time. Instead, we'll sort them by process size and then by CPU time which is a more useful way to do it. This is only possible because MetaCard's sort is stable, which means order is preserved on sequential sorts if elements have the same key value.
Finally, the updatelist handler uses the send command to schedule a call back to itself in a few seconds and stores the ID of the timer send creates in a variable. This local variable can be used to cancel the timer using the cancel function. For example, when the application is iconified, we want to stop the processing (See Listing 3).
I snuck in a bit of object-oriented programming in the send command. The message, sent to “me”, is the object whose script is currently executing. The time interval is specified as “the update interval of me”. This object has a custom property named updateinterval that is persistent, which means it's saved whenever the object is saved. That's why you don't see any initialization of this property in these listings; it's data stored with the stack, not the code.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
- Weechat, Irssi's Little Brother
- New Products
- Developer Poll
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




1 hour 18 min ago
2 hours 4 min ago
2 hours 14 min ago
2 hours 19 min ago
4 hours 29 min ago
4 hours 30 min ago
5 hours 15 min ago
6 hours 4 min ago
6 hours 27 min ago
8 hours 4 min ago