All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.gemini.engine.MemoryInfo Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 *
 *  * 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.flink.runtime.state.gemini.engine;

import org.apache.flink.runtime.state.gemini.engine.exceptions.GeminiRuntimeException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;

/**
 * Information for memory that can be used by DB.
 */
public class MemoryInfo {

	private static final Logger LOG = LoggerFactory.getLogger(MemoryInfo.class);

	/**
	 * Whether to use off-heap.
	 */
	private final boolean useOffHeap;

	/**
	 * Whether to use offheap for read.
	 */
	private final boolean useOffheapForRead;

	/**
	 * Size of heap that can be used.
	 */
	private final long totalHeapSize;

	/**
	 * Size of off-heap that can be used.
	 */
	private final long totalOffheapSize;

	public MemoryInfo(GConfiguration configuration) {
		this.useOffHeap = configuration.isUseOffheap();
		this.useOffheapForRead = configuration.isUseOffheapForRead();
		this.totalHeapSize = calculateHeapSize(configuration);
		this.totalOffheapSize = calculateOffheapSize(configuration);
		LOG.info(this.toString());
	}

	private long calculateHeapSize(GConfiguration configuration) {
		long heapSize = configuration.getHeapSize();
		if (heapSize > 0) {
			return heapSize;
		}

		long jvmHeapSize = getJvmHeapSize();
		return (long) (jvmHeapSize / configuration.getNumberSlots() * configuration.getMemoryRatio());
	}

	private long calculateOffheapSize(GConfiguration configuration) {
		long offheapSize = configuration.getOffheapSize();
		if (offheapSize > 0) {
			return offheapSize;
		}

		long jvmOffheapSize = getJvmDirectMemorySize();
		return (long) (jvmOffheapSize / configuration.getNumberSlots() * configuration.getMemoryRatio());
	}

	public boolean isUseOffHeap() {
		return useOffHeap;
	}

	public boolean isUseOffheapForRead() {
		return useOffheapForRead;
	}

	public long getTotalHeapSize() {
		return totalHeapSize;
	}

	public long getTotalOffheapSize() {
		return totalOffheapSize;
	}

	public static long getJvmHeapSize() {
		return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
	}

	public static long getJvmDirectMemorySize() {
		try {
			Class c = Class.forName("java.nio.Bits");
			Field maxMemory = c.getDeclaredField("maxMemory");
			maxMemory.setAccessible(true);
			return (Long) maxMemory.get(null);
		} catch (Exception e) {
			throw new GeminiRuntimeException(e);
		}
	}

	@Override
	public String toString() {
		return "MemoryInfo={" +
			"useOffheap=" + useOffHeap +
			", useOffheapForRead=" + useOffheapForRead +
			", totalHeapSize=" + totalHeapSize +
			", totalOffheapSize=" + totalOffheapSize +
			"}";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy