diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2014-12-10 18:28:55 -0500 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2014-12-10 18:28:55 -0500 |
commit | 294b99aabf3eb96323a3159b7a5e1b4bfc1ff04a (patch) | |
tree | dbb6bfba564d2979c8cebbfebe162a9659dff5aa /libs/pbd | |
parent | 17707b9674958391949e59a724a264cdcc2d65ff (diff) |
remove file manager LRU cache from code.
This was a very clever attempt to fix a non-problem. If the platform doesn't have enough file descriptors available
then the platform is broken and we're not going to hack around trying to fix it.
Diffstat (limited to 'libs/pbd')
-rw-r--r-- | libs/pbd/file_manager.cc | 334 | ||||
-rw-r--r-- | libs/pbd/file_utils.cc | 7 | ||||
-rw-r--r-- | libs/pbd/pbd/file_manager.h | 157 | ||||
-rw-r--r-- | libs/pbd/pbd/sndfile_manager.h | 58 | ||||
-rw-r--r-- | libs/pbd/sndfile_manager.cc | 95 | ||||
-rw-r--r-- | libs/pbd/wscript | 2 |
6 files changed, 2 insertions, 651 deletions
diff --git a/libs/pbd/file_manager.cc b/libs/pbd/file_manager.cc deleted file mode 100644 index 702c1e1b8f..0000000000 --- a/libs/pbd/file_manager.cc +++ /dev/null @@ -1,334 +0,0 @@ -/* - Copyright (C) 2010 Paul Davis - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#include <sys/time.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <cassert> -#include <cstdio> - -#include <glib.h> -#include <glib/gstdio.h> - -#ifdef __APPLE__ -#include <mach/mach_time.h> -#endif - -#include "pbd/compose.h" -#include "pbd/file_manager.h" -#include "pbd/resource.h" -#include "pbd/debug.h" - -using namespace std; -using namespace PBD; - -FileManager* FileDescriptor::_manager; - -FileManager::FileManager () - : _open (0) -{ - struct ResourceLimit rl; - - /* XXX: this is a bit arbitrary */ - if (get_resource_limit (OpenFiles, rl)) { - _max_open = rl.current_limit - 64; - } else { - _max_open = 256; - } - - DEBUG_TRACE (DEBUG::FileManager, string_compose ("FileManager can open up to %1 files.\n", _max_open)); -} - -void -FileManager::add (FileDescriptor* d) -{ - Glib::Threads::Mutex::Lock lm (_mutex); - _files.push_back (d); -} - -/** @return true on error, otherwise false */ -bool -FileManager::allocate (FileDescriptor* d) -{ - Glib::Threads::Mutex::Lock lm (_mutex); - - if (!d->is_open()) { - - /* this file needs to be opened */ - - if (_open == _max_open) { - - /* We already have the maximum allowed number of files opened, so we must try to close one. - Find the unallocated, open file with the lowest last_used time. - */ - - double lowest_last_used = DBL_MAX; - list<FileDescriptor*>::iterator oldest = _files.end (); - - for (list<FileDescriptor*>::iterator i = _files.begin(); i != _files.end(); ++i) { - if ((*i)->is_open() && (*i)->_refcount == 0) { - if ((*i)->_last_used < lowest_last_used) { - lowest_last_used = (*i)->_last_used; - oldest = i; - } - } - } - - if (oldest == _files.end()) { - /* no unallocated and open files exist, so there's nothing we can do */ - return true; - } - - close (*oldest); - DEBUG_TRACE ( - DEBUG::FileManager, - string_compose ( - "closed file for %1 to release file handle; now have %2 of %3 open\n", - (*oldest)->_path, _open, _max_open - ) - ); - } - - if (d->open ()) { - DEBUG_TRACE (DEBUG::FileManager, string_compose ("open of %1 failed.\n", d->_path)); - return true; - } - - _open++; - - DEBUG_TRACE (DEBUG::FileManager, string_compose ("opened file for %1; now have %2 of %3 open.\n", d->_path, _open, _max_open)); - } - -#ifdef __APPLE__ - d->_last_used = mach_absolute_time(); -#elif defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_C_SOURCE >= 199309L) - struct timespec t; - clock_gettime (CLOCK_MONOTONIC, &t); - d->_last_used = t.tv_sec + (double) t.tv_nsec / 10e9; -#else - struct timeval now; - gettimeofday (&now, NULL); - d->_last_used = now.tv_sec + (double) now.tv_usec / 10e6; -#endif - - d->_refcount++; - - return false; -} - -/** Tell FileManager that a FileDescriptor is no longer needed for a given handle */ -void -FileManager::release (FileDescriptor* d) -{ - Glib::Threads::Mutex::Lock lm (_mutex); - - d->_refcount--; - assert (d->_refcount >= 0); -} - -/** Remove a file from our lists. It will be closed if it is currently open. */ -void -FileManager::remove (FileDescriptor* d) -{ - Glib::Threads::Mutex::Lock lm (_mutex); - - if (d->is_open ()) { - close (d); - DEBUG_TRACE ( - DEBUG::FileManager, - string_compose ("closed file for %1; file is being removed; now have %2 of %3 open\n", d->_path, _open, _max_open) - ); - } - - _files.remove (d); -} - -void -FileManager::close (FileDescriptor* d) -{ - /* we must have a lock on our mutex */ - - d->close (); - d->Closed (); /* EMIT SIGNAL */ - _open--; -} - -FileDescriptor::FileDescriptor (string const & n, bool w) - : _refcount (0) - , _last_used (0) - , _path (n) - , _writeable (w) -{ - -} - -FileManager* -FileDescriptor::manager () -{ - if (_manager == 0) { - _manager = new FileManager; - } - - return _manager; -} - -/** Release a previously allocated handle to this file */ -void -FileDescriptor::release () -{ - manager()->release (this); -} - - - -/** @param file_name Filename. - * @param writeable true to open writeable, otherwise false. - * @param mode Open mode for the file. - */ - -FdFileDescriptor::FdFileDescriptor (string const & file_name, bool writeable, mode_t mode) - : FileDescriptor (file_name, writeable) - , _fd (-1) - , _mode (mode) -{ - manager()->add (this); -} - -FdFileDescriptor::~FdFileDescriptor () -{ - manager()->remove (this); -} - -bool -FdFileDescriptor::is_open () const -{ - /* we must have a lock on the FileManager's mutex */ - - return _fd != -1; -} - -bool -FdFileDescriptor::open () -{ - /* we must have a lock on the FileManager's mutex */ - - /* files must be opened with O_BINARY flag on windows - * or it treats the file as a text stream and puts in - * line endings in etc - */ -#ifdef PLATFORM_WINDOWS -#define WRITE_FLAGS O_RDWR | O_CREAT | O_BINARY -#define READ_FLAGS O_RDONLY | O_BINARY -#else -#define WRITE_FLAGS O_RDWR | O_CREAT -#define READ_FLAGS O_RDONLY -#endif - _fd = ::g_open (_path.c_str(), _writeable ? WRITE_FLAGS : READ_FLAGS, _mode); - return (_fd == -1); -} - -void -FdFileDescriptor::close () -{ - /* we must have a lock on the FileManager's mutex */ - - ::close (_fd); - _fd = -1; -} - -/** @return fd, or -1 on error */ -int -FdFileDescriptor::allocate () -{ - bool const f = manager()->allocate (this); - if (f) { - return -1; - } - - /* this is ok thread-wise because allocate () has incremented - the Descriptor's refcount, so the file will not be closed - */ - return _fd; -} - - -void -FileDescriptor::set_path (const string& p) -{ - _path = p; -} - -/** @param file_name Filename. - * @param mode Mode to pass to fopen. - */ - -StdioFileDescriptor::StdioFileDescriptor (string const & file_name, std::string const & mode) - : FileDescriptor (file_name, false) - , _file (0) - , _mode (mode) -{ - manager()->add (this); -} - -StdioFileDescriptor::~StdioFileDescriptor () -{ - manager()->remove (this); -} - -bool -StdioFileDescriptor::is_open () const -{ - /* we must have a lock on the FileManager's mutex */ - - return _file != 0; -} - -bool -StdioFileDescriptor::open () -{ - /* we must have a lock on the FileManager's mutex */ - - _file = fopen (_path.c_str(), _mode.c_str()); - return (_file == 0); -} - -void -StdioFileDescriptor::close () -{ - /* we must have a lock on the FileManager's mutex */ - - fclose (_file); - _file = 0; -} - -/** @return FILE*, or 0 on error */ -FILE* -StdioFileDescriptor::allocate () -{ - bool const f = manager()->allocate (this); - if (f) { - return 0; - } - - /* this is ok thread-wise because allocate () has incremented - the Descriptor's refcount, so the file will not be closed - */ - return _file; -} diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc index 849d7f05fd..89f6818704 100644 --- a/libs/pbd/file_utils.cc +++ b/libs/pbd/file_utils.cc @@ -50,7 +50,6 @@ #endif #include "pbd/compose.h" -#include "pbd/file_manager.h" #include "pbd/file_utils.h" #include "pbd/debug.h" #include "pbd/error.h" @@ -282,11 +281,9 @@ copy_file(const std::string & from_path, const std::string & to_path) { if (!Glib::file_test (from_path, Glib::FILE_TEST_EXISTS)) return false; - FdFileDescriptor from_file(from_path, false, 0444); - FdFileDescriptor to_file(to_path, true, 0666); + int fd_from (::open (from_path.c_str(), O_RDONLY)); + int fd_to (::open (to_path.c_str(), O_CREAT|O_TRUNC|O_RDWR, 0666)); - int fd_from = from_file.allocate (); - int fd_to = to_file.allocate (); char buf[4096]; // BUFSIZ ?? ssize_t nread; diff --git a/libs/pbd/pbd/file_manager.h b/libs/pbd/pbd/file_manager.h deleted file mode 100644 index d85c88fa46..0000000000 --- a/libs/pbd/pbd/file_manager.h +++ /dev/null @@ -1,157 +0,0 @@ -/* - Copyright (C) 2010 Paul Davis - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#ifndef __pbd_file_manager_h__ -#define __pbd_file_manager_h__ - -#include <sys/types.h> -#include <string> -#include <map> -#include <list> -#include <glibmm/threads.h> - -#include "pbd/libpbd_visibility.h" -#include "pbd/signals.h" - -namespace PBD { - -class LIBPBD_API FileManager; - -/** Parent class for FileDescriptors. - * - * When a subclass is instantiated, the file it describes is added to a - * list. The FileDescriptor can be `allocated', meaning that its - * file will be opened on the filesystem, and can then be `released'. - * FileDescriptors are reference counted as they are allocated and - * released. When a descriptor's refcount is 0, the file on the - * filesystem is eligible to be closed if necessary to free up file - * handles for other files. - * - * The upshot of all this is that Ardour can manage the number of - * open files to stay within limits imposed by the operating system. - */ - -class LIBPBD_API FileDescriptor -{ -public: - FileDescriptor (std::string const &, bool); - virtual ~FileDescriptor () {} - - const std::string& path() const { return _path; } - - void release (); - virtual void set_path (const std::string&); - - /** Emitted when the file is closed */ - PBD::Signal0<void> Closed; - -protected: - - friend class FileManager; - - /* These methods and variables must be called / accessed - with a lock held on the FileManager's mutex - */ - - /** @return false on success, true on failure */ - virtual bool open () = 0; - virtual void close () = 0; - virtual bool is_open () const = 0; - - int _refcount; ///< number of active users of this file - double _last_used; ///< monotonic time that this file was last allocated - std::string _path; ///< file path - bool _writeable; ///< true if it should be opened writeable, otherwise false - - FileManager* manager (); - -private: - - static FileManager* _manager; -}; - - -/** FileDescriptor for a file to be opened using POSIX open */ -class LIBPBD_API FdFileDescriptor : public FileDescriptor -{ -public: - FdFileDescriptor (std::string const & file_name, bool writeable, mode_t mode); - ~FdFileDescriptor (); - - int allocate (); - -private: - - friend class FileManager; - - bool open (); - void close (); - bool is_open () const; - - int _fd; ///< file descriptor, or -1 if the file is closed - mode_t _mode; ///< mode to use when creating files -}; - -/** FileDescriptor for a file opened using stdio */ -class LIBPBD_API StdioFileDescriptor : public FileDescriptor -{ -public: - StdioFileDescriptor (std::string const & file_name, std::string const & mode); - ~StdioFileDescriptor (); - - FILE* allocate (); - -private: - - friend class FileManager; - - bool open (); - void close (); - bool is_open () const; - - FILE* _file; - std::string _mode; -}; - - -/** Class to limit the number of files held open */ -class LIBPBD_API FileManager -{ -public: - FileManager (); - - void add (FileDescriptor *); - void remove (FileDescriptor *); - - void release (FileDescriptor *); - bool allocate (FileDescriptor *); - -private: - - void close (FileDescriptor *); - - std::list<FileDescriptor*> _files; ///< files we know about - Glib::Threads::Mutex _mutex; ///< mutex for _files, _open and FileDescriptor contents - int _open; ///< number of open files - int _max_open; ///< maximum number of open files -}; - -} - -#endif diff --git a/libs/pbd/pbd/sndfile_manager.h b/libs/pbd/pbd/sndfile_manager.h deleted file mode 100644 index 7abc8ea3d6..0000000000 --- a/libs/pbd/pbd/sndfile_manager.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - Copyright (C) 2010 Paul Davis - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#ifndef __pbd_sndfile_manager_h__ -#define __pbd_sndfile_manager_h__ - -#include <sys/types.h> -#include <string> -#include <map> -#include <sndfile.h> -#include <glibmm/threads.h> - -#include "pbd/libpbd_visibility.h" -#include "pbd/signals.h" -#include "pbd/file_manager.h" - -namespace PBD { - -/** FileDescriptor for a file to be opened using libsndfile */ -class LIBPBD_API SndFileDescriptor : public FileDescriptor -{ -public: - SndFileDescriptor (std::string const & file_name, bool writeable, SF_INFO* info); - ~SndFileDescriptor (); - - SNDFILE* allocate (); - -private: - - friend class FileManager; - - bool open (); - void close (); - bool is_open () const; - - SNDFILE* _sndfile; ///< SNDFILE* pointer, or 0 if the file is closed - SF_INFO* _info; ///< libsndfile's info for this file -}; - -} - -#endif /* __pbd_sndfile_manager_h__ */ diff --git a/libs/pbd/sndfile_manager.cc b/libs/pbd/sndfile_manager.cc deleted file mode 100644 index c028bc11ba..0000000000 --- a/libs/pbd/sndfile_manager.cc +++ /dev/null @@ -1,95 +0,0 @@ -/* - Copyright (C) 2010 Paul Davis - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program 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 General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -/** @file libs/pbd/sndfile_manager.cc - * @brief A FileDescriptor for files opened using libsndfile. - */ - -#include <sys/time.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <cassert> -#include "pbd/compose.h" -#include "pbd/sndfile_manager.h" -#include "pbd/debug.h" - -using namespace std; -using namespace PBD; - -/** @param file_name Filename. - * @param writeable true to open writeable, otherwise false. - * @param info SF_INFO for the file. - */ - -SndFileDescriptor::SndFileDescriptor (string const & file_name, bool writeable, SF_INFO* info) - : FileDescriptor (file_name, writeable) - , _sndfile (0) - , _info (info) -{ - manager()->add (this); -} - -SndFileDescriptor::~SndFileDescriptor () -{ - manager()->remove (this); -} - -/** @return SNDFILE*, or 0 on error */ -SNDFILE* -SndFileDescriptor::allocate () -{ - bool const f = manager()->allocate (this); - if (f) { - return 0; - } - - /* this is ok thread-wise because allocate () has incremented - the Descriptor's refcount, so the file will not be closed - */ - return _sndfile; -} - -void -SndFileDescriptor::close () -{ - /* we must have a lock on the FileManager's mutex */ - - assert (_sndfile); - sf_close (_sndfile); - _sndfile = 0; -} - -bool -SndFileDescriptor::is_open () const -{ - /* we must have a lock on the FileManager's mutex */ - - return _sndfile != 0; -} - -bool -SndFileDescriptor::open () -{ - /* we must have a lock on the FileManager's mutex */ - - _sndfile = sf_open (_path.c_str(), _writeable ? SFM_RDWR : SFM_READ, _info); - return (_sndfile == 0); -} - diff --git a/libs/pbd/wscript b/libs/pbd/wscript index ba0b265b99..1702627cc0 100644 --- a/libs/pbd/wscript +++ b/libs/pbd/wscript @@ -46,7 +46,6 @@ libpbd_sources = [ 'epa.cc', 'error.cc', 'ffs.cc', - 'file_manager.cc', 'file_utils.cc', 'fpu.cc', 'glib_semaphore.cc', @@ -68,7 +67,6 @@ libpbd_sources = [ 'semutils.cc', 'shortpath.cc', 'signals.cc', - 'sndfile_manager.cc', 'stacktrace.cc', 'stateful_diff_command.cc', 'stateful.cc', |