Sunday, January 24, 2016

ESP8266 Wifi With Arduino Uno and Nano

If you are trying to add Wifi connectivity to an existing Arduino project or have serious aspirations for developing a Internet of Things (IoT) solution, Arduino + ESP8266 wifi module is one  of the top choices. Especially the Nano because it is super cheap (<$3) and is very small in size. Using some sort of web-server directly on ESP8266 (e.g. via Lua) doesn't cut it due to the lack of IO pins on ESP8266. You can get a full IoT node out at under $12 with a few sensors, Arduino Nano and a ESP9266 module (excluding the power supply).

Inspite of a plethora of posts online it turned out to be very hard for me to get this to combination to work. I spent atleast 3-4 days until I actually got this right. The main problem I see is that a lot of the solutions online are actually down-right incorrect, not-recommended or for other similar boards (e.g. Arduino Mega). Also there are a few gotchas that were not commonly called out. Before I start let me get all of those out of the way

  1. Arduino Uno/Nano is very different from say Mega which can supply more current and have different number of UART. The steps to make a Uno and Nano work is different from them.
  2. Power Supply
    1. ESP8266 is powered by 3.3V and NOT 5V. So you cannot have a common power supply between Arduino and ESP8266
    2. ESP8266 draws way more current (200mA) then it can be supplied by the 3.3v pin on the Uno/Nano. Don’t even try them, I don't buy anyone who claims to have done this. Maybe they have some other high power variant of Arduino (Mega??) that can do this.
    3. So you either use a 3.3v 1A power supply to ESP8266 with common ground with the 5V powering Arduino, or you use a step down 5v to 3.3v (e.g. like here).
  3. Arduino <-> ESP8266
    1. All the ESP8266 I bought  came with the UART serial IO speed (BAUD) set to 115200. Now the problem is that Uno/Nano has only one HW serial, which is set to be used for communicating with the PC over USB with which you are debugging. You can use any other two IO pins to talk to the ESP8266 using SoftwareSerial, but it does not support that high a BAUD speed. If you try 115200 to communicate with Arduino <-> ESP8266 you will get garbage. A lot of articles online show a setup with Arduino Mega which does have two HW serial IO using which you can easily get 115200 and more. So you need to dial the ESP8266 settings to move the communication speed to a more manageable BAUD of 9600
    2. Arduino IO pins have 5V and ESP8266 accepts 3.3 v (max 3.6). I have seen people directly connect the pins but you are over driving the ESP8266. If it doesn’t burn out immediately (the cheaper ones does), it will burn out soon. I suggest you use a voltage divider using simple resistor to have Arduino transmission (TX) drive ESP8266 receive (RX)
    3. For some strange reason D2/D3 pins on Arduino Nano didn’t work for me for the communicating with ESP8266. I have no explanation for this and it happened on two separate Nano. The Arduino would just read a whole bunch of garbage character. So I had to move to the pins 8/9.
    4. In spite of whatever I did, garbage characters would still come in sometimes. So I wrote a small filter code to ignore them

 

Things you need

  1. ESP8266
  2. Arduino Nano
  3. Power supply 5v and 3.3v
  4. Resistors 1K, 2.2K, 10K
  5. FTDI USB to serial TTL adapter. Link (optional, see below)

Setting up ESP8266

imageAs mentioned above I first set the ESP8266 BAUD rate to 9600. If yours is already 9600 then nothing to be done, if not you need to make the following connection

PC (USB) <-> FTDI <-> ESP8266

Then using specific AT commands from the PC set the 9600 BAUD rate on the ESP8266. I used the following circuit. Where the connections are as follows

FTDI TX –> Via voltage divider (to move 5v to ~3.3v) to ESP8266 RX (blue wire)
FTDI RX –> Directly to ESP8266 TX (green wire). A 3.3v on Nano I/0 pin will be considered as 1.
FTDI GND to common ground (black)

ESP8266 GND to common GND (black)
ESP8266 VCC to 3.3v (red)
ESP8266 CH_PD to 3.3v via a 10K  resistor (red)

Power supply GND to common GND

PC to FTDI USB.

One that is set bring up Arduino IDE and do the following using the menu

  1. Tools –> Port –>COM{n}. For me it was COM6
  2. Then Tools –> Serial monitor

In the serial monitor ensure you have the following set correctly. The BAUD should match the preset BAUD of your ESP8266. If you are not sure, use 115200 and type the command AT. If should return OK, if not try changing the BAUD, until you get that.

image

Then change the BAUD rate by using the following command, and you should get OK back

AT+CIOBAUD=9600

After that immediately change the BAUD rate in the serial monitor to be 9600 baud as well and issue a AT command. You should see OK. You are all set for the ESP8266.

Setting up Arduino Nano + ESP8266

