summaryrefslogtreecommitdiff
path: root/examples/Mem_Map
diff options
context:
space:
mode:
authorlevine <levine@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1996-10-21 21:41:34 +0000
committerlevine <levine@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>1996-10-21 21:41:34 +0000
commita5fdebc5f6375078ec1763850a4ca23ec7fe6458 (patch)
treebcf0a25c3d45a209a6e3ac37b233a4812f29c732 /examples/Mem_Map
downloadATCD-a5fdebc5f6375078ec1763850a4ca23ec7fe6458.tar.gz
Initial revision
Diffstat (limited to 'examples/Mem_Map')
-rw-r--r--examples/Mem_Map/IO-tests/IO_Test.cpp186
-rw-r--r--examples/Mem_Map/IO-tests/IO_Test.h71
-rw-r--r--examples/Mem_Map/IO-tests/Makefile63
-rw-r--r--examples/Mem_Map/IO-tests/test_io.cpp155
-rw-r--r--examples/Mem_Map/Makefile22
-rw-r--r--examples/Mem_Map/file-reverse/Makefile58
-rw-r--r--examples/Mem_Map/file-reverse/file-reverse.cpp45
7 files changed, 600 insertions, 0 deletions
diff --git a/examples/Mem_Map/IO-tests/IO_Test.cpp b/examples/Mem_Map/IO-tests/IO_Test.cpp
new file mode 100644
index 00000000000..979fd724181
--- /dev/null
+++ b/examples/Mem_Map/IO-tests/IO_Test.cpp
@@ -0,0 +1,186 @@
+#include "ace/OS.h"
+// @(#)IO_Test.cpp 1.1 10/18/96
+
+#include "ace/Mem_Map.h"
+#include "IO_Test.h"
+
+IO_Test::IO_Test (const char *name, ACE_Profile_Timer &tm)
+ : name_ (name), tm_ (tm)
+{
+}
+
+const char *
+IO_Test::name (void)
+{
+ return this->name_;
+}
+
+Slow_Read_Write_Test::Slow_Read_Write_Test (char *name, ACE_Profile_Timer &tm)
+ : IO_Test (name, tm)
+{
+}
+
+int
+Slow_Read_Write_Test::run_test (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ int ifd = fileno (input_fp);
+ int ofd = fileno (output_fp);
+
+ this->tm_.start ();
+
+ while (--iterations >= 0)
+ {
+ char c;
+
+ while (ACE_OS::read (ifd, &c, sizeof c) > 0)
+ ::write (ofd, &c, sizeof c);
+
+ ACE_OS::lseek (ifd, 0, SEEK_SET);
+ ACE_OS::lseek (ofd, 0, SEEK_SET);
+ }
+
+ this->tm_.stop ();
+ return 0;
+}
+
+Stdio_Test::Stdio_Test (char *name, ACE_Profile_Timer &tm)
+ : IO_Test (name, tm)
+{
+}
+
+int
+Stdio_Test::run_test (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ this->tm_.start ();
+
+ while (--iterations >= 0)
+ {
+ int c;
+
+ while ((c = getc (input_fp)) != EOF)
+ putc (c, output_fp);
+
+ ACE_OS::rewind (input_fp);
+ ACE_OS::rewind (output_fp);
+ }
+ this->tm_.stop ();
+ return 0;
+}
+
+Block_Read_Write_Test::Block_Read_Write_Test (char *name, ACE_Profile_Timer &tm)
+ : IO_Test (name, tm)
+{
+}
+
+int
+Block_Read_Write_Test::run_test (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ int ifd = fileno (input_fp);
+ int ofd = fileno (output_fp);
+
+ this->tm_.start ();
+
+ while (--iterations >= 0)
+ {
+ char buf[BUFSIZ];
+ int n;
+
+ while ((n = ACE_OS::read (ifd, buf, sizeof buf)) > 0)
+ ::write (ofd, buf, n);
+
+ ACE_OS::lseek (ifd, 0, SEEK_SET);
+ ACE_OS::lseek (ofd, 0, SEEK_SET);
+ }
+
+ this->tm_.stop ();
+ return 0;
+}
+
+Block_Fread_Fwrite_Test::Block_Fread_Fwrite_Test (char *name, ACE_Profile_Timer &tm)
+ : IO_Test (name, tm)
+{
+}
+
+int
+Block_Fread_Fwrite_Test::run_test (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ this->tm_.start ();
+
+ while (--iterations >= 0)
+ {
+ char buf[BUFSIZ];
+ int n;
+
+ while ((n = ACE_OS::fread (buf, 1, sizeof buf, input_fp)) != 0)
+ ::fwrite (buf, n, 1, output_fp);
+
+ ACE_OS::rewind (input_fp);
+ ACE_OS::rewind (output_fp);
+ }
+
+ this->tm_.stop ();
+ return 0;
+}
+
+Mmap1_Test::Mmap1_Test (char *name, ACE_Profile_Timer &tm)
+ : IO_Test (name, tm)
+{
+}
+
+int
+Mmap1_Test::run_test (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ ACE_Mem_Map map_input (fileno (input_fp));
+ void *src = 0;
+
+ if (map_input (src) == -1)
+ return -1;
+ else
+ {
+ this->tm_.start ();
+
+ while (--iterations >= 0)
+ if (ACE_OS::write (fileno (output_fp), src, map_input.size ()) == -1)
+ return -1;
+
+ this->tm_.stop ();
+ }
+
+ if (map_input.unmap () == -1)
+ return -1;
+ else
+ return 0;
+}
+
+Mmap2_Test::Mmap2_Test (char *name, ACE_Profile_Timer &tm)
+ : IO_Test (name, tm)
+{
+}
+
+int
+Mmap2_Test::run_test (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ ACE_Mem_Map map_input (fileno (input_fp));
+ int size = map_input.size ();
+ ACE_Mem_Map map_output (fileno (output_fp), size, PROT_WRITE, MAP_SHARED);
+ void *src = 0;
+ void *dst = 0;
+
+ if (map_input (src) == -1 || map_output (dst) == -1)
+ return -1;
+ else
+ {
+ this->tm_.start ();
+
+ while (--iterations >= 0)
+ ACE_OS::memcpy (dst, src, size);
+
+ this->tm_.stop ();
+ }
+
+ if (map_input.unmap () == -1
+ || map_output.unmap () == -1)
+ return -1;
+ else
+ return 0;
+}
diff --git a/examples/Mem_Map/IO-tests/IO_Test.h b/examples/Mem_Map/IO-tests/IO_Test.h
new file mode 100644
index 00000000000..b45aabf17d9
--- /dev/null
+++ b/examples/Mem_Map/IO-tests/IO_Test.h
@@ -0,0 +1,71 @@
+/* -*- C++ -*- */
+// @(#)IO_Test.h 1.1 10/18/96
+
+/* Class hierarchy for the File I/O tests. */
+
+#include "ace/Profile_Timer.h"
+
+/* Base class for all the File I/O tests. */
+
+class IO_Test
+{
+public:
+ // Initialize the test name
+ IO_Test (const char *name, ACE_Profile_Timer &tm);
+
+ // Return the name of the test
+ const char *name (void);
+
+ // Execute the IO test (note this is a pure virtual function...)
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp) = 0;
+
+protected:
+ // Name of the test
+ const char *name_;
+
+ // Reference to a timer
+ ACE_Profile_Timer &tm_;
+};
+
+class Slow_Read_Write_Test : public IO_Test
+{
+public:
+ Slow_Read_Write_Test (char *name, ACE_Profile_Timer &tm);
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp);
+};
+
+class Stdio_Test : public IO_Test
+{
+public:
+ Stdio_Test (char *name, ACE_Profile_Timer &tm);
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp);
+};
+
+class Block_Read_Write_Test : public IO_Test
+{
+public:
+ Block_Read_Write_Test (char *name, ACE_Profile_Timer &tm);
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp);
+};
+
+class Block_Fread_Fwrite_Test : public IO_Test
+{
+public:
+ Block_Fread_Fwrite_Test (char *name, ACE_Profile_Timer &tm);
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp);
+};
+
+class Mmap1_Test : public IO_Test
+{
+public:
+ Mmap1_Test (char *name, ACE_Profile_Timer &tm);
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp);
+};
+
+class Mmap2_Test : public IO_Test
+{
+public:
+ Mmap2_Test (char *name, ACE_Profile_Timer &tm);
+ virtual int run_test (int iterations, FILE *input_fp, FILE *output_fp);
+};
+
diff --git a/examples/Mem_Map/IO-tests/Makefile b/examples/Mem_Map/IO-tests/Makefile
new file mode 100644
index 00000000000..c7f825136cb
--- /dev/null
+++ b/examples/Mem_Map/IO-tests/Makefile
@@ -0,0 +1,63 @@
+#----------------------------------------------------------------------------
+# @(#)Makefile 1.1 10/18/96
+#
+# Makefile for Mem_Map IO tests
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+# Local macros
+#----------------------------------------------------------------------------
+
+BIN = test_io
+
+FILES = IO_Test
+
+LSRC = $(addsuffix .cpp,$(FILES))
+LOBJ = $(addsuffix .o,$(FILES))
+SHOBJ = $(addsuffix .so,$(FILES))
+
+LDLIBS = $(addprefix .shobj/,$(SHOBJ))
+
+VLDLIBS = $(LDLIBS:%=%$(VAR))
+
+BUILD = $(VBIN)
+
+#----------------------------------------------------------------------------
+# Include macros and targets
+#----------------------------------------------------------------------------
+
+include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.nonested.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.lib.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.bin.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.local.GNU
+
+#----------------------------------------------------------------------------
+# Local targets
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+# Dependencies
+#----------------------------------------------------------------------------
+
+# DO NOT DELETE THIS LINE -- g++dep uses it.
+# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
+
+.obj/IO_Test.o .shobj/IO_Test.so: IO_Test.cpp \
+ $(WRAPPER_ROOT)/ace/OS.h \
+ $(WRAPPER_ROOT)/ace/Time_Value.h \
+ $(WRAPPER_ROOT)/ace/config.h \
+ $(WRAPPER_ROOT)/ace/Trace.h \
+ $(WRAPPER_ROOT)/ace/Mem_Map.h \
+ $(WRAPPER_ROOT)/ace/ACE.h \
+ $(WRAPPER_ROOT)/ace/ACE.i \
+ $(WRAPPER_ROOT)/ace/Log_Msg.h \
+ $(WRAPPER_ROOT)/ace/Log_Record.h \
+ $(WRAPPER_ROOT)/ace/Log_Priority.h \
+ $(WRAPPER_ROOT)/ace/Log_Record.i \
+ IO_Test.h \
+ $(WRAPPER_ROOT)/ace/Profile_Timer.h
+
+# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
diff --git a/examples/Mem_Map/IO-tests/test_io.cpp b/examples/Mem_Map/IO-tests/test_io.cpp
new file mode 100644
index 00000000000..a9a9a918712
--- /dev/null
+++ b/examples/Mem_Map/IO-tests/test_io.cpp
@@ -0,0 +1,155 @@
+/* Test program for different methods of copying files. */
+// @(#)test_io.cpp 1.1 10/18/96
+
+
+#include "ace/OS.h"
+#include "ace/Profile_Timer.h"
+#include "ace/Get_Opt.h"
+#include "ace/Signal.h"
+#include "IO_Test.h"
+
+/* Name of program. */
+static char *program_name;
+
+/* Name of default input file. */
+static char *input_filename = "/usr/dict/words";
+
+/* Name of default output file. */
+static char *output_filename = "/tmp/foo";
+
+/* Check if removing output file upon completion... */
+static int remove_output = 1;
+
+/* Count of the number of iterations to run the tests. */
+static int iteration_count = 100;
+
+/* Profiler used to keep track of file I/O time. */
+static ACE_Profile_Timer tm;
+
+/* Explain usage and exit. */
+
+static void
+print_usage_and_die (void)
+{
+ cerr << "usage: "
+ << program_name
+ << " [-i input_file] [-o output_file] [-n iteration_count] [-r]\n";
+ ACE_OS::exit (1);
+}
+
+/* Clean up the output file on exit from a signal. */
+
+static void
+clean_up (int = 0)
+{
+ if (remove_output)
+ ACE_OS::unlink (output_filename);
+ ACE_OS::exit (0);
+}
+
+/* Set up the program name used in error messages. */
+
+static void
+set_program_name (char name[])
+{
+ if ((name = strrchr (name, '/')) == 0)
+ program_name = name;
+ else
+ program_name = name + 1;
+}
+
+/* Parse the command-line arguments and set options. */
+
+static void
+parse_args (int argc, char *argv[])
+{
+ ACE_Get_Opt get_opt (argc, argv, "i:n:o:r");
+
+ for (int c; ((c = get_opt ()) != -1); )
+ switch (c)
+ {
+ case 'i':
+ input_filename = get_opt.optarg;
+ break;
+ case 'n':
+ iteration_count = ACE_OS::atoi (get_opt.optarg);
+ break;
+ case 'o':
+ output_filename = get_opt.optarg;
+ break;
+ case 'r':
+ remove_output = 0;
+ break;
+ default:
+ print_usage_and_die ();
+ break;
+ }
+}
+
+/* Vector of pointers to derived classes that inherit from IO_Test base class. */
+
+static IO_Test *test_vector[100];
+
+static void
+run_tests (int iterations, FILE *input_fp, FILE *output_fp)
+{
+ // If HP/UX didn't suck so badly we could initialize in the global scope...
+ test_vector[0] = new Stdio_Test ("Stdio_Test", tm);
+ test_vector[1] = new Block_Fread_Fwrite_Test ("Block_Fread_Fwrite_Test", tm);
+ test_vector[2] = new Block_Read_Write_Test ("Block_Read_Write_Test", tm);
+ test_vector[3] = new Mmap1_Test ("Mmap1_Test", tm);
+ test_vector[4] = new Mmap2_Test ("Mmap2_Test", tm);
+ /* test_vector[5] = new Slow_Read_Write_Test ("Slow"Read_Write_Test", tm) */
+ test_vector[5] = (IO_Test *) 0;
+
+ for (int i = 0; test_vector[i] != 0; i++)
+ {
+ if (ACE_OS::ftruncate (fileno (output_fp), 0) == -1)
+ ::perror ("ftruncate");
+
+ cerr << "--------------------\n"
+ << "starting " << test_vector[i]->name () << " for " << iterations
+ << " iteration(s):\n";
+
+ if (test_vector[i]->run_test (iterations, input_fp, output_fp) == -1)
+ ::perror (test_vector[i]->name ());
+
+ ACE_Profile_Timer::ACE_Elapsed_Time et;
+ tm.elapsed_time (et);
+
+ cerr << "wallclock time = " << et.real_time
+ << ", user time = " << et.user_time
+ << ", system time = " << et.system_time << endl;
+
+ delete test_vector[i];
+ }
+
+ cerr << "--------------------\n";
+}
+
+int
+main (int argc, char *argv[])
+{
+ FILE *input_fp;
+ FILE *output_fp;
+
+ set_program_name (argv[0]);
+ parse_args (argc, argv);
+ ACE_Sig_Action sig ((ACE_SignalHandler) clean_up, SIGINT);
+
+ if ((input_fp = ACE_OS::fopen (input_filename, "r")) == 0)
+ ACE_OS::perror (input_filename), ACE_OS::exit (1);
+
+ ACE_OS::unlink (output_filename);
+
+ if ((output_fp = ACE_OS::fopen (output_filename, "w+")) == 0)
+ ACE_OS::perror (output_filename), ACE_OS::exit (1);
+
+ run_tests (iteration_count, input_fp, output_fp);
+
+ if (ACE_OS::fclose (input_fp) == -1 || ACE_OS::fclose (output_fp) == -1)
+ ACE_OS::perror ("fclose"), ACE_OS::exit (1);
+
+ clean_up ();
+ return 0;
+}
diff --git a/examples/Mem_Map/Makefile b/examples/Mem_Map/Makefile
new file mode 100644
index 00000000000..3c5d5f2d800
--- /dev/null
+++ b/examples/Mem_Map/Makefile
@@ -0,0 +1,22 @@
+#----------------------------------------------------------------------------
+# @(#)Makefile 1.1 10/18/96
+#
+# Makefile for the Mem_Map test directory
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+# Local macros
+#----------------------------------------------------------------------------
+
+DIRS = IO-tests \
+ file-reverse
+
+#----------------------------------------------------------------------------
+# macros and targets
+#----------------------------------------------------------------------------
+
+include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.nested.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.nolocal.GNU
diff --git a/examples/Mem_Map/file-reverse/Makefile b/examples/Mem_Map/file-reverse/Makefile
new file mode 100644
index 00000000000..ccc7ce66724
--- /dev/null
+++ b/examples/Mem_Map/file-reverse/Makefile
@@ -0,0 +1,58 @@
+#----------------------------------------------------------------------------
+# @(#)Makefile 1.1 10/18/96
+#
+# Makefile for Mem_Map file reverse test
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+# Local macros
+#----------------------------------------------------------------------------
+
+BIN = file-reverse
+
+LSRC = $(addsuffix .cpp,$(BIN))
+
+LDLIBS =
+
+VLDLIBS = $(LDLIBS:%=%$(VAR))
+
+BUILD = $(VBIN)
+
+INSTALL =
+
+#----------------------------------------------------------------------------
+# Include macros and targets
+#----------------------------------------------------------------------------
+
+include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.nonested.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.bin.GNU
+include $(WRAPPER_ROOT)/include/makeinclude/rules.local.GNU
+
+#----------------------------------------------------------------------------
+# Local targets
+#----------------------------------------------------------------------------
+
+#----------------------------------------------------------------------------
+# Dependencies
+#----------------------------------------------------------------------------
+
+# DO NOT DELETE THIS LINE -- g++dep uses it.
+# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
+
+.obj/file-reverse.o .shobj/file-reverse.so: file-reverse.cpp \
+ $(WRAPPER_ROOT)/ace/Log_Msg.h \
+ $(WRAPPER_ROOT)/ace/Log_Record.h \
+ $(WRAPPER_ROOT)/ace/ACE.h \
+ $(WRAPPER_ROOT)/ace/OS.h \
+ $(WRAPPER_ROOT)/ace/Time_Value.h \
+ $(WRAPPER_ROOT)/ace/config.h \
+ $(WRAPPER_ROOT)/ace/Trace.h \
+ $(WRAPPER_ROOT)/ace/ACE.i \
+ $(WRAPPER_ROOT)/ace/Log_Priority.h \
+ $(WRAPPER_ROOT)/ace/Log_Record.i \
+ $(WRAPPER_ROOT)/ace/Mem_Map.h
+
+# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
diff --git a/examples/Mem_Map/file-reverse/file-reverse.cpp b/examples/Mem_Map/file-reverse/file-reverse.cpp
new file mode 100644
index 00000000000..d3431c40b0f
--- /dev/null
+++ b/examples/Mem_Map/file-reverse/file-reverse.cpp
@@ -0,0 +1,45 @@
+#include "ace/Log_Msg.h"
+// @(#)file-reverse.cpp 1.1 10/18/96
+
+#include "ace/Mem_Map.h"
+
+static void
+putline (const char *s)
+{
+ while (putchar (*s++) != '\n')
+ continue;
+}
+
+static void
+print_array_in_reverse (char *array, int size)
+{
+ size--;
+
+ if (array[size] == '\0')
+ array[size] = '\n';
+
+ while (--size >= 0)
+ if (array[size] == '\n')
+ putline (array + size + 1);
+
+ putline (array);
+}
+
+int
+main (int argc, char **argv)
+{
+ ACE_LOG_MSG->open (argv[0]);
+ void *cp;
+
+ if (argc != 2)
+ ACE_ERROR_RETURN ((LM_ERROR, "usage: %n file\n%a"), -1);
+
+ ACE_Mem_Map mmap;
+
+ if (mmap.map (argv[1]) == -1)
+ ACE_ERROR_RETURN ((LM_ERROR, "%n: %p\n%a", "mmap"), -1);
+
+ print_array_in_reverse ((char *) mmap.addr (), mmap.size ());
+
+ return 0;
+}