Skip to content

Commit

Permalink
add comment version
Browse files Browse the repository at this point in the history
  • Loading branch information
imbajin committed Oct 22, 2024
1 parent 73d38df commit cee7b40
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 164 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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 org.apache.hugegraph.store.node.metrics;

import static java.util.Locale.ENGLISH;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import lombok.Getter;

class ProcFileHandler {

// Cache duration in milliseconds
private static final long CACHE_DURATION_MS = 100;
// Singleton instances of ProcFileHandler
private static final Map<String, ProcFileHandler> handlerInstances = new HashMap<>();
private static final Object handlerLock = new Object();
// Cached data
private static final Map<Path, List<String>> cachedContent = new HashMap<>();
private static final Object contentLock = new Object();
// Base path for proc filesystem
private static final Path PROC_BASE_PATH = Paths.get("/proc", "self");
private final Path filePath;
private final boolean isOSSupported;
private long lastReadTimestamp = -1;

// Private constructor to initialize the handler
private ProcFileHandler(String entry) {
this(PROC_BASE_PATH, entry, false);
}

// Package-private constructor for testing
ProcFileHandler(Path base, String entry) {
this(base, entry, true);
}

// Private constructor with an OS support flag
private ProcFileHandler(Path base, String entry, boolean forceOSSupport) {
Objects.requireNonNull(base);
Objects.requireNonNull(entry);

this.filePath = base.resolve(entry);
this.isOSSupported = forceOSSupport ||
System.getProperty("os.name").toLowerCase(ENGLISH).startsWith("linux");
}

// Get an instance of ProcFileHandler
static ProcFileHandler getInstance(String key) {
Objects.requireNonNull(key);

synchronized (handlerLock) {
ProcFileHandler handler = handlerInstances.get(key);
if (handler == null) {
handler = new ProcFileHandler(key);
handlerInstances.put(key, handler);
}
return handler;
}
}

// Get the file path
Path getFilePath() {
return filePath;
}

// Read the proc file
ReadResult readFile() throws IOException {
return readFile(currentTimeMillis());
}

// Read the proc file with a specific time
ReadResult readFile(long currentTimeMillis) throws IOException {
synchronized (contentLock) {
final Path key = getFilePath().getFileName();

final ReadResult readResult;
if (lastReadTimestamp == -1 ||
lastReadTimestamp + CACHE_DURATION_MS < currentTimeMillis) {
final List<String> lines = readFilePath(filePath);
cacheContent(key, lines);
lastReadTimestamp = currentTimeMillis();
readResult = new ReadResult(lines, lastReadTimestamp);
} else {
readResult = new ReadResult(cachedContent.get(key), lastReadTimestamp);
}
return readResult;
}
}

// Read the content of the path
List<String> readFilePath(Path path) throws IOException {
Objects.requireNonNull(path);

if (!isOSSupported) {
return Collections.emptyList();
}
return Files.readAllLines(path);
}

// Cache the result
void cacheContent(Path key, List<String> lines) {
Objects.requireNonNull(key);
Objects.requireNonNull(lines);

cachedContent.put(key, lines);
}

// Get the current time in milliseconds
long currentTimeMillis() {
return System.currentTimeMillis();
}

// Result of reading the proc file
@Getter
static class ReadResult {

private final List<String> lines;
private final long readTime;

ReadResult(List<String> lines, long readTime) {
this.lines = Objects.requireNonNull(lines);
this.readTime = readTime;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.hugegraph.store.node.metrics;

import static org.apache.hugegraph.store.node.metrics.ProcfsReader.ReadResult;
import static org.apache.hugegraph.store.node.metrics.ProcFileHandler.ReadResult;

import java.io.IOException;
import java.util.Collection;
Expand All @@ -31,26 +31,27 @@ abstract class ProcfsRecord {

private final Object syncLock = new Object();

private final ProcfsReader fileReader;
private final ProcFileHandler fileReader;

private long lastProcessedTime = -1;

protected ProcfsRecord(ProcfsReader fileReader) {
protected ProcfsRecord(ProcFileHandler fileReader) {
this.fileReader = Objects.requireNonNull(fileReader);
}

protected final void gatherData() {
synchronized (syncLock) {
try {
final ReadResult readResult = fileReader.read();
if (readResult != null && (lastProcessedTime == -1 || lastProcessedTime != readResult.getReadTime())) {
final ReadResult readResult = fileReader.readFile();
if (readResult != null &&
(lastProcessedTime == -1 || lastProcessedTime != readResult.getReadTime())) {
clear();
process(readResult.getLines());
lastProcessedTime = readResult.getReadTime();
}
} catch (IOException e) {
clear();
logger.warn("Failed reading '" + fileReader.getEntryPath() + "'!", e);
logger.warn("Failed reading '" + fileReader.getFilePath() + "'!", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;

/**
* 2022/3/1
*
* @version 0.1.0
*/
public class ProcfsMetrics {

public final static String PREFIX = "process_memory";
Expand All @@ -47,19 +42,20 @@ private static void registerMeters() {
}

private static void registerProcessGauge() {
Gauge.builder(PREFIX + ".rss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.RSS))
.register(registry);
Gauge.builder(PREFIX + ".rss.bytes",
() -> smaps.getMetric(SystemMemoryStats.MetricKey.RSS)).register(registry);

Gauge.builder(PREFIX + ".pss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.PSS))
.register(registry);
Gauge.builder(PREFIX + ".pss.bytes",
() -> smaps.getMetric(SystemMemoryStats.MetricKey.PSS)).register(registry);

Gauge.builder(PREFIX + ".vss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.VSS))
.register(registry);
Gauge.builder(PREFIX + ".vss.bytes",
() -> smaps.getMetric(SystemMemoryStats.MetricKey.VSS)).register(registry);

Gauge.builder(PREFIX + ".swap.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.SWAP))
.register(registry);
Gauge.builder(PREFIX + ".swap.bytes",
() -> smaps.getMetric(SystemMemoryStats.MetricKey.SWAP)).register(registry);

Gauge.builder(PREFIX + ".swappss.bytes", () -> smaps.getMetric(SystemMemoryStats.KEY.SWAPPSS))
Gauge.builder(PREFIX + ".swappss.bytes",
() -> smaps.getMetric(SystemMemoryStats.MetricKey.SWAPPSS))
.register(registry);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class SystemMemoryStats extends ProcfsRecord {
private final Map<MetricKey, AtomicLong> metrics = new HashMap<>();

public SystemMemoryStats() {
super(ProcfsReader.getInstance("smaps"));
super(ProcFileHandler.getInstance("smaps"));
}

/* default */ SystemMemoryStats(ProcfsReader reader) {
/* default */ SystemMemoryStats(ProcFileHandler reader) {
super(reader);
}

Expand Down

0 comments on commit cee7b40

Please sign in to comment.