This step should work for Uno as well. Essentially make the same circuit as above, but now instead of FTDI use an Arduino. I used pins 8 and 9 on Arduino for the RX and TX respectively.

image

 

Debugging and Setup WIFI

Even though I could easily run AT commands with the PC <->FTDI <-> ESP8266, I ran into various issues while doing the same programmatically in PC <->Arduino <-> ESP8266 setup. So I wrote the following very simple code to pass on commands I typed in the PC via the Arduino to the ESP8266 and reverse for outputs.

The code is at GitHub as https://github.com/bonggeek/Samples/blob/master/Arduino/SerialRepeater.ino

#include <SoftwareSerial.h>
SoftwareSerial softSerial(8, 9); // RX, TX

void setup() 
{
  uint32_t baud = 9600;
  Serial.begin(baud);
  softSerial.begin(baud);
  Serial.print("SETUP!! @");
  Serial.println(baud);
}

void loop() 
{
    while(softSerial.available() > 0) 
    {
      char a = softSerial.read();
      if(a == '\0')
        continue;
      if(a != '\r' && a != '\n' && (a < 32))
        continue;
      Serial.print(a);
    }
    
    while(Serial.available() > 0)
    {
      char a = Serial.read();
      Serial.write(a);
      softSerial.write(a);
    }
}

With this code built and uploaded to Arduino I launched the Serial monitor on my PC. After that I could type commands in my Serial Monitor and have the Arduino pass that only ESP8266 and read back the response. I can still see some junk chars coming back (in RED). All commands are in Green and could easily enumerate all Wifi in range using AT+CWLAP and even connect to my Wifi.

image

Wednesday, December 23, 2015

Publishing a ASP.NET 5 Web-Application to IIS Locally



I ran into few issues and discovered some kinks in publishing the new ASP.NET 5 Web-Application to an Internet Information Services (IIS) on the local box and then accessing it from other devices on the same network.

While there may be a number of different ways of doing this, the following worked for me.

Visual Studio

After you have create a new Project using File > New Project > ASP.NET Web Application

image

Change the build to use x64 and not ANY CPU

image

Now Right click on the project and choose publish. We will use File System publishing to push the output to a folder location and then get IIS to load it

image

Publish target is inside default IIS web root folder. This might be different for your setup.

clip_image001

Use 64 bit release in settings
clip_image002

Finally publish it

clip_image003

So with this step done your web application is now published to c:\inetpub\wwwroot\HomeServer

IIS

Now launch the IIS Manager by hitting Win key and searching for IIS Manager

Right click on default web-site and use Add Application.

clip_image001[7]

Create and point the application to the published app. Note that this is not the top level c:\inetpub\wwwroot\HomeServer, but rather the wwwroot folder inside it. This is required because the web.config is inside that folder. So we use c:\inetpub\wwwroot\HomeServer\wwwroot

clip_image002[7]

Hit, OK to create the web-app and then restart the web-site

clip_image003[7]

Now browse to the web-site, which in my case is http://localhost/HomeServer

clip_image004

Accessing from local network

To access the same website from other devices on the same network you need to enable access through the firewall. Search and select (Win key and type) “Allow an App Through Windows Firewall” then in the Control panel window that opens (Control Panel\System and Security\Windows Firewall\Allowed apps), click the “Change Settings” button and then check “World Wide Web Services”

image

Find the local servers IP by running the ipconfig command in command shell. Then you can reach this from other devices on the same network.

Screen shot of accessing the web-site from my cell phone connected to the same network over wifi.

clip_image006

Wednesday, November 18, 2015

Dual Booting Ubuntu and Windows 10

Even though I hit a ton of online resources outlining the details, I struggled a lot getting this to work. So in this blog I am outlining what worked for me.

My setup was a office laptop (Lenovo X1 Carbon) with Trusted Platform Module (TPM), secure boot and Bit-locker enabled. My goal is to dual boot Ubuntu 15.10 with Windows 10.

Setup the installer USB

Get a USB thumb drive and format it as FAT32. NOTE: Do NOT use NTFS, otherwise the installation will completely fail with Kernel Panic

image

I downloaded my Ubuntu 15.10 from http://www.ubuntu.com/desktop. Then I downloaded the Universal USB Installer from http://www.pendrivelinux.com/universal-usb-installer-easy-as-1-2-3/. I used the following setting to create the installer pen drive

image

Setup your PC for Dual Boot

If you do not have secure boot (UEFI or bitlocker) life is simple. But if you are reading this blog, then definitely you are not in that boat.

First of all disable boot-locker before making any changes into startup settings. Just type Bitlocker in the search box and choose Manage Bitlocker. In that turn it off.

image

Also ensure you have a empty partition to install Ubuntu onto. If you do not you need to partition your disk. There are various freeware and even Windows Disk Management can do that for you. I have the following D drive that I intended to install Ubuntu onto

Capture

Disable Secure Boot

From WinKey+R run the command

shutdown.exe /r /o /f /t 00

Windows will restart with the following screen, choose Troubleshoot

20151112_200310

In the next, advanced options screen choose UEFI Firmware settings

20151112_200348

The use the restart button

20151112_200423

This will bring up the BIOS window. It will differ for different PC/Laptop, but in any case you need to disable various secure boot options. In my Lenovo Thinkpad, I followed the following screen.

Go to Security –> Secure Boot and Disable Secure Boot

20151112_201347

20151112_201409

Then in startup enable legacy boot.

20151112_202504

Save the changes and exit (F10) and boot into windows

Installing Ubuntu

Now in Windows run the same command as before

shutdown.exe /r /o /f /t 00

This will bring up the following options, choose “Use a Device”

20151113_180925

In that use “Boot Menu”

20151113_180946

If you have done everything right the machine will restart and offer a boot menu with your USB thumb drive listed as an option.

20151113_181034

Selecting the bootable thumb drive should launch Ubuntu installer. Choose Install Ubuntu to start installing. Follow through the installer wizard.

20151113_181101

Once you have installed Ubuntu and dual boot is set, you can boot into Windows and then enable Bit-locker.

Thursday, October 22, 2015

How to add a breakpoint in a managed generic method in windbg (sos)

Milkyway over Mt. Rainier

This is not really a blog post but a micro-post. Someone asked me and since I couldn’t find any post out there calling it out, thought I’d add

If you want to add a breakpoint to a managed method inside windbg using sos extension, the obvious way is to use the extension command !bpmd. However, if the target method is generic or inside a generic type it is slightly tricky, you don’t use <T> but rather `<count of generic types>

So I have a the following inside by foo.exe managed app

namespace Abhinaba
{
public class PriorityThreadPool<t> : IDisposable
{
public bool RunTask(T param, Action<t> action)
{
// cool stuff
}
}
}

To set breakpoint in it I use the following (notice the red highlighted part)

!bpmd ApplicationHost.exe Xap.ApplicationHost.PriorityThreadPool`1.RunTask

Thursday, October 15, 2015

Backlight and detergent

imageI found a cheap $6 black light (or rather UV) flashlight on Amazon. Once it arrived, me and my daughter went out on the quest of finding what glows best in it. Basically some sort of phosphor that absorbs the UV light and emits visible light making it seem to glow in the dark.

Based on a quick bing.com  search we tried a bunch of stuff, but what worked best is laundry detergent. I knew that to make clothes seem bright these have phosphors in them. The indirect proof was how bright a newly laundered T-shirt was glowing. Until I popped open one of the Costco laundry detergent pods and put it under UV light I never could’ve guessed the intense glow they emitted.

Enjoy a video of that.

Wednesday, April 01, 2015

List of Modules loaded

While working on the .NET Loader and now in Bing where I am right now working on some features around module loading I frequently need to know and filter on the list of modules (dll/exe) loaded in a process or on the whole system. There are many ways to do that like use GUI tools like Process Explorer (https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) or even attach a debugger and get the list of loaded modules. But those to me seems either cumbersome (GUI) or intrusive (debugger). So I have written a small command line tool. It’s native and less than 100kb in size. You can get the source on GitHub at https://github.com/bonggeek/Samples/tree/master/ListModule or the binary at http://1drv.ms/1NAzkvy.

The usage is simple. To see the modules loaded in all processes with the name note in it. You just use the following

F:\GitHub\Samples\ListModule>listmodule note
Searching for note in 150 processes

\Device\HarddiskVolume2\Program Files\Microsoft Office 15\root\office15\ONENOTEM.EXE (8896)
========================================================
(0x00DB0000) C:\Program Files\Microsoft Office 15\root\office15\ONENOTEM.EXE
(0xCBEF0000) C:\windows\SYSTEM32\ntdll.dll
(0x776D0000) C:\windows\SYSTEM32\wow64.dll
...

\Device\HarddiskVolume2\Program Files\Microsoft Office 15\root\office15\onenote.exe (12192)
========================================================
(0x01340000) C:\Program Files\Microsoft Office 15\root\office15\ONENOTE.EXE
(0xCBEF0000) C:\windows\SYSTEM32\ntdll.dll
...

\Device\HarddiskVolume2\Windows\System32\notepad.exe (19680)
========================================================
(0xF64A0000) C:\windows\system32\notepad.exe
(0xCBEF0000) C:\windows\SYSTEM32\ntdll.dll
(0xCB7D0000) C:\windows\system32\KERNEL32.DLL
...


The code uses Win32 APIs to get the info. This is a quick tool I wrote, so if you find any bugs, send it my way.

Wednesday, November 05, 2014

.NET RyuJIT Rocks

IMG_0070_stitch.jpg

