From e911e356ac0e77b7633e2ec2d74dee62d926c227 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Wed, 25 Mar 2020 12:01:01 +0100 Subject: [PATCH 1/1] convert teshsuite/msg/io-file to examples/c/io-file-system --- MANIFEST.in | 4 +- examples/c/CMakeLists.txt | 4 +- examples/c/io-file-system/io-file-system.c | 94 +++++++++++++++ examples/c/io-file-system/io-file-system.tesh | 21 ++++ teshsuite/msg/CMakeLists.txt | 4 +- teshsuite/msg/io-file/io-file.c | 107 ------------------ teshsuite/msg/io-file/io-file.tesh | 56 --------- 7 files changed, 121 insertions(+), 169 deletions(-) create mode 100644 examples/c/io-file-system/io-file-system.c create mode 100644 examples/c/io-file-system/io-file-system.tesh delete mode 100644 teshsuite/msg/io-file/io-file.c delete mode 100644 teshsuite/msg/io-file/io-file.tesh diff --git a/MANIFEST.in b/MANIFEST.in index 5038c32cb5..b1a03cb4c4 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -124,6 +124,8 @@ include examples/c/io-disk-raw/io-disk-raw.tesh include examples/c/io-file-remote/io-file-remote.c include examples/c/io-file-remote/io-file-remote.tesh include examples/c/io-file-remote/io-file-remote_d.xml +include examples/c/io-file-system/io-file-system.c +include examples/c/io-file-system/io-file-system.tesh include examples/c/platform-failures/platform-failures.c include examples/c/platform-failures/platform-failures.tesh include examples/c/platform-properties/platform-properties.c @@ -662,8 +664,6 @@ include teshsuite/msg/cloud-two-tasks/cloud-two-tasks.c include teshsuite/msg/cloud-two-tasks/cloud-two-tasks.tesh include teshsuite/msg/get_sender/get_sender.c include teshsuite/msg/get_sender/get_sender.tesh -include teshsuite/msg/io-file/io-file.c -include teshsuite/msg/io-file/io-file.tesh include teshsuite/msg/task_destroy_cancel/task_destroy_cancel.c include teshsuite/msg/task_destroy_cancel/task_destroy_cancel.tesh include teshsuite/msg/task_listen_from/task_listen_from.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 3ade2ce16b..89de75230a 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -10,7 +10,7 @@ foreach(x dht-pastry exec-async exec-basic exec-dvfs exec-remote exec-waitany energy-exec energy-exec-ptask energy-vm - io-disk-raw io-file-remote + io-disk-raw io-file-remote io-file-system platform-failures platform-properties plugin-hostload synchro-semaphore) @@ -102,7 +102,7 @@ foreach(x dht-kademlia dht-pastry exec-async exec-basic exec-dvfs exec-remote exec-waitany energy-exec energy-exec-ptask energy-vm - io-disk-raw io-file-remote + io-disk-raw io-file-remote io-file-system platform-failures platform-properties plugin-hostload synchro-semaphore) diff --git a/examples/c/io-file-system/io-file-system.c b/examples/c/io-file-system/io-file-system.c new file mode 100644 index 0000000000..6d2399fb10 --- /dev/null +++ b/examples/c/io-file-system/io-file-system.c @@ -0,0 +1,94 @@ +/* Copyright (c) 2008-2020. 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 "simgrid/actor.h" +#include "simgrid/disk.h" +#include "simgrid/engine.h" +#include "simgrid/host.h" +#include "simgrid/plugins/file_system.h" + +#include "xbt/log.h" +#include "xbt/str.h" +#include "xbt/sysdep.h" + +#include /* SEEK_SET */ + +XBT_LOG_NEW_DEFAULT_CATEGORY(io_file_system, "Messages specific for this io example"); + +static void show_info(unsigned int disk_count, sg_disk_t* disks) +{ + XBT_INFO("Storage info on %s:", sg_host_self_get_name()); + + for (unsigned int i = 0; i < disk_count; i++) { + sg_disk_t d = disks[i]; + // Retrieve disk's information + XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", sg_disk_name(d), sg_disk_get_mount_point(d), + sg_disk_get_size_used(d), sg_disk_get_size_free(d), sg_disk_get_size(d)); + } +} + +static void host(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) +{ + unsigned int disk_count; + sg_disk_t* disks; + sg_host_disks(sg_host_self(), &disk_count, &disks); + + show_info(disk_count, disks); + + // Open an non-existing file to create it + const char* filename = "/scratch/tmp/data.txt"; + sg_file_t file = sg_file_open(filename, NULL); + + sg_size_t write = sg_file_write(file, 200000); // Write 200,000 bytes + XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename); + + // check that sizes have changed + show_info(disk_count, disks); + + // Now retrieve the size of created file and read it completely + const sg_size_t file_size = sg_file_get_size(file); + sg_file_seek(file, 0, SEEK_SET); + const sg_size_t read = sg_file_read(file, file_size); + XBT_INFO("Read %llu bytes on %s", read, filename); + + // Now write 100,000 bytes in tmp/data.txt + write = sg_file_write(file, 100000); // Write 100,000 bytes + XBT_INFO("Write %llu bytes on %s", write, filename); + + // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme + const char* newpath = "/scratch/tmp/simgrid.readme"; + XBT_INFO("Move '%s' to '%s'", sg_file_get_name(file), newpath); + sg_file_move(file, newpath); + + // Test attaching some user data to the file + sg_file_set_data(file, xbt_strdup("777")); + char* file_data = (char*)(sg_file_get_data(file)); + XBT_INFO("User data attached to the file: %s", file_data); + xbt_free(file_data); + + // Close the file + sg_file_close(file); + + show_info(disk_count, disks); + + // Reopen the file and then unlink it + file = sg_file_open("/scratch/tmp/simgrid.readme", NULL); + XBT_INFO("Unlink file: '%s'", sg_file_get_name(file)); + sg_file_unlink(file); + + show_info(disk_count, disks); + xbt_free(disks); +} + +int main(int argc, char** argv) +{ + simgrid_init(&argc, argv); + sg_storage_file_system_init(); + simgrid_load_platform(argv[1]); + sg_actor_create("host", sg_host_by_name("bob"), host, 0, NULL); + simgrid_run(); + + return 0; +} diff --git a/examples/c/io-file-system/io-file-system.tesh b/examples/c/io-file-system/io-file-system.tesh new file mode 100644 index 0000000000..409b82b9a1 --- /dev/null +++ b/examples/c/io-file-system/io-file-system.tesh @@ -0,0 +1,21 @@ +#!/usr/bin/env tesh + +$ ${bindir:=.}/io-file-system-c ${platfdir}/hosts_with_disks.xml +> [bob:host:(1) 0.000000] [io_file_system/INFO] Storage info on bob: +> [bob:host:(1) 0.000000] [io_file_system/INFO] Disk1 (/scratch) Used: 36933331; Free: 536833978669; Total: 536870912000. +> [bob:host:(1) 0.000000] [io_file_system/INFO] Disk2 (/) Used: 0; Free: 536870912000; Total: 536870912000. +> [bob:host:(1) 0.005000] [io_file_system/INFO] Create a 200000 bytes file named '/scratch/tmp/data.txt' on /scratch +> [bob:host:(1) 0.005000] [io_file_system/INFO] Storage info on bob: +> [bob:host:(1) 0.005000] [io_file_system/INFO] Disk1 (/scratch) Used: 37133331; Free: 536833778669; Total: 536870912000. +> [bob:host:(1) 0.005000] [io_file_system/INFO] Disk2 (/) Used: 0; Free: 536870912000; Total: 536870912000. +> [bob:host:(1) 0.007000] [io_file_system/INFO] Read 200000 bytes on /scratch/tmp/data.txt +> [bob:host:(1) 0.009500] [io_file_system/INFO] Write 100000 bytes on /scratch/tmp/data.txt +> [bob:host:(1) 0.009500] [io_file_system/INFO] Move '/scratch/tmp/data.txt' to '/scratch/tmp/simgrid.readme' +> [bob:host:(1) 0.009500] [io_file_system/INFO] User data attached to the file: 777 +> [bob:host:(1) 0.009500] [io_file_system/INFO] Storage info on bob: +> [bob:host:(1) 0.009500] [io_file_system/INFO] Disk1 (/scratch) Used: 37233331; Free: 536833678669; Total: 536870912000. +> [bob:host:(1) 0.009500] [io_file_system/INFO] Disk2 (/) Used: 0; Free: 536870912000; Total: 536870912000. +> [bob:host:(1) 0.009500] [io_file_system/INFO] Unlink file: '/scratch/tmp/simgrid.readme' +> [bob:host:(1) 0.009500] [io_file_system/INFO] Storage info on bob: +> [bob:host:(1) 0.009500] [io_file_system/INFO] Disk1 (/scratch) Used: 36933331; Free: 536833978669; Total: 536870912000. +> [bob:host:(1) 0.009500] [io_file_system/INFO] Disk2 (/) Used: 0; Free: 536870912000; Total: 536870912000. diff --git a/teshsuite/msg/CMakeLists.txt b/teshsuite/msg/CMakeLists.txt index 732649cbfb..092f9d2373 100644 --- a/teshsuite/msg/CMakeLists.txt +++ b/teshsuite/msg/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(x cloud-two-tasks get_sender io-file task_listen_from task_destroy_cancel) +foreach(x cloud-two-tasks get_sender task_listen_from task_destroy_cancel) if(enable_msg) add_executable (${x} EXCLUDE_FROM_ALL ${x}/${x}.c) target_link_libraries(${x} simgrid) @@ -14,7 +14,7 @@ set(teshsuite_src ${teshsuite_src} PARENT_SCOPE) set(tesh_files ${tesh_files} PARENT_SCOPE) if(enable_msg) - foreach(x cloud-two-tasks get_sender task_destroy_cancel task_listen_from io-file) + foreach(x cloud-two-tasks get_sender task_destroy_cancel task_listen_from) ADD_TESH_FACTORIES(tesh-msg-${x} "raw" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/msg/${x} diff --git a/teshsuite/msg/io-file/io-file.c b/teshsuite/msg/io-file/io-file.c deleted file mode 100644 index 6444f5fdf3..0000000000 --- a/teshsuite/msg/io-file/io-file.c +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright (c) 2008-2020. 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 "simgrid/msg.h" -#include "simgrid/plugins/file_system.h" - -#include /* SEEK_SET */ - -XBT_LOG_NEW_DEFAULT_CATEGORY(io_file, "Messages specific for this io example"); - -static int host(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[]) -{ - msg_file_t file = NULL; - sg_size_t read; - sg_size_t write; - const char* st_name; - - switch (MSG_process_self_PID()) { - case 1: - file = MSG_file_open("/tmp/include/surf/simgrid_dtd.h", NULL); - st_name = "Disk2"; - break; - case 2: - file = MSG_file_open("/home/doc/simgrid/examples/platforms/nancy.xml", NULL); - st_name = "Disk1"; - break; - case 3: - file = MSG_file_open("/home/doc/simgrid/examples/platforms/g5k_cabinets.xml", NULL); - st_name = "Disk3"; - break; - case 4: - file = MSG_file_open("/home/doc/simgrid/examples/platforms/g5k.xml", NULL); - MSG_file_dump(file); - st_name = "Disk4"; - break; - default: - xbt_die("FILENAME NOT DEFINED %s", MSG_process_get_name(MSG_process_self())); - } - - const char* filename = MSG_file_get_name(file); - XBT_INFO("\tOpen file '%s'", filename); - const_sg_storage_t st = MSG_storage_get_by_name(st_name); - - XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu", filename, MSG_storage_get_used_size(st), - MSG_storage_get_size(st)); - - /* Try to read for 10MB */ - read = MSG_file_read(file, 10000000); - XBT_INFO("\tHave read %llu from '%s'", read, filename); - - /* Write 100KB in file from the current position, i.e, end of file or 10MB */ - write = MSG_file_write(file, 100000); - XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write, filename, MSG_file_get_size(file)); - - XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu", filename, MSG_storage_get_used_size(st), - MSG_storage_get_size(st)); - - /* rewind to the beginning of the file */ - XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename); - MSG_file_seek(file, 0, SEEK_SET); - - /* Try to read 110KB */ - read = MSG_file_read(file, 110000); - XBT_INFO("\tHave read %llu from '%s' (of size %llu)", read, filename, MSG_file_get_size(file)); - - /* rewind once again to the beginning of the file */ - XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename); - MSG_file_seek(file, 0, SEEK_SET); - - /* Write 110KB in file from the current position, i.e, end of file or 10MB */ - write = MSG_file_write(file, 110000); - XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write, filename, MSG_file_get_size(file)); - - XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu", filename, MSG_storage_get_used_size(st), - MSG_storage_get_size(st)); - - if (MSG_process_self_PID() == 1) { - XBT_INFO("\tUnlink file '%s'", MSG_file_get_name(file)); - MSG_file_unlink(file); - } else { - XBT_INFO("\tClose file '%s'", filename); - MSG_file_close(file); - } - return 0; -} - -int main(int argc, char** argv) -{ - MSG_init(&argc, argv); - MSG_storage_file_system_init(); - - MSG_create_environment(argv[1]); - xbt_dynar_t hosts = MSG_hosts_as_dynar(); - MSG_function_register("host", host); - unsigned long nb_hosts = xbt_dynar_length(hosts); - XBT_INFO("Number of host '%lu'", nb_hosts); - for (int i = 0; i < nb_hosts; i++) { - MSG_process_create("host", host, NULL, xbt_dynar_get_as(hosts, i, msg_host_t)); - } - xbt_dynar_free(&hosts); - - int res = MSG_main(); - XBT_INFO("Simulation time %.6f", MSG_get_clock()); - return res != MSG_OK; -} diff --git a/teshsuite/msg/io-file/io-file.tesh b/teshsuite/msg/io-file/io-file.tesh deleted file mode 100644 index 87589ece32..0000000000 --- a/teshsuite/msg/io-file/io-file.tesh +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env tesh - -$ ${bindir:=.}/io-file ${platfdir}/storage/storage.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n" -> [ 0.000000] (0:maestro@) Number of host '4' -> [ 0.000000] (1:host@alice) Open file '/tmp/include/surf/simgrid_dtd.h' -> [ 0.000000] (1:host@alice) Capacity of the storage element '/tmp/include/surf/simgrid_dtd.h' is stored on: 13221994 / 536870912000 -> [ 0.000000] (2:host@bob) Open file '/home/doc/simgrid/examples/platforms/nancy.xml' -> [ 0.000000] (2:host@bob) Capacity of the storage element '/home/doc/simgrid/examples/platforms/nancy.xml' is stored on: 36933331 / 536870912000 -> [ 0.000000] (3:host@carl) Open file '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' -> [ 0.000000] (3:host@carl) Capacity of the storage element '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' is stored on: 36933331 / 536870912000 -> [ 0.000000] (4:host@denise) File Descriptor information: -> Full path: '/home/doc/simgrid/examples/platforms/g5k.xml' -> Size: 17028 -> Mount point: '/home' -> Storage Id: 'Disk4' -> Storage Type: 'single_SSD' -> File Descriptor Id: 0 -> [ 0.000000] (4:host@denise) Open file '/home/doc/simgrid/examples/platforms/g5k.xml' -> [ 0.000000] (4:host@denise) Capacity of the storage element '/home/doc/simgrid/examples/platforms/g5k.xml' is stored on: 13221994 / 536870912000 -> [ 0.000040] (2:host@bob) Have read 4028 from '/home/doc/simgrid/examples/platforms/nancy.xml' -> [ 0.000085] (4:host@denise) Have read 17028 from '/home/doc/simgrid/examples/platforms/g5k.xml' -> [ 0.000118] (1:host@alice) Have read 23583 from '/tmp/include/surf/simgrid_dtd.h' -> [ 0.000226] (3:host@carl) Have read 22645 from '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' -> [ 0.001752] (4:host@denise) Have written 100000 in '/home/doc/simgrid/examples/platforms/g5k.xml'. Size now is: 117028 -> [ 0.001752] (4:host@denise) Capacity of the storage element '/home/doc/simgrid/examples/platforms/g5k.xml' is stored on: 13321994 / 536870912000 -> [ 0.001752] (4:host@denise) Coming back to the beginning of the stream for file '/home/doc/simgrid/examples/platforms/g5k.xml' -> [ 0.001785] (1:host@alice) Have written 100000 in '/tmp/include/surf/simgrid_dtd.h'. Size now is: 123583 -> [ 0.001785] (1:host@alice) Capacity of the storage element '/tmp/include/surf/simgrid_dtd.h' is stored on: 13321994 / 536870912000 -> [ 0.001785] (1:host@alice) Coming back to the beginning of the stream for file '/tmp/include/surf/simgrid_dtd.h' -> [ 0.002302] (4:host@denise) Have read 110000 from '/home/doc/simgrid/examples/platforms/g5k.xml' (of size 117028) -> [ 0.002302] (4:host@denise) Coming back to the beginning of the stream for file '/home/doc/simgrid/examples/platforms/g5k.xml' -> [ 0.002335] (1:host@alice) Have read 110000 from '/tmp/include/surf/simgrid_dtd.h' (of size 123583) -> [ 0.002335] (1:host@alice) Coming back to the beginning of the stream for file '/tmp/include/surf/simgrid_dtd.h' -> [ 0.003374] (2:host@bob) Have written 100000 in '/home/doc/simgrid/examples/platforms/nancy.xml'. Size now is: 104028 -> [ 0.003374] (2:host@bob) Capacity of the storage element '/home/doc/simgrid/examples/platforms/nancy.xml' is stored on: 37033331 / 536870912000 -> [ 0.003374] (2:host@bob) Coming back to the beginning of the stream for file '/home/doc/simgrid/examples/platforms/nancy.xml' -> [ 0.003560] (3:host@carl) Have written 100000 in '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml'. Size now is: 122645 -> [ 0.003560] (3:host@carl) Capacity of the storage element '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' is stored on: 37033331 / 536870912000 -> [ 0.003560] (3:host@carl) Coming back to the beginning of the stream for file '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' -> [ 0.004135] (4:host@denise) Have written 110000 in '/home/doc/simgrid/examples/platforms/g5k.xml'. Size now is: 110000 -> [ 0.004135] (4:host@denise) Capacity of the storage element '/home/doc/simgrid/examples/platforms/g5k.xml' is stored on: 13314966 / 536870912000 -> [ 0.004135] (4:host@denise) Close file '/home/doc/simgrid/examples/platforms/g5k.xml' -> [ 0.004168] (1:host@alice) Have written 110000 in '/tmp/include/surf/simgrid_dtd.h'. Size now is: 110000 -> [ 0.004168] (1:host@alice) Capacity of the storage element '/tmp/include/surf/simgrid_dtd.h' is stored on: 13308411 / 536870912000 -> [ 0.004168] (1:host@alice) Unlink file '/tmp/include/surf/simgrid_dtd.h' -> [ 0.004414] (2:host@bob) Have read 104028 from '/home/doc/simgrid/examples/platforms/nancy.xml' (of size 104028) -> [ 0.004414] (2:host@bob) Coming back to the beginning of the stream for file '/home/doc/simgrid/examples/platforms/nancy.xml' -> [ 0.004660] (3:host@carl) Have read 110000 from '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' (of size 122645) -> [ 0.004660] (3:host@carl) Coming back to the beginning of the stream for file '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' -> [ 0.008081] (2:host@bob) Have written 110000 in '/home/doc/simgrid/examples/platforms/nancy.xml'. Size now is: 110000 -> [ 0.008081] (2:host@bob) Capacity of the storage element '/home/doc/simgrid/examples/platforms/nancy.xml' is stored on: 37039303 / 536870912000 -> [ 0.008081] (2:host@bob) Close file '/home/doc/simgrid/examples/platforms/nancy.xml' -> [ 0.008326] (3:host@carl) Have written 110000 in '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml'. Size now is: 110000 -> [ 0.008326] (3:host@carl) Capacity of the storage element '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' is stored on: 37020686 / 536870912000 -> [ 0.008326] (3:host@carl) Close file '/home/doc/simgrid/examples/platforms/g5k_cabinets.xml' -> [ 0.008326] (0:maestro@) Simulation time 0.008326 -- 2.20.1