Sean, 

Thank you much for this. I am curious about this:

(defn cpu-usage 
  "Return utilization (as a string) and number of CPUs and load average." 
  [] 
  (let [bean (operating-system-bean) 
        data ((juxt cpus load-average) bean)] 
    (cons (cpu-percentage data) 
          data))) 


I notice that on my server, if at the command line I simply run "top" I see 
CPU load averages .5 or .6, but this function gives me numbers around 40. 
Could you point me to some information about what the JVM is really 
reporting with this number? 



On Wednesday, September 12, 2012 1:21:20 PM UTC-4, Sean Corfield wrote:
>
> Here's some code we use to get memory and CPU usage in a displayable form: 
>
> (defn- as-megabytes 
>   "Given a sequence of byte amounts, return megabyte amounts 
>    as string, with an M suffix." 
>   [memory] 
>   (map #(str (int (/ % 1024 1024)) "M") memory)) 
>
> (defn- as-percentage 
>   "Given a pair of values, return the percentage as a string." 
>   [[a b]] 
>   (str (int (* 100 (/ a b))) "%")) 
>
> (defn- memory-bean 
>   "Return the MemoryMXBean." 
>   [] 
>   (java.lang.management.ManagementFactory/getMemoryMXBean)) 
>
> (defn- heap-usage 
>   "Given a MemoryMXBean, return the heap memory usage." 
>   [^java.lang.management.MemoryMXBean bean] 
>   (.getHeapMemoryUsage bean)) 
>
> (defn- heap-used-max 
>   "Given heap memory usage, return a pair of used/max values." 
>   [^java.lang.management.MemoryUsage usage] 
>   [(.getUsed usage) (.getMax usage)]) 
>
> (defn memory-usage 
>   "Return percentage, used, max heap as strings." 
>   [] 
>   (let [used-max (-> (memory-bean) (heap-usage) (heap-used-max))] 
>     (cons (as-percentage used-max) 
>           (as-megabytes used-max)))) 
>
> (defn- operating-system-bean 
>   "Return the OperatingSystemMXBean." 
>   [] 
>   (java.lang.management.ManagementFactory/getOperatingSystemMXBean)) 
>
> (defn- cpus 
>   "Given an OSMXBean, return the number of processors." 
>   [^java.lang.management.OperatingSystemMXBean bean] 
>   (.getAvailableProcessors bean)) 
>
> (defn- load-average 
>   "Given an OSMXBean, return the load average for the last minute." 
>   [^java.lang.management.OperatingSystemMXBean bean] 
>   (.getSystemLoadAverage bean)) 
>
> (defn- cpu-percentage 
>   "Given the number of CPUs and the load-average, return the 
>    percentage utilization as a string." 
>   [[cpus load-average]] 
>   (str (int (* 100 (/ load-average cpus))) "%")) 
>
> (defn cpu-usage 
>   "Return utilization (as a string) and number of CPUs and load average." 
>   [] 
>   (let [bean (operating-system-bean) 
>         data ((juxt cpus load-average) bean)] 
>     (cons (cpu-percentage data) 
>           data))) 
>
> On Wed, Sep 12, 2012 at 10:12 AM, larry google groups 
> <lawrenc...@gmail.com <javascript:>> wrote: 
> > I need to know how much memory my app is using (it is a long running 
> > service). I see that some people have asked about how to measue memory 
> use. 
> > Robert McIntyre asked, "get the total memory used by a data structure?" 
> Some 
> > of the answers imply that it is easy to get the total memory use of the 
> app 
> > (much easier than getting the memory used by a particular data 
> structure?). 
> > I am ignorant of the JVM. How can I find the total memory used at any 
> given 
> > moment by my app? 
> > 
> > Goal: I wrote a small app that uses Ring and Moustache and Enlive. It 
> lives 
> > on my server and runs perpetually. I worry about it crashing, or 
> becoming 
> > overloaded. I am setting up some ping services (not sure which yet, 
> Nagios, 
> > or Puppet or something) to ask the app "Are you still alive?" I've 
> > established a special Moustache route just for the ping. It occurred to 
> me 
> > that the ping could get some useful info, like memory, and save that to 
> a 
> > file. That would give me the a good time series about the real world 
> memory 
> > use, maybe every 5 minutes. 
> > 
> > I established the app with this JVM setting: 
> > 
> >  :jvm-opts ["-Xmx4000m"] 
> > 
> > Within that limit, I'd like to know what is going on. 
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to