`
cloudeagle_bupt
  • 浏览: 541165 次
文章分类
社区版块
存档分类
最新评论

MappedByteBuffer共享内存

 
阅读更多

写入

package shareMemory;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hama.bsp.Combiner;
import org.apache.hama.graph.GraphJobMessage;
import org.apache.hama.util.ReflectionUtils;
import org.apache.hama.util.WritableUtils;

/**
 * 模拟将同节点内部消息写在本地共享内存中以加速通信
 * 
 * @author qiangliu
 * 
 */
public class ShareMemory {

	public MappedByteBuffer mbb;
	public static int bufferSize = 50 * 1024 * 1024;  //内存
	RandomAccessFile raf; // 共享内存对应文件
//	public static String shareFile = "/opt/hama-0.7.1/shareMemory/shareMemory.txt";
	public static String shareFile = "F:\\test\\shareMemory\\sharememory.txt";

	ConcurrentHashMap<IntWritable, VertexMessage> vertexMessages = new ConcurrentHashMap<IntWritable, VertexMessage>(); //消息容器
	private Combiner<Writable> combiner;
	  
	ShareMemory() {
		try {
			raf = new RandomAccessFile(shareFile, "rw");
		    final String combinerName = "org.apache.hama.examples.PageRank$PagerankCombiner"; 
//		    Class com = Class.forName(combinerName) ;
			combiner = (Combiner<Writable>) ReflectionUtils
		            .newInstance(combinerName);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

  /**
   * mock 10000 Pagerank消息,消息在写入共享内存时,发送消息时先保存在链表中,写入时用WritableUtils序列化后按字节写入
   */
	public void generateMessages() {
  	    //method 1 : 使用WritableUtils,后面建议改成在最后写入时再序列化,前面直接combine! 但是会存在当到同一个id的值很多且不能combine时就不好处理了!
		for(int j = 1; j<3 ; j++) {  //模拟3个super-step
			for(int i = 0 ;i<1000; i++) {
				DoubleWritable value = new DoubleWritable(i/j) ;
				VertexMessage vMessage = new VertexMessage(new IntWritable(i), value) ;
				
				if (combiner != null && vertexMessages.get(vMessage.getVertexId())!=null ) {
					DoubleWritable combined = (DoubleWritable) combiner.combine(getIterableMessages(value)) ;
			        vMessage.setVertexVertexValue(combined) ; //更改合并后的值
			        vertexMessages.put(vMessage.getVertexId(), vMessage);
				}  
					
				vertexMessages.put(vMessage.getVertexId(), vMessage) ;
			}
		}
	}

	public void close() {
		try {
			mbb.clear() ;
			raf.close() ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		 ShareMemory shm = new ShareMemory() ;
		 long startTime = System.currentTimeMillis() ;
		 shm.generateMessages() ;
		 System.out.println("Generate Messages last :" + (System.currentTimeMillis() - startTime));

		 startTime = System.currentTimeMillis() ;
		 ExecutorService pool = Executors.newCachedThreadPool() ;   
		 CompletionService<Boolean> exchangeResult = new ExecutorCompletionService<Boolean>(pool); 
		 int destSize = 0 ;
		 destSize++ ;
		 exchangeResult.submit(shm.new MesssageShareSender()) ;
	     int count = 0 ;  
	     while(count < destSize) {  
	         Future<Boolean> f = exchangeResult.poll();  
	         if(f == null)  continue ;
	         count++;  
	     }
		 System.out.println("Send Messages last :" + (System.currentTimeMillis() - startTime));

		 pool.shutdown() ;
		 shm.close() ;
	 }
	
	  public static Iterable<Writable> getIterableMessages(final Writable vertexValue) {

		    return new Iterable<Writable>() {
		      Writable value ;

		      @Override
		      public Iterator<Writable> iterator() {
		    	 value = vertexValue ;
		    	
		        return new Iterator<Writable>() {
                  int index = 1 ;
		        	
		          @Override
		          public boolean hasNext() {
		            return (index == 1) ? true : false;
		          }

		          @Override
		          public Writable next() {
		            index--;
		            return vertexValue;
		          }

		          @Override
		          public void remove() {
		          }
		        };
		      }
		    };
		  }
	
   /**
    * 同节点消息产生后,多线程发送给共享内存.两个问题: 1.为什么要用GraphJobMessage? 2. 直接写入快还是用流快?
    * @author Administrator
    */
	class MesssageShareSender implements  Callable<Boolean>{

		@Override
		public Boolean call() throws Exception {
			try {
				FileChannel fc = raf.getChannel();
				FileLock flock = fc.tryLock();
				
				if(flock==null) {
					Thread.sleep(10) ;
				} else {
					mbb = fc.map(MapMode.READ_WRITE, 0, ShareMemory.bufferSize);  //因为写之前不知道需要映射多大共享内存,暂定50M
					mbb.position(4) ; //预留一个int长度(4个字节)作为文件长度
					int totalLength  = 4 ; //最大2G
					
					//模拟发送,最好像v0.6.4版本中,为每个任务的数据设置一个目录,所有发送到该任务的共享内存数据全部映射到这里!
				    Iterator<Entry<IntWritable, VertexMessage>> it = vertexMessages.entrySet()
				            .iterator();
			        while (it.hasNext()) {
			          Entry<IntWritable, VertexMessage> e = it.next();
			          it.remove();
			          byte[] serialized = WritableUtils.serialize(e.getValue().getVertexVertexValue()) ;
			          GraphJobMessage gjm = new GraphJobMessage(e.getValue().getVertexId(), serialized) ;
			          gjm.setVertexId(e.getKey());
			          gjm.setFlag(GraphJobMessage.VERTEX_FLAG);
			          byte[] message = WritableUtils.serialize(gjm) ;
			          int msgLen = message.length ;
 			          mbb.putInt(msgLen) ;                   //这样快还是写入流读取,比如写到流里, DataOutput output output.toByteArray()
// 			          System.out.println("Position : " + mbb.position());
			          mbb.put(message);
//			          System.out.println("Position : " + mbb.position());
			          totalLength = totalLength + msgLen +4 ;
			        }
			        mbb.putInt(0,mbb.position()) ; //补写长度
//					System.out.println(" IsLoaded: " + mbb.isLoaded() + " Length: "+ totalLength +" Position: " + mbb.position());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}
	}
}


读取

package shareMemory;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.FileChannel.MapMode;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hama.graph.GraphJobMessage;
import org.apache.hama.graph.GraphJobRunner;
import org.apache.hama.util.WritableUtils;


public class ReadingProcess {
	byte[] buffer ; //缓存
	public MappedByteBuffer mbb;
	RandomAccessFile raf; // 共享内存对应文件
	ConcurrentHashMap<IntWritable, VertexMessage> vertexMessages = new ConcurrentHashMap<IntWritable, VertexMessage>(); // 消息容器
	
	public ReadingProcess() {
		try {
			raf = new RandomAccessFile(ShareMemory.shareFile, "rw");
			FileChannel fc = raf.getChannel();
			mbb = fc.map(MapMode.READ_ONLY, 0, fc.size()); // 映射的共享内存
			mbb.load() ; // 预加载进内存
			GraphJobRunner.VERTEX_ID_CLASS = IntWritable.class ;
			GraphJobRunner.VERTEX_VALUE_CLASS = DoubleWritable.class ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			mbb.clear() ; //清空mbb
			raf.close() ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void readData() {
		try {
			FileChannel fc = raf.getChannel();
			FileLock flock = fc.tryLock();
			while(flock==null) {           //轮询等待读取消息
				Thread.sleep(10) ;
			} 
			System.out.println("IsLoaded: " + mbb.isLoaded() +" position:"+ mbb.position());
			int fileLength = mbb.getInt() ;
//			buffer = new byte[fileLength] ;
//			mbb.get(buffer) ; //本地消息缓存, 是否一次性读出?
			
			while(mbb.position() < fileLength ) {
				int msgLength = mbb.getInt() ;
				if(msgLength>0) {
//					System.out.println("Position : " + mbb.position());
					byte[] message = new byte[msgLength] ;
					mbb.get(message) ;
//					System.out.println("Position : " + mbb.position());
					GraphJobMessage gjm = new GraphJobMessage() ;
					WritableUtils.deserialize(message, gjm) ;
					if (!vertexMessages.containsKey(gjm.getVertexId())) {
						DoubleWritable vertexValue = new DoubleWritable() ;
						WritableUtils.deserialize(gjm.getValuesBytes(), vertexValue) ;
						IntWritable vertexId = (IntWritable) gjm.getVertexId() ;
						vertexMessages.put(vertexId, new VertexMessage(vertexId, vertexValue)) ;
	 				} else {
	 					System.out.println("Combine Error!");
	 				}
				}
			}
//			System.out.println("test ");
			close() ;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 	public static void main(String[] args) {
 		ReadingProcess rp = new ReadingProcess();
 		rp.readData() ;
	}
}


分享到:
评论

相关推荐

    commons-mmf.rar_java nio_java共享内存_共享内存

    java的共享内存管理.基于MMF设计。封装了java.nio.MappedByteBuffer.在大流量实时业务系统时,可以极大的提高处理效率

    深入浅出MappedByteBuffer.pdf

    深入浅出MappedByteBuffer

    Bug ID 4724038 (fs) Add unmap method to MappedByteBuffer

    Bug ID 4724038 (fs) Add unmap method to MappedByteBuffer

    mmfinvoker:简单的进程间 java 请求-响应库

    mmfinvoker 这是一个简单的 java 库,它使用 nio.MappedByteBuffer 在内存映射文件上实现请求/响应功能。

    文件分割和合并(您的文件大的不能传输怎么办?)

    本人初学c++,写了一个小软件,能把大文件分割问小文件,然后可以统国网络传输,到了网络另一端,再用此软件拼接! 希望用过的人能提宝贵意见! 13521825644 qq 362192302

    j2se项目源码及介绍_last指令

    返回说明 @return MappedByteBuffer 内存映射缓冲。 异常说明 throws 考虑异常 流程原理 调用实例 函数原型 private void readLog(MappedByteBuffer buffer, Vector&lt;LogRecord&gt; logins,Vector&lt;LogRecord&gt; ...

    读取文件数据并解析成bean实体类

    很多时候需要文件做数据交互,接收到文件后需要对文件解析成bean实体类,这里提供的是工具类,任意文件转任意实体都可以,只要简单的配置一下Class类,很实用

    jdk-14_linux-x64_bin.rpm

    非易失性映射的字节缓冲将添加新的 JDK 特定文件映射模式,该模式允许 FileChannel API 用于创建引用非易失性内存(NVM)的 MappedByteBuffer 实例。 358:Helpful NullPointerExceptions 改进 ...

    【密码:5261】Oracle官网下载64位-JDK14

    jdk14新特性:改进NullPointerExceptions,通过准确描述哪些变量为null...非易失性映射的字节缓冲将添加新的JDK特定文件映射模式,该模式允许FileChannel API用于创建引用非易失性内存(NVM)的MappedByteBuffer实例。

    mmf4j:MemoryMappedFiles4Java

    该库旨在将内存映射文件引入Java。 与已经存在的MappedByteBuffer相比,目标是更好地控制创建,修改和销毁。 它试图统一在不同操作系统上使用此类映射的接口,这意味着许多细节无法实现。 此外,在某些情况下,您...

    sambox:一个PDFBox分支,打算用作Sejda和PDFsam的PDF处理程序

    当您只需要文档的一部分时(例如,您只需要信息字典或文档的页数),这将使内存占用量降至最低。 要读取的多个I / O实现。 SAMBox使用允许使用基于java.nio.channels.FileChannel , java.io.InputStream和java.nio...

    百度地图开发java源码-inertiaSearch:挑战赛

    百度地图开发java源码 tmp #inertiaSearch 2016年写的代码,现在觉得思路有很多提升的地方,但是毕竟努力过,还是贴在Readme.md 里面 ...对于原始的数据文件做内存映射,并做对应索引,所有索引做hash

    Android代码-BitMap

    a pratise of bigdata sorting,use some common util or class,like File,FileOutputStream,RandomAccessFile,HashMap,BufferedOutputStream,ByteBuffer,MappedByteBuffer,FileInputStream. as a newer of ...

    CsvReader:CsvReader

    the performance improvement is not so much as against using MappedByteBuffer. -&gt; Otherwise I will totally use MappedByteBuffer 我无法在symbol.txt上进行验证。 单元测试:我找不到足够的时间来运行所有...

    txt文档阅读器

    //MappedByteBuffer 将文件直接映射到内存 private int m_mbBufLen = 0; private int m_mbBufBegin = 0; private int m_mbBufEnd = 0; private String m_strCharsetName = "gbk";//文本格式 private Bitmap m_...

    javaredis源码-jredis-master:java实现redis

    java redis源码杰里迪斯 Java的redis实现,与redis服务器相同。 特征 ...MappedByteBuffer 2.fixed unit size ,head 4 byte write in last item position, and then each item write in 4 byte with i

    ip地址库 很全的库

    // 内存映射文件 private MappedByteBuffer mbb; // 单一模式实例 private static volatile IPSeeker instance = null; // 起始地区的开始和结束的绝对偏移 private long ipBegin, ipEnd; // 为提高...

    java8源码-netty-learn:这是一个用于netty学习的工程

    MappedByteBuffer DirectByteBuffer HeapByteBuffer ShortBuffer IntBuffer LongBuffer FloatBuffer DoubleBuffer CharBuffer Selector选择器 Selector的作用就是配合一个线程来管理多个channel,获取这些channel上...

    Android渠道打包工具packer-ng-plugin.zip

    }读取ZIP文件注释,有两个版本的实现,这里使用的是 RandomAccessFile ,另一个版本使用的是 MappedByteBuffer ,经过测试,对于特别长的注释,使用内存映射文件读取性能要稍微好一些,对于特别短的注释(比如渠道名...

Global site tag (gtag.js) - Google Analytics