As the CLR team announced a few days back, CTP for the new fast JIT from .NET (code named RyuJIT) is out. Go check out the announcement at http://blogs.msdn.com/b/clrcodegeneration/archive/2014/10/31/ryujit-ctp5-getting-closer-to-shipping-and-with-better-simd-support.aspx. In the post they say

Recently the Bing team has tried using RyuJIT on top of 4.5.1 in some of their processing, and they see a 25% reduction in startup time in their scenario.  This is the most significant real-world throughput win we have witnessed on RyuJIT thus far.

Being from that “Bing Team” let me just say that RyuJIT just blew our expectation. Some of our workloads run incredibly large managed loads (many 1000s of assemblies running million of methods). We saw dramatic drop in startup time as well as subsequent JIT time. And also may I add that we are actually using the CTP bits to serve live traffic. It’s been pretty stable for us.

The graph shows the startup times averaged over all machines.

image

Friday, October 31, 2014

Halloween Costume with Arduino

This Halloween me and my daughter decided to add some dazzle to her fairy costume. Since we were anyway learning to code on Arduino we decided to dip our hands in wearables.

The basic idea is to build a costume that glows when someone comes close. The project was intended to teach a 9 year old to code and is hence simple enough for her to grasp. We used the following

Parts

  1. Arduino UNO board
  2. TIP120 transistor
  3. Diode 1N4004
  4. 1K Resistor
  5. HC-SR04 Ultrasonic Range Finder

Circuit

It’s best to consider the circuit as two separate pieces. One to acquire the distance of someone approaching using the HC-SR04 ultrasound range finder. The second is to actually make the LED strip glow.

The first part consists of connecting the 4 pins of the HC-SR04 as follows

image

We cannot simply drive the LED strip using an output pin of Arduino because the strip drains way more current than that can be supplied by the Arduino chip. So we use a TIP120 or TIP121 chip as shown below

image

There is a nice explanation of this whole setup at http://www.instructables.com/id/Use-Arduino-with-TIP120-transistor-to-control-moto/. The same principles hold, but instead of a fan we use a LED strip in our case.

Code

The entire code is available on GitHub at https://github.com/bonggeek/GlowDress/ (I cleaned up the code a tiny bit after my daughter wrote it). This is how it looks

#include <ultrasonicranging.h>

#define ECHO_PIN 2 // ECHO pin of HC-SR04
#define TRIG_PIN 3 // Trigger pin of HC-SR04
#define LED_OUT 5 // Drive LED (Base pin of TIP120

const int space = 125; // Distance in cm in which to trigger LED

void setup()
{
Serial.begin (9600);
pinMode(TRIG_PIN, OUTPUT); // trigger pin of US range finder
pinMode(ECHO_PIN, INPUT); // Echo pin of US range finder
pinMode(LED_OUT, OUTPUT); // base of TIP120 to drive LED

analogWrite(LED_OUT, 0);
}

void GlowLed()
{
// Slowly get from LED strip off to full bright (glow-in)
for (int brightness = 0; brightness < 255; brightness++)
{
analogWrite(LED_OUT, brightness);
delay(3);
}

// Slowly get from LED strip on to full off (glow-out)
for (int brightness = 255; brightness >= 0; brightness--)
{
analogWrite(LED_OUT, brightness);
delay(3);
}
}

void loop()
{
int distance = GetDistanceInCm(TRIG_PIN, ECHO_PIN);
Serial.println(distance);

if (distance <= 0 || distance > space)
{
analogWrite(LED_OUT, 0);
delay(500);
return;
}

if (distance <= space)
{
GlowLed();
}
}

Here to abstract away the intricacies of how distance is received from the ranger, I have used GetDistanceInCm. The source for this library is at  https://github.com/bonggeek/GlowDress/tree/master/UltraSonicRanging.


Once we tested out the circuit we went ahead and soldered it on a board. My daughter did receive a battle scar (a small burn from iron) but we battled on.

IMG_0565This is how it looks partially done 

IMG_0574


With my wife’s help we sewed it underneath her fairy dress. It was pretty well concealed other than the sensor sticking out a bit.


IMG_0578


Tuesday, September 30, 2014

.NET Just in Time Compilation and Warming up Your System

IMG_7823.jpg

One of the primary obstacle we face while scaling our system is Just In Time (JIT) compilation of the .NET stack. We run a .NET managed application server at a huge scale with many thousands of managed assemblies on each server (and many many thousands of those servers distributed globally).We deploy code daily and since those are managed code they are JITed at each deployment. Our system is very sensitive to latency and we do not want those servers getting new code to cost us execution latency. So we use various mechanisms to warm the servers before they start serving queries. Common techniques we use are

  1. NGEN
  2. Force JITing (a specialized multi-core JIT technique see bottom of post)
  3. Sending warmer queries that warm up the system

