Some Bits: Nelson's log

16 posts in April 2014

League of Legends packet loss

I play LoL half the time on a wired DSL line in San Francisco and it plays just fine. The other half the time I play on a fixed wireless network link in rural California and I get enough lag it’s not much fun to play. I’m trying to figure out if the source of that lag is my ISP’s wireless network or something else.

LoL has net logs which are remarkably helpful. The LoL client reports it’s losing about 300 packets in a 30 minute game, typically 1 or 2 every 10 seconds. And occasionally one bad lag burst, 20 lost packets in a 10 second frame. That’s consistent with what I see in the game, the occasional lag hiccup and then once or twice a game really bad lag. I’ve also poked around a fair amount into the UDP packet stream for the game to measure bandwidth, burst speeds, etc. It’s hard to diagnose without anything like TCP sequence numbers. But FWIW the game never seems to get much faster than 40,000 kbits/sec or so, well below my ISP’s honest speed of 1 mbit/sec.

I was guessing the link layer of the fixed wireless link is causing the packet loss. But my ISP has been great in helping me figure this out. They tell me they measure link packet loss and there isn’t any. Also the network equipment does have various retransmits etc to work around wireless flakiness. Another possibility is my router, or maybe even something in the Internet outside my ISP. Hard to know!

Along the way I’m trying to learn more about ARP caches. This documentation of the Linux ARP cache is helpful. So is the command “ip -s neighbor list” for inspecting the state of the ARP cache, although I don’t fully understand the output.

 

chosen, for Javascript selection

Chosen is neat; it’s a Javascript library that replaces the usual browser form select dropdown with a much more capable version. Check the demo. The only drawback is it requires jQuery or Prototype.

But not anymore! There’s a version that works natively; here’s a pull request for it based on this fork. I’ve built it and it seems to work. 30k JS + 10k CSS after minification. The jQuery version is only 26k JS, but then you also need 80k of jQuery to run it. So big win.

I had a heck of a time building this. Not because it’s complicated, it’s just a few files. But “native” in this case actually means “Coffeescript and Sass with a Grunt build and btw that implies a Ruby dependency”. Bah, kids these days. Here’s a copy of my build, the “public” directory, which contains the minified Javascript, CSS, and a demo that seems to work. I might have built this wrong so better to get it from official sources.

 

go on Mac makes static binaries!

I’ve complained a bunch about how MacOS doesn’t let you compile static binaries; you absolutely must dynamically link libc.

go doesn’t have that problem. go doesn’t use libc. The binaries “go build” creates appear to be truly statically linked, at least according to “otool -L”. Although apparently there’s another compiler “gccgo” that does link with glibc, or else I misunderstand.

Bonus fact: if you strip a Mac go binary, it segfaults when you run it.

Why does Apple hate Unix?

I upgraded an iMac to Mavericks, just so I could share binaries with the other Mac I have. Now I’m undoing the damage the upgrade did.

I have to download and reinstall the compiler. With the mysterious “xcode-select --install”. Because according to Apple, Unix computers don’t come with compilers. And if you have one they go in and delete it on upgrade. At least there’s a standalone command line compiler now, it’s better than having to get the whole Xcode IDE bloat.

I have to download and reinstall the X server. I don’t begrudge Apple not shipping X11 by default, it’s pretty awful and dated. But I have a fully working XQuartz on my system. Why delete it when I upgrade?

I’m starting to flirt with the idea of giving up on MacOS on my desktop. It’s bad at games, and it’s bad at Unix development. It is good at ordinary consumer use though, web browsers. I kind of wish I had a way to run Windows for games, ChromeOS for web browsing, and Linux for development. All on one machine with transparent interaction.

Bandwidth usage in a house

From my place up in Grass Valley, the one with the slow 768kbps link. Recently upgraded to a screamin’ 1024kbps, which makes a bigger difference than you’d think. (800kbps is apparently a sweet spot for online video.)

I’m averaging about 11GB download a month. That’s 33 hours of fully saturated link a month, or about 5% of total capacity. I’m surprised there’s an average of about 2GB upload too. The asymmetric upstream speed is terribly slow, so I work really hard to never rely on it. Then again a TCP ACK packet is about 3% of the size of the data packet it’s acknowledging, so that accounts for 0.3GB of upload just to match the downloads. (Roughly, details vary.)

 

Date Download Upload Total
2014-04 10.47 GB 1.17 GB 11.63 GB
2014-03 17.38 GB 1.55 GB 18.94 GB
2014-02 10.10 GB 1.32 GB 11.42 GB
2014-01 7.57 GB 1.11 GB 8.68 GB
2013-12 10.35 GB 1.71 GB 12.07 GB
2013-11 11.17 GB 1.55 GB 12.72 GB
2013-10 4.15 GB 0.59 GB 4.73 GB
2013-09 10.04 GB 1.41 GB 11.45 GB
2013-08 11.66 GB 1.39 GB 13.05 GB
2013-07 13.97 GB 2.15 GB 16.12 GB
2013-06 14.92 GB 2.49 GB 17.41 GB
2013-05 13.06 GB 1.96 GB 15.02 GB
2013-04 12.97 GB 1.50 GB 14.46 GB
2013-03 10.81 GB 1.21 GB 12.02 GB
2013-02 10.79 GB 1.35 GB 12.14 GB
2013-01 10.93 GB 1.71 GB 12.64 GB
2012-12 15.97 GB 2.01 GB 17.97 GB
2012-11 4.57 GB 0.58 GB 5.15 GB

MacOS binary incompatibility

I keep forgetting this; MacOS binaries are not compatible with earlier MacOS versions. I built a bunch of Homebrew stuff on 10.9 and copied the binaries to a 10.8 machine. And get awesome errors like

dyld: lazy symbol binding failed: Symbol not found: ___strlcpy_chk
Referenced from: /usr/homebrew/bin/rsync
Expected in: /usr/lib/libSystem.B.dylib

Some sort of library / linker design choice, I guess. It’s as baffling as Apple’s decision not to allow static linking, because the kernel API may change so you must dynamically link to libc to mediate.

Homebrew has a way to build with --universal, but I think that’s more for 32 bit compatibility than minor OS revisions.

 

Unison is slow

I like Unison for synchronizing files, but it is slow. I finally have a little insight into why; it’s calculating fingerprints for file contents. rsync seldom cares about file contents, it generally gets by with just metadata like file size and mtime. Unison’s approach seems to be fingerprint every file. At least it caches the fingerprints, it used to not!

The problem is the fingerprint algorithm is slow, slow, slow. Like it takes many seconds to fingerprint a 2 gigabyte file. I can’t find a paper that describes the algorithm and my OCaml is rusty, but the code makes mention of MD5 and the paper references “cryptographic checksum”. Using crypto hashes as generic hashes used to be a fad, I’m guilty of it myself, but unless you’re worried about cryptographic adversaries it’s always better to use a fast CRC or the like instead of a crypto hash. Crypto hashes are slow.

