Showing posts with label LSL. Show all posts
Showing posts with label LSL. Show all posts

Friday, September 14, 2007

Pure C# support

ScriptEngine supports both LSL and C# within the same script. Mainly this means that you can write LSL, but when you want you can use C# code. For example instead of using llToLower(mystring) you can use mystring.ToLower(), instead of llStringLength(mystring) you can use mystring.Length().

But to make sure that we support pure C# code, I've added support for forcing the compiler to not use LSL preprocessor. This means skipping directly to compile without going through LSL to C# converter.

Any script that starts with the 4 letters "//C#" or //c#" will be compiled as C# directly. This ensures that we have 100% C# support.

There are a few rules you need to follow. Mainly:

  • Any LSL event needs to be renamed to <STATE>_event_<EVENTNAME>.
    For example "start_entry()" in default state will be "public void default_event_state_entry()"
  • LSL events needs to be public, of course.
  • Script needs to start with "//C#" without the quotes and without space in front of it. These 4 characters needs to be the first thing inside the script!
  • You still have to use ll-commands for stuff that native C# doesn't support. But don't worry - the ll-commands are just the name, you are actually writing in C#. ;)

If you put a LSL script into an object in OpenSim, the C# code of that script will be debug-logged to the "bin\ScriptEngine\"-folder. So if you have any trouble, just write up a LSL code, put it into an object and you have a working C# script that does the same thing.

Untested example:

//C#

namespace SecondLife
{

   public class Script : OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass
   {

      public Script() { }

      // Your script goes here!
      int touch_count = 0;
      int tick_count = 0;
      int objnum = 1;

      public void default_event_state_entry()
      {
         llSay(0,
"Hello, Avatar! Starting timer...");
         llSetTimerEvent(10);
      }

      public void default_event_touch_start(int total_number)
      {
         touch_count++;
         llSay(0,
"Object was touched. Touch count: " + touch_count);
      }

      public void default_event_timer()
      {
         tick_count++;
         llSay(0,
"Timer count: " + tick_count);
      }
   }
}

Sunday, August 26, 2007

Small stresstest

114 scripts loaded. 1 script per AppDomain. Most scripts are alone inside a prim, except the big box over there that has something like 10 scripts in it (it is bigger so I could fit more scripts into it).







My computer is a 2,8GHz Prescott with 2GB RAM and Windows Vista Ultimate.
60 of the scripts run the timer event every 1 second. 50 run it every 5 seconds.



Basically this tells us that ScriptEngine uses a bit too much memory. This is expected. CPU usage while running 114 events every x seconds is almost nothing.


Increase from 38MB to 76MB RAM when creating objects and scripts, so ~10MB for 25 scripts+objects.


The scripts don't do much work right now, but if they did the speed would still be high as they are compiled to CPU native code.



Results after running for 9 hours

OpenSim did not crash or eat any more CPU or memory over time. However the Second Life viewer did crash, so I had to log in again... Funny ;)

Saturday, August 25, 2007

So where are we now?

[Updated Monday 13:45 GMT+1]
[Updated Wednesday 21:45 GMT+1]
[Updated Saturday 22:45 GMT+1]

As always its important with a summary for those who don't want to read techie details.

Here is current status of LSL script support:
+ We have LSL script support.
+ We can run hundreds of scripts simlutaneously, probably not thousands (needs testing). We have confirmed 50 simultaneous scripts without any sign of trouble.
+ We can utilize multicore CPU's for running scripts.
+ We can to some degree unload scripts to free up memory. (if we use 1 AppDomain per script)
+ Events from server are put into a queue, so server threads are released imediately.
+ Some llFunctions are implemented.
+ ScriptEngine is more or less ready to bind individual scripts to individual prims.
+ We have implemented touch_start event when someone touches a prim.
+ We have implemented llSetTimerEvent.
+ Script compile errors are displayed in-world.
+ We can put scripts into prims.

- Can not run too many scripts that contain infinite loops or sleeps to long.
- No memory/CPU sharing/limitation/throttling on scripts.
- Almost no security implemented on scripts.
- Can not move script between regions.
- Missing 300+ llFunctions.
- Missing most events.





There is room for making scripts run 2-5 times faster, if not more. And same in memory usage. This is because of the development process.
"Get it working, remove bugs, make it fast."


There is much data related to each script. I suspect if we load too many scripts the working set (WSClock) will be too high. Meaning there is too much data to process, so the CPU and memory will be more occupied with looking at what to do than actually doing it.

Introduction of a new class

Some LL-commands takes longer to return. These often return with a event. For example llSetTimerEvent(10) will return a timer() event every 10 seconds. llHttpRequest() will return when it receives answer from HTTP-server.

To handle these types of requests a new class has been added as a subclass of ScriptEngine. The class is named LSLLongCmdHandler.cs. This class will contain handlers for adding/removing requests. Adding when script executes a command, removing when command is not complete yet but script is being moved to another region or otherwise removed.

The class has a dedicated thread that will be working the different services, for example async HTTP requests and queueing of timer events at the correct intervals.
LSL_BuiltIn_Commands.cs accesses this class through ScriptEngine.


Example from LSL_BuiltIn_Commands.cs:

public void llSetTimerEvent(double sec)
{
// Setting timer repeat
m_ScriptEngine.myLSLLongCmdHandler.SetTimerEvent(m_localID, m_itemID, sec);
}

Sunday, August 19, 2007

Speed

I've added support for multithreading execution of script events. Only one object will be processed at the time. This will benefit from MultiCore CPU's. Though it makes each call more expensive because we have to keep track of mutexed objects.

Also added caching of event binding (function call) so we only use Reflection on first execution of a given event within a given state.

Changed sleep time of inactive threads from 200ms to 50ms, so they are more responsive.

AppDomains

.Net does not allow unloading of assemblies. If you want to unload an assembly you have to unload the whole AppDomain.

I've created AppDomainManager that handles creating AppDomain, finding a free AppDomain, loading scripts into it, and unloading AppDomains with no active scripts in them.

Its purpose is to create AppDomains and put X number of scripts into each of them. Later when we get microthreading support we will add a feature to move scripts between AppDomains, so if one or more AppDomains has only a few active scripts we can move these to a fresh AppDomain and unload the old one.

With the introduction of AppDomain there came a few changes to the source. Because of how communication between AppDomains (mainly between scripts AppDomain and the main AppDomain) works there has been made some changes.

First of all a new assembly called OpenSim.Region.ScriptEngine.Common.dll has been introduced. It contains interface for scripts and some common code for all AppDomains to execute events inside scripts.

Secondly the LSL BuiltIns (llFunctions) has been moved to a separate file (LSL_BuiltIn_Commands.cs) and are no longer part of the LSL_BaseClass.cs.

Also OpenSim.Region.ScriptEngine.Common.LSL_Types that contains custom Vector and Rotation objects is now used throughout DotNetEngine/Compiler.

Each AppDomain has only 2 references, OpenSim.Region.ScriptEngine.Common.dll and OpenSim.Region.ScriptEngine.DotNetEngine.dll.

Friday, August 17, 2007

llSay implemented

Picture of llSay() implemented.


Tuesday, August 14, 2007

llFunction interface/empty prototypes

Charles Krinkle has helped in adding interface and necessary empty prototype implementations of base class for the compiled script.

Almost all functions has been implemented (they contain no code yet though). This helps me test the compiler on all kinds of scripts without getting errors.

Thanks dude! :)

New status after Monday (and a milestone)

ScriptEngine has now been implemented!

We have a default script that is placed in all objects. We have a "touch_start" event. And we have llSay() with output to server console.

Once inventory is working we can bind user made scripts to user made objects. So now we can start implementing all 350+ builtin ll-functions.

We will be able to test the functions in-world as they are being implemented. Just modify the script "Default.lsl" under "bin\ScriptEngines\" folder to use the new command you are implementing.

There may still be problems compiling some scripts. Please let me know about these problems so I can fix them!
Next step for me is to implement error handling around ScriptEngine.

Sunday, August 12, 2007

Status of LSL compiler

Status update

LSL->C# Translator is close to complete. Needs bigger/more scripts to bughunt.
Compiler is capable of compiling C# to .Net (invokes LSL->C# trans if needed).
Script Manager is capable of loading and initializing script .Net Assembly.

Missing:
LSL BuiltIn command implementation.
Event implementation.

How to contribute?

OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass.cs contains a s*load of empty functions. These are all the LSL-functions. And all needs to be implemented. Implement most important/most used first.

There is a "World" object available (scene).

Example of implementation:


public void llSay(Int channelID, string text)
{
World.Something.Somewhere.SendMessageToEveryoneAround(channelID, text);
}

LSL to C#

To compile LSL code I figured the best way would be to first translate it into C#.
Since LL will be supporting both LSL and C# we can use the same translator on a web page or similar to help people who know LSL to understand C#.
Also it is much simpler for other developers on OpenSim to contribute to the code when its C# instead of MSIL.

The translator uses some regex and a couple of byte-by-byte passes over the code. Written in less than a day, no big magic involved.

In-memory compile to assembly (that will be cached for later reuse).
Assembly is dynamically loaded in same way as LSO.

The script inherits from a LSL_BaseClass.cs that contains all the builtin-functions like llSay(), llWhisper, etc.

Changing tactics

After a meeting with Babbage Linden in Zero Lindens office hours on thursday I realized that with the new Mono/.Net compiler they are implementing we will have to make a few adjustments.

It was earlier assume that compilation would be done at client of both LSL and LSO, therefore I focused on making a LSO (LSL virtual machine) ByteCode to .Net CIL (Assembly language) translator.

But because of some security concerns in the .Net ByteCode LL has chosen to do compilation on server. LSL or C# code is sent to server where it is compiled and put into an object. The compiled code is never sent from the viewer.

Therefore I am now writing a LSL compiler that will run on server.

While waiting for Linden to update their client (probably 0,5-1 years) we will use the text-copy of LSL script sent by viewer and compile that. Meaning we are not using the LSO at all.

So basically we are scrapping the LSO compiler, and going for a LSL compiler. There are many clear advantages to this, so I'm quite happy about it.