In this effort I frequently handle questions regarding how the system JITs managed methods. Even after so many years of the CLR JIT existing there seems to be confusion around when JIT happens, what is the unit of compilation. So I thought I’d make a quick post on this topic.

Consider the following simple code I have in One.cs

using System;

namespace Foo
{
class MyClass
{
public void a()
{
int i = 0;
while(true)
{
b(i++);
}
}

public void b(int i)
{
Console.WriteLine(i);
}
}

class Program
{
static void Main()
{
MyClass mc = new MyClass();
mc.a();
}
}
}


Of interest is the function Foo.MyClass.a and Foo.MyClass.b. We will debug to find out exactly when the later is JITed.



First I compile and then launch the debugger. I will use the windbg debugger and the sos extensions extensively in this post. Also read https://support2.microsoft.com/kb/319037?wa=wsignin1.0 to see how to setup symbol servers to debug into the CLR.

csc /debug+ One.cs
windbg one.exe


After that in windbg I run the following command to setup

.sympath+ d:\MySymbols          ;$$ Use the Microsoft symbol server (see link above)
sxe ld:clr ;$$ break on CLR loaded
g ;$$ continue the program until you break on CLR.dll being loaded
.loadby sos clr ;$$ load the sos debugger extension

!bpmd One.exe Foo.MyClass.a ;$$ Set a managed break point in a()
g ;$$ continue until break point in a() is hit



When this break point is hit, we have obviously already JITed MyClass.a() and executing it. The question we now have is that whether all the functions a() calls like MyClass.b() already JITed. If not when/how will that be JITed. Lets debug it!!



**Color coding indicates how I take output of one command to give inputs to the next one.



First lets find the this pointer for the MyClass instance. This can be obtained from the current managed call stack

0:000> !clrstack -a
PARAMETERS:
this (0x0000000000d9eea0) = 0x0000000002922c58



The details of the this object shows the MethodTable for it. The MethodTable has pointer to EEClass (cold data).

0:000> !do 0x0000000002922c58
Name: Foo.MyClass
MethodTable: 00007ffab2f640d8
EEClass: 00007ffab3072340
Size: 24(0x18) bytes
File: d:\Skydrive\Code\C#\_JITPresentation\One.exe
Fields:


Now we can see more details of the MethodTable, which will show the individual methods descriptors.

0:000> !dumpmt -md 00007ffab2f640d8
EEClass: 00007ffab3072340
Module: 00007ffab2f62fc8
Name: Foo.MyClass
mdToken: 0000000002000002
File: d:\Skydrive\Code\C#\_JITPresentation\One.exe
BaseSize: 0x18
ComponentSize: 0x0
Slots in VTable: 7
Number of IFaces in IFaceMap: 0
--------------------------------------
MethodDesc Table
Entry MethodDesc JIT Name
00007ffb07c16300 00007ffb077c80e8 PreJIT System.Object.ToString()
00007ffb07c5e760 00007ffb077c80f0 PreJIT System.Object.Equals(System.Object)
00007ffb07c61ad0 00007ffb077c8118 PreJIT System.Object.GetHashCode()
00007ffb07c5eb50 00007ffb077c8130 PreJIT System.Object.Finalize()
00007ffab3080120 00007ffab2f640d0 JIT Foo.MyClass..ctor()
00007ffab3080170 00007ffab2f640b0 JIT Foo.MyClass.a()
00007ffab2f6c050 00007ffab2f640c0 NONE Foo.MyClass.b(Int32)


The type has 7 methods. Also the out clearly indicates that Foo.MyClass.a() is JITed and Foo.MyClass.b() is NONE (or not JITed). We can get more details about these methods

0:000> !dumpmd 00007ffab2f640b0
Method Name: Foo.MyClass.a()
Class: 00007ffab3072340
MethodTable: 00007ffab2f640d8
mdToken: 0000000006000001
Module: 00007ffab2f62fc8
IsJitted: yes
CodeAddr: 00007ffab3080170 <----- JITed
Transparency: Critical
i. 0:000> !dumpmd 00007ffab2f640c0
Method Name: Foo.MyClass.b(Int32)
Class: 00007ffab3072340
MethodTable: 00007ffab2f640d8
mdToken: 0000000006000002
Module: 00007ffab2f62fc8
IsJitted: no
CodeAddr: ffffffffffffffff <----- Not yet JITed


So at this point we know that a() is JITed but the method b() it calls is not. In that the question arises that if it is not what is the content of the native instructions for a() and what does that code call into for b(). The disassembly will clearly show that the entire method a() is JITed and that for outward managed calls there are calls to stubs

