Stxxl  1.4.0
io/test_cancel.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  io/test_cancel.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2009-2011 Johannes Singler <singler@kit.edu>
00007  *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <stxxl/io>
00015 #include <stxxl/aligned_alloc>
00016 
00017 //! \example io/test_cancel.cpp
00018 //! This tests the request cancelation mechanisms.
00019 
00020 using stxxl::file;
00021 
00022 struct print_completion
00023 {
00024     void operator () (stxxl::request * ptr)
00025     {
00026         std::cout << "Request completed: " << ptr << std::endl;
00027     }
00028 };
00029 
00030 int main(int argc, char ** argv)
00031 {
00032     if (argc < 3)
00033     {
00034         std::cout << "Usage: " << argv[0] << " filetype tempfile" << std::endl;
00035         return -1;
00036     }
00037 
00038     const stxxl::uint64 size = 64 * 1024 * 1024, num_blocks = 16;
00039     char * buffer = (char *)stxxl::aligned_alloc<4096>(size);
00040     memset(buffer, 0, size);
00041 
00042     stxxl::compat_unique_ptr<stxxl::file>::result file(
00043         stxxl::create_file(argv[1], argv[2], stxxl::file::CREAT | stxxl::file::RDWR | stxxl::file::DIRECT));
00044 
00045     file->set_size(num_blocks * size);
00046     stxxl::request_ptr req[num_blocks];
00047 
00048     //without cancelation
00049     std::cout << "Posting " << num_blocks << " requests." << std::endl;
00050     stxxl::stats_data stats1(*stxxl::stats::get_instance());
00051     unsigned i = 0;
00052     for ( ; i < num_blocks; i++)
00053         req[i] = file->awrite(buffer, i * size, size, print_completion());
00054     wait_all(req, num_blocks);
00055     std::cout << stxxl::stats_data(*stxxl::stats::get_instance()) - stats1;
00056 
00057     //with cancelation
00058     std::cout << "Posting " << num_blocks << " requests." << std::endl;
00059     stxxl::stats_data stats2(*stxxl::stats::get_instance());
00060     for (unsigned i = 0; i < num_blocks; i++)
00061         req[i] = file->awrite(buffer, i * size, size, print_completion());
00062     //cancel first half
00063     std::cout << "Canceling first " << num_blocks / 2 << " requests." << std::endl;
00064     unsigned num_canceled = cancel_all(req, req + num_blocks / 2);
00065     std::cout << "Successfully canceled " << num_canceled << " requests." << std::endl;
00066     //cancel every second in second half
00067     for (unsigned i = num_blocks / 2; i < num_blocks; i += 2)
00068     {
00069         std::cout << "Canceling request " << &(*(req[i])) << std::endl;
00070         if (req[i]->cancel())
00071             std::cout << "Request canceled: " << &(*(req[i])) << std::endl;
00072         else
00073             std::cout << "Request not canceled: " << &(*(req[i])) << std::endl;
00074     }
00075     wait_all(req, num_blocks);
00076     std::cout << stxxl::stats_data(*stxxl::stats::get_instance()) - stats2;
00077 
00078     stxxl::aligned_dealloc<4096>(buffer);
00079 
00080     return 0;
00081 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines