Permalink
Browse files

Some changes to temp_file, and file_stream

Conflicts:

	tpie/CMakeLists.txt
	tpie/file_accessor/file_accessor.h
	tpie/file_stream.h
	tpie/stats.h
	tpie/tempname.cpp
	tpie/tempname.h
  • Loading branch information...
Mortal committed Mar 19, 2012
1 parent 2b42f66 commit a3cee905cc18fdad65db96f9407478313bba5599
Showing with 184 additions and 21 deletions.
  1. +2 −0 tpie/CMakeLists.txt
  2. +4 −0 tpie/file_accessor/file_accessor_crtp.h
  3. +49 −17 tpie/file_stream.h
  4. +55 −0 tpie/stats.cpp
  5. +60 −0 tpie/stats.h
  6. +5 −4 tpie/tempname.cpp
  7. +9 −0 tpie/tempname.h
View
@@ -62,6 +62,7 @@ set (HEADERS
sysinfo.h
tpie_assert.h
tpie_log.h
+ stats.h
types.h
tempname.h
unused.h
@@ -95,6 +96,7 @@ set (SOURCES
tpie.cpp
tpie_log.cpp
file_base.cpp
+ stats.cpp
"${CMAKE_CURRENT_BINARY_DIR}/sysinfo.cpp"
)
@@ -82,6 +82,10 @@ class file_accessor_crtp {
inline stream_size_type size() const {return m_size;}
inline const std::string & path() const {return m_path;}
inline memory_size_type user_data_size() const {return m_userDataSize;}
+
+ inline stream_size_type byte_size() const {
+ return ((m_size + m_blockItems - 1)/m_blockItems) * m_blockSize + header_size();
+ }
};
}
View
@@ -71,6 +71,7 @@ class file_stream {
m_index = std::numeric_limits<memory_size_type>::max();
m_block.data = tpie_new_array<char>(block_memory_usage());
+ m_tempFile = 0;
};
///////////////////////////////////////////////////////////////////////////
@@ -81,6 +82,7 @@ class file_stream {
tpie_delete_array(m_block.data, block_memory_usage());
}
+
/////////////////////////////////////////////////////////////////////////
/// \copydoc file_base::open
/// \sa file_base::open
@@ -89,23 +91,25 @@ class file_stream {
file_base::access_type accessType=file_base::read_write,
memory_size_type userDataSize=0) throw (stream_exception) {
close();
- m_canRead = accessType == file_base::read || accessType == file_base::read_write;
- m_canWrite = accessType == file_base::write || accessType == file_base::read_write;
- m_fileAccessor->open(path, m_canRead, m_canWrite, m_itemSize, m_blockSize, userDataSize);
- m_size = m_fileAccessor->size();
- m_open = true;
-
- m_blockStartIndex = 0;
- m_nextBlock = std::numeric_limits<stream_size_type>::max();
- m_nextIndex = std::numeric_limits<memory_size_type>::max();
- m_index = std::numeric_limits<memory_size_type>::max();
+ open_inner(path, accessType, userDataSize);
+ }
- m_block.size = 0;
- m_block.number = std::numeric_limits<stream_size_type>::max();
- m_block.dirty = false;
-
- initialize();
- seek(0);
+ /////////////////////////////////////////////////////////////////////////
+ ///
+ /////////////////////////////////////////////////////////////////////////
+ inline void open(memory_size_type userDataSize=0) throw (stream_exception) {
+ close();
+ m_ownedTempFile.reset(new temp_file());
+ m_tempFile=m_ownedTempFile.get();
+ open_inner(m_tempFile->path(), file_base::read_write, userDataSize);
+ }
+
+ inline void open(temp_file & file,
+ file_base::access_type accessType=file_base::read_write,
+ memory_size_type userDataSize=0) throw (stream_exception) {
+ close();
+ m_tempFile=&file;
+ open_inner(m_tempFile->path(), accessType, userDataSize);
}
@@ -142,6 +146,8 @@ class file_stream {
m_fileAccessor->close();
}
m_open = false;
+ m_ownedTempFile.reset();
+ m_tempFile=NULL;
}
/////////////////////////////////////////////////////////////////////////
@@ -418,8 +424,32 @@ class file_stream {
memory_size_type m_itemSize;
bool m_open;
file_accessor::file_accessor * m_fileAccessor;
-
block_t m_block;
+ std::auto_ptr<temp_file> m_ownedTempFile;
+ temp_file * m_tempFile;
+
+ inline void open_inner(const std::string & path,
+ file_base::access_type accessType,
+ memory_size_type user_data_size) throw (stream_exception) {
+ m_canRead = accessType == file_base::read || accessType == file_base::read_write;
+ m_canWrite = accessType == file_base::write || accessType == file_base::read_write;
+ m_fileAccessor->open(path, m_canRead, m_canWrite, m_itemSize, m_blockSize, user_data_size);
+ m_size = m_fileAccessor->size();
+ m_open = true;
+
+ m_blockStartIndex = 0;
+ m_nextBlock = std::numeric_limits<stream_size_type>::max();
+ m_nextIndex = std::numeric_limits<memory_size_type>::max();
+ m_index = std::numeric_limits<memory_size_type>::max();
+
+ m_block.size = 0;
+ m_block.number = std::numeric_limits<stream_size_type>::max();
+ m_block.dirty = false;
+
+ initialize();
+ seek(0);
+ }
+
///////////////////////////////////////////////////////////////////////////
/// \brief Use file_accessor to fetch indicated block number into m_block.
@@ -482,6 +512,8 @@ class file_stream {
assert(m_index <= m_blockItems);
m_block.size = std::max(m_block.size, m_index);
m_size = std::max(m_size, static_cast<stream_size_type>(m_index)+m_blockStartIndex);
+ if (m_tempFile)
+ m_tempFile->update_recorded_size(m_fileAccessor->byte_size());
}
}
View
@@ -0,0 +1,55 @@
+// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
+// vi:set ts=4 sts=4 sw=4 noet :
+// Copyright 2008,2012, The TPIE development team
+//
+// This file is part of TPIE.
+//
+// TPIE is free software: you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the
+// Free Software Foundation, either version 3 of the License, or (at your
+// option) any later version.
+//
+// TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with TPIE. If not, see <http://www.gnu.org/licenses/>
+
+// The tpie_stats class for recording statistics. The parameter C is
+// the number of statistics to be recorded.
+
+namespace tpie {
+
+ static stream_size_type temp_file_usage=0;
+ static stream_size_type bytes_read=0;
+ static stream_size_type bytes_written=0;
+
+ stream_size_type get_tempfile_usage() {
+ return temp_file_usage;
+ }
+
+ void increment_temp_file_usage(stream_offset_type delta) {
+ stream_offset_type x=temp_file_usage+delta;
+ if (x < 0) temp_file_usage=0;
+ temp_file_usage=x;
+ }
+
+ stream_size_type get_bytes_read() {
+ return bytes_read;
+ }
+
+ stream_size_type get_bytes_written() {
+ return bytes_written;
+ }
+
+ void increment_bytes_read(stream_size_type delta) {
+ bytes_read += delta;
+ }
+
+ void increment_bytes_written(stream_size_type delta) {
+ bytes_written += delta;
+ }
+} // tpie namespace
+
View
@@ -0,0 +1,60 @@
+// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
+// vi:set ts=4 sts=4 sw=4 noet :
+// Copyright 2008,2012, The TPIE development team
+//
+// This file is part of TPIE.
+//
+// TPIE is free software: you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the
+// Free Software Foundation, either version 3 of the License, or (at your
+// option) any later version.
+//
+// TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with TPIE. If not, see <http://www.gnu.org/licenses/>
+
+// The tpie_stats class for recording statistics. The parameter C is
+// the number of statistics to be recorded.
+#ifndef _TPIE_STATS_H
+#define _TPIE_STATS_H
+#include <tpie/types.h>
+
+namespace tpie {
+ ///////////////////////////////////////////////////////////////////////
+ /// \brief Return the number of bytes currently beeing used by temporery files
+ ///////////////////////////////////////////////////////////////////////
+ stream_size_type get_tempfile_usage();
+
+ ///////////////////////////////////////////////////////////////////////
+ /// \brief Increment (possibly by a negatime amount) the number of bytes beeing
+ /// used by temporery files
+ ///////////////////////////////////////////////////////////////////////
+ void increment_temp_file_usage(stream_offset_type delta);
+
+ ///////////////////////////////////////////////////////////////////////
+ /// \brief Return the number of bytes read from disk since program start
+ ///////////////////////////////////////////////////////////////////////
+ stream_size_type get_bytes_read();
+
+ ///////////////////////////////////////////////////////////////////////
+ /// \brief Return the number of bytes written to disk since program start
+ ///////////////////////////////////////////////////////////////////////
+ stream_size_type get_bytes_written();
+
+ ///////////////////////////////////////////////////////////////////////
+ /// \brief Inform that stats module that an additional delta bytes have
+ /// been read from disk.
+ ///////////////////////////////////////////////////////////////////////
+ void increment_bytes_read(stream_size_type delta);
+
+ ///////////////////////////////////////////////////////////////////////
+ /// \brief Inform the stats module than an additional delta bytes have
+ /// been written to disk
+ ///////////////////////////////////////////////////////////////////////
+ void increment_bytes_written(stream_size_type delta);
+} // tpie namespace
+#endif //_TPIE_STATS_H
View
@@ -123,7 +123,6 @@ std::string tempname::tpie_dir_name(const std::string& post_base, const std::str
std::string tempname::get_actual_path() {
//information about the search order is in the header
std::string dir;
-
if(!default_path.empty())
dir = default_path; //user specified path
else if(getenv(AMI_SINGLE_DEVICE_ENV) != NULL) //TPIE env variable
@@ -207,9 +206,9 @@ const std::string& tempname::get_default_extension() {
return default_extension;
}
-temp_file::temp_file(): m_persist(false) {}
+temp_file::temp_file(): m_persist(false), m_recordedSize(0) {}
-temp_file::temp_file(const std::string & path, bool persist): m_path(path), m_persist(persist) {}
+temp_file::temp_file(const std::string & path, bool persist): m_path(path), m_persist(persist), m_recordedSize(0) {}
const std::string & temp_file::path() {
if (m_path.empty())
@@ -228,7 +227,9 @@ temp_file::~temp_file() {
}
void temp_file::free() {
- if (!m_path.empty() && !m_persist && boost::filesystem::exists(m_path))
+ if (!m_path.empty() && !m_persist && boost::filesystem::exists(m_path)) {
boost::filesystem::remove(m_path);
+ update_recorded_size(0);
+ }
m_path="";
}
View
@@ -26,6 +26,7 @@
// Get definitions for working with Unix and Windows
#include <tpie/portability.h>
+#include <tpie/stats.h>
#include <stdexcept>
// The name of the environment variable pointing to a tmp directory.
#define TMPDIR_ENV "TMPDIR"
@@ -110,6 +111,7 @@ namespace tpie {
private:
std::string m_path;
bool m_persist;
+ stream_size_type m_recordedSize;
public:
///////////////////////////////////////////////////////////////////////
/// \returns Whether this file should not be deleted when this object
@@ -133,6 +135,13 @@ namespace tpie {
///////////////////////////////////////////////////////////////////////
void set_path(const std::string & path, bool persist=false);
+ void update_recorded_size(stream_size_type size) {
+ increment_temp_file_usage(static_cast<stream_offset_type>(size)-
+ static_cast<stream_offset_type>(m_recordedSize));
+ m_recordedSize=size;
+
+ }
+
///////////////////////////////////////////////////////////////////////
/// \brief Create a temp_file and generate a random temporary file
/// name.

0 comments on commit a3cee90

Please sign in to comment.