Last active
February 28, 2020 15:15
-
-
Save gonewest818/bfa37a77a1967eadc838f6938701efa8 to your computer and use it in GitHub Desktop.
getting thread stats via jmx in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns xyz.core | |
(:require [clojure.string :refer [lower-case]]) | |
(:import java.lang.management.ManagementFactory)) | |
(defn describe-thread | |
"describe thread based on thread-info" | |
[thread-mx-bean thread-info] | |
(let [id (.getThreadId thread-info)] | |
{:id id | |
:name (.getThreadName thread-info) | |
:state (-> thread-info .getThreadState .toString lower-case keyword) | |
:blocked-count (.getBlockedCount thread-info) | |
:blocked-time (.getBlockedTime thread-info) | |
:waited-count (.getWaitedCount thread-info) | |
:waited-time (.getWaitedTime thread-info) | |
:cpu-time (.getThreadCpuTime thread-mx-bean id) | |
:user-time (.getThreadUserTime thread-mx-bean id)})) | |
(defn thread-top | |
"list all threads sorted by cpu" | |
[] | |
(if-let [mx (ManagementFactory/getThreadMXBean)] | |
(let [thread-dump (.dumpAllThreads mx false false) | |
thread-info (map (partial describe-thread mx) thread-dump)] | |
(reverse (sort-by :cpu-time thread-info))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment