Mono

<videoflash>cGoM9p7q1jk</videoflash>
DOWNLOAD this video in high-quality! and watch more videos of Mono in action
"Mono for Second Life" refers to a simulator upgrade which can dramatically speed the running of scripts — especially calculation intensive ones. The Linden Scripting Language ( LSL ) has not changed in any way*, so all of your existing scripted objects and attachments continue to function as before, only now they will have the opportunity to run faster. The key to this improvement is an open-sourced scripting engine called Mono. 
Mono is now live on the main grid with server version 1.24.3 
== How LSL scripts work ==
Linden Lab server programs, known as simulators or sims, run all LSL scripts. When you teleport, or region cross, your new region's sim takes on the task of running all your scripted attachments. But a sim cannot understand LSL directly -- the language was designed for human readability, not machine. So before the script can be executed, it must be turned into a machine readable format. This process is called compilation, and the resulting machine-readable version of the script is called bytecode. LSL scripts are compiled when they are created by resident-programmers. The bytecode itself is stored on the Linden Lab asset servers and never needs to be referred to directly by residents. Instead, when you rez a scripted object, the simulator for the region you are in notes the script(s) in the object, and requests the appropriate bytecode from the asset database. The simulator program has several parts, and the part which runs the script bytecode is called the LSL scripting engine or virtual machine. 
In today's Second Life, scripts are everywhere: from simple rotating objects to complicated vehicles, vendors, or attachments that respond to chat commands. For many regions the scripting engine is kept busy trying to execute hundreds of scripts all at once. As the number and complexity of scripts in a region rises, so do the demands upon the simulator. After a certain point the scripting engine starts taking up so much processing time that the rest of the simulator (particularly the physics engine) bogs down, resulting in server-side lag. Thus, anything that can speed up the execution of scripts can push out the point where server lag starts to occur.
== Enter Mono ==
Mono is an open-source scripting engine with a proven record of speed and versatility. But switching engines requires a compiler that can turn LSL scripts into Mono bytecode. This is tricky, because the goal is to make scripts running under Mono behave exactly like scripts running under the original engine. This desire for backward compatibility required an extraordinary amount of testing.
== Mono on the main grid ==
Thanks to the efforts of the beta test residents, and the Linden development and QA teams, Mono was deployed to the main grid in server version 1.24 on 29 August 2008. 
The Mono Viewer changes were first available in the 1.21 client, and are now present in all allowed SL viewers. The viewer changes include a checkbox on the script edit dialog (which allows you to make a script compile to Mono), a Tools menu item to allow you to recompile to Mono all the scripts in your selection, and a change to simulator statistics to show events per second instead of instructions per second. 
Another viewer dependency concerns the "Script perf" line in the statistics bar (ctrl-shft-1). In older viewers (1.20 and lower) this line will no longer contain useful data. In 1.21 and later viewers this line becomes "Script events" and gives the number of events handled per second. 
Once Mono has been live on the main grid for several months Linden Lab may turn off compilation to the original scripting engine. At that point all new and edited scripts will be Mono. However, the servers will also maintain the original scripting engine to enable old scripts to run as before, but run as Mono as soon as they are edited.
== Mono benefits ==
Performance benchmark tests show that Mono is up to 220 times faster than LSL2. The benchmarks were math-intensive scripts typically used to evaluate performance. For ordinary scripts, the performance gains are much more humble.
Mono uses more memory than the typical LSL bytecode. It offsets this by introducing dynamic memory allocation. LSL2 allocates a full 16KB for all scripts, even simple "Hello, Avatar" ones. Mono allocates only the memory it needs. In tests on typical regions it turns out that the combination of Mono using more memory, but allocating memory better, making it far smaller as the overall memory footprint goes.
In some extreme cases Mono scripts can use up to four times the memory as LSL2 scripts. To maintain backwards compatibility, the script size limit has been increased from 16KB to 64KB.
Mono tip:
Mono can do bytecode sharing. Thus multiple copies of scripts with the same asset id will only take up as much room as one instance. Imagine some script that you use a dozen times on your land. If each of the objects containing the script is separately compiled from text source, you will use up a dozen times the script's size of memory. But if instead you simply drag a copy of the single, already compiled script into each of the dozen objects, then no matter how many copies exist they only take up the size of one script (plus data) in memory.
== How to use Mono ==
Mono is now fully deployed to the main grid with server 1.24.3. All allowed SL viewers have the interface to create Mono scripts. 
* Log in to Second Life with a Release or Release Candidate viewer
* Create/edit a script and compile it to Mono
** Create an object and add a new script to it
** Edit the script from the object's contents tab.
** On the script editing dialog you will see a new checkbox at the bottom "Mono". Check it.
** Hit Save to recompile this script to Mono.
** You may now treat this script like any other. It will automatically run in the Mono runtime, regardless of ownership transfers or viewer version.
** To convert a Mono script back to ordinary LSL2, uncheck the checkbox.
* To convert a number of scripted objects all to Mono at once
** Rez all the scripted objects
** Use the select tool to select them all.
** From the Tools menu select Recompile Selection / Mono.
After you have converted a scripted object to Mono you will need to do a full QA run. Though Mono is compatible with old LSL2, there are timing differences which may cause object behavioral changes. Test your objects thoroughly before releasing Mono versions. 
To report Mono issues, use JIRA
* Public JIRA create issue
* If an issue already exists, and it appears to be the same failure, then simply add your information as a comment and attachment to that issue.
* If not, open a new SVC ticket for each bug you find. In the ticket summary please use the word "Mono" so that we can filter for them. 
* In addition to your description of the problem please attach your script itself, or (ideally) a smaller script which illustrates the difference in behavior between the two scripting engines.
* Link your JIRA to the meta JIRA.
== Testing ==
During the integration of Mono we have used tests to ensure there are no regressions. For the current version of the regression test see LSL Language Test. This effectively serves as the specification of LSL.
There are tests for library call bindings in LSL Library Call Test 1 and LSL Library Call Test 2. This is split to overcome the memory limitations.
Event test script
There are several benchmarks to test the performance. They are available here:
*LSL Recursion Benchmark
*LSL Mandelbrot Benchmark
*LSL Partial Sums Benchmark
*LSL NSieve Benchmark
*LSL NSieve Bits Benchmark
== FAQs ==
----
; Can you give a few example scripts where Mono's speed increase is readily apparent? : These calculation intensive scripts run considerably faster under Mono: LSL_Recursion_Benchmark, LSL_Mandelbrot_Benchmark, LSL_Partial_Sums_Benchmark, LSL_NSieve_Benchmark, LSL_NSieve_Bits_Benchmark.
; As far as previously purchased LSL scripts, why do I have to wait for the scripter to convert the script to run on Mono, why can’t I just do this myself? : The increase in speed provided by Mono can cause problems with objects using communication between multiple scripts and there are a few cases where we have had to make Mono behave slightly differently to LSL. It's much safer to have the original scripter convert and test the scripts.
; Will scripts compiled to Mono work on an older version of the viewer? : Yes. You only need a viewer version 1.21 or later if you want to make Mono scripts. You can run Mono scripts from any viewer since Mono actually runs on the server.
; Is there an indicator of some kind to tell me that a script is running on Mono? How can I tell? : No. It's very difficult to work out whether objects and all the objects they contain use Mono scripts, so we haven't attempted to display it.
; Given that OpenSim runs on Mono, will LL implementing Mono expedite interoperability between the two worlds? : We are talking to OpenSim developers about script interoperability based on Mono.
; Has the available memory for scripts changed? (Currently 16k in LSL2 VM) : For the same LSL script the Mono bytecode and original (LSL2) bytecode will be of different size. In order to be compatible with all known scripts, we have expanded the size ceiling for Mono to be 64k. This is ok to do for Mono because unlike LSL2, Mono allocates memory dynamically, whereas all LSL2 scripts occupy 16K. Mono scripts only allocate the memory that they need.
; 64K? Wow, isn't that going to encourage inefficient scripting? : We hope that the change will promote more efficient scripting. Currently programmers have to get around the 16K limit by using multiple scripts, and a lot of cycles get spent on passing data between those scripts. With a single script that would not be necessary.
; Are there other ways to make my scripts even more memory efficient when using Mono? : Indeed, Mono can do what's called bytecode sharing. Suppose you have a region which uses many instances of the same script, like XYText or Puppeteer, for example. As long as all the instances share the same asset id, the bytecode will only be added to memory once, and shared by all the copies. Key to making this work is ensuring that you simply copy the scripts (or the objects the scripts are within) after they have been saved for the final time. If you have purchased a script that is used many times, ask the creator for a Mono version, and then copy that version into the objects. It's important that you copy the scripts, so that the asset id is the same. If you recompile each instance separately, they will get different asset ids and the engine won't be able to share the bytecode.
; Will I have to manually convert all my objects to using Mono, or is there an automated tool? : Yes, you must manually invoke compilation to Mono. Though you can make use of the Tools Menu to recompile all scripts in selection to Mono.
; Can I keep my scripts running on the original scripting engine forever? : We have no plans to eliminate the original engine. This will be re-examined after Mono has been live on the Main Grid for a while. But right now it is easier to continue to support the original scripting engine than to migrate all scripts.
; Will I be able to write scripts in languages besides LSL since Mono supports lots of languages? : Eventually. Right now our goal is to make Mono completely compatible with the original scripting engine for LSL scripts.
 
; Will LSL be getting real language features with this change? (ie arrays, references/pointers, includes/imports) : No. The LSL language is not changing with this update.
; In the original scripting engine scripts are compiled on the viewer then uploaded, will that change with Mono? : Yes, Mono compilation is done in a distributed fashion on the sim hosts.
; Related to the above, I use a "clever trick" to upload my compiled bytecode in LSL2 with out the correct script text. What will happen to scripts I uploaded in this way when converted to Mono? Will I be able to continue to use my "clever trick" for Mono scripts? : The Mono compiler looks only at the script text. The Mono engine will only run bytecode which has been compiled by our Mono compiler. You will not be able to run any uploaded Mono bytecode.
; What about bytecode scripts whose LSL code has been lost, ie scripts that still run, but result in "Script missing from database." when you try to edit them? Is there any possibility of bytecode translation, or are these scripts stuck in the original scripting engine forever? : There are currently no plans to allow byte code translation of LSL scripts, only compiling from source. This may be considered depending on Resident demand.
; What are the known differences between the LSL2 and Mono compilers and runtimes?
: We have not tried to make Mono 100% compatible with the original engine. At least not to the point of duplicating any of the tricks or hacks that LSL2 allowed. Below are listed some known behavioral differences. Add to the list as more are discovered.
:* Unicode support. From Strife Onizuka "In LSO LSL, the entire Unicode range was supported by complying to RFC 2279 (about 2 billion possible characters). Mono supports RFC 3629 which supplants RFC 2279 and limits the Unicode range to the first 1,114,112 character codes. This directly effects these functions: llBase64ToString, llUnescapeURL. Strings being passed from LSO scripts to Mono scripts will become corrupted (in a reliable way) if they contain characters outside the limited Unicode range." -- 
:* Dividing by zero - LSL2 errors on divide by zero, Mono uses value "Infinity" and does not error. Under server 1.30, this is an error under both environments, watch for updates.
; After Mono launches why not recompile *all* scripts everywhere on the Grid to Mono?
: While it is certainly appealing to have only one compiler and runtime to support, practical concerns make this not feasible. Here's a list of the difficulties that give us pause:
:* We do not have the text asset (the LSL code) for all running scripts. These "bytecode only" scripts would stop working.
:* The automatic recompile would restart all scripts. Many scripts are meant to run continuously without restarting.
:* Mono scripts have a different timing profile than the original (usually faster). This will introduce behavior differences which will lead to some scripts breaking, often in subtle ways.
:* Some scripts take advantage of undocumented "features" of LSL2. We did not strive for 100% compatibility in such situations, but rather made Mono behave as sensibly and predictably as possible.
:* As a result of both reasons above, scripts need QA work after they have been recompiled. Residents who code and sell LSL scripts will have to test and possibly adjust the behavior of Mono versions of their scripts. If conversion were automatic they would not be reimbursed for their QA effort. With manual recompilation the Resident scripters can sell Mono versions of their scripts as an upgrade, after they have tested and modified them. 
:* Recompiling all scripts would play rather heavy-handedly with the permissions system. If someone has made and sold a script as "no-modify", an automatic recompilation would violate their policy. While some scripters would be ok with this, many would not.
; Is Mono still available on the preview grid? : Yes, see Mono/Beta FAQ.
== Mono Memory Myths ==
NOTE: The following has been commented on since by the OpenSim wiki - Miguel was testing an idle region without any background activity or users. It is not a representitive test. See more at 
Experience with Mono in Opensim has led to the widespread belief that Mono on Linux is an extreme memory hog compared to .NET on MS Windows. This belief has been countered by the creator of Mono, Miguel de Icaza, after he performed direct tests on 14th June 2009 in Vista 32, Linux 32, and Linux 64. His results and advice were posted on pastebin, and are repeated here since that site is not intended for persistent publication:
 A third follow up.
 
 With the help from the guys on #opensim on irc.freenode.org I got myself a sample virtual world from:
 
 http://opensimworlds.com/index.php?part=worlds
 
 I used "Nu Athens" a free download and loaded it up on 3 configurations:
 
 Vista, running 32 bit OpenSim
 Linux, running 64 bit OpenSim
 Linux, running 32 bit OpenSim
 
 And then I loaded all the four files provided in the zip file using the command:
 
 load oar FILE.tar.gz
 
 The results are as follows:
 
 Vista/32: 115 megs + 5 meg helper process (OpenSim.vhost.exe)
 
 Mono/32: 92 megs, after a few minutes of inactivity, it goes down to 87 megs.
 
 Mono/64: 122 megs, after a few minutes of inactivity, it goes down to 89 megs. 
 
 The garbage collector is responsible for the difference in memory usage between the load finished
 and waiting (I ran into this problem because I came to measure again after a few seconds and the
 size had been reduced).
 
 The Vista system remains at 120 megs total for the same world.
 
 So Mono on Linux on both 32 and 64 bit configurations is consuming less memory than Vista, some
 40 megs out of 120, or one third less memory.
 
 Perhaps the difference is in the version of Mono that we are running. I am running with Mono 2.4,
 and the OpenSim documentation implies that OpenSim can work with systems like Mono 1.2.6 which is
 primitive by our standards (that was released more than two years ago).
 
 In Mono 2.0, Mono 2.2 and Mono 2.4 we introduced many memory reduction features. From using
 precise collection for the HEAP, to reducing the size of generics-heavy code to reduction in code
 size generated and runtime metadata tables.
 
 I would appreciate if you could post a correction to your data in the main article, as it seems to
 have spread and this could negatively impact the perception of the Mono's community work towards
 making a great .NET implementation for Unix.
 
 Miguel
* PS. Miguel is measuring RSS (not VSIZE), for reasons explained in an earlier post.
Category:Features
Category:Performance and stability