From a8386a00238d13b282abb5884ee51d0cb30e0361 Mon Sep 17 00:00:00 2001 From: Samuel Lepetit Date: Tue, 12 Jun 2012 21:32:10 +0200 Subject: [PATCH] Add File support and an example to cover it. --- CMakeLists.txt | 18 ++++++---- examples/io/IO.java | 36 +++++++++++++++++++ examples/io/Node.java | 59 ++++++++++++++++++++++++++++++ examples/io/storage.xml | 58 ++++++++++++++++++++++++++++++ org/simgrid/msg/File.java | 66 ++++++++++++++++++++++++++++++++++ src/jmsg_file.c | 76 +++++++++++++++++++++++++++++++++++++++ src/jmsg_file.h | 50 ++++++++++++++++++++++++++ 7 files changed, 357 insertions(+), 6 deletions(-) create mode 100644 examples/io/IO.java create mode 100644 examples/io/Node.java create mode 100644 examples/io/storage.xml create mode 100644 org/simgrid/msg/File.java create mode 100644 src/jmsg_file.c create mode 100644 src/jmsg_file.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 327131ae71..8f032edf95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,22 +82,25 @@ set(JMSG_C_SRC src/jmsg.h src/jmsg_comm.c src/jmsg_comm.h + src/jmsg_file.c + src/jmsg_file.h src/jmsg_host.c src/jmsg_host.h src/jmsg_process.c src/jmsg_process.h - src/jmsg_task.c - src/jmsg_task.h - src/jmsg_synchro.c - src/jmsg_synchro.h src/jmsg_rngstream.c src/jmsg_rngstream.h + src/jmsg_synchro.c + src/jmsg_synchro.h + src/jmsg_task.c + src/jmsg_task.h ) set(JMSG_JAVA_SRC org/simgrid/msg/Host.java org/simgrid/msg/HostFailureException.java org/simgrid/msg/HostNotFoundException.java + org/simgrid/msg/File.java org/simgrid/msg/JniException.java org/simgrid/msg/Msg.java org/simgrid/msg/MsgException.java @@ -139,6 +142,8 @@ set(JAVA_EXAMPLES examples/commTime/Master.java examples/commTime/Slave.java examples/commTime/CommTimeTest.java + examples/io/IO.java + examples/io/Node.java examples/masterslave/FinalizeTask.java examples/masterslave/Forwarder.java examples/masterslave/Master.java @@ -267,8 +272,9 @@ add_custom_command( COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/bittorrent/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/chord/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/commTime/*.java - COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/masterslave/*.java - COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/master_slave_bypass/*.java + COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/io/*.java + COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/masterslave/*.java + COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/master_slave_bypass/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/master_slave_kill/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/migration/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/mutualExclusion/centralized/*.java diff --git a/examples/io/IO.java b/examples/io/IO.java new file mode 100644 index 0000000000..6a6674fb8f --- /dev/null +++ b/examples/io/IO.java @@ -0,0 +1,36 @@ +/* + * 2012. The SimGrid Team. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. + */ +package io; + +import org.simgrid.msg.Host; +import org.simgrid.msg.Msg; +import org.simgrid.msg.MsgException; +/** + * This example demonstrates of how to use the other + * kind of resources, such as disk or GPU. These resources are quite + * experimental for now, but here we go anyway. + */ +public class IO { + public static void main(String[] args) throws MsgException { + Msg.init(args); + if(args.length < 1) { + Msg.info("Usage : IO platform_file "); + Msg.info("example : IO platform.xml "); + System.exit(1); + } + Msg.createEnvironment(args[0]); + + Host[] hosts = Host.all(); + + Msg.info("Number of hosts:" + hosts.length); + for (int i = 0; i < hosts.length && i < 4; i++) { + new io.Node(hosts[i],i).start(); + } + + Msg.run(); + } +} \ No newline at end of file diff --git a/examples/io/Node.java b/examples/io/Node.java new file mode 100644 index 0000000000..5afc600bcc --- /dev/null +++ b/examples/io/Node.java @@ -0,0 +1,59 @@ +/* + * 2012. The SimGrid Team. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. + */ +package io; + +import org.simgrid.msg.File; +import org.simgrid.msg.Host; +import org.simgrid.msg.HostNotFoundException; +import org.simgrid.msg.Msg; +import org.simgrid.msg.MsgException; +import org.simgrid.msg.Process; + +public class Node extends Process { + private static String FILENAME1 = "/home/user/Install/simgrid/doc/simgrid/examples/platforms/g5k.xml"; + private static String FILENAME2 = "/home/user/Install/simgrid/doc/simgrid/examples/platforms/One_cluster_no_backbone.xml"; + private static String FILENAME3 = "/home/user/Install/simgrid/doc/simgrid/examples/platforms/g5k_cabinets.xml"; + private static String FILENAME4 = "/home/user/Install/simgrid/doc/simgrid/examples/platforms/nancy.xml"; + + protected int number; + + public Node(Host host, int number) throws HostNotFoundException { + super(host, Integer.toString(number), null); + this.number = number; + } + public void main(String[] args) throws MsgException { + String mount = "C:"; + String filename; + switch (number) { + case 0: + filename = FILENAME1; + break; + case 1: + filename = FILENAME2; + break; + case 2: + filename = FILENAME3; + break; + case 3: + filename = FILENAME4; + break; + default: + filename = FILENAME1; + } + Msg.info("Open file " + filename); + File file = new File(mount,filename, "rw"); + + long read = file.read(mount,10000000,1); + Msg.info("Having read " + read + " on " + filename); + + long write = file.read(mount,100000,1); + Msg.info("Having write " + write + " on " + filename); + + read = file.read(mount,10000000,1); + Msg.info("Having read " + read + " on " + filename); + } +} \ No newline at end of file diff --git a/examples/io/storage.xml b/examples/io/storage.xml new file mode 100644 index 0000000000..58fca8974e --- /dev/null +++ b/examples/io/storage.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org/simgrid/msg/File.java b/org/simgrid/msg/File.java new file mode 100644 index 0000000000..0f85eb4fa9 --- /dev/null +++ b/org/simgrid/msg/File.java @@ -0,0 +1,66 @@ +package org.simgrid.msg; +/** +* Copyright 2012 The SimGrid team. All right reserved. +* +* This program is free software; you can redistribute +* it and/or modify it under the terms of the license +* (GNU LGPL) which comes with this package. +* +*/ + +public class File { + protected String storage; + /** + * Represents the bind between the java comm and the + * native C comm. You must never access it, since it is + * automatically set. + */ + public long bind = 0; + /** + * Constructor, opens the file. + * @param storage is the name where you can find the stream + * @param path is the file location on the storage + * @param mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.): r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. + */ + public File(String storage, String path, String mode) { + this.storage = storage; + open(storage, path, mode); + } + protected void finalize() { + close(storage); + } + /** + * Opens the file whose name is the string pointed to by path. + * @param storage is the name where you can find the stream + * @param path is the file location on the storage + * @param mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.): r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. + */ + protected native void open(String storage, String path, String mode); + /** + * Read elements of a file. + * @param storage is the name where you can find the stream + * @param size of each element + * @param nMemb is the number of elements of data to write + */ + public native long read(String storage, long size, long nMemb); + /** + * Write elements into a file. + * @param storage is the name where you can find the stream + * @param size of each element + * @param nMemb is the number of elements of data to write + */ + public native long write(String storage, long size, long nMemb); + /** + * Close the file. + * @param storage is the name where you can find the stream + */ + public native void close(String storage); + + /** + * Class initializer, to initialize various JNI stuff + */ + public static native void nativeInit(); + static { + nativeInit(); + } +} \ No newline at end of file diff --git a/src/jmsg_file.c b/src/jmsg_file.c new file mode 100644 index 0000000000..3695fb8379 --- /dev/null +++ b/src/jmsg_file.c @@ -0,0 +1,76 @@ +/* Functions related to the java file API. */ +/* Copyright (c) 2012. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ +#include "jmsg_file.h" +#include "jxbt_utilities.h" +void jfile_bind(JNIEnv *env, jobject jfile, msg_file_t stream) { + (*env)->SetLongField(env, jfile, jfile_field_bind, (jlong) (long) (stream)); +} + +msg_file_t jfile_get_native(JNIEnv *env, jobject jfile) { + msg_file_t file = (msg_file_t)(*env)->GetLongField(env, jfile, jfile_field_bind); + return file; +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_File_nativeInit(JNIEnv *env, jclass cls) { + jclass class_File = (*env)->FindClass(env, "org/simgrid/msg/File"); + jfile_field_bind = jxbt_get_jfield(env , class_File, "bind", "J"); + xbt_assert((jfile_field_bind != NULL), "Can't find \"bind\" field in File class."); +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_File_open(JNIEnv *env, jobject jfile, jobject jstorage, jobject jpath, jobject jmode) { + + const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0); + const char *path = (*env)->GetStringUTFChars(env, jpath, 0); + const char *mode = (*env)->GetStringUTFChars(env, jmode, 0); + + msg_file_t file = MSG_file_open(storage, path, mode); + + jfile_bind(env, jfile, file); + + (*env)->ReleaseStringUTFChars(env, jstorage, storage); + (*env)->ReleaseStringUTFChars(env, jpath, path); + (*env)->ReleaseStringUTFChars(env, jmode, mode); +} +JNIEXPORT jlong JNICALL +Java_org_simgrid_msg_File_read(JNIEnv *env, jobject jfile, jobject jstorage, jlong jsize, jlong jnmemb) { + msg_file_t file = jfile_get_native(env, jfile); + + const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0); + + size_t n = MSG_file_read(storage, NULL,(size_t)jsize, (size_t)jnmemb, file); + + (*env)->ReleaseStringUTFChars(env, jstorage, storage); + + return (jlong)n; +} + +JNIEXPORT jlong JNICALL +Java_org_simgrid_msg_File_write(JNIEnv *env, jobject jfile, jobject jstorage, jlong jsize, jlong jnmemb) { + msg_file_t file = jfile_get_native(env, jfile); + + const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0); + + size_t n = MSG_file_write(storage, NULL, (size_t)jsize, (size_t)jnmemb, file); + + (*env)->ReleaseStringUTFChars(env, jstorage, storage); + + return (jlong)n; +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_File_close(JNIEnv *env, jobject jfile, jobject jstorage) { + msg_file_t file = jfile_get_native(env, jfile); + + const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0); + + MSG_file_close(storage, file); + jfile_bind(env, jfile, NULL); + + (*env)->ReleaseStringUTFChars(env, jstorage, storage); + +} + diff --git a/src/jmsg_file.h b/src/jmsg_file.h new file mode 100644 index 0000000000..4202ea2ac9 --- /dev/null +++ b/src/jmsg_file.h @@ -0,0 +1,50 @@ +/* Functions related to the java file API. */ +/* Copyright (c) 2012. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef MSG_JFILE_H +#define MSG_JFILE_H +#include +#include "msg/msg.h" + +jfieldID jfile_field_bind; + +void jfile_bind(JNIEnv *env, jobject jfile, msg_file_t stream); +msg_file_t jfile_get_native(JNIEnv *env, jobject jfile); +/** + * Class org_simgrid_msg_File + * Method nativeInit + * Signature ()V + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_File_nativeInit(JNIEnv*, jclass); +/** + * Class org_simgrid_msg_File + * Method open + * Signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_File_open(JNIEnv*, jobject, jobject, jobject, jobject); +/** + * Class org_simgrid_msg_File + * Method read + */ +JNIEXPORT jlong JNICALL +Java_org_simgrid_msg_File_read(JNIEnv*, jobject, jobject, jlong, jlong); +/** + * Class org_simgrid_msg_File + * Method write + */ +JNIEXPORT jlong JNICALL +Java_org_simgrid_msg_File_write(JNIEnv*, jobject, jobject, jlong, jlong); +/** + * Class org_simgrid_msg_File + * Method close + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_File_close(JNIEnv*, jobject, jobject); + +#endif -- 2.20.1