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{})
| 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. |
| 1561 | func (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. |
no test coverage detected