[plt-scheme] web-server memory use

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue May 1 15:14:13 EDT 2007

On 5/1/07, Dave Gurnell <d.j.gurnell at gmail.com> wrote:
> Thanks for you help so far, guys. Jay - could you send me the code
> you knocked up with Matthew's make-cust-box, please? I'm not sure I
> understand what you're talking about in terms of implementation.

Code at the bottom.

> -- How to get memory accounting working in web-server.
> It looks as if this won't be too difficult to achieve. Even without
> the direct implementation Matthew was talking about, this would be
> very useful as a debugging tool to determine how memory-heavy our
> code is.


> -- How to make sure LRU behaves "well".
> I'm not sure whether or not you were saying there was a problem with
> this. I haven't really got the full picture in my head yet.
> The LRU in our web-server uses the value returned by (current-memory-
> use) to determine the rate at which it needs to kill off
> continuations. If memory is not freed up properly when continuations
> are deleted, then (current-memory-use) will increase regardless of
> LRU's efforts. As far as I can see this all works, but I haven't done
> any real stress testing yet.

All the continuation managers work when they expire individual
continuations, because they remove them from the hash-table and thus
are no longer live. However, what I was suggesting is that when an
instance is killed, all I do is kill the custodian, and if the
custodian isn't accounting properly there is a problem.

> In my investigations, I noticed that LRU deletes continuations but
> never instances. Once all continuations are removed from an instance,
> it just sits there in the hash table. I don't know if this would
> cause a big drain on memory... I haven't ascertained yet what the
> instance structure's data field refers to.

There is code in lru.ss to remove the instances. I haven't noticed a
problem with Continue, so perhaps we should off-line and get a test
case, or your logs. There is `race condition', in that an instance may
instantaneously have no continuations in the htable, but is about to
put one in. This happens in the beginning, with a use of send/forward,
or under memory pressure.

> Past experiences have hinted that our software for Queen Mary,
> University of London may be taxing web-server more than other
> applications that I've heard about. This makes me nervous about our
> problems with continuation expiry. Our next release will contain a
> number of critical pages, and we can't afford to have them go wrong.
> I'm keen to get these issues resolved and make things as stable as
> possible. We've exchanged a number of ideas about this here at
> Untyped, but we need to work out exactly what's happening before we
> can make an informed decision.

With Continue I put additional information in the URLs with my
url-param.plt module and then the server uses this information to
partial reconstruct expired continuations. Perhaps that will be useful
for you as a stop-gap.


Jay McCarthy <jay.mccarthy at gmail.com>

jay $ svn diff servlet.ss dispatchers/dispatch-servlets.ss managers/manager.ss
Index: servlet.ss
--- servlet.ss  (revision 6112)
+++ servlet.ss  (working copy)
@@ -7,6 +7,7 @@
+           "private/cust-box.ss"

   ;; ************************************************************
@@ -92,7 +93,8 @@
        (let/cc k
          (define instance-id (get-current-servlet-instance-id))
          (define ctxt (thread-cell-ref current-execution-context))
-         (define k-embedding ((manager-continuation-store!
(current-servlet-manager)) instance-id k expiration-handler))
+         (define k-embedding ((manager-continuation-store!
+                              instance-id (make-cust-box
(current-custodian) k) expiration-handler))
          (define k-url ((current-url-transform)
                          (list* instance-id k-embedding)
Index: dispatchers/dispatch-servlets.ss
--- dispatchers/dispatch-servlets.ss    (revision 6112)
+++ dispatchers/dispatch-servlets.ss    (working copy)
@@ -17,6 +17,7 @@
+           "../private/cust-box.ss"
@@ -257,8 +258,8 @@
                      (thread-cell-set! current-execution-context
                                         conn req suspend))
-                     (let ([k ((manager-continuation-lookup manager)
instance-id k-id salt)])
-                       (k req)))])
+                     (let ([kcb ((manager-continuation-lookup
manager) instance-id k-id salt)])
+                       ((cust-box-value kcb) req)))])
               (output-response conn response))
             (semaphore-post (servlet-instance-data-mutex data)))))
       ((manager-instance-unlock! manager) instance-id)
Index: managers/manager.ss
--- managers/manager.ss (revision 6112)
+++ managers/manager.ss (working copy)
@@ -21,8 +21,8 @@
                     [instance-lock! (number? . -> . void)]
                     [instance-unlock! (number? . -> . void)]
                     [clear-continuations! (number? . -> . void)]
-                    [continuation-store! (number? procedure?
expiration-handler? . -> . (list/c number? number?))]
-                    [continuation-lookup (number? number? number? .
-> . procedure?)])]
+                    [continuation-store! (number? any/c
expiration-handler? . -> . (list/c number? number?))]
+                    [continuation-lookup (number? number? number? .
-> . any/c)])]
    [struct (exn:fail:servlet-manager:no-instance exn:fail)
            ([msg string?]
             [continuation-marks continuation-mark-set?]

Posted on the users mailing list.