While I’m complaining I can’t help but think being written in OCaml is part of why Unison doesn’t get more attention and development now. Not many folks know ML, you know? It also doesn’t help with the speed. I can’t tell if there’s an ML version of whatever fingerprint hash it’s calculating or if it’s using a native optimized library. Also the whole thing is single threaded, which for an app doing slow CPU work on many independent files is a mess. OCaml apparently doesn’t support concurrency :-(

Tomato on the ASUS RT-AC66U

I thought I had the router thing all figured out and then I decided I wanted 5GHz wifi so I bought an ASUS RT-AC66U. At $180 ASUS still isn’t managing to ship working firmware, although it is better than it used to be. Seems to be based on a reskinned Tomato version that’s not entirely awful. I did have a few problems though; unreasonable sluggishness if I’m running BitTorrent, and the DNS server seemed to simply block occasionally until some unrelated TCP download on my network completed. Stupid.

I thought I’d try something new so I went with the Merlin’s Tower firmware. It’s a lightly patched stock ASUS firmware, fixing a few bugs and adding a few features. It kind of worked, but I still had the sluggishness and DNS problems and it made me sad. For some reason I’ve never liked DD-WRT. So today I’m going back to my favorite, Tomato.

Except Tomato is a mess. The original release is no longer updated, without any notice on the site. Tomato-USB is also abandoned, as is Toastman’s versions. The best bet seems to be Tomato Shibby. Don’t be put off by the Polish language and awful site design, the guy’s serious and is doing a good job on firmware development.

In the end I found this guide to firmware updating helpful, along with these instructions for using tftp instead of ASUS’ proprietary firmware flasher. Once I found the right firmware version it mostly worked. Although man it takes a long time to boot, like 60+ seconds.

Configured more or less like the last one. One bug in the Shibby firmware; first time it boots it doesn’t recognize the 5GHz wireless adapter. You have to reboot it once. Details in this video.

Had to choose yet another DDNS service. I used dyndns for years, sad they finally left the valuable free market. Hover still has no DDNS support either. And couldn’t figure out how to use ASUS’ own service from Tomato. I used this guide (possibly spam) to find my way to freedns.afraid.org which is kind of neat and has thousands of hostnames to choose from. I settled on us.to. There’s a bit of magic where you have to grab the “token” out of freedns’ wget script to configure Tomato.

I should give up on this consumer stuff entirely and buy a serious router. At $180 for the ASUS gear it’s not exactly a bargain. Next project is to add a second access point upstairs, fortunately I have lots of ethernet drops.

d3.js: using CSV for numbers

D3 has two main ways to load data; JSON files or CSV files. JSON is generally better but bloated for long repeated rows of data, for which CSV is better because it only prints the key names once. But CSV has a drawback which is that it’s untyped. So D3 just lamely loads everything as strings. Most of the time Javascript’s automatic string to float conversion lets you ignore that but it fails in some places, like sorting with d3.ascending.

Fortunately D3’s CSV library lets you create an accessor function which manipulates the data as you load it. Here’s a function which will load every row and try to convert things to numbers. I wrote it in very verbose Javascript to make it clear what’s going on. I don’t care about the number of lines, but I do wonder if this is less efficient than possible.

// convert strings to numbers in CSV
function convertNumbers(row) {
  var r = {};
  for (var k in row) {
    r[k] = +row[k];
    if (isNaN(r[k])) {
      r[k] = row[k];
    }
  }
  return r;
}

d3.csv("data.csv", convertNumbers, function(error, inputData) {
  // do work on the data here
}

Note that the accessor function has to create a new object; you can’t just return the one the accessor passes you.

Homebrew observations

You can’t move a Homebrew binary directory. I installed Homebrew in /usr/homebrew2 and then renamed that to /usr/homebrew and a bunch of stuff broke because it had the path /usr/homebrew2 baked into it. bash-completion, also python.

Many Homebrew packages don’t compile cleanly. I ran into three: mg, unison, and ggobi. I filed bugs and got prompt responses (and a patch for ggobi in 12 minutes!), which is pretty great. OTOH it feels to me like Homebrew doesn’t have any sort of automated build-and-test setup for packages, these were all really simple bugs that anyone installing the code would hit. Some basic integration testing would go a long way.

I’m very grateful to Homebrew but the whole project feels pretty ad hoc and brittle to me. No surprise since it started out being such a simple thing. And simple is good! But I think at this point it’s evolved into needing to be a serious package manager, but hasn’t yet evolved all the serious package management. I wish we’d just use dpkg on MacOS instead. Fink is exactly that, but my experiences with it have been bad.

Starting anew with a clean homebrew install. Note I’ve already set PATH and BREW to use /usr/homebrew, the non-standard location

sudo mkdir /usr/homebrew
sudo chown nelson /usr/homebrew
cd /usr/homebrew

git clone https://github.com/Homebrew/homebrew.git
mv homebrew/* homebrew/.git* .
rmdir homebrew

brew tap homebrew/dupes
brew install atk autoconf automake bash-completion cairo clens cloog faac ffmpeg fontconfig freetype freexl gdal gdbm gdk-pixbuf geos gettext gfortran ggobi giflib git glib gmp gobject-introspection gpp graphicsmagick grc gtk+ harfbuzz icu4c iftop isl jpeg jq json-c lame less libffi libgeotiff liblwgeom libmpc libpng libspatialite libtiff libtool libxml2 lzlib makedepend mp4v2 mpfr nmap node objective-caml openssl optipng ossp-uuid p7zip pango pgdbf pixman pkg-config postgis postgresql proj pv python readline rsync sqlite texi2html unrar wget x264 xvid xz yasm

brew install mg
brew install unison

Battlefield 4 graphs

Some quick data views, looking at player statistics from the game Battlefield 4. I’m scraping stats from a site that lets me get a bunch of numbers for players, statistics like “Kill/Death Ratio” and “Score per Minute” and the like. I collected a bucket of stats for the top 1000 players (by rounds completed) for all 5 platforms, then graphed various variables. Honestly I haven’t found a very interesting story in this data yet, the main thing I’ve learned is that time played is not really correlated with any measure of skill. Ie: no one learns to play better. But more to consider.

Scatter plot is for two variables (lableled on axes), histogram below is the x axis.

Kill/Death Ratio vs. Time Played Win/Loss Ratio vs Kill/Death Ratio

Interactive data exploration

I’m exploring a multivariate data set. Battlefield 4 data, to be exact, I have a dataset for thousands of players with such statistics as “time played”, “kill/death ratio”, “win/loss ratio”, etc. Fun stuff. Now I’m exploring it looking for intresting patterns, clusters, etc. And doing it by hand-writing Javascript code with D3.

I wish there were a solid consensus tool for exploring datasets and showing graphics. Excel and R seem to be the two most popular. But Excel is too primitive and R is too much like programming already.

 

Wanted: browser based page scraping

Doing yet another HTML scraping project, contemplating the slowness and desolation that is BeautifulSoup or spend hours learning scrapy or surely there’s something better by now?

There is, my web browser, with DOM CSS queries. Just load the page and do querySelector and you’re done. Most modern HTML is quite nicely scrapable in browser Javascript. The problem is you can’t effectively script a browser to process thousands of pages. I’d hoped node.js would offer a solution but they don’t have some battle-hardened HTML parser like a browser has. There are some options, wonder if any are worth the time to learn about.

 

Chrome extension gripes

Working along on my Chrome extension, generally impressed with how well thought out and thoroughly documented the extension support is. That being said, some wrinkles...

There’s no support for reloading extension code when it’s changed during development. There’s some hacks, I’m using Extensions Reloader which gives me a button to press to reload the Javascript code. But it won’t reload the manifest, so it’s not a complete solution. And even then I have to hit the extension reload the button then refresh the page to debug stuff, it’s awkward.

Chrome has adopted an awkward API for signaling errors in the extension API, the variable chrome.runtime.lastError is set. This makes checking for errors a huge nuisance, but maybe it’s the only way to do this given the weird lifecycle of extensions? Good thing Javascript is single threaded :-P. It’s a shame Javascript’s built in exceptions are not very useful. I like the D3 pattern of setting an error object on the callback function, at least it makes the error variable a bit more explicit.

The Chrome extension storage API is fairly capable, I particularly like that it lets you store data in a place that Chrome synchronizes between the user’s browsers. But the API is just enough different from DOM localStorage to be a bit obnoxious. Also the get() method is incredibly slow, like 500ms to retrieve my 10 bytes of state. That means I can’t just store stuff there and fetch it from my content script, too slow for something whose purpose is to modify a page’s presentation. So I have to create a background page and move the config fetching there, an unwelcome complication. Update: it’s not always so slow. Sometimes it’s only 40ms. Sometimes its 200ms. I’m running a bunch of other extensions, should test it more cleanly.

 

Dog food

I found the perfect way to encourage myself to work on this Chrome extension. Install a version whose UI is so eye-bleedingly awful I can’t help but fix it.

Screen Shot 2014-04-08 at 9.07.47 AM

My first Chrome Extension

I wrote a Chrome Extension today. Or the start of one. Want to modify the content of some pages on Metafilter in order to try out a UI idea. I started with the diediedead userscript, that runs just fine by itself if you manually load it. But I want a proper Chrome extension so I can make it configurable (and easily usable) so I read up on how to build Chrome extensions. It’s easy, at least for this easy thing I’ve done so far, although there’s a lot more complexity for a more complex extension.

The one hassle is Chrome doesn’t auto-reload extensions under development. It does reload userscripts though, weird. Anyway the Chrome Extensions Reloader Extension helps, at least there’s a single button to press.