MCPcopy
hub / github.com/google/gvisor / UpdateUsage

Method UpdateUsage

pkg/sentry/pgalloc/pgalloc.go:1561–1598  ·  view source on GitHub ↗

UpdateUsage ensures that the memory usage statistics in usage.MemoryAccounting are up to date. If memCgIDs is nil, all the pages will be scanned. Else only the pages which belong to the memory cgroup ids in memCgIDs will be scanned and the memory usage will be updated.

(memCgIDs map[uint32]struct{})

Source from the content-addressed store, hash-verified

1559// will be scanned. Else only the pages which belong to the memory cgroup ids
1560// in memCgIDs will be scanned and the memory usage will be updated.
1561func (f *MemoryFile) UpdateUsage(memCgIDs map[uint32]struct{}) error {
1562 // If we already know of every committed page, skip scanning.
1563 currentUsage, err := f.TotalUsage()
1564 if err != nil {
1565 return err
1566 }
1567 f.mu.Lock()
1568 defer f.mu.Unlock()
1569 if currentUsage == f.knownCommittedBytes {
1570 return nil
1571 }
1572
1573 if f.isSaving {
1574 log.Debugf("pgalloc.MemoryFile.UpdateUsage() inhibited during MemoryFile save")
1575 return nil
1576 }
1577
1578 // Linux updates usage values at CONFIG_HZ; throttle our scans to the same
1579 // frequency.
1580 startTime := time.Now()
1581 if startTime.Before(f.nextCommitScan) {
1582 return nil
1583 }
1584 if memCgIDs == nil {
1585 f.nextCommitScan = startTime.Add(time.Second / linux.CLOCKS_PER_SEC)
1586 }
1587
1588 err = f.updateUsageLocked(memCgIDs)
1589 if _, ok := err.(updateUsageDuringSaveErr); ok {
1590 log.Debugf("pgalloc.MemoryFile.UpdateUsage() inhibited during MemoryFile save")
1591 return nil
1592 }
1593 if log.IsLogging(log.Debug) {
1594 log.Debugf("UpdateUsage: took %v, currentUsage=%d knownCommittedBytes=%d",
1595 time.Since(startTime), currentUsage, f.knownCommittedBytes)
1596 }
1597 return err
1598}
1599
1600// updateUsageLocked attempts to detect commitment of previously-uncommitted
1601// pages, and updates memory accounting to reflect newly-committed pages.

Callers 4

CollectMethod · 0.80
getUsageFunction · 0.80
GenerateMethod · 0.80
EventMethod · 0.80

Calls 9

TotalUsageMethod · 0.95
updateUsageLockedMethod · 0.95
DebugfFunction · 0.92
IsLoggingFunction · 0.92
LockMethod · 0.65
UnlockMethod · 0.65
NowMethod · 0.65
AddMethod · 0.65
BeforeMethod · 0.45

Tested by

no test coverage detected