1 package org
.argeo
.cms
.jcr
.internal
;
4 import java
.lang
.management
.ManagementFactory
;
6 import org
.apache
.jackrabbit
.api
.stats
.RepositoryStatistics
;
7 import org
.apache
.jackrabbit
.stats
.RepositoryStatisticsImpl
;
8 import org
.argeo
.api
.cms
.CmsLog
;
11 * Background thread started by the kernel, which gather statistics and
12 * monitor/control other processes.
14 public class StatisticsThread
extends Thread
{
15 private final static CmsLog log
= CmsLog
.getLog(StatisticsThread
.class);
17 private RepositoryStatisticsImpl repoStats
;
19 /** The smallest period of operation, in ms */
20 private final long PERIOD
= 60 * 1000l;
22 private final static long m
= 1000l * 1000l;
23 private final static long M
= 1024l * 1024l;
25 private boolean running
= true;
27 private CmsLog kernelStatsLog
= CmsLog
.getLog("argeo.stats.kernel");
28 private CmsLog nodeStatsLog
= CmsLog
.getLog("argeo.stats.node");
30 @SuppressWarnings("unused")
31 private long cycle
= 0l;
33 public StatisticsThread(String name
) {
37 private void doSmallestPeriod() {
38 // Clean expired sessions
39 // FIXME re-enable it in CMS
40 //CmsSessionImpl.closeInvalidSessions();
42 if (kernelStatsLog
.isDebugEnabled()) {
43 StringBuilder line
= new StringBuilder(64);
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();
51 long uptime
= ManagementFactory
.getRuntimeMXBean().getUptime() / (1000 * 60);
52 if (uptime
> 24 * 60) {
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
)
59 kernelStatsLog
.debug(line
);
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()
72 // currentRoot = root;
76 // long totalSpace = currentRoot.getTotalSpace();
77 StringBuilder line
= new StringBuilder(128);
78 line
.append("§\t").append(freeSpace
).append(" MB left in " + dataDir
);
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');
86 nodeStatsLog
.debug(line
);
92 if (log
.isTraceEnabled())
93 log
.trace("Kernel thread started.");
94 final long periodNs
= PERIOD
* m
;
96 long beginNs
= System
.nanoTime();
99 long waitNs
= periodNs
- (System
.nanoTime() - beginNs
);
104 sleep(waitNs
/ m
, (int) (waitNs
% m
));
105 } catch (InterruptedException e
) {
112 public synchronized void destroyAndJoin() {
118 // } catch (InterruptedException e) {
119 // // throw new CmsException("Kernel thread destruction was interrupted");
120 // log.error("Kernel thread destruction was interrupted", e);