Sunday, March 30, 2014

Communicating with the Intel Galileo

Developers have confusing array of ways to communicate with the Intel Galileo embedded board.  This is partially because the board wants to look like an Arduino while at the same time letting developers make use of the much larger hardware provided by the Intel platform.  There are probably 4 main areas for communication

  1. Programming the board with the IDE.
  2. Communication between the board and a host computer using the Arduino library.
  3. Administration  and monitoring of the board's embedded Linux operating system.
  4. Communicating between the board and other systems using network or other Linux level capabilities.
This chart describes the main channels of communication.  The Arduino IDE and Arduino Serial device are mapped to the USB port. The USB port does not support any type of multi-channel composite device.  This means you can only communicate with the underlying Linux system if you build a custom serial port cable or connect via Ethernet.  The embedded Linux does not start telnetd or sshd.  The SD card Linux does not start telnetd or sshd by default.

Intel expects folks to communicat using /dev/ttys1 with a serial to phono jack cable. I'm not sure why they didn't just leave the port as a 3.3v/5v signal level that could be used with an FTDI style USB to serial adapter. That is the way most folks connect to routers, and other embedded systems.  Most modern laptops don't have real serial ports so the Virtual COM USB adapters would have been perfect.

The Simplest Way: Connecting via Ethernet

Ethernet is the simplest way to communicate with an Intel Galileo board without making or buying any type of custom cabling. You will need an Ethernet cable to connect the Galileo to your laptop or your network hub/router.  I prefer to use my hub because that leaves my development machine connected to the main network in case I need to look stuff up on the Internet.

My network is all DHCP for address -  This means I can put embedded devices at fixed addresses if I want to talk to them without using some kind of zeroconf tooling.  The following script enables telnet and configures the Ethernet port to

void setup() {
  system("telnetd -l /bin/sh");
  system("ifconfig eth0 netmask up");
void loop() {
  // Put the rest of your sketch here.

Connecting from a Windows Machine

Download and install Putty.  It provides nice terminal services that let you communicate to the Intel Galileo via Ethernet or over the virtual com port created when the board's USB is connected to your computer. Putty also supports SSH if that is what you enabled with your script above instead of telnet.


You have to reload and re-run the sketch every time you power on the Galileo board unless you have saved it to the optional SD card.  


Telnet and ifconfig for Intel Galileo
Great serial port discussion

Last Edited 3/30/2014

Saturday, March 29, 2014

Communicating with Ambient Orb via Serial Port

Pins on the back of ambient

Powered by PIC 18f242 CPU with 16K bytes ROM and 768 bytes RAM.

Physical Connection

pin 1 is on the left looking at it from the back

Pin 1 Vunreg - 11 volts DC from power transformer
Pin2 MCLR - used to reset device for programming. Leave unconnected
Pin 3 PGD - used for programming. Leave unconnected
Pin 4 PGC - used for programming. Leave unconnected
Pin 5 RS-232 RX (TTL level signal)
Pin 6 RS-232 TX (TTL level signal)
Pin 7 Ground

RS-232 Parameters

The serial port connects to the orb with the following fixed parameters:

  • 19200 Baud
  • 8 data bits
  • no parity
  • 1 stop bit
  • no flow control

Sample Script
  1. Install perl
  2. Install serial port library for perl with
    apt-get install libdevice-serialport-perl
  3. Download
  4. Change the tty port to match that created when you plugged in your USB-->serial cable.
Bluetooth Setup
Sparkfun BlueSmirf

connect with Hyperterm
+++ to go to command mode within 60 seconds of start
su,19 to set the serial port baud rate to 19200 for ambient device
sy,0004 for standard power 12db
sy,fff8 for 0db
o shows settings
gy shows current power
sn,<new name> to set the name
--- to leave command mode

Managing the Data Lifetime and Access Through Scoping

 Software developers think of the lifetime of data or objects within the confines of the language or frameworks they are using.  They need to think of data lifespan or scope as a first class component of design.

Business and business process data is valid for some period of time.  It has a lifetime, scope, that may vary based on business rules, transaction time or some other function.  Technical or software transaction data is also scoped, valid for some period of time.

This talk focuses on how these lifetimes can be more naturally integrated into a running software system.


Every business entity, attribute or property is valid for some period of time. It may be the lifespan of a project, a contract period , the length of time someone is a customer. 

 Transient copies of the data or newly created business entities may be valid for the life of some user session or only while some transaction operation is active.  This means applications often operate on data with overlapping lifespans, or scopes.   Global customer information is valid during a session while a reservation, policy or loan update is only valid until it committed to some back-end system.  Think about larger state-full applications where transactions can span multiple pages or where multiple different transactions are integrated into a single software flow.

Good software design takes into account this mixing of lifespan.  We want to plan ahead by grouping related data and same scope data.  We also want to build infrastructure that makes data cleanup and reset easier and more reliable. Good frameworks mean more reliable data with less mixing or missed cleanup.  The best systems support both explicit and implicit lifecycle management.

Teams can ignore pieces of this discussion when working simple forms based CRUD applications where a page maps to a table and where each submission commits data independently.

Overlapping and nested


Infrastructure Scope


Well Understood Web Scopes


Business Data Lifespan


Operation Specific Scopes


Possible Implementations


Web Frameworks


Custom Scopes Using DI


Proactive Explicit Scope


DI Blending Different Scopes

Tuesday, March 18, 2014

Adding a Bluetooth adapter to a Dell E6430

My Dell outlet site E6430 didn't come with a Bluetooth adapter or the cable to attach it.  Dell offers their 380 which supports bluetooth 4.0 LE.  You can go to EBay to purchase the parts.  The cable is kind of rare and surprisingly pricey. I have found in the past that the Dell parts department will often beat Ebay especially for slightly older or refurbished parts.  It has always worked out well for me.  Go to their parts website and start a chat session with a Dell representative.  They can sell you parts that do not show up on parts / accessories site.  Dell used to have a lot of interesting parts on their web site.  They seem to have pulled a lot of that off the public facing site so you're best bet is to use a chat session or call the toll free number.  My total cost was $24.35.  You need the following to do an upgrade:
  1. Dell 380 Bluetooth Adapter part number 3YX8R.  It cost me $9.99.
  2. Dell E6430 Bluetooth cable part number 1RNW1. It cost me $4.99
  3. A screwdriver
  4. Some patience.
The service manual tells you how to take apart the machine.  The basic steps are:
  1. Remove the battery
  2. Remove the hard drive (2 screws)
  3. Remove the rest of the screws holding the back on. You don't have to remove the screws that hold down the keyboard.
  4. Attach the cable to the bluetooth adapter. The cable runs up, away from the component.
  5. Plug the other end of the cable into the connector under the hard drive SATA connector. I used a small flat blade screwdriver to make sure it was seated.  My hands are too big to grip the connector when putting it in under the SATA Disk Connector
  6. Tuck the cable into the channel guide above the Bluetooth adapter and run the excess under the SATA connector.

You may wish to test the upgrade before putting the unit back together.   One of my cables was not in all the way and I had to take the back off again to fix it.

Thursday, March 13, 2014

A quick jni4net performance hack

jni4net is a great library that creates Java proxies for Microsoft based C# libraries on the Windows platform. It makes Java JNI easy with good performance.  You follow this processes to use jni4net

  1. Create your C# library
  2. Run proxygen to generate Java native library source code and C# adapter source that shims in between Java and the original library.
  3. Run javac to compile the java code
  4. Create a jar file of the compiled java code
  5. Run the .Net csc.exe that comes with the default .Net installation to compile the C# code
  6. Create a new DLL of the just compiled C# code
  7. Create a Java program that uses the new generated Jar file, the generated C# dll and the original C# dll.

I found a quick hack that improves the performance of the jni4net bridge that involves inserting a line of code in each generate C# method. This hack improves the Java-to-DLL call time only.  It does not impact the performance of the Java code on one side or the C# code on the other.  It lowers the overhead of highly repetitive method calls by up to 100x for methods with no type marshaling.  Each method looks something like this:

I add one line of code that makes the method look like this:

This Powershell script modifies all jni4net generated methods to add the code shown above.  You can also find the script on github :


Java API Wrapper for Windows Performance Counters

Now its time for a quick overview of the Java wrapper API for the Windows Performance Counters.

System Preparation 
Library Initialization
Counter Caching 
Key Retrieval 
Counter Usage
Time Based Operations
Related Posts


The lifecycle for these counters is pretty straightforward:
  1. System Preparation: creation of counters in the Windows OS.
  2. Library Initialization: loading and initializing the library inside your running program
  3. Counter caching an optional step that warms up the counter library
  4. Key retrieval for any counter you use
  5. Counter usage including the API

System Preparation

You must create any custom counters as Administrator before running any program that uses the Performance Counter system. The simplest way to do this is in a Powershell script because Powershell is available on all modern Windows operating systems.  The following sample script creates a new Category "Freemansoft.JavaTestCategory" and two simple counters  "TestCounter" and  "TestRate".  

Some counters work in pairs with a primary counter and a base counter. You must create paired counters together in the same Powershell script. Windows assumes that compound counters are always created with the primary first and the base immediately following. 

Library Initialization

You must load the counter library one time somewhere in your program setup and initialization. The library uses Java JNI to load a DLL. Each DLL can only be loaded once.  Here is code from one of the Java examples.

Category / Counter Caching

This library wraps a Windows API that proxies the Windows Performance Counter library. There is a large, about 400ms, penalty the first time you access a performance counter.  You can remove this penalty for users by warming up the library by pre-caching the categories.  There is a simple API for this using this method  where you specify the full string name of the Category and the optional instance. You can pass null or empty string for a Category with only a default Instance.


Key Retrieval

The underlying C#/C library API accepts string based Category, Instance and Counter names for every operation. We get significantly better performance by creating an API that avoids string marshaling and copying wherever possible. 

The WindowsPerformanceFacade class uses a unique integer based key to represent a Category, Instance, Counter combination.  The Instance value is set to null in the case of a Category without multiple instances. Applications should retrieve and cache keys for any counters they intend to use in their start-up code.

Counter Usage

The rest of the API manipulates counters.  There are a couple subtleties to the API

  1. Most of the manipulation methods have two overrides. One that takes a single value and the other takes two values.  
    • Always use the single for simple counters.  
    • You can use either the single or two value methods for compound counters where there is a primary counter and a base counter.  
      • The single value method will automatically increment any associated base counter by 1. 
      • The two value method lets you specify exactly the values you want for the primary and base counter. 
  2. There is no two value API for Decrement().
  3. NextValue() returns the calculated value not the RawValue of the counter.  See the Microsoft documentation for information on how calculations are made for various counters.
  4. SetRawValue() and GetRawValue() get the current non-computed value for the counter. You may use SetRawValue() to initialize a counter. Both counters must be individually manipulated if you are using SetRawValue() There is no Counter/Base counter combination method at this time.


Time Based Operations

Some counters use intrinsic time calculations. Counters like ops/second are simple counters that accept some integer as a count and then calculate the rate/second using the built in timing calculations.

Some compound counters use time in the numerator of a rate calculation, time/packet or time/byte. The caller provides time in these situations using the Microsoft Stopwatch timer and not the system timer. You cannot use Java Date or Time values when doing time operations in the Windows Performance Counters. The Stopwatch is a high resolution timer with more precision than the standard timer.  The Stopwatch  is exposed to Java applications via the GetStopwatchTimestamp() in both WindowsPerformanceFacade and WidnowsPerformanceLiason. 

  1. Interval timers can be based on some "amount of time" that an operation took or paused. In this case, make a GetStopWatchTimestamp()call prior to the operation and then another at the end of the operation. Pass the difference in the two times to the timer.
  2. Other timers just take the current time as a value. In this case just call the GetStopWatchTimestamp()method and pass the results to the timer call.
Source code on GitHub

This blog uses the Syntax Highlighter by Alex Gorbatchev

Sunday, March 9, 2014

Recording Java Metrics with the Windows Performance Counters

Example Java Code 
Library Performance 
Library and Source Code
Repository Organization
Related Posts

Java provides good monitoring through the MBean interface and monitoring tooling. It is nice because it works the same no matter what platform you run on.  There are times when you want to plug into the native performance recording tools especially when running on Windows machines because this makes it easy to integrate your Java application with your Windows based operational consoles.

A Java test program generates 2,000,000 performance counter events per second in a single thread and up to 6,500,000 updates per second multi-threaded. (source available on github) This means that counter updates can be made at normal volumes with very little effect on the system.

I've created a simple C# library and Java / JNI wrapper that lets you record native Microsoft Windows Performance Counters directly from your Java code.  Those events can then be viewed with perfmon.exe or other Microsoft Windows tools.  You can also view performance counters on remote machines using perfmon.exe.

The graph on the right shows how the rate operations per second counter behaves when feed a square wave of traffic: 200 messages per second for 2 seconds followed by 0 messages per second for 2 second.  The counter averages the rate/second across time resulting in a saw tooth pattern.

Example Java Code

The previous perfmon.exe image was built by the following code that calls the increment() functions on a single counter at regular intervals.

  1. The first portion initializes the bridge. 
  2. The second registers the category/counter combination to obtain a key. 
  3. The final section updates the counter in a loop.


Library Performance

This table describes the library's maximum performance as measured using a circa 2011 I7 Macbook Pro. The test programs are in the source tree on GitHub.  The C# library has two public interfaces.

  • WindowsPerformanceLiason is an instance method based interface that uses string Category, Instance and Counter keys in the same way the underlying calls to the windows system do. This approach suffers from continual string mapping and copying as strings are passed across the Java / C# boundary.
  • WindowsPerformanceFacade is a static class wrapper accepts integer keys representing a Category, Instance and Counter.  Callers first requests an integer key using those three strings and then uses the integer key from then on. The previous example code demonstrates use of this interface.
The JNI code was generated with JNI4Net.  Initial performance was pretty disappointing both from a maximum Counters / Second and from CPU usage point of view.  I found what I think is a small problem with the JNI4Net support library. I wrote a small powershell script that massages the generated C# JNI code to get the "Patched JNI4Net" numbers you see below.

ThreadsC# LibraryInstance Methods
String Keys
Default JNI4Net
Instance Methods
String Keys
Patched JNI4Net
Static Methods
Integer Counter Key
Default JNI4Net
Static Methods
Integer Counter Key
Patched JNI4Net

Library and Source Code

You can find the library on GitHub  C# source, Java test program source, powershell build and counter configuration scripts and pre-built binaries available on GitHub.  Pre-built dll and jar files are located in the Packages directory.

Most java clients will use the WindowsPerformanceCounterFacade class in green.  It totally encapsulates the functionality for updating Windows Performance Counters from a Java prog


Your program's classpath needs to include the WindowsPerformanceCounter jar file, the jni4net jar file and the directory where the two dlls are on the file system. Here is my Eclipse .classpath file when working with the examples.

It should work for most folks building the examples because of the relative path to the libraries.

Creating Counters in the Windows Operating System

You must create the counters with using an account with Administrator privilge.  Powershell scripts are the simplest way to do this. You can find a powershell script in the java directory on Github.  A sample can also be found in an earlier blog article. You can create as many counters as you wish in a single Powershell script.  Compound or "bonded" counters must be created as sequential counters with the primary counter first and the base counter second.

Repository Organization

-- This section is incomplete --

Friday, March 7, 2014

Windows Performance Counters

Microsoft Windows has a mechanism for recording performance metrics from running applications using the Windows Performance Counters.  This is a high speed system that can be used to gather counts, rats, averages or other numbers based on raw counts or ratios like items/second.  You can see these counters using the perfmon application.  This system has low overhead and is capable of a high rate of capture. I measured 10 million messages / second using 4 threads on a 2011 Macbook Pro.  Performance Counters are generated/updated by major system modules and can be extended to include your own modules. Windows 8 comes with over 29,000 standard counters. You can see them by running the typeperf.exe -qx > counterlist.txt.  You can see the Performance counters in action using perfmon.

This screen shot shows the perfmon application monitoring a cpu performance counter and a custom test performance counter generated by this c# library I've put up on github.  The ROCPS64 counter is a Rate Operations Per Second 64 bit counter that counts the number of updates per second.  Look at the Last field where it shows 10,056,419 operations per second.  (I've scaled the view divided by 1,000,000 so that it can be overlayed with the % CPU processor time. This test consumes about 45% of the CPU which represents 4/8 hperthreads running at almost 100% untilization.

Note: Perfmon is showing a standard CPU counter overlayed with a custom "ROCPS64" rate of operations per second counter.


Performance counters are managed under a Category / Instance / Counter hierarchy.  

  • The Category represents the area of interest like CPU, Disk or Application.  
  • The Instance  represents one of many of that item like the an individual disk drive, C:, D:.  Categories can have only one, default, instance. You're laptops power management counters only exist once for the one laptop. These would use the default instance which is specified by a null in the API.  
  • Actual performance information is recorded in individual Counters.  Each Category/Instance can have any number of counters. Each instance within a category normally has the same counters.  For example:  each CPU\Core has the same statistics, %load , current frequency.
Microsoft provides access to the performance counters with a C function library and  a set of C# wrapper classes.  Both APIs are string based where each counter update call accepts the string name of the Category, (optional) Instance and the Counter.  It is a string based API.

API Performance

The Performance Counter sub-system is capable of very high parallel counter update rates so that all the O/S and application metrics can be written with minimal impact to the system. I mentioned above that parallel testing shows that a user land application can update a custom counter at about 2.5 million updates per second per hyperthread core on a circa 2011 Intel I7 laptop. This is maximum performance. Programs should/would update counters at significantly lower rates to leave CPU time to do actual work.  

Programs first retrieve the counter as part of a Category. My testing shows that Category/Counter retrieval is very slow on the order order of 300-400ms.  

Programs should retrieve the Category/Counter objects one time and cache them to get reasonable performance.Counters are thread safe so you can updated individual counters from multiple threads with almost linear performance.

Programs update the counters through the Increment(), IncrementBy() or Decrement() methods provided by the API. Increment() is up to 10X times faster than IncrementBy(1).   Use IncrementBy() to update counters showing things like "Bytes per message". Do not call Increment() 40,000 times to represent 40,000 bytes. 

Counter Types

All counters have the same Increment(), IncrementBy(), Decrement() update API and the same NextValue() retrieval API.  NextValue() returns the calculated value based on the counter type.  Microsoft doesn't call it this but I think of the counters as three different types. I think of them as Simple, ImplicitRatio and Compound Ratio.  The first two types each use a single actual counter.  The last type requires two bound counters where the two counter values are combined for some calculated values.  


 Simple counter Counters that just keep a rolling count.  NextValue() returns the current count. 

Implicate Ratio

Implicit Ratio counters calculate ratios based on the counter value as a numerator and some implicit denominator like seconds.  The  RateOfCountsPerSecond counter is a good example.  You increment the counter and the calculated NextValue() is computed using the count and some values from the system clocks.  

Compound Ratio

More sophisticated counters represent ratios our calculation where more than one counter are involved to provide values.  Each of the compound counters is made up of a primary counter and a base counter.   Example: You could have a bytes per dollar counter where one counter represented the number of bytes processed and the other counter represented the CPU cost for the processing. Windows requires that a counter and base counter be created sequentially with the counter first and the base counter second.

Creating Categories and Counters

Microsoft requires that you have Administrative privileges to create Performance Counters.  You can create your own counters using Powershell or in your C# code.  You must run the Powershell, your program or Visual Studio as Administrators in order to create your own counters. Programs can read counters without escalated rights.

This PowerShell code creates a Category with four counters that make up three metrics: There is one raw counter, one simple rate counter and a compound counter made up of two counters. Paired counters must be added to the counter list consecutively with the main counter first and the base counter second.


C# code is on github that provides automatic caching Category / Instance / Counter objects.  I wrote this code to provide Performance Counter access to Java that I'll discuss in another blog. The C# code and unit tests are located in the WindowsPerformanceCountersForJava directory. Benchmark results were generated using the multi-threaded performance unit-test.  

All unit tests create their own performance counters before running the test and remove them afterwards.  This means you have to build and run the code with Visual Studio running as Administrator. I could have created a Powershell script for counter creation so that VS wouldn't require Administrator access but this provides a single solution for automated testing.

You can find a pre-built FreemanSoft.PerformanceCounters.dll is in the Packages folder on github.

Related Posts