Making i3status work with multiple batteries

The Problem

I recently installed Mint 17 on a Thinkpad T440, and am using i3 as my window manager.

For most of those little things that don’t quite work out of the box in Linux there is a ready-made solution just a Google search away.

I found one that wasn’t. In laptops like the T440 that have two batteries (a non-removable internal and the removable slice battery), i3status defaults to only showing the status of a single battery. I knew that sometimes Linux can be a little less power-save friendly than Windows… but when I saw my estimated time remaining drop by over half on Linux vs on Windows 7… let’s just say I wasn’t happy.

Thankfully, the issue was not in battery or usage, but in the way i3status measures and reports charge. It is pretty easy to simply tell i3status to display both batteries independently (as below), but I would rather just go ahead and combine both.

Selection_002

My initial plan was to fork i3status and build out this functionality, but I found that approach what we might call “non-trivial.”

So, quick and dirty shell scripts to the rescue.

The Solution

By default, i3status looks in /sys/class/power_supply/BAT%d/uevent to extract the information for the battery. So, we are going to create a fake uevent and point i3status at it.

First, ~/.batt.sh:


#!/bin/bash

while true
do
paste /sys/class/power_supply/BAT0/uevent /sys/class/power_supply/BAT1/uevent | awk '{split($0,a,"="); split(a[2],b," "); (a[3] == "Charging" || b[1] == "Charging") ? $5 = "Charging" : $5 = (a[3] + b[1])/2; print a[1] "=" $5}' > .uevent
sleep 5
done

This pulls the information on your two batteries (BAT0 and BAT1) and pipes it into ~/.uevent. I have this set to run every five seconds (“sleep 5”) however you can change this depending how frequently you update your status bar.

You will want this script to run everytime you login, I simply added this to my ~/.i3/config file


exec ~/.batt.sh

Finally, point i3status at your new “battery” by adding this to ~/.i3status.conf (Substitute your username for <user> in the path!):


order += "battery 2"

battery 2 {
integer_battery_capacity = true
last_full_capacity = true
path = "/home/<user>/.uevent"
format = "%status %percentage %remaining"
}

Restart i3 with Mod + Shift + R and you should see your new “battery” pop up.

Selection_001 You may also want to remove the other batteries from your i3status.conf.

The script could probably use some more tweaking, and I may yet fork i3status, but for now this is working great!

Problems Installing SQL Server 2012 on Windows Server 2008 R2

I spent the better part of this afternoon chasing an installation failure. And it took a good bit of searching to find the answer, so here it is, at least in my case:

When installing SQL Server (2012 in my case, but from my reading it looks like this impacts other versions as well), the installation seems to go just fine, however you get an error:

“An external component has thrown an exception.”

The install logs don’t point to any specific error, or missing dependency.

At first I thought it was an install problem. Uninstall. Re-install. Same error.

Then maybe it was the install media? Grabbed a fresh copy. Uninstall. Re-Install. Same error.

Finally, after much trial and error I was able to track it down.

SQL Server installation will fail in this manner if you are running the Debug/Checked version of Windows.

Debug

So if you are having this problem, check to see what version you’re running.

Unfortunately there is no easier fix than a complete ground-up re-install of the server.

Hopefully this saves someone else all the hassle I had figuring it out.

NPOI vs Excel COM Interop Efficiency

I am working on some internal tools to automate some non-trivial data analysis work that my team does on a daily basis.

The data is packaged in Excel files (and comes from the tool that generates it as an old-school .xls binary file, to make things even more fun!). The files can run anywhere from a few hundred rows to a quarter of a million rows or more.

Currently we are using a combination of manual Excel analysis  (VLOOKUP, PivotTables, etc – real good time) and some existing Access Macros. Let’s just say there is a lot of room for optimization in this process.

So, as I’ve been creating the automation toolkit one of the first tasks is – of course – importing the data. I am using C# and figured “Hey, let’s use what Microsoft provides” and I jumped in with the Excel COM Interop. Woof.

Now, conceivably I just have no idea what I’m doing, but I found it to be extremely clunky, extremely resource intensive, and it has a really bad habit of orphaning a bunch of copies of Excel running in the background if anything hangs during data processing. Plus you have to have Excel installed to code against it – so it works great on my development workstation, not ideal for  server deployment.

Once I realized there were some serious headaches just waiting for me I started looking around for alternatives, and found NPOI. It has a few implementation quirks if you’re coming from the Interop. For me, the biggest change is that NPOI zero-indexes everything – getting row 10 means asking for row 9 – not difficult, but easy to introduce weird bugs when trying to select specific cells in a sheet.

But, little irritations like that mean nothing when you see the performance gains. I wired up some test code using NPOI and the first time I ran it I thought it must have crashed or failed – how could it have finished so quickly!?

How much of a change? Here’s one example I wrote up real quick:

NPOI vs Excel

For this 17,000+ line file NPOI was able to iterate over every row and print the value of the first cell in less than 7 seconds. It took Excel over 35 seconds to complete the same task.

Considering that this file is on the smaller end of what we do, those kinds of latency are simply unacceptable. Also, in my brief testing it seems that while NPOI scales pretty linearly, Excel does not – as the datasets grow, Excel gets exponentially slower.

Even running the same code as above without any output – simply iterating over each row and incrementing a counter – Excel is much slower. NPOI does this in less than 5 seconds, Excel takes almost 13.

So while NPOI is far from perfect, for what I’m doing right now it seems to be the best tool available.

And remember kids, friends don’t let friends use pre-2007 (.XLS) Excel files.

Look for another post on the differences in performance and scalability of parsing XLS vs XLSX in the near future.

For the test above I did the following: (Sorry, WordPress probably mangled the indentation.)

 Stopwatch sw = new Stopwatch();
 Stopwatch xlSw = new Stopwatch();
 int rowCounter = 0;
 int xlRowCounter = 0;

 sw.Start();

 using (var fs = File.OpenRead(&quot;test.xls&quot;))
 {
 IWorkbook wb = new HSSFWorkbook(fs);

 ISheet ws = wb.GetSheetAt(0);

 for (int row = 0; row &lt;= ws.LastRowNum; row++)
 {
 if (ws.GetRow(row) != null)
 {
 Console.WriteLine(ws.GetRow(row).GetCell(0).ToString());
 rowCounter++;
 }
 }
 }

 sw.Stop();

 xlSw.Start();

 Application xlApp = new Application();
 Workbook xlWb = xlApp.Workbooks.Open(Environment.CurrentDirectory + &quot;\\test.xls&quot;);
 Worksheet xlWs = xlWb.Sheets[1];

 Range aRange = xlWs.UsedRange;

 foreach (Range row in aRange.Rows)
 {
 Console.WriteLine(row.Cells[1,1].Value.ToString());
 xlRowCounter++;
 }

 xlWb.Close();
 xlApp.Quit();

 xlSw.Stop();

 Console.WriteLine(&quot;NPOI counted {0} rows in {1}&quot;, rowCounter, sw.Elapsed);
 Console.WriteLine(&quot;Excel counted {0} rows in {1}&quot;, xlRowCounter, xlSw.Elapsed);