DX Unified Infrastructure Management

Expand all | Collapse all

Using threads in NSA

  • 1.  Using threads in NSA

    Posted Oct 01, 2009 02:12 AM
    I have been thinking about using threads in the NSA. The following warning appears in the whitepaper:
    Please note that this inline code runs in a complete separate context, thus not able to access any data in the callers context.
    The first argument to the thread() function is a string containing the code to be executed. I wanted the code to call another function from my script, but it was not working. Then I realized that functions are just normal variables in Lua, so the warning above applies to them as well as data variables.

    Is there any way to work around this? The code I want to thread is fairly long and involved, so I do not think I can just create it as a string to hand to the thread() function. I think I may be out of luck if I insist on keeping my code in functions rather than a string.

    Thanks,
    Keith


  • 2.  Using threads in NSA

    Posted Oct 01, 2009 02:53 AM
    I'm not a big LUA scripter myself, but a possible solution might be to break your functions out into a separate file and do something like:

    --from http://www.lua.org/pil/8.html
    f = loadfile("foo.lua") -- load and compile
    f() --define functions
    foo() --call foo function



  • 3.  Using threads in NSA

    Posted Oct 01, 2009 02:29 PM
    You could also exploit the module concept.  The thread is implemented by spinning of a lua chunk in its own thread, thus not seing "the caller script".

    If you collect your code in a module (or by using loadfile) you could load the chunk on demand either by using the require or loadfile primitives.

    Consider the file case.lua placed in modules/case:

    module (..., package.seeall)

    function tryit(arg)
       printf("in tryit, argument:%s",tostring(arg))
    end


    You may then call load and use the functions by this script:

    function timeout()

        runs = runs + 1
        probe.log(0,"timeout #%d...",runs)
       
        if runs % 3 == 0 then
            probe.log(0,"time to spin of a thread...")
            thread ()
               
                require ("case")
                case.tryit(args)
               
                probe.log(0,"thread done...")
                ]],"arg1,arg2")       
        end   
           
    end

    --
    -- Main
    --
    runs = 0

    probe.register("***","1.10","Nimsoft Corporation 2009")
    probe.run()
    probe.unregister()


    Carstein


  • 4.  Using threads in NSA

    Posted Oct 01, 2009 05:18 PM
    Cool, thanks for the suggestions, guys! They look like they would work well. Of course, being unable to share variables with the threads will pose its own set of problems, but there would be ways to work around that as well.

    Thanks,
    Keith


  • 5.  Using threads in NSA

    Posted Oct 01, 2009 11:52 PM
    Out of curiousity - is the Lua global table( _G) not available for sharing functions and variables among threads in the NSA?

    -edit:
      I just tried using the NSA and the global table is not shared amongst threads. Looks like you might have to share thread data through the filesystem, registry(ick) or some Lua IPC lib.


  • 6.  Using threads in NSA

    Posted Oct 02, 2009 04:52 AM
    There is a way to pass arguments into a thread, but that looks a bit cumbersome if you are dealing with more than a few values.  The use case currently on my mind would require quite a bit of data to be shared.  (I have a Lua probe that uses SNMP, and I was thinking of making the checks parallel with threads.)

    I think the best way around this would be to use something like JSON to package up the data, so it can be reinterpreted on the receiving side.  LuaForge has 3 modules for adding JSON support to Lua, so I might have to check those out.

    -Keith


  • 7.  Using threads in NSA

    Posted Oct 02, 2009 02:44 PM
    If I'm understanding you correctly and your intention is to pass data
    one way, you can just fill a table in one thread with data, convert it
    to a string(if you google "Lua table tostring | serialization" you'll find alot of functions to do this), pass that string as the first argument to your new thread
    and then use Lua's loadstring() on the new thread to recreate the table.


  • 8.  Using threads in NSA

    Posted Oct 02, 2009 05:06 PM
    Yes, that's exactly what I'm talking about. JSON is just another way to serialize data.

    -Keith


  • 9.  Using threads in NSA

    Posted Dec 03, 2009 12:47 AM
    I just saw this and had one other question regarding the use of threads inside NSA.  Is there a way to tell when a thread has finished?  I'm executing a bunch of things, and want to run them in parallel, but as this is not a long running process, I would like to know when the threads terminate so that I can terminate the caller as well.

    Any help would be appreciated.

    --shakeel


  • 10.  Using threads in NSA

    Posted Dec 03, 2009 02:21 PM
    I guess the simplest is to create semaphore files, the caller creates a file prior to kicking off the thread and the thread removes them upon completion.  The main script can then monitor the semaphore files.


  • 11.  Using threads in NSA

    Posted Dec 04, 2009 12:18 AM
    Ahh yes, that would work!

    Thanks, for the help!

    --shakeel