Tag Archives: Mono

Technology brotherhoods


I often hear people talking about which technology is better between C / C++ / C# .Net / java. Most of the time, it’s more a political/brotherhood/community thing (like football, even thought frenchies like me aren’t so proud of their team now) than a technical talk.
I find it absurd. Computer science is about efficiency and making more money. You can take into account the pleasure you have to work on a technology (as you will be more likely to be more efficient with it), but it should still remain about efficiency.

The following video is about java & C# .Net. I’ve done quite a lot of C# .Net, Java and C, and some ObjectiveC (while developing iPhone applications) and a little bit of C++. The only thing I feel is that Sun with java had the good ideas, Microsoft with .Net took them and finished the work. Still, I often code in java, mostly for the TC65 chip, it’s quite comfortable. But for instance, it was longer to implement the M2MP protocol on the TC65 chip than the C# .net server. Mostly because of the unsigned types I had to “emulate” and the lack of generics (which are not available on the TC65’s JVM).

By the way, I’m running .Net apps on Linux everyday using Mono.

Here is the best example of what I’m talking about :

For all these lame people who don’t have an HTML5 compatible browser, you can go to the original video. The reason I downloaded it here in my blog is that you are forced to register to YouTube to watch it.

Stupid C++ vs C# performance comparison

I found out that there is real test than the little one i did here. In fact the main interest of the post is this spreadsheet :

I recently did a simple and stupid comparison test between C++ (g++) and C# .Net (mono).

My point here is that C# can actually be considered as a very fast language. It allows automatic hardware-specific optimization. The only real drawback you have in the .Net framework is the garbage collector. It prevents the C# .Net it from being a realtime language/framework. But does it really matter for you ?

The purpose of the following test is to show that C++ isn’t faster than C# “as is”. I KNOW C# can not be faster than C++ because C# can do few hardware specific optimizations when you can do all of them in C++, because all the systems calls have to be done in C/C++ and because there’s no such thing like Template and forced inlining in C#.

But if you’re searching for arguments to choose C# .Net over native C++, you should also consider these :

SO ! Here is the test…

I wrote these two sample programs :

One in C++ :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
 
long stupidThing( long nb ) {
	long out = 1;
	while( nb > 0 )
		out *= nb--;
 
 
	return out;
}
 
int main() {
 
	long total = 0;
 
	for( int i = 0; i < 1000000; ++i )
		for( long l = 0; l < 100; ++l )
			total += stupidThing( l );
 
	cout << "Total : " << total << endl;
 
	return 0;
}

One in C# :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
 
namespace test {
        class Program {
                static long stupidThing( long nb ) {
                        long ret = 1;
                        while ( nb > 0 )
                                ret *= nb--;
                        return ret;
                }
 
                static void Main( string[] args ) {
 
                        long total = 0;
 
                        for ( int i = 0; i < 1000000; ++i )
                                for ( long l = 0; l < 100; ++l )
                                        total += stupidThing( l );
                        Console.WriteLine( "Total : {0}", total );
                }
        }
}

First of all, I open a shell in real-time priority, because I don’t want my other processses to mess with my tests :

1
# rtprio 99 bash

Then I compile the two programs :

1
2
# gmcs test.cs
# g++ -O4 test.cpp -o test

And then I launch my test :

On a 64 bits host :

1
Kernel : 2.6.9-023stab051.3-smp #1 SMP Wed Nov 4 18:36:34 MSK 2009 x86_64 x86_64 x86_64 GNU/Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# time ./test ; time ./test ; time ./test ; time mono test.exe ; time mono test.exe ; time mono test.exe
Total : -6192109806162068864
 
real    0m12.433s
user    0m12.394s
sys     0m0.049s
Total : -6192109806162068864
 
real    0m12.415s
user    0m12.411s
sys     0m0.014s
Total : -6192109806162068864
 
real    0m12.430s
user    0m12.411s
sys     0m0.026s
Total : -6192109806162068864
 
real    0m10.311s
user    0m10.287s
sys     0m0.029s
Total : -6192109806162068864
 
real    0m10.254s
user    0m10.247s
sys     0m0.011s
Total : -6192109806162068864
 
real    0m10.250s
user    0m10.255s
sys     0m0.012s

C# clearly beats C++ here. Well

On a 32 bits host :

1
Kernel : 2.6.30-2-686 #1 SMP Fri Dec 4 00:53:20 UTC 2009 i686 GNU/Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# time ./test ; time ./test ; time ./test ; time mono test.exe ; time mono test.exe ; time mono test.exe
Total : 100461056
 
real    1m10.927s
user    1m7.376s
sys     0m0.056s
Total : 100461056
 
real    1m12.590s
user    1m8.976s
sys     0m0.020s
Total : 100461056
 
real    1m13.279s
user    1m9.532s
sys     0m0.056s
Total : -6192109806162068864
 
real    2m22.492s
user    2m15.260s
sys     0m0.136s
Total : -6192109806162068864
 
real    2m23.002s
user    2m15.760s
sys     0m0.104s
Total : -6192109806162068864
 
real    2m25.102s
user    2m17.709s
sys     0m0.144s

C++ beats C# here, but in 32 bits C++ use other types whereas C# use the same. In C# long is always 64 bits, in C++ it can be 64 bits or 32 bits (depending on the current architecture).

Mono Tools for Visual Studio

I talked about this earlier.

The mono tools for Visual Studio is a commercial product that easily enables you to test and port your Windows applications to mono.

As I told before this is really a great idea. Because I think Visual Studio is the best IDE and Linux is the best server OS (in my opinion, it’s more efficient and more robust than Windows Server). So, I think it’s the perfect gateway between this two very different (but very close since Mono appeared) environments to achieve greatness.

And if you don’t want to buy it, well, you don’t have to. Mono is free and you can very easily test your Windows programs on it. You just have to install mono, copy your exe (plus its required DLLs) to the Linux host and type something like “mono SampleProgram.exe”.

Server moved

I’ve switched from my two three years old dedicated servers to one brand new virtual server. Reasons are : These servers costed me too much and they were becoming old (risk of failure increases). It wasn’t worth it.

I spent last night doing that because I didn’t want to interrupt anybody using these servers.

My two servers were running some Debian and I’m now switching to a CentOS virtual server. I was a little bit worried at first that CentOS would have a crappy package management system, but its yum is in fact working the same way as Debian’s apt-get and OpenSuse’s zypper. The oool thing is that these three package management systems roughly work the same way : install , you don’t have to learn a new “ultimate” way to upgrade your software (like on FreeBSD). By the way, the faster package management system is yum, and the slowest one is zypper.

The biggest problem I had so far was to move all the databases. Previously, I was always using some Debian hosts, I was using exactly the same version of MySQL, so I just had to copy the MySQL datadir and logs files. Here, I had to export/import everything.

CentOS has an antique 1.2.4 version of Mono (when the current version is 2.4). I couldn’t manage to compile from the tarball but compiling from the SVN worked fine.

1
2
3
4
5
6
7
8
9
]# /usr/local/bin/mono --version
Mono JIT compiler version 2.5 (/trunk/mono r140917 Sat Aug 29 05:29:19 CEST 2009)
Copyright (C) 2002-2008 Novell, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        GC:            Included Boehm (with typed GC and Parallel Mark)
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none

Mono Tools for Visual Studio : I have tested it !

Yes, I have tested MonoVS with the version 0.2.2641 (on both client and server). I installed OpenSuse 11.1 and added the MonoVS software repository, and everything worked ! I would have prefer to get it from SVN in order to use it in my Debian hosts but the mono development team seems to have removed it from their SVN repository.

So, the Mono Remote Debugger for Visual Studio works, but there still some bugs. Deployment is super fast and it copies all the required DLL.

Remote debugging can be used (launching it is really fast too), but has some bugs, here are the ones I could find :
– On the classical mouse hover debugging popup, the expandable element “Non-public members” is always empty if available. If not available, every private member is displayed like any other public member variable.
– In my first tests, the “watch” window wouldn’t allow any variable to be expanded.
– If you have a variable not yet declared (but available) in the watch window and try to expand it, debugging just stops without any warning
– Sometimes, when I stop debugging I get a message saying something went wrong and the debugger might be unstable
– Once, after making a pause and then a stop, it totally crashed Visual Studio (but it only happened once).

