sysstat is a collection of utilities for Linux that provide performance and activity usage monitoring. In today’s post, I’ll go through a brief explanation of these utilities.
iostat
iostat(1) reports CPU statistics and input/output statistics for devices, partitions and network filesystems.
mpstat goes a little deeper into how the cpu time is divided up among its responsibilities. By specifying -P ALL on the command line to it, you can get a report per cpu:
pidstat will give you the utilisation breakdown by process that’s running on your system.
sar
sar(1) collects, reports and saves system activity information (CPU, memory, disks, interrupts, network interfaces, TTY, kernel tables,etc.)
sar requires that data collection is on to be used. The settings defined in /etc/default/sysstat will control this collection process. As sar is the collection mechanism, other applications use this data:
sadc(8) is the system activity data collector, used as a backend for sar.
sa1(8) collects and stores binary data in the system activity daily data file. It is a front end to sadc designed to be run from cron.
sa2(8) writes a summarized daily activity report. It is a front end to sar designed to be run from cron.
sadf(1) displays data collected by sar in multiple formats (CSV, XML, etc.) This is useful to load performance data into a database, or import them in a spreadsheet to make graphs.
Docker is a platform that allows you to bundle up your applications and their dependencies into a distributable container easing the overhead in environment setup and deployment.
The Dockerfile reference in the docker documentation set goes through the important pieces of building an image.
In today’s post, I’m just going to run through some of the commands that I’ve found most useful.
Building a container
# build an image and assign it a tagsudo docker build -t username/imagename:tag .
Controlling containers
# run a single commandsudo docker run ubuntu /bin/echo 'Hello world'# run a container in a daemonized statesudo docker run -d ubuntu /bin/sh -c"while true; do echo hello world; sleep 1; done"# run a container interactivelysudo docker run -t-i ubuntu /bin/bash
# connect to a running containersudo docker attach container_id
# stop a running containersudo docker stop container_name
# remove a containersudo docker rm container_name
# remove an imagesudo docker rmi image_name
When running a container, -p will allow you to control port mappings and -v will allow you to control volume locations.
Getting information from docker
# list imagessudo docker images
# list running containerssudo docker ps
# list all containerssudo docker ps -a# inspecting the settings of a containersudo docker inspect container_name
# check existing port mappingssudo docker port container_name
# retrieve stdout from a running containersudo docker logs container_name
sudo docker logs -f container_name
A really handy feature that has been included in the Scala programming language is xml literals. The xml literals feature allows you to declare blocks of xml directly into your Scala code. As you’ll see below, you’re not limited to static xml blocks and you’re also given the full higher-order function architecture to navigate and process your xml data.
Definition and creation
You can create an xml literal very simply inside of your Scala code:
We test if the gender attribute contains an “M”, and if so we empty out the node. To apply this transform to the source data, we use the RuleTransformer class.
Pretty much the same. The only extra complexity is ensuring that we have an age attribute and getting it casted to an integer for us to perform arithmetic testing.
The RuleTransformer class accommodates if we want to use these two transforms in conjunction with each other.
Python provides a simple way to define anonymous functions through the use of the lambda keyword. Today’s post will be a brief introduction to using lambdas in python along with some of the supported higher-order functions.
Declaration
For today’s useless example, I’m going to create a “greeter” function. This function will take in a name and give you back a greeting. This would be defined using a python function like so:
defgreet(name):return"Hello, %s."%(name,)
Invoking this function gives you endless greetings:
We can transform this function into a lambda with a simple re-structure:
greeter=lambdaname:"Hello %s"%(name,)
Just to show you a more complex definition (i.e. one that uses more than one parameter), I’ve prepared a lambda that will execute the quadratic formula.
Now that we’re able to define some anonymous functions, they really come into their own when used in conjunction with higher-order functions. The primary functions here are filter, map and reduce.
We can filter a list of numbers to only include the even numbers.
filter(lambdax:x%2==0,range(1,10))
Of course it’s the lambda x: x%2 == 0 performing the even-number test for us.
We can reduce a list of numbers to produce the accumulation of all of those values:
reduce(lambdax,y:x+y,range(1,10))
Finally, we can transform a list or map a function over a list of numbers and turn them into their inverses:
For all of the software development that I’ve done here, I’m using the standard Arduino IDE available from their site.
Using the module “normally”
Having a look at the documentation for the Liquid Crystal library, there’s already a comprehensive implementation of filling, scrolling and blinking functions available.
The idea of this article isn’t to re-implement this library, it’s to build on it to add functionality.
The usual setup of the library would looks something similar to this:
This initializes our LCD module, ready for us to start writing to it:
lcd.print("Hello, world!");
Nothing really special here, and rather than trying to piece together the “Hello, World” example from this code here, you’d be better off checking out the samples section for this module.
What’s the idea?
Given that we have a display of 16 characters in width by two characters in height, we’re rather limited in what we can do. What I want to do is expand the 16x2 interface to have a “virtual canvas” to work with of any arbitrary size that you can just use the 16x2 LCD as a window into the canvas.
To begin with, we’ll create some constants to assert our assumptions!
These are pretty straight forward. All of the constants come in handy. Bounds checking is where they really shine, but we also specifically use LCD_HEIGHT as a pitch-type variable that helps us calculate how far into a flat-memory array we need to be to start writing. More on this later.
What do we want to do?
I think initially it’d be nice to:
Define a virtual area (that’s larger than our physical display)
Move the “window” to any point in the virtual area
Render the view at the window co-ordinates to the physical display
Print to arbitrary points of the buffer
I’ve created a class, LCDDoubleBuffer that should wrap all of this functionality up. I’m going to start going through the code from here on.
Define the virtual area
Give a LiquidCrystal reference and buffer dimensions, we can start to initialize our double buffer:
_width, _height and _size all manage the internal data structure for us so that we remember how big our virtual area is.
_lcd is our link back to the “real-world” for us to perform some physical side-effects on our display.
_win_x and _win_y are to co-ordinates to the top left-hand corner of the window that we’ll display on the LCD.
Finally, _buffer is our virtual canvas. Be careful here! Looking at the memory specs, you only get a whopping 2k SRAM to play with. With a char taking up 1 byte, you can see that you’ll quickly go over this if you’re not careful.
Working with the buffer
Well, it’s pretty simple. These are all string operations, and they can be reduced even further than this to say that they’re just memory operations.
To write a string at a given location, we use memcpy. Be careful here! The code that I present below doesn’t perform any bounds checking on where we are in the buffer. This is buffer under-and-overrun city, right here.
That big-fat-if-statement in the middle is protecting us from colouring outside the lines. When we are outside the bounds, we’ll write a clearing character (in this case a whitespace).
Actually using the code
We’ve got a double-buffer defined, ready to go. To test it out, I’ve decided to spice-up the “Hello, World” example and get it scrolling on the display using cosine values so it should ease-out and ease-in; back and forth.
Don’t get too excited though. It’s not brilliant.
// create the LCD reference and double bufferLiquidCrystallcd(8,9,4,5,6,7);LCDDoubleBufferdbuf(&lcd,32,32);voidsetup(){Serial.begin(9600);lcd.begin(16,2);// put "hello world" in the top cornerdbuf.clear();dbuf.print(0,0,"Hello, world!");}floatdegs=0.0f;voidloop(){// calculate and flatten to an intfloata=-3+(cos(degs)*5);intx=(int)a;// move in position and renderdbuf.moveTo(x,0);dbuf.render();delay(50);// move along the cos curvedegs+=0.1f;// 360 degrees == 0 degreesif(degs>360.0f){degs=0.0f;}}
And there you have it. The full source of this demo is available here as a gist in my GitHub repository.