Archive for the ‘monitoring’ Category
memprof: A Ruby level memory profiler

If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.
What is memprof and why do I care?
memprof is a Ruby gem which supplies memory profiler functionality similar to bleak_house without patching the Ruby VM. You just install the gem, call a function or two, and off you go.
Where do I get it?
memprof is available on gemcutter, so you can just:
gem install memprof
Feel free to browse the source code at: http://github.com/ice799/memprof.
How do I use it?
Using memprof is simple. Before we look at some examples, let me explain more precisely what memprof is measuring.
memprof is measuring the number of objects created and not destroyed during a segment of Ruby code. The ideal use case for memprof is to show you where objects that do not get destroyed are being created:
- Objects are created and not destroyed when you create new classes. This is a good thing.
- Sometimes garbage objects sit around until
garbage_collecthas had a chance to run. These objects will go away. - Yet in other cases you might be holding a reference to a large chain of objects without knowing it. Until you remove this reference, the entire chain of objects will remain in memory taking up space.
memprof will show objects created in all cases listed above.
OK, now Let’s take a look at two examples and their output.
A simple program with an obvious memory “leak”:
require 'memprof'
@blah = Hash.new([])
Memprof.start
100.times {
@blah[1] << "aaaaa"
}
1000.times {
@blah[2] << "bbbbb"
}
Memprof.stats
Memprof.stop
This program creates 1100 objects which are not destroyed during the start and stop sections of the file because references are held for each object created.
Let's look at the output from memprof:
1000 test.rb:11:String
100 test.rb:7:String
In this example memprof shows the 1100 created, broken up by file, line number, and type.
Let's take a look at another example:
require 'memprof' Memprof.start require "stringio" StringIO.new Memprof.stats
This simple program is measuring the number of objects created when requiring stringio.
Let's take a look at the output:
108 /custom/ree/lib/ruby/1.8/x86_64-linux/stringio.so:0:__node__
14 test2.rb:3:String
2 /custom/ree/lib/ruby/1.8/x86_64-linux/stringio.so:0:Class
1 test2.rb:4:StringIO
1 test2.rb:4:String
1 test2.rb:3:Array
1 /custom/ree/lib/ruby/1.8/x86_64-linux/stringio.so:0:Enumerable
This output shows an internal Ruby interpreter type __node__ was created (these represent code), as well as a few Strings and other objects. Some of these objects are just garbage objects which haven't had a chance to be recycled yet.
What if nudge the garbage_collector along a little bit just for our example? Let's add the following two lines of code to our previous example:
GC.start Memprof.stats
We're now nudging the garbage collector and outputting memprof stats information again. This should show fewer objects, as the garbage collector will recycle some of the garbage objects:
108 /custom/ree/lib/ruby/1.8/x86_64-linux/stringio.so:0:__node__
2 test2.rb:3:String
2 /custom/ree/lib/ruby/1.8/x86_64-linux/stringio.so:0:Class
1 /custom/ree/lib/ruby/1.8/x86_64-linux/stringio.so:0:Enumerable
As you can see above, a few Strings and other objects went away after the garbage collector ran.
Which Rubies and systems are supported?
- Only unstripped binaries are supported. To determine if your Ruby binary is stripped, simply run:
file `which ruby`. If it is, consult your package manager's documentation. Most Linux distributions offer a package with an unstripped Ruby binary. - Only x86_64 is supported at this time. Hopefully, I'll have time to add support for i386/i686 in the immediate future.
- Linux Ruby Enterprise Edition (1.8.6 and 1.8.7) is supported.
- Linux MRI Ruby 1.8.6 and 1.8.7 built with --disable-shared are supported. Support for --enable-shared binaries is coming soon.
- Snow Leopard support is experimental at this time.
- Ruby 1.9 support coming soon.
How does it work?
If you've been reading my blog over the last week or so, you'd have noticed two previous blog posts (here and here) that describe some tricks I came up with for modifying a running binary image in memory.
memprof is a combination of all those tricks and other hacks to allow memory profiling in Ruby without the need for custom patches to the Ruby VM. You simply require the gem and off you go.
memprof works by inserting trampolines on object allocation and deallocation routines. It gathers metadata about the objects and outputs this information when the stats method is called.
What else is planned?
Myself, Jake Douglas, and Aman Gupta have lots of interesting ideas for new features. We don't want to ruin the surprise, but stay tuned. More cool stuff coming really soon :)
Thanks for reading and don't forget to subscribe (via RSS or e-mail) and follow me on twitter.
Rewrite your Ruby VM at runtime to hot patch useful features

If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.
Some notes before the blood starts flowin’
- CAUTION: What you are about to read is dangerous, non-portable, and (in most cases) stupid.
- The code and article below refer only to the x86_64 architecture.
- Grab some gauze. This is going to get ugly.
TLDR
This article shows off a Ruby gem which has the power to overwrite a Ruby binary in memory while it is running to allow your code to execute in place of internal VM functions. This is useful if you’d like to hook all object allocation functions to build a memory profiler.
This gem is on GitHub
Yes, it’s on GitHub: http://github.com/ice799/memprof.
I want a memory profiler for Ruby
This whole science experiment started during RubyConf when Aman and I began brainstorming ways to build a memory profiling tool for Ruby.
The big problem in our minds was that for most tools we’d have to include patches to the Ruby VM. That process is long and somewhat difficult, so I started thinking about ways to do this without modifying the Ruby source code itself.
The memory profiler is NOT DONE just yet. I thought that the hack I wrote to let us build something without modifying Ruby source code was interesting enough that it warranted a blog post. So let’s get rolling.
What is a trampoline?
Let’s pretend you have 2 functions: functionA() and functionB(). Let’s assume that functionA() calls functionB().
Now also imagine that you’d like to insert a piece of code to execute in between the call to functionB(). You can imagine inserting a piece of code that diverts execution elsewhere, creating a flow: functionA() –> functionC() –> functionB()
You can accomplish this by inserting a trampoline.
A trampoline is a piece of code that program execution jumps into and then bounces out of and on to somewhere else1.
This hack relies on the use of multiple trampolines. We’ll see why shortly.
Two different kinds of trampolines
There are two different kinds of trampolines that I considered while writing this hack, let’s take a closer look at both.
Caller-side trampoline
A caller-side trampoline works by overwriting the opcodes in the .text segment of the program in the calling function causing it to call a different function at runtime.
The big pros of this method are:
- You aren’t overwriting any code, only the address operand of a
callqinstruction. - Since you are only changing an operand, you can hook any function. You don’t need to build custom trampolines for each function.
This method also has some big cons too:
- You’ll need to scan the entire binary in memory and find and overwrite all address operands of
callq. This is problematic because if you overwrite any false-positives you might break your application. - You have to deal with the implications of
callq, which can be painful as we’ll see soon.
Callee-side trampoline
A callee-side trampoline works by overwriting the opcodes in the .text segment of the program in the called function, causing it to call another function immediately
The big pro of this method is:
- You only need to overwrite code in one place and don’t need to worry about accidentally scribbling on bytes that you didn’t mean to.
this method has some big cons too:
- You’ll need to carefully construct your trampoline code to only overwrite as little of the function as possible (or some how restore opcodes), especially if you expect the original function to work as expected later.
- You’ll need to special case each trampoline you build for different optimization levels of the binary you are hooking into.
I went with a caller-side trampoline because I wanted to ensure that I can hook any function and not have to worry about different Ruby binaries causing problems when they are compiled with different optimization levels.
The stage 1 trampoline
To insert my trampolines I needed to insert some binary into the process and then overwrite callq instructions like this:
41150b: e8 cc 4e 02 00 callq 4363dc [rb_newobj] 411510: 48 89 45 f8 ....
In the above code snippet, the byte e8 is the callq opcode and the bytes cc 4e 02 00 are the distance to rb_newobj from the address of the next instruction, 0×411510
All I need to do is change the 4 bytes following e8 to equal the displacement between the next instruction, 0×411510 in this case, and my trampoline.
Problem.
My first cut at this code lead me to an important realization: the callq instructions used expect a 32bit displacement from the function I am calling and not absolute addresses. But, the 64bit address space is very large. The displacement between the code for the Ruby binary that lives in the .text segment is so far away from my Ruby gem that the displacement cannot be represented with only 32bits.
So what now?
Well, luckily mmap has a flag MAP_32BIT which maps a page in the first 2GB of the address space. If I map some code there, it should be well within the range of values whose displacement I can represent in 32bits.
So, why not map a second trampoline to that page which can contains code that can call an absolute address?
My stage 1 trampoline code looks something like this:
/* the struct below is just a sequence of bytes which represent the
* following bit of assembly code, including 3 nops for padding:
*
* mov $address, %rbx
* callq *%rbx
* ret
* nop
* nop
* nop
*/
struct tramp_tbl_entry ent = {
.mov = {'\x48','\xbb'},
.addr = (long long)&error_tramp,
.callq = {'\xff','\xd3'},
.ret = '\xc3',
.pad = {'\x90','\x90','\x90'},
};
tramp_table = mmap(NULL, 4096, PROT_WRITE|PROT_READ|PROT_EXEC,
MAP_32BIT|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (tramp_table != MAP_FAILED) {
for (; i < 4096/sizeof(struct tramp_tbl_entry); i ++ ) {
memcpy(tramp_table + i, &ent, sizeof(struct tramp_tbl_entry));
}
}
}
It mmaps a single page and writes a table of default trampolines (like a jump table) that all call an error trampoline by default. When a new trampoline is inserted, I just go to that entry in the table and insert the address that should be called.
To get around the displacement challenge described above, the addresses I insert into the stage 1 trampoline table are addresses for stage 2 trampolines.
The stage 2 trampoline
Setting up the stage 2 trampolines are pretty simple once the stage 1 trampoline table has been written to memory. All that needs to be done is update the address field in a free stage 1 trampoline to be the address of my stage 2 trampoline. These trampolines are written in C and live in my Ruby gem.
static void
insert_tramp(char *trampee, void *tramp) {
void *trampee_addr = find_symbol(trampee);
int entry = tramp_size;
tramp_table[tramp_size].addr = (long long)tramp;
tramp_size++;
update_image(entry, trampee_addr);
}
An example of a stage 2 trampoline for rb_newobj might be:
static VALUE
newobj_tramp() {
/* print the ruby source and line number where the allocation is occuring */
printf("source = %s, line = %d\n", ruby_sourcefile, ruby_sourceline);
/* call newobj like normal so the ruby app can continue */
return rb_newobj();
}
Programatically rewriting the Ruby binary in memory
Overwriting the Ruby binary to cause my stage 1 trampolines to get hit is pretty simple, too. I can just scan the .text segment of the binary looking for bytes which look like callq instructions. Then, I can sanity check by reading the next 4 bytes which should be the displacement to the original function. Doing that sanity check should prevent false positives.
static void
update_image(int entry, void *trampee_addr) {
char *byte = text_segment;
size_t count = 0;
int fn_addr = 0;
void *aligned_addr = NULL;
/* check each byte in the .text segment */
for(; count < text_segment_len; count++) {
/* if it looks like a callq instruction... */
if (*byte == '\xe8') {
/* the next 4 bytes SHOULD BE the original displacement */
fn_addr = *(int *)(byte+1);
/* do a sanity check to make sure the next few bytes are an accurate displacement.
* this helps to eliminate false positives.
*/
if (trampee_addr - (void *)(byte+5) == fn_addr) {
aligned_addr = (void*)(((long)byte+1)&~(0xffff));
/* mark the page in the .text segment as writable so it can be modified */
mprotect(aligned_addr, (void *)byte+1 - aligned_addr + 10,
PROT_READ|PROT_WRITE|PROT_EXEC);
/* calculate the new displacement and write it */
*(int *)(byte+1) = (uint32_t)((void *)(tramp_table + entry)
- (void *)(byte + 5));
/* disallow writing to this page of the .text segment again */
mprotect(aligned_addr, (((void *)byte+1) - aligned_addr) + 10,
PROT_READ|PROT_EXEC);
}
}
byte++;
}
}
Sample output
After requiring my ruby gem and running a test script which creates lots of objects, I see this output:
... source = test.rb, line = 8 source = test.rb, line = 8 source = test.rb, line = 8 source = test.rb, line = 8 source = test.rb, line = 8 source = test.rb, line = 8 source = test.rb, line = 8 ...
Showing the file name and line number for each object getting allocated. That should be a strong enough primitive to build a Ruby memory profiler without requiring end users to build a custom version of Ruby. It should also be possible to re-implement bleak_house by using this gem (and maybe another trick or two).
Awesome.
Conclusion
- One step closer to building a memory profiler without requiring end users to find and use patches floating around the internet.
- It is unclear whether cheap tricks like this are useful or harmful, but they are fun to write.
- If you understand how your system works at an intimate level, nearly anything is possible. The work required to make it happen might be difficult though.
Thanks for reading and don't forget to subscribe (via RSS or e-mail) and follow me on twitter.
References
Extending ltrace to make your Ruby/Python/Perl/PHP apps faster

If you enjoy this article, subscribe (via RSS or e-mail) and follow me on twitter.
A few days ago, Aman (@tmm1) was complaining to me about a slow running process:
I want to see what is happening in userland and trace calls to extensions. Why doesn’t ltrace work for Ruby processes? I want to figure out which MySQL queries are causing my app to be slow.
It turns out that ltrace did not have support for libraries loaded with libdl. This is a problem for languages like Ruby, Python, PHP, Perl, and others because in many cases extensions, libraries, and plugins for these languages are loaded by the VM using libdl. This means that ltrace is somewhat useless for tracking down performance issues in dynamic languages.
A couple late nights of hacking and I managed to finagle libdl support in ltrace. Since most people probably don’t care about the technical details of how it was implemented, I’ll start with showing how to use the patch I wrote and what sort of output you can expect. This patch has made tracking down slow queries (among other things) really easy and I hope others will find this useful.
How to use ltrace:
After you’ve applied my patch (below) and rebuilt ltrace, let’s say you’d like to trace MySQL queries and have ltrace tell you when the query was executed and how long it took. There are two steps:
- Give ltrace info so it can pretty print – echo “int mysql_real_query(addr,string,ulong);” > custom.conf
- Tell ltrace you want to hear about
mysql_real_query:ltrace -F custom.conf -ttTgx mysql_real_query -p <pid>
Here’s what those arguments mean:
- -F use a custom config file when pretty-printing (default: /etc/ltrace.conf, add your stuff there to avoid -F if you wish).
- -tt print the time (including microseconds) when the call was executed
- -T time the call and print how long it took
- -x tells ltrace the name of the function you care about
- -g avoid placing breakpoints on all library calls except the ones you specify with -x. This is optional, but it makes ltrace produce much less output and is a lot easier to read if you only care about your one function.
PHP
Test script
mysql_connect("localhost", "root");
while(true){
mysql_query("SELECT sleep(1)");
}
ltrace output
22:31:50.507523 zend_hash_find(0x025dc3a0, "mysql_query", 12) = 0 <0.000029> 22:31:50.507781 mysql_real_query(0x027bc540, "SELECT sleep(1)", 15) = 0 <1.000600> 22:31:51.508531 zend_hash_find(0x025dc3a0, "mysql_query", 12) = 0 <0.000025> 22:31:51.508675 mysql_real_query(0x027bc540, "SELECT sleep(1)", 15) = 0 <1.000926>
ltrace command
ltrace -ttTg -x zend_hash_find -x mysql_real_query -p [pid of script above]
Python
Test script
import MySQLdb
db = MySQLdb.connect("localhost", "root", "", "test")
cursor = db.cursor()
sql = """SELECT sleep(1)"""
while True:
cursor.execute(sql)
data = cursor.fetchone()
db.close()
ltrace output
22:24:39.104786 PyEval_SaveThread() = 0x21222e0 <0.000029> 22:24:39.105020 PyEval_SaveThread() = 0x21222e0 <0.000024> 22:24:39.105210 PyEval_SaveThread() = 0x21222e0 <0.000024> 22:24:39.105303 mysql_real_query(0x021d01d0, "SELECT sleep(1)", 15) = 0 <1.002083> 22:24:40.107553 PyEval_SaveThread() = 0x21222e0 <0.000026> 22:24:40.107713 PyEval_SaveThread()= 0x21222e0 <0.000024> 22:24:40.107909 PyEval_SaveThread() = 0x21222e0 <0.000025> 22:24:40.108013 mysql_real_query(0x021d01d0, "SELECT sleep(1)", 15) = 0 <1.001821>
ltrace command
ltrace -ttTg -x PyEval_SaveThread -x mysql_real_query -p [pid of script above]
Perl
Test script
#!/usr/bin/perl
use DBI;
$dsn = "DBI:mysql:database=test;host=localhost";
$dbh = DBI->connect($dsn, "root", "");
$drh = DBI->install_driver("mysql");
@databases = DBI->data_sources("mysql");
$sth = $dbh->prepare("SELECT SLEEP(1)");
while (1) {
$sth->execute;
}
ltrace output
22:42:11.194073 Perl_push_scope(0x01bd3010) =<0.000028> 22:42:11.194299 mysql_real_query(0x01bfbf40, "SELECT SLEEP(1)", 15) = 0 <1.000876> 22:42:12.195302 Perl_push_scope(0x01bd3010) = <0.000024> 22:42:12.195408 mysql_real_query(0x01bfbf40, "SELECT SLEEP(1)", 15) = 0 <1.000967>
ltrace command
ltrace -ttTg -x mysql_real_query -x Perl_push_scope -p [pid of script above]
Ruby
Test script
require 'rubygems'
require 'sequel'
DB = Sequel.connect('mysql://root@localhost/test')
while true
p DB['select sleep(1)'].select.first
GC.start
end
snip of ltrace output
22:10:00.195814 garbage_collect() = 0 <0.022194> 22:10:00.218438 mysql_real_query(0x02740000, "select sleep(1)", 15) = 0 <1.001100> 22:10:01.219884 garbage_collect() = 0 <0.021401> 22:10:01.241679 mysql_real_query(0x02740000, "select sleep(1)", 15) = 0 <1.000812>
ltrace command used:
ltrace -ttTg -x garbage_collect -x mysql_real_query -p [pid of script above]
Where to get it
- On github: http://github.com/ice799/ltrace/tree/libdl
- Raw patch (NOTE: This should apply cleanly against ltrace 0.5.3): ltrace.patch
How ltrace works normally
ltrace works by setting software breakpoints on entries in a process’ Procedure Linkage Table (PLT).
What is a software breakpoint
A software breakpoint is just a series of bytes (0xcc on the x86 and x86_64) that raise a debug interrupt (interrupt 3 on the x86 and x86_64). When interrupt 3 is raised, the CPU executes a handler installed by the kernel. The kernel then sends a signal to the process that generated the interrupt. (Want to know more about how signals and interrupts work? Check out an earlier blog post: here)
What is a PLT and how does it work?
A PLT is a table of absolute addresses to functions. It is used because the link editor doesn’t know where functions in shared objects will be located. Instead, a table is created so that the program and the dynamic linker can work together to find and execute functions in shared objects. I’ve simplified the explanation a bit1, but at a high level:
- Program calls a function in a shared object, the link editor makes sure that the program jumps to a slot in the PLT.
- The program sets some data up for the dynamic linker and then hands control over to it.
- The dynamic linker looks at the info set up by the program and fills in the absolute address of the function that was called in the PLT.
- Then the dynamic linker calls the function.
- Subsequent calls to the same function jump to the same slot in the PLT, but every time after the first call the absolute address is already in the PLT (because when the dynamic linker is invoked the first time, it fills in the absolute address in the PLT).
Since all calls to library functions occur via the PLT, ltrace sets breakpoints on each PLT entry in a program.
Why ltrace didn’t work with libdl loaded libraries
Libraries loaded with libdl are loaded at run time and functions (and other symbols) are accessed by querying the dynamic linker (by calling dlsym()). The compiler and link editor don’t know anything about libraries loaded this way (they may not even exist!) and as such no PLT entries are created for them.
Since no PLT entries exist, ltrace can’t trace these functions.
What needed to be done to make ltrace libdl-aware
OK, so we understand the problem. ltrace only sets breakpoints on PLT entries and libdl loaded libraries don’t have PLT entries. How can this be fixed?
Luckily, the dynamic linker and ELF all work together to save your ass.
Executable and Linking Format (ELF) is a file format for executables, shared libraries, and more2. The file format can get a bit complicated, but all you really need to know is: ELF consists of different sections which hold different types of entries. There is a section called .dynamic which has an entry named DT_DEBUG. This entry stores the address of a debugging structure in the address space of the process. In Linux, this struct has type struct r_debug.
How to use struct r_debug to win the game
The debug structure is updated by the dynamic linker at runtime to reflect the current state of shared object loading. The structure contains 3 things that will help us in our quest:
- state – the current state of the mapping change taking place (begin add, begin delete, consistent)
- brk – the address of a function internal to the dynamic linker that will be called when the linker maps, unmaps, or has completed mapping a shared object.
- link map – Pointer to the start of a list of currently loaded objects. This list is called the link map and is represented as a
struct link_mapin Linux.
Tie it all together and bring it home
To add support for libdl loaded libraries to ltrace, the steps are:
- Find the address of the debug structure in the
.dynamicsection of the program. - Set a software breakpoint on
brk. - When the dynamic linker updates the link map, it will trigger the software breakpoint.
- When the breakpoint is triggered, check
statein the debug structure. - If a new library has been added, walk the link map and figure out what was added.
- Search the added library’s symbol table for the symbols we care about.
- Set a software breakpoints on whatever is found.
- Steps 3-8 repeat.
That isn’t too hard all thanks to the dynamic linker providing a way for us to hook into its internal events.
Conclusion
- Read the System V ABI for your CPU. It is filled with insanely useful information that can help you be a better programmer.
- Use the source. A few times while hacking on this patch I looked through the source for GDB and glibc to help me figure out what was going on.
- Understanding how things work at a low-level can help you build tools to solve your high-level problems.
Thanks for reading and don’t forget to subscribe (via RSS or e-mail) and follow me on twitter.
References
Useful kernel and driver performance tweaks for your Linux server

This article is going to address some kernel and driver tweaks that are interesting and useful. We use several of these in production with excellent performance, but you should proceed with caution and do research prior to trying anything listed below.
Tickless System
The tickless kernel feature allows for on-demand timer interrupts. This means that during idle periods, fewer timer interrupts will fire, which should lead to power savings, cooler running systems, and fewer useless context switches.
Kernel option: CONFIG_NO_HZ=y
Timer Frequency
You can select the rate at which timer interrupts in the kernel will fire. When a timer interrupt fires on a CPU, the process running on that CPU is interrupted while the timer interrupt is handled. Reducing the rate at which the timer fires allows for fewer interruptions of your running processes. This option is particularly useful for servers with multiple CPUs where processes are not running interactively.
Kernel options: CONFIG_HZ_100=y and CONFIG_HZ=100
Connector
The connector module is a kernel module which reports process events such as fork, exec, and exit to userland. This is extremely useful for process monitoring. You can build a simple system (or use an existing one like god) to watch mission-critical processes. If the processes die due to a signal (like SIGSEGV, or SIGBUS) or exit unexpectedly you’ll get an asynchronous notification from the kernel. The processes can then be restarted by your monitor keeping downtime to a minimum when unexpected events occur.
Kernel options: CONFIG_CONNECTOR=y and CONFIG_PROC_EVENTS=y
TCP segmentation offload (TSO)
A popular feature among newer NICs is TCP segmentation offload (TSO). This feature allows the kernel to offload the work of dividing large packets into smaller packets to the NIC. This frees up the CPU to do more useful work and reduces the amount of overhead that the CPU passes along the bus. If your NIC supports this feature, you can enable it with ethtool:
[joe@timetobleed]% sudo ethtool -K eth1 tso on
Let’s quickly verify that this worked:
[joe@timetobleed]% sudo ethtool -k eth1 Offload parameters for eth1: rx-checksumming: on tx-checksumming: on scatter-gather: on tcp segmentation offload: on udp fragmentation offload: off generic segmentation offload: on large receive offload: off [joe@timetobleed]% dmesg | tail -1 [892528.450378] 0000:04:00.1: eth1: TSO is Enabled
Intel I/OAT DMA Engine
This kernel option enables the Intel I/OAT DMA engine that is present in recent Xeon CPUs. This option increases network throughput as the DMA engine allows the kernel to offload network data copying from the CPU to the DMA engine. This frees up the CPU to do more useful work.
Check to see if it’s enabled:
[joe@timetobleed]% dmesg | grep ioat ioatdma 0000:00:08.0: setting latency timer to 64 ioatdma 0000:00:08.0: Intel(R) I/OAT DMA Engine found, 4 channels, device version 0x12, driver version 3.64 ioatdma 0000:00:08.0: irq 56 for MSI/MSI-X
There’s also a sysfs interface where you can get some statistics about the DMA engine. Check the directories under /sys/class/dma/.
Kernel options: CONFIG_DMADEVICES=y and CONFIG_INTEL_IOATDMA=y and CONFIG_DMA_ENGINE=y and CONFIG_NET_DMA=y and CONFIG_ASYNC_TX_DMA=y
Direct Cache Access (DCA)
Intel’s I/OAT also includes a feature called Direct Cache Access (DCA). DCA allows a driver to warm a CPU cache. A few NICs support DCA, the most popular (to my knowledge) is the Intel 10GbE driver (ixgbe). Refer to your NIC driver documentation to see if your NIC supports DCA. To enable DCA, a switch in the BIOS must be flipped. Some vendors supply machines that support DCA, but don’t expose a switch for DCA. If that is the case, see my last blog post for how to enable DCA manually.
You can check if DCA is enabled:
[joe@timetobleed]% dmesg | grep dca dca service started, version 1.8
If DCA is possible on your system but disabled you’ll see:
ioatdma 0000:00:08.0: DCA is disabled in BIOS
Which means you’ll need to enable it in the BIOS or manually.
Kernel option: CONFIG_DCA=y
NAPI
The “New API” (NAPI) is a rework of the packet processing code in the kernel to improve performance for high speed networking. NAPI provides two major features1:
Interrupt mitigation: High-speed networking can create thousands of interrupts per second, all of which tell the system something it already knew: it has lots of packets to process. NAPI allows drivers to run with (some) interrupts disabled during times of high traffic, with a corresponding decrease in system load.
Packet throttling: When the system is overwhelmed and must drop packets, it’s better if those packets are disposed of before much effort goes into processing them. NAPI-compliant drivers can often cause packets to be dropped in the network adaptor itself, before the kernel sees them at all.
Many recent NIC drivers automatically support NAPI, so you don’t need to do anything. Some drivers need you to explicitly specify NAPI in the kernel config or on the command line when compiling the driver. If you are unsure, check your driver documentation. A good place to look for docs is in your kernel source under Documentation, available on the web here: http://lxr.linux.no/linux+v2.6.30/Documentation/networking/ but be sure to select the correct kernel version, first!
Older e1000 drivers (newer drivers, do nothing): make CFLAGS_EXTRA=-DE1000_NAPI install
Throttle NIC Interrupts
Some drivers allow the user to specify the rate at which the NIC will generate interrupts. The e1000e driver allows you to pass a command line option InterruptThrottleRate
when loading the module with insmod. For the e1000e there are two dynamic interrupt throttle mechanisms, specified on the command line as 1 (dynamic) and 3 (dynamic conservative). The adaptive algorithm traffic into different classes and adjusts the interrupt rate appropriately. The difference between dynamic and dynamic conservative is the the rate for the “Lowest Latency” traffic class, dynamic (1) has a much more aggressive interrupt rate for this traffic class.
As always, check your driver documentation for more information.
With modprobe: insmod e1000e.o InterruptThrottleRate=1
Process and IRQ affinity
Linux allows the user to specify which CPUs processes and interrupt handlers are bound.
- Processes You can use
tasksetto specify which CPUs a process can run on - Interrupt Handlers The interrupt map can be found in /proc/interrupts, and the affinity for each interrupt can be set in the file smp_affinity in the directory for each interrupt under /proc/irq/
This is useful because you can pin the interrupt handlers for your NICs to specific CPUs so that when a shared resource is touched (a lock in the network stack) and loaded to a CPU cache, the next time the handler runs, it will be put on the same CPU avoiding costly cache invalidations that can occur if the handler is put on a different CPU.
However, reports2 of up to a 24% improvement can be had if processes and the IRQs for the NICs the processes get data from are pinned to the same CPUs. Doing this ensures that the data loaded into the CPU cache by the interrupt handler can be used (without invalidation) by the process; extremely high cache locality is achieved.
oprofile
oprofile is a system wide profiler that can profile both kernel and application level code. There is a kernel driver for oprofile which generates collects data in the x86′s Model Specific Registers (MSRs) to give very detailed information about the performance of running code. oprofile can also annotate source code with performance information to make fixing bottlenecks easy. See oprofile’s homepage for more information.
Kernel options: CONFIG_OPROFILE=y and CONFIG_HAVE_OPROFILE=y
epoll
epoll(7) is useful for applications which must watch for events on large numbers of file descriptors. The epoll interface is designed to easily scale to large numbers of file descriptors. epoll is already enabled in most recent kernels, but some strange distributions (which will remain nameless) have this feature disabled.
Kernel option: CONFIG_EPOLL=y
Conclusion
- There are a lot of useful levers that can be pulled when trying to squeeze every last bit of performance out of your system
- It is extremely important to read and understand your hardware documentation if you hope to achieve the maximum throughput your system can achieve
- You can find documentation for your kernel online at the Linux LXR. Make sure to select the correct kernel version because docs change as the source changes!
Thanks for reading and don’t forget to subscribe (via RSS or e-mail) and follow me on twitter.
References
Fix a bug in Ruby’s configure.in and get a ~30% performance boost.

Special thanks…
Going out to Jake Douglas for pushing the initial investigation and getting the ball rolling.
The whole --enable-pthread thing
Ask any Ruby hacker how to easily increase performance in a threaded Ruby application and they’ll probably tell you:
Yo dude… Everyone knows you need to configure Ruby with --disable-pthread.
And it’s true; configure Ruby with --disable-pthread and you get a ~30% performance boost. But… why?
For this, we’ll have to turn to our handy tool strace. We’ll also need a simple Ruby program to this one. How about something like this:
def make_thread
Thread.new {
a = []
10_000_000.times {
a << "a"
a.pop
}
}
end
t = make_thread
t1 = make_thread
t.join
t1.join
Now, let's run strace on a version of Ruby configure'd with --enable-pthread and point it at our test script. The output from strace looks like this:
22:46:16.706136 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706177 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706218 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706259 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000005> 22:46:16.706301 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706342 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706383 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706425 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004> 22:46:16.706466 rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0 <0.000004>
Pages and pages and pages of sigprocmask system calls (Actually, running with strace -c, I get about 20,054,180 calls to sigprocmask, WOW). Running the same test script against a Ruby built with --disable-pthread and the output does not have pages and pages of sigprocmask calls (only 3 times, a HUGE reduction).
OK, so let's just set a breakpoint in GDB... right?
OK, so we should just be able to set a breakpoint on sigprocmask and figure out who is calling it.
Well, not exactly. You can try it, but the breakpoint won't trigger (we'll see why a little bit later).
Hrm, that kinda sucks and is confusing. This will make it harder to track down who is calling sigprocmask in the threaded case.
Well, we know that when you run configure the script creates a config.h with a bunch of defines that Ruby uses to decide which functions to use for what. So let's compare ./configure --enable-pthread with ./configure --disable-pthread:
[joe@mawu:/home/joe/ruby]% diff config.h config.h.pthread > #define _REENTRANT 1 > #define _THREAD_SAFE 1 > #define HAVE_LIBPTHREAD 1 > #define HAVE_NANOSLEEP 1 > #define HAVE_GETCONTEXT 1 > #define HAVE_SETCONTEXT 1
OK, now if we grep the Ruby source code, we see that whenever HAVE_[SG]ETCONTEXT are set, Ruby uses the system calls setcontext() and getcontext() to save and restore state for context switching and for exception handling (via the EXEC_TAG).
What about when HAVE_[SG]ETCONTEXT are not define'd? Well in that case, Ruby uses _setjmp/_longjmp.
Bingo!
That's what's going on! From the _setjmp/_longjmp man page:
... The _longjmp() and _setjmp() functions shall be equivalent to longjmp() and setjmp(), respectively, with the additional restriction that _longjmp() and _setjmp() shall not manipulate the signal mask...
And from the [sg]etcontext man page:
... uc_sigmask is the set of signals blocked in this context (see sigprocmask(2)) ...
The issue is that getcontext calls sigprocmask on every invocation but _setjmp does not.
BUT WAIT if that's true why didn't GDB hit a sigprocmask breakpoint before?
x86_64 assembly FTW, again
Let's fire up gdb and figure out this breakpoint-not-breaking thing. First, let's start by disassembling getcontext (snipped for brevity):
(gdb) p getcontext
$1 = {
(gdb) disas getcontext
...
0x00007ffff782517f
0x00007ffff7825186
...
Yeah, that's pretty weird. I'll explain why in a minute, but let's look at the disassembly of sigprocmask first:
(gdb) p sigprocmask
$2 = {
(gdb) disas sigprocmask
...
0x00007ffff7817383 <__sigprocmask+67>: mov $0xe,%rax
0x00007ffff7817388 <__sigprocmask+72>: syscall
...
Yeah, this is a bit confusing, but here's the deal.
Recent Linux kernels implement a shiny new method for calling system calls called sysenter/sysexit. This new way was created because the old way (int $0x80) turned out to be pretty slow. So Intel created some new instructions to execute system calls without such huge overhead.
All you need to know right now (I'll try to blog more about this in the future) is that the %rax register holds the system call number. The syscall instruction transfers control to the kernel and the kernel figures out which syscall you wanted by checking the value in %rax. Let's just make sure that sigprocmask is actually 0xe:
[joe@pluto:/usr/include]% grep -Hrn "sigprocmask" asm-x86_64/unistd.h asm-x86_64/unistd.h:44:#define __NR_rt_sigprocmask 14
Bingo. It's calling sigprocmask (albeit a bit obscurely).
OK, so getcontext isn't calling sigprocmask directly, instead it replicates a bunch of code that sigprocmask has in its function body. That's why we didn't hit the sigprocmask breakpoint; GDB was going to break if you landed on the address 0x7ffff7817340 but you didn't.
Instead, getcontext reimplements the wrapper code for sigprocmask itself and GDB is none the wiser.
Mystery solved.
The patch
Get it HERE
The patch works by adding a new configure flag called --disable-ucontext to allow you to specifically disable [sg]etcontext from being called, you use this in conjunction with --enable-pthread, like this:
./configure --disable-ucontext --enable-pthread
After you build Ruby configured like that, its performance is on par with (and sometimes slightly faster) than Ruby built with --disable-pthread for about a 30% performance boost when compared to --enable-pthread.
I added the switch because I wanted to preserve the original Ruby behavior, if you just pass --enable-pthread without --disable-ucontext Ruby will do the old thing and generate piles of sigprocmasks.
Conclusion
- Things aren't always what they seem - GDB may lie to you. Be careful.
- Use the source, Luke. Libraries can do unexpected things, debug builds of libc can help!
- I know I keep saying this, assembly is useful. Start learning it today!
If you enjoyed this blog post, consider subscribing (via RSS) or following (via twitter).
You'll want to stay tuned; tmm1 and I have been on a roll the past week. Lots of cool stuff coming out!

