Init initializes global 'MemoryAccounting'.
()
| 248 | |
| 249 | // Init initializes global 'MemoryAccounting'. |
| 250 | func Init() error { |
| 251 | initOnce.Do(func() { |
| 252 | initErr = func() error { |
| 253 | const name = "memory-usage" |
| 254 | fd, err := memutil.CreateMemFD(name, 0) |
| 255 | if err != nil { |
| 256 | return fmt.Errorf("error creating usage file: %v", err) |
| 257 | } |
| 258 | file := os.NewFile(uintptr(fd), name) |
| 259 | if err := file.Truncate(int64(RTMemoryStatsSize)); err != nil { |
| 260 | return fmt.Errorf("error truncating usage file: %v", err) |
| 261 | } |
| 262 | // Note: We rely on the returned page being initially zeroed. This will |
| 263 | // always be the case for a newly mapped page from /dev/shm. If we obtain |
| 264 | // the shared memory through some other means in the future, we may have to |
| 265 | // explicitly zero the page. |
| 266 | mmap, err := memutil.MapFile(0, RTMemoryStatsSize, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED, file.Fd(), 0) |
| 267 | if err != nil { |
| 268 | return fmt.Errorf("error mapping usage file: %v", err) |
| 269 | } |
| 270 | |
| 271 | MemoryAccounting = &MemoryLocked{ |
| 272 | File: file, |
| 273 | RTMemoryStats: RTMemoryStatsPointer(mmap), |
| 274 | MemCgIDToMemStats: make(map[uint32]*memoryStats), |
| 275 | } |
| 276 | return nil |
| 277 | }() |
| 278 | }) |
| 279 | return initErr |
| 280 | } |
| 281 | |
| 282 | // MemoryAccounting is the global memory stats. |
| 283 | // |
no test coverage detected
searching dependent graphs…