1. 16 Jan, 2017 3 commits
    • Kirill Smelkov's avatar
      bigfile/py: Factor out code to "unpin" a buffer to separate functions · 3a8e1beb
      Kirill Smelkov authored
      This code was added in 6da5172e (bigfile/py: Teach storeblk() how to
      correctly propagate traceback on error) to unpin a storeblk pybuf to not
      care whether its refcount == 1 - this way to be able to propagate python
      error upper not caring whether pybuf is still referenced or not.
      
      9aa6a5d7 (bigfile/py: Teach loadblk() to automatically break reference
      cycles to pybuf) adds a note that such unpinning is not strictly
      correct: becuase of other buffer objects were created from pybuf - they
      are copying pointers on initialization and unpinning pybuf won't adjust
      them.
      
      However for loadblk codepath it turned out (see next patch) it is not
      completely possible to unreference pybuf in all cases. For this reason
      loadblk will be falling back to unpinning too.
      
      As a preparatory step move common code to shared functions.
      3a8e1beb
    • Kirill Smelkov's avatar
      bigfile/py/loadblk: Replace pybuf with a stub object in calling frame in case it stays alive · 61b18a40
      Kirill Smelkov authored
      It turns out some code wants to store tracebacks e.g. for further
      logging/whatever. This way GC won't help to free up references to pybuf.
      However if pybuf remain referenced only from calling frames, we can
      change there reference to pybuf to a stub object "<pybuf>" and this way
      remove the reference.
      
      With added test but without loadblk changes the failure would be as:
      
          pybigfile_loadblk WARN: pybuf->ob_refcnt != 1 even after GC:
          pybuf (ob_refcnt=2):    <read-write buffer ptr 0x7fae4911f000, size 2097152 at 0x7fae4998cef0>
          pybuf referrers:        [<frame object at 0x556daff41aa0>]		<-- NOTE
          bigfile/_bigfile.c:613 pybigfile_loadblk        BUG!
      61b18a40
    • Kirill Smelkov's avatar
      bigfile/py/test_basic: Rework exception testing codepath so it is active on py3 also · f01b27d2
      Kirill Smelkov authored
      As comments being removed states "on python3 exception state is cleared
      upon exiting from `except`" - so let's move exc_* fetching under except
      clause - this way we'll get correct exception objects on both py2 and py3.
      f01b27d2
  2. 12 Jan, 2017 2 commits
    • Kirill Smelkov's avatar
      bigfile/py: Factor outcode to get list of objects that refer to obj to XPyObject_GetReferrers() · ca7c1b6d
      Kirill Smelkov authored
      We'll need it in next patch to get and analyze this list.
      ca7c1b6d
    • Kirill Smelkov's avatar
      bigfile/py: Dump pybuf referrers if pybuf->ob_refcnt != 1 before dying in loadblk epilogue · 20b41a5a
      Kirill Smelkov authored
      Instead of only printing "BUG" let's print information about objects
      which still refer to pybuf - to help debugging.
      
      For example with the following artificial pybuf leak
      
      ```
      diff --git a/bigfile/tests/test_basic.py b/bigfile/tests/test_basic.py
      index c737621..f5e057a 100644
      --- a/bigfile/tests/test_basic.py
      +++ b/bigfile/tests/test_basic.py
      @@ -126,6 +126,7 @@ def test_basic():
      
       # test that python exception state is preserved across pagefaulting
       def test_pagefault_savestate():
      +    zzz = []
           class BadFile(BigFile):
               def loadblk(self, blk, buf):
                   # simulate some errors in-between to overwrite thread exception
      @@ -154,6 +155,7 @@ def loadblk(self, blk, buf):
                   # which result in holding additional ref to buf, but loadblk caller
                   # will detect and handle this situation via garbage-collecting
                   # above cycle.
      +            zzz.append(buf)
      
                   self.loadblk_run = 1
      ```
      
      it dies this way:
      
          bigfile/_bigfile.c:567 pybigfile_loadblk WARN: pybuf->ob_refcnt != 1 even after GC:
          pybuf (ob_refcnt=2):    <read-write buffer ptr 0x7f08d3e88000, size 2097152 at 0x7f08d48b7070>
          pybuf referrers:        [[<read-write buffer ptr 0x7f08d3e88000, size 2097152 at 0x7f08d48b7070>]]
          bigfile/_bigfile.c:573 pybigfile_loadblk        BUG!
      20b41a5a
  3. 11 Jan, 2017 2 commits
    • Kirill Smelkov's avatar
      bigfile/py: Teach loadblk() to automatically break reference cycles to pybuf · 9aa6a5d7
      Kirill Smelkov authored
      Because otherwise we bug on pybuf->ob_refcnt != 1.
      
      Such cycles might happen if inside loadblk implementation an exception
      is internally raised and then caught even in deeply internal function
      which does not receive pybuf as argument or by some other way:
      
      After
      
      	_, _, exc_traceback = sys.exc_info()
      
      there is a reference loop created:
      
      	exc_traceback
      	  |        ^
      	  |        |
      	  v     .f_localsplus
      	 frame
      
      and since exc_traceback object holds reference to deepest frame, which via f_back
      will be holding reference to frames up to frame with pybuf argument, it
      will result in additional reference to pybuf being held until the above
      cycle is garbage collected.
      
      So to solve the problem while leaving loadblk, if pybuf->ob_refcnt !=
      let's first do garbage-collection, and only then recheck left
      references. After GC reference-loops created by exceptions should go
      away.
      
      NOTE PyGC_Collect() (C way to call gc.collect()) always performs
          GC - it is not affected by gc.disable() which disables only
          _automatic_ garbage collection.
      
      NOTE it turned out out storeblk logic to unpin pybuf (see
          6da5172e "bigfile/py: Teach storeblk() how to correctly propagate
          traceback on error") is flawed, because when e.g. creating memoryview
          from pybuf internal pointer is copied and then clearing original buf
          does not result in clearing the copy.
      
      NOTE it is ok to do gc.collect() from under sighandler - at least we are
          already doing it for a long time via running non-trivial python code
          which for sure triggers automatic GC from time to time (see also
          786d418d "bigfile: Simple test that we can handle GC from-under
          sighandler" for the reference)
      
      Fixes: nexedi/wendelin.core#7
      9aa6a5d7
    • Kirill Smelkov's avatar
      bigfile/py: Factor exception state clearing code to XPyErr_FullClear() · b4c269eb
      Kirill Smelkov authored
      In the next patch we will need to use this from several places.
      b4c269eb
  4. 28 Sep, 2016 2 commits
  5. 14 Aug, 2016 1 commit
    • Kirill Smelkov's avatar
      bigfile/zodb/ZBlk1: Don't miss to deactivate/free internal .chunktab buckets in loadblkdata() · 542917d1
      Kirill Smelkov authored
      13c0c17c (bigfile/zodb: Format #1 which is optimized for small changes)
      used BTree to organize ZBlk1 block's chunks and for loadblkdata() added
      "TODO we are missing to free internal BTree structures on data load".
      
      nexedi/wendelin.core#3 besides other
      things showed that even when we deactivate ZData objects, we are still
      keeping them as ghosts occupying memory and the same for IOBucket
      objects.
      
      This all happens because there is no proper way to deactivate whole
      btree - including internal buckets objects. And since internal buckets
      are not deactivated, they stay in picklecache and thus hold a reference
      to ZData objects and ZData objects in turn, even if explicitly
      deactivated, stay in memory.
      
      We can fix this all via implementing whole-btree deactivation procedure.
      
      To do so we need to iterate over all btree buckets recursively, but
      unfortunately there is no BTree API to access/iterate btree's buckets.
      We can however still get reference to first top-level buckets via
      gc.get_referents(btree) and then scan buckets further without hacks.
      
      gc.get_referents(btree) is a hack, but
      
      - it works in O(1)  (we only get pointers from btree, not scanning all
        gcable objects and deducing them)
      - it works reliable if we filter out non-interesting objects.
      
      So in the end it works.
      
      Before the patch loading more and more ZBlk1 data with objgraph
      instrumentation was showing itself like
      
          #                                    Nobj        δ
          wendelin.bigfile.file_zodb.ZData     7168      +512
          BTrees.IOBTree.IOBucket               238       +17
          BTrees.IOBTree.IOBTree                 14        +1
      
      and after this patch we now have
      
          BTrees.IOBTree.IOBTree                 14        +1
      
      we cannot remove that "IOBTree + 1", since ZBlk1 is holding direct
      reference on it (via .chunktab) and we have to keep ZBlk1 live with
      ._v_zfile and ._v_zblk set for invalidation to work. "+1 IOBtree" is
      however small - 144 bytes per 2M (= 0.006%) so we can neglect that the
      same way we neglect keeping ZBlk1 staying live for each block.
      542917d1
  6. 14 Jul, 2016 4 commits
    • Kirill Smelkov's avatar
      wendelin.core v0.7 · f7803634
      Kirill Smelkov authored
      f7803634
    • Kirill Smelkov's avatar
      bigfile/virtmem: usleep() needs unistd.h · d9d6409f
      Kirill Smelkov authored
      The following started to appear after recent gcc upgrade on my host:
      
      bigfile/virtmem.c: In function `vma_on_pagefault':
      bigfile/virtmem.c:696:9: warning: implicit declaration of function `usleep' [-Wimplicit-function-declaration]
               usleep(10000);  // XXX with 1000 uslepp still busywaits
      d9d6409f
    • Kirill Smelkov's avatar
      setup: Propagate only setuptools, not whole sys.path · 8f624957
      Kirill Smelkov authored
      This updates and fixes 487e5226 (setup: specify setuptools location
      explicitly when calling make.) to use @kazuhiko original idea to
      propagate only setuptools location. The reason is - when propagating
      whole sys.path things break under tox tests:
      
      ---- 8< ----
      ========================================================================== test session starts ===========================================================================
      platform linux2 -- Python 2.7.12, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
      rootdir: /home/kirr/src/wendelin/release/wendelin.core, inifile:
      collected 33 items
      
      bigarray/tests/test_arrayzodb.py .......
      bigarray/tests/test_basic.py ........
      bigfile/tests/test_basic.py ....
      bigfile/tests/test_filefile.py .
      bigfile/tests/test_filezodb.py ........
      bigfile/tests/test_thread.py ....
      lib/tests/test_calc.py .
      
      ======================================================================= 33 passed in 14.14 seconds =======================================================================
      x86_64-linux-gnu-gcc -pthread -g -Wall -D_GNU_SOURCE -std=gnu99 -fplan9-extensions -Wno-declaration-after-statement -Wno-error=declaration-after-statement  -Iinclude -I3rdparty/ccan -I3rdparty/include   bigfile/tests/tfault.c lib/bug.c lib/utils.c 3rdparty/ccan/ccan/tap/tap.c  -o bigfile/tests/tfault.t
      t/tfault-run bigfile/tests/tfault.t faultr on_pagefault
      ok 1 - !pagefault_init()
      Fatal Python error: Py_Initialize: Unable to get the locale encoding
        File ".../wendelin.core/.tox/py27-ZODB3-zblk0-fs-numpy110/lib/python2.7/encodings/__init__.py", line 123
          raise CodecRegistryError,\
                                  ^
      SyntaxError: invalid syntax
      
      Current thread 0x00007f9b80024780 (most recent call first):
      t/tfault-run: line 28: 21521 Аварийный останов         (core dumped) gdb -q -batch $tfault core > core.info
      E: can't gdb(core)
      Makefile:189: ошибка выполнения рецепта для цели «faultr.tfault»
      make: *** [faultr.tfault] Ошибка 1
      rm bigfile/tests/test_virtmem.t bigfile/tests/test_ram.t bigfile/tests/tfault.t bigfile/tests/test_pagemap.t
      error: Failed to execute `make test`
      ERROR: InvocationError: '.../wendelin.core/.tox/py27-ZODB3-zblk0-fs-numpy110/bin/python setup.py test'
      ________________________________________________________________________________ summary _________________________________________________________________________________
      ERROR:   py27-ZODB3-zblk0-fs-numpy110: commands failed
      ---- 8< ----
      
      What happens here is:
      
      - gdb is used in automated tests
      - gdb is linked with libpython3.5
      - tox is currently running tests with python27
      - setup.py sets PYTHONPATH to whole path from python27
      - gdb, upon starting, initializes python runtime, which tries to load
        py27 modules under py35 -> oops.
      
      So propagating only setuptools location practically solves the issue for
      now, but still there is potential risk that in future, there will be
      other modules put in the same location as setuptools (location is parent
      dir of a module/package) which python tries to load on startup and which
      might be incompatible between 2 & 3.
      
      Thus not setting PYTHONPATH at all - e.g. creating environments
      virtualenv way - would be better, but buildout fundamentally works the
      other way. We can not also use -c 'buildout sys.path hack' in $PYTHON
      itself, as $PYTHON is used in general way inside Makefile.
      
      So let the hack with setuptools location in PYTHONPATH stay there until
      it practically works.
      8f624957
    • Kazuhiko's avatar
      setup: specify setuptools location explicitly when calling make. · 487e5226
      Kazuhiko authored
      If wendelin.core is built under buildout and setuptools exists only in
      buildout environment, 'python setup.py' called inside make will fail
      without this change.
      
          Building 'wendelin.core'
          Running easy_install:
          "/path/to/python2.7" "-c" "import sys; sys.path[0:0] = ['/path/to/setuptools-19.6.2-py2.7.egg']; from setuptools.command.easy_install import main; main()"...
          path_list=['/path/to/setuptools-19.6.2-py2.7.egg']
          ...
          <<< setup.py: os.system('make %s PYTHON="%s"' % (target, sys.executable))
          ...
          make[1]: Leaving directory '/tmp/xxx/wendelin.core-0.6/3rdparty/ccan'
          /path/to/python2.7 setup.py ll_build_ext --inplace
          Traceback (most recent call last):
            File "setup.py", line 18, in <module>
              from setuptools import setup, Extension, Command, find_packages
          ImportError: No module named setuptools
          Makefile:40: recipe for target 'bigfile/_bigfile.so' failed
          make: *** [bigfile/_bigfile.so] Error 1
          error: Setup script exited with error: Failed to execute `make all`
      
      [ @kirr: as a solution we are propagating whole python path via make.
      
        This should cover both setuptools case, and any other future potential
        "preloaded-by-buildout" egg. ]
      
      /reviewed-on nexedi/wendelin.core!1
      487e5226
  7. 06 Jul, 2016 1 commit
    • Kirill Smelkov's avatar
      Fix build for Python 3.5 · e6beab19
      Kirill Smelkov authored
      @kazuhiko reports that wendelin.core build is currently broken on Python 3.5.
      Indeed it was:
      
          In file included from bigfile/_bigfile.c:37:0:
          ./include/wendelin/compat_py2.h: In function ‘_PyThreadState_UncheckedGetx’:
          ./include/wendelin/compat_py2.h:66:28: warning: implicit declaration of function ‘_Py_atomic_load_relaxed’ [-Wimplicit-function-declaration]
               return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
                                      ^
          ./include/wendelin/compat_py2.h:66:53: error: ‘_PyThreadState_Current’ undeclared (first use in this function)
               return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
                                                               ^
          ./include/wendelin/compat_py2.h:66:53: note: each undeclared identifier is reported only once for each function it appears in
          ./include/wendelin/compat_py2.h:67:1: warning: control reaches end of non-void function [-Wreturn-type]
           }
           ^
      
      The story here is that in 3.5 they decided to remove direct access to
      _PyThreadState_Current and atomic implementations - because that might
      semantically conflict with other headers implementing atomics - and
      provide only access by function.
      
      Starting from Python 3.5.2rc1 the function to get current thread state
      without asserting it is !NULL - _PyThreadState_UncheckedGet() - was added:
      
          https://github.com/python/cpython/commit/df858591
      
      so for those python versions we can directly use it.
      
      After the fix wendelin.core tox tests pass under all python2.7, python3.4 and python3.5.
      
      More context here:
      
          https://bugs.python.org/issue26154
          https://bugs.python.org/issue25150
      
      Fixes: nexedi/wendelin.core#1
      e6beab19
  8. 01 Jul, 2016 2 commits
  9. 27 Jun, 2016 2 commits
  10. 24 Jun, 2016 1 commit
    • Kirill Smelkov's avatar
      bigfile/pagemap: Fix non-leaf page iteration · ee9bcd00
      Kirill Smelkov authored
      Since the beginning of pagemap (45af76e6 "bigfile/pagemap: specialized
      {} uint64 -> void * mapping") we had a bug sitting in
      __pagemap_for_each_leaftab() (non-leaf iterating logic behind
      pagemap_for_each):
      
      After entry to stack-down was found, we did not updated tailv[l]
      accordingly. Thus if there are non-adjacent entries an entry could be
      e.g. emitted many times:
      
           l 3  __down 0x7f79da1ee000
           tailv[4]: 0x7f79da1ee000
            -> tailv[4] 0x7f79da1ee000  __down 0x7f79da1ed000
      
           l 4  __down 0x7f79da1ed000
           tailv[5]: 0x7f79da1ed000
           h 5  l 5  leaftab: 0x7f79da1ed000      <--
            lvl 5  idx 169  page 0x55aa
          ok 9 - pagemap_for_each(0) == 21930
      
           l 5  __down (nil)
           tailv[4]: 0x7f79da1ee008
            -> tailv[4] 0x7f79da1ee008  __down 0x7f79da1ed000
      
           l 4  __down 0x7f79da1ed000
           tailv[5]: 0x7f79da1ed000
           h 5  l 5  leaftab: 0x7f79da1ed000      <--
            lvl 5  idx 169  page 0x55aa
          not ok 10 - pagemap_for_each(1) == 140724106500272
      
      And many-time-emitted entries are not only incorrect, but can also lead
      to not-handled segmentation faults in e.g. fileh_close():
      
          https://lab.nexedi.com/nexedi/wendelin.core/blob/v0.6-1-gb0b2c52/bigfile/virtmem.c#L179
      
          /* drop all pages (dirty or not) associated with this fileh */
          pagemap_for_each(page, &fileh->pagemap) {
              /* it's an error to close fileh to mapping of which an access is
               * currently being done in another thread */
              BUG_ON(page->state == PAGE_LOADING);
              page_drop_memory(page);
              list_del(&page->lru);                           <-- HERE
              bzero(page, sizeof(*page)); /* just in case */
              free(page);
          }
      
      ( because after first bzero of a page, the page is all 0 bytes including
        page->lru{.next,.prev} so on the second time when the same page is
        emitted by pagemap_for_each, list_del(&page->lru) will try to set
        page->lru.next = ... which will segfault. )
      
      So fix it by properly updating tailv[l] while we scan/iterate current level.
      
      NOTE
      
      This applies only to non-leaf pagemap levels, as leaf level is scanned
      with separate loop in pagemap_for_each. That's why we probably did not
      noticed this earlier - up until now our usual workloads was to change
      data in adjacent batches and that means adjacent pages.
      
      Though today @Tyagov was playing with wendelin.core in some other way and
      it uncovered the bug.
      ee9bcd00
  11. 13 Jun, 2016 5 commits
  12. 12 Jun, 2016 1 commit
    • Kirill Smelkov's avatar
      3rdparty/ccan: Update · 89d099af
      Kirill Smelkov authored
      Just update to latest CCAN for it to be a fresh one.
      
      Throught the modules we use there are no real updates, just a fix for
      one warning in array_size.
      89d099af
  13. 20 Apr, 2016 1 commit
    • Kirill Smelkov's avatar
      bigfile/zodb: Don't write ZBlk to DB if it was not changed · 6773fda0
      Kirill Smelkov authored
      For ZBlk1 we already compare ZData content about whether it was changed
      compared to data already stored to DB, and do not store it twice if data
      is the same.
      
      However ZBlk itself is always marked as changed, if corresponding memory
      page was dirtied. This results in transactions like
      
      Trans #33915309 tid=03b6944919befeee time=2016-04-17 22:01:06.034237 offset=140320105842
          status=' ' user='...' description='...'
        # ... other parts, but no ZData here
        data #00002 oid=000000000026fc4c size=79 class=wendelin.bigfile.file_zodb.ZBlk1
      
      where ZBlk1 is committed the same without necessity.
      
      NOTE we cannot avoid committing ZBlk in all cases, because it is used to signal
          other DB clients that a ZBlk needs to be invalidated and this way associated
          fileh pages are invalidated too.
      
          This cannot work via ZData, because ZData don't have back-pointer to
          ZBlk1 or to corresponding zfile.
      6773fda0
  14. 05 Apr, 2016 1 commit
    • Kirill Smelkov's avatar
      Fix develop install for setuptools >= 19.4 · 2ce96a76
      Kirill Smelkov authored
      Starting from setuptools 19.4, more concrete from the following commit:
      
          https://github.com/pypa/setuptools/commit/ebc54982
      
      setuptools sorts namespaced packages .__path__ to be in sync with sys.path .
      That however breaks for wendelin.core used from in-tree or installed in
      development mode, because we are doing tricks in top-level import redirector
      (see e870781d "Top-level in-tree import redirector"):
      
          (z+numpy.v2)kirr@teco:~/tmp/trashme/wendelin.core$ python -c 'import wendelin'
          Traceback (most recent call last):
            File "<string>", line 1, in <module>
            File "wendelin.py", line 39, in <module>
              __import__('pkg_resources').declare_namespace(__name__)
            File "/home/kirr/src/wendelin/venv/z+numpy.v2/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2081, in declare_namespace
              _handle_ns(packageName, path_item)
            File "/home/kirr/src/wendelin/venv/z+numpy.v2/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2026, in _handle_ns
              _rebuild_mod_path(path, packageName, module)
            File "/home/kirr/src/wendelin/venv/z+numpy.v2/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2050, in _rebuild_mod_path
              orig_path.sort(key=position_in_sys_path)
            File "/home/kirr/src/wendelin/venv/z+numpy.v2/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2045, in position_in_sys_path
              return sys_path.index(_normalize_cached(os.sep.join(parts)))
          ValueError: '/home/kirr/tmp/trashme' is not in list
      
      Here wendelin.py added /home/kirr/tmp/trashme/wendelin.core to .__path__ and
      setuptools' _handle_ns() wants to order that dir's parent in correspondence
      with sys.path, but parent path is not there - oops.
      
      We can workaround the problem, by first not initializing .__path__ and letting
      
          __import__('pkg_resources').declare_namespace(__name__)
      
      fully handle and initialize it, and only after it is done we make the
      correction for wendelin modules located not under .../wendelin.core/wendelin/
      but under .../wendelin.core/ .
      
      Importing was tested to work with the fix with both setuptools 20.6.7 and older
      setuptools 17.1.1, i.e. here we should not be breaking backward compatibility.
      
      /reported-by @tatuya, @Camata, @Tyagov
      
      /reviewed-on !1
      2ce96a76
  15. 18 Dec, 2015 1 commit
    • Kirill Smelkov's avatar
      ZBigArray: Compatibility fix to read arrays from DB that were previously saved without order info · 2ca0f076
      Kirill Smelkov authored
      Commit ab9ca2df (bigarray: Add support for FORTRAN ordering) added
      ability to define array order, but there I made a mistake of not caring
      about how previously-saved to DB arrays would be read back.
      
      The thing is BigArray gained new data member ._order which is
      automatically saved to DB thanks to ZBigArray inheriting from
      Persistent; on load-from-db path we just read object state from DB,
      which for ZBigArray is dict, and restore object attributes from it.
      
      But for previously-saved data, obviously, there is no 'order' entry and thus
      this way restored objects are restored not in full to current code expectations
      and it can boom e.g. this way:
      
          zarray.resize((new_one,old_shape[1]))
        Module wendelin.bigarray, line 190, in resize
          self._init0(new_shape, self.dtype, order=self._order)
        AttributeError: 'ZBigArray' object has no attribute '_order'
      
      Solution to fix is: on restore-from-DB path, see if a data member is not
      present on restored object, and if it has default value in BigArray set it to
      that.
      
      ( code to get function defaults is from
        http://stackoverflow.com/questions/12627118/get-a-function-arguments-default-value )
      
      /cc @Tyagov, @klaus
      2ca0f076
  16. 15 Dec, 2015 6 commits
    • Kirill Smelkov's avatar
      tox: Bump NEO to 1.6 · 18b40b18
      Kirill Smelkov authored
      18b40b18
    • Kirill Smelkov's avatar
      bigfile/virtmem: Do loadblk() with virtmem lock released · f49c11a3
      Kirill Smelkov authored
      loadblk() calls are potentially slow and external code that serve the cal can
      take other locks in addition to virtmem lock taken by virtmem subsystem. If
      that "other locks" are also taken before external code calls e.g.
      fileh_invalidate_page() in different codepath a deadlock can happen, e.g.
      
            T1                  T2
      
            page-access         invalidation-from-server received
            V -> loadblk
                                Z   <- ClientStorage.invalidateTransaction()
            Z -> zeo.load
                                V   <- fileh_invalidate_page
      
      The solution to avoid deadlock is to call loadblk() with virtmem lock released
      and upon loadblk() completion recheck virtmem data structures carefully.
      
      To make that happen:
      
      - new page state is introduces:
      
          PAGE_LOADING                (file content loading is  in progress)
      
      - virtmem releases virt_lock before calling loadblk() when serving pagefault
      
      - because loading is now done with virtmem lock released, now:
      
      1. After loading completes we need to recheck fileh/vma data structures
      
         The recheck is done in full - vma_on_pagefault() just asks its driver (see
         VM_RETRY and VM_HANDLED codes) to retry handling the fault completely. This
         should work as the freshly loaded page was just inserted into fileh->pagemap
         and should be found there in the cache on next lookup.
      
         On the other hand this also works correctly, if there was concurrent change
         - e.g. vma was unmapped while we were loading the data - in that case the
         fault will be also processed correctly - but loaded data will stay in
         fileh->pagemap (and if not used will be evicted as not-needed
         eventually by RAM reclaim).
      
      2. Similar to retrying mechanism is used for cases when two threads
         concurrently access the same page and would both try to load corresponding
         block - only one thread issues the actual loadblk() and another waits for load
         to complete with polling and VM_RETRY.
      
      3. To correctly invalidate loading-in-progress pages another new page state
         is introduced:
      
          PAGE_LOADING_INVALIDATED    (file content loading was in progress
                                       while request to invalidate the page came in)
      
         which fileh_invalidate_page() uses to propagate invalidation message to
         loadblk() caller.
      
      4. Blocks loading can now happen in parallel with other block loading and
         other virtmem operations - e.g. invalidation. For such cases tests are added
         to test_thread.py
      
      5. virtmem lock now becomes just regular lock, instead of being previously
         recursive.
      
         For virtmem lock to be recursive was needed for cases, when code under
         loadblk() could trigger other virtmem calls, e.g. due to GC and calling
         another VMA dtor that would want to lock virtmem, but virtmem lock was
         already held.
      
         This is no longer needed.
      
      6. To catch double faults we now cannot use just on static variable
         in_on_pagefault. That variable thus becomes thread-local.
      
      7. Old test in test_thread to "test that access vs access don't overlap" no
         longer holds true - and is thus removed.
      
      /cc @Tyagov, @klaus
      f49c11a3
    • Kirill Smelkov's avatar
      bigfile/virtmem: Factor functionality to unlock/retake GIL into own functions · 0231a65d
      Kirill Smelkov authored
      Previously we were doing virt_lock() / virt_unlock() which automatically
      were making sure to unlock GIL before locking virtmem, and to restore
      GIL state to previous after virtmem lock happened. virt_unlock() was
      unlocking just the virtmem lock without touching GIL at all - that works
      because the running code would eventually release GIL as python
      regularly does so to allowing multiple threads to run.
      
      In the next patch however, we'll need to wait for in-progress-loading
      page to complete, and that wait has to be done with GIL released (so
      other python threads could run), and for doing so we'll need
      functionality to make sure GIL is unlocked and retake it back, not tied
      to virt_lock().
      
      So factor it out.
      0231a65d
    • Kirill Smelkov's avatar
      bigfile/tests: move NotifyChannel to test_thread.py · 99cd1f03
      Kirill Smelkov authored
      NotifyChannel was introduced in c7c01ce4 (bigfile/zodb: ZODB.Connection
      can migrate between threads on close/open and we have to care) to test
      thread interaction specific to ZODB.
      
      We'll however need NotifyChannel to do more threading test of virtmem
      core, and this way the proper place for NotifyChannel is test_thread.py
      itself.
      
      Move it.
      99cd1f03
    • Kirill Smelkov's avatar
      bigfile/virtmem: Remove obsolete XXX about locking · 81bf620c
      Kirill Smelkov authored
      Both comments are from the beginning - from 9a293c2d (bigfile/virtmem:
      Userspace Virtual Memory Manager) - but d53271b9 patch (bigfile/virtmem:
      Big Virtmem lock) missed to update them.
      81bf620c
    • Kirill Smelkov's avatar
      bigfile: Plug memory leak in ramh_close() · 997ebacd
      Kirill Smelkov authored
      No one was freeing RAMH structure itself, and thus ASAN reports e.g.:
      
      ==15935==ERROR: LeakSanitizer: detected memory leaks
      
      Direct leak of 32 byte(s) in 1 object(s) allocated from:
          #0 0x7f29c89f1001 in __interceptor_calloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x94001)
          #1 0x401da1 in zalloc include/wendelin/utils.h:65
          #2 0x408128 in shmfs_ramh_open bigfile/tests/../ram_shmfs.c:202
          #3 0x407611 in ramh_open bigfile/tests/../ram.c:81
          #4 0x402560 in fileh_open bigfile/tests/../virtmem.c:131
          #5 0x427ca1 in test_pagefault_savestate bigfile/tests/test_virtmem.c:1022
          #6 0x4281ba in main bigfile/tests/test_virtmem.c:1061
          #7 0x7f29c83b8b44 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b44)
      
      NOTE similar leak remains open in ram_close(), but it is a bit involved
      to fix and the effort will be removed anyway after we switch to kernel
      virtual memory manager. Besides ramh are opened and closed all the time
      and ram only once.
      997ebacd
  17. 05 Nov, 2015 1 commit
    • Kirill Smelkov's avatar
      bigfile/ram_shmfs: Warn users explicitly if fallocate() is not supported · e8c05a22
      Kirill Smelkov authored
      Currently wendelin.core does not work on e.g. Debian 7, because that
      distro has too old kernel without support for fallocate on tmpfs.
      But the diagnostics of failure is not clear and looks like just being
      out of memory:
      
          bigfile/tests/../virtmem.c:845 OOM      BUG!
      
      what happens in fact is that
      
          - virtmem tries to allocate a page -> calls shmfs_alloc_page(),
          - fallocate() fails with "operation not supported" error code
          - virtmem sees this as page allocation failure,
          - tries to reclaim pages,
          - but there are no allocated pages at all -> OOM
      
      Detect whether fallocate() error is operational error, or simply
      "fallocate not supported" and if latter, report to user. Now it looks
      like:
      
          bigfile/tests/../ram_shmfs.c:129 shmfs_alloc_page WARN: fallocate() not supported
          bigfile/tests/../virtmem.c:845 OOM      BUG!
      
      /cc @Tyagov
      e8c05a22
  18. 02 Nov, 2015 1 commit
  19. 02 Oct, 2015 3 commits
    • Kirill Smelkov's avatar
      wendelin.core v0.5 · c507d900
      Kirill Smelkov authored
      c507d900
    • Kirill Smelkov's avatar
      bigfile/zodb/tests: When testing check whole ZBlk content, not only [0] · 1895f0ed
      Kirill Smelkov authored
      Since 13c0c17c (bigfile/zodb: Format #1 which is optimized for small
      changes) each ZBlk is split into chunks, and the procedure to load/save
      blk data collects/disassembles the chunks.
      
      Previously in bigfile/zodb tests, we were testing by verifying only [0]
      element in each block, so this way most code for chunks
      collection/disassembling was not tested.
      
      Fix this aspect by making sure whole block content is as would-be
      expected in tests.
      1895f0ed
    • Kirill Smelkov's avatar
      changelog: Convert it to pretty rst · c7750965
      Kirill Smelkov authored
      It was reStructuredText already, but links showing was ugly, especially
      when there were several links in the same line.
      
      Use kernelnewbies style to refer to commits, when particular commit url
      is not shown to user, only hyperlinked. This way it looks nicer.
      c7750965