Permalink
Browse files

Move filename expansion into models.py

  • Loading branch information...
1 parent 7f084ae commit 146aa4d8d39f53d4055b6f1c29f1140ae72bd344 @schmichael committed Nov 19, 2012
Showing with 32 additions and 24 deletions.
  1. +10 −24 mmstats/_mmap.py
  2. +22 −0 mmstats/models.py
View
@@ -1,42 +1,28 @@
+import collections
import ctypes
import ctypes.util
import errno
libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
import mmap as stdlib_mmap
import os
-import sys
-
-
-from . import libgettid
-from .defaults import DEFAULT_PATH, DEFAULT_FILENAME
PAGESIZE = stdlib_mmap.PAGESIZE
-def init_mmap(path=DEFAULT_PATH, filename=DEFAULT_FILENAME, size=PAGESIZE):
- """Given path, filename => filename, size, mmap
+MmapInfo = collections.namedtuple('MmapInfo', ('fd', 'size', 'pointer'))
- :param path: path to store mmaped files
- :param filename: filename template for mmaped files
- :param size: minimum size of the mmaped region (will be rounded up to the
- nearest multiple of `PAGESIZE`)
- Substitutions documented in :class:`~mmstats.models.BaseMmStats`
- """
- substitutions = {
- 'CMD': os.path.basename(sys.argv[0]),
- 'PID': os.getpid(),
- 'TID': libgettid.gettid(),
- }
- # Format filename and path with substitution variables
- filename = filename.format(**substitutions)
- path = path.format(**substitutions)
+def init_mmap(filename, size=PAGESIZE):
+ """Create an mmap given a location `filename` and minimum `size` in bytes
- full_path = os.path.join(path, filename)
+ Returns an MmapInfo tuple with the file descriptor, actual size, and a
+ pointer to the begging of the mmap.
+ Note that the size returned is rounded up to the nearest PAGESIZE.
+ """
# Create new empty file to back memory map on disk
- fd = os.open(full_path, os.O_CREAT | os.O_TRUNC | os.O_RDWR)
+ fd = os.open(filename, os.O_CREAT | os.O_TRUNC | os.O_RDWR)
if size > PAGESIZE:
if size % PAGESIZE:
size = size + (PAGESIZE - (size % PAGESIZE))
@@ -46,7 +32,7 @@ def init_mmap(path=DEFAULT_PATH, filename=DEFAULT_FILENAME, size=PAGESIZE):
# Zero out the file
os.ftruncate(fd, size)
m_ptr = mmap(size, fd)
- return (fd, full_path, size, m_ptr)
+ return MmapInfo(fd, size, m_ptr)
# Linux consts from /usr/include/bits/mman.h
View
@@ -8,6 +8,28 @@
from .defaults import DEFAULT_PATH, DEFAULT_FILENAME
+def _expand_filename(path=DEFAULT_PATH, filename=DEFAULT_FILENAME):
+ """Compute mmap's full path given a `path` and `filename`.
+
+ :param path: path to store mmaped files
+ :param filename: filename template for mmaped files
+ :returns: fully expanded path and filename
+ :rtype: str
+
+ Substitutions documented in :class:`~mmstats.models.BaseMmStats`
+ """
+ substitutions = {
+ 'CMD': os.path.basename(sys.argv[0]),
+ 'PID': os.getpid(),
+ 'TID': libgettid.gettid(),
+ }
+ # Format filename and path with substitution variables
+ filename = filename.format(**substitutions)
+ path = path.format(**substitutions)
+
+ return os.path.join(path, filename)
+
+
class FieldState(object):
"""Holds field state for each Field instance"""

0 comments on commit 146aa4d

Please sign in to comment.