1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.lang
.management
.ManagementFactory
;
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 import org
.argeo
.cms
.CmsException
;
13 * Background thread started by the {@link Kernel}, which gather statistics and
14 * monitor/control other processes.
16 class KernelThread
extends Thread
{
17 @SuppressWarnings("unused")
18 private final Kernel kernel
;
19 private final RepositoryStatisticsImpl repoStats
;
21 /** The smallest period of operation, in ms */
22 private final long PERIOD
= 60 * 1000l;
24 private final static long m
= 1000l * 1000l;
25 private final static long M
= 1024l * 1024l;
27 private boolean running
= true;
29 private Log kernelStatsLog
= LogFactory
.getLog("argeo.stats.kernel");
30 private Log nodeStatsLog
= LogFactory
.getLog("argeo.stats.node");
32 @SuppressWarnings("unused")
33 private long cycle
= 0l;
35 public KernelThread(Kernel kernel
) {
36 super(kernel
.threadGroup
, kernel
.getClass().getSimpleName());
38 this.repoStats
= kernel
.repository
.getRepositoryStatistics();
41 private void doSmallestPeriod() {
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()
49 .getSystemLoadAverage();
52 long uptime
= ManagementFactory
.getRuntimeMXBean().getUptime()
54 if (uptime
> 24 * 60) {
58 line
.append(uptime
).append(min ?
" min" : " h").append('\t');
59 line
.append(loadAvg
).append('\t').append(maxMem
).append('\t')
60 .append(totalMem
).append('\t').append(freeMem
).append('\t');
61 kernelStatsLog
.debug(line
);
64 if (nodeStatsLog
.isDebugEnabled()) {
65 File dataDir
= KernelUtils
.getOsgiInstanceDir();
66 long freeSpace
= dataDir
.getUsableSpace() / M
;
67 // File currentRoot = null;
68 // for (File root : File.listRoots()) {
69 // String rootPath = root.getAbsolutePath();
70 // if (dataDir.getAbsolutePath().startsWith(rootPath)) {
71 // if (currentRoot == null
72 // || (rootPath.length() > currentRoot.getPath()
74 // currentRoot = root;
78 // long totalSpace = currentRoot.getTotalSpace();
79 StringBuilder line
= new StringBuilder(128);
80 line
.append("§\t").append(freeSpace
)
81 .append(" MB left in " + dataDir
);
83 for (RepositoryStatistics
.Type type
: RepositoryStatistics
.Type
85 long[] vals
= repoStats
.getTimeSeries(type
).getValuePerMinute();
86 long val
= vals
[vals
.length
- 1];
87 line
.append(type
.name()).append('\t').append(val
).append('\n');
89 nodeStatsLog
.debug(line
);
95 final long periodNs
= PERIOD
* m
;
97 long beginNs
= System
.nanoTime();
100 long waitNs
= periodNs
- (System
.nanoTime() - beginNs
);
105 sleep(waitNs
/ m
, (int) (waitNs
% m
));
106 } catch (InterruptedException e
) {
113 synchronized void destroyAndJoin() {
119 } catch (InterruptedException e
) {
120 throw new CmsException("Kernel thread destruction was interrupted");