Browse files

Add heap heartbeats. Closes #114.

  • Loading branch information...
1 parent 8bfefca commit 6e79876cd637c18c455905b741fdea307a8b19d8 @amalloy amalloy committed Sep 20, 2011
Showing with 40 additions and 0 deletions.
  1. +1 −0 config.clj
  2. +14 −0 src/foreclojure/core.clj
  3. +25 −0 src/foreclojure/periodic.clj
View
1 config.clj
@@ -9,6 +9,7 @@
:pass ""
:repo-url "https://github.com/4clojure/4clojure"
:golfing-active true
+ :heartbeat nil ; set to, eg, [1 :hour] for periodic heap information on stdout
;; this list is just for bootstrapping - the real DB is authoritative
:contributors ["amalloy" "dbyrne" "raynes" "cmeier" "devn" "0x89"
"citizen428" "daviddavis" "clinteger" "amcnamara"]}
View
14 src/foreclojure/core.clj
@@ -16,6 +16,7 @@
[foreclojure.graphs :only [graph-routes]]
[foreclojure.mongo :only [prepare-mongo]]
[foreclojure.utils :only [wrap-uri-binding]]
+ [foreclojure.periodic :only [schedule-task]]
[ring.adapter.jetty :only [run-jetty]]
[ring.middleware.reload :only [wrap-reload]]
[ring.middleware.stacktrace :only [wrap-stacktrace]]
@@ -51,8 +52,21 @@
wrap-strip-trailing-slash
wrap-gzip))
+(defn register-heartbeat []
+ (when-let [period (:heartbeat config)]
+ (apply schedule-task
+ (let [^java.io.PrintWriter out *out*
+ ^Runtime r (Runtime/getRuntime)]
+ (fn []
+ (.println out (format "%d/%d/%d MB free/total/max"
+ (int (/ (. r (freeMemory)) 1e6))
+ (int (/ (. r (totalMemory)) 1e6))
+ (int (/ (. r (maxMemory)) 1e6))))))
+ period)))
+
(defn run []
(prepare-mongo)
+ (register-heartbeat)
(run-jetty (var app) {:join? *block-server* :port 8080}))
(defn -main [& args]
View
25 src/foreclojure/periodic.clj
@@ -0,0 +1,25 @@
+(ns foreclojure.periodic
+ (:import (java.util.concurrent ScheduledThreadPoolExecutor
+ ScheduledExecutorService
+ TimeUnit)))
+
+;; Stolen from clojail, but I wrote it myself anyway.
+;; I guess it deserves a library of its own? Or maybe in useful?
+(def uglify-time-unit
+ (into {} (for [[enum aliases] {TimeUnit/NANOSECONDS [:ns :nanoseconds]
+ TimeUnit/MICROSECONDS [:us :microseconds]
+ TimeUnit/MILLISECONDS [:ms :milliseconds]
+ TimeUnit/SECONDS [:s :sec :seconds]
+ TimeUnit/MINUTES [:m :min :minutes]
+ TimeUnit/HOURS [:h :hours]
+ TimeUnit/DAYS [:d :days]}
+ alias aliases]
+ {alias enum})))
+
+(def ^ScheduledExecutorService pool
+ (memoize (fn []
+ (ScheduledThreadPoolExecutor. 2))))
+
+(defn schedule-task [task period unit]
+ (.scheduleAtFixedRate (pool) task 0
+ period (uglify-time-unit unit)))

0 comments on commit 6e79876

Please sign in to comment.