Uses for advice: Instrumentation in event-driven simulations

| No TrackBacks

Reading Gary King’s query for users of advice (see also What is advice?), I’d like to add that advice and friends have uses beyond the usual debugging and patching uses that normally come to mind:

The ability to dynamically add or remove instrumentation code at runtime can be quite useful in cases such as event-driven simulations: When you are modeling complex systems, where long-running simulations can see billions of events and sub-events (i.e. sequences of distinct steps taking place during one event), it is often vitally important to efficiently log and analyze only those events or sub-events that really matter to the purpose of this particular simulation run: The simple approach of logging everything under the sun, and only later deciding what to analyse is often impractical due to speed and space constraints.

So the better approach is to only log that which is necessary, however the difficulty lies in determining which events matter and which don’t, especially since for different simulation runs, the things that matter might be completely different, depending on which analysis the user wants performed.

The approach we took in our logistics simulation framework was to use advice, or rather a home-built instrumentation framework built on frobbing CMU CL’s fdefn-function directly, similar to Gerd Möllmann’s fwrappers code now present in CMU CL: Using this facility report developers could define which methods they wanted instrumented, and, having full access to all information the methods had, use this to record all the information they needed for a particular report.

The instrumentation code was packaged together with the code that analyzed the recorded information and produced the final output, and the simulation user could then switch on or off individual reports at runtime for a simulation run, and only the overhead needed for those reports was inflicted on that particular run.

So a simple analysis which hooks two methods, melde-einlagerung and melde-auslagerung might look like this (sorry for the german names):

(define-analysis lager-umsatz-analysis
    ((laeger)
     (zugang-table :initform (make-hash-table :test #'eq))
     (abgang-table :initform (make-hash-table :test #'eq)))
  (:setup
   (setf laeger (find-subjekt-types 'Lagergruppe)))
  (:instrument
   (melde-einlagerung (element quelle gebinde)
     (when (typep element 'Lagergruppe)
       (incf (gethash element zugang-table 0) (gebinde-volumen gebinde)))
     (call-real-function element quelle gebinde))
   (melde-auslagerung (element quelle gebinde)
     (when (typep element 'Lagergruppe)
       (incf (gethash element abgang-table 0) (gebinde-volumen gebinde)))
     (call-real-function element quelle gebinde)))
  (:update
   (multiple-value-bind (sim-zeit-string real-zeit-string)
       (analysis-zeit-strings lager-umsatz-analysis
                              (- (aktive-zeit)
                                 (analysis-interval lager-umsatz-analysis)))
     (loop
        for lager in laeger
        for bestand = (lagerelement-belegt lager)
        for kapazitaet = (lagerelement-kapazitaet lager)
        for zugang = (gethash lager zugang-table 0)
        for abgang = (gethash lager abgang-table 0)
        do
          (format t "~A;~A;~A;~,2F;~,2F;~,2F;~,2F~%"
                  sim-zeit-string real-zeit-string (subjekt-name lager)
                  (safe-prozent zugang bestand)
                  (safe-prozent abgang bestand)
                  (safe-prozent zugang kapazitaet)
                  (safe-prozent abgang kapazitaet))
        finally
          (clrhash zugang-table)
          (clrhash abgang-table)))))

Of course with Lisp there are also other solutions to this problem, like runtime-compilation of conditionally-instrumented methods, however using advice was a simple and effective solution to this particular problem, which also works without access to the source of methods to be hooked.

No TrackBacks

TrackBack URL: http://mt.pierre-mai.de/mt/mt-tb.cgi/6

About this Entry

This page contains a single entry by Pierre R. Mai published on December 13, 2007 10:57 PM.

Alan Becker: Animator vs. Animation was the previous entry in this blog.

Kent M. Pitman: The Revised Maclisp Manual (The Pitmanual) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Recent Assets

  • Picture of PRM

Recent Activity

2011-02-17

  • Pierre tweeted, "Service-oriented Architecture (n.): What you do if you have no idea what service is, nor the any notion what software architecture is about."

2010-12-03

  • Pierre tweeted, "Again on the runway waiting for snow-delayed take-off. Quite the lifestyle..."

2010-11-29

  • Pierre tweeted, "The Future(tm): Uploading software releases and submitting policy input to the EU from a plane waiting 1h at the airport for its de-icing ,)"

2010-10-28

  • Pierre tweeted, "Achieved unbelievable win against Siedle, Siemens by connecting front gate intercom to PBX. Sad about the complexity involved though..."

2010-09-20

  • Pierre tweeted, "RT @jwz: Visual Transistor-level Simulation of the 6502 - in Javascript. http://j.mp/bWHK5F"

2010-09-17

  • Pierre tweeted, "Thanks to @mikeysan I'm playing with say -v "Cellos" on my Mac instead of configuring FreeBSD servers... say -v "Cellos" 'Thanks, mikeysan!'"

2010-08-04

  • Pierre tweeted, "Resurrecting my old Apple //e (yet again) lead to switching power-supply debugging (shorted rectifier dual diode) and soldering fun."

2010-07-25

  • Pierre tweeted, "Star Trek/iPad nostalgia/futurology cross-over: Reading the ST:TNG Technical Manual on the iPad, downloaded directly from subspace..."

2010-06-23

2010-06-02

  • Pierre tweeted, "RT @HansHuebner: U.v.d.Leyen als Bundespräsidentin? Ist das Amt nicht schon beschädigt genug?"