Skip to content

Commit

Permalink
incusd/storage/drivers/lvm: Cache VG extent size
Browse files Browse the repository at this point in the history
Querying this value can get extremely expensive on busy systems.

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Sep 6, 2024
1 parent 442b89b commit b6c1dd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/server/storage/drivers/driver_lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"slices"
"strconv"
"strings"
"sync"
"time"

"github.com/lxc/incus/v6/internal/linux"
Expand All @@ -26,6 +27,9 @@ import (

const lvmVgPoolMarker = "incus_pool" // Indicator tag used to mark volume groups as in use.

var lvmExtentSize map[string]int64
var lvmExtentSizeMu sync.Mutex

var lvmLoaded bool
var lvmVersion string

Expand Down
19 changes: 18 additions & 1 deletion internal/server/storage/drivers/driver_lvm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ func (d *lvm) volumeGroupExists(vgName string) (bool, []string, error) {

// volumeGroupExtentSize gets the volume group's physical extent size in bytes.
func (d *lvm) volumeGroupExtentSize(vgName string) (int64, error) {
// Look for cached value.
lvmExtentSizeMu.Lock()
defer lvmExtentSizeMu.Unlock()

if lvmExtentSize == nil {
lvmExtentSize = map[string]int64{}
} else if lvmExtentSize[d.name] > 0 {
return lvmExtentSize[d.name], nil
}

output, err := subprocess.TryRunCommand("vgs", "--noheadings", "--nosuffix", "--units", "b", "-o", "vg_extent_size", vgName)
if err != nil {
if d.isLVMNotFoundExitError(err) {
Expand All @@ -142,7 +152,14 @@ func (d *lvm) volumeGroupExtentSize(vgName string) (int64, error) {
}

output = strings.TrimSpace(output)
return strconv.ParseInt(output, 10, 64)
val, err := strconv.ParseInt(output, 10, 64)
if err != nil {
return -1, err
}

lvmExtentSize[d.name] = val

return val, nil
}

// countLogicalVolumes gets the count of volumes (both normal and thin) in a volume group.
Expand Down

0 comments on commit b6c1dd0

Please sign in to comment.