0:000> u 00007ffab3080170 L24
One!Foo.MyClass.a() [d:\Skydrive\Code\C#\_JITPresentation\One.cs @ 8]:
00007ffa`b3080170 48894c2408 mov qword ptr [rsp+8],rcx
00007ffa`b3080175 4883ec38 sub rsp,38h
00007ffa`b3080179 c744242000000000 mov dword ptr [rsp+20h],0
00007ffa`b3080181 c644242400 mov byte ptr [rsp+24h],0
00007ffa`b3080186 48b83834f6b2fa7f0000 mov rax,7FFAB2F63438h
00007ffa`b3080190 8b00 mov eax,dword ptr [rax]
00007ffa`b3080192 85c0 test eax,eax
00007ffa`b3080194 7405 je One!Foo.MyClass.a()+0x2b (00007ffa`b308019b)
00007ffa`b3080196 e82574b25f call clr!JIT_DbgIsJustMyCode (00007ffb`12ba75c0)
00007ffa`b308019b 90 nop
00007ffa`b308019c c744242000000000 mov dword ptr [rsp+20h],0
00007ffa`b30801a4 eb23 jmp One!Foo.MyClass.a()+0x59 (00007ffa`b30801c9)
00007ffa`b30801a6 90 nop
00007ffa`b30801a7 8b4c2420 mov ecx,dword ptr [rsp+20h]
00007ffa`b30801ab ffc1 inc ecx
00007ffa`b30801ad 8b442420 mov eax,dword ptr [rsp+20h]
00007ffa`b30801b1 89442428 mov dword ptr [rsp+28h],eax
00007ffa`b30801b5 894c2420 mov dword ptr [rsp+20h],ecx
00007ffa`b30801b9 8b542428 mov edx,dword ptr [rsp+28h]
00007ffa`b30801bd 488b4c2440 mov rcx,qword ptr [rsp+40h]
00007ffa`b30801c2 e889beeeff call Foo.MyClass.b(Int32) (00007ffa`b2f6c050)
00007ffa`b30801c7 90 nop
00007ffa`b30801c8 90 nop

00007ffa`b30801c9 c644242401 mov byte ptr [rsp+24h],1
00007ffa`b30801ce ebd6 jmp One!Foo.MyClass.a()+0x36 (00007ffa`b30801a6)
00007ffa`b30801d0 90 nop
00007ffa`b30801d1 4883c438 add rsp,38h
00007ffa`b30801d5 c3 ret
00007ffa`b30801d6 0000 add byte ptr [rax],al
00007ffa`b30801d8 1909 sbb dword ptr [rcx],ecx
00007ffa`b30801da 0100 add dword ptr [rax],eax
00007ffa`b30801dc 096200 or dword ptr [rdx],esp


So we see that for b a call is made to the memory location 00007ffa`b2f6c050. We can see what is there now by disassembling that address.

0:000> u 00007ffa`b2f6c050
Foo.MyClass.b(Int32):
00007ffa`b2f6c050 e87b5e755f call clr!PrecodeFixupThunk (00007ffb`126c1ed0)
00007ffa`b2f6c055 5e pop rsi
00007ffa`b2f6c056 0201 add al,byte ptr [rcx]


So basically instead of real native JITed code existing for b() there is actually a stub or thunk in it’s place. So we clearly establish that when a function is called it’s entire code is JITed and other method it calls is not yet JITed (however, there are caveats like inline methods etc). Now we can now go and set a breakpoint inside JIT to break when it tries it JIT the b() method. This is what we do

0:000> bp clr!UnsafeJitFunction ;$$ entry point for JITing a method
0:000> g ;$$ continue executing until we hit the UnsafeJITFunction
0:000> k ;$$ dump the stack for JITing
clr!UnsafeJitFunction
clr!MethodDesc::MakeJitWorker
clr!MethodDesc::DoPrestub
clr!PreStubWorker+0x3d6
clr!ThePreStub+0x5a [f:\dd\ndp\clr\src\vm\amd64\ThePreStubAMD64.asm @ 92]
One!Foo.MyClass.a()+0x57 [d:\Skydrive\Code\C#\_JITPresentation\One.cs @ 12]


As we can see that the JITing actually happened in the same call thread that is executing a() and exactly when b was called. ThePreStub finally calls the JITer. The JITer will actually JIT the method b() and backtrack the stack and patch up the call, so that it will actually now be a call straight to the JITed copy of b(). We hit g couple of times and now see what happens for the MethodDescriptor for b()

0:000> !dumpmd 00007ffab2f640c0
Method Name: Foo.MyClass.b(Int32)
Class: 00007ffab3072340
MethodTable: 00007ffab2f640d8
mdToken: 0000000006000002
Module: 00007ffab2f62fc8
IsJitted: yes
CodeAddr: 00007ffab30801f0 <-- Now it is JITed
Transparency: Critical


As we see b() is now JITed and we can see it’s disassembly as well. However, more interesting, lets go back and see what the disassembly of a() now contains

0:000> u 00007ffab3080170 L24
One!Foo.MyClass.a() [d:\Skydrive\Code\C#\_JITPresentation\One.cs @ 8]:
00007ffa`b3080170 48894c2408 mov qword ptr [rsp+8],rcx
00007ffa`b3080175 4883ec38 sub rsp,38h
00007ffa`b3080179 c744242000000000 mov dword ptr [rsp+20h],0
00007ffa`b3080181 c644242400 mov byte ptr [rsp+24h],0
00007ffa`b3080186 48b83834f6b2fa7f0000 mov rax,7FFAB2F63438h
00007ffa`b3080190 8b00 mov eax,dword ptr [rax]
00007ffa`b3080192 85c0 test eax,eax
00007ffa`b3080194 7405 je One!Foo.MyClass.a()+0x2b (00007ffa`b308019b)
00007ffa`b3080196 e82574b25f call clr!JIT_DbgIsJustMyCode (00007ffb`12ba75c0)
00007ffa`b308019b 90 nop
00007ffa`b308019c c744242000000000 mov dword ptr [rsp+20h],0
00007ffa`b30801a4 eb23 jmp One!Foo.MyClass.a()+0x59 (00007ffa`b30801c9)
00007ffa`b30801a6 90 nop
00007ffa`b30801a7 8b4c2420 mov ecx,dword ptr [rsp+20h]
00007ffa`b30801ab ffc1 inc ecx
00007ffa`b30801ad 8b442420 mov eax,dword ptr [rsp+20h]
00007ffa`b30801b1 89442428 mov dword ptr [rsp+28h],eax
00007ffa`b30801b5 894c2420 mov dword ptr [rsp+20h],ecx
00007ffa`b30801b9 8b542428 mov edx,dword ptr [rsp+28h]
00007ffa`b30801bd 488b4c2440 mov rcx,qword ptr [rsp+40h]
00007ffa`b30801c2 e889beeeff call Foo.MyClass.b(Int32) (00007ffa`b2f6c050)
00007ffa`b30801c7 90 nop
00007ffa`b30801c8 90 nop
00007ffa`b30801c9 c644242401 mov byte ptr [rsp+24h],1
00007ffa`b30801ce ebd6 jmp One!Foo.MyClass.a()+0x36 (00007ffa`b30801a6)
00007ffa`b30801d0 90 nop
00007ffa`b30801d1 4883c438 add rsp,38h
00007ffa`b30801d5 c3 ret
00007ffa`b30801d6 0000 add byte ptr [rax],al
00007ffa`b30801d8 1909 sbb dword ptr [rcx],ecx
00007ffa`b30801da 0100 add dword ptr [rax],eax
00007ffa`b30801dc 096200 or dword ptr [rdx],esp
00007ffa`b30801df 005600 add byte ptr [rsi],dl


Now if we re-disassemble the target of this call at 00007ffa`b2f6c050

0:000> u 00007ffa`b2f6c050
Foo.MyClass.b(Int32):
00007ffa`b2f6c050 e99b411100 jmp One!Foo.MyClass.b(Int32) (00007ffa`b30801f0)
00007ffa`b2f6c055 5f pop rdi
00007ffa`b2f6c056 0201 add al,byte ptr [rcx]


As you can see the address has been patched up and now b() is JITed and a() calls into b() without going through any stubs.



Obviously in this example I took a bunch of assumptions, but hopefully you now have the understanding to go debug your own scenarios and see what is at play. Some of the takeaways if you have JIT issues at startup



JIT happens at method granularity



Use modular code. Especially if you have error handling and other code which is rarely or almost never used, instead of having them in the main function, move them out. This will ensure that they are never JITed or at best not JITed at startup

void Foo()
{
try{
//...
}
catch(Exception ex)
{
ComplexErrorHandling(ex);
}
}


is better than

void Foo()
{
try{
//...
}
catch(Exception ex)
{
LogLocal();
UploadToSomeServer();
// MoreCode;
// EvenMoreCode;
}
}


For a given function running it once, JITs the whole function. However, do note that if it has difference code branches and each calls other functions you will need to execute all branches. In the case below Foo has to be called with both true and false to ensure downstream methods are JITed

void Foo(bool flag)
{
if(flag)
YesFlag();
else
NoFlag();
}


JITing happens in the same thread as the calls. The JIT engine does takes lock to ensure there is no races while JITing the same method from multiple threads.



Consider using NGEN, MultiCoreJIT or ForceJIT all methods you care about or even build your own mechanism based on the following code



Here’s some PseudoCode to force JIT which accomplishes that using RuntimeHelpers.PrepareMethod API (note this code does no error handling whatsoever). You can craft code around this to ForceJIT only assemblies and/or types in them that is causing JIT bottlenecks. Also this can be parallelized across cores. The .NET Multicore JIT is based on similar principle but automatically does it by generating a profile of what executes at your application startup and then JITing it for you in the next go.

using System;
using System.Reflection;
namespace ConsoleApplication5
{
class Program
{
static private void ForceJit(Assembly assembly)
{
var types = assembly.GetTypes();

foreach (Type type in types)
{
var ctors = type.GetConstructors(BindingFlags.NonPublic
| BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.Static);

foreach (var ctor in ctors)
{
JitMethod(assembly, ctor);
}

var methods = type.GetMethods(BindingFlags.DeclaredOnly
| BindingFlags.NonPublic
| BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.Static);

foreach (var method in methods)
{
JitMethod(assembly, method);
}
}
}

static private void JitMethod(Assembly assembly, MethodBase method)
{
if (method.IsAbstract || method.ContainsGenericParameters)
{
return;
}

System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle);
}

static void Main(string[] args)
{
ForceJit(Assembly.LoadFile(@"d:\Scratch\asm.dll"));
}
}
}

Thursday, September 18, 2014

SuppressIldasmAttribute – The Insanity

Meteors and sky Wish Poosh Campground, Cle Elum Lake, WA

We use ildasm in our build deployment pipeline. Recently one internal partner pinged me saying that it was failing with a weird message that ildasm is failing to disassemble one particular assembly. I instantly assumed it to be a race condition (locks taken on the file, some sort of anti-virus holding read locks, etc). However, he reported back it is a persistent problem. I asked for the assembly and tried to run

ildasm foo.dll

I was greeted with


image


Dumbfounded I dug around and found this weird attribute on this assembly

[assembly: SuppressIldasmAttribute] 

MSDN points out http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.suppressildasmattribute(v=vs.110).aspx that this attribute is to make ildasm not disassembly a given assembly. For the life of me I cannot fathom why someone invented this attribute. This is one of those things which is so surreal…. Obviously you can simply point reflector or any of the gazillion other disassemblers to this assembly and they be happy to oblige. False sense of security is worse than lack of security, I’d recommend not to use this attribute.

Wednesday, August 13, 2014

Hiking Summerland and the Pan Handle Gap

Length: 12.0 miles, roundtrip
Elevation Gain: 2950 ft.
Highest Point: 6800 ft
Link: http://www.wta.org/go-hiking/hikes/panhandle-gap

It’s been 4 years I have been hiking around, but never really kept a log of those hikes. However, on this hike while discussing with some of the folks I suddenly realized I have been forgetting details of some of the past hikes. So I thought I’ll start making short blogs on the hikes/climbs I go on.

I went on this hike with the good people on /meetup.com. It was after a long break because these days I am mostly hiking with my daughter Prokriti and also Somtapa joins us frequently. Both of them are in India for the next 6 weeks. We all met at the Renton park and ride (close to Walmart) and finally reached and started from the trailhead around 10:00 a.m.IMG_7633

This hike starts from the road to the Sunrise visitor center of Mt. Rainier. A few miles before sunrise the trail starts from the bridge over the Frying pan creek.

image

Initially the hike is kind of boring and like any other forest cover hikes. Also kind of flat.

imageThis continues until the first 3 miles where the trail only gains a meager 1200ft. The rest of the 1800 is gained in the next 2 miles. At the 3rd mile there is a scary bridge over a fast and furiously flowing creek. When I climbed the same route 2 years back with Prokriti who was just 7 at that time, it was really really scary. This time around the bridge seemed to be recently repaired and in good condition.

IMG_7612

IMG_7620_PanoAfter this bridge the trail went into relentless switchbacks and reached Summerland at 5900 ft. This part falls on the Wonderland trail that goes around the whole of Mt. Rainier. Summerland is one of the most beautiful meadows I have come across. It has a nice campground as well.

IMG_7706-Edit

IMG_7865

IMG_7849After sitting around here for some time and having food we continued towards the Panhandle gap. Within a quarter of a mile the landscape changed dramatically. It seemed we were instantly transported to a different world of volcanic rocks. The immense powers of the glaciers were evident all around.

IMG_7720

IMG_7847_stitch

IMG_7825-Edit

However, we did not have much time to enjoy the views. The sun was hot and we still had a long way to go. We marched on. Unfortunately I soon discovered that I was getting really low on electrolytes. I started getting cramps and muscle twitch. The heat was getting to me. I also discovered my pack of Nuun electrolyte tabs had fallen off. Thankfully my teammates had some coconut water and Gatorade to share (bless their souls). Replenished I marched on. Finally reached the pan handle gap at 6800ft elevation.

The views were gorgeous. As our lead Ryan said “this is why we do it, if someone doesn’t love this view they should give up hiking”. Also why it’s called a gap was very evident.

IMG_7777_stitch-Edit

IMG_7787_stitch

IMG_7799_stitch

After a long break I headed down to position myself below the Fryingpan Glacier so that I could photograph the other braver souls as they glissaded down.

IMG_7812

IMG_7823

It was a long march home. The 6 miles of tread back seemed never ending!!!