├── z ├── rtutil.s ├── simd │ ├── stub_search_amd64.go │ ├── add_test.go │ ├── search_amd64.s │ ├── search.go │ ├── asm2.go │ └── baseline.go ├── calloc_32bit.go ├── calloc_64bit.go ├── mmap_wasip1.go ├── calloc_nojemalloc.go ├── file_linux.go ├── file_default.go ├── mmap_plan9.go ├── mmap.go ├── histogram_test.go ├── mmap_darwin.go ├── mmap_unix.go ├── calloc.go ├── flags_test.go ├── bbloom_test.go ├── mmap_windows.go ├── rtutil.go ├── calloc_test.go ├── mmap_linux.go ├── LICENSE ├── z_test.go ├── z.go ├── allocator_test.go ├── calloc_jemalloc.go ├── README.md ├── histogram.go ├── file.go ├── rtutil_test.go ├── bbloom.go ├── flags.go ├── buffer_test.go └── allocator.go ├── contrib ├── memtestc │ ├── .gitignore │ └── list.c ├── memtest │ ├── .gitignore │ ├── README.md │ ├── withjemalloc.go │ ├── nojemalloc.go │ └── main.go └── demo │ ├── node_golang.go │ ├── node_jemalloc.go │ ├── node_allocator.go │ └── node.go ├── .mailmap ├── sim ├── gli.lirs.gz ├── sim_test.go └── sim.go ├── .github ├── ISSUE_TEMPLATE ├── CODEOWNERS └── workflows │ └── main.yml ├── .deepsource.toml ├── go.mod ├── test.sh ├── ring_test.go ├── sketch_test.go ├── go.sum ├── ring.go ├── ttl.go ├── stress_test.go ├── sketch.go ├── store_test.go ├── store.go ├── policy_test.go ├── metrics.go ├── CHANGELOG.md ├── README.md └── LICENSE /z/rtutil.s: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /contrib/memtestc/.gitignore: -------------------------------------------------------------------------------- 1 | /list 2 | -------------------------------------------------------------------------------- /contrib/memtest/.gitignore: -------------------------------------------------------------------------------- 1 | /list 2 | /memtest 3 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | Manish R Jain 2 | -------------------------------------------------------------------------------- /sim/gli.lirs.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/outcaste-io/ristretto/HEAD/sim/gli.lirs.gz -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE: -------------------------------------------------------------------------------- 1 | **GitHub Issues are deprecated. Use [Discuss Issues](https://discuss.dgraph.io/c/issues/35/ristretto/40) for reporting issues about this repository.** 2 | -------------------------------------------------------------------------------- /contrib/demo/node_golang.go: -------------------------------------------------------------------------------- 1 | // +build !jemalloc 2 | 3 | package main 4 | 5 | func newNode(val int) *node { 6 | return &node{val: val} 7 | } 8 | 9 | func freeNode(n *node) {} 10 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # CODEOWNERS info: https://help.github.com/en/articles/about-code-owners 2 | # Owners are automatically requested for review for PRs that changes code 3 | # that they own. 4 | * @manishrjain @martinmr @jarifibrahim 5 | -------------------------------------------------------------------------------- /z/simd/stub_search_amd64.go: -------------------------------------------------------------------------------- 1 | // Code generated by command: go run asm2.go -out search_amd64.s -stubs stub_search_amd64.go. DO NOT EDIT. 2 | 3 | package simd 4 | 5 | // Search finds the first idx for which xs[idx] >= k in xs. 6 | func Search(xs []uint64, k uint64) int16 7 | -------------------------------------------------------------------------------- /.deepsource.toml: -------------------------------------------------------------------------------- 1 | version = 1 2 | 3 | test_patterns = [ 4 | '**/*_test.go' 5 | ] 6 | 7 | exclude_patterns = [ 8 | 9 | ] 10 | 11 | [[analyzers]] 12 | name = 'go' 13 | enabled = true 14 | 15 | 16 | [analyzers.meta] 17 | import_path = 'github.com/dgraph-io/ristretto' 18 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/outcaste-io/ristretto 2 | 3 | go 1.12 4 | 5 | require ( 6 | github.com/cespare/xxhash/v2 v2.1.1 7 | github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 8 | github.com/dustin/go-humanize v1.0.0 9 | github.com/pkg/errors v0.9.1 10 | github.com/stretchr/testify v1.7.0 11 | go.uber.org/atomic v1.9.0 12 | golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b 13 | ) 14 | -------------------------------------------------------------------------------- /contrib/demo/node_jemalloc.go: -------------------------------------------------------------------------------- 1 | // +build jemalloc,!allocator 2 | 3 | package main 4 | 5 | import ( 6 | "unsafe" 7 | 8 | "github.com/outcaste-io/ristretto/z" 9 | ) 10 | 11 | func newNode(val int) *node { 12 | b := z.Calloc(nodeSz, "demo") 13 | n := (*node)(unsafe.Pointer(&b[0])) 14 | n.val = val 15 | return n 16 | } 17 | 18 | func freeNode(n *node) { 19 | buf := (*[z.MaxArrayLen]byte)(unsafe.Pointer(n))[:nodeSz:nodeSz] 20 | z.Free(buf) 21 | } 22 | -------------------------------------------------------------------------------- /z/calloc_32bit.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 | // of this source code is governed by a BSD-style license that can be found in 3 | // the LICENSE file. 4 | 5 | // +build 386 amd64p32 arm armbe mips mipsle mips64p32 mips64p32le ppc sparc 6 | 7 | package z 8 | 9 | const ( 10 | // MaxArrayLen is a safe maximum length for slices on this architecture. 11 | MaxArrayLen = 1<<31 - 1 12 | // MaxBufferSize is the size of virtually unlimited buffer on this architecture. 13 | MaxBufferSize = 1 << 30 14 | ) 15 | -------------------------------------------------------------------------------- /contrib/memtest/README.md: -------------------------------------------------------------------------------- 1 | memtest tests the effect of the C memory allocator. The default version uses Calloc from the stdlib. 2 | 3 | If the program is built using the `jemalloc` build tag, then the allocator used will be jemalloc. 4 | 5 | # Monitoring # 6 | 7 | To monitor the memory use of this program, the following bash snippet is useful: 8 | 9 | ``` 10 | while true; do 11 | ps -C memtest -o vsz=,rss= >> memphys.csv 12 | sleep 1 13 | done 14 | ``` 15 | 16 | This is of course contingent upon the fact that the binary of this program is called `memtest`. 17 | -------------------------------------------------------------------------------- /z/calloc_64bit.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 | // of this source code is governed by a BSD-style license that can be found in 3 | // the LICENSE file. 4 | 5 | // +build amd64 arm64 arm64be ppc64 ppc64le mips64 mips64le riscv64 s390x sparc64 6 | 7 | package z 8 | 9 | const ( 10 | // MaxArrayLen is a safe maximum length for slices on this architecture. 11 | MaxArrayLen = 1<<50 - 1 12 | // MaxBufferSize is the size of virtually unlimited buffer on this architecture. 13 | MaxBufferSize = 256 << 30 14 | ) 15 | -------------------------------------------------------------------------------- /z/simd/add_test.go: -------------------------------------------------------------------------------- 1 | package simd 2 | 3 | import ( 4 | "math" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/require" 8 | ) 9 | 10 | func TestSearch(t *testing.T) { 11 | keys := make([]uint64, 512) 12 | for i := 0; i < len(keys); i += 2 { 13 | keys[i] = uint64(i) 14 | keys[i+1] = 1 15 | } 16 | 17 | for i := 0; i < len(keys); i++ { 18 | idx := int(Search(keys, uint64(i))) 19 | require.Equal(t, (i+1)/2, idx, "%v\n%v", i, keys) 20 | } 21 | require.Equal(t, 256, int(Search(keys, math.MaxInt64>>1))) 22 | require.Equal(t, 256, int(Search(keys, math.MaxInt64))) 23 | } 24 | -------------------------------------------------------------------------------- /contrib/memtest/withjemalloc.go: -------------------------------------------------------------------------------- 1 | //+build jemalloc 2 | 3 | package main 4 | 5 | import ( 6 | "os" 7 | 8 | "github.com/outcaste-io/ristretto/z" 9 | ) 10 | 11 | func Calloc(size int) []byte { return z.Calloc(size, "memtest") } 12 | func Free(bs []byte) { z.Free(bs) } 13 | func NumAllocBytes() int64 { return z.NumAllocBytes() } 14 | 15 | func check() { 16 | if buf := z.CallocNoRef(1, "memtest"); len(buf) == 0 { 17 | panic("Not using manual memory management. Compile with jemalloc.") 18 | os.Exit(1) 19 | } else { 20 | z.Free(buf) 21 | } 22 | 23 | z.StatsPrint() 24 | } 25 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | starttest() { 4 | set -e 5 | GO111MODULE=on go test -race ./... 6 | } 7 | 8 | if [ -z "${TEAMCITY_VERSION}" ]; then 9 | # running locally, so start test in a container 10 | # TEAMCITY_VERSION=local will avoid recursive calls, when it would be running in container 11 | docker run --rm --name ristretto-test -ti \ 12 | -v `pwd`:/go/src/github.com/outcaste-io/ristretto \ 13 | --workdir /go/src/github.com/outcaste-io/ristretto \ 14 | --env TEAMCITY_VERSION=local \ 15 | golang:1.16 \ 16 | sh test.sh 17 | else 18 | # running in teamcity, since teamcity itself run this in container, let's simply run this 19 | starttest 20 | fi 21 | -------------------------------------------------------------------------------- /contrib/demo/node_allocator.go: -------------------------------------------------------------------------------- 1 | // +build jemalloc,allocator 2 | 3 | package main 4 | 5 | import ( 6 | "unsafe" 7 | 8 | "github.com/outcaste-io/ristretto/z" 9 | ) 10 | 11 | // Defined in node.go. 12 | func init() { 13 | alloc = z.NewAllocator(10 << 20, "demo") 14 | } 15 | 16 | func newNode(val int) *node { 17 | // b := alloc.Allocate(nodeSz) 18 | b := alloc.AllocateAligned(nodeSz) 19 | n := (*node)(unsafe.Pointer(&b[0])) 20 | n.val = val 21 | alloc.Allocate(1) // Extra allocate just to demonstrate AllocateAligned is working as expected. 22 | return n 23 | } 24 | 25 | func freeNode(n *node) { 26 | // buf := (*[z.MaxArrayLen]byte)(unsafe.Pointer(n))[:nodeSz:nodeSz] 27 | // z.Free(buf) 28 | } 29 | -------------------------------------------------------------------------------- /contrib/memtest/nojemalloc.go: -------------------------------------------------------------------------------- 1 | //+build !jemalloc 2 | 3 | package main 4 | 5 | // #include 6 | import "C" 7 | import ( 8 | "reflect" 9 | "sync/atomic" 10 | "unsafe" 11 | ) 12 | 13 | func Calloc(size int) []byte { 14 | if size == 0 { 15 | return make([]byte, 0) 16 | } 17 | ptr := C.calloc(C.size_t(size), 1) 18 | if ptr == nil { 19 | panic("OOM") 20 | } 21 | hdr := reflect.SliceHeader{Data: uintptr(ptr), Len: size, Cap: size} 22 | atomic.AddInt64(&numbytes, int64(size)) 23 | return *(*[]byte)(unsafe.Pointer(&hdr)) 24 | } 25 | 26 | func Free(bs []byte) { 27 | if len(bs) == 0 { 28 | return 29 | } 30 | 31 | if sz := cap(bs); sz != 0 { 32 | bs = bs[:cap(bs)] 33 | C.free(unsafe.Pointer(&bs[0])) 34 | atomic.AddInt64(&numbytes, -int64(sz)) 35 | } 36 | } 37 | 38 | func NumAllocBytes() int64 { return atomic.LoadInt64(&numbytes) } 39 | 40 | func check() {} 41 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # This is a basic workflow to help you get started with Actions 2 | 3 | name: Issue Closer 4 | 5 | # Controls when the action will run. Triggers the workflow on push or pull request 6 | # events but only for the master branch 7 | on: 8 | issues: 9 | types: [ opened ] 10 | 11 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 12 | jobs: 13 | # This workflow contains a single job called "build" 14 | build: 15 | # The type of runner that the job will run on 16 | runs-on: ubuntu-latest 17 | 18 | # Steps represent a sequence of tasks that will be executed as part of the job 19 | steps: 20 | - name: Close Issue 21 | uses: peter-evans/close-issue@v1.0.1 22 | with: 23 | comment: | 24 | **Use [Discuss Issues](https://discuss.dgraph.io/c/issues/35/ristretto/40) for reporting issues about this repository.** 25 | -------------------------------------------------------------------------------- /z/simd/search_amd64.s: -------------------------------------------------------------------------------- 1 | // Code generated by command: go run asm2.go -out search_amd64.s -stubs stub_search_amd64.go. DO NOT EDIT. 2 | 3 | #include "textflag.h" 4 | 5 | // func Search(xs []uint64, k uint64) int16 6 | TEXT ·Search(SB), NOSPLIT, $0-34 7 | MOVQ xs_base+0(FP), AX 8 | MOVQ xs_len+8(FP), CX 9 | MOVQ k+24(FP), DX 10 | 11 | // Save n 12 | MOVQ CX, BX 13 | 14 | // Initialize idx register to zero. 15 | XORL BP, BP 16 | 17 | loop: 18 | // Unroll1 19 | CMPQ (AX)(BP*8), DX 20 | JAE Found 21 | 22 | // Unroll2 23 | CMPQ 16(AX)(BP*8), DX 24 | JAE Found2 25 | 26 | // Unroll3 27 | CMPQ 32(AX)(BP*8), DX 28 | JAE Found3 29 | 30 | // Unroll4 31 | CMPQ 48(AX)(BP*8), DX 32 | JAE Found4 33 | 34 | // plus8 35 | ADDQ $0x08, BP 36 | CMPQ BP, CX 37 | JB loop 38 | JMP NotFound 39 | 40 | Found2: 41 | ADDL $0x02, BP 42 | JMP Found 43 | 44 | Found3: 45 | ADDL $0x04, BP 46 | JMP Found 47 | 48 | Found4: 49 | ADDL $0x06, BP 50 | 51 | Found: 52 | MOVL BP, BX 53 | 54 | NotFound: 55 | MOVL BX, BP 56 | SHRL $0x1f, BP 57 | ADDL BX, BP 58 | SHRL $0x01, BP 59 | MOVL BP, ret+32(FP) 60 | RET 61 | -------------------------------------------------------------------------------- /z/mmap_wasip1.go: -------------------------------------------------------------------------------- 1 | //go:build wasip1 2 | 3 | /* 4 | * Copyright 2023 Dgraph Labs, Inc. and Contributors 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package z 20 | 21 | import ( 22 | "os" 23 | "syscall" 24 | ) 25 | 26 | func mmap(fd *os.File, writeable bool, size int64) ([]byte, error) { 27 | return nil, syscall.ENOSYS 28 | } 29 | 30 | func munmap(b []byte) error { 31 | return syscall.ENOSYS 32 | } 33 | 34 | func madvise(b []byte, readahead bool) error { 35 | return syscall.ENOSYS 36 | } 37 | 38 | func msync(b []byte) error { 39 | return syscall.ENOSYS 40 | } 41 | -------------------------------------------------------------------------------- /z/calloc_nojemalloc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 | // of this source code is governed by a BSD-style license that can be found in 3 | // the LICENSE file. 4 | 5 | // +build !jemalloc !cgo 6 | 7 | package z 8 | 9 | import ( 10 | "fmt" 11 | ) 12 | 13 | // Provides versions of Calloc, CallocNoRef, etc when jemalloc is not available 14 | // (eg: build without jemalloc tag). 15 | 16 | // Calloc allocates a slice of size n. 17 | func Calloc(n int, tag string) []byte { 18 | return make([]byte, n) 19 | } 20 | 21 | // CallocNoRef will not give you memory back without jemalloc. 22 | func CallocNoRef(n int, tag string) []byte { 23 | // We do the add here just to stay compatible with a corresponding Free call. 24 | return nil 25 | } 26 | 27 | // Free does not do anything in this mode. 28 | func Free(b []byte) {} 29 | 30 | func Leaks() string { return "Leaks: Using Go memory" } 31 | func StatsPrint() { 32 | fmt.Println("Using Go memory") 33 | } 34 | 35 | // ReadMemStats doesn't do anything since all the memory is being managed 36 | // by the Go runtime. 37 | func ReadMemStats(_ *MemStats) { return } 38 | -------------------------------------------------------------------------------- /contrib/demo/node.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "runtime" 6 | "unsafe" 7 | 8 | "github.com/outcaste-io/ristretto/z" 9 | "github.com/dustin/go-humanize" 10 | ) 11 | 12 | type node struct { 13 | val int 14 | next *node 15 | } 16 | 17 | var nodeSz = int(unsafe.Sizeof(node{})) 18 | var alloc *z.Allocator 19 | 20 | func printNode(n *node) { 21 | if n == nil { 22 | return 23 | } 24 | if n.val%100000 == 0 { 25 | fmt.Printf("node: %d\n", n.val) 26 | } 27 | printNode(n.next) 28 | } 29 | 30 | func main() { 31 | N := 2000001 32 | root := newNode(-1) 33 | n := root 34 | for i := 0; i < N; i++ { 35 | nn := newNode(i) 36 | n.next = nn 37 | n = nn 38 | } 39 | fmt.Printf("Allocated memory: %s Objects: %d\n", 40 | humanize.IBytes(uint64(z.NumAllocBytes())), N) 41 | 42 | runtime.GC() 43 | printNode(root) 44 | fmt.Println("printing done") 45 | 46 | if alloc != nil { 47 | alloc.Release() 48 | } else { 49 | n = root 50 | for n != nil { 51 | left := n 52 | n = n.next 53 | freeNode(left) 54 | } 55 | } 56 | fmt.Printf("After freeing. Allocated memory: %d\n", z.NumAllocBytes()) 57 | 58 | var ms runtime.MemStats 59 | runtime.ReadMemStats(&ms) 60 | fmt.Printf("HeapAlloc: %s\n", humanize.IBytes(ms.HeapAlloc)) 61 | } 62 | -------------------------------------------------------------------------------- /z/file_linux.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | // Truncate would truncate the mmapped file to the given size. On Linux, we truncate 24 | // the underlying file and then call mremap, but on other systems, we unmap first, 25 | // then truncate, then re-map. 26 | func (m *MmapFile) Truncate(maxSz int64) error { 27 | if err := m.Sync(); err != nil { 28 | return fmt.Errorf("while sync file: %s, error: %v\n", m.Fd.Name(), err) 29 | } 30 | if err := m.Fd.Truncate(maxSz); err != nil { 31 | return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err) 32 | } 33 | 34 | var err error 35 | m.Data, err = mremap(m.Data, int(maxSz)) // Mmap up to max size. 36 | return err 37 | } 38 | -------------------------------------------------------------------------------- /z/simd/search.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 2 | 3 | /* 4 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package simd 20 | 21 | // Search uses the Clever search to find the correct key. 22 | func Search(xs []uint64, k uint64) int16 { 23 | if len(xs) < 8 || (len(xs) % 8 != 0) { 24 | return Naive(xs, k) 25 | } 26 | var twos, pk [4]uint64 27 | pk[0] = k 28 | pk[1] = k 29 | pk[2] = k 30 | pk[3] = k 31 | for i := 0; i < len(xs); i += 8 { 32 | twos[0] = xs[i] 33 | twos[1] = xs[i+2] 34 | twos[2] = xs[i+4] 35 | twos[3] = xs[i+6] 36 | if twos[0] >= pk[0] { 37 | return int16(i / 2) 38 | } 39 | if twos[1] >= pk[1] { 40 | return int16((i + 2) / 2) 41 | } 42 | if twos[2] >= pk[2] { 43 | return int16((i + 4) / 2) 44 | } 45 | if twos[3] >= pk[3] { 46 | return int16((i + 6) / 2) 47 | } 48 | 49 | } 50 | return int16(len(xs) / 2) 51 | } 52 | -------------------------------------------------------------------------------- /z/file_default.go: -------------------------------------------------------------------------------- 1 | // +build !linux 2 | 3 | /* 4 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package z 20 | 21 | import "fmt" 22 | 23 | // Truncate would truncate the mmapped file to the given size. On Linux, we truncate 24 | // the underlying file and then call mremap, but on other systems, we unmap first, 25 | // then truncate, then re-map. 26 | func (m *MmapFile) Truncate(maxSz int64) error { 27 | if err := m.Sync(); err != nil { 28 | return fmt.Errorf("while sync file: %s, error: %v\n", m.Fd.Name(), err) 29 | } 30 | if err := Munmap(m.Data); err != nil { 31 | return fmt.Errorf("while munmap file: %s, error: %v\n", m.Fd.Name(), err) 32 | } 33 | if err := m.Fd.Truncate(maxSz); err != nil { 34 | return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err) 35 | } 36 | var err error 37 | m.Data, err = Mmap(m.Fd, true, maxSz) // Mmap up to max size. 38 | return err 39 | } 40 | -------------------------------------------------------------------------------- /z/mmap_plan9.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "os" 21 | "syscall" 22 | ) 23 | 24 | // Mmap uses the mmap system call to memory-map a file. If writable is true, 25 | // memory protection of the pages is set so that they may be written to as well. 26 | func mmap(fd *os.File, writable bool, size int64) ([]byte, error) { 27 | return nil, syscall.EPLAN9 28 | } 29 | 30 | // Munmap unmaps a previously mapped slice. 31 | func munmap(b []byte) error { 32 | return syscall.EPLAN9 33 | } 34 | 35 | // Madvise uses the madvise system call to give advise about the use of memory 36 | // when using a slice that is memory-mapped to a file. Set the readahead flag to 37 | // false if page references are expected in random order. 38 | func madvise(b []byte, readahead bool) error { 39 | return syscall.EPLAN9 40 | } 41 | 42 | func msync(b []byte) error { 43 | return syscall.EPLAN9 44 | } 45 | -------------------------------------------------------------------------------- /ring_test.go: -------------------------------------------------------------------------------- 1 | package ristretto 2 | 3 | import ( 4 | "sync" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/require" 8 | ) 9 | 10 | type testConsumer struct { 11 | push func([]uint64) 12 | save bool 13 | } 14 | 15 | func (c *testConsumer) Push(items []uint64) bool { 16 | if c.save { 17 | c.push(items) 18 | return true 19 | } 20 | return false 21 | } 22 | 23 | func TestRingDrain(t *testing.T) { 24 | drains := 0 25 | r := newRingBuffer(&testConsumer{ 26 | push: func(items []uint64) { 27 | drains++ 28 | }, 29 | save: true, 30 | }, 1) 31 | for i := 0; i < 100; i++ { 32 | r.Push(uint64(i)) 33 | } 34 | require.Equal(t, 100, drains, "buffers shouldn't be dropped with BufferItems == 1") 35 | } 36 | 37 | func TestRingReset(t *testing.T) { 38 | drains := 0 39 | r := newRingBuffer(&testConsumer{ 40 | push: func(items []uint64) { 41 | drains++ 42 | }, 43 | save: false, 44 | }, 4) 45 | for i := 0; i < 100; i++ { 46 | r.Push(uint64(i)) 47 | } 48 | require.Equal(t, 0, drains, "testConsumer shouldn't be draining") 49 | } 50 | 51 | func TestRingConsumer(t *testing.T) { 52 | mu := &sync.Mutex{} 53 | drainItems := make(map[uint64]struct{}) 54 | r := newRingBuffer(&testConsumer{ 55 | push: func(items []uint64) { 56 | mu.Lock() 57 | defer mu.Unlock() 58 | for i := range items { 59 | drainItems[items[i]] = struct{}{} 60 | } 61 | }, 62 | save: true, 63 | }, 4) 64 | for i := 0; i < 100; i++ { 65 | r.Push(uint64(i)) 66 | } 67 | l := len(drainItems) 68 | require.NotEqual(t, 0, l) 69 | require.True(t, l <= 100) 70 | } 71 | -------------------------------------------------------------------------------- /z/mmap.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "os" 21 | ) 22 | 23 | // Mmap uses the mmap system call to memory-map a file. If writable is true, 24 | // memory protection of the pages is set so that they may be written to as well. 25 | func Mmap(fd *os.File, writable bool, size int64) ([]byte, error) { 26 | return mmap(fd, writable, size) 27 | } 28 | 29 | // Munmap unmaps a previously mapped slice. 30 | func Munmap(b []byte) error { 31 | return munmap(b) 32 | } 33 | 34 | // Madvise uses the madvise system call to give advise about the use of memory 35 | // when using a slice that is memory-mapped to a file. Set the readahead flag to 36 | // false if page references are expected in random order. 37 | func Madvise(b []byte, readahead bool) error { 38 | return madvise(b, readahead) 39 | } 40 | 41 | // Msync would call sync on the mmapped data. 42 | func Msync(b []byte) error { 43 | return msync(b) 44 | } 45 | -------------------------------------------------------------------------------- /z/histogram_test.go: -------------------------------------------------------------------------------- 1 | package z 2 | 3 | import ( 4 | "math" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/require" 8 | ) 9 | 10 | func TestPercentile00(t *testing.T) { 11 | size := int(math.Ceil((float64(514) - float64(32)) / float64(4))) 12 | bounds := make([]float64, size+1) 13 | for i := range bounds { 14 | if i == 0 { 15 | bounds[0] = 32 16 | continue 17 | } 18 | if i == size { 19 | bounds[i] = 514 20 | break 21 | } 22 | bounds[i] = bounds[i-1] + 4 23 | } 24 | 25 | h := NewHistogramData(bounds) 26 | for v := 16; v <= 1024; v = v + 4 { 27 | for i := 0; i < 1000; i++ { 28 | h.Update(int64(v)) 29 | } 30 | } 31 | 32 | require.Equal(t, h.Percentile(0.0), 32.0) 33 | } 34 | 35 | func TestPercentile99(t *testing.T) { 36 | size := int(math.Ceil((float64(514) - float64(32)) / float64(4))) 37 | bounds := make([]float64, size+1) 38 | for i := range bounds { 39 | if i == 0 { 40 | bounds[0] = 32 41 | continue 42 | } 43 | if i == size { 44 | bounds[i] = 514 45 | break 46 | } 47 | bounds[i] = bounds[i-1] + 4 48 | } 49 | h := NewHistogramData(bounds) 50 | for v := 16; v <= 512; v = v + 4 { 51 | for i := 0; i < 1000; i++ { 52 | h.Update(int64(v)) 53 | } 54 | } 55 | 56 | require.Equal(t, h.Percentile(0.99), 512.0) 57 | } 58 | 59 | func TestPercentile100(t *testing.T) { 60 | size := int(math.Ceil((float64(514) - float64(32)) / float64(4))) 61 | bounds := make([]float64, size+1) 62 | for i := range bounds { 63 | if i == 0 { 64 | bounds[0] = 32 65 | continue 66 | } 67 | if i == size { 68 | bounds[i] = 514 69 | break 70 | } 71 | bounds[i] = bounds[i-1] + 4 72 | } 73 | h := NewHistogramData(bounds) 74 | for v := 16; v <= 1024; v = v + 4 { 75 | for i := 0; i < 1000; i++ { 76 | h.Update(int64(v)) 77 | } 78 | } 79 | require.Equal(t, h.Percentile(1.0), 514.0) 80 | } 81 | -------------------------------------------------------------------------------- /z/mmap_darwin.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "os" 21 | "syscall" 22 | "unsafe" 23 | 24 | "golang.org/x/sys/unix" 25 | ) 26 | 27 | // Mmap uses the mmap system call to memory-map a file. If writable is true, 28 | // memory protection of the pages is set so that they may be written to as well. 29 | func mmap(fd *os.File, writable bool, size int64) ([]byte, error) { 30 | mtype := unix.PROT_READ 31 | if writable { 32 | mtype |= unix.PROT_WRITE 33 | } 34 | return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED) 35 | } 36 | 37 | // Munmap unmaps a previously mapped slice. 38 | func munmap(b []byte) error { 39 | return unix.Munmap(b) 40 | } 41 | 42 | // This is required because the unix package does not support the madvise system call on OS X. 43 | func madvise(b []byte, readahead bool) error { 44 | advice := unix.MADV_NORMAL 45 | if !readahead { 46 | advice = unix.MADV_RANDOM 47 | } 48 | 49 | _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), 50 | uintptr(len(b)), uintptr(advice)) 51 | if e1 != 0 { 52 | return e1 53 | } 54 | return nil 55 | } 56 | 57 | func msync(b []byte) error { 58 | return unix.Msync(b, unix.MS_SYNC) 59 | } 60 | -------------------------------------------------------------------------------- /z/simd/asm2.go: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | package main 4 | 5 | import ( 6 | . "github.com/mmcloughlin/avo/build" 7 | . "github.com/mmcloughlin/avo/operand" 8 | ) 9 | 10 | //go:generate go run asm2.go -out search_amd64.s -stubs stub_search_amd64.go 11 | 12 | func main() { 13 | TEXT("Search", NOSPLIT, "func(xs []uint64, k uint64) int16") 14 | Doc("Search finds the first idx for which xs[idx] >= k in xs.") 15 | ptr := Load(Param("xs").Base(), GP64()) 16 | n := Load(Param("xs").Len(), GP64()) 17 | key := Load(Param("k"), GP64()) 18 | retInd := ReturnIndex(0) 19 | retVal, err := retInd.Resolve() 20 | if err != nil { 21 | panic(err) 22 | } 23 | 24 | Comment("Save n") 25 | n2 := GP64() 26 | MOVQ(n, n2) 27 | 28 | Comment("Initialize idx register to zero.") 29 | idx := GP64() 30 | XORL(idx.As32(), idx.As32()) 31 | 32 | Label("loop") 33 | m := Mem{Base: ptr, Index: idx, Scale: 8} 34 | 35 | Comment("Unroll1") 36 | CMPQ(m, key) 37 | JAE(LabelRef("Found")) 38 | 39 | Comment("Unroll2") 40 | CMPQ(m.Offset(16), key) 41 | JAE(LabelRef("Found2")) 42 | 43 | Comment("Unroll3") 44 | CMPQ(m.Offset(32), key) 45 | JAE(LabelRef("Found3")) 46 | 47 | Comment("Unroll4") 48 | CMPQ(m.Offset(48), key) 49 | JAE(LabelRef("Found4")) 50 | 51 | Comment("plus8") 52 | ADDQ(Imm(8), idx) 53 | CMPQ(idx, n) 54 | JB(LabelRef("loop")) 55 | JMP(LabelRef("NotFound")) 56 | 57 | Label("Found2") 58 | ADDL(Imm(2), idx.As32()) 59 | JMP(LabelRef("Found")) 60 | 61 | Label("Found3") 62 | ADDL(Imm(4), idx.As32()) 63 | JMP(LabelRef("Found")) 64 | 65 | Label("Found4") 66 | ADDL(Imm(6), idx.As32()) 67 | 68 | Label("Found") 69 | MOVL(idx.As32(), n2.As32()) // n2 is no longer being used 70 | 71 | Label("NotFound") 72 | MOVL(n2.As32(), idx.As32()) 73 | SHRL(Imm(31), idx.As32()) 74 | ADDL(n2.As32(), idx.As32()) 75 | SHRL(Imm(1), idx.As32()) 76 | MOVL(idx.As32(), retVal.Addr) 77 | RET() 78 | 79 | Generate() 80 | } 81 | -------------------------------------------------------------------------------- /z/mmap_unix.go: -------------------------------------------------------------------------------- 1 | //go:build !windows && !darwin && !plan9 && !linux && !wasip1 2 | // +build !windows,!darwin,!plan9,!linux,!wasip1 3 | 4 | /* 5 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package z 21 | 22 | import ( 23 | "os" 24 | 25 | "golang.org/x/sys/unix" 26 | ) 27 | 28 | // Mmap uses the mmap system call to memory-map a file. If writable is true, 29 | // memory protection of the pages is set so that they may be written to as well. 30 | func mmap(fd *os.File, writable bool, size int64) ([]byte, error) { 31 | mtype := unix.PROT_READ 32 | if writable { 33 | mtype |= unix.PROT_WRITE 34 | } 35 | return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED) 36 | } 37 | 38 | // Munmap unmaps a previously mapped slice. 39 | func munmap(b []byte) error { 40 | return unix.Munmap(b) 41 | } 42 | 43 | // Madvise uses the madvise system call to give advise about the use of memory 44 | // when using a slice that is memory-mapped to a file. Set the readahead flag to 45 | // false if page references are expected in random order. 46 | func madvise(b []byte, readahead bool) error { 47 | flags := unix.MADV_NORMAL 48 | if !readahead { 49 | flags = unix.MADV_RANDOM 50 | } 51 | return unix.Madvise(b, flags) 52 | } 53 | 54 | func msync(b []byte) error { 55 | return unix.Msync(b, unix.MS_SYNC) 56 | } 57 | -------------------------------------------------------------------------------- /sketch_test.go: -------------------------------------------------------------------------------- 1 | package ristretto 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/require" 7 | ) 8 | 9 | func TestSketch(t *testing.T) { 10 | defer func() { 11 | require.NotNil(t, recover()) 12 | }() 13 | 14 | s := newCmSketch(5) 15 | require.Equal(t, uint64(7), s.mask) 16 | newCmSketch(0) 17 | } 18 | 19 | func TestSketchIncrement(t *testing.T) { 20 | s := newCmSketch(16) 21 | s.Increment(1) 22 | s.Increment(5) 23 | s.Increment(9) 24 | for i := 0; i < cmDepth; i++ { 25 | if s.rows[i].string() != s.rows[0].string() { 26 | break 27 | } 28 | require.False(t, i == cmDepth-1, "identical rows, bad seeding") 29 | } 30 | } 31 | 32 | func TestSketchEstimate(t *testing.T) { 33 | s := newCmSketch(16) 34 | s.Increment(1) 35 | s.Increment(1) 36 | require.Equal(t, int64(2), s.Estimate(1)) 37 | require.Equal(t, int64(0), s.Estimate(0)) 38 | } 39 | 40 | func TestSketchReset(t *testing.T) { 41 | s := newCmSketch(16) 42 | s.Increment(1) 43 | s.Increment(1) 44 | s.Increment(1) 45 | s.Increment(1) 46 | s.Reset() 47 | require.Equal(t, int64(2), s.Estimate(1)) 48 | } 49 | 50 | func TestSketchClear(t *testing.T) { 51 | s := newCmSketch(16) 52 | for i := 0; i < 16; i++ { 53 | s.Increment(uint64(i)) 54 | } 55 | s.Clear() 56 | for i := 0; i < 16; i++ { 57 | require.Equal(t, int64(0), s.Estimate(uint64(i))) 58 | } 59 | } 60 | 61 | func TestNext2Power(t *testing.T) { 62 | sz := 12 << 30 63 | szf := float64(sz) * 0.01 64 | val := int64(szf) 65 | t.Logf("szf = %.2f val = %d\n", szf, val) 66 | pow := next2Power(val) 67 | t.Logf("pow = %d. mult 4 = %d\n", pow, pow*4) 68 | } 69 | 70 | func BenchmarkSketchIncrement(b *testing.B) { 71 | s := newCmSketch(16) 72 | b.SetBytes(1) 73 | for n := 0; n < b.N; n++ { 74 | s.Increment(1) 75 | } 76 | } 77 | 78 | func BenchmarkSketchEstimate(b *testing.B) { 79 | s := newCmSketch(16) 80 | s.Increment(1) 81 | b.SetBytes(1) 82 | for n := 0; n < b.N; n++ { 83 | s.Estimate(1) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /z/calloc.go: -------------------------------------------------------------------------------- 1 | package z 2 | 3 | import "sync/atomic" 4 | 5 | var numBytes int64 6 | 7 | // NumAllocBytes returns the number of bytes allocated using calls to z.Calloc. The allocations 8 | // could be happening via either Go or jemalloc, depending upon the build flags. 9 | func NumAllocBytes() int64 { 10 | return atomic.LoadInt64(&numBytes) 11 | } 12 | 13 | // MemStats is used to fetch JE Malloc Stats. The stats are fetched from 14 | // the mallctl namespace http://jemalloc.net/jemalloc.3.html#mallctl_namespace. 15 | type MemStats struct { 16 | // Total number of bytes allocated by the application. 17 | // http://jemalloc.net/jemalloc.3.html#stats.allocated 18 | Allocated uint64 19 | // Total number of bytes in active pages allocated by the application. This 20 | // is a multiple of the page size, and greater than or equal to 21 | // Allocated. 22 | // http://jemalloc.net/jemalloc.3.html#stats.active 23 | Active uint64 24 | // Maximum number of bytes in physically resident data pages mapped by the 25 | // allocator, comprising all pages dedicated to allocator metadata, pages 26 | // backing active allocations, and unused dirty pages. This is a maximum 27 | // rather than precise because pages may not actually be physically 28 | // resident if they correspond to demand-zeroed virtual memory that has not 29 | // yet been touched. This is a multiple of the page size, and is larger 30 | // than stats.active. 31 | // http://jemalloc.net/jemalloc.3.html#stats.resident 32 | Resident uint64 33 | // Total number of bytes in virtual memory mappings that were retained 34 | // rather than being returned to the operating system via e.g. munmap(2) or 35 | // similar. Retained virtual memory is typically untouched, decommitted, or 36 | // purged, so it has no strongly associated physical memory (see extent 37 | // hooks http://jemalloc.net/jemalloc.3.html#arena.i.extent_hooks for 38 | // details). Retained memory is excluded from mapped memory statistics, 39 | // e.g. stats.mapped (http://jemalloc.net/jemalloc.3.html#stats.mapped). 40 | // http://jemalloc.net/jemalloc.3.html#stats.retained 41 | Retained uint64 42 | } 43 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= 2 | github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 3 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 5 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 | github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= 7 | github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= 8 | github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= 9 | github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= 10 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 11 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 12 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 13 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 14 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 15 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 16 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 17 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 18 | go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= 19 | go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= 20 | golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8= 21 | golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 22 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 23 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 24 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 25 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 26 | -------------------------------------------------------------------------------- /z/flags_test.go: -------------------------------------------------------------------------------- 1 | package z 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "os/user" 7 | "path/filepath" 8 | "testing" 9 | "time" 10 | 11 | "github.com/stretchr/testify/require" 12 | ) 13 | 14 | func TestFlag(t *testing.T) { 15 | const opt = `bool_key=true; int-key=5; float-key=0.05; string_key=value; ;` 16 | const def = `bool_key=false; int-key=0; float-key=1.0; string-key=; other-key=5; 17 | duration-minutes=15m; duration-hours=12h; duration-days=30d;` 18 | 19 | _, err := NewSuperFlag("boolo-key=true").MergeWithDefault(def) 20 | require.Error(t, err) 21 | _, err = newSuperFlagImpl("key-without-value") 22 | require.Error(t, err) 23 | 24 | // bool-key and int-key should not be overwritten. Only other-key should be set. 25 | sf := NewSuperFlag(opt) 26 | sf.MergeAndCheckDefault(def) 27 | 28 | require.Equal(t, true, sf.GetBool("bool-key")) 29 | require.Equal(t, uint64(5), sf.GetUint64("int-key")) 30 | require.Equal(t, "value", sf.GetString("string-key")) 31 | require.Equal(t, uint64(5), sf.GetUint64("other-key")) 32 | 33 | require.Equal(t, time.Minute*15, sf.GetDuration("duration-minutes")) 34 | require.Equal(t, time.Hour*12, sf.GetDuration("duration-hours")) 35 | require.Equal(t, time.Hour*24*30, sf.GetDuration("duration-days")) 36 | } 37 | 38 | func TestFlagDefault(t *testing.T) { 39 | def := `one=false; two=; three=;` 40 | f := NewSuperFlag(`one=true; two=4;`).MergeAndCheckDefault(def) 41 | require.Equal(t, true, f.GetBool("one")) 42 | require.Equal(t, int64(4), f.GetInt64("two")) 43 | } 44 | 45 | func TestGetPath(t *testing.T) { 46 | usr, err := user.Current() 47 | require.NoError(t, err) 48 | homeDir := usr.HomeDir 49 | cwd, err := os.Getwd() 50 | require.NoError(t, err) 51 | 52 | tests := []struct { 53 | path string 54 | expected string 55 | }{ 56 | { 57 | "/home/user/file.txt", 58 | "/home/user/file.txt", 59 | }, 60 | { 61 | "~/file.txt", 62 | filepath.Join(homeDir, "file.txt"), 63 | }, 64 | { 65 | "~/abc/../file.txt", 66 | filepath.Join(homeDir, "file.txt"), 67 | }, 68 | { 69 | "~/", 70 | homeDir, 71 | }, 72 | { 73 | "~filename", 74 | filepath.Join(cwd, "~filename"), 75 | }, 76 | { 77 | "./filename", 78 | filepath.Join(cwd, "filename"), 79 | }, 80 | { 81 | "", 82 | "", 83 | }, 84 | { 85 | "./", 86 | cwd, 87 | }, 88 | } 89 | 90 | get := func(p string) string { 91 | opt := fmt.Sprintf("file=%s", p) 92 | sf := NewSuperFlag(opt) 93 | return sf.GetPath("file") 94 | } 95 | 96 | for _, tc := range tests { 97 | actual := get(tc.path) 98 | require.Equalf(t, tc.expected, actual, "Failed on testcase: %s", tc.path) 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /z/bbloom_test.go: -------------------------------------------------------------------------------- 1 | package z 2 | 3 | import ( 4 | "crypto/rand" 5 | "fmt" 6 | "testing" 7 | 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | var ( 12 | wordlist1 [][]byte 13 | n = 1 << 16 14 | bf *Bloom 15 | ) 16 | 17 | func TestMain(m *testing.M) { 18 | wordlist1 = make([][]byte, n) 19 | for i := range wordlist1 { 20 | b := make([]byte, 32) 21 | rand.Read(b) 22 | wordlist1[i] = b 23 | } 24 | fmt.Println("\n###############\nbbloom_test.go") 25 | fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n") 26 | 27 | m.Run() 28 | 29 | } 30 | 31 | func TestM_NumberOfWrongs(t *testing.T) { 32 | bf = NewBloomFilter(float64(n*10), float64(7)) 33 | 34 | cnt := 0 35 | for i := range wordlist1 { 36 | hash := MemHash(wordlist1[i]) 37 | if !bf.AddIfNotHas(hash) { 38 | cnt++ 39 | } 40 | } 41 | fmt.Printf("Bloomfilter New(7* 2**16, 7) (-> size=%v bit): \n Check for 'false positives': %v wrong positive 'Has' results on 2**16 entries => %v %%\n", len(bf.bitset)<<6, cnt, float64(cnt)/float64(n)) 42 | 43 | } 44 | 45 | func TestM_JSON(t *testing.T) { 46 | const shallBe = int(1 << 16) 47 | 48 | bf = NewBloomFilter(float64(n*10), float64(7)) 49 | 50 | cnt := 0 51 | for i := range wordlist1 { 52 | hash := MemHash(wordlist1[i]) 53 | if !bf.AddIfNotHas(hash) { 54 | cnt++ 55 | } 56 | } 57 | 58 | Json := bf.JSONMarshal() 59 | 60 | // create new bloomfilter from bloomfilter's JSON representation 61 | bf2, err := JSONUnmarshal(Json) 62 | require.NoError(t, err) 63 | 64 | cnt2 := 0 65 | for i := range wordlist1 { 66 | hash := MemHash(wordlist1[i]) 67 | if !bf2.AddIfNotHas(hash) { 68 | cnt2++ 69 | } 70 | } 71 | require.Equal(t, shallBe, cnt2) 72 | } 73 | 74 | func BenchmarkM_New(b *testing.B) { 75 | for r := 0; r < b.N; r++ { 76 | _ = NewBloomFilter(float64(n*10), float64(7)) 77 | } 78 | } 79 | 80 | func BenchmarkM_Clear(b *testing.B) { 81 | bf = NewBloomFilter(float64(n*10), float64(7)) 82 | for i := range wordlist1 { 83 | hash := MemHash(wordlist1[i]) 84 | bf.Add(hash) 85 | } 86 | b.ResetTimer() 87 | for r := 0; r < b.N; r++ { 88 | bf.Clear() 89 | } 90 | } 91 | 92 | func BenchmarkM_Add(b *testing.B) { 93 | bf = NewBloomFilter(float64(n*10), float64(7)) 94 | b.ResetTimer() 95 | for r := 0; r < b.N; r++ { 96 | for i := range wordlist1 { 97 | hash := MemHash(wordlist1[i]) 98 | bf.Add(hash) 99 | } 100 | } 101 | 102 | } 103 | 104 | func BenchmarkM_Has(b *testing.B) { 105 | b.ResetTimer() 106 | for r := 0; r < b.N; r++ { 107 | for i := range wordlist1 { 108 | hash := MemHash(wordlist1[i]) 109 | bf.Has(hash) 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /z/mmap_windows.go: -------------------------------------------------------------------------------- 1 | // +build windows 2 | 3 | /* 4 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package z 20 | 21 | import ( 22 | "fmt" 23 | "os" 24 | "syscall" 25 | "unsafe" 26 | ) 27 | 28 | func mmap(fd *os.File, write bool, size int64) ([]byte, error) { 29 | protect := syscall.PAGE_READONLY 30 | access := syscall.FILE_MAP_READ 31 | 32 | if write { 33 | protect = syscall.PAGE_READWRITE 34 | access = syscall.FILE_MAP_WRITE 35 | } 36 | fi, err := fd.Stat() 37 | if err != nil { 38 | return nil, err 39 | } 40 | 41 | // In windows, we cannot mmap a file more than it's actual size. 42 | // So truncate the file to the size of the mmap. 43 | if fi.Size() < size { 44 | if err := fd.Truncate(size); err != nil { 45 | return nil, fmt.Errorf("truncate: %s", err) 46 | } 47 | } 48 | 49 | // Open a file mapping handle. 50 | sizelo := uint32(size >> 32) 51 | sizehi := uint32(size) & 0xffffffff 52 | 53 | handler, err := syscall.CreateFileMapping(syscall.Handle(fd.Fd()), nil, 54 | uint32(protect), sizelo, sizehi, nil) 55 | if err != nil { 56 | return nil, os.NewSyscallError("CreateFileMapping", err) 57 | } 58 | 59 | // Create the memory map. 60 | addr, err := syscall.MapViewOfFile(handler, uint32(access), 0, 0, uintptr(size)) 61 | if addr == 0 { 62 | return nil, os.NewSyscallError("MapViewOfFile", err) 63 | } 64 | 65 | // Close mapping handle. 66 | if err := syscall.CloseHandle(syscall.Handle(handler)); err != nil { 67 | return nil, os.NewSyscallError("CloseHandle", err) 68 | } 69 | 70 | // Slice memory layout 71 | // Copied this snippet from golang/sys package 72 | var sl = struct { 73 | addr uintptr 74 | len int 75 | cap int 76 | }{addr, int(size), int(size)} 77 | 78 | // Use unsafe to turn sl into a []byte. 79 | data := *(*[]byte)(unsafe.Pointer(&sl)) 80 | 81 | return data, nil 82 | } 83 | 84 | func munmap(b []byte) error { 85 | return syscall.UnmapViewOfFile(uintptr(unsafe.Pointer(&b[0]))) 86 | } 87 | 88 | func madvise(b []byte, readahead bool) error { 89 | // Do Nothing. We don’t care about this setting on Windows 90 | return nil 91 | } 92 | 93 | func msync(b []byte) error { 94 | return syscall.FlushViewOfFile(uintptr(unsafe.Pointer(&b[0])), uintptr(len(b))) 95 | } 96 | -------------------------------------------------------------------------------- /z/simd/baseline.go: -------------------------------------------------------------------------------- 1 | package simd 2 | 3 | import ( 4 | "fmt" 5 | "runtime" 6 | "sort" 7 | "sync" 8 | ) 9 | 10 | // Search finds the key using the naive way 11 | func Naive(xs []uint64, k uint64) int16 { 12 | var i int 13 | for i = 0; i < len(xs); i += 2 { 14 | x := xs[i] 15 | if x >= k { 16 | return int16(i / 2) 17 | } 18 | } 19 | return int16(i / 2) 20 | } 21 | 22 | func Clever(xs []uint64, k uint64) int16 { 23 | if len(xs) < 8 { 24 | return Naive(xs, k) 25 | } 26 | var twos, pk [4]uint64 27 | pk[0] = k 28 | pk[1] = k 29 | pk[2] = k 30 | pk[3] = k 31 | for i := 0; i < len(xs); i += 8 { 32 | twos[0] = xs[i] 33 | twos[1] = xs[i+2] 34 | twos[2] = xs[i+4] 35 | twos[3] = xs[i+6] 36 | if twos[0] >= pk[0] { 37 | return int16(i / 2) 38 | } 39 | if twos[1] >= pk[1] { 40 | return int16((i + 2) / 2) 41 | } 42 | if twos[2] >= pk[2] { 43 | return int16((i + 4) / 2) 44 | } 45 | if twos[3] >= pk[3] { 46 | return int16((i + 6) / 2) 47 | } 48 | 49 | } 50 | return int16(len(xs) / 2) 51 | } 52 | 53 | func Parallel(xs []uint64, k uint64) int16 { 54 | cpus := runtime.NumCPU() 55 | if cpus%2 != 0 { 56 | panic(fmt.Sprintf("odd number of CPUs %v", cpus)) 57 | } 58 | sz := len(xs)/cpus + 1 59 | var wg sync.WaitGroup 60 | retChan := make(chan int16, cpus) 61 | for i := 0; i < len(xs); i += sz { 62 | end := i + sz 63 | if end >= len(xs) { 64 | end = len(xs) 65 | } 66 | chunk := xs[i:end] 67 | wg.Add(1) 68 | go func(hd int16, xs []uint64, k uint64, wg *sync.WaitGroup, ch chan int16) { 69 | for i := 0; i < len(xs); i += 2 { 70 | if xs[i] >= k { 71 | ch <- (int16(i) + hd) / 2 72 | break 73 | } 74 | } 75 | wg.Done() 76 | }(int16(i), chunk, k, &wg, retChan) 77 | } 78 | wg.Wait() 79 | close(retChan) 80 | var min int16 = (1 << 15) - 1 81 | for i := range retChan { 82 | if i < min { 83 | min = i 84 | } 85 | } 86 | if min == (1<<15)-1 { 87 | return int16(len(xs) / 2) 88 | } 89 | return min 90 | } 91 | 92 | func Binary(keys []uint64, key uint64) int16 { 93 | return int16(sort.Search(len(keys), func(i int) bool { 94 | if i*2 >= len(keys) { 95 | return true 96 | } 97 | return keys[i*2] >= key 98 | })) 99 | } 100 | 101 | func cmp2_native(twos, pk [2]uint64) int16 { 102 | if twos[0] == pk[0] { 103 | return 0 104 | } 105 | if twos[1] == pk[1] { 106 | return 1 107 | } 108 | return 2 109 | } 110 | 111 | func cmp4_native(fours, pk [4]uint64) int16 { 112 | for i := range fours { 113 | if fours[i] >= pk[i] { 114 | return int16(i) 115 | } 116 | } 117 | return 4 118 | } 119 | 120 | func cmp8_native(a [8]uint64, pk [4]uint64) int16 { 121 | for i := range a { 122 | if a[i] >= pk[0] { 123 | return int16(i) 124 | } 125 | } 126 | return 8 127 | } 128 | -------------------------------------------------------------------------------- /contrib/memtestc/list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // A simple C program for traversal of a linked list 18 | #include 19 | #include 20 | #include 21 | 22 | struct Node { 23 | int data; 24 | char* buf; 25 | struct Node* next; 26 | }; 27 | 28 | // This function prints contents of linked list starting from 29 | // the given node 30 | void printList(struct Node* n) 31 | { 32 | while (n != NULL) { 33 | printf(" %d ", n->data); 34 | n = n->next; 35 | } 36 | } 37 | 38 | long long int lo = 1L << 30; 39 | long long int hi = 16L << 30; 40 | 41 | struct Node* newNode(int sz) { 42 | struct Node* n = (struct Node*)calloc(1, sizeof(struct Node)); 43 | n->buf = calloc(sz, 1); 44 | for (int i = 0; i < sz; i++) { 45 | n->buf[i] = 0xff; 46 | } 47 | n->data = sz; 48 | n->next = NULL; 49 | return n; 50 | } 51 | 52 | void allocate(struct Node* n, int sz) { 53 | struct Node* nn = newNode(sz); 54 | struct Node* tmp = n->next; 55 | n->next = nn; 56 | nn->next = tmp; 57 | } 58 | 59 | int dealloc(struct Node* n) { 60 | if (n->next == NULL) { 61 | printf("n->next is NULL\n"); 62 | exit(1); 63 | } 64 | struct Node* tmp = n->next; 65 | n->next = tmp->next; 66 | int sz = tmp->data; 67 | free(tmp->buf); 68 | free(tmp); 69 | return sz; 70 | } 71 | 72 | int main() 73 | { 74 | struct Node* root = newNode(100); 75 | 76 | long long int total = 0; 77 | int increase = 1; 78 | while(1) { 79 | if (increase == 1) { 80 | int sz = (1 + rand() % 256) << 20; 81 | allocate(root, sz); 82 | if (root->next == NULL) { 83 | printf("root->next is NULL\n"); 84 | exit(1); 85 | } 86 | total += sz; 87 | if (total > hi) { 88 | increase = 0; 89 | } 90 | } else { 91 | int sz = dealloc(root); 92 | total -= sz; 93 | if (total < lo) { 94 | increase = 1; 95 | sleep(5); 96 | } else { 97 | usleep(10); 98 | } 99 | } 100 | 101 | long double gb = total; 102 | gb /= (1 << 30); 103 | printf("Total size: %.2LF\n", gb); 104 | }; 105 | 106 | return 0; 107 | } 108 | 109 | -------------------------------------------------------------------------------- /sim/sim_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package sim 18 | 19 | import ( 20 | "bytes" 21 | "compress/gzip" 22 | "os" 23 | "testing" 24 | ) 25 | 26 | func TestZipfian(t *testing.T) { 27 | s := NewZipfian(1.5, 1, 100) 28 | m := make(map[uint64]uint64, 100) 29 | for i := 0; i < 100; i++ { 30 | k, err := s() 31 | if err != nil { 32 | t.Fatal(err) 33 | } 34 | m[k]++ 35 | } 36 | if len(m) == 0 || len(m) == 100 { 37 | t.Fatal("zipfian not skewed") 38 | } 39 | } 40 | 41 | func TestUniform(t *testing.T) { 42 | s := NewUniform(100) 43 | for i := 0; i < 100; i++ { 44 | if _, err := s(); err != nil { 45 | t.Fatal(err) 46 | } 47 | } 48 | } 49 | 50 | func TestParseLIRS(t *testing.T) { 51 | s := NewReader(ParseLIRS, bytes.NewReader([]byte{ 52 | '0', '\n', 53 | '1', '\r', '\n', 54 | '2', '\r', '\n', 55 | })) 56 | for i := uint64(0); i < 3; i++ { 57 | v, err := s() 58 | if err != nil { 59 | t.Fatal(err) 60 | } 61 | if v != i { 62 | t.Fatal("value mismatch") 63 | } 64 | } 65 | } 66 | 67 | func TestReadLIRS(t *testing.T) { 68 | f, err := os.Open("./gli.lirs.gz") 69 | if err != nil { 70 | t.Fatal(err) 71 | } 72 | r, err := gzip.NewReader(f) 73 | if err != nil { 74 | t.Fatal(err) 75 | } 76 | s := NewReader(ParseLIRS, r) 77 | for i := uint64(0); i < 100; i++ { 78 | if _, err = s(); err != nil { 79 | t.Fatal(err) 80 | } 81 | } 82 | } 83 | 84 | func TestParseARC(t *testing.T) { 85 | s := NewReader(ParseARC, bytes.NewReader([]byte{ 86 | '1', '2', '7', ' ', '6', '4', ' ', '0', ' ', '0', '\r', '\n', 87 | '1', '9', '1', ' ', '3', '6', ' ', '0', ' ', '0', '\r', '\n', 88 | })) 89 | for i := uint64(0); i < 100; i++ { 90 | v, err := s() 91 | if err != nil { 92 | t.Fatal(err) 93 | } 94 | if v != 127+i { 95 | t.Fatal("value mismatch") 96 | } 97 | } 98 | } 99 | 100 | func TestCollection(t *testing.T) { 101 | s := NewUniform(100) 102 | c := Collection(s, 100) 103 | if len(c) != 100 { 104 | t.Fatal("collection not full") 105 | } 106 | } 107 | 108 | func TestStringCollection(t *testing.T) { 109 | s := NewUniform(100) 110 | c := StringCollection(s, 100) 111 | if len(c) != 100 { 112 | t.Fatal("string collection not full") 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /z/rtutil.go: -------------------------------------------------------------------------------- 1 | // MIT License 2 | 3 | // Copyright (c) 2019 Ewan Chou 4 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | package z 24 | 25 | import ( 26 | "unsafe" 27 | ) 28 | 29 | // NanoTime returns the current time in nanoseconds from a monotonic clock. 30 | //go:linkname NanoTime runtime.nanotime 31 | func NanoTime() int64 32 | 33 | // CPUTicks is a faster alternative to NanoTime to measure time duration. 34 | //go:linkname CPUTicks runtime.cputicks 35 | func CPUTicks() int64 36 | 37 | type stringStruct struct { 38 | str unsafe.Pointer 39 | len int 40 | } 41 | 42 | //go:noescape 43 | //go:linkname memhash runtime.memhash 44 | func memhash(p unsafe.Pointer, h, s uintptr) uintptr 45 | 46 | // MemHash is the hash function used by go map, it utilizes available hardware instructions(behaves 47 | // as aeshash if aes instruction is available). 48 | // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash. 49 | func MemHash(data []byte) uint64 { 50 | ss := (*stringStruct)(unsafe.Pointer(&data)) 51 | return uint64(memhash(ss.str, 0, uintptr(ss.len))) 52 | } 53 | 54 | // MemHashString is the hash function used by go map, it utilizes available hardware instructions 55 | // (behaves as aeshash if aes instruction is available). 56 | // NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash. 57 | func MemHashString(str string) uint64 { 58 | ss := (*stringStruct)(unsafe.Pointer(&str)) 59 | return uint64(memhash(ss.str, 0, uintptr(ss.len))) 60 | } 61 | 62 | // FastRand is a fast thread local random function. 63 | //go:linkname FastRand runtime.fastrand 64 | func FastRand() uint32 65 | 66 | //go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers 67 | func memclrNoHeapPointers(p unsafe.Pointer, n uintptr) 68 | 69 | func Memclr(b []byte) { 70 | if len(b) == 0 { 71 | return 72 | } 73 | p := unsafe.Pointer(&b[0]) 74 | memclrNoHeapPointers(p, uintptr(len(b))) 75 | } 76 | -------------------------------------------------------------------------------- /z/calloc_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | "testing" 23 | "time" 24 | 25 | "math/rand" 26 | 27 | "github.com/stretchr/testify/require" 28 | ) 29 | 30 | // $ go test -failfast -run xxx -bench . -benchmem -count 10 > out.txt 31 | // $ benchstat out.txt 32 | // name time/op 33 | // Allocation/Pool-8 200µs ± 5% 34 | // Allocation/Calloc-8 100µs ±11% 35 | // 36 | // name alloc/op 37 | // Allocation/Pool-8 477B ±29% 38 | // Allocation/Calloc-8 4.00B ± 0% 39 | // 40 | // name allocs/op 41 | // Allocation/Pool-8 1.00 ± 0% 42 | // Allocation/Calloc-8 0.00 43 | func BenchmarkAllocation(b *testing.B) { 44 | b.Run("Pool", func(b *testing.B) { 45 | pool := sync.Pool{ 46 | New: func() interface{} { 47 | return make([]byte, 4<<10) 48 | }, 49 | } 50 | b.RunParallel(func(pb *testing.PB) { 51 | source := rand.NewSource(time.Now().UnixNano()) 52 | r := rand.New(source) 53 | for pb.Next() { 54 | x := pool.Get().([]byte) 55 | sz := r.Intn(100) << 10 56 | if len(x) < sz { 57 | x = make([]byte, sz) 58 | } 59 | r.Read(x) 60 | pool.Put(x) 61 | } 62 | }) 63 | }) 64 | 65 | b.Run("Calloc", func(b *testing.B) { 66 | b.RunParallel(func(pb *testing.PB) { 67 | source := rand.NewSource(time.Now().UnixNano()) 68 | r := rand.New(source) 69 | for pb.Next() { 70 | sz := r.Intn(100) << 10 71 | x := Calloc(sz, "test") 72 | r.Read(x) 73 | Free(x) 74 | } 75 | }) 76 | }) 77 | } 78 | 79 | func TestCalloc(t *testing.T) { 80 | // Check if we're using jemalloc. 81 | // JE_MALLOC_CONF="abort:true,tcache:false" 82 | 83 | StatsPrint() 84 | buf := CallocNoRef(1, "test") 85 | if len(buf) == 0 { 86 | t.Skipf("Not using jemalloc. Skipping test.") 87 | } 88 | Free(buf) 89 | require.Equal(t, int64(0), NumAllocBytes()) 90 | 91 | buf1 := Calloc(128, "test") 92 | require.Equal(t, int64(128), NumAllocBytes()) 93 | buf2 := Calloc(128, "test") 94 | require.Equal(t, int64(256), NumAllocBytes()) 95 | 96 | Free(buf1) 97 | require.Equal(t, int64(128), NumAllocBytes()) 98 | 99 | // _ = buf2 100 | Free(buf2) 101 | require.Equal(t, int64(0), NumAllocBytes()) 102 | fmt.Println(Leaks()) 103 | 104 | // Double free would panic when debug mode is enabled in jemalloc. 105 | // Free(buf2) 106 | // require.Equal(t, int64(0), NumAllocBytes()) 107 | } 108 | -------------------------------------------------------------------------------- /z/mmap_linux.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "os" 21 | "reflect" 22 | "unsafe" 23 | 24 | "golang.org/x/sys/unix" 25 | ) 26 | 27 | // mmap uses the mmap system call to memory-map a file. If writable is true, 28 | // memory protection of the pages is set so that they may be written to as well. 29 | func mmap(fd *os.File, writable bool, size int64) ([]byte, error) { 30 | mtype := unix.PROT_READ 31 | if writable { 32 | mtype |= unix.PROT_WRITE 33 | } 34 | return unix.Mmap(int(fd.Fd()), 0, int(size), mtype, unix.MAP_SHARED) 35 | } 36 | 37 | // mremap is a Linux-specific system call to remap pages in memory. This can be used in place of munmap + mmap. 38 | func mremap(data []byte, size int) ([]byte, error) { 39 | // taken from 40 | const MREMAP_MAYMOVE = 0x1 41 | 42 | header := (*reflect.SliceHeader)(unsafe.Pointer(&data)) 43 | mmapAddr, _, errno := unix.Syscall6( 44 | unix.SYS_MREMAP, 45 | header.Data, 46 | uintptr(header.Len), 47 | uintptr(size), 48 | uintptr(MREMAP_MAYMOVE), 49 | 0, 50 | 0, 51 | ) 52 | if errno != 0 { 53 | return nil, errno 54 | } 55 | 56 | header.Data = mmapAddr 57 | header.Cap = size 58 | header.Len = size 59 | return data, nil 60 | } 61 | 62 | // munmap unmaps a previously mapped slice. 63 | // 64 | // unix.Munmap maintains an internal list of mmapped addresses, and only calls munmap 65 | // if the address is present in that list. If we use mremap, this list is not updated. 66 | // To bypass this, we call munmap ourselves. 67 | func munmap(data []byte) error { 68 | if len(data) == 0 || len(data) != cap(data) { 69 | return unix.EINVAL 70 | } 71 | _, _, errno := unix.Syscall( 72 | unix.SYS_MUNMAP, 73 | uintptr(unsafe.Pointer(&data[0])), 74 | uintptr(len(data)), 75 | 0, 76 | ) 77 | if errno != 0 { 78 | return errno 79 | } 80 | return nil 81 | } 82 | 83 | // madvise uses the madvise system call to give advise about the use of memory 84 | // when using a slice that is memory-mapped to a file. Set the readahead flag to 85 | // false if page references are expected in random order. 86 | func madvise(b []byte, readahead bool) error { 87 | flags := unix.MADV_NORMAL 88 | if !readahead { 89 | flags = unix.MADV_RANDOM 90 | } 91 | return unix.Madvise(b, flags) 92 | } 93 | 94 | // msync writes any modified data to persistent storage. 95 | func msync(b []byte) error { 96 | return unix.Msync(b, unix.MS_SYNC) 97 | } 98 | -------------------------------------------------------------------------------- /z/LICENSE: -------------------------------------------------------------------------------- 1 | bbloom.go 2 | 3 | // The MIT License (MIT) 4 | // Copyright (c) 2014 Andreas Briese, eduToolbox@Bri-C GmbH, Sarstedt 5 | 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | // this software and associated documentation files (the "Software"), to deal in 8 | // the Software without restriction, including without limitation the rights to 9 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | // the Software, and to permit persons to whom the Software is furnished to do so, 11 | // subject to the following conditions: 12 | 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | rtutil.go 24 | 25 | // MIT License 26 | 27 | // Copyright (c) 2019 Ewan Chou 28 | 29 | // Permission is hereby granted, free of charge, to any person obtaining a copy 30 | // of this software and associated documentation files (the "Software"), to deal 31 | // in the Software without restriction, including without limitation the rights 32 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 33 | // copies of the Software, and to permit persons to whom the Software is 34 | // furnished to do so, subject to the following conditions: 35 | 36 | // The above copyright notice and this permission notice shall be included in all 37 | // copies or substantial portions of the Software. 38 | 39 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 40 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 41 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 42 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 43 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 44 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 45 | // SOFTWARE. 46 | 47 | Modifications: 48 | 49 | /* 50 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 51 | * 52 | * Licensed under the Apache License, Version 2.0 (the "License"); 53 | * you may not use this file except in compliance with the License. 54 | * You may obtain a copy of the License at 55 | * 56 | * http://www.apache.org/licenses/LICENSE-2.0 57 | * 58 | * Unless required by applicable law or agreed to in writing, software 59 | * distributed under the License is distributed on an "AS IS" BASIS, 60 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 61 | * See the License for the specific language governing permissions and 62 | * limitations under the License. 63 | */ 64 | 65 | -------------------------------------------------------------------------------- /ring.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ristretto 18 | 19 | import ( 20 | "sync" 21 | ) 22 | 23 | // ringConsumer is the user-defined object responsible for receiving and 24 | // processing items in batches when buffers are drained. 25 | type ringConsumer interface { 26 | Push([]uint64) bool 27 | } 28 | 29 | // ringStripe is a singular ring buffer that is not concurrent safe. 30 | type ringStripe struct { 31 | cons ringConsumer 32 | data []uint64 33 | capa int 34 | } 35 | 36 | func newRingStripe(cons ringConsumer, capa int64) *ringStripe { 37 | return &ringStripe{ 38 | cons: cons, 39 | data: make([]uint64, 0, capa), 40 | capa: int(capa), 41 | } 42 | } 43 | 44 | // Push appends an item in the ring buffer and drains (copies items and 45 | // sends to Consumer) if full. 46 | func (s *ringStripe) Push(item uint64) { 47 | s.data = append(s.data, item) 48 | // Decide if the ring buffer should be drained. 49 | if len(s.data) >= s.capa { 50 | // Send elements to consumer and create a new ring stripe. 51 | if s.cons.Push(s.data) { 52 | s.data = make([]uint64, 0, s.capa) 53 | } else { 54 | s.data = s.data[:0] 55 | } 56 | } 57 | } 58 | 59 | // ringBuffer stores multiple buffers (stripes) and distributes Pushed items 60 | // between them to lower contention. 61 | // 62 | // This implements the "batching" process described in the BP-Wrapper paper 63 | // (section III part A). 64 | type ringBuffer struct { 65 | pool *sync.Pool 66 | } 67 | 68 | // newRingBuffer returns a striped ring buffer. The Consumer in ringConfig will 69 | // be called when individual stripes are full and need to drain their elements. 70 | func newRingBuffer(cons ringConsumer, capa int64) *ringBuffer { 71 | // LOSSY buffers use a very simple sync.Pool for concurrently reusing 72 | // stripes. We do lose some stripes due to GC (unheld items in sync.Pool 73 | // are cleared), but the performance gains generally outweigh the small 74 | // percentage of elements lost. The performance primarily comes from 75 | // low-level runtime functions used in the standard library that aren't 76 | // available to us (such as runtime_procPin()). 77 | return &ringBuffer{ 78 | pool: &sync.Pool{ 79 | New: func() interface{} { return newRingStripe(cons, capa) }, 80 | }, 81 | } 82 | } 83 | 84 | // Push adds an element to one of the internal stripes and possibly drains if 85 | // the stripe becomes full. 86 | func (b *ringBuffer) Push(item uint64) { 87 | // Reuse or create a new stripe. 88 | stripe := b.pool.Get().(*ringStripe) 89 | stripe.Push(item) 90 | b.pool.Put(stripe) 91 | } 92 | -------------------------------------------------------------------------------- /z/z_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "math" 21 | "testing" 22 | 23 | "github.com/stretchr/testify/require" 24 | ) 25 | 26 | func verifyHashProduct(t *testing.T, wantKey, wantConflict, key, conflict uint64) { 27 | require.Equal(t, wantKey, key) 28 | require.Equal(t, wantConflict, conflict) 29 | } 30 | 31 | func TestKeyToHash(t *testing.T) { 32 | var key uint64 33 | var conflict uint64 34 | 35 | key, conflict = KeyToHash(uint64(1)) 36 | verifyHashProduct(t, 1, 0, key, conflict) 37 | 38 | key, conflict = KeyToHash(1) 39 | verifyHashProduct(t, 1, 0, key, conflict) 40 | 41 | key, conflict = KeyToHash(int32(2)) 42 | verifyHashProduct(t, 2, 0, key, conflict) 43 | 44 | key, conflict = KeyToHash(int32(-2)) 45 | verifyHashProduct(t, math.MaxUint64-1, 0, key, conflict) 46 | 47 | key, conflict = KeyToHash(int64(-2)) 48 | verifyHashProduct(t, math.MaxUint64-1, 0, key, conflict) 49 | 50 | key, conflict = KeyToHash(uint32(3)) 51 | verifyHashProduct(t, 3, 0, key, conflict) 52 | 53 | key, conflict = KeyToHash(int64(3)) 54 | verifyHashProduct(t, 3, 0, key, conflict) 55 | } 56 | 57 | func TestMulipleSignals(t *testing.T) { 58 | closer := NewCloser(0) 59 | require.NotPanics(t, func() { closer.Signal() }) 60 | // Should not panic. 61 | require.NotPanics(t, func() { closer.Signal() }) 62 | require.NotPanics(t, func() { closer.SignalAndWait() }) 63 | 64 | // Attempt 2. 65 | closer = NewCloser(1) 66 | require.NotPanics(t, func() { closer.Done() }) 67 | 68 | require.NotPanics(t, func() { closer.SignalAndWait() }) 69 | // Should not panic. 70 | require.NotPanics(t, func() { closer.SignalAndWait() }) 71 | require.NotPanics(t, func() { closer.Signal() }) 72 | } 73 | 74 | func TestCloser(t *testing.T) { 75 | closer := NewCloser(1) 76 | go func() { 77 | defer closer.Done() 78 | <-closer.Ctx().Done() 79 | }() 80 | closer.SignalAndWait() 81 | } 82 | 83 | func TestZeroOut(t *testing.T) { 84 | dst := make([]byte, 4*1024) 85 | fill := func() { 86 | for i := 0; i < len(dst); i++ { 87 | dst[i] = 0xFF 88 | } 89 | } 90 | check := func(buf []byte, b byte) { 91 | for i := 0; i < len(buf); i++ { 92 | require.Equalf(t, b, buf[i], "idx: %d", i) 93 | } 94 | } 95 | fill() 96 | 97 | ZeroOut(dst, 0, 1) 98 | check(dst[:1], 0x00) 99 | check(dst[1:], 0xFF) 100 | 101 | ZeroOut(dst, 0, 1024) 102 | check(dst[:1024], 0x00) 103 | check(dst[1024:], 0xFF) 104 | 105 | ZeroOut(dst, 0, len(dst)) 106 | check(dst, 0x00) 107 | } 108 | -------------------------------------------------------------------------------- /ttl.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ristretto 18 | 19 | import ( 20 | "sync" 21 | "time" 22 | ) 23 | 24 | var ( 25 | // TODO: find the optimal value or make it configurable. 26 | bucketDurationSecs = int64(5) 27 | ) 28 | 29 | func storageBucket(t time.Time) int64 { 30 | return (t.Unix() / bucketDurationSecs) + 1 31 | } 32 | 33 | func cleanupBucket(t time.Time) int64 { 34 | // The bucket to cleanup is always behind the storage bucket by one so that 35 | // no elements in that bucket (which might not have expired yet) are deleted. 36 | return storageBucket(t) - 1 37 | } 38 | 39 | // bucket type is a map of key to conflict. 40 | type bucket map[uint64]uint64 41 | 42 | // expirationMap is a map of bucket number to the corresponding bucket. 43 | type expirationMap struct { 44 | sync.RWMutex 45 | buckets map[int64]bucket 46 | } 47 | 48 | func newExpirationMap() *expirationMap { 49 | return &expirationMap{ 50 | buckets: make(map[int64]bucket), 51 | } 52 | } 53 | 54 | func (m *expirationMap) add(key, conflict uint64, expiration time.Time) { 55 | if m == nil { 56 | return 57 | } 58 | 59 | // Items that don't expire don't need to be in the expiration map. 60 | if expiration.IsZero() { 61 | return 62 | } 63 | 64 | bucketNum := storageBucket(expiration) 65 | m.Lock() 66 | defer m.Unlock() 67 | 68 | b, ok := m.buckets[bucketNum] 69 | if !ok { 70 | b = make(bucket) 71 | m.buckets[bucketNum] = b 72 | } 73 | b[key] = conflict 74 | } 75 | 76 | func (m *expirationMap) update(key, conflict uint64, oldExpTime, newExpTime time.Time) { 77 | if m == nil { 78 | return 79 | } 80 | if oldExpTime.IsZero() && newExpTime.IsZero() { 81 | return 82 | } 83 | 84 | m.Lock() 85 | defer m.Unlock() 86 | 87 | oldBucketNum := storageBucket(oldExpTime) 88 | newBucketNum := storageBucket(newExpTime) 89 | if oldBucketNum == newBucketNum { 90 | // No change. 91 | return 92 | } 93 | 94 | oldBucket, ok := m.buckets[oldBucketNum] 95 | if ok { 96 | delete(oldBucket, key) 97 | } 98 | 99 | newBucket, ok := m.buckets[newBucketNum] 100 | if !ok { 101 | newBucket = make(bucket) 102 | m.buckets[newBucketNum] = newBucket 103 | } 104 | newBucket[key] = conflict 105 | } 106 | 107 | func (m *expirationMap) del(key uint64, expiration time.Time) { 108 | if m == nil { 109 | return 110 | } 111 | 112 | bucketNum := storageBucket(expiration) 113 | m.Lock() 114 | defer m.Unlock() 115 | _, ok := m.buckets[bucketNum] 116 | if !ok { 117 | return 118 | } 119 | delete(m.buckets[bucketNum], key) 120 | } 121 | 122 | // cleanup removes all the items in the bucket that was just completed. It deletes 123 | // those items from the store, and calls the onEvict function on those items. 124 | // This function is meant to be called periodically. 125 | func (m *expirationMap) cleanup(store *shardedMap, policy *lfuPolicy, onEvict itemCallback) { 126 | if m == nil { 127 | return 128 | } 129 | 130 | m.Lock() 131 | now := time.Now() 132 | bucketNum := cleanupBucket(now) 133 | keys := m.buckets[bucketNum] 134 | delete(m.buckets, bucketNum) 135 | m.Unlock() 136 | 137 | for key, conflict := range keys { 138 | // Sanity check. Verify that the store agrees that this key is expired. 139 | if store.Expiration(key).After(now) { 140 | continue 141 | } 142 | 143 | cost := policy.Cost(key) 144 | policy.Del(key) 145 | _, value := store.Del(key, conflict) 146 | 147 | if onEvict != nil { 148 | onEvict(&Item{Key: key, 149 | Conflict: conflict, 150 | Value: value, 151 | Cost: cost, 152 | }) 153 | } 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /stress_test.go: -------------------------------------------------------------------------------- 1 | package ristretto 2 | 3 | import ( 4 | "container/heap" 5 | "fmt" 6 | "math/rand" 7 | "runtime" 8 | "sync" 9 | "testing" 10 | "time" 11 | 12 | "github.com/outcaste-io/ristretto/sim" 13 | "github.com/stretchr/testify/require" 14 | ) 15 | 16 | func TestStressSetGet(t *testing.T) { 17 | c, err := NewCache(&Config{ 18 | NumCounters: 1000, 19 | MaxCost: 100, 20 | IgnoreInternalCost: true, 21 | BufferItems: 64, 22 | Metrics: true, 23 | }) 24 | require.NoError(t, err) 25 | 26 | for i := 0; i < 100; i++ { 27 | c.Set(i, i, 1) 28 | } 29 | time.Sleep(wait) 30 | wg := &sync.WaitGroup{} 31 | for i := 0; i < runtime.GOMAXPROCS(0); i++ { 32 | wg.Add(1) 33 | go func() { 34 | r := rand.New(rand.NewSource(time.Now().UnixNano())) 35 | for a := 0; a < 1000; a++ { 36 | k := r.Int() % 10 37 | if val, ok := c.Get(k); val == nil || !ok { 38 | err = fmt.Errorf("expected %d but got nil", k) 39 | break 40 | } else if val != nil && val.(int) != k { 41 | err = fmt.Errorf("expected %d but got %d", k, val.(int)) 42 | break 43 | } 44 | } 45 | wg.Done() 46 | }() 47 | } 48 | wg.Wait() 49 | require.NoError(t, err) 50 | require.Equal(t, 1.0, c.Metrics.Ratio()) 51 | } 52 | 53 | func TestStressHitRatio(t *testing.T) { 54 | key := sim.NewZipfian(1.0001, 1, 1000) 55 | c, err := NewCache(&Config{ 56 | NumCounters: 1000, 57 | MaxCost: 100, 58 | BufferItems: 64, 59 | Metrics: true, 60 | }) 61 | require.NoError(t, err) 62 | 63 | o := NewClairvoyant(100) 64 | for i := 0; i < 10000; i++ { 65 | k, err := key() 66 | require.NoError(t, err) 67 | 68 | if _, ok := o.Get(k); !ok { 69 | o.Set(k, k, 1) 70 | } 71 | if _, ok := c.Get(k); !ok { 72 | c.Set(k, k, 1) 73 | } 74 | } 75 | t.Logf("actual: %.2f, optimal: %.2f", c.Metrics.Ratio(), o.Metrics().Ratio()) 76 | } 77 | 78 | // Clairvoyant is a mock cache providing us with optimal hit ratios to compare 79 | // with Ristretto's. It looks ahead and evicts the absolute least valuable item, 80 | // which we try to approximate in a real cache. 81 | type Clairvoyant struct { 82 | capacity uint64 83 | hits map[uint64]uint64 84 | access []uint64 85 | } 86 | 87 | func NewClairvoyant(capacity uint64) *Clairvoyant { 88 | return &Clairvoyant{ 89 | capacity: capacity, 90 | hits: make(map[uint64]uint64), 91 | access: make([]uint64, 0), 92 | } 93 | } 94 | 95 | // Get just records the cache access so that we can later take this event into 96 | // consideration when calculating the absolute least valuable item to evict. 97 | func (c *Clairvoyant) Get(key interface{}) (interface{}, bool) { 98 | c.hits[key.(uint64)]++ 99 | c.access = append(c.access, key.(uint64)) 100 | return nil, false 101 | } 102 | 103 | // Set isn't important because it is only called after a Get (in the case of our 104 | // hit ratio benchmarks, at least). 105 | func (c *Clairvoyant) Set(key, value interface{}, cost int64) bool { 106 | return false 107 | } 108 | 109 | func (c *Clairvoyant) Metrics() *Metrics { 110 | stat := newMetrics() 111 | look := make(map[uint64]struct{}, c.capacity) 112 | data := &clairvoyantHeap{} 113 | heap.Init(data) 114 | for _, key := range c.access { 115 | if _, has := look[key]; has { 116 | stat.add(hit, 0, 1) 117 | continue 118 | } 119 | if uint64(data.Len()) >= c.capacity { 120 | victim := heap.Pop(data) 121 | delete(look, victim.(*clairvoyantItem).key) 122 | } 123 | stat.add(miss, 0, 1) 124 | look[key] = struct{}{} 125 | heap.Push(data, &clairvoyantItem{key, c.hits[key]}) 126 | } 127 | return stat 128 | } 129 | 130 | type clairvoyantItem struct { 131 | key uint64 132 | hits uint64 133 | } 134 | 135 | type clairvoyantHeap []*clairvoyantItem 136 | 137 | func (h clairvoyantHeap) Len() int { return len(h) } 138 | func (h clairvoyantHeap) Less(i, j int) bool { return h[i].hits < h[j].hits } 139 | func (h clairvoyantHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 140 | 141 | func (h *clairvoyantHeap) Push(x interface{}) { 142 | *h = append(*h, x.(*clairvoyantItem)) 143 | } 144 | 145 | func (h *clairvoyantHeap) Pop() interface{} { 146 | old := *h 147 | n := len(old) 148 | x := old[n-1] 149 | *h = old[0 : n-1] 150 | return x 151 | } 152 | -------------------------------------------------------------------------------- /contrib/memtest/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | // #include 20 | import "C" 21 | import ( 22 | "fmt" 23 | "math/rand" 24 | "net/http" 25 | _ "net/http/pprof" 26 | "os" 27 | "os/signal" 28 | "runtime" 29 | "sync/atomic" 30 | "syscall" 31 | "time" 32 | "unsafe" 33 | 34 | "github.com/dustin/go-humanize" 35 | "github.com/outcaste-io/ristretto/z" 36 | ) 37 | 38 | type S struct { 39 | key uint64 40 | val []byte 41 | next *S 42 | inGo bool 43 | } 44 | 45 | var ( 46 | ssz = int(unsafe.Sizeof(S{})) 47 | lo, hi = int64(1 << 30), int64(16 << 30) 48 | increase = true 49 | stop int32 50 | fill []byte 51 | maxMB = 32 52 | 53 | cycles int64 = 16 54 | ) 55 | var numbytes int64 56 | var counter int64 57 | 58 | func newS(sz int) *S { 59 | var s *S 60 | if b := Calloc(ssz); len(b) > 0 { 61 | s = (*S)(unsafe.Pointer(&b[0])) 62 | } else { 63 | s = &S{inGo: true} 64 | } 65 | 66 | s.val = Calloc(sz) 67 | copy(s.val, fill) 68 | if s.next != nil { 69 | panic(fmt.Sprintf("news.next must be nil: %p", s.next)) 70 | } 71 | return s 72 | } 73 | 74 | func freeS(s *S) { 75 | Free(s.val) 76 | if !s.inGo { 77 | buf := (*[z.MaxArrayLen]byte)(unsafe.Pointer(s))[:ssz:ssz] 78 | Free(buf) 79 | } 80 | } 81 | 82 | func (s *S) allocateNext(sz int) { 83 | ns := newS(sz) 84 | s.next, ns.next = ns, s.next 85 | } 86 | 87 | func (s *S) deallocNext() { 88 | if s.next == nil { 89 | panic("next should not be nil") 90 | } 91 | next := s.next 92 | s.next = next.next 93 | freeS(next) 94 | } 95 | 96 | func memory() { 97 | // In normal mode, z.NumAllocBytes would always be zero. So, this program would misbehave. 98 | curMem := NumAllocBytes() 99 | if increase { 100 | if curMem > hi { 101 | increase = false 102 | } 103 | } else { 104 | if curMem < lo { 105 | increase = true 106 | runtime.GC() 107 | time.Sleep(3 * time.Second) 108 | 109 | counter++ 110 | } 111 | } 112 | var js z.MemStats 113 | z.ReadMemStats(&js) 114 | 115 | fmt.Printf("[%d] Current Memory: %s. Increase? %v, MemStats [Active: %s, Allocated: %s,"+ 116 | " Resident: %s, Retained: %s]\n", 117 | counter, humanize.IBytes(uint64(curMem)), increase, 118 | humanize.IBytes(js.Active), humanize.IBytes(js.Allocated), 119 | humanize.IBytes(js.Resident), humanize.IBytes(js.Retained)) 120 | } 121 | 122 | func viaLL() { 123 | ticker := time.NewTicker(10 * time.Millisecond) 124 | defer ticker.Stop() 125 | 126 | root := newS(1) 127 | for range ticker.C { 128 | if counter >= cycles { 129 | fmt.Printf("Finished %d cycles. Deallocating...\n", counter) 130 | break 131 | } 132 | if atomic.LoadInt32(&stop) == 1 { 133 | break 134 | } 135 | if increase { 136 | root.allocateNext(rand.Intn(maxMB) << 20) 137 | } else { 138 | root.deallocNext() 139 | } 140 | memory() 141 | } 142 | for root.next != nil { 143 | root.deallocNext() 144 | memory() 145 | } 146 | freeS(root) 147 | } 148 | 149 | func main() { 150 | check() 151 | fill = make([]byte, maxMB<<20) 152 | rand.Read(fill) 153 | 154 | c := make(chan os.Signal) 155 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) 156 | go func() { 157 | <-c 158 | fmt.Println("Stopping") 159 | atomic.StoreInt32(&stop, 1) 160 | }() 161 | go func() { 162 | if err := http.ListenAndServe("0.0.0.0:8080", nil); err != nil { 163 | panic(fmt.Sprintf("Error: %v", err)) 164 | } 165 | }() 166 | 167 | viaLL() 168 | if left := NumAllocBytes(); left != 0 { 169 | panic(fmt.Sprintf("Unable to deallocate all memory: %v\n", left)) 170 | } 171 | runtime.GC() 172 | fmt.Println("Done. Reduced to zero memory usage.") 173 | time.Sleep(5 * time.Second) 174 | } 175 | -------------------------------------------------------------------------------- /sketch.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This package includes multiple probabalistic data structures needed for 18 | // admission/eviction metadata. Most are Counting Bloom Filter variations, but 19 | // a caching-specific feature that is also required is a "freshness" mechanism, 20 | // which basically serves as a "lifetime" process. This freshness mechanism 21 | // was described in the original TinyLFU paper [1], but other mechanisms may 22 | // be better suited for certain data distributions. 23 | // 24 | // [1]: https://arxiv.org/abs/1512.00727 25 | package ristretto 26 | 27 | import ( 28 | "fmt" 29 | "math/rand" 30 | "time" 31 | ) 32 | 33 | // cmSketch is a Count-Min sketch implementation with 4-bit counters, heavily 34 | // based on Damian Gryski's CM4 [1]. 35 | // 36 | // [1]: https://github.com/dgryski/go-tinylfu/blob/master/cm4.go 37 | type cmSketch struct { 38 | rows [cmDepth]cmRow 39 | seed [cmDepth]uint64 40 | mask uint64 41 | } 42 | 43 | const ( 44 | // cmDepth is the number of counter copies to store (think of it as rows). 45 | cmDepth = 4 46 | ) 47 | 48 | func newCmSketch(numCounters int64) *cmSketch { 49 | if numCounters == 0 { 50 | panic("cmSketch: bad numCounters") 51 | } 52 | // Get the next power of 2 for better cache performance. 53 | numCounters = next2Power(numCounters) 54 | sketch := &cmSketch{mask: uint64(numCounters - 1)} 55 | // Initialize rows of counters and seeds. 56 | source := rand.New(rand.NewSource(time.Now().UnixNano())) 57 | for i := 0; i < cmDepth; i++ { 58 | sketch.seed[i] = source.Uint64() 59 | sketch.rows[i] = newCmRow(numCounters) 60 | } 61 | return sketch 62 | } 63 | 64 | // Increment increments the count(ers) for the specified key. 65 | func (s *cmSketch) Increment(hashed uint64) { 66 | for i := range s.rows { 67 | s.rows[i].increment((hashed ^ s.seed[i]) & s.mask) 68 | } 69 | } 70 | 71 | // Estimate returns the value of the specified key. 72 | func (s *cmSketch) Estimate(hashed uint64) int64 { 73 | min := byte(255) 74 | for i := range s.rows { 75 | val := s.rows[i].get((hashed ^ s.seed[i]) & s.mask) 76 | if val < min { 77 | min = val 78 | } 79 | } 80 | return int64(min) 81 | } 82 | 83 | // Reset halves all counter values. 84 | func (s *cmSketch) Reset() { 85 | for _, r := range s.rows { 86 | r.reset() 87 | } 88 | } 89 | 90 | // Clear zeroes all counters. 91 | func (s *cmSketch) Clear() { 92 | for _, r := range s.rows { 93 | r.clear() 94 | } 95 | } 96 | 97 | // cmRow is a row of bytes, with each byte holding two counters. 98 | type cmRow []byte 99 | 100 | func newCmRow(numCounters int64) cmRow { 101 | return make(cmRow, numCounters/2) 102 | } 103 | 104 | func (r cmRow) get(n uint64) byte { 105 | return byte(r[n/2]>>((n&1)*4)) & 0x0f 106 | } 107 | 108 | func (r cmRow) increment(n uint64) { 109 | // Index of the counter. 110 | i := n / 2 111 | // Shift distance (even 0, odd 4). 112 | s := (n & 1) * 4 113 | // Counter value. 114 | v := (r[i] >> s) & 0x0f 115 | // Only increment if not max value (overflow wrap is bad for LFU). 116 | if v < 15 { 117 | r[i] += 1 << s 118 | } 119 | } 120 | 121 | func (r cmRow) reset() { 122 | // Halve each counter. 123 | for i := range r { 124 | r[i] = (r[i] >> 1) & 0x77 125 | } 126 | } 127 | 128 | func (r cmRow) clear() { 129 | // Zero each counter. 130 | for i := range r { 131 | r[i] = 0 132 | } 133 | } 134 | 135 | func (r cmRow) string() string { 136 | s := "" 137 | for i := uint64(0); i < uint64(len(r)*2); i++ { 138 | s += fmt.Sprintf("%02d ", (r[(i/2)]>>((i&1)*4))&0x0f) 139 | } 140 | s = s[:len(s)-1] 141 | return s 142 | } 143 | 144 | // next2Power rounds x up to the next power of 2, if it's not already one. 145 | func next2Power(x int64) int64 { 146 | x-- 147 | x |= x >> 1 148 | x |= x >> 2 149 | x |= x >> 4 150 | x |= x >> 8 151 | x |= x >> 16 152 | x |= x >> 32 153 | x++ 154 | return x 155 | } 156 | -------------------------------------------------------------------------------- /z/z.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "context" 21 | "fmt" 22 | "os" 23 | "sync" 24 | 25 | "github.com/cespare/xxhash/v2" 26 | ) 27 | 28 | // TODO: Figure out a way to re-use memhash for the second uint64 hash, we 29 | // already know that appending bytes isn't reliable for generating a 30 | // second hash (see Ristretto PR #88). 31 | // 32 | // We also know that while the Go runtime has a runtime memhash128 33 | // function, it's not possible to use it to generate [2]uint64 or 34 | // anything resembling a 128bit hash, even though that's exactly what 35 | // we need in this situation. 36 | func KeyToHash(key interface{}) (uint64, uint64) { 37 | if key == nil { 38 | return 0, 0 39 | } 40 | switch k := key.(type) { 41 | case uint64: 42 | return k, 0 43 | case string: 44 | return MemHashString(k), xxhash.Sum64String(k) 45 | case []byte: 46 | return MemHash(k), xxhash.Sum64(k) 47 | case byte: 48 | return uint64(k), 0 49 | case int: 50 | return uint64(k), 0 51 | case int32: 52 | return uint64(k), 0 53 | case uint32: 54 | return uint64(k), 0 55 | case int64: 56 | return uint64(k), 0 57 | default: 58 | panic("Key type not supported") 59 | } 60 | } 61 | 62 | var ( 63 | dummyCloserChan <-chan struct{} 64 | tmpDir string 65 | ) 66 | 67 | // Closer holds the two things we need to close a goroutine and wait for it to 68 | // finish: a chan to tell the goroutine to shut down, and a WaitGroup with 69 | // which to wait for it to finish shutting down. 70 | type Closer struct { 71 | waiting sync.WaitGroup 72 | 73 | ctx context.Context 74 | cancel context.CancelFunc 75 | } 76 | 77 | // SetTmpDir sets the temporary directory for the temporary buffers. 78 | func SetTmpDir(dir string) { 79 | tmpDir = dir 80 | } 81 | 82 | // NewCloser constructs a new Closer, with an initial count on the WaitGroup. 83 | func NewCloser(initial int) *Closer { 84 | ret := &Closer{} 85 | ret.ctx, ret.cancel = context.WithCancel(context.Background()) 86 | ret.waiting.Add(initial) 87 | return ret 88 | } 89 | 90 | // AddRunning Add()'s delta to the WaitGroup. 91 | func (lc *Closer) AddRunning(delta int) { 92 | lc.waiting.Add(delta) 93 | } 94 | 95 | // Ctx can be used to get a context, which would automatically get cancelled when Signal is called. 96 | func (lc *Closer) Ctx() context.Context { 97 | if lc == nil { 98 | return context.Background() 99 | } 100 | return lc.ctx 101 | } 102 | 103 | // Signal signals the HasBeenClosed signal. 104 | func (lc *Closer) Signal() { 105 | // Todo(ibrahim): Change Signal to return error on next badger breaking change. 106 | lc.cancel() 107 | } 108 | 109 | // HasBeenClosed gets signaled when Signal() is called. 110 | func (lc *Closer) HasBeenClosed() <-chan struct{} { 111 | if lc == nil { 112 | return dummyCloserChan 113 | } 114 | return lc.ctx.Done() 115 | } 116 | 117 | // Done calls Done() on the WaitGroup. 118 | func (lc *Closer) Done() { 119 | if lc == nil { 120 | return 121 | } 122 | lc.waiting.Done() 123 | } 124 | 125 | // Wait waits on the WaitGroup. (It waits for NewCloser's initial value, AddRunning, and Done 126 | // calls to balance out.) 127 | func (lc *Closer) Wait() { 128 | lc.waiting.Wait() 129 | } 130 | 131 | // SignalAndWait calls Signal(), then Wait(). 132 | func (lc *Closer) SignalAndWait() { 133 | lc.Signal() 134 | lc.Wait() 135 | } 136 | 137 | // ZeroOut zeroes out all the bytes in the range [start, end). 138 | func ZeroOut(dst []byte, start, end int) { 139 | if start < 0 || start >= len(dst) { 140 | return // BAD 141 | } 142 | if end >= len(dst) { 143 | end = len(dst) 144 | } 145 | if end-start <= 0 { 146 | return 147 | } 148 | Memclr(dst[start:end]) 149 | // b := dst[start:end] 150 | // for i := range b { 151 | // b[i] = 0x0 152 | // } 153 | } 154 | 155 | func fatal(args ...interface{}) { 156 | defer os.Exit(1) 157 | panic(fmt.Sprint(args...)) 158 | } 159 | 160 | func fatalf(format string, args ...interface{}) { 161 | defer os.Exit(1) 162 | panic(fmt.Sprintf(format, args...)) 163 | } 164 | -------------------------------------------------------------------------------- /z/allocator_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "math/rand" 21 | "sort" 22 | "sync" 23 | "testing" 24 | "unsafe" 25 | 26 | "github.com/stretchr/testify/require" 27 | ) 28 | 29 | func TestAllocate(t *testing.T) { 30 | a := NewAllocator(1024, "test") 31 | defer a.Release() 32 | 33 | check := func() { 34 | t.Logf("Running checks\n") 35 | require.Equal(t, 0, len(a.Allocate(0))) 36 | require.Equal(t, 1, len(a.Allocate(1))) 37 | require.Equal(t, 1<<20+1, len(a.Allocate(1<<20+1))) 38 | require.Equal(t, 256<<20, len(a.Allocate(256<<20))) 39 | require.Panics(t, func() { a.Allocate(maxAlloc + 1) }) 40 | } 41 | 42 | check() 43 | t.Logf("%s", a) 44 | prev := a.Allocated() 45 | t.Logf("Resetting\n") 46 | a.Reset() 47 | check() 48 | t.Logf("%s", a) 49 | require.Equal(t, int(prev), int(a.Allocated())) 50 | t.Logf("Allocated: %d\n", prev) 51 | } 52 | 53 | func TestAllocateSize(t *testing.T) { 54 | a := NewAllocator(1024, "test") 55 | require.Equal(t, 1024, len(a.buffers[0])) 56 | a.Release() 57 | 58 | b := NewAllocator(1025, "test") 59 | require.Equal(t, 2048, len(b.buffers[0])) 60 | b.Release() 61 | } 62 | 63 | func TestAllocateReset(t *testing.T) { 64 | a := NewAllocator(16, "test") 65 | defer a.Release() 66 | 67 | buf := make([]byte, 128) 68 | rand.Read(buf) 69 | for i := 0; i < 1000; i++ { 70 | a.Copy(buf) 71 | } 72 | 73 | prev := a.Allocated() 74 | a.Reset() 75 | for i := 0; i < 100; i++ { 76 | a.Copy(buf) 77 | } 78 | t.Logf("%s", a) 79 | require.Equal(t, prev, a.Allocated()) 80 | } 81 | 82 | func TestAllocateTrim(t *testing.T) { 83 | a := NewAllocator(16, "test") 84 | defer a.Release() 85 | 86 | buf := make([]byte, 128) 87 | rand.Read(buf) 88 | for i := 0; i < 1000; i++ { 89 | a.Copy(buf) 90 | } 91 | 92 | N := 2048 93 | a.TrimTo(N) 94 | require.LessOrEqual(t, int(a.Allocated()), N) 95 | } 96 | 97 | func TestPowTwo(t *testing.T) { 98 | require.Equal(t, 2, log2(4)) 99 | require.Equal(t, 2, log2(7)) 100 | require.Equal(t, 3, log2(8)) 101 | require.Equal(t, 3, log2(15)) 102 | require.Equal(t, 4, log2(16)) 103 | require.Equal(t, 4, log2(31)) 104 | require.Equal(t, 10, log2(1024)) 105 | require.Equal(t, 10, log2(1025)) 106 | require.Equal(t, 10, log2(2047)) 107 | require.Equal(t, 11, log2(2048)) 108 | } 109 | 110 | func TestAllocateAligned(t *testing.T) { 111 | a := NewAllocator(1024, "test") 112 | defer a.Release() 113 | 114 | a.Allocate(1) 115 | out := a.Allocate(1) 116 | ptr := uintptr(unsafe.Pointer(&out[0])) 117 | require.True(t, ptr%8 == 1) 118 | 119 | out = a.AllocateAligned(5) 120 | ptr = uintptr(unsafe.Pointer(&out[0])) 121 | require.True(t, ptr%8 == 0) 122 | 123 | out = a.AllocateAligned(3) 124 | ptr = uintptr(unsafe.Pointer(&out[0])) 125 | require.True(t, ptr%8 == 0) 126 | } 127 | 128 | func TestAllocateConcurrent(t *testing.T) { 129 | a := NewAllocator(63, "test") 130 | defer a.Release() 131 | 132 | N := 10240 133 | M := 16 134 | var wg sync.WaitGroup 135 | 136 | m := make(map[uintptr]struct{}) 137 | mu := new(sync.Mutex) 138 | for i := 0; i < M; i++ { 139 | wg.Add(1) 140 | go func() { 141 | defer wg.Done() 142 | var bufs []uintptr 143 | for j := 0; j < N; j++ { 144 | buf := a.Allocate(16) 145 | require.Equal(t, 16, len(buf)) 146 | bufs = append(bufs, uintptr(unsafe.Pointer(&buf[0]))) 147 | } 148 | 149 | mu.Lock() 150 | for _, b := range bufs { 151 | if _, ok := m[b]; ok { 152 | t.Fatalf("Did not expect to see the same ptr") 153 | } 154 | m[b] = struct{}{} 155 | } 156 | mu.Unlock() 157 | }() 158 | } 159 | wg.Wait() 160 | t.Logf("Size of allocator: %v. Allocator: %s\n", a.Size(), a) 161 | 162 | require.Equal(t, N*M, len(m)) 163 | var sorted []uintptr 164 | for ptr := range m { 165 | sorted = append(sorted, ptr) 166 | } 167 | 168 | sort.Slice(sorted, func(i, j int) bool { 169 | return sorted[i] < sorted[j] 170 | }) 171 | 172 | var last uintptr 173 | for _, ptr := range sorted { 174 | if ptr-last < 16 { 175 | t.Fatalf("Should not have less than 16: %v %v\n", ptr, last) 176 | } 177 | // fmt.Printf("ptr [%d]: %x %d\n", i, ptr, ptr-last) 178 | last = ptr 179 | } 180 | } 181 | 182 | func BenchmarkAllocate(b *testing.B) { 183 | a := NewAllocator(15, "test") 184 | b.RunParallel(func(pb *testing.PB) { 185 | for pb.Next() { 186 | buf := a.Allocate(1) 187 | if len(buf) != 1 { 188 | b.FailNow() 189 | } 190 | } 191 | }) 192 | b.StopTimer() 193 | b.Logf("%s", a) 194 | } 195 | -------------------------------------------------------------------------------- /store_test.go: -------------------------------------------------------------------------------- 1 | package ristretto 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | 7 | "github.com/outcaste-io/ristretto/z" 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | func TestStoreSetGet(t *testing.T) { 12 | s := newShardedMap(nil) 13 | key, conflict := z.KeyToHash(1) 14 | i := Item{ 15 | Key: key, 16 | Conflict: conflict, 17 | Value: 2, 18 | } 19 | s.Set(&i) 20 | val, ok := s.Get(key, conflict) 21 | require.True(t, ok) 22 | require.Equal(t, 2, val.(int)) 23 | 24 | i.Value = 3 25 | s.Set(&i) 26 | val, ok = s.Get(key, conflict) 27 | require.True(t, ok) 28 | require.Equal(t, 3, val.(int)) 29 | 30 | key, conflict = z.KeyToHash(2) 31 | i = Item{ 32 | Key: key, 33 | Conflict: conflict, 34 | Value: 2, 35 | } 36 | s.Set(&i) 37 | val, ok = s.Get(key, conflict) 38 | require.True(t, ok) 39 | require.Equal(t, 2, val.(int)) 40 | } 41 | 42 | func TestStoreDel(t *testing.T) { 43 | s := newShardedMap(nil) 44 | key, conflict := z.KeyToHash(1) 45 | i := Item{ 46 | Key: key, 47 | Conflict: conflict, 48 | Value: 1, 49 | } 50 | s.Set(&i) 51 | s.Del(key, conflict) 52 | val, ok := s.Get(key, conflict) 53 | require.False(t, ok) 54 | require.Nil(t, val) 55 | 56 | s.Del(2, 0) 57 | } 58 | 59 | func TestStoreClear(t *testing.T) { 60 | s := newShardedMap(nil) 61 | for i := uint64(0); i < 1000; i++ { 62 | key, conflict := z.KeyToHash(i) 63 | it := Item{ 64 | Key: key, 65 | Conflict: conflict, 66 | Value: i, 67 | } 68 | s.Set(&it) 69 | } 70 | s.Clear(nil) 71 | for i := uint64(0); i < 1000; i++ { 72 | key, conflict := z.KeyToHash(i) 73 | val, ok := s.Get(key, conflict) 74 | require.False(t, ok) 75 | require.Nil(t, val) 76 | } 77 | } 78 | 79 | func TestStoreUpdate(t *testing.T) { 80 | s := newShardedMap(nil) 81 | key, conflict := z.KeyToHash(1) 82 | i := Item{ 83 | Key: key, 84 | Conflict: conflict, 85 | Value: 1, 86 | } 87 | s.Set(&i) 88 | i.Value = 2 89 | _, ok := s.Update(&i) 90 | require.True(t, ok) 91 | 92 | val, ok := s.Get(key, conflict) 93 | require.True(t, ok) 94 | require.NotNil(t, val) 95 | 96 | val, ok = s.Get(key, conflict) 97 | require.True(t, ok) 98 | require.Equal(t, 2, val.(int)) 99 | 100 | i.Value = 3 101 | _, ok = s.Update(&i) 102 | require.True(t, ok) 103 | 104 | val, ok = s.Get(key, conflict) 105 | require.True(t, ok) 106 | require.Equal(t, 3, val.(int)) 107 | 108 | key, conflict = z.KeyToHash(2) 109 | i = Item{ 110 | Key: key, 111 | Conflict: conflict, 112 | Value: 2, 113 | } 114 | _, ok = s.Update(&i) 115 | require.False(t, ok) 116 | val, ok = s.Get(key, conflict) 117 | require.False(t, ok) 118 | require.Nil(t, val) 119 | } 120 | 121 | func TestStoreCollision(t *testing.T) { 122 | s := newShardedMap(nil) 123 | s.shards[1].Lock() 124 | s.shards[1].data[1] = storeItem{ 125 | key: 1, 126 | conflict: 0, 127 | value: 1, 128 | } 129 | s.shards[1].Unlock() 130 | val, ok := s.Get(1, 1) 131 | require.False(t, ok) 132 | require.Nil(t, val) 133 | 134 | i := Item{ 135 | Key: 1, 136 | Conflict: 1, 137 | Value: 2, 138 | } 139 | s.Set(&i) 140 | val, ok = s.Get(1, 0) 141 | require.True(t, ok) 142 | require.NotEqual(t, 2, val.(int)) 143 | 144 | _, ok = s.Update(&i) 145 | require.False(t, ok) 146 | val, ok = s.Get(1, 0) 147 | require.True(t, ok) 148 | require.NotEqual(t, 2, val.(int)) 149 | 150 | s.Del(1, 1) 151 | val, ok = s.Get(1, 0) 152 | require.True(t, ok) 153 | require.NotNil(t, val) 154 | } 155 | 156 | func TestStoreExpiration(t *testing.T) { 157 | s := newShardedMap(nil) 158 | key, conflict := z.KeyToHash(1) 159 | expiration := time.Now().Add(time.Second) 160 | i := Item{ 161 | Key: key, 162 | Conflict: conflict, 163 | Value: 1, 164 | Expiration: expiration, 165 | } 166 | s.Set(&i) 167 | val, ok := s.Get(key, conflict) 168 | require.True(t, ok) 169 | require.Equal(t, 1, val.(int)) 170 | 171 | ttl := s.Expiration(key) 172 | require.Equal(t, expiration, ttl) 173 | 174 | s.Del(key, conflict) 175 | 176 | _, ok = s.Get(key, conflict) 177 | require.False(t, ok) 178 | require.True(t, s.Expiration(key).IsZero()) 179 | 180 | // missing item 181 | key, _ = z.KeyToHash(4340958203495) 182 | ttl = s.Expiration(key) 183 | require.True(t, ttl.IsZero()) 184 | } 185 | 186 | func BenchmarkStoreGet(b *testing.B) { 187 | s := newShardedMap(nil) 188 | key, conflict := z.KeyToHash(1) 189 | i := Item{ 190 | Key: key, 191 | Conflict: conflict, 192 | Value: 1, 193 | } 194 | s.Set(&i) 195 | b.SetBytes(1) 196 | b.RunParallel(func(pb *testing.PB) { 197 | for pb.Next() { 198 | s.Get(key, conflict) 199 | } 200 | }) 201 | } 202 | 203 | func BenchmarkStoreSet(b *testing.B) { 204 | s := newShardedMap(nil) 205 | key, conflict := z.KeyToHash(1) 206 | b.SetBytes(1) 207 | b.RunParallel(func(pb *testing.PB) { 208 | for pb.Next() { 209 | i := Item{ 210 | Key: key, 211 | Conflict: conflict, 212 | Value: 1, 213 | } 214 | s.Set(&i) 215 | } 216 | }) 217 | } 218 | 219 | func BenchmarkStoreUpdate(b *testing.B) { 220 | s := newShardedMap(nil) 221 | key, conflict := z.KeyToHash(1) 222 | i := Item{ 223 | Key: key, 224 | Conflict: conflict, 225 | Value: 1, 226 | } 227 | s.Set(&i) 228 | b.SetBytes(1) 229 | b.RunParallel(func(pb *testing.PB) { 230 | for pb.Next() { 231 | s.Update(&Item{ 232 | Key: key, 233 | Conflict: conflict, 234 | Value: 2, 235 | }) 236 | } 237 | }) 238 | } 239 | -------------------------------------------------------------------------------- /z/calloc_jemalloc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 | // of this source code is governed by a BSD-style license that can be found in 3 | // the LICENSE file. 4 | 5 | //go:build jemalloc 6 | // +build jemalloc 7 | 8 | package z 9 | 10 | /* 11 | #cgo LDFLAGS: /usr/local/lib/libjemalloc_outcaste.a -L/usr/local/lib -Wl,-rpath,/usr/local/lib -ljemalloc_outcaste -lm -lstdc++ -pthread -ldl 12 | #include 13 | #include 14 | */ 15 | import "C" 16 | import ( 17 | "bytes" 18 | "fmt" 19 | "sync" 20 | "sync/atomic" 21 | "unsafe" 22 | 23 | "github.com/dustin/go-humanize" 24 | ) 25 | 26 | // The go:linkname directives provides backdoor access to private functions in 27 | // the runtime. Below we're accessing the throw function. 28 | 29 | //go:linkname throw runtime.throw 30 | func throw(s string) 31 | 32 | // New allocates a slice of size n. The returned slice is from manually managed 33 | // memory and MUST be released by calling Free. Failure to do so will result in 34 | // a memory leak. 35 | // 36 | // Compile jemalloc with ./configure --with-jemalloc-prefix="je_" 37 | // https://android.googlesource.com/platform/external/jemalloc_new/+/6840b22e8e11cb68b493297a5cd757d6eaa0b406/TUNING.md 38 | // These two config options seems useful for frequent allocations and deallocations in 39 | // multi-threaded programs (like we have). 40 | // JE_MALLOC_CONF="background_thread:true,metadata_thp:auto" 41 | // 42 | // Compile Go program with `go build -tags=jemalloc` to enable this. 43 | 44 | type dalloc struct { 45 | t string 46 | sz int 47 | } 48 | 49 | var dallocsMu sync.Mutex 50 | var dallocs map[unsafe.Pointer]*dalloc 51 | 52 | func init() { 53 | // By initializing dallocs, we can start tracking allocations and deallocations via z.Calloc. 54 | dallocs = make(map[unsafe.Pointer]*dalloc) 55 | } 56 | 57 | func Calloc(n int, tag string) []byte { 58 | if n == 0 { 59 | return make([]byte, 0) 60 | } 61 | // We need to be conscious of the Cgo pointer passing rules: 62 | // 63 | // https://golang.org/cmd/cgo/#hdr-Passing_pointers 64 | // 65 | // ... 66 | // Note: the current implementation has a bug. While Go code is permitted 67 | // to write nil or a C pointer (but not a Go pointer) to C memory, the 68 | // current implementation may sometimes cause a runtime error if the 69 | // contents of the C memory appear to be a Go pointer. Therefore, avoid 70 | // passing uninitialized C memory to Go code if the Go code is going to 71 | // store pointer values in it. Zero out the memory in C before passing it 72 | // to Go. 73 | 74 | ptr := C.je_calloc(C.size_t(n), 1) 75 | if ptr == nil { 76 | // NB: throw is like panic, except it guarantees the process will be 77 | // terminated. The call below is exactly what the Go runtime invokes when 78 | // it cannot allocate memory. 79 | throw("out of memory") 80 | } 81 | 82 | uptr := unsafe.Pointer(ptr) 83 | dallocsMu.Lock() 84 | dallocs[uptr] = &dalloc{ 85 | t: tag, 86 | sz: n, 87 | } 88 | dallocsMu.Unlock() 89 | atomic.AddInt64(&numBytes, int64(n)) 90 | // Interpret the C pointer as a pointer to a Go array, then slice. 91 | return (*[MaxArrayLen]byte)(uptr)[:n:n] 92 | } 93 | 94 | // CallocNoRef does the exact same thing as Calloc with jemalloc enabled. 95 | func CallocNoRef(n int, tag string) []byte { 96 | return Calloc(n, tag) 97 | } 98 | 99 | // Free frees the specified slice. 100 | func Free(b []byte) { 101 | if sz := cap(b); sz != 0 { 102 | b = b[:cap(b)] 103 | ptr := unsafe.Pointer(&b[0]) 104 | C.je_free(ptr) 105 | atomic.AddInt64(&numBytes, -int64(sz)) 106 | dallocsMu.Lock() 107 | delete(dallocs, ptr) 108 | dallocsMu.Unlock() 109 | } 110 | } 111 | 112 | func Leaks() string { 113 | if dallocs == nil { 114 | return "Leak detection disabled. Enable with 'leak' build flag." 115 | } 116 | dallocsMu.Lock() 117 | defer dallocsMu.Unlock() 118 | if len(dallocs) == 0 { 119 | return "NO leaks found." 120 | } 121 | m := make(map[string]int) 122 | for _, da := range dallocs { 123 | m[da.t] += da.sz 124 | } 125 | var buf bytes.Buffer 126 | fmt.Fprintf(&buf, "Allocations:\n") 127 | for f, sz := range m { 128 | fmt.Fprintf(&buf, "%s at file: %s\n", humanize.IBytes(uint64(sz)), f) 129 | } 130 | return buf.String() 131 | } 132 | 133 | // ReadMemStats populates stats with JE Malloc statistics. 134 | func ReadMemStats(stats *MemStats) { 135 | if stats == nil { 136 | return 137 | } 138 | // Call an epoch mallclt to refresh the stats data as mentioned in the docs. 139 | // http://jemalloc.net/jemalloc.3.html#epoch 140 | // Note: This epoch mallctl is as expensive as a malloc call. It takes up the 141 | // malloc_mutex_lock. 142 | epoch := 1 143 | sz := unsafe.Sizeof(&epoch) 144 | C.je_mallctl( 145 | (C.CString)("epoch"), 146 | unsafe.Pointer(&epoch), 147 | (*C.size_t)(unsafe.Pointer(&sz)), 148 | unsafe.Pointer(&epoch), 149 | (C.size_t)(unsafe.Sizeof(epoch))) 150 | stats.Allocated = fetchStat("stats.allocated") 151 | stats.Active = fetchStat("stats.active") 152 | stats.Resident = fetchStat("stats.resident") 153 | stats.Retained = fetchStat("stats.retained") 154 | } 155 | 156 | // fetchStat is used to read a specific attribute from je malloc stats using mallctl. 157 | func fetchStat(s string) uint64 { 158 | var out uint64 159 | sz := unsafe.Sizeof(&out) 160 | C.je_mallctl( 161 | (C.CString)(s), // Query: eg: stats.allocated, stats.resident, etc. 162 | unsafe.Pointer(&out), // Variable to store the output. 163 | (*C.size_t)(unsafe.Pointer(&sz)), // Size of the output variable. 164 | nil, // Input variable used to set a value. 165 | 0) // Size of the input variable. 166 | return out 167 | } 168 | 169 | func StatsPrint() { 170 | opts := C.CString("mdablxe") 171 | C.je_malloc_stats_print(nil, nil, opts) 172 | C.free(unsafe.Pointer(opts)) 173 | } 174 | -------------------------------------------------------------------------------- /z/README.md: -------------------------------------------------------------------------------- 1 | ## bbloom: a bitset Bloom filter for go/golang 2 | === 3 | 4 | package implements a fast bloom filter with real 'bitset' and JSONMarshal/JSONUnmarshal to store/reload the Bloom filter. 5 | 6 | NOTE: the package uses unsafe.Pointer to set and read the bits from the bitset. If you're uncomfortable with using the unsafe package, please consider using my bloom filter package at github.com/AndreasBriese/bloom 7 | 8 | === 9 | 10 | changelog 11/2015: new thread safe methods AddTS(), HasTS(), AddIfNotHasTS() following a suggestion from Srdjan Marinovic (github @a-little-srdjan), who used this to code a bloomfilter cache. 11 | 12 | This bloom filter was developed to strengthen a website-log database and was tested and optimized for this log-entry mask: "2014/%02i/%02i %02i:%02i:%02i /info.html". 13 | Nonetheless bbloom should work with any other form of entries. 14 | 15 | ~~Hash function is a modified Berkeley DB sdbm hash (to optimize for smaller strings). sdbm http://www.cse.yorku.ca/~oz/hash.html~~ 16 | 17 | Found sipHash (SipHash-2-4, a fast short-input PRF created by Jean-Philippe Aumasson and Daniel J. Bernstein.) to be about as fast. sipHash had been ported by Dimtry Chestnyk to Go (github.com/dchest/siphash ) 18 | 19 | Minimum hashset size is: 512 ([4]uint64; will be set automatically). 20 | 21 | ###install 22 | 23 | ```sh 24 | go get github.com/AndreasBriese/bbloom 25 | ``` 26 | 27 | ###test 28 | + change to folder ../bbloom 29 | + create wordlist in file "words.txt" (you might use `python permut.py`) 30 | + run 'go test -bench=.' within the folder 31 | 32 | ```go 33 | go test -bench=. 34 | ``` 35 | 36 | ~~If you've installed the GOCONVEY TDD-framework http://goconvey.co/ you can run the tests automatically.~~ 37 | 38 | using go's testing framework now (have in mind that the op timing is related to 65536 operations of Add, Has, AddIfNotHas respectively) 39 | 40 | ### usage 41 | 42 | after installation add 43 | 44 | ```go 45 | import ( 46 | ... 47 | "github.com/AndreasBriese/bbloom" 48 | ... 49 | ) 50 | ``` 51 | 52 | at your header. In the program use 53 | 54 | ```go 55 | // create a bloom filter for 65536 items and 1 % wrong-positive ratio 56 | bf := bbloom.New(float64(1<<16), float64(0.01)) 57 | 58 | // or 59 | // create a bloom filter with 650000 for 65536 items and 7 locs per hash explicitly 60 | // bf = bbloom.New(float64(650000), float64(7)) 61 | // or 62 | bf = bbloom.New(650000.0, 7.0) 63 | 64 | // add one item 65 | bf.Add([]byte("butter")) 66 | 67 | // Number of elements added is exposed now 68 | // Note: ElemNum will not be included in JSON export (for compatability to older version) 69 | nOfElementsInFilter := bf.ElemNum 70 | 71 | // check if item is in the filter 72 | isIn := bf.Has([]byte("butter")) // should be true 73 | isNotIn := bf.Has([]byte("Butter")) // should be false 74 | 75 | // 'add only if item is new' to the bloomfilter 76 | added := bf.AddIfNotHas([]byte("butter")) // should be false because 'butter' is already in the set 77 | added = bf.AddIfNotHas([]byte("buTTer")) // should be true because 'buTTer' is new 78 | 79 | // thread safe versions for concurrent use: AddTS, HasTS, AddIfNotHasTS 80 | // add one item 81 | bf.AddTS([]byte("peanutbutter")) 82 | // check if item is in the filter 83 | isIn = bf.HasTS([]byte("peanutbutter")) // should be true 84 | isNotIn = bf.HasTS([]byte("peanutButter")) // should be false 85 | // 'add only if item is new' to the bloomfilter 86 | added = bf.AddIfNotHasTS([]byte("butter")) // should be false because 'peanutbutter' is already in the set 87 | added = bf.AddIfNotHasTS([]byte("peanutbuTTer")) // should be true because 'penutbuTTer' is new 88 | 89 | // convert to JSON ([]byte) 90 | Json := bf.JSONMarshal() 91 | 92 | // bloomfilters Mutex is exposed for external un-/locking 93 | // i.e. mutex lock while doing JSON conversion 94 | bf.Mtx.Lock() 95 | Json = bf.JSONMarshal() 96 | bf.Mtx.Unlock() 97 | 98 | // restore a bloom filter from storage 99 | bfNew := bbloom.JSONUnmarshal(Json) 100 | 101 | isInNew := bfNew.Has([]byte("butter")) // should be true 102 | isNotInNew := bfNew.Has([]byte("Butter")) // should be false 103 | 104 | ``` 105 | 106 | to work with the bloom filter. 107 | 108 | ### why 'fast'? 109 | 110 | It's about 3 times faster than William Fitzgeralds bitset bloom filter https://github.com/willf/bloom . And it is about so fast as my []bool set variant for Boom filters (see https://github.com/AndreasBriese/bloom ) but having a 8times smaller memory footprint: 111 | 112 | 113 | Bloom filter (filter size 524288, 7 hashlocs) 114 | github.com/AndreasBriese/bbloom 'Add' 65536 items (10 repetitions): 6595800 ns (100 ns/op) 115 | github.com/AndreasBriese/bbloom 'Has' 65536 items (10 repetitions): 5986600 ns (91 ns/op) 116 | github.com/AndreasBriese/bloom 'Add' 65536 items (10 repetitions): 6304684 ns (96 ns/op) 117 | github.com/AndreasBriese/bloom 'Has' 65536 items (10 repetitions): 6568663 ns (100 ns/op) 118 | 119 | github.com/willf/bloom 'Add' 65536 items (10 repetitions): 24367224 ns (371 ns/op) 120 | github.com/willf/bloom 'Test' 65536 items (10 repetitions): 21881142 ns (333 ns/op) 121 | github.com/dataence/bloom/standard 'Add' 65536 items (10 repetitions): 23041644 ns (351 ns/op) 122 | github.com/dataence/bloom/standard 'Check' 65536 items (10 repetitions): 19153133 ns (292 ns/op) 123 | github.com/cabello/bloom 'Add' 65536 items (10 repetitions): 131921507 ns (2012 ns/op) 124 | github.com/cabello/bloom 'Contains' 65536 items (10 repetitions): 131108962 ns (2000 ns/op) 125 | 126 | (on MBPro15 OSX10.8.5 i7 4Core 2.4Ghz) 127 | 128 | 129 | With 32bit bloom filters (bloom32) using modified sdbm, bloom32 does hashing with only 2 bit shifts, one xor and one substraction per byte. smdb is about as fast as fnv64a but gives less collisions with the dataset (see mask above). bloom.New(float64(10 * 1<<16),float64(7)) populated with 1<<16 random items from the dataset (see above) and tested against the rest results in less than 0.05% collisions. 130 | -------------------------------------------------------------------------------- /sim/sim.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package sim 18 | 19 | import ( 20 | "bufio" 21 | "errors" 22 | "fmt" 23 | "io" 24 | "math/rand" 25 | "strconv" 26 | "strings" 27 | "time" 28 | ) 29 | 30 | var ( 31 | // ErrDone is returned when the underlying file has ran out of lines. 32 | ErrDone = errors.New("no more values in the Simulator") 33 | // ErrBadLine is returned when the trace file line is unrecognizable to 34 | // the Parser. 35 | ErrBadLine = errors.New("bad line for trace format") 36 | ) 37 | 38 | // Simulator is the central type of the `sim` package. It is a function 39 | // returning a key from some source (composed from the other functions in this 40 | // package, either generated or parsed). You can use these Simulators to 41 | // approximate access distributions. 42 | type Simulator func() (uint64, error) 43 | 44 | // NewZipfian creates a Simulator returning numbers following a Zipfian [1] 45 | // distribution infinitely. Zipfian distributions are useful for simulating real 46 | // workloads. 47 | // 48 | // [1]: https://en.wikipedia.org/wiki/Zipf%27s_law 49 | func NewZipfian(s, v float64, n uint64) Simulator { 50 | z := rand.NewZipf(rand.New(rand.NewSource(time.Now().UnixNano())), s, v, n) 51 | return func() (uint64, error) { 52 | return z.Uint64(), nil 53 | } 54 | } 55 | 56 | // NewUniform creates a Simulator returning uniformly distributed [1] (random) 57 | // numbers [0, max) infinitely. 58 | // 59 | // [1]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) 60 | func NewUniform(max uint64) Simulator { 61 | m := int64(max) 62 | r := rand.New(rand.NewSource(time.Now().UnixNano())) 63 | return func() (uint64, error) { 64 | return uint64(r.Int63n(m)), nil 65 | } 66 | } 67 | 68 | // Parser is used as a parameter to NewReader so we can create Simulators from 69 | // varying trace file formats easily. 70 | type Parser func(string, error) ([]uint64, error) 71 | 72 | // NewReader creates a Simulator from two components: the Parser, which is a 73 | // filetype specific function for parsing lines, and the file itself, which will 74 | // be read from. 75 | // 76 | // When every line in the file has been read, ErrDone will be returned. For some 77 | // trace formats (LIRS) there is one item per line. For others (ARC) there is a 78 | // range of items on each line. Thus, the true number of items in each file 79 | // is hard to determine, so it's up to the user to handle ErrDone accordingly. 80 | func NewReader(parser Parser, file io.Reader) Simulator { 81 | b := bufio.NewReader(file) 82 | s := make([]uint64, 0) 83 | i := -1 84 | var err error 85 | return func() (uint64, error) { 86 | // only parse a new line when we've run out of items 87 | if i++; i == len(s) { 88 | // parse sequence from line 89 | if s, err = parser(b.ReadString('\n')); err != nil { 90 | s = []uint64{0} 91 | } 92 | i = 0 93 | } 94 | return s[i], err 95 | } 96 | } 97 | 98 | // ParseLIRS takes a single line of input from a LIRS trace file as described in 99 | // multiple papers [1] and returns a slice containing one number. A nice 100 | // collection of LIRS trace files can be found in Ben Manes' repo [2]. 101 | // 102 | // [1]: https://en.wikipedia.org/wiki/LIRS_caching_algorithm 103 | // [2]: https://git.io/fj9gU 104 | func ParseLIRS(line string, err error) ([]uint64, error) { 105 | if line = strings.TrimSpace(line); line != "" { 106 | // example: "1\r\n" 107 | key, err := strconv.ParseUint(line, 10, 64) 108 | return []uint64{key}, err 109 | } 110 | return nil, ErrDone 111 | } 112 | 113 | // ParseARC takes a single line of input from an ARC trace file as described in 114 | // "ARC: a self-tuning, low overhead replacement cache" [1] by Nimrod Megiddo 115 | // and Dharmendra S. Modha [1] and returns a sequence of numbers generated from 116 | // the line and any error. For use with NewReader. 117 | // 118 | // [1]: https://scinapse.io/papers/1860107648 119 | func ParseARC(line string, err error) ([]uint64, error) { 120 | if line != "" { 121 | // example: "0 5 0 0\n" 122 | // 123 | // - first block: starting number in sequence 124 | // - second block: number of items in sequence 125 | // - third block: ignore 126 | // - fourth block: global line number (not used) 127 | cols := strings.Fields(line) 128 | if len(cols) != 4 { 129 | return nil, ErrBadLine 130 | } 131 | start, err := strconv.ParseUint(cols[0], 10, 64) 132 | if err != nil { 133 | return nil, err 134 | } 135 | count, err := strconv.ParseUint(cols[1], 10, 64) 136 | if err != nil { 137 | return nil, err 138 | } 139 | // populate sequence from start to start + count 140 | seq := make([]uint64, count) 141 | for i := range seq { 142 | seq[i] = start + uint64(i) 143 | } 144 | return seq, nil 145 | } 146 | return nil, ErrDone 147 | } 148 | 149 | // Collection evaluates the Simulator size times and saves each item to the 150 | // returned slice. 151 | func Collection(simulator Simulator, size uint64) []uint64 { 152 | collection := make([]uint64, size) 153 | for i := range collection { 154 | collection[i], _ = simulator() 155 | } 156 | return collection 157 | } 158 | 159 | // StringCollection evaluates the Simulator size times and saves each item to 160 | // the returned slice, after converting it to a string. 161 | func StringCollection(simulator Simulator, size uint64) []string { 162 | collection := make([]string, size) 163 | for i := range collection { 164 | n, _ := simulator() 165 | collection[i] = fmt.Sprintf("%d", n) 166 | } 167 | return collection 168 | } 169 | -------------------------------------------------------------------------------- /store.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ristretto 18 | 19 | import ( 20 | "sync" 21 | "time" 22 | ) 23 | 24 | // TODO: Do we need this to be a separate struct from Item? 25 | type storeItem struct { 26 | key uint64 27 | conflict uint64 28 | value interface{} 29 | expiration time.Time 30 | } 31 | 32 | const numShards uint64 = 256 33 | 34 | type updateFn func(prev, cur interface{}) bool 35 | type shardedMap struct { 36 | shards []*lockedMap 37 | expiryMap *expirationMap 38 | shouldUpdate func(prev, cur interface{}) bool 39 | } 40 | 41 | // newShardedMap is safe for concurrent usage. 42 | func newShardedMap(fn updateFn) *shardedMap { 43 | sm := &shardedMap{ 44 | shards: make([]*lockedMap, int(numShards)), 45 | expiryMap: newExpirationMap(), 46 | } 47 | if fn == nil { 48 | fn = func(prev, cur interface{}) bool { 49 | return true 50 | } 51 | } 52 | for i := range sm.shards { 53 | sm.shards[i] = newLockedMap(fn, sm.expiryMap) 54 | } 55 | return sm 56 | } 57 | 58 | func (sm *shardedMap) Get(key, conflict uint64) (interface{}, bool) { 59 | return sm.shards[key%numShards].get(key, conflict) 60 | } 61 | 62 | func (sm *shardedMap) Expiration(key uint64) time.Time { 63 | return sm.shards[key%numShards].Expiration(key) 64 | } 65 | 66 | func (sm *shardedMap) Set(i *Item) { 67 | if i == nil { 68 | // If item is nil make this Set a no-op. 69 | return 70 | } 71 | 72 | sm.shards[i.Key%numShards].Set(i) 73 | } 74 | 75 | func (sm *shardedMap) Del(key, conflict uint64) (uint64, interface{}) { 76 | return sm.shards[key%numShards].Del(key, conflict) 77 | } 78 | 79 | func (sm *shardedMap) Update(newItem *Item) (interface{}, bool) { 80 | return sm.shards[newItem.Key%numShards].Update(newItem) 81 | } 82 | 83 | func (sm *shardedMap) Cleanup(policy *lfuPolicy, onEvict itemCallback) { 84 | sm.expiryMap.cleanup(sm, policy, onEvict) 85 | } 86 | 87 | func (sm *shardedMap) Clear(onEvict itemCallback) { 88 | for i := uint64(0); i < numShards; i++ { 89 | sm.shards[i].Clear(onEvict) 90 | } 91 | } 92 | 93 | type lockedMap struct { 94 | sync.RWMutex 95 | data map[uint64]storeItem 96 | em *expirationMap 97 | shouldUpdate updateFn 98 | } 99 | 100 | func newLockedMap(fn updateFn, em *expirationMap) *lockedMap { 101 | return &lockedMap{ 102 | data: make(map[uint64]storeItem), 103 | em: em, 104 | shouldUpdate: fn, 105 | } 106 | } 107 | 108 | func (m *lockedMap) get(key, conflict uint64) (interface{}, bool) { 109 | m.RLock() 110 | item, ok := m.data[key] 111 | m.RUnlock() 112 | if !ok { 113 | return nil, false 114 | } 115 | if conflict != 0 && (conflict != item.conflict) { 116 | return nil, false 117 | } 118 | 119 | // Handle expired items. 120 | if !item.expiration.IsZero() && time.Now().After(item.expiration) { 121 | return nil, false 122 | } 123 | return item.value, true 124 | } 125 | 126 | func (m *lockedMap) Expiration(key uint64) time.Time { 127 | m.RLock() 128 | defer m.RUnlock() 129 | return m.data[key].expiration 130 | } 131 | 132 | func (m *lockedMap) Set(i *Item) { 133 | if i == nil { 134 | // If the item is nil make this Set a no-op. 135 | return 136 | } 137 | 138 | m.Lock() 139 | defer m.Unlock() 140 | item, ok := m.data[i.Key] 141 | 142 | if ok { 143 | // The item existed already. We need to check the conflict key and reject the 144 | // update if they do not match. Only after that the expiration map is updated. 145 | if i.Conflict != 0 && (i.Conflict != item.conflict) { 146 | return 147 | } 148 | if !m.shouldUpdate(item.value, i.Value) { 149 | return 150 | } 151 | m.em.update(i.Key, i.Conflict, item.expiration, i.Expiration) 152 | } else { 153 | // The value is not in the map already. There's no need to return anything. 154 | // Simply add the expiration map. 155 | m.em.add(i.Key, i.Conflict, i.Expiration) 156 | } 157 | 158 | m.data[i.Key] = storeItem{ 159 | key: i.Key, 160 | conflict: i.Conflict, 161 | value: i.Value, 162 | expiration: i.Expiration, 163 | } 164 | } 165 | 166 | func (m *lockedMap) Del(key, conflict uint64) (uint64, interface{}) { 167 | m.Lock() 168 | item, ok := m.data[key] 169 | if !ok { 170 | m.Unlock() 171 | return 0, nil 172 | } 173 | if conflict != 0 && (conflict != item.conflict) { 174 | m.Unlock() 175 | return 0, nil 176 | } 177 | 178 | if !item.expiration.IsZero() { 179 | m.em.del(key, item.expiration) 180 | } 181 | 182 | delete(m.data, key) 183 | m.Unlock() 184 | return item.conflict, item.value 185 | } 186 | 187 | func (m *lockedMap) Update(newItem *Item) (interface{}, bool) { 188 | m.Lock() 189 | defer m.Unlock() 190 | 191 | item, ok := m.data[newItem.Key] 192 | if !ok { 193 | return nil, false 194 | } 195 | if newItem.Conflict != 0 && (newItem.Conflict != item.conflict) { 196 | return nil, false 197 | } 198 | if !m.shouldUpdate(item.value, newItem.Value) { 199 | return item.value, false 200 | } 201 | 202 | m.em.update(newItem.Key, newItem.Conflict, item.expiration, newItem.Expiration) 203 | m.data[newItem.Key] = storeItem{ 204 | key: newItem.Key, 205 | conflict: newItem.Conflict, 206 | value: newItem.Value, 207 | expiration: newItem.Expiration, 208 | } 209 | return item.value, true 210 | } 211 | 212 | func (m *lockedMap) Clear(onEvict itemCallback) { 213 | m.Lock() 214 | i := &Item{} 215 | if onEvict != nil { 216 | for _, si := range m.data { 217 | i.Key = si.key 218 | i.Conflict = si.conflict 219 | i.Value = si.value 220 | onEvict(i) 221 | } 222 | } 223 | m.data = make(map[uint64]storeItem) 224 | m.Unlock() 225 | } 226 | -------------------------------------------------------------------------------- /policy_test.go: -------------------------------------------------------------------------------- 1 | package ristretto 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | 7 | "github.com/stretchr/testify/require" 8 | ) 9 | 10 | func TestPolicy(t *testing.T) { 11 | defer func() { 12 | require.Nil(t, recover()) 13 | }() 14 | newPolicy(100, 10) 15 | } 16 | 17 | func TestPolicyMetrics(t *testing.T) { 18 | p := newPolicy(100, 10) 19 | p.CollectMetrics(newMetrics()) 20 | require.NotNil(t, p.metrics) 21 | require.NotNil(t, p.costs.metrics) 22 | } 23 | 24 | func TestPolicyProcessItems(t *testing.T) { 25 | p := newPolicy(100, 10) 26 | p.itemsCh <- []uint64{1, 2, 2} 27 | time.Sleep(wait) 28 | p.Lock() 29 | require.Equal(t, int64(2), p.admit.Estimate(2)) 30 | require.Equal(t, int64(1), p.admit.Estimate(1)) 31 | p.Unlock() 32 | 33 | p.stop <- struct{}{} 34 | p.itemsCh <- []uint64{3, 3, 3} 35 | time.Sleep(wait) 36 | p.Lock() 37 | require.Equal(t, int64(0), p.admit.Estimate(3)) 38 | p.Unlock() 39 | } 40 | 41 | func TestPolicyPush(t *testing.T) { 42 | p := newPolicy(100, 10) 43 | require.True(t, p.Push([]uint64{})) 44 | 45 | keepCount := 0 46 | for i := 0; i < 10; i++ { 47 | if p.Push([]uint64{1, 2, 3, 4, 5}) { 48 | keepCount++ 49 | } 50 | } 51 | require.NotEqual(t, 0, keepCount) 52 | } 53 | 54 | func TestPolicyAdd(t *testing.T) { 55 | p := newPolicy(1000, 100) 56 | if victims, added := p.Add(1, 101); victims != nil || added { 57 | t.Fatal("can't add an item bigger than entire cache") 58 | } 59 | p.Lock() 60 | p.costs.add(1, 1) 61 | p.admit.Increment(1) 62 | p.admit.Increment(2) 63 | p.admit.Increment(3) 64 | p.Unlock() 65 | 66 | victims, added := p.Add(1, 1) 67 | require.Nil(t, victims) 68 | require.False(t, added) 69 | 70 | victims, added = p.Add(2, 20) 71 | require.Nil(t, victims) 72 | require.True(t, added) 73 | 74 | victims, added = p.Add(3, 90) 75 | require.NotNil(t, victims) 76 | require.True(t, added) 77 | 78 | victims, added = p.Add(4, 20) 79 | require.NotNil(t, victims) 80 | require.False(t, added) 81 | } 82 | 83 | func TestPolicyHas(t *testing.T) { 84 | p := newPolicy(100, 10) 85 | p.Add(1, 1) 86 | require.True(t, p.Has(1)) 87 | require.False(t, p.Has(2)) 88 | } 89 | 90 | func TestPolicyDel(t *testing.T) { 91 | p := newPolicy(100, 10) 92 | p.Add(1, 1) 93 | p.Del(1) 94 | p.Del(2) 95 | require.False(t, p.Has(1)) 96 | require.False(t, p.Has(2)) 97 | } 98 | 99 | func TestPolicyCap(t *testing.T) { 100 | p := newPolicy(100, 10) 101 | p.Add(1, 1) 102 | require.Equal(t, int64(9), p.Cap()) 103 | } 104 | 105 | func TestPolicyUpdate(t *testing.T) { 106 | p := newPolicy(100, 10) 107 | p.Add(1, 1) 108 | p.Update(1, 2) 109 | p.Lock() 110 | require.Equal(t, int64(2), p.costs.keyCosts[1]) 111 | p.Unlock() 112 | } 113 | 114 | func TestPolicyCost(t *testing.T) { 115 | p := newPolicy(100, 10) 116 | p.Add(1, 2) 117 | require.Equal(t, int64(2), p.Cost(1)) 118 | require.Equal(t, int64(-1), p.Cost(2)) 119 | } 120 | 121 | func TestPolicyClear(t *testing.T) { 122 | p := newPolicy(100, 10) 123 | p.Add(1, 1) 124 | p.Add(2, 2) 125 | p.Add(3, 3) 126 | p.Clear() 127 | require.Equal(t, int64(10), p.Cap()) 128 | require.False(t, p.Has(1)) 129 | require.False(t, p.Has(2)) 130 | require.False(t, p.Has(3)) 131 | } 132 | 133 | func TestPolicyClose(t *testing.T) { 134 | defer func() { 135 | require.NotNil(t, recover()) 136 | }() 137 | 138 | p := newPolicy(100, 10) 139 | p.Add(1, 1) 140 | p.Close() 141 | p.itemsCh <- []uint64{1} 142 | } 143 | 144 | func TestPushAfterClose(t *testing.T) { 145 | p := newPolicy(100, 10) 146 | p.Close() 147 | require.False(t, p.Push([]uint64{1, 2})) 148 | } 149 | 150 | func TestAddAfterClose(t *testing.T) { 151 | p := newPolicy(100, 10) 152 | p.Close() 153 | p.Add(1, 1) 154 | } 155 | 156 | func TestSampledLFUAdd(t *testing.T) { 157 | e := newSampledLFU(4) 158 | e.add(1, 1) 159 | e.add(2, 2) 160 | e.add(3, 1) 161 | require.Equal(t, int64(4), e.used) 162 | require.Equal(t, int64(2), e.keyCosts[2]) 163 | } 164 | 165 | func TestSampledLFUDel(t *testing.T) { 166 | e := newSampledLFU(4) 167 | e.add(1, 1) 168 | e.add(2, 2) 169 | e.del(2) 170 | require.Equal(t, int64(1), e.used) 171 | _, ok := e.keyCosts[2] 172 | require.False(t, ok) 173 | e.del(4) 174 | } 175 | 176 | func TestSampledLFUUpdate(t *testing.T) { 177 | e := newSampledLFU(4) 178 | e.add(1, 1) 179 | require.True(t, e.updateIfHas(1, 2)) 180 | require.Equal(t, int64(2), e.used) 181 | require.False(t, e.updateIfHas(2, 2)) 182 | } 183 | 184 | func TestSampledLFUClear(t *testing.T) { 185 | e := newSampledLFU(4) 186 | e.add(1, 1) 187 | e.add(2, 2) 188 | e.add(3, 1) 189 | e.clear() 190 | require.Equal(t, 0, len(e.keyCosts)) 191 | require.Equal(t, int64(0), e.used) 192 | } 193 | 194 | func TestSampledLFURoom(t *testing.T) { 195 | e := newSampledLFU(16) 196 | e.add(1, 1) 197 | e.add(2, 2) 198 | e.add(3, 3) 199 | require.Equal(t, int64(6), e.roomLeft(4)) 200 | } 201 | 202 | func TestSampledLFUSample(t *testing.T) { 203 | e := newSampledLFU(16) 204 | e.add(4, 4) 205 | e.add(5, 5) 206 | sample := e.fillSample([]*policyPair{ 207 | {1, 1}, 208 | {2, 2}, 209 | {3, 3}, 210 | }) 211 | k := sample[len(sample)-1].key 212 | require.Equal(t, 5, len(sample)) 213 | require.NotEqual(t, 1, k) 214 | require.NotEqual(t, 2, k) 215 | require.NotEqual(t, 3, k) 216 | require.Equal(t, len(sample), len(e.fillSample(sample))) 217 | e.del(5) 218 | sample = e.fillSample(sample[:len(sample)-2]) 219 | require.Equal(t, 4, len(sample)) 220 | } 221 | 222 | func TestTinyLFUIncrement(t *testing.T) { 223 | a := newTinyLFU(4) 224 | a.Increment(1) 225 | a.Increment(1) 226 | a.Increment(1) 227 | require.True(t, a.door.Has(1)) 228 | require.Equal(t, int64(2), a.freq.Estimate(1)) 229 | 230 | a.Increment(1) 231 | require.False(t, a.door.Has(1)) 232 | require.Equal(t, int64(1), a.freq.Estimate(1)) 233 | } 234 | 235 | func TestTinyLFUEstimate(t *testing.T) { 236 | a := newTinyLFU(8) 237 | a.Increment(1) 238 | a.Increment(1) 239 | a.Increment(1) 240 | require.Equal(t, int64(3), a.Estimate(1)) 241 | require.Equal(t, int64(0), a.Estimate(2)) 242 | } 243 | 244 | func TestTinyLFUPush(t *testing.T) { 245 | a := newTinyLFU(16) 246 | a.Push([]uint64{1, 2, 2, 3, 3, 3}) 247 | require.Equal(t, int64(1), a.Estimate(1)) 248 | require.Equal(t, int64(2), a.Estimate(2)) 249 | require.Equal(t, int64(3), a.Estimate(3)) 250 | require.Equal(t, int64(6), a.incrs) 251 | } 252 | 253 | func TestTinyLFUClear(t *testing.T) { 254 | a := newTinyLFU(16) 255 | a.Push([]uint64{1, 3, 3, 3}) 256 | a.clear() 257 | require.Equal(t, int64(0), a.incrs) 258 | require.Equal(t, int64(0), a.Estimate(3)) 259 | } 260 | -------------------------------------------------------------------------------- /z/histogram.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "fmt" 21 | "math" 22 | "strings" 23 | 24 | "github.com/dustin/go-humanize" 25 | ) 26 | 27 | // Creates bounds for an histogram. The bounds are powers of two of the form 28 | // [2^min_exponent, ..., 2^max_exponent]. 29 | func HistogramBounds(minExponent, maxExponent uint32) []float64 { 30 | var bounds []float64 31 | for i := minExponent; i <= maxExponent; i++ { 32 | bounds = append(bounds, float64(int(1)< 4) 39 | bounds := make([]float64, num) 40 | bounds[0] = 1 41 | bounds[1] = 2 42 | for i := 2; i < num; i++ { 43 | bounds[i] = bounds[i-1] + bounds[i-2] 44 | } 45 | return bounds 46 | } 47 | 48 | // HistogramData stores the information needed to represent the sizes of the keys and values 49 | // as a histogram. 50 | type HistogramData struct { 51 | Bounds []float64 52 | Count int64 53 | CountPerBucket []int64 54 | Min int64 55 | Max int64 56 | Sum int64 57 | } 58 | 59 | // NewHistogramData returns a new instance of HistogramData with properly initialized fields. 60 | func NewHistogramData(bounds []float64) *HistogramData { 61 | return &HistogramData{ 62 | Bounds: bounds, 63 | CountPerBucket: make([]int64, len(bounds)+1), 64 | Max: 0, 65 | Min: math.MaxInt64, 66 | } 67 | } 68 | 69 | func (histogram *HistogramData) Copy() *HistogramData { 70 | if histogram == nil { 71 | return nil 72 | } 73 | return &HistogramData{ 74 | Bounds: append([]float64{}, histogram.Bounds...), 75 | CountPerBucket: append([]int64{}, histogram.CountPerBucket...), 76 | Count: histogram.Count, 77 | Min: histogram.Min, 78 | Max: histogram.Max, 79 | Sum: histogram.Sum, 80 | } 81 | } 82 | 83 | // Update changes the Min and Max fields if value is less than or greater than the current values. 84 | func (histogram *HistogramData) Update(value int64) { 85 | if histogram == nil { 86 | return 87 | } 88 | if value > histogram.Max { 89 | histogram.Max = value 90 | } 91 | if value < histogram.Min { 92 | histogram.Min = value 93 | } 94 | 95 | histogram.Sum += value 96 | histogram.Count++ 97 | 98 | for index := 0; index <= len(histogram.Bounds); index++ { 99 | // Allocate value in the last buckets if we reached the end of the Bounds array. 100 | if index == len(histogram.Bounds) { 101 | histogram.CountPerBucket[index]++ 102 | break 103 | } 104 | 105 | if value < int64(histogram.Bounds[index]) { 106 | histogram.CountPerBucket[index]++ 107 | break 108 | } 109 | } 110 | } 111 | 112 | // Mean returns the mean value for the histogram. 113 | func (histogram *HistogramData) Mean() float64 { 114 | if histogram.Count == 0 { 115 | return 0 116 | } 117 | return float64(histogram.Sum) / float64(histogram.Count) 118 | } 119 | 120 | // String converts the histogram data into human-readable string. 121 | func (histogram *HistogramData) String() string { 122 | if histogram == nil { 123 | return "" 124 | } 125 | var b strings.Builder 126 | 127 | b.WriteString("\n -- Histogram: \n") 128 | b.WriteString(fmt.Sprintf("Min value: %d \n", histogram.Min)) 129 | b.WriteString(fmt.Sprintf("Max value: %d \n", histogram.Max)) 130 | b.WriteString(fmt.Sprintf("Count: %d \n", histogram.Count)) 131 | b.WriteString(fmt.Sprintf("50p: %.2f \n", histogram.Percentile(0.5))) 132 | b.WriteString(fmt.Sprintf("75p: %.2f \n", histogram.Percentile(0.75))) 133 | b.WriteString(fmt.Sprintf("90p: %.2f \n", histogram.Percentile(0.90))) 134 | 135 | numBounds := len(histogram.Bounds) 136 | var cum float64 137 | for index, count := range histogram.CountPerBucket { 138 | if count == 0 { 139 | continue 140 | } 141 | 142 | // The last bucket represents the bucket that contains the range from 143 | // the last bound up to infinity so it's processed differently than the 144 | // other buckets. 145 | if index == len(histogram.CountPerBucket)-1 { 146 | lowerBound := uint64(histogram.Bounds[numBounds-1]) 147 | page := float64(count*100) / float64(histogram.Count) 148 | cum += page 149 | b.WriteString(fmt.Sprintf("[%s, %s) %d %.2f%% %.2f%%\n", 150 | humanize.IBytes(lowerBound), "infinity", count, page, cum)) 151 | continue 152 | } 153 | 154 | upperBound := uint64(histogram.Bounds[index]) 155 | lowerBound := uint64(0) 156 | if index > 0 { 157 | lowerBound = uint64(histogram.Bounds[index-1]) 158 | } 159 | 160 | page := float64(count*100) / float64(histogram.Count) 161 | cum += page 162 | b.WriteString(fmt.Sprintf("[%d, %d) %d %.2f%% %.2f%%\n", 163 | lowerBound, upperBound, count, page, cum)) 164 | } 165 | b.WriteString(" --\n") 166 | return b.String() 167 | } 168 | 169 | // Percentile returns the percentile value for the histogram. 170 | // value of p should be between [0.0-1.0] 171 | func (histogram *HistogramData) Percentile(p float64) float64 { 172 | if histogram == nil { 173 | return 0 174 | } 175 | 176 | if histogram.Count == 0 { 177 | // if no data return the minimum range 178 | return histogram.Bounds[0] 179 | } 180 | pval := int64(float64(histogram.Count) * p) 181 | for i, v := range histogram.CountPerBucket { 182 | pval = pval - v 183 | if pval <= 0 { 184 | if i == len(histogram.Bounds) { 185 | break 186 | } 187 | return histogram.Bounds[i] 188 | } 189 | } 190 | // default return should be the max range 191 | return histogram.Bounds[len(histogram.Bounds)-1] 192 | } 193 | 194 | // Clear reset the histogram. Helpful in situations where we need to reset the metrics 195 | func (histogram *HistogramData) Clear() { 196 | if histogram == nil { 197 | return 198 | } 199 | 200 | histogram.Count = 0 201 | histogram.CountPerBucket = make([]int64, len(histogram.Bounds)+1) 202 | histogram.Sum = 0 203 | histogram.Max = 0 204 | histogram.Min = math.MaxInt64 205 | } 206 | -------------------------------------------------------------------------------- /z/file.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "encoding/binary" 21 | "fmt" 22 | "io" 23 | "os" 24 | "path/filepath" 25 | 26 | "github.com/pkg/errors" 27 | ) 28 | 29 | // MmapFile represents an mmapd file and includes both the buffer to the data 30 | // and the file descriptor. 31 | type MmapFile struct { 32 | Data []byte 33 | Fd *os.File 34 | } 35 | 36 | var NewFile = errors.New("Create a new file") 37 | 38 | func OpenMmapFileUsing(fd *os.File, sz int, writable bool) (*MmapFile, error) { 39 | filename := fd.Name() 40 | fi, err := fd.Stat() 41 | if err != nil { 42 | return nil, errors.Wrapf(err, "cannot stat file: %s", filename) 43 | } 44 | 45 | var rerr error 46 | fileSize := fi.Size() 47 | if sz > 0 && fileSize == 0 { 48 | // If file is empty, truncate it to sz. 49 | if err := fd.Truncate(int64(sz)); err != nil { 50 | return nil, errors.Wrapf(err, "error while truncation") 51 | } 52 | fileSize = int64(sz) 53 | rerr = NewFile 54 | } 55 | 56 | // fmt.Printf("Mmaping file: %s with writable: %v filesize: %d\n", fd.Name(), writable, fileSize) 57 | buf, err := Mmap(fd, writable, fileSize) // Mmap up to file size. 58 | if err != nil { 59 | return nil, errors.Wrapf(err, "while mmapping %s with size: %d", fd.Name(), fileSize) 60 | } 61 | 62 | if fileSize == 0 { 63 | dir, _ := filepath.Split(filename) 64 | go SyncDir(dir) 65 | } 66 | return &MmapFile{ 67 | Data: buf, 68 | Fd: fd, 69 | }, rerr 70 | } 71 | 72 | // OpenMmapFile opens an existing file or creates a new file. If the file is 73 | // created, it would truncate the file to maxSz. In both cases, it would mmap 74 | // the file to maxSz and returned it. In case the file is created, z.NewFile is 75 | // returned. 76 | func OpenMmapFile(filename string, flag int, maxSz int) (*MmapFile, error) { 77 | // fmt.Printf("opening file %s with flag: %v\n", filename, flag) 78 | fd, err := os.OpenFile(filename, flag, 0666) 79 | if err != nil { 80 | return nil, errors.Wrapf(err, "unable to open: %s", filename) 81 | } 82 | writable := true 83 | if flag == os.O_RDONLY { 84 | writable = false 85 | } 86 | return OpenMmapFileUsing(fd, maxSz, writable) 87 | } 88 | 89 | type mmapReader struct { 90 | Data []byte 91 | offset int 92 | } 93 | 94 | func (mr *mmapReader) Read(buf []byte) (int, error) { 95 | if mr.offset > len(mr.Data) { 96 | return 0, io.EOF 97 | } 98 | n := copy(buf, mr.Data[mr.offset:]) 99 | mr.offset += n 100 | if n < len(buf) { 101 | return n, io.EOF 102 | } 103 | return n, nil 104 | } 105 | 106 | func (m *MmapFile) NewReader(offset int) io.Reader { 107 | return &mmapReader{ 108 | Data: m.Data, 109 | offset: offset, 110 | } 111 | } 112 | 113 | // Bytes returns data starting from offset off of size sz. If there's not enough data, it would 114 | // return nil slice and io.EOF. 115 | func (m *MmapFile) Bytes(off, sz int) ([]byte, error) { 116 | if len(m.Data[off:]) < sz { 117 | return nil, io.EOF 118 | } 119 | return m.Data[off : off+sz], nil 120 | } 121 | 122 | // Slice returns the slice at the given offset. 123 | func (m *MmapFile) Slice(offset int) []byte { 124 | sz := binary.BigEndian.Uint32(m.Data[offset:]) 125 | start := offset + 4 126 | next := start + int(sz) 127 | if next > len(m.Data) { 128 | return []byte{} 129 | } 130 | res := m.Data[start:next] 131 | return res 132 | } 133 | 134 | // AllocateSlice allocates a slice of the given size at the given offset. 135 | func (m *MmapFile) AllocateSlice(sz, offset int) ([]byte, int, error) { 136 | start := offset + 4 137 | 138 | // If the file is too small, double its size or increase it by 1GB, whichever is smaller. 139 | if start+sz > len(m.Data) { 140 | const oneGB = 1 << 30 141 | growBy := len(m.Data) 142 | if growBy > oneGB { 143 | growBy = oneGB 144 | } 145 | if growBy < sz+4 { 146 | growBy = sz + 4 147 | } 148 | if err := m.Truncate(int64(len(m.Data) + growBy)); err != nil { 149 | return nil, 0, err 150 | } 151 | } 152 | 153 | binary.BigEndian.PutUint32(m.Data[offset:], uint32(sz)) 154 | return m.Data[start : start+sz], start + sz, nil 155 | } 156 | 157 | func (m *MmapFile) Sync() error { 158 | if m == nil { 159 | return nil 160 | } 161 | return Msync(m.Data) 162 | } 163 | 164 | func (m *MmapFile) Delete() error { 165 | // Badger can set the m.Data directly, without setting any Fd. In that case, this should be a 166 | // NOOP. 167 | if m.Fd == nil { 168 | return nil 169 | } 170 | 171 | if err := Munmap(m.Data); err != nil { 172 | return fmt.Errorf("while munmap file: %s, error: %v\n", m.Fd.Name(), err) 173 | } 174 | m.Data = nil 175 | if err := m.Fd.Truncate(0); err != nil { 176 | return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err) 177 | } 178 | if err := m.Fd.Close(); err != nil { 179 | return fmt.Errorf("while close file: %s, error: %v\n", m.Fd.Name(), err) 180 | } 181 | return os.Remove(m.Fd.Name()) 182 | } 183 | 184 | // Close would close the file. It would also truncate the file if maxSz >= 0. 185 | func (m *MmapFile) Close(maxSz int64) error { 186 | // Badger can set the m.Data directly, without setting any Fd. In that case, this should be a 187 | // NOOP. 188 | if m.Fd == nil { 189 | return nil 190 | } 191 | if err := m.Sync(); err != nil { 192 | return fmt.Errorf("while sync file: %s, error: %v\n", m.Fd.Name(), err) 193 | } 194 | if err := Munmap(m.Data); err != nil { 195 | return fmt.Errorf("while munmap file: %s, error: %v\n", m.Fd.Name(), err) 196 | } 197 | if maxSz >= 0 { 198 | if err := m.Fd.Truncate(maxSz); err != nil { 199 | return fmt.Errorf("while truncate file: %s, error: %v\n", m.Fd.Name(), err) 200 | } 201 | } 202 | return m.Fd.Close() 203 | } 204 | 205 | func SyncDir(dir string) error { 206 | df, err := os.Open(dir) 207 | if err != nil { 208 | return errors.Wrapf(err, "while opening %s", dir) 209 | } 210 | if err := df.Sync(); err != nil { 211 | return errors.Wrapf(err, "while syncing %s", dir) 212 | } 213 | if err := df.Close(); err != nil { 214 | return errors.Wrapf(err, "while closing %s", dir) 215 | } 216 | return nil 217 | } 218 | -------------------------------------------------------------------------------- /z/rtutil_test.go: -------------------------------------------------------------------------------- 1 | package z 2 | 3 | import ( 4 | "hash/fnv" 5 | "math/rand" 6 | "sync/atomic" 7 | "testing" 8 | "time" 9 | 10 | "github.com/dgryski/go-farm" 11 | ) 12 | 13 | func BenchmarkMemHash(b *testing.B) { 14 | buf := make([]byte, 64) 15 | rand.Read(buf) 16 | 17 | b.ReportAllocs() 18 | b.ResetTimer() 19 | for i := 0; i < b.N; i++ { 20 | _ = MemHash(buf) 21 | } 22 | b.SetBytes(int64(len(buf))) 23 | } 24 | 25 | func BenchmarkMemHashString(b *testing.B) { 26 | s := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." 27 | 28 | b.ReportAllocs() 29 | b.ResetTimer() 30 | for i := 0; i < b.N; i++ { 31 | _ = MemHashString(s) 32 | } 33 | b.SetBytes(int64(len(s))) 34 | } 35 | 36 | func BenchmarkSip(b *testing.B) { 37 | buf := make([]byte, 64) 38 | rand.Read(buf) 39 | for i := 0; i < b.N; i++ { 40 | SipHash(buf) 41 | } 42 | } 43 | 44 | func BenchmarkFarm(b *testing.B) { 45 | buf := make([]byte, 64) 46 | rand.Read(buf) 47 | for i := 0; i < b.N; i++ { 48 | farm.Fingerprint64(buf) 49 | } 50 | } 51 | 52 | func BenchmarkFnv(b *testing.B) { 53 | buf := make([]byte, 64) 54 | rand.Read(buf) 55 | f := fnv.New64a() 56 | for i := 0; i < b.N; i++ { 57 | f.Write(buf) 58 | f.Sum64() 59 | f.Reset() 60 | } 61 | } 62 | 63 | func SipHash(p []byte) (l, h uint64) { 64 | // Initialization. 65 | v0 := uint64(8317987320269560794) // k0 ^ 0x736f6d6570736575 66 | v1 := uint64(7237128889637516672) // k1 ^ 0x646f72616e646f6d 67 | v2 := uint64(7816392314733513934) // k0 ^ 0x6c7967656e657261 68 | v3 := uint64(8387220255325274014) // k1 ^ 0x7465646279746573 69 | t := uint64(len(p)) << 56 70 | 71 | // Compression. 72 | for len(p) >= 8 { 73 | m := uint64(p[0]) | uint64(p[1])<<8 | uint64(p[2])<<16 | uint64(p[3])<<24 | 74 | uint64(p[4])<<32 | uint64(p[5])<<40 | uint64(p[6])<<48 | uint64(p[7])<<56 75 | 76 | v3 ^= m 77 | 78 | // Round 1. 79 | v0 += v1 80 | v1 = v1<<13 | v1>>51 81 | v1 ^= v0 82 | v0 = v0<<32 | v0>>32 83 | 84 | v2 += v3 85 | v3 = v3<<16 | v3>>48 86 | v3 ^= v2 87 | 88 | v0 += v3 89 | v3 = v3<<21 | v3>>43 90 | v3 ^= v0 91 | 92 | v2 += v1 93 | v1 = v1<<17 | v1>>47 94 | v1 ^= v2 95 | v2 = v2<<32 | v2>>32 96 | 97 | // Round 2. 98 | v0 += v1 99 | v1 = v1<<13 | v1>>51 100 | v1 ^= v0 101 | v0 = v0<<32 | v0>>32 102 | 103 | v2 += v3 104 | v3 = v3<<16 | v3>>48 105 | v3 ^= v2 106 | 107 | v0 += v3 108 | v3 = v3<<21 | v3>>43 109 | v3 ^= v0 110 | 111 | v2 += v1 112 | v1 = v1<<17 | v1>>47 113 | v1 ^= v2 114 | v2 = v2<<32 | v2>>32 115 | 116 | v0 ^= m 117 | p = p[8:] 118 | } 119 | 120 | // Compress last block. 121 | switch len(p) { 122 | case 7: 123 | t |= uint64(p[6]) << 48 124 | fallthrough 125 | case 6: 126 | t |= uint64(p[5]) << 40 127 | fallthrough 128 | case 5: 129 | t |= uint64(p[4]) << 32 130 | fallthrough 131 | case 4: 132 | t |= uint64(p[3]) << 24 133 | fallthrough 134 | case 3: 135 | t |= uint64(p[2]) << 16 136 | fallthrough 137 | case 2: 138 | t |= uint64(p[1]) << 8 139 | fallthrough 140 | case 1: 141 | t |= uint64(p[0]) 142 | } 143 | 144 | v3 ^= t 145 | 146 | // Round 1. 147 | v0 += v1 148 | v1 = v1<<13 | v1>>51 149 | v1 ^= v0 150 | v0 = v0<<32 | v0>>32 151 | 152 | v2 += v3 153 | v3 = v3<<16 | v3>>48 154 | v3 ^= v2 155 | 156 | v0 += v3 157 | v3 = v3<<21 | v3>>43 158 | v3 ^= v0 159 | 160 | v2 += v1 161 | v1 = v1<<17 | v1>>47 162 | v1 ^= v2 163 | v2 = v2<<32 | v2>>32 164 | 165 | // Round 2. 166 | v0 += v1 167 | v1 = v1<<13 | v1>>51 168 | v1 ^= v0 169 | v0 = v0<<32 | v0>>32 170 | 171 | v2 += v3 172 | v3 = v3<<16 | v3>>48 173 | v3 ^= v2 174 | 175 | v0 += v3 176 | v3 = v3<<21 | v3>>43 177 | v3 ^= v0 178 | 179 | v2 += v1 180 | v1 = v1<<17 | v1>>47 181 | v1 ^= v2 182 | v2 = v2<<32 | v2>>32 183 | 184 | v0 ^= t 185 | 186 | // Finalization. 187 | v2 ^= 0xff 188 | 189 | // Round 1. 190 | v0 += v1 191 | v1 = v1<<13 | v1>>51 192 | v1 ^= v0 193 | v0 = v0<<32 | v0>>32 194 | 195 | v2 += v3 196 | v3 = v3<<16 | v3>>48 197 | v3 ^= v2 198 | 199 | v0 += v3 200 | v3 = v3<<21 | v3>>43 201 | v3 ^= v0 202 | 203 | v2 += v1 204 | v1 = v1<<17 | v1>>47 205 | v1 ^= v2 206 | v2 = v2<<32 | v2>>32 207 | 208 | // Round 2. 209 | v0 += v1 210 | v1 = v1<<13 | v1>>51 211 | v1 ^= v0 212 | v0 = v0<<32 | v0>>32 213 | 214 | v2 += v3 215 | v3 = v3<<16 | v3>>48 216 | v3 ^= v2 217 | 218 | v0 += v3 219 | v3 = v3<<21 | v3>>43 220 | v3 ^= v0 221 | 222 | v2 += v1 223 | v1 = v1<<17 | v1>>47 224 | v1 ^= v2 225 | v2 = v2<<32 | v2>>32 226 | 227 | // Round 3. 228 | v0 += v1 229 | v1 = v1<<13 | v1>>51 230 | v1 ^= v0 231 | v0 = v0<<32 | v0>>32 232 | 233 | v2 += v3 234 | v3 = v3<<16 | v3>>48 235 | v3 ^= v2 236 | 237 | v0 += v3 238 | v3 = v3<<21 | v3>>43 239 | v3 ^= v0 240 | 241 | v2 += v1 242 | v1 = v1<<17 | v1>>47 243 | v1 ^= v2 244 | v2 = v2<<32 | v2>>32 245 | 246 | // Round 4. 247 | v0 += v1 248 | v1 = v1<<13 | v1>>51 249 | v1 ^= v0 250 | v0 = v0<<32 | v0>>32 251 | 252 | v2 += v3 253 | v3 = v3<<16 | v3>>48 254 | v3 ^= v2 255 | 256 | v0 += v3 257 | v3 = v3<<21 | v3>>43 258 | v3 ^= v0 259 | 260 | v2 += v1 261 | v1 = v1<<17 | v1>>47 262 | v1 ^= v2 263 | v2 = v2<<32 | v2>>32 264 | 265 | // return v0 ^ v1 ^ v2 ^ v3 266 | 267 | hash := v0 ^ v1 ^ v2 ^ v3 268 | h = hash >> 1 269 | l = hash << 1 >> 1 270 | return l, h 271 | } 272 | 273 | func BenchmarkNanoTime(b *testing.B) { 274 | for i := 0; i < b.N; i++ { 275 | NanoTime() 276 | } 277 | } 278 | 279 | func BenchmarkCPUTicks(b *testing.B) { 280 | for i := 0; i < b.N; i++ { 281 | CPUTicks() 282 | } 283 | } 284 | 285 | // goos: linux 286 | // goarch: amd64 287 | // pkg: github.com/outcaste-io/ristretto/z 288 | // BenchmarkFastRand-16 1000000000 0.292 ns/op 289 | // BenchmarkRandSource-16 1000000000 0.747 ns/op 290 | // BenchmarkRandGlobal-16 6822332 176 ns/op 291 | // BenchmarkRandAtomic-16 77950322 15.4 ns/op 292 | // PASS 293 | // ok github.com/outcaste-io/ristretto/z 4.808s 294 | func benchmarkRand(b *testing.B, fab func() func() uint32) { 295 | b.RunParallel(func(pb *testing.PB) { 296 | gen := fab() 297 | for pb.Next() { 298 | gen() 299 | } 300 | }) 301 | } 302 | 303 | func BenchmarkFastRand(b *testing.B) { 304 | benchmarkRand(b, func() func() uint32 { 305 | return FastRand 306 | }) 307 | } 308 | 309 | func BenchmarkRandSource(b *testing.B) { 310 | benchmarkRand(b, func() func() uint32 { 311 | s := rand.New(rand.NewSource(time.Now().Unix())) 312 | return func() uint32 { return s.Uint32() } 313 | }) 314 | } 315 | 316 | func BenchmarkRandGlobal(b *testing.B) { 317 | benchmarkRand(b, func() func() uint32 { 318 | return func() uint32 { return rand.Uint32() } 319 | }) 320 | } 321 | 322 | func BenchmarkRandAtomic(b *testing.B) { 323 | var x uint32 324 | benchmarkRand(b, func() func() uint32 { 325 | return func() uint32 { return uint32(atomic.AddUint32(&x, 1)) } 326 | }) 327 | } 328 | -------------------------------------------------------------------------------- /metrics.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package ristretto 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "sync" 23 | "sync/atomic" 24 | 25 | "github.com/outcaste-io/ristretto/z" 26 | ) 27 | 28 | type metricType int 29 | 30 | const ( 31 | // The following 2 keep track of hits and misses. 32 | hit = iota 33 | miss 34 | // The following 3 keep track of number of keys added, updated and evicted. 35 | keyAdd 36 | keyUpdate 37 | keyEvict 38 | // The following 2 keep track of cost of keys added and evicted. 39 | costAdd 40 | costEvict 41 | // The following keep track of how many sets were dropped or rejected later. 42 | dropSets 43 | rejectSets 44 | // The following 2 keep track of how many gets were kept and dropped on the 45 | // floor. 46 | dropGets 47 | keepGets 48 | // This should be the final enum. Other enums should be set before this. 49 | doNotUse 50 | ) 51 | 52 | func stringFor(t metricType) string { 53 | switch t { 54 | case hit: 55 | return "hit" 56 | case miss: 57 | return "miss" 58 | case keyAdd: 59 | return "keys-added" 60 | case keyUpdate: 61 | return "keys-updated" 62 | case keyEvict: 63 | return "keys-evicted" 64 | case costAdd: 65 | return "cost-added" 66 | case costEvict: 67 | return "cost-evicted" 68 | case dropSets: 69 | return "sets-dropped" 70 | case rejectSets: 71 | return "sets-rejected" // by policy. 72 | case dropGets: 73 | return "gets-dropped" 74 | case keepGets: 75 | return "gets-kept" 76 | default: 77 | return "unidentified" 78 | } 79 | } 80 | 81 | // Metrics is a snapshot of performance statistics for the lifetime of a cache instance. 82 | type Metrics struct { 83 | all [doNotUse][]*uint64 84 | 85 | mu sync.RWMutex 86 | life *z.HistogramData // Tracks the life expectancy of a key. 87 | } 88 | 89 | // collectMetrics just creates a new *Metrics instance and adds the pointers 90 | // to the cache and policy instances. 91 | func (c *Cache) collectMetrics() { 92 | c.Metrics = newMetrics() 93 | c.policy.CollectMetrics(c.Metrics) 94 | } 95 | 96 | func newMetrics() *Metrics { 97 | s := &Metrics{ 98 | life: z.NewHistogramData(z.HistogramBounds(1, 16)), 99 | } 100 | for i := 0; i < doNotUse; i++ { 101 | s.all[i] = make([]*uint64, 256) 102 | slice := s.all[i] 103 | for j := range slice { 104 | slice[j] = new(uint64) 105 | } 106 | } 107 | return s 108 | } 109 | 110 | func (p *Metrics) add(t metricType, hash, delta uint64) { 111 | if p == nil { 112 | return 113 | } 114 | valp := p.all[t] 115 | // Avoid false sharing by padding at least 64 bytes of space between two 116 | // atomic counters which would be incremented. 117 | idx := (hash % 25) * 10 118 | atomic.AddUint64(valp[idx], delta) 119 | } 120 | 121 | func (p *Metrics) get(t metricType) uint64 { 122 | if p == nil { 123 | return 0 124 | } 125 | valp := p.all[t] 126 | var total uint64 127 | for i := range valp { 128 | total += atomic.LoadUint64(valp[i]) 129 | } 130 | return total 131 | } 132 | 133 | // Hits is the number of Get calls where a value was found for the corresponding key. 134 | func (p *Metrics) Hits() uint64 { 135 | return p.get(hit) 136 | } 137 | 138 | // Misses is the number of Get calls where a value was not found for the corresponding key. 139 | func (p *Metrics) Misses() uint64 { 140 | return p.get(miss) 141 | } 142 | 143 | // KeysAdded is the total number of Set calls where a new key-value item was added. 144 | func (p *Metrics) KeysAdded() uint64 { 145 | return p.get(keyAdd) 146 | } 147 | 148 | // KeysUpdated is the total number of Set calls where the value was updated. 149 | func (p *Metrics) KeysUpdated() uint64 { 150 | return p.get(keyUpdate) 151 | } 152 | 153 | // KeysEvicted is the total number of keys evicted. 154 | func (p *Metrics) KeysEvicted() uint64 { 155 | return p.get(keyEvict) 156 | } 157 | 158 | // CostAdded is the sum of costs that have been added (successful Set calls). 159 | func (p *Metrics) CostAdded() uint64 { 160 | return p.get(costAdd) 161 | } 162 | 163 | // CostEvicted is the sum of all costs that have been evicted. 164 | func (p *Metrics) CostEvicted() uint64 { 165 | return p.get(costEvict) 166 | } 167 | 168 | // SetsDropped is the number of Set calls that don't make it into internal 169 | // buffers (due to contention or some other reason). 170 | func (p *Metrics) SetsDropped() uint64 { 171 | return p.get(dropSets) 172 | } 173 | 174 | // SetsRejected is the number of Set calls rejected by the policy (TinyLFU). 175 | func (p *Metrics) SetsRejected() uint64 { 176 | return p.get(rejectSets) 177 | } 178 | 179 | // GetsDropped is the number of Get counter increments that are dropped 180 | // internally. 181 | func (p *Metrics) GetsDropped() uint64 { 182 | return p.get(dropGets) 183 | } 184 | 185 | // GetsKept is the number of Get counter increments that are kept. 186 | func (p *Metrics) GetsKept() uint64 { 187 | return p.get(keepGets) 188 | } 189 | 190 | // Ratio is the number of Hits over all accesses (Hits + Misses). This is the 191 | // percentage of successful Get calls. 192 | func (p *Metrics) Ratio() float64 { 193 | if p == nil { 194 | return 0.0 195 | } 196 | hits, misses := p.get(hit), p.get(miss) 197 | if hits == 0 && misses == 0 { 198 | return 0.0 199 | } 200 | return float64(hits) / float64(hits+misses) 201 | } 202 | 203 | func (p *Metrics) trackEviction(numSeconds int64) { 204 | if p == nil { 205 | return 206 | } 207 | p.mu.Lock() 208 | defer p.mu.Unlock() 209 | p.life.Update(numSeconds) 210 | } 211 | 212 | func (p *Metrics) LifeExpectancySeconds() *z.HistogramData { 213 | if p == nil { 214 | return nil 215 | } 216 | p.mu.RLock() 217 | defer p.mu.RUnlock() 218 | return p.life.Copy() 219 | } 220 | 221 | // Clear resets all the metrics. 222 | func (p *Metrics) Clear() { 223 | if p == nil { 224 | return 225 | } 226 | for i := 0; i < doNotUse; i++ { 227 | for j := range p.all[i] { 228 | atomic.StoreUint64(p.all[i][j], 0) 229 | } 230 | } 231 | p.mu.Lock() 232 | p.life = z.NewHistogramData(z.HistogramBounds(1, 16)) 233 | p.mu.Unlock() 234 | } 235 | 236 | // String returns a string representation of the metrics. 237 | func (p *Metrics) String() string { 238 | if p == nil { 239 | return "" 240 | } 241 | var buf bytes.Buffer 242 | for i := 0; i < doNotUse; i++ { 243 | t := metricType(i) 244 | fmt.Fprintf(&buf, "%s: %d ", stringFor(t), p.get(t)) 245 | } 246 | fmt.Fprintf(&buf, "gets-total: %d ", p.get(hit)+p.get(miss)) 247 | fmt.Fprintf(&buf, "hit-ratio: %.2f", p.Ratio()) 248 | return buf.String() 249 | } 250 | -------------------------------------------------------------------------------- /z/bbloom.go: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // Copyright (c) 2014 Andreas Briese, eduToolbox@Bri-C GmbH, Sarstedt 3 | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | // this software and associated documentation files (the "Software"), to deal in 6 | // the Software without restriction, including without limitation the rights to 7 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | // the Software, and to permit persons to whom the Software is furnished to do so, 9 | // subject to the following conditions: 10 | 11 | // The above copyright notice and this permission notice shall be included in all 12 | // copies or substantial portions of the Software. 13 | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | 21 | package z 22 | 23 | import ( 24 | "bytes" 25 | "encoding/json" 26 | "math" 27 | "unsafe" 28 | ) 29 | 30 | // helper 31 | var mask = []uint8{1, 2, 4, 8, 16, 32, 64, 128} 32 | 33 | func getSize(ui64 uint64) (size uint64, exponent uint64) { 34 | if ui64 < uint64(512) { 35 | ui64 = uint64(512) 36 | } 37 | size = uint64(1) 38 | for size < ui64 { 39 | size <<= 1 40 | exponent++ 41 | } 42 | return size, exponent 43 | } 44 | 45 | func calcSizeByWrongPositives(numEntries, wrongs float64) (uint64, uint64) { 46 | size := -1 * numEntries * math.Log(wrongs) / math.Pow(float64(0.69314718056), 2) 47 | locs := math.Ceil(float64(0.69314718056) * size / numEntries) 48 | return uint64(size), uint64(locs) 49 | } 50 | 51 | // NewBloomFilter returns a new bloomfilter. 52 | func NewBloomFilter(params ...float64) (bloomfilter *Bloom) { 53 | var entries, locs uint64 54 | if len(params) == 2 { 55 | if params[1] < 1 { 56 | entries, locs = calcSizeByWrongPositives(params[0], params[1]) 57 | } else { 58 | entries, locs = uint64(params[0]), uint64(params[1]) 59 | } 60 | } else { 61 | fatal("usage: New(float64(number_of_entries), float64(number_of_hashlocations))" + 62 | " i.e. New(float64(1000), float64(3)) or New(float64(number_of_entries)," + 63 | " float64(number_of_hashlocations)) i.e. New(float64(1000), float64(0.03))") 64 | } 65 | size, exponent := getSize(entries) 66 | bloomfilter = &Bloom{ 67 | sizeExp: exponent, 68 | size: size - 1, 69 | setLocs: locs, 70 | shift: 64 - exponent, 71 | } 72 | bloomfilter.Size(size) 73 | return bloomfilter 74 | } 75 | 76 | // Bloom filter 77 | type Bloom struct { 78 | bitset []uint64 79 | ElemNum uint64 80 | sizeExp uint64 81 | size uint64 82 | setLocs uint64 83 | shift uint64 84 | } 85 | 86 | // <--- http://www.cse.yorku.ca/~oz/hash.html 87 | // modified Berkeley DB Hash (32bit) 88 | // hash is casted to l, h = 16bit fragments 89 | // func (bl Bloom) absdbm(b *[]byte) (l, h uint64) { 90 | // hash := uint64(len(*b)) 91 | // for _, c := range *b { 92 | // hash = uint64(c) + (hash << 6) + (hash << bl.sizeExp) - hash 93 | // } 94 | // h = hash >> bl.shift 95 | // l = hash << bl.shift >> bl.shift 96 | // return l, h 97 | // } 98 | 99 | // Add adds hash of a key to the bloomfilter. 100 | func (bl *Bloom) Add(hash uint64) { 101 | h := hash >> bl.shift 102 | l := hash << bl.shift >> bl.shift 103 | for i := uint64(0); i < bl.setLocs; i++ { 104 | bl.Set((h + i*l) & bl.size) 105 | bl.ElemNum++ 106 | } 107 | } 108 | 109 | // Has checks if bit(s) for entry hash is/are set, 110 | // returns true if the hash was added to the Bloom Filter. 111 | func (bl Bloom) Has(hash uint64) bool { 112 | h := hash >> bl.shift 113 | l := hash << bl.shift >> bl.shift 114 | for i := uint64(0); i < bl.setLocs; i++ { 115 | if !bl.IsSet((h + i*l) & bl.size) { 116 | return false 117 | } 118 | } 119 | return true 120 | } 121 | 122 | // AddIfNotHas only Adds hash, if it's not present in the bloomfilter. 123 | // Returns true if hash was added. 124 | // Returns false if hash was already registered in the bloomfilter. 125 | func (bl *Bloom) AddIfNotHas(hash uint64) bool { 126 | if bl.Has(hash) { 127 | return false 128 | } 129 | bl.Add(hash) 130 | return true 131 | } 132 | 133 | // TotalSize returns the total size of the bloom filter. 134 | func (bl *Bloom) TotalSize() int { 135 | // The bl struct has 5 members and each one is 8 byte. The bitset is a 136 | // uint64 byte slice. 137 | return len(bl.bitset)*8 + 5*8 138 | } 139 | 140 | // Size makes Bloom filter with as bitset of size sz. 141 | func (bl *Bloom) Size(sz uint64) { 142 | bl.bitset = make([]uint64, sz>>6) 143 | } 144 | 145 | // Clear resets the Bloom filter. 146 | func (bl *Bloom) Clear() { 147 | for i := range bl.bitset { 148 | bl.bitset[i] = 0 149 | } 150 | } 151 | 152 | // Set sets the bit[idx] of bitset. 153 | func (bl *Bloom) Set(idx uint64) { 154 | ptr := unsafe.Pointer(uintptr(unsafe.Pointer(&bl.bitset[idx>>6])) + uintptr((idx%64)>>3)) 155 | *(*uint8)(ptr) |= mask[idx%8] 156 | } 157 | 158 | // IsSet checks if bit[idx] of bitset is set, returns true/false. 159 | func (bl *Bloom) IsSet(idx uint64) bool { 160 | ptr := unsafe.Pointer(uintptr(unsafe.Pointer(&bl.bitset[idx>>6])) + uintptr((idx%64)>>3)) 161 | r := ((*(*uint8)(ptr)) >> (idx % 8)) & 1 162 | return r == 1 163 | } 164 | 165 | // bloomJSONImExport 166 | // Im/Export structure used by JSONMarshal / JSONUnmarshal 167 | type bloomJSONImExport struct { 168 | FilterSet []byte 169 | SetLocs uint64 170 | } 171 | 172 | // NewWithBoolset takes a []byte slice and number of locs per entry, 173 | // returns the bloomfilter with a bitset populated according to the input []byte. 174 | func newWithBoolset(bs *[]byte, locs uint64) *Bloom { 175 | bloomfilter := NewBloomFilter(float64(len(*bs)<<3), float64(locs)) 176 | for i, b := range *bs { 177 | *(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&bloomfilter.bitset[0])) + uintptr(i))) = b 178 | } 179 | return bloomfilter 180 | } 181 | 182 | // JSONUnmarshal takes JSON-Object (type bloomJSONImExport) as []bytes 183 | // returns bloom32 / bloom64 object. 184 | func JSONUnmarshal(dbData []byte) (*Bloom, error) { 185 | bloomImEx := bloomJSONImExport{} 186 | if err := json.Unmarshal(dbData, &bloomImEx); err != nil { 187 | return nil, err 188 | } 189 | buf := bytes.NewBuffer(bloomImEx.FilterSet) 190 | bs := buf.Bytes() 191 | bf := newWithBoolset(&bs, bloomImEx.SetLocs) 192 | return bf, nil 193 | } 194 | 195 | // JSONMarshal returns JSON-object (type bloomJSONImExport) as []byte. 196 | func (bl Bloom) JSONMarshal() []byte { 197 | bloomImEx := bloomJSONImExport{} 198 | bloomImEx.SetLocs = bl.setLocs 199 | bloomImEx.FilterSet = make([]byte, len(bl.bitset)<<3) 200 | for i := range bloomImEx.FilterSet { 201 | bloomImEx.FilterSet[i] = *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&bl.bitset[0])) + 202 | uintptr(i))) 203 | } 204 | data, err := json.Marshal(bloomImEx) 205 | if err != nil { 206 | fatal("json.Marshal failed: ", err) 207 | } 208 | return data 209 | } 210 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project will adhere to [Semantic Versioning](http://semver.org/spec/v2.0.0.html) starting v1.0.0. 6 | 7 | ## Unreleased 8 | 9 | ## [0.1.0] - 2021-06-03 10 | 11 | [0.1.0]: https://github.com/dgraph-io/ristretto/compare/v0.1.0..v0.0.3 12 | This release contains bug fixes and improvements to Ristretto. It also contains 13 | major updates to the z package. The z package contains types such as Tree (B+ 14 | tree), Buffer, Mmap file, etc. All these types are used in Badger and Dgraph to 15 | improve performance and reduce memory requirements. 16 | 17 | ### Changed 18 | - Make item public. Add a new onReject call for rejected items. (#180) 19 | 20 | ### Added 21 | - Use z.Buffer backing for B+ tree (#268) 22 | - expose GetTTL function (#270) 23 | - docs(README): Ristretto is production-ready. (#267) 24 | - Add IterateKV (#265) 25 | - feat(super-flags): Add GetPath method in superflags (#258) 26 | - add GetDuration to SuperFlag (#248) 27 | - add Has, GetFloat64, and GetInt64 to SuperFlag (#247) 28 | - move SuperFlag to Ristretto (#246) 29 | - add SuperFlagHelp tool to generate flag help text (#251) 30 | - allow empty defaults in SuperFlag (#254) 31 | - add mmaped b+ tree (#207) 32 | - Add API to allow the MaxCost of an existing cache to be updated. (#200) 33 | - Add OnExit handler which can be used for manual memory management (#183) 34 | - Add life expectancy histogram (#182) 35 | - Add mechanism to wait for items to be processed. (#184) 36 | 37 | ### Fixed 38 | - change expiration type from int64 to time.Time (#277) 39 | - fix(buffer): make buffer capacity atleast defaultCapacity (#273) 40 | - Fixes for z.PersistentTree (#272) 41 | - Initialize persistent tree correctly (#271) 42 | - use xxhash v2 (#266) 43 | - update comments to correctly reflect counter space usage (#189) 44 | - enable riscv64 builds (#264) 45 | - Switch from log to glog (#263) 46 | - Use Fibonacci for latency numbers 47 | - cache: fix race when clearning a cache (#261) 48 | - Check for keys without values in superflags (#259) 49 | - chore(perf): using tags instead of runtime callers to improve the performance of leak detection (#255) 50 | - fix(Flags): panic on user errors (#256) 51 | - fix SuperFlagHelp newline (#252) 52 | - fix(arm): Fix crashing under ARMv6 due to memory mis-alignment (#239) 53 | - Fix incorrect unit test coverage depiction (#245) 54 | - chore(histogram): adding percentile in histogram (#241) 55 | - fix(windows): use filepath instead of path (#244) 56 | - fix(MmapFile): Close the fd before deleting the file (#242) 57 | - Fixes CGO_ENABLED=0 compilation error (#240) 58 | - fix(build): fix build on non-amd64 architectures (#238) 59 | - fix(b+tree): Do not double the size of btree (#237) 60 | - fix(jemalloc): Fix the stats of jemalloc (#236) 61 | - Don't print stuff, only return strings. 62 | - Bring memclrNoHeapPointers to z (#235) 63 | - increase number of buffers from 32 to 64 in allocator (#234) 64 | - Set minSize to 1MB. 65 | - Opt(btree): Use Go memory instead of mmap files 66 | - Opt(btree): Lightweight stats calculation 67 | - Put padding internally to z.Buffer 68 | - Chore(z): Add SetTmpDir API to set the temp directory (#233) 69 | - Add a BufferFrom 70 | - Bring z.Allocator and z.AllocatorPool back 71 | - Fix(z.Allocator): Make Allocator use Go memory 72 | - Updated ZeroOut to use a simple for loop. (#231) 73 | - Add concurrency back 74 | - Add a test to check concurrency of Allocator. 75 | - Fix(buffer): Expose padding by z.Buffer's APIs and fix test (#222) 76 | - AllocateSlice should Truncate if the file is not big enough (#226) 77 | - Zero out allocations for structs now that we're reusing Allocators. 78 | - Fix the ristretto substring 79 | - Deal with nil z.AllocatorPool 80 | - Create an AllocatorPool class. 81 | - chore(btree): clean NewTree API (#225) 82 | - fix(MmapFile): Don't error out if fileSize > sz (#224) 83 | - feat(btree): allow option to reset btree and mmaping it to specified file. (#223) 84 | - Use mremap on Linux instead of munmap+mmap (#221) 85 | - Reuse pages in B+ tree (#220) 86 | - fix(allocator): make nil allocator return go byte slice (#217) 87 | - fix(buffer): Make padding internal to z.buffer (#216) 88 | - chore(buffer): add a parent directory field in z.Buffer (#215) 89 | - Make Allocator concurrent 90 | - Fix infinite loop in allocator (#214) 91 | - Add trim func 92 | - Use allocator pool. Turn off freelist. 93 | - Add freelists to Allocator to reuse. 94 | - make DeleteBelow delete values that are less than lo (#211) 95 | - Avoid an unnecessary Load procedure in IncrementOffset. 96 | - Add Stats method in Btree. 97 | - chore(script): fix local test script (#210) 98 | - fix(btree): Increase buffer size if needed. (#209) 99 | - chore(btree): add occupancy ratio, search benchmark and compact bug fix (#208) 100 | - Add licenses, remove prints, and fix a bug in compact 101 | - Add IncrementOffset API for z.buffers (#206) 102 | - Show count when printing histogram (#201) 103 | - Zbuffer: Add LenNoPadding and make padding 8 bytes (#204) 104 | - Allocate Go memory in case allocator is nil. 105 | - Add leak detection via leak build flag and fix a leak during cache.Close. 106 | - Add some APIs for allocator and buffer 107 | - Sync before truncation or close. 108 | - Handle nil MmapFile for Sync. 109 | - Public methods must not panic after Close() (#202) 110 | - Check for RD_ONLY correctly. 111 | - Modify MmapFile APIs 112 | - Add a bunch of APIs around MmapFile 113 | - Move APIs for mmapfile creation over to z package. 114 | - Add ZeroOut func 115 | - Add SliceOffsets 116 | - z: Add TotalSize method on bloom filter (#197) 117 | - Add Msync func 118 | - Buffer: Use 256 GB mmap size instead of MaxInt64 (#198) 119 | - Add a simple test to check next2Pow 120 | - Improve memory performance (#195) 121 | - Have a way to automatically mmap a growing buffer (#196) 122 | - Introduce Mmapped buffers and Merge Sort (#194) 123 | - Add a way to access an allocator via reference. 124 | - Use jemalloc.a to ensure compilation with the Go binary 125 | - Fix up a build issue with ReadMemStats 126 | - Add ReadMemStats function (#193) 127 | - Allocator helps allocate memory to be used by unsafe structs (#192) 128 | - Improve histogram output 129 | - Move Closer from y to z (#191) 130 | - Add histogram.Mean() method (#188) 131 | - Introduce Calloc: Manual Memory Management via jemalloc (#186) 132 | 133 | ## [0.0.3] - 2020-07-06 134 | 135 | [0.0.3]: https://github.com/dgraph-io/ristretto/compare/v0.0.2..v0.0.3 136 | 137 | ### Changed 138 | 139 | ### Added 140 | 141 | ### Fixed 142 | 143 | - z: use MemHashString and xxhash.Sum64String ([#153][]) 144 | - Check conflict key before updating expiration map. ([#154][]) 145 | - Fix race condition in Cache.Clear ([#133][]) 146 | - Improve handling of updated items ([#168][]) 147 | - Fix droppedSets count while updating the item ([#171][]) 148 | 149 | ## [0.0.2] - 2020-02-24 150 | 151 | [0.0.2]: https://github.com/dgraph-io/ristretto/compare/v0.0.1..v0.0.2 152 | 153 | ### Added 154 | 155 | - Sets with TTL. ([#122][]) 156 | 157 | ### Fixed 158 | 159 | - Fix the way metrics are handled for deletions. ([#111][]) 160 | - Support nil `*Cache` values in `Clear` and `Close`. ([#119][]) 161 | - Delete item immediately. ([#113][]) 162 | - Remove key from policy after TTL eviction. ([#130][]) 163 | 164 | [#111]: https://github.com/dgraph-io/ristretto/issues/111 165 | [#113]: https://github.com/dgraph-io/ristretto/issues/113 166 | [#119]: https://github.com/dgraph-io/ristretto/issues/119 167 | [#122]: https://github.com/dgraph-io/ristretto/issues/122 168 | [#130]: https://github.com/dgraph-io/ristretto/issues/130 169 | 170 | ## 0.0.1 171 | 172 | First release. Basic cache functionality based on a LFU policy. 173 | -------------------------------------------------------------------------------- /z/flags.go: -------------------------------------------------------------------------------- 1 | package z 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "os/user" 7 | "path/filepath" 8 | "sort" 9 | "strconv" 10 | "strings" 11 | "time" 12 | 13 | "github.com/pkg/errors" 14 | ) 15 | 16 | // SuperFlagHelp makes it really easy to generate command line `--help` output for a SuperFlag. For 17 | // example: 18 | // 19 | // const flagDefaults = `enabled=true; path=some/path;` 20 | // 21 | // var help string = z.NewSuperFlagHelp(flagDefaults). 22 | // Flag("enabled", "Turns on ."). 23 | // Flag("path", "The path to ."). 24 | // Flag("another", "Not present in defaults, but still included."). 25 | // String() 26 | // 27 | // The `help` string would then contain: 28 | // 29 | // enabled=true; Turns on . 30 | // path=some/path; The path to . 31 | // another=; Not present in defaults, but still included. 32 | // 33 | // All flags are sorted alphabetically for consistent `--help` output. Flags with default values are 34 | // placed at the top, and everything else goes under. 35 | type SuperFlagHelp struct { 36 | head string 37 | defaults *SuperFlag 38 | flags map[string]string 39 | } 40 | 41 | func NewSuperFlagHelp(defaults string) *SuperFlagHelp { 42 | return &SuperFlagHelp{ 43 | defaults: NewSuperFlag(defaults), 44 | flags: make(map[string]string, 0), 45 | } 46 | } 47 | 48 | func (h *SuperFlagHelp) Head(head string) *SuperFlagHelp { 49 | h.head = head 50 | return h 51 | } 52 | 53 | func (h *SuperFlagHelp) Flag(name, description string) *SuperFlagHelp { 54 | h.flags[name] = description 55 | return h 56 | } 57 | 58 | func (h *SuperFlagHelp) String() string { 59 | defaultLines := make([]string, 0) 60 | otherLines := make([]string, 0) 61 | for name, help := range h.flags { 62 | val, found := h.defaults.m[name] 63 | line := fmt.Sprintf(" %s=%s; %s\n", name, val, help) 64 | if found { 65 | defaultLines = append(defaultLines, line) 66 | } else { 67 | otherLines = append(otherLines, line) 68 | } 69 | } 70 | sort.Strings(defaultLines) 71 | sort.Strings(otherLines) 72 | dls := strings.Join(defaultLines, "") 73 | ols := strings.Join(otherLines, "") 74 | if len(h.defaults.m) == 0 && len(ols) == 0 { 75 | // remove last newline 76 | dls = dls[:len(dls)-1] 77 | } 78 | // remove last newline 79 | if len(h.defaults.m) == 0 && len(ols) > 1 { 80 | ols = ols[:len(ols)-1] 81 | } 82 | return h.head + "\n" + dls + ols 83 | } 84 | 85 | func parseFlag(flag string) (map[string]string, error) { 86 | kvm := make(map[string]string) 87 | for _, kv := range strings.Split(flag, ";") { 88 | if strings.TrimSpace(kv) == "" { 89 | continue 90 | } 91 | // For a non-empty separator, 0 < len(splits) ≤ 2. 92 | splits := strings.SplitN(kv, "=", 2) 93 | k := strings.TrimSpace(splits[0]) 94 | if len(splits) < 2 { 95 | return nil, fmt.Errorf("superflag: missing value for '%s' in flag: %s", k, flag) 96 | } 97 | k = strings.ToLower(k) 98 | k = strings.ReplaceAll(k, "_", "-") 99 | kvm[k] = strings.TrimSpace(splits[1]) 100 | } 101 | return kvm, nil 102 | } 103 | 104 | type SuperFlag struct { 105 | m map[string]string 106 | } 107 | 108 | func NewSuperFlag(flag string) *SuperFlag { 109 | sf, err := newSuperFlagImpl(flag) 110 | if err != nil { 111 | fatal(err) 112 | } 113 | return sf 114 | } 115 | 116 | func newSuperFlagImpl(flag string) (*SuperFlag, error) { 117 | m, err := parseFlag(flag) 118 | if err != nil { 119 | return nil, err 120 | } 121 | return &SuperFlag{m}, nil 122 | } 123 | 124 | func (sf *SuperFlag) String() string { 125 | if sf == nil { 126 | return "" 127 | } 128 | kvs := make([]string, 0, len(sf.m)) 129 | for k, v := range sf.m { 130 | kvs = append(kvs, fmt.Sprintf("%s=%s", k, v)) 131 | } 132 | return strings.Join(kvs, "; ") 133 | } 134 | 135 | func (sf *SuperFlag) MergeAndCheckDefault(flag string) *SuperFlag { 136 | sf, err := sf.MergeWithDefault(flag) 137 | if err != nil { 138 | fatal(err) 139 | } 140 | return sf 141 | } 142 | 143 | func (sf *SuperFlag) Merge(flag string) *SuperFlag { 144 | src, err := parseFlag(flag) 145 | if err != nil { 146 | fatal(err) 147 | } 148 | for k, v := range src { 149 | if _, ok := sf.m[k]; !ok { 150 | fatal("Unable to find the flag in SuperFlag") 151 | } 152 | sf.m[k] = v 153 | } 154 | return sf 155 | } 156 | 157 | func (sf *SuperFlag) MergeWithDefault(flag string) (*SuperFlag, error) { 158 | if sf == nil { 159 | m, err := parseFlag(flag) 160 | if err != nil { 161 | return nil, err 162 | } 163 | return &SuperFlag{m}, nil 164 | } 165 | 166 | src, err := parseFlag(flag) 167 | if err != nil { 168 | return nil, err 169 | } 170 | 171 | numKeys := len(sf.m) 172 | for k := range src { 173 | if _, ok := sf.m[k]; ok { 174 | numKeys-- 175 | } 176 | } 177 | if numKeys != 0 { 178 | return nil, fmt.Errorf("superflag: found invalid options in flag: %s.\nvalid options: %v", sf, flag) 179 | } 180 | for k, v := range src { 181 | if _, ok := sf.m[k]; !ok { 182 | sf.m[k] = v 183 | } 184 | } 185 | return sf, nil 186 | } 187 | 188 | func (sf *SuperFlag) Has(opt string) bool { 189 | val := sf.GetString(opt) 190 | return val != "" 191 | } 192 | 193 | func (sf *SuperFlag) GetDuration(opt string) time.Duration { 194 | val := sf.GetString(opt) 195 | if val == "" { 196 | return time.Duration(0) 197 | } 198 | if strings.Contains(val, "d") { 199 | val = strings.Replace(val, "d", "", 1) 200 | days, err := strconv.ParseUint(val, 0, 64) 201 | if err != nil { 202 | return time.Duration(0) 203 | } 204 | return time.Hour * 24 * time.Duration(days) 205 | } 206 | d, err := time.ParseDuration(val) 207 | if err != nil { 208 | return time.Duration(0) 209 | } 210 | return d 211 | } 212 | 213 | func (sf *SuperFlag) GetBool(opt string) bool { 214 | val := sf.GetString(opt) 215 | if val == "" { 216 | return false 217 | } 218 | b, err := strconv.ParseBool(val) 219 | if err != nil { 220 | err = errors.Wrapf(err, 221 | "Unable to parse %s as bool for key: %s. Options: %s\n", 222 | val, opt, sf) 223 | fatalf("%+v", err) 224 | } 225 | return b 226 | } 227 | 228 | func (sf *SuperFlag) GetFloat64(opt string) float64 { 229 | val := sf.GetString(opt) 230 | if val == "" { 231 | return 0 232 | } 233 | f, err := strconv.ParseFloat(val, 64) 234 | if err != nil { 235 | err = errors.Wrapf(err, 236 | "Unable to parse %s as float64 for key: %s. Options: %s\n", 237 | val, opt, sf) 238 | fatalf("%+v", err) 239 | } 240 | return f 241 | } 242 | 243 | func (sf *SuperFlag) GetInt64(opt string) int64 { 244 | val := sf.GetString(opt) 245 | if val == "" { 246 | return 0 247 | } 248 | i, err := strconv.ParseInt(val, 0, 64) 249 | if err != nil { 250 | err = errors.Wrapf(err, 251 | "Unable to parse %s as int64 for key: %s. Options: %s\n", 252 | val, opt, sf) 253 | fatalf("%+v", err) 254 | } 255 | return i 256 | } 257 | 258 | func (sf *SuperFlag) GetUint64(opt string) uint64 { 259 | val := sf.GetString(opt) 260 | if val == "" { 261 | return 0 262 | } 263 | u, err := strconv.ParseUint(val, 0, 64) 264 | if err != nil { 265 | err = errors.Wrapf(err, 266 | "Unable to parse %s as uint64 for key: %s. Options: %s\n", 267 | val, opt, sf) 268 | fatalf("%+v", err) 269 | } 270 | return u 271 | } 272 | 273 | func (sf *SuperFlag) GetUint32(opt string) uint32 { 274 | val := sf.GetString(opt) 275 | if val == "" { 276 | return 0 277 | } 278 | u, err := strconv.ParseUint(val, 0, 32) 279 | if err != nil { 280 | err = errors.Wrapf(err, 281 | "Unable to parse %s as uint32 for key: %s. Options: %s\n", 282 | val, opt, sf) 283 | fatalf("%+v", err) 284 | } 285 | return uint32(u) 286 | } 287 | 288 | func (sf *SuperFlag) GetString(opt string) string { 289 | if sf == nil { 290 | return "" 291 | } 292 | return sf.m[opt] 293 | } 294 | 295 | func (sf *SuperFlag) GetPath(opt string) string { 296 | p := sf.GetString(opt) 297 | path, err := expandPath(p) 298 | if err != nil { 299 | fatalf("Failed to get path: %+v", err) 300 | } 301 | return path 302 | } 303 | 304 | // expandPath expands the paths containing ~ to /home/user. It also computes the absolute path 305 | // from the relative paths. For example: ~/abc/../cef will be transformed to /home/user/cef. 306 | func expandPath(path string) (string, error) { 307 | if len(path) == 0 { 308 | return "", nil 309 | } 310 | if path[0] == '~' && (len(path) == 1 || os.IsPathSeparator(path[1])) { 311 | usr, err := user.Current() 312 | if err != nil { 313 | return "", errors.Wrap(err, "Failed to get the home directory of the user") 314 | } 315 | path = filepath.Join(usr.HomeDir, path[1:]) 316 | } 317 | 318 | var err error 319 | path, err = filepath.Abs(path) 320 | if err != nil { 321 | return "", errors.Wrap(err, "Failed to generate absolute path") 322 | } 323 | return path, nil 324 | } 325 | -------------------------------------------------------------------------------- /z/buffer_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "bytes" 21 | "encoding/binary" 22 | "encoding/hex" 23 | "fmt" 24 | "math/rand" 25 | "sort" 26 | "testing" 27 | "time" 28 | 29 | "github.com/stretchr/testify/require" 30 | ) 31 | 32 | func TestBuffer(t *testing.T) { 33 | rand.Seed(time.Now().Unix()) 34 | const capacity = 512 35 | buffers := newTestBuffers(t, capacity) 36 | 37 | for _, buf := range buffers { 38 | name := fmt.Sprintf("Using buffer type: %s", buf.bufType) 39 | t.Run(name, func(t *testing.T) { 40 | // This is just for verifying result 41 | var bytesBuf bytes.Buffer 42 | bytesBuf.Grow(capacity) 43 | 44 | // Writer small []byte 45 | var smallData [256]byte 46 | rand.Read(smallData[:]) 47 | var bigData [1024]byte 48 | rand.Read(bigData[:]) 49 | 50 | _, err := buf.Write(smallData[:]) 51 | require.NoError(t, err, "unable to write data to page buffer") 52 | _, err = buf.Write(bigData[:]) 53 | require.NoError(t, err, "unable to write data to page buffer") 54 | 55 | // Write data to bytesBuffer also, just to match result. 56 | bytesBuf.Write(smallData[:]) 57 | bytesBuf.Write(bigData[:]) 58 | require.Equal(t, buf.Bytes(), bytesBuf.Bytes()) 59 | }) 60 | } 61 | } 62 | 63 | func TestBufferWrite(t *testing.T) { 64 | rand.Seed(time.Now().Unix()) 65 | const capacity = 32 66 | buffers := newTestBuffers(t, capacity) 67 | 68 | for _, buf := range buffers { 69 | name := fmt.Sprintf("Using buffer type: %s", buf.bufType) 70 | t.Run(name, func(t *testing.T) { 71 | var data [128]byte 72 | rand.Read(data[:]) 73 | bytesBuf := new(bytes.Buffer) 74 | 75 | end := 32 76 | for i := 0; i < 3; i++ { 77 | n, err := buf.Write(data[:end]) 78 | require.NoError(t, err, "unable to write bytes to buffer") 79 | require.Equal(t, n, end, "length of buffer and length written should be equal") 80 | 81 | // append to bb also for testing. 82 | bytesBuf.Write(data[:end]) 83 | 84 | require.Equal(t, buf.Bytes(), bytesBuf.Bytes()) 85 | end = end * 2 86 | } 87 | 88 | }) 89 | } 90 | } 91 | 92 | func TestBufferAutoMmap(t *testing.T) { 93 | buf := NewBuffer(1<<20, "test").WithAutoMmap(64<<20, "") 94 | defer func() { require.NoError(t, buf.Release()) }() 95 | 96 | N := 128 << 10 97 | var wb [1024]byte 98 | for i := 0; i < N; i++ { 99 | rand.Read(wb[:]) 100 | b := buf.SliceAllocate(len(wb)) 101 | copy(b, wb[:]) 102 | } 103 | t.Logf("Buffer size: %d\n", buf.LenWithPadding()) 104 | 105 | buf.SortSlice(func(l, r []byte) bool { 106 | return bytes.Compare(l, r) < 0 107 | }) 108 | t.Logf("sort done\n") 109 | 110 | var count int 111 | var last []byte 112 | buf.SliceIterate(func(slice []byte) error { 113 | require.True(t, bytes.Compare(slice, last) >= 0) 114 | last = append(last[:0], slice...) 115 | count++ 116 | return nil 117 | }) 118 | require.Equal(t, N, count) 119 | } 120 | 121 | func TestBufferSimpleSort(t *testing.T) { 122 | bufs := newTestBuffers(t, 1<<20) 123 | for _, buf := range bufs { 124 | name := fmt.Sprintf("Using buffer type: %s", buf.bufType) 125 | t.Run(name, func(t *testing.T) { 126 | for i := 0; i < 25600; i++ { 127 | b := buf.SliceAllocate(4) 128 | binary.BigEndian.PutUint32(b, uint32(rand.Int31n(256000))) 129 | } 130 | buf.SortSlice(func(ls, rs []byte) bool { 131 | left := binary.BigEndian.Uint32(ls) 132 | right := binary.BigEndian.Uint32(rs) 133 | return left < right 134 | }) 135 | var last uint32 136 | var i int 137 | buf.SliceIterate(func(slice []byte) error { 138 | num := binary.BigEndian.Uint32(slice) 139 | if num < last { 140 | fmt.Printf("num: %d idx: %d last: %d\n", num, i, last) 141 | } 142 | i++ 143 | require.GreaterOrEqual(t, num, last) 144 | last = num 145 | // fmt.Printf("Got number: %d\n", num) 146 | return nil 147 | }) 148 | }) 149 | } 150 | } 151 | 152 | func TestBufferSlice(t *testing.T) { 153 | const capacity = 32 154 | buffers := newTestBuffers(t, capacity) 155 | 156 | for _, buf := range buffers { 157 | name := fmt.Sprintf("Using buffer type: %s", buf.bufType) 158 | t.Run(name, func(t *testing.T) { 159 | count := 10000 160 | exp := make([][]byte, 0, count) 161 | 162 | // Create "count" number of slices. 163 | for i := 0; i < count; i++ { 164 | sz := 1 + rand.Intn(8) 165 | testBuf := make([]byte, sz) 166 | rand.Read(testBuf) 167 | 168 | newSlice := buf.SliceAllocate(sz) 169 | require.Equal(t, sz, copy(newSlice, testBuf)) 170 | 171 | // Save testBuf for verification. 172 | exp = append(exp, testBuf) 173 | } 174 | 175 | compare := func() { 176 | i := 0 177 | buf.SliceIterate(func(slice []byte) error { 178 | // All the slices returned by the buffer should be equal to what we 179 | // inserted earlier. 180 | if !bytes.Equal(exp[i], slice) { 181 | fmt.Printf("exp: %s got: %s\n", hex.Dump(exp[i]), hex.Dump(slice)) 182 | t.Fail() 183 | } 184 | require.Equal(t, exp[i], slice) 185 | i++ 186 | return nil 187 | }) 188 | require.Equal(t, len(exp), i) 189 | } 190 | compare() // same order as inserted. 191 | 192 | t.Logf("Sorting using sort.Slice\n") 193 | sort.Slice(exp, func(i, j int) bool { 194 | return bytes.Compare(exp[i], exp[j]) < 0 195 | }) 196 | t.Logf("Sorting using buf.SortSlice\n") 197 | buf.SortSlice(func(a, b []byte) bool { 198 | return bytes.Compare(a, b) < 0 199 | }) 200 | t.Logf("Done sorting\n") 201 | compare() // same order after sort. 202 | }) 203 | } 204 | } 205 | 206 | func TestBufferSort(t *testing.T) { 207 | const capacity = 32 208 | bufs := newTestBuffers(t, capacity) 209 | 210 | for _, buf := range bufs { 211 | name := fmt.Sprintf("Using buffer type: %s", buf.bufType) 212 | t.Run(name, func(t *testing.T) { 213 | const N = 10000 214 | 215 | for i := 0; i < N; i++ { 216 | newSlice := buf.SliceAllocate(8) 217 | uid := uint64(rand.Int63()) 218 | binary.BigEndian.PutUint64(newSlice, uid) 219 | } 220 | 221 | test := func(start, end int) { 222 | start = buf.StartOffset() + 12*start 223 | end = buf.StartOffset() + 12*end 224 | buf.SortSliceBetween(start, end, func(ls, rs []byte) bool { 225 | lhs := binary.BigEndian.Uint64(ls) 226 | rhs := binary.BigEndian.Uint64(rs) 227 | return lhs < rhs 228 | }) 229 | 230 | slice, next := []byte{}, start 231 | var last uint64 232 | var count int 233 | for next >= 0 && next < end { 234 | slice, next = buf.Slice(next) 235 | uid := binary.BigEndian.Uint64(slice) 236 | require.GreaterOrEqual(t, uid, last) 237 | last = uid 238 | count++ 239 | } 240 | require.Equal(t, (end-start)/12, count) 241 | } 242 | for i := 10; i <= N; i += 10 { 243 | test(i-10, i) 244 | } 245 | test(0, N) 246 | }) 247 | } 248 | } 249 | 250 | // Test that the APIs returns the expected offsets. 251 | func TestBufferPadding(t *testing.T) { 252 | bufs := newTestBuffers(t, 1<<10) 253 | for _, buf := range bufs { 254 | name := fmt.Sprintf("Using buffer type: %s", buf.bufType) 255 | t.Run(name, func(t *testing.T) { 256 | sz := rand.Int31n(100) 257 | 258 | writeOffset := buf.AllocateOffset(int(sz)) 259 | require.Equal(t, buf.StartOffset(), writeOffset) 260 | 261 | b := make([]byte, sz) 262 | rand.Read(b) 263 | 264 | copy(buf.Bytes(), b) 265 | data := buf.Data(buf.StartOffset()) 266 | require.Equal(t, b, data[:sz]) 267 | }) 268 | } 269 | } 270 | 271 | func newTestBuffers(t *testing.T, capacity int) []*Buffer { 272 | var bufs []*Buffer 273 | 274 | buf := NewBuffer(capacity, "test") 275 | bufs = append(bufs, buf) 276 | 277 | buf, err := NewBufferTmp("", capacity) 278 | require.NoError(t, err) 279 | bufs = append(bufs, buf) 280 | 281 | t.Cleanup(func() { 282 | for _, buf := range bufs { 283 | require.NoError(t, buf.Release()) 284 | } 285 | }) 286 | 287 | return bufs 288 | } 289 | 290 | func TestSmallBuffer(t *testing.T) { 291 | buf := NewBuffer(5, "test") 292 | t.Cleanup(func() { 293 | require.NoError(t, buf.Release()) 294 | }) 295 | // Write something to buffer so sort actually happens. 296 | buf.WriteSlice([]byte("abc")) 297 | // This test fails if the buffer has offset > currSz. 298 | require.NotPanics(t, func() { 299 | buf.SortSlice(func(left, right []byte) bool { 300 | return true 301 | }) 302 | }) 303 | } 304 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ristretto 2 | [![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/outcaste-io/ristretto) 3 | [![Go Report Card](https://img.shields.io/badge/go%20report-A%2B-brightgreen)](https://goreportcard.com/report/github.com/outcaste-io/ristretto) 4 | [![Coverage](https://gocover.io/_badge/github.com/outcaste-io/ristretto)](https://gocover.io/github.com/outcaste-io/ristretto) 5 | ![Tests](https://github.com/outcaste-io/ristretto/workflows/tests/badge.svg) 6 | 7 | **This is a fork of dgraph-io/ristretto, maintained by @manishrjain.** 8 | 9 | Ristretto is a fast, concurrent cache library built with a focus on performance and correctness. 10 | 11 | The motivation to build Ristretto comes from the need for a contention-free 12 | cache. 13 | 14 | [issues]: https://github.com/outcaste-io/issues 15 | 16 | ## Features 17 | 18 | * **High Hit Ratios** - with our unique admission/eviction policy pairing, Ristretto's performance is best in class. 19 | * **Eviction: SampledLFU** - on par with exact LRU and better performance on Search and Database traces. 20 | * **Admission: TinyLFU** - extra performance with little memory overhead (12 bits per counter). 21 | * **Fast Throughput** - we use a variety of techniques for managing contention and the result is excellent throughput. 22 | * **Cost-Based Eviction** - any large new item deemed valuable can evict multiple smaller items (cost could be anything). 23 | * **Fully Concurrent** - you can use as many goroutines as you want with little throughput degradation. 24 | * **Metrics** - optional performance metrics for throughput, hit ratios, and other stats. 25 | * **Simple API** - just figure out your ideal `Config` values and you're off and running. 26 | 27 | ## Note on jemalloc 28 | 29 | We have been using jemalloc v5.2.1. 30 | To use jemalloc, please configure jemalloc with these flags: 31 | 32 | ``` 33 | ./configure --with-install-suffix='_outcaste' --with-jemalloc-prefix='je_' --with-malloc-conf='background_thread:true,metadata_thp:auto'; \ 34 | make 35 | make install_lib install_include # Use sudo if needed in this step. 36 | ``` 37 | 38 | outserv/outserv Makefile has these build steps already present. You can run 39 | `make jemalloc` to install it. This jemalloc would not interfere with any other 40 | jemalloc installation that might already be present on the system. 41 | 42 | 43 | ## Status 44 | 45 | Ristretto is production-ready. See [Projects using Ristretto](#projects-using-ristretto). 46 | 47 | ## Table of Contents 48 | 49 | * [Usage](#Usage) 50 | * [Example](#Example) 51 | * [Config](#Config) 52 | * [NumCounters](#Config) 53 | * [MaxCost](#Config) 54 | * [BufferItems](#Config) 55 | * [Metrics](#Config) 56 | * [OnEvict](#Config) 57 | * [KeyToHash](#Config) 58 | * [Cost](#Config) 59 | * [Benchmarks](#Benchmarks) 60 | * [Hit Ratios](#Hit-Ratios) 61 | * [Search](#Search) 62 | * [Database](#Database) 63 | * [Looping](#Looping) 64 | * [CODASYL](#CODASYL) 65 | * [Throughput](#Throughput) 66 | * [Mixed](#Mixed) 67 | * [Read](#Read) 68 | * [Write](#Write) 69 | * [Projects using Ristretto](#projects-using-ristretto) 70 | * [FAQ](#FAQ) 71 | 72 | ## Usage 73 | 74 | ### Example 75 | 76 | ```go 77 | func main() { 78 | cache, err := ristretto.NewCache(&ristretto.Config{ 79 | NumCounters: 1e7, // number of keys to track frequency of (10M). 80 | MaxCost: 1 << 30, // maximum cost of cache (1GB). 81 | BufferItems: 64, // number of keys per Get buffer. 82 | }) 83 | if err != nil { 84 | panic(err) 85 | } 86 | 87 | // set a value with a cost of 1 88 | cache.Set("key", "value", 1) 89 | 90 | // wait for value to pass through buffers 91 | cache.Wait() 92 | 93 | value, found := cache.Get("key") 94 | if !found { 95 | panic("missing value") 96 | } 97 | fmt.Println(value) 98 | cache.Del("key") 99 | } 100 | ``` 101 | 102 | ### Config 103 | 104 | The `Config` struct is passed to `NewCache` when creating Ristretto instances (see the example above). 105 | 106 | **NumCounters** `int64` 107 | 108 | NumCounters is the number of 4-bit access counters to keep for admission and eviction. We've seen good performance in setting this to 10x the number of items you expect to keep in the cache when full. 109 | 110 | For example, if you expect each item to have a cost of 1 and MaxCost is 100, set NumCounters to 1,000. Or, if you use variable cost values but expect the cache to hold around 10,000 items when full, set NumCounters to 100,000. The important thing is the *number of unique items* in the full cache, not necessarily the MaxCost value. 111 | 112 | **MaxCost** `int64` 113 | 114 | MaxCost is how eviction decisions are made. For example, if MaxCost is 100 and a new item with a cost of 1 increases total cache cost to 101, 1 item will be evicted. 115 | 116 | MaxCost can also be used to denote the max size in bytes. For example, if MaxCost is 1,000,000 (1MB) and the cache is full with 1,000 1KB items, a new item (that's accepted) would cause 5 1KB items to be evicted. 117 | 118 | MaxCost could be anything as long as it matches how you're using the cost values when calling Set. 119 | 120 | **BufferItems** `int64` 121 | 122 | BufferItems is the size of the Get buffers. The best value we've found for this is 64. 123 | 124 | If for some reason you see Get performance decreasing with lots of contention (you shouldn't), try increasing this value in increments of 64. This is a fine-tuning mechanism and you probably won't have to touch this. 125 | 126 | **Metrics** `bool` 127 | 128 | Metrics is true when you want real-time logging of a variety of stats. The reason this is a Config flag is because there's a 10% throughput performance overhead. 129 | 130 | **OnEvict** `func(hashes [2]uint64, value interface{}, cost int64)` 131 | 132 | OnEvict is called for every eviction. 133 | 134 | **KeyToHash** `func(key interface{}) [2]uint64` 135 | 136 | KeyToHash is the hashing algorithm used for every key. If this is nil, Ristretto has a variety of [defaults depending on the underlying interface type](https://github.com/outcaste-io/ristretto/blob/master/z/z.go#L19-L41). 137 | 138 | Note that if you want 128bit hashes you should use the full `[2]uint64`, 139 | otherwise just fill the `uint64` at the `0` position and it will behave like 140 | any 64bit hash. 141 | 142 | **Cost** `func(value interface{}) int64` 143 | 144 | Cost is an optional function you can pass to the Config in order to evaluate 145 | item cost at runtime, and only for the Set calls that aren't dropped (this is 146 | useful if calculating item cost is particularly expensive and you don't want to 147 | waste time on items that will be dropped anyways). 148 | 149 | To signal to Ristretto that you'd like to use this Cost function: 150 | 151 | 1. Set the Cost field to a non-nil function. 152 | 2. When calling Set for new items or item updates, use a `cost` of 0. 153 | 154 | ## Benchmarks 155 | 156 | The benchmarks can be found in https://github.com/dgraph-io/benchmarks/tree/master/cachebench/ristretto. 157 | 158 | ### Hit Ratios 159 | 160 | #### Search 161 | 162 | This trace is described as "disk read accesses initiated by a large commercial 163 | search engine in response to various web search requests." 164 | 165 |

166 | 167 |

168 | 169 | #### Database 170 | 171 | This trace is described as "a database server running at a commercial site 172 | running an ERP application on top of a commercial database." 173 | 174 |

175 | 176 |

177 | 178 | #### Looping 179 | 180 | This trace demonstrates a looping access pattern. 181 | 182 |

183 | 184 |

185 | 186 | #### CODASYL 187 | 188 | This trace is described as "references to a CODASYL database for a one hour 189 | period." 190 | 191 |

192 | 193 |

194 | 195 | ### Throughput 196 | 197 | All throughput benchmarks were ran on an Intel Core i7-8700K (3.7GHz) with 16gb 198 | of RAM. 199 | 200 | #### Mixed 201 | 202 |

203 | 204 |

205 | 206 | #### Read 207 | 208 |

209 | 210 |

211 | 212 | #### Write 213 | 214 |

215 | 216 |

217 | 218 | ## Projects Using Ristretto 219 | 220 | Below is a list of known projects that use Ristretto: 221 | 222 | - [Badger](https://github.com/dgraph-io/badger) - Embeddable key-value DB in Go 223 | - [Dgraph](https://github.com/dgraph-io/dgraph) - Horizontally scalable and distributed GraphQL database with a graph backend 224 | - [Vitess](https://github.com/vitessio/vitess) - Database clustering system for horizontal scaling of MySQL 225 | - [SpiceDB](https://github.com/authzed/spicedb) - Horizontally scalable permissions database 226 | 227 | ## FAQ 228 | 229 | ### How are you achieving this performance? What shortcuts are you taking? 230 | 231 | We go into detail in the [Ristretto blog post](https://blog.dgraph.io/post/introducing-ristretto-high-perf-go-cache/), but in short: our throughput performance can be attributed to a mix of batching and eventual consistency. Our hit ratio performance is mostly due to an excellent [admission policy](https://arxiv.org/abs/1512.00727) and SampledLFU eviction policy. 232 | 233 | As for "shortcuts," the only thing Ristretto does that could be construed as one is dropping some Set calls. That means a Set call for a new item (updates are guaranteed) isn't guaranteed to make it into the cache. The new item could be dropped at two points: when passing through the Set buffer or when passing through the admission policy. However, this doesn't affect hit ratios much at all as we expect the most popular items to be Set multiple times and eventually make it in the cache. 234 | 235 | ### Is Ristretto distributed? 236 | 237 | No, it's just like any other Go library that you can import into your project and use in a single process. 238 | -------------------------------------------------------------------------------- /z/allocator.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package z 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "math" 23 | "math/bits" 24 | "math/rand" 25 | "strings" 26 | "sync" 27 | "sync/atomic" 28 | "time" 29 | "unsafe" 30 | 31 | "github.com/dustin/go-humanize" 32 | ) 33 | 34 | // Allocator amortizes the cost of small allocations by allocating memory in 35 | // bigger chunks. Internally it uses z.Calloc to allocate memory. Once 36 | // allocated, the memory is not moved, so it is safe to use the allocated bytes 37 | // to unsafe cast them to Go struct pointers. Maintaining a freelist is slow. 38 | // Instead, Allocator only allocates memory, with the idea that finally we 39 | // would just release the entire Allocator. 40 | type Allocator struct { 41 | sync.Mutex 42 | compIdx uint64 // Stores bufIdx in 32 MSBs and posIdx in 32 LSBs. 43 | buffers [][]byte 44 | Ref uint64 45 | Tag string 46 | } 47 | 48 | // allocs keeps references to all Allocators, so we can safely discard them later. 49 | var allocsMu *sync.Mutex 50 | var allocRef uint64 51 | var allocs map[uint64]*Allocator 52 | var calculatedLog2 []int 53 | 54 | func init() { 55 | allocsMu = new(sync.Mutex) 56 | allocs = make(map[uint64]*Allocator) 57 | 58 | // Set up a unique Ref per process. 59 | rand.Seed(time.Now().UnixNano()) 60 | allocRef = uint64(rand.Int63n(1<<16)) << 48 61 | 62 | calculatedLog2 = make([]int, 1025) 63 | for i := 1; i <= 1024; i++ { 64 | calculatedLog2[i] = int(math.Log2(float64(i))) 65 | } 66 | } 67 | 68 | // NewAllocator creates an allocator starting with the given size. 69 | func NewAllocator(sz int, tag string) *Allocator { 70 | ref := atomic.AddUint64(&allocRef, 1) 71 | // We should not allow a zero sized page because addBufferWithMinSize 72 | // will run into an infinite loop trying to double the pagesize. 73 | if sz < 512 { 74 | sz = 512 75 | } 76 | a := &Allocator{ 77 | Ref: ref, 78 | buffers: make([][]byte, 64), 79 | Tag: tag, 80 | } 81 | l2 := uint64(log2(sz)) 82 | if bits.OnesCount64(uint64(sz)) > 1 { 83 | l2 += 1 84 | } 85 | a.buffers[0] = Calloc(1<> 32), int(pos & 0xFFFFFFFF) 142 | } 143 | 144 | // Size returns the size of the allocations so far. 145 | func (a *Allocator) Size() int { 146 | pos := atomic.LoadUint64(&a.compIdx) 147 | bi, pi := parse(pos) 148 | var sz int 149 | for i, b := range a.buffers { 150 | if i < bi { 151 | sz += len(b) 152 | continue 153 | } 154 | sz += pi 155 | return sz 156 | } 157 | panic("Size should not reach here") 158 | } 159 | 160 | func log2(sz int) int { 161 | if sz < len(calculatedLog2) { 162 | return calculatedLog2[sz] 163 | } 164 | pow := 10 165 | sz >>= 10 166 | for sz > 1 { 167 | sz >>= 1 168 | pow++ 169 | } 170 | return pow 171 | } 172 | 173 | func (a *Allocator) Allocated() uint64 { 174 | var alloc int 175 | for _, b := range a.buffers { 176 | alloc += cap(b) 177 | } 178 | return uint64(alloc) 179 | } 180 | 181 | func (a *Allocator) TrimTo(max int) { 182 | var alloc int 183 | for i, b := range a.buffers { 184 | if len(b) == 0 { 185 | break 186 | } 187 | alloc += len(b) 188 | if alloc < max { 189 | continue 190 | } 191 | Free(b) 192 | a.buffers[i] = nil 193 | } 194 | } 195 | 196 | // Release would release the memory back. Remember to make this call to avoid memory leaks. 197 | func (a *Allocator) Release() { 198 | if a == nil { 199 | return 200 | } 201 | 202 | var alloc int 203 | for _, b := range a.buffers { 204 | if len(b) == 0 { 205 | break 206 | } 207 | alloc += len(b) 208 | Free(b) 209 | } 210 | 211 | allocsMu.Lock() 212 | delete(allocs, a.Ref) 213 | allocsMu.Unlock() 214 | } 215 | 216 | const maxAlloc = 1 << 30 217 | 218 | func (a *Allocator) MaxAlloc() int { 219 | return maxAlloc 220 | } 221 | 222 | const nodeAlign = unsafe.Sizeof(uint64(0)) - 1 223 | 224 | func (a *Allocator) AllocateAligned(sz int) []byte { 225 | tsz := sz + int(nodeAlign) 226 | out := a.Allocate(tsz) 227 | // We are reusing allocators. In that case, it's important to zero out the memory allocated 228 | // here. We don't always zero it out (in Allocate), because other functions would be immediately 229 | // overwriting the allocated slices anyway (see Copy). 230 | ZeroOut(out, 0, len(out)) 231 | 232 | addr := uintptr(unsafe.Pointer(&out[0])) 233 | aligned := (addr + nodeAlign) & ^nodeAlign 234 | start := int(aligned - addr) 235 | 236 | return out[start : start+sz] 237 | } 238 | 239 | func (a *Allocator) Copy(buf []byte) []byte { 240 | if a == nil { 241 | return append([]byte{}, buf...) 242 | } 243 | out := a.Allocate(len(buf)) 244 | copy(out, buf) 245 | return out 246 | } 247 | 248 | func (a *Allocator) addBufferAt(bufIdx, minSz int) { 249 | for { 250 | if bufIdx >= len(a.buffers) { 251 | panic(fmt.Sprintf("Allocator can not allocate more than %d buffers", len(a.buffers))) 252 | } 253 | if len(a.buffers[bufIdx]) == 0 { 254 | break 255 | } 256 | if minSz <= len(a.buffers[bufIdx]) { 257 | // No need to do anything. We already have a buffer which can satisfy minSz. 258 | return 259 | } 260 | bufIdx++ 261 | } 262 | assert(bufIdx > 0) 263 | // We need to allocate a new buffer. 264 | // Make pageSize double of the last allocation. 265 | pageSize := 2 * len(a.buffers[bufIdx-1]) 266 | // Ensure pageSize is bigger than sz. 267 | for pageSize < minSz { 268 | pageSize *= 2 269 | } 270 | // If bigger than maxAlloc, trim to maxAlloc. 271 | if pageSize > maxAlloc { 272 | pageSize = maxAlloc 273 | } 274 | 275 | buf := Calloc(pageSize, a.Tag) 276 | assert(len(a.buffers[bufIdx]) == 0) 277 | a.buffers[bufIdx] = buf 278 | } 279 | 280 | func (a *Allocator) Allocate(sz int) []byte { 281 | if a == nil { 282 | return make([]byte, sz) 283 | } 284 | if sz > maxAlloc { 285 | panic(fmt.Sprintf("Unable to allocate more than %d\n", maxAlloc)) 286 | } 287 | if sz == 0 { 288 | return nil 289 | } 290 | for { 291 | pos := atomic.AddUint64(&a.compIdx, uint64(sz)) 292 | bufIdx, posIdx := parse(pos) 293 | buf := a.buffers[bufIdx] 294 | if posIdx > len(buf) { 295 | a.Lock() 296 | newPos := atomic.LoadUint64(&a.compIdx) 297 | newBufIdx, _ := parse(newPos) 298 | if newBufIdx != bufIdx { 299 | a.Unlock() 300 | continue 301 | } 302 | a.addBufferAt(bufIdx+1, sz) 303 | atomic.StoreUint64(&a.compIdx, uint64((bufIdx+1)<<32)) 304 | a.Unlock() 305 | // We added a new buffer. Let's acquire slice the right way by going back to the top. 306 | continue 307 | } 308 | data := buf[posIdx-sz : posIdx] 309 | return data 310 | } 311 | } 312 | 313 | type AllocatorPool struct { 314 | numGets int64 315 | allocCh chan *Allocator 316 | closer *Closer 317 | } 318 | 319 | func NewAllocatorPool(sz int) *AllocatorPool { 320 | a := &AllocatorPool{ 321 | allocCh: make(chan *Allocator, sz), 322 | closer: NewCloser(1), 323 | } 324 | go a.freeupAllocators() 325 | return a 326 | } 327 | 328 | func (p *AllocatorPool) Get(sz int, tag string) *Allocator { 329 | if p == nil { 330 | return NewAllocator(sz, tag) 331 | } 332 | atomic.AddInt64(&p.numGets, 1) 333 | select { 334 | case alloc := <-p.allocCh: 335 | alloc.Reset() 336 | alloc.Tag = tag 337 | return alloc 338 | default: 339 | return NewAllocator(sz, tag) 340 | } 341 | } 342 | func (p *AllocatorPool) Return(a *Allocator) { 343 | if a == nil { 344 | return 345 | } 346 | if p == nil { 347 | a.Release() 348 | return 349 | } 350 | a.TrimTo(400 << 20) 351 | 352 | select { 353 | case p.allocCh <- a: 354 | return 355 | default: 356 | a.Release() 357 | } 358 | } 359 | 360 | func (p *AllocatorPool) Release() { 361 | if p == nil { 362 | return 363 | } 364 | p.closer.SignalAndWait() 365 | } 366 | 367 | func (p *AllocatorPool) freeupAllocators() { 368 | defer p.closer.Done() 369 | 370 | ticker := time.NewTicker(2 * time.Second) 371 | defer ticker.Stop() 372 | 373 | releaseOne := func() bool { 374 | select { 375 | case alloc := <-p.allocCh: 376 | alloc.Release() 377 | return true 378 | default: 379 | return false 380 | } 381 | } 382 | 383 | var last int64 384 | for { 385 | select { 386 | case <-p.closer.HasBeenClosed(): 387 | close(p.allocCh) 388 | for alloc := range p.allocCh { 389 | alloc.Release() 390 | } 391 | return 392 | 393 | case <-ticker.C: 394 | gets := atomic.LoadInt64(&p.numGets) 395 | if gets != last { 396 | // Some retrievals were made since the last time. So, let's avoid doing a release. 397 | last = gets 398 | continue 399 | } 400 | releaseOne() 401 | } 402 | } 403 | } 404 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | --------------------------------------------------------------------------------