Waking up your PC over the Internet

To save power, particularly during the hot summer months, I’ve wanted to be able to wake up home PCs remotely.  I tend to use my home PCs to troubleshoot network connectivity issues while at work but other than that there’s really no need for the PCs to be on during the day.

Wake-on-LAN (WOL) has been around since the 90s and provides a way to wake up a PC remotely.

What does it take to make it work?

  1. WOL must be supported and enabled in the BIOS.
  2. WOL must be supported and enabled on the Network Interface Card (Device Manager –> Network Adapter properties –> Advanced).
  3. Power Management must be enabled for the NIC (Device Manager –> Network Adapter properties –> Power Management though this may vary from driver to driver).
  4. A program that will send the magic-packet.  The Wikipedia entry for Wake-on-LAN links to several programs that will do this.
  5. Forwarding a UDP port through the firewall to the LAN broadcast address.  In my case I forwarded UDP port 9 to 192.168.0.255.

On Windows there are other ways to remotely wake up a PC.  Devices that support the “Network Device Class Power Management Reference Specification”, a Microsoft standard described here, defines 2 additional methods: a network wake-up frame and detection of a change in the network link state.  The NIC has to support pattern-match based WOL to take advantage of the Network Wake-Up Frame method.

Both the magic packet or the Network Wake-Up Frame can be delivered in 3 ways: NetBIOS broadcast, ARP resolution and Unicast.

Since we intend to do this remotely we can rule out NetBIOS broadcast and ARP because the PCs are behind a firewall whose protection we want.  That leaves Unicast.  Windows registers the MAC address with the NIC driver as a pattern.  An IP packet with the MAC address and IP of the NIC will therefore serve as a Network Wake-Up Frame and bring the PC out of Sleep.

Since the PCs are behind a firewall 1 or more ports will need to be forwarded.  On my D-LINK 855-DIR router forwarding port 9 to the broadcast address for the internal network (192.168.0.255 in my case) does the trick!

Debugging with Immediate Mode

Sometimes you want a quick way to start stepping through some code that you suspect has an error.

Depending on where the code lives simply getting to it can be a pain.  A function in a service that depends on a bunch of other libraries can be non-trivial to execute in an isolated context.  The 20mins or so of setup, over time, starts to add up.

In a web or client-server context getting to the code of interest may be even more convoluted.  It may require a client to step through several pages before triggering the request that results in execution of the code.  Put that code inside a web service and oy, we’re talking 20-30mins just to start stepping through a function.

Visual Studio’s immediate mode can execute functions in the context of the current project.  CTRL+D,W to open up the Immediate window then ?int temp=SomeStaticFunc(3);

results in the value of temp being printed out.  If the function isn’t static you’ll need to have some other function that creates the desired instance (let’s call it p).  Then in the Immediate window ?int temp=p.SomeInstanceMethod(3); does the trick.

Of course there are other ways to accomplish the same result but Immediate mode is another tool in the toolset for quickly getting into a function of interest.

Reservations and Commitments

Most programs need memory to do their work.  Since programs don’t typically know where in memory they’re going to be loaded programs work with a virtual address space.

It’s virtual in the sense that it doesn’t correspond to any particular hardware implementation of memory.  Neither address lines nor flip-flops, the stuff of RAM that you buy in a store, spring into existence when your program refers to a location in memory.

Under the hood the operating system keeps track of where a virtual address is stored in physical memory.  On Windows the data structure that stores this mapping is called the page map.  Virtual and physical memory are divided up into equal sized blocks called pages (on x86 these pages are 4k long).

As programs start and stop, read and write and go about their business these pages are recycled (how conscientious of them!).

At any given moment a Virtual Address Space Page can be Free, Reserved or Committed.  The importance of distinguishing these states often appears when dealing with Big Data (e.g. high resolution imagery, video, audio, large volumes of text).

In many ways virtual pages are like tables at a restaurant.  Before a restaurant opens the tables may well be in a closet somewhere.  They’re free to be used but customers can’t use them just yet.  Trying to use these tables, folded and unprepared as they are, is likely to be an unpleasant experience liable to draw the ire of the restaurant owner.

Even before the tables are set and arranged a restaurant can take reservations for it.  Once reserved it can’t be reserved for someone else (unless the reservation is cancelled).  On the other hand, even though it’s reserved it’s not yet in use and might well still be folded away in a closet (since the restaurant hasn’t opened yet).

The committed state is analogous to a table that’s been placed and prepared.  It now takes up space in the building.  The customer can use the table for whatever purposes are allowed by the restaurant.