And this is not really a bug, but unhandled exception are displayed in the dirty popup. This isn’t pretty.

If do “Run remotely in Mono”, it will display the Console output in the server’s console. If you do “Debug remotely in Mono”, the Console output is redirect the Debug output window.

This tool is still in private beta (I guess anyone has a good chance to be accepted like I was), but it can already help a lot of people. Even if you just use the Remote Running (which includes deploying the assembly), it still worth using this tool.

Mono Tools for Visual Studio

Just a little post for all these people who seem to think Mono is just an other short-term open-source software.

I’ve used it for quite some time with a production “real time” network server, which is running for something like 6 months now, and it performs very well. I do everything on my Windows host and then copy and launch the final app on the Linux host. But there are still two problems :

  • Not all .Net classes are supported. WCF (the most powerfull to do two-way async/sync communication) isn’t one of them.
  • You can’t use the powerfull Visual Studio debugger and you can’t take advantage of the PDB files (as they are not compatibles with mono).

Well, the Mono team has solved this second problem with their Mono Tools for Visual Studio. I have already applied twice and haven’t received any inivitation for the private mono tools tryout. But I guess it will be released to the public pretty soon (within 6 months). The mono guys are working really fast (but not as fast as the Microsoft .Net development team).

Sometimes people should just consider using Mono for their (web) applications. In my opinion, an ASP.Net + DataBase Linux server is faster to manage than a equivalent Windows Server. It doesn’t slow down with uptime, it doesn’t have dozens of useless services, it doesn’t require to restart for updates and real problems are way easier to diagnose.
The real limitation for me are the super Microsoft APIs and tools like WCF, LinQ, SQL Server 2008 (with its Integration and Analysis services) that you can only run on Windows.

NetEventServer

I talked some time ago about a library I made to take advantage of the kernel network events. I now release it and explain how to use it. It can help people to do some little network softwares without knowing where to start from.

I built it for network servers made to communicate with remotely connected embedded chips. I wanted to be able to always stay in touch with a huge number of chips without any real cost. So, my very personal goal was to built a server network layer for massive M2M applications.
I also made a little web server with it supporting Keep-Alive and partial file download (wit the “Range” header) and an other little library to send serialized objects.

I made this little network library to accomplish two main goals :

  • Simplify network server development
  • Be able to support a lot of connections

It is actually able to support a lot of connections : On a little Linux server using Mono (with 512 MB memory with swap deactivated), I easily managed to listen to 60 000 simultaneous connections without consuming more than 40% of the server’s memory.

And it allows to create network servers in only few line of code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Text;
using SoftIngenia.NetEventServer;
 
namespace TestServer {
 
	/// <summary>
	/// My little server
	/// </summary>
	class MyServer {
 
		public static String BytesToString( Byte[] data ) {
			var sb = new StringBuilder();
			sb.Append( String.Format( "[ {0} ] {{ ", data.Length ) );
			for ( int i = 0; i < data.Length; ++i )
				sb.Append( String.Format( " 0x{0:X02}", data[ i ] ) );
			sb.Append( " }" );
			return sb.ToString();
		}
 
		/// <summary>
		/// My server view of the client
		/// </summary>
		class MyClient {
			public MyClient( uint id ) {
				Id = id;
			}
 
			public uint Id { get; private set; }
 
			public int NbMessagesReceived { get; set; }
 
			public void Treat( byte[] data ) {
				Console.WriteLine( "{0}.Treat( {1} );", this, BytesToString( data ) );
				NbMessagesReceived++;
			}
 
			public override string ToString() {
				return String.Format( "Client{{Id={0}}}", Id );
			}
		}
 
		private readonly TcpEventServer _server;
		private readonly Dictionary<uint, MyClient> _clients = new Dictionary<uint, MyClient>();
 
