<commit_before>;; Copyright (c) <NAME>. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file LICENSE at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns conexp.contrib.profiler
  (:use [conexp.base :only (ns-doc)])
  (:use clojure.contrib.profile
        [clojure.contrib.def :only (defnk, defvar-)]
        [clojure.contrib.pprint :only (pprint, cl-format)]
        [clojure.contrib.except :only (throw-if, throw-if-not)]))

(ns-doc
 "Provides simple function for statistical and instrumental
 profiling.")

;;; Low Level

(defn current-thread
  "Returns the current thread."
  []
  (Thread/currentThread))

(defn- get-root-thread-group
  "Returns the root ThreadGroup."
  []
  (loop [^ThreadGroup tg (.getThreadGroup ^Thread (Thread/currentThread))]
    (if-let [parent (.getParent tg)]
      (recur parent)
      tg)))

(defn get-thread-list
  "Returns the list of all threads currently running."
  []
  (let [^ThreadGroup rg (get-root-thread-group),
        thread-count (.activeCount rg),
        ^"[Ljava.lang.Thread;" array (make-array Thread (* 2 thread-count)),
        returned-count (.enumerate rg array true)]
    (if (< returned-count (* 2 thread-count))
      (take-while (comp not nil?) array)
      (get-thread-list))))

(defn- stackdump
  "Returns a sequence of names representing the stack of the given
  thread."
  [^Thread thread]
  (map (fn [^StackTraceElement stack-element]
         (str (.getClassName stack-element)
              "/"
              (.getMethodName stack-element)
              " ("
              (.getFileName stack-element)
              ":"
              (.getLineNumber stack-element)
              ")"))
       (.getStackTrace thread)))

(defonce #^{:private true,
            :doc "Contains for every thread a hash-map mapping method
  names to the number of occurences found while profiling."}
  profiled-data (ref {}))

(defn show-profiled-threads
  "Returns a sequence of all profiled threads."
  []
  (keys @profiled-data))

(defonce #^{:private true,
            :doc "Contains for every thread the profiling thread."}
  profilers (ref {}))<commit_msg>Profiler now does not recognize recursive calls anymore<commit_after>;; Copyright (c) <NAME>. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file LICENSE at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns conexp.contrib.profiler
  (:use [conexp.base :only (ns-doc)])
  (:use clojure.contrib.profile
        [clojure.contrib.def :only (defnk, defvar-)]
        [clojure.contrib.pprint :only (pprint, cl-format)]
        [clojure.contrib.except :only (throw-if, throw-if-not)]))

(ns-doc
 "Provides simple function for statistical and instrumental
 profiling.")

;;; Low Level

(defn current-thread
  "Returns the current thread."
  []
  (Thread/currentThread))

(defn- get-root-thread-group
  "Returns the root ThreadGroup."
  []
  (loop [^ThreadGroup tg (.getThreadGroup ^Thread (Thread/currentThread))]
    (if-let [parent (.getParent tg)]
      (recur parent)
      tg)))

(defn get-thread-list
  "Returns the list of all threads currently running."
  []
  (let [^ThreadGroup rg (get-root-thread-group),
        thread-count (.activeCount rg),
        ^"[Ljava.lang.Thread;" array (make-array Thread (* 2 thread-count)),
        returned-count (.enumerate rg array true)]
    (if (< returned-count (* 2 thread-count))
      (take-while (comp not nil?) array)
      (get-thread-list))))

(defn- stackdump
  "Returns a sequence of names representing the stack of the given
  thread."
  [^Thread thread]
  (distinct (map (fn [^StackTraceElement stack-element]
                   (str (.getClassName stack-element)
                        "/"
                        (.getMethodName stack-element)
                        " ("
                        (.getFileName stack-element)
                        ":"
                        (.getLineNumber stack-element)
                        ")"))
                 (.getStackTrace thread))))

(defonce #^{:private true,
            :doc "Contains for every thread a hash-map mapping method
  names to the number of occurences found while profiling."}
  profiled-data (ref {}))

(defn show-profiled-threads
  "Returns a sequence of all profiled threads."
  []
  (keys @profiled-data))

(defonce #^{:private true,
            :doc "Contains for every thread the profiling thread."}
  profilers (ref {}))
{'fraction_non_alphanumeric': 0.12221105527638192, 'fraction_numerical': 0.002412060301507538, 'mean_word_length': 2.7217651458489156, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904641', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1416, 'n_words': 492}