Skip to content

Instantly share code, notes, and snippets.

@enkore
Last active August 23, 2023 18:02
Show Gist options
  • Save enkore/14f7bd9f56d6cc17914a73345fd30fc4 to your computer and use it in GitHub Desktop.
Save enkore/14f7bd9f56d6cc17914a73345fd30fc4 to your computer and use it in GitHub Desktop.

Revisions

  1. enkore revised this gist Oct 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion show-odd-cache.py
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ def show_odd_cache_entries(_, args, repository, cache, manifest, key, count, col
    'size': 1,
    'csize': 2,
    }[column]
    contents = heapq.nlargest(count, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1][index])
    contents = fun(count, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1][index])
    fmt = '%-64s %-16s %-16s %-16s'
    print(fmt % ('Object ID', 'refcount', 'size', 'csize'))
    for key, (refcount, size, csize) in contents:
  2. enkore revised this gist Oct 5, 2016. 1 changed file with 21 additions and 7 deletions.
    28 changes: 21 additions & 7 deletions show-odd-cache.py
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,15 @@
    Usage:
    python show-odd-cache.py <repository> [<number of entries to show>]
    python show-odd-cache.py <repository> [<column to sort on>] [<number of entries to show>]
    Prints a list of the twenty cache entries with the highest reference count.
    This makes certain kinds of cache corruption (by bad drives, RAM etc) easy.
    Columns: **refcount**, size (payload size), csize (size in repo)
    Prepend a '-' to see the smallest values.
    Tested on Borg 1.0.x, Borg 1.1.x; works with remote repos.
    """

    @@ -22,8 +26,15 @@

    class XArchiver(Archiver):
    @with_repository(cache=True)
    def show_odd_cache_entries(_, args, repository, cache, manifest, key, count):
    contents = heapq.nlargest(count, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1])
    def show_odd_cache_entries(_, args, repository, cache, manifest, key, count, column):
    fun = heapq.nsmallest if column.startswith('-') else heapq.nlargest
    column = column.strip('-')
    index = {
    'refcount': 0,
    'size': 1,
    'csize': 2,
    }[column]
    contents = heapq.nlargest(count, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1][index])
    fmt = '%-64s %-16s %-16s %-16s'
    print(fmt % ('Object ID', 'refcount', 'size', 'csize'))
    for key, (refcount, size, csize) in contents:
    @@ -38,8 +49,11 @@ class Args:

    if __name__ == '__main__':
    try:
    count = int(sys.argv[2])
    except:
    column = sys.argv[2]
    except IndexError:
    column = 'refcount'
    try:
    count = int(sys.argv[3])
    except IndexError:
    count = 20
    XArchiver().show_odd_cache_entries(Args, count=count)

    XArchiver().show_odd_cache_entries(Args, count=count, column=column)
  3. enkore revised this gist Oct 5, 2016. 1 changed file with 24 additions and 5 deletions.
    29 changes: 24 additions & 5 deletions show-odd-cache.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,29 @@
    """
    show-odd-cache.py
    -----------------
    Usage:
    python show-odd-cache.py <repository> [<number of entries to show>]
    Prints a list of the twenty cache entries with the highest reference count.
    This makes certain kinds of cache corruption (by bad drives, RAM etc) easy.
    Tested on Borg 1.0.x, Borg 1.1.x; works with remote repos.
    """

    import heapq
    import sys
    from binascii import hexlify

    from borg.archiver import Archiver, with_repository
    from borg.archiver import Archiver, with_repository, UMASK_DEFAULT
    from borg.helpers import location_validator


    class XArchiver(Archiver):
    @with_repository(cache=True)
    def show_odd_cache_entries(_, args, repository, cache, manifest, key):
    contents = heapq.nlargest(20, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1])
    def show_odd_cache_entries(_, args, repository, cache, manifest, key, count):
    contents = heapq.nlargest(count, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1])
    fmt = '%-64s %-16s %-16s %-16s'
    print(fmt % ('Object ID', 'refcount', 'size', 'csize'))
    for key, (refcount, size, csize) in contents:
    @@ -19,8 +32,14 @@ def show_odd_cache_entries(_, args, repository, cache, manifest, key):

    class Args:
    location = location_validator(archive=False)(sys.argv[1])

    umask = UMASK_DEFAULT
    remote_path = 'borg'


    if __name__ == '__main__':
    XArchiver().show_odd_cache_entries(Args)
    try:
    count = int(sys.argv[2])
    except:
    count = 20
    XArchiver().show_odd_cache_entries(Args, count=count)

  4. enkore revised this gist Oct 4, 2016. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions show-odd-cache.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@

    import heapq
    import sys
    from binascii import hexlify

    @@ -9,9 +10,7 @@
    class XArchiver(Archiver):
    @with_repository(cache=True)
    def show_odd_cache_entries(_, args, repository, cache, manifest, key):
    contents = list(cache.chunks.iteritems())
    contents.sort(key=lambda k_r_s_c: k_r_s_c[1], reverse=True)
    contents = contents[:20]
    contents = heapq.nlargest(20, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1])
    fmt = '%-64s %-16s %-16s %-16s'
    print(fmt % ('Object ID', 'refcount', 'size', 'csize'))
    for key, (refcount, size, csize) in contents:
  5. enkore created this gist Oct 4, 2016.
    27 changes: 27 additions & 0 deletions show-odd-cache.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@

    import sys
    from binascii import hexlify

    from borg.archiver import Archiver, with_repository
    from borg.helpers import location_validator


    class XArchiver(Archiver):
    @with_repository(cache=True)
    def show_odd_cache_entries(_, args, repository, cache, manifest, key):
    contents = list(cache.chunks.iteritems())
    contents.sort(key=lambda k_r_s_c: k_r_s_c[1], reverse=True)
    contents = contents[:20]
    fmt = '%-64s %-16s %-16s %-16s'
    print(fmt % ('Object ID', 'refcount', 'size', 'csize'))
    for key, (refcount, size, csize) in contents:
    print(fmt % (hexlify(key).decode(), refcount, size, csize))


    class Args:
    location = location_validator(archive=False)(sys.argv[1])



    if __name__ == '__main__':
    XArchiver().show_odd_cache_entries(Args)