Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/syncthing/usage_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/connections"
"github.com/syncthing/syncthing/lib/dialer"
"github.com/syncthing/syncthing/lib/memsize"
"github.com/syncthing/syncthing/lib/model"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/scanner"
Expand Down Expand Up @@ -71,8 +72,7 @@ func reportData(cfg configIntf, m modelIntf, connectionsService connectionsIntf,
res["sha256Perf"] = cpuBench(5, 125*time.Millisecond, false)
res["hashPerf"] = cpuBench(5, 125*time.Millisecond, true)

bytes, err := memorySize()
if err == nil {
if bytes, err := memsize.MemorySize(); err == nil {
res["memorySize"] = bytes / 1024 / 1024
}
res["numCPU"] = runtime.NumCPU()
Expand Down
117 changes: 117 additions & 0 deletions lib/diskoverflow/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

// Package diskoverflow provides several data container types which are limited
// in their memory usage. Once the total memory limit is reached, all new data
// is written to disk.
// Do not use any instances of these types concurrently!
package diskoverflow

import "github.com/syncthing/syncthing/lib/protocol"

// Value must be implemented by every type that is to be stored in a disk spilling container.
type Value interface {
Size() int64
Marshal() []byte
Unmarshal([]byte) Value // The returned Value must not be a reference to the receiver.
}

// ValueFileInfo implements Value for protocol.FileInfo
type ValueFileInfo struct{ protocol.FileInfo }

func (s *ValueFileInfo) Size() int64 {
return int64(s.ProtoSize())
}

func (s *ValueFileInfo) Marshal() []byte {
data, err := s.FileInfo.Marshal()
if err != nil {
panic("bug: marshalling FileInfo should never fail: " + err.Error())
}
return data
}

func (s *ValueFileInfo) Unmarshal(v []byte) Value {
out := &ValueFileInfo{}
if err := out.FileInfo.Unmarshal(v); err != nil {
panic("unmarshal failed: " + err.Error())
}
return out
}

// Magical limit below which the underlying containers of slices/maps are never
// reset to save space.
// Variable for test, shouldn't ever be changed in code.
var minCompactionSize int64 = 10 << protocol.MiB

type common interface {
close()
length() int
}

type Iterator interface {
Release()
Next() bool
}

type ValueIterator interface {
Iterator
Value() Value
}

type SortValueIterator interface {
Iterator
Value() SortValue
}

type iteratorParent interface {
released()
value() interface{}
}

const (
concurrencyMsg = "iteration in progress - don't modify or start a new iteration concurrently"
)

type memIterator struct {
pos int
len int
reverse bool
parent iteratorParent
}

func newMemIterator(p iteratorParent, reverse bool, len int) *memIterator {
it := &memIterator{
len: len,
reverse: reverse,
parent: p,
}
if reverse {
it.pos = len
} else {
it.pos = -1
}
return it
}

func (si *memIterator) Next() bool {
if si.reverse {
if si.pos == 0 {
return false
}
si.pos--
return true
}
if si.pos == si.len-1 {
return false
}
si.pos++
return true
}

func (si *memIterator) Release() {
si.parent.released()
}
26 changes: 26 additions & 0 deletions lib/diskoverflow/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package diskoverflow

import (
"os"
"strings"

"github.com/syncthing/syncthing/lib/logger"
)

var (
l = logger.DefaultLogger.NewFacility("diskoverflow", "data containers that spill to disk if too big")
)

func init() {
l.SetDebug("diskoverflow", strings.Contains(os.Getenv("STTRACE"), "diskoverflow") || os.Getenv("STTRACE") == "all")
}

func shouldDebug() bool {
return l.ShouldDebug("diskoverflow")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't see this used.

}
113 changes: 113 additions & 0 deletions lib/diskoverflow/limiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (C) 2018 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package diskoverflow

import (
"math/rand"

"github.com/syncthing/syncthing/lib/memsize"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/sync"
)

var (
availableMemory int64
totalLimit1 int64
totalLimit2 int64
totalLimitHard int64
limit1 int64
limit2 int64
)

func init() {
if total, err := memsize.MemorySize(); err != nil {
// availableMemory = config.Options().FallBackMemoryLimit
availableMemory = 100 << protocol.MiB
} else {
availableMemory = total
}
setLimits()
}

func setLimits() {
totalLimit1 = availableMemory / 100 * 45
totalLimit2 = availableMemory / 100 * 50
totalLimitHard = availableMemory / 100 * 55
limit1 = availableMemory / 100 * 10
limit2 = availableMemory / 100 * 1
}

// Tracks active disk overflow types, with information about how much memory
// they currently occupy and whether they were already told to spill to disk.
var lim = limiter{
sizes: make(map[int]int64),
spilling: make(map[int]struct{}),
mut: sync.NewRWMutex(),
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the noble idea here, but this thing adds a lot of complexity. Perhaps we should specify the size as we initialize these types, and like adults divide it in half if we know we are creating two of these containers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand scans happen in parallel, so being able to have a good grip on this might be hard.


type limiter struct {
totalSize int64
sizes map[int]int64
spilling map[int]struct{}
mut sync.RWMutex
}

func (li *limiter) register() int {
var key int
li.mut.Lock()
for ok := true; ok; _, ok = li.sizes[key] {
key = rand.Int()
}
li.sizes[key] = 0
li.mut.Unlock()
return key
}

func (li *limiter) deregister(key int) {
li.mut.Lock()
li.totalSize -= li.sizes[key]
delete(li.sizes, key)
delete(li.spilling, key)
li.mut.Unlock()
}

// The returned bool is true, if the container can keep using memory
func (li *limiter) add(key int, size int64) bool {
li.mut.Lock()
Comment thread
imsodin marked this conversation as resolved.
Resolved
defer li.mut.Unlock()
if _, ok := li.spilling[key]; ok || li.shouldSpillLocked(key) {
return false
}
li.totalSize += size
li.sizes[key] += size
return true
}

func (li *limiter) size(key int) int64 {
li.mut.RLock()
defer li.mut.RUnlock()
return li.sizes[key]
}

func (li *limiter) remove(key int, size int64) {
li.mut.Lock()
li.totalSize -= size
li.sizes[key] -= size
li.mut.Unlock()
}

func (li *limiter) shouldSpillLocked(key int) bool {
switch s := li.sizes[key]; {
case li.totalSize > totalLimitHard:
return true
case li.totalSize > totalLimit2:
return s > limit2
case li.totalSize > totalLimit1:
return s > limit1
}
return false
}
Loading