-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add linux memory metrics #50 * lookoutAuotConfiguration get extensions from beanFactory;(企业版 lookoutAuotConfiguration 会很早从beanfactory中获取,当时可能还没注入这些扩展组件)
- Loading branch information
Showing
27 changed files
with
311 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
.../lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/MemoryStatsMetricsImporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alipay.lookout.os.linux; | ||
|
||
import com.alipay.lookout.api.Gauge; | ||
import com.alipay.lookout.api.Id; | ||
import com.alipay.lookout.api.Registry; | ||
import com.alipay.lookout.api.composite.MixinMetric; | ||
import com.alipay.lookout.common.log.LookoutLoggerFactory; | ||
import com.alipay.lookout.os.CachedMetricsImporter; | ||
import com.alipay.lookout.os.utils.FileUtils; | ||
import org.slf4j.Logger; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* resolve memory metrics from "/proc/meminfo" | ||
* | ||
* @author: kevin.luy@antfin.com | ||
* 2019-03-12 17:30 | ||
**/ | ||
public class MemoryStatsMetricsImporter extends CachedMetricsImporter { | ||
|
||
private final Logger logger = LookoutLoggerFactory | ||
.getLogger(DiskUsageMetricsImporter.class); | ||
|
||
private static final String DEFAULT_FILE_PATH = "/proc/meminfo"; | ||
private static final String SPLIT = "\\s+"; | ||
private String filePath; | ||
private Memstats memstats; | ||
|
||
/** | ||
* default constructor | ||
*/ | ||
public MemoryStatsMetricsImporter() { | ||
this(DEFAULT_FILE_PATH, DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
/** | ||
* @param filePath filePath | ||
* @param timeout timeout | ||
* @param timeoutUnit timeoutUnit | ||
*/ | ||
public MemoryStatsMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) { | ||
super(timeout, timeoutUnit); | ||
this.filePath = filePath; | ||
memstats = new Memstats(); | ||
disable = !new File(filePath).exists(); | ||
if (!disable) | ||
loadIfNessesary(); | ||
} | ||
|
||
@Override | ||
protected void doRegister(Registry registry) { | ||
Id id = registry.createId("os.memory.stats"); | ||
MixinMetric mixin = registry.mixinMetric(id); | ||
|
||
mixin.gauge("total.bytes", new Gauge<Long>() { | ||
@Override | ||
public Long value() { | ||
loadIfNessesary(); | ||
return memstats.memTotal; | ||
} | ||
}); | ||
|
||
mixin.gauge("free.bytes", new Gauge<Long>() { | ||
@Override | ||
public Long value() { | ||
loadIfNessesary(); | ||
return memstats.memFree; | ||
} | ||
}); | ||
mixin.gauge("buffers.bytes", new Gauge<Long>() { | ||
@Override | ||
public Long value() { | ||
loadIfNessesary(); | ||
return memstats.buffers; | ||
} | ||
}); | ||
mixin.gauge("cached.bytes", new Gauge<Long>() { | ||
@Override | ||
public Long value() { | ||
loadIfNessesary(); | ||
return memstats.cached; | ||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
protected void loadValues() { | ||
try { | ||
List<String> lines = FileUtils.readFileAsStringArray(filePath); | ||
Memstats ms = new Memstats(); | ||
int i = 0; | ||
for (String line : lines) { | ||
if (i == 4) | ||
break; | ||
String[] infos = line.split(SPLIT); | ||
if (infos == null) { | ||
continue; | ||
} | ||
if ("MemTotal:".equals(infos[0])) { | ||
ms.memTotal = Long.parseLong(infos[1]) * 1024; | ||
i++; | ||
continue; | ||
} | ||
if ("MemFree:".equals(infos[0])) { | ||
ms.memFree = Long.parseLong(infos[1]) * 1024; | ||
i++; | ||
continue; | ||
} | ||
if ("Buffers:".equals(infos[0])) { | ||
ms.buffers = Long.parseLong(infos[1]) * 1024; | ||
i++; | ||
continue; | ||
} | ||
if ("Cached:".equals(infos[0])) { | ||
ms.cached = Long.parseLong(infos[1]) * 1024; | ||
i++; | ||
continue; | ||
} | ||
} | ||
memstats = ms; | ||
} catch (Exception e) { | ||
logger.debug("warning,can't parse line at /proc/meminfo", e.getMessage()); | ||
} | ||
} | ||
|
||
private class Memstats { | ||
long memTotal; | ||
long memFree; | ||
long cached; | ||
long buffers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...kout-ext-os/src/test/java/com/alipay/lookout/os/linux/MemoryStatsMetricsImporterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alipay.lookout.os.linux; | ||
|
||
import com.alipay.lookout.api.Measurement; | ||
import com.alipay.lookout.api.Metric; | ||
import com.alipay.lookout.api.Registry; | ||
import com.alipay.lookout.core.DefaultRegistry; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author: kevin.luy@antfin.com | ||
* @create: 2019-03-12 21:40 | ||
**/ | ||
public class MemoryStatsMetricsImporterTest { | ||
|
||
@Test | ||
public void test() { | ||
Registry registry = new DefaultRegistry(); | ||
MemoryStatsMetricsImporter memoryStatsMetricsImporter = new MemoryStatsMetricsImporter( | ||
"src/test/resources/proc_meminfo", 1000, TimeUnit.MILLISECONDS); | ||
memoryStatsMetricsImporter.register(registry); | ||
Iterator<Metric> iterator = registry.iterator(); | ||
while (iterator.hasNext()) { | ||
Metric metric = iterator.next(); | ||
Collection<Measurement> measurements = metric.measure().measurements(); | ||
Assert.assertEquals(measurements.size(), 4); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
MemTotal: 4106756 kB | ||
MemFree: 51004 kB | ||
Buffers: 495632 kB | ||
Cached: 1691248 kB | ||
SwapCached: 0 kB | ||
Active: 2799008 kB | ||
Inactive: 1047516 kB | ||
Active(anon): 1393568 kB | ||
Inactive(anon): 286376 kB | ||
Active(file): 1405440 kB | ||
Inactive(file): 761140 kB | ||
Unevictable: 0 kB | ||
Mlocked: 0 kB | ||
SwapTotal: 2097144 kB | ||
SwapFree: 2097144 kB | ||
Dirty: 660 kB | ||
Writeback: 0 kB | ||
AnonPages: 1658804 kB | ||
Mapped: 73820 kB | ||
Shmem: 21180 kB | ||
Slab: 127452 kB | ||
SReclaimable: 99996 kB | ||
SUnreclaim: 27456 kB | ||
KernelStack: 5928 kB | ||
PageTables: 12424 kB | ||
NFS_Unstable: 0 kB | ||
Bounce: 0 kB | ||
WritebackTmp: 0 kB | ||
CommitLimit: 4150520 kB | ||
Committed_AS: 5889456 kB | ||
VmallocTotal: 34359738367 kB | ||
VmallocUsed: 17652 kB | ||
VmallocChunk: 34359717664 kB | ||
HardwareCorrupted: 0 kB | ||
AnonHugePages: 0 kB | ||
HugePages_Total: 0 | ||
HugePages_Free: 0 | ||
HugePages_Rsvd: 0 | ||
HugePages_Surp: 0 | ||
Hugepagesize: 2048 kB | ||
DirectMap4k: 4194304 kB | ||
DirectMap2M: 0 kB |
Oops, something went wrong.