Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

66
LINES

< > BotCompany Repo | #1036379 // SingleThreadProfiler backup

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11146L/63K).

1  
sclass SingleThreadProfiler is AutoCloseable {
2  
  // config
3  
  settable Thread threadToSample = currentThread();
4  
  settable int interval = 40; // 25 Hz
5  
  settable bool skipSleeping;
6  
  
7  
  // results
8  
  new MultiSet<S> stackTraceMultiSet;
9  
  gettable int samples;
10  
  
11  
  // temporary
12  
  java.util.Timer timer;
13  
  Lock lock = lock();
14  
15  
  void start {
16  
    lock lock;
17  
    printWithMS("Starting profiler for " + threadToSample);
18  
    stop();
19  
    clear();
20  
    timer = doEvery_daemon(interval, r {
21  
      StackTraceElement[] stackTrace = threadToSample.getStackTrace();
22  
      if (skipSleeping && !isThreadRunnable_x(stackTrace)) ret;
23  
      
24  
      lock lock;
25  
      samples++;
26  
      stackTraceMultiSet.add(stackTraceToString(stackTrace));
27  
    });
28  
  }
29  
30  
  public  void stop aka close() {
31  
    lock lock;
32  
    if (timer != null) {
33  
      stopTimer(timer);
34  
      timer = null;
35  
      printWithMS("Stopped profiler for " + threadToSample);
36  
    }
37  
  }
38  
39  
  void clear() {
40  
    lock lock;
41  
    stackTraceMultiSet.clear();
42  
    samples = 0;
43  
  }
44  
45  
  MultiSet<S> results() {
46  
    lock lock;
47  
    ret cloneMultiSet(stackTraceMultiSet);
48  
  }
49  
50  
  MultiSet<S> stopAndGetResults() {
51  
    lock lock;
52  
    stop();
53  
    ret results();
54  
  }
55  
  
56  
  S renderFullResults(bool mostImportantLast) {
57  
    var traces = results();
58  
    int n = traces.size();
59  
    int percent = ratioToIntPercent(l(traces), samples);
60  
    ret (samples == 0 ? "Nothing sampled"
61  
      : percent + "% core activity [" + n2(samples, "sample") + " taken]") + "\n\n"
62  
      + joinMap(mostImportantLast ? traces.lowestFirst() : traces.highestFirst(), trace ->
63  
      traces.get(trace) + "/" + n + "\n" + trace + "\n\n"
64  
    );
65  
  }
66  
}

Author comment

Began life as a copy of #1035050

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036379
Snippet name: SingleThreadProfiler backup
Eternal ID of this version: #1036379/1
Text MD5: 30e2a0a81b8dbf7996314f1fed6d6e91
Transpilation MD5: 358b0e3c8f947590156274908926d18c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-30 11:50:40
Source code size: 1697 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 121 / 170
Referenced in: [show references]