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