Tuesday, March 30, 2010

Building Memcached on Windows

I like to be able to compile my software on multiple platforms with
different compilers, because it force me to write that complies to
standards (if not you'll most likely have a spaghetti of #ifdefs all
over your code). One of the bonuses you get from using multiple
compilers is that you'll get "more eyes" looking at your code, and
they may warn you on different things. Supporting multiple platforms
will also make it easier to ensure that you don't have 'hidden bombs'
in your code (byte order, alignment, assumptions that you can
dereference a NIL pointer etc)...

Building software that runs on different flavors of Unix/Linux isn't
that hard, but adding support for Microsoft Windows offers some
challenges. Among the challenges are:

Microsoft Windows doesn't provide the headerfiles found on most Unix-like systems (unistd.h etc).

Sockets is implemented in a separate "subsystem" on windows.

Win32 provides another threads/mutex implementation than what most software written for Unix-like systems use (pthreads).

Microsoft doesn't supply a C99 compiler. One would think that it would be safe to assume that one could use a C standard that is more than 10 years old....

I am really glad to announce that we just pushed a number of
changesets to memcached to allow you to build memcached on windows
"just as easy" as you would on your favorite Unix/Linux system. This
means that as of today users using Microsoft Windows is no longer
stuck with an ancient version!

`The first thing you need to do in order to build memcached on Windows
is to install the "fullversion"
of msysgit. In
addition to installing git, it also installs a compiler capable of
building a 32 bit version of memcached.

So let's go ahead and build the dependencies we need to get our 32bit
memcached version up and running!

The first thing up
is libevent. You
should download the latest 2.x release available (there is a bug in
all versions up to (and including) 2.0.4, so unless there is a new one
out there you can grab a development version I pushed
to libevent-2.0.4-alpha-dev.tar.gz),
and install it with the following commands (from within your
msysgit-shell). Please note that I'm using /tmp because I've had
problems using my "home directory" because of spaces in the directory
name (or at least I guess that's the reason ;-) ):

$ cd /tmp
$ tar xfz libevent-2.0.4-alpha-dev.tar.gz
$ cd libevent-2.0.4-alpha-dev
$ ./configure --prefix=/usr/local
$ make all (this will fail with an error, but don't care about that.. it's is in the example code)
$ make install (this will fail with an error, but don't care about that.. it's just an example)

It's time to start build memcached!!! Let's check out the source
code and build it!

Go ahead and telnet to port 11211 and issue the "stats" command to
verify that it works!

You don't need to install msysgit on the systems where you want to
run memcached, but you do need to include pthreadGC2.dll from the
msysgit distribution. That means that if you want to run memcached
on another machine you need to copy the following files:
memcached.exe .libs/default_engine.so and /mingw/bin/pthreadGC2.dll
(Just place them in the same directory on the machine you want to
run memcached on :-)

But wait, the world is 64 bit by now. Lets build a 64 bit binary instead

So how does it perform?

I'm pretty sure a lot of the "Linux fanboys" are ready to jump in and
tell how much faster the Linux version is. I have to admit that I was
a bit skeptical in the beginning if the layers we added to get "Unix
compatibility" had any performance impact. The only way to know for
sure is to run a benchmark to measure the performance.

I ran a small benchmark where I had my client create 512 connections
to the server, and then randomly choosing a connection and perform a
get or a set operation on one of the 100 000 items I had stored in
the server. I ran 500 000 operation (33% of the operations are set
operations), and calculated the average time. I can dual-boot one of
my machines into Windows 7 and Red Hat Enterprise Linux, so my
results shows the "out of the box"-numbers between Windows 7 and Red
Hat Enterprise Linux running on the same hardware. I used my
OpenSolaris box to drive the test (connected to the same
switch). Posting numbers is always "dangerous", because people put
too much into the absolute numbers. The intention with my graphs is
to show that the version running on Microsoft Windows is pretty much
"on par" with the Linux version.

Buuuuut..... the version from trondn/memcached.git is 1.3.3 and the last official version is 1.4.5 (http://www.memcached.org)....Using totalcommander I notice several differences between versions.And if I try to use the official repository instead of trondn's repository, the code wont compile.Also, the result exe wont work as a windows service or as a deamon either. This port does the trick but uses version 1.2.1 (Dec 23, 2006)...

Never the less, the version 1.3.3 seems to work ok. Here is the compiled result from my Intel x86:http://fox.originsoftware.com.ar/memcached-1.3.3.zip

IANAL, but AFAIK the BSD license allows you to mix in other licenses, and the LGPL library allows you to link with other licenses.. You do however need to comply to the terms of both the licenses, so if you make modifications to the lgpl bits you need to make the source available to the people you give the binary... People should just stay away from *GPL and the world would be a better place...

when I use this command: make -f win32/Makefile.mingw ,something wrong. make win32/Makefile.mingw: No such file or directory make *** No rules to make target win32/Makefile.mingw . stop.I install msysgit in C:\msysgit\msysgit. The Tmp path is F:\tmp.

please help to deal with it, tks.by the way , I am a chinese ,so my English is not well.

Hello.Sorry my bad English.Trond Norbye, I try to build memcached from MB-6508 branch on msysgit 1.7.11 and I got errorhttp://pastebin.com/fCuzE80vWhat is the dependency tcmalloc_minimal.dll and where I can get this dll?

I am trying to build memcached now and get some problems linking to libevent.

I get your git version of Memcached and use the win32/Makefile.mingw replacing some of the -lxxx with -Lpathtolibrary as teh whole path on my env is quite a mess.

I downloaded the latest libevent code too and build it using nmake, which produced a bunch of .lib files. I copied the libevent.lib file and use it for the linking with -L on the memcached.exe recipe and copied the header files to the mingw include folder so they are found.

Sadly when I try to build I get a lot of "undefined reference to event_*".

I think the problem is that I am linking wrongly but I am not sure as I am not familiar with gcc and c any more, Im a .Net guy this days.

If you can bring some light you will be more than appreciated. I found the following thread (https://groups.google.com/forum/#!topic/memcached/P4qMK4upyMk) where you answer some questions but the original problem wasn't hte same as mine.

Sure, when I'm done.. I'm currently working on making the compilation process of Couchbase better on windows. (please note that you may run Couchbase on Windows, and it does support memcached buckets...)