CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

LocalCacheFile Class Reference

#include <LocalCacheFile.h>

Inheritance diagram for LocalCacheFile:
Storage IOInput IOOutput

List of all members.

Public Member Functions

virtual void close (void)
virtual void flush (void)
 LocalCacheFile (Storage *base, const std::string &tmpdir="")
virtual IOOffset position (IOOffset offset, Relative whence=SET)
virtual bool prefetch (const IOPosBuffer *what, IOSize n)
virtual IOSize read (void *into, IOSize n)
virtual IOSize read (void *into, IOSize n, IOOffset pos)
virtual IOSize readv (IOBuffer *into, IOSize n)
virtual IOSize readv (IOPosBuffer *into, IOSize n)
virtual void resize (IOOffset size)
virtual IOSize write (const void *from, IOSize n)
virtual IOSize write (const void *from, IOSize n, IOOffset pos)
virtual IOSize writev (const IOPosBuffer *from, IOSize n)
virtual IOSize writev (const IOBuffer *from, IOSize n)
 ~LocalCacheFile (void)

Private Member Functions

void cache (IOOffset start, IOOffset end)

Private Attributes

unsigned int cacheCount_
unsigned int cacheTotal_
bool closedFile_
Filefile_
IOOffset image_
std::vector< char > present_
Storagestorage_

Detailed Description

Proxy class to copy a file locally in large chunks.

Definition at line 10 of file LocalCacheFile.h.


Constructor & Destructor Documentation

LocalCacheFile::LocalCacheFile ( Storage base,
const std::string &  tmpdir = "" 
)

Definition at line 22 of file LocalCacheFile.cc.

References cacheTotal_, Exception, reco_application_tbsim_DetSim-Digi_cfg::File, file_, image_, AlCaHLTBitMon_ParallelJobs::p, listBenchmarks::pattern, present_, File::resize(), AlCaHLTBitMon_QueryRunRegistry::string, and groupFilesInBlocks::temp.

  : image_(base->size()),
    file_(0),
    storage_(base),
    closedFile_(false),
    cacheCount_(0),
    cacheTotal_((image_ + CHUNK_SIZE - 1) / CHUNK_SIZE)
{
  present_.resize(cacheTotal_, 0);

  std::string pattern(tmpdir);
  if (pattern.empty())
    if (char *p = getenv("TMPDIR"))
      pattern = p;
  if (pattern.empty())
    pattern = "/tmp";
  pattern += "/cmssw-shadow-XXXXXX";

  std::vector<char> temp(pattern.c_str(), pattern.c_str()+pattern.size()+1);
  int fd = mkstemp(&temp[0]);
  if (fd == -1)
    throw cms::Exception("LocalCacheFile")
      << "Cannot create temporary file '" << pattern << "': "
      << strerror(errno) << " (error " << errno << ")";

  unlink(&temp[0]);
  file_ = new File(fd);
  file_->resize(image_);
}
LocalCacheFile::~LocalCacheFile ( void  )

Definition at line 52 of file LocalCacheFile.cc.

References file_, and storage_.

{
  delete file_;
  delete storage_;
}

Member Function Documentation

void LocalCacheFile::cache ( IOOffset  start,
IOOffset  end 
) [private]

Definition at line 59 of file LocalCacheFile.cc.

References cacheCount_, cacheTotal_, CHUNK_SIZE, Storage::close(), closedFile_, alignCSCRings::e, Exception, IOChannel::fd(), file_, image_, getHLTprescales::index, min, present_, Storage::read(), dqm_diff::start, storage_, and svgfig::window().

Referenced by prefetch(), read(), and readv().

{
  start = (start / CHUNK_SIZE) * CHUNK_SIZE;
  end = std::min(end, image_);

  IOSize nread = 0;
  IOSize index = start / CHUNK_SIZE;

  while (start < end)
  {
    IOSize len = std::min(image_ - start, CHUNK_SIZE);
    if (! present_[index])
    {
      void *window = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, file_->fd(), start);
      if (window == MAP_FAILED)
        throw cms::Exception("LocalCacheFile")
          << "Unable to map a window of local cache file: "
          << strerror(errno) << " (error " << errno << ")";

      try
      {
        nread = storage_->read(window, len, start);
      }
      catch (cms::Exception &e)
      {
        munmap(window, len);
        std::ostringstream ost;
        ost << "Unable to cache " << len << " byte file segment at " << start << ": ";
        throw cms::Exception("LocalCacheFile", ost.str(), e);
      }

      munmap(window, len);

      if (nread != len)
        throw cms::Exception("LocalCacheFile")
          << "Unable to cache " << len << " byte file segment at " << start
          << ": got only " << nread << " bytes back";

      present_[index] = 1;
      ++cacheCount_;
      if (cacheCount_ == cacheTotal_)
      {
        storage_->close();
        closedFile_ = true;
      }
    }

    start += len;
    ++index;
  }
}
void LocalCacheFile::close ( void  ) [virtual]

Reimplemented from Storage.

Definition at line 181 of file LocalCacheFile.cc.

References Storage::close(), File::close(), closedFile_, file_, and storage_.

{
  if (!closedFile_)
  {
    storage_->close();
  }
  file_->close();
}
void LocalCacheFile::flush ( void  ) [virtual]

Reimplemented from Storage.

Definition at line 177 of file LocalCacheFile.cc.

References nowrite().

{ nowrite("flush"); }
IOOffset LocalCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
) [virtual]

Implements Storage.

Definition at line 169 of file LocalCacheFile.cc.

References file_, and File::position().

{ return file_->position(offset, whence); }
bool LocalCacheFile::prefetch ( const IOPosBuffer what,
IOSize  n 
) [virtual]

Reimplemented from Storage.