		public MyServer( int portNumber ) {
			_server = new TcpEventServer( portNumber );
			_server.ClientConnected += server_ClientConnected;
			_server.ClientDisconnected += server_ClientDisconnected;
			_server.BinaryDataReceivedFromClient += server_BinaryDataReceivedFromClient;
		}
 
		public void StartListening() {
			_server.StartListening();
		}
 
		void server_BinaryDataReceivedFromClient( uint clientId, byte[] data ) {
			_clients[ clientId ].Treat( data );
		}
 
		void server_ClientDisconnected( uint clientId ) {
			Console.WriteLine( "Client {0} disconnected !", clientId );
			_clients.Remove( clientId );
		}
 
		void server_ClientConnected( uint clientId ) {
			Console.WriteLine( "Client {0} connected from {1} !", clientId, _server.RemoteEndPoint( clientId ) );
			_clients.Add( clientId, new MyClient( clientId ) );
		}
 
 
	}
 
	class Program {
		static void Main() {
			var myserver = new MyServer( 3000 );
			myserver.StartListening();
 
			Console.WriteLine( "Listening..." );
			Console.ReadLine();
		}
	}
}

This app launched gives you something like that :

1
2
3
4
5
Listening...
Client 1 connected from 127.0.0.1:53792 !
Client{Id=1}.Treat( [ 5 ] { 0x68 0x65 0x6C 0x6C 0x6F } ); // "hello"
Client{Id=1}.Treat( [ 2 ] { 0x0D 0x0A } ); // '<CR>' '<LF>'
Client 1 disconnected !

The library also enables you to receive data as text. You just have to subscribe to the “ReceivedLine” event. There’s no performance cost if you don’t subscribe to the event.

For network server, you still need to do some frame recognition. I usually instantiate a FrameParsing class into every client on the server side.

You can download the NetEventServer library with its XML and PDB files.

Lighttpd + Mono ASP.Net : The right configuration

As I already told before, I love the Mono project. It enables to run the powerful Microsoft .Net Framework on UNIX/Linux/BSD systems.

I recently wanted to test a very cool feature of ASP.Net on a mono server. So I did a little

1
apt-get install lighttpd mono-fastcgi-server2 -y

The feature I wanted to try was a web scripting method ( with the “[WebMethod]” attribute) exporting some JSON directly from your method return value.

Here is the web scripting method declaration :

1
2
3
4
5
6
7
8
9
10
 
[ScriptService]
public partial class _Default:Page {
 
	[WebMethod]
	[ScriptMethod( ResponseFormat = ResponseFormat.Json, UseHttpGet = false )]
	public static String TestMethod() {
		return DateTime.Now.ToString();
	}
}

And here is the javascript / JQuery code that gets the data :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<div id="toto"></div>
 
<script type="text/javascript">
    function doIt() {
        $(document).ready(function() {
            $.ajax({
                type: "POST",
                url: "/Default.aspx/TestMethod",
                contentType: "application/json; charset=utf-8",
                data: "{}",
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
            });
        });
        function AjaxSucceeded(result) {
            document.getElementById("toto").innerHTML = result.d;
        }
        function AjaxFailed(result) {
           document.getElementById("toto").innerHTML = "Error : "+result.status + ' ' + result.statusText;
        }
        setTimeout("doIt()", 1000);
    }
    doIt();
 
</script>

In my /etc/lighttpd/conf-enabled/10-fastcgi.conf file, I had this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server.modules   += ( "mod_fastcgi" )
fastcgi.server    = (
        "" => ((
                "socket" => mono_shared_dir + "fastcgi-mono-server",
                "bin-path" => mono_fastcgi_server,
                "bin-environment" => (
                        "PATH" => "/bin:/usr/bin:" + mono_dir + "bin",
                        "LD_LIBRARY_PATH" => mono_dir + "lib:",
                        "MONO_SHARED_DIR" => mono_shared_dir,
                        "MONO_FCGI_LOGLEVELS" => "Standard",
                        "MONO_FCGI_LOGFILE" => "/var/log/lighttpd/mono.log",
                        "MONO_FCGI_ROOT" => mono_fcgi_root,
                        "MONO_FCGI_APPLICATIONS" => mono_fcgi_applications
                ),
                "max-procs" => 1,
                "check-local" => "disable"
        ))
)

Everytime I launched a call from javascript, I got (with JS) I got a “405 Method not allowed”. Well, that was pretty disturbing. Mostly because a google search on this didn’t give me anything.
My first thought was that mono didn’t react the same way the .Net framework does. But this isn’t it. It came from my crappy lighttpd config file. It didn’t search for the Default.aspx file but for the Default.aspx/TesMethod.
What you need to do is set :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fastcgi.server    = (
        ".aspx" => ((
            # Everything you have in your file (like my file above)
        )
 
fastcgi.map-extensions = (
        ".asmx"   => ".aspx",
        ".ashx"   => ".aspx",
        ".asax"   => ".aspx",
        ".ascx"   => ".aspx",
        ".soap"   => ".aspx",
        ".rem"    => ".aspx",
        ".axd"    => ".aspx",
        ".cs"     => ".aspx",
        ".config" => ".aspx",
        ".dll"    => ".aspx"
)

And that will even improve your performances because any other file will be handled directly by lighttpd.

One quick note : There’s something weird with VS2008 WebDev.WebServer.Exe, the WebMethod request takes at least 1.0s to complete. On the same host with XSP, it’s around 15 ms. And on the Celeron 2.6Ghz linux server with lighttpd it’s around 12 ms. And on a IIS 7 server (bundled with Windows 7), it takes 12 ms. So why is the Visual Studio’s WebDev.WebServer so slow ?

By the way, why should we use a Linux + lighttpd + Mono server when we can use a Windows + IIS + ASP.Net server ? The reason is mainly that for the same usage, Linux consumes less resources and it’s easier to customize to suit your needs.

An other question you might have : Why use JQuery + JSON when you can use everything in the Microsoft AJAX Framework ? The main reason is that I really have the feeling to lose control with Microsoft AJAX Framework, I don’t like the huge automatically generated code. And it doesn’t make really fast web interfaces. With JQuery, everything goes faster and it’s way simpler to understand (and debug).

Related :
Using JQuery with ASP.Net

kernel event to improve network performance on .Net

I have built a simple tcp/udp network server library. I used it on lots of little programs and on one pretty important vehicle tracking server. The first version was using one thread for each connected client. It was working but it consumed a lot of memory. When the server reached something like 3000 simultanneous connections, the kernel was killing it for consuming to much memory.

I built a second version which was using network events. This allows you to directly receive network information from the kernel. It uses IOCP on Windows, epoll on Linux and kqueue on FreeBSD. This means that it’s not the program which frequently ask the kernel if there’s new data, it’s the kernel which tells the program that some new data has arrived.

The code change was pretty minimal. It’s just a little bit weird because you have to pre-allocate a buffer to allow the kernel to store data on it. I personally set a 128 bytes buffer. You have to add some BeginReceive calls everywhere and add some callbacks method.

My goal was to be able to reach 10 000 connections. After some few tests, I could reach 60 000 simulteanneous connections. The hardest part is in fact to allow the kernel to receive that many connection and to setup clients to open them. Once connections are opened, CPU consumption is always at 0% and server answers instantly to any client’s request.

That is for me the proof that .Net programs on Mono are well suited for massive network connection servers.

MapPoint WebService authentification with Mono

Recently, I was faced with a little problem. I built a .net program which calls a MapPoint WebService. It worked fine on Windows but failed on Mono/Linux with a “401 Unauthorized” error.

As it really made no sense, I decided to listen to the network communication. It did it with wireshark on my computer and tcpdump on the Mono/Linux host. And by looking at the header of the HTTP request, I noticed they were some slight differences.

I solved the problem by specifying some credentials and by using a particular host name for these credentials, here is the code :

1
2
3
4
5
6
var cred = new System.Net.NetworkCredential("---user---", "---password---");
_credCache = new CredentialCache();
_credCache.Add( new Uri( "http://findv3.staging.mappoint.net" ), "Digest", cred );
_finder = new FindServiceSoap();
_finder.Credentials = _credCache;
_finder.PreAuthenticate = true;