]> git.argeo.org Git - lgpl/argeo-commons.git/blob - StatisticsThread.java
b5ec05005f9f760e2d4ec53f55201ab075eed6f4
[lgpl/argeo-commons.git] / StatisticsThread.java
1 package org.argeo.cms.jcr.internal;
2
3 import java.io.File;
4 import java.lang.management.ManagementFactory;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.jackrabbit.api.stats.RepositoryStatistics;
9 import org.apache.jackrabbit.stats.RepositoryStatisticsImpl;
10
11 /**
12 * Background thread started by the kernel, which gather statistics and
13 * monitor/control other processes.
14 */
15 public class StatisticsThread extends Thread {
16 private final static Log log = LogFactory.getLog(StatisticsThread.class);
17
18 private RepositoryStatisticsImpl repoStats;
19
20 /** The smallest period of operation, in ms */
21 private final long PERIOD = 60 * 1000l;
22 /** One ms in ns */
23 private final static long m = 1000l * 1000l;
24 private final static long M = 1024l * 1024l;
25
26 private boolean running = true;
27
28 private Log kernelStatsLog = LogFactory.getLog("argeo.stats.kernel");
29 private Log nodeStatsLog = LogFactory.getLog("argeo.stats.node");
30
31 @SuppressWarnings("unused")
32 private long cycle = 0l;
33
34 public StatisticsThread(String name) {
35 super(name);
36 }
37
38 private void doSmallestPeriod() {
39 // Clean expired sessions
40 // FIXME re-enable it in CMS
41 //CmsSessionImpl.closeInvalidSessions();
42
43 if (kernelStatsLog.isDebugEnabled()) {
44 StringBuilder line = new StringBuilder(64);
45 line.append(\t");
46 long freeMem = Runtime.getRuntime().freeMemory() / M;
47 long totalMem = Runtime.getRuntime().totalMemory() / M;
48 long maxMem = Runtime.getRuntime().maxMemory() / M;
49 double loadAvg = ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();
50 // in min
51 boolean min = true;
52 long uptime = ManagementFactory.getRuntimeMXBean().getUptime() / (1000 * 60);
53 if (uptime > 24 * 60) {
54 min = false;
55 uptime = uptime / 60;
56 }
57 line.append(uptime).append(min ? " min" : " h").append('\t');
58 line.append(loadAvg).append('\t').append(maxMem).append('\t').append(totalMem).append('\t').append(freeMem)
59 .append('\t');
60 kernelStatsLog.debug(line);
61 }
62
63 if (nodeStatsLog.isDebugEnabled()) {
64 File dataDir = KernelUtils.getOsgiInstanceDir();
65 long freeSpace = dataDir.getUsableSpace() / M;
66 // File currentRoot = null;
67 // for (File root : File.listRoots()) {
68 // String rootPath = root.getAbsolutePath();
69 // if (dataDir.getAbsolutePath().startsWith(rootPath)) {
70 // if (currentRoot == null
71 // || (rootPath.length() > currentRoot.getPath()
72 // .length())) {
73 // currentRoot = root;
74 // }
75 // }
76 // }
77 // long totalSpace = currentRoot.getTotalSpace();
78 StringBuilder line = new StringBuilder(128);
79 line.append(\t").append(freeSpace).append(" MB left in " + dataDir);
80 line.append('\n');
81 if (repoStats != null)
82 for (RepositoryStatistics.Type type : RepositoryStatistics.Type.values()) {
83 long[] vals = repoStats.getTimeSeries(type).getValuePerMinute();
84 long val = vals[vals.length - 1];
85 line.append(type.name()).append('\t').append(val).append('\n');
86 }
87 nodeStatsLog.debug(line);
88 }
89 }
90
91 @Override
92 public void run() {
93 if (log.isTraceEnabled())
94 log.trace("Kernel thread started.");
95 final long periodNs = PERIOD * m;
96 while (running) {
97 long beginNs = System.nanoTime();
98 doSmallestPeriod();
99
100 long waitNs = periodNs - (System.nanoTime() - beginNs);
101 if (waitNs < 0)
102 continue;
103 // wait
104 try {
105 sleep(waitNs / m, (int) (waitNs % m));
106 } catch (InterruptedException e) {
107 // silent
108 }
109 cycle++;
110 }
111 }
112
113 public synchronized void destroyAndJoin() {
114 running = false;
115 notifyAll();
116 // interrupt();
117 // try {
118 // join(PERIOD * 2);
119 // } catch (InterruptedException e) {
120 // // throw new CmsException("Kernel thread destruction was interrupted");
121 // log.error("Kernel thread destruction was interrupted", e);
122 // }
123 }
124 }