Definition at line 191 of file LocalCacheFile.cc.

References cache(), end, file_, i, n, IOPosBuffer::offset(), File::prefetch(), IOPosBuffer::size(), and dqm_diff::start.

{
  for (IOSize i = 0; i < n; ++i)
  {
    IOOffset start = what[i].offset();
    IOOffset end = start + what[i].size();
    cache(start, end);
  }
  
  return file_->prefetch(what, n);
}
IOSize LocalCacheFile::read ( void *  into,
IOSize  n 
) [virtual]

Read into into at most n number of bytes.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

Returns:
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons.
Exceptions:
Incase of error, a #IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

Implements IOInput.

Definition at line 112 of file LocalCacheFile.cc.

References cache(), file_, File::position(), and File::read().

{
  IOOffset here = file_->position();
  cache(here, here + n);

  return file_->read(into, n);
}
IOSize LocalCacheFile::read ( void *  into,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 121 of file LocalCacheFile.cc.

References cache(), file_, and File::read().

{
  cache(pos, pos + n);
  return file_->read(into, n, pos);
}
IOSize LocalCacheFile::readv ( IOBuffer into,
IOSize  buffers 
) [virtual]

Read from the input stream into multiple scattered buffers. There are buffers to fill in an array starting at into; the memory those buffers occupy does not need to be contiguous. The buffers are filled in the order given, eac buffer is filled fully before the subsequent buffers.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

The base class implementation uses read(void *, IOSize) method, but derived classes may implement a more efficient alternative.

Returns:
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons. Note that the return value indicates the number of bytes read, not the number of buffers; it is the sum total of bytes filled into all the buffers.
Exceptions:
Incase of error, a #IOError exception is thrown. However if some data has already been read, the error is swallowed and the method returns the data read so far. It is assumed that persistent errors will occur anyway on the next read and sporadic errors like stream becoming unvailable can be ignored. Use xread() if a different policy is desirable.

Reimplemented from IOInput.

Definition at line 128 of file LocalCacheFile.cc.

References cache(), end, file_, i, n, File::position(), File::readv(), Storage::size(), and dqm_diff::start.

{
  IOOffset start = file_->position();
  IOOffset end = start;
  for (IOSize i = 0; i < n; ++i)
    end += into[i].size();
  cache(start, end);

  return file_->readv(into, n);
}
IOSize LocalCacheFile::readv ( IOPosBuffer into,
IOSize  n 
) [virtual]

Reimplemented from Storage.

Definition at line 140 of file LocalCacheFile.cc.

References cache(), end, i, n, IOPosBuffer::offset(), Storage::readv(), IOPosBuffer::size(), dqm_diff::start, and storage_.

{
  for (IOSize i = 0; i < n; ++i)
  {
    IOOffset start = into[i].offset();
    IOOffset end = start + into[i].size();
    cache(start, end);
  }

  return storage_->readv(into, n);
}
void LocalCacheFile::resize ( IOOffset  size) [virtual]

Implements Storage.

Definition at line 173 of file LocalCacheFile.cc.

References nowrite().

{ nowrite("resize"); }
IOSize LocalCacheFile::write ( const void *  from,
IOSize  n,
IOOffset  pos 
) [virtual]

Reimplemented from Storage.

Definition at line 157 of file LocalCacheFile.cc.

References nowrite().

{ nowrite("write"); return 0; }
IOSize LocalCacheFile::write ( const void *  from,
IOSize  n 
) [virtual]

Write n bytes of data starting at address from.

Returns:
The number of bytes written. Normally this will be n, but can be less, even zero, for example if the stream is non-blocking mode and cannot accept input at this time.
Exceptions:
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception -- the return value will be less than requested.

Implements IOOutput.

Definition at line 153 of file LocalCacheFile.cc.

References nowrite().

{ nowrite("write"); return 0; }
IOSize LocalCacheFile::writev ( const IOPosBuffer from,
IOSize  n 
) [virtual]

Reimplemented from Storage.

Definition at line 165 of file LocalCacheFile.cc.

References nowrite().

{ nowrite("writev"); return 0; }
IOSize LocalCacheFile::writev ( const IOBuffer from,
IOSize  buffers 
) [virtual]

Write to the output stream from multiple buffers. There are buffers to fill in an array starting at from. The buffers are filled in the order given, each buffer fully before the subsequent buffers. The method uses write(const void *, IOSize), but may be implemented more efficiently in derived classes.

Note that derived classes should not normally call this method, as it simply routes the call back to derived class through the other virtual functions. Use this method only at the "outside edge" when transferring calls from one object to another, not in up/down calls in the inheritance tree.

Returns:
The number of bytes actually written. This is less or equal to the size of the buffers. The value can be less than requested if the stream is unable to accept all the output for platform or implementation reasons. Note that the return value indicates the number of bytes written, not the number of buffers; it is the sum total of bytes written from all the buffers.
Exceptions:
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception -- the return value will be less than requested.

Reimplemented from IOOutput.

Definition at line 161 of file LocalCacheFile.cc.

References nowrite().

{ nowrite("writev"); return 0; }

Member Data Documentation

unsigned int LocalCacheFile::cacheCount_ [private]

Definition at line 42 of file LocalCacheFile.h.

Referenced by cache().

unsigned int LocalCacheFile::cacheTotal_ [private]

Definition at line 43 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

Definition at line 41 of file LocalCacheFile.h.

Referenced by cache(), and close().

Definition at line 37 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

std::vector<char> LocalCacheFile::present_ [private]

Definition at line 38 of file LocalCacheFile.h.

Referenced by cache(), and LocalCacheFile().

Definition at line 40 of file LocalCacheFile.h.

Referenced by cache(), close(), readv(), and ~LocalCacheFile().