Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2012 Robert Yang |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 7 | import os, logging, re |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | import bb |
| 9 | logger = logging.getLogger("BitBake.Monitor") |
| 10 | |
| 11 | def printErr(info): |
| 12 | logger.error("%s\n Disk space monitor will NOT be enabled" % info) |
| 13 | |
| 14 | def convertGMK(unit): |
| 15 | |
| 16 | """ Convert the space unit G, M, K, the unit is case-insensitive """ |
| 17 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 18 | unitG = re.match(r'([1-9][0-9]*)[gG]\s?$', unit) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | if unitG: |
| 20 | return int(unitG.group(1)) * (1024 ** 3) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 21 | unitM = re.match(r'([1-9][0-9]*)[mM]\s?$', unit) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | if unitM: |
| 23 | return int(unitM.group(1)) * (1024 ** 2) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 24 | unitK = re.match(r'([1-9][0-9]*)[kK]\s?$', unit) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | if unitK: |
| 26 | return int(unitK.group(1)) * 1024 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 27 | unitN = re.match(r'([1-9][0-9]*)\s?$', unit) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | if unitN: |
| 29 | return int(unitN.group(1)) |
| 30 | else: |
| 31 | return None |
| 32 | |
| 33 | def getMountedDev(path): |
| 34 | |
| 35 | """ Get the device mounted at the path, uses /proc/mounts """ |
| 36 | |
| 37 | # Get the mount point of the filesystem containing path |
| 38 | # st_dev is the ID of device containing file |
| 39 | parentDev = os.stat(path).st_dev |
| 40 | currentDev = parentDev |
| 41 | # When the current directory's device is different from the |
| 42 | # parent's, then the current directory is a mount point |
| 43 | while parentDev == currentDev: |
| 44 | mountPoint = path |
| 45 | # Use dirname to get the parent's directory |
| 46 | path = os.path.dirname(path) |
| 47 | # Reach the "/" |
| 48 | if path == mountPoint: |
| 49 | break |
| 50 | parentDev= os.stat(path).st_dev |
| 51 | |
| 52 | try: |
| 53 | with open("/proc/mounts", "r") as ifp: |
| 54 | for line in ifp: |
| 55 | procLines = line.rstrip('\n').split() |
| 56 | if procLines[1] == mountPoint: |
| 57 | return procLines[0] |
| 58 | except EnvironmentError: |
| 59 | pass |
| 60 | return None |
| 61 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 62 | def getDiskData(BBDirs): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | |
| 64 | """Prepare disk data for disk space monitor""" |
| 65 | |
| 66 | # Save the device IDs, need the ID to be unique (the dictionary's key is |
| 67 | # unique), so that when more than one directory is located on the same |
| 68 | # device, we just monitor it once |
| 69 | devDict = {} |
| 70 | for pathSpaceInode in BBDirs.split(): |
| 71 | # The input format is: "dir,space,inode", dir is a must, space |
| 72 | # and inode are optional |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 73 | pathSpaceInodeRe = re.match(r'([^,]*),([^,]*),([^,]*),?(.*)', pathSpaceInode) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | if not pathSpaceInodeRe: |
| 75 | printErr("Invalid value in BB_DISKMON_DIRS: %s" % pathSpaceInode) |
| 76 | return None |
| 77 | |
| 78 | action = pathSpaceInodeRe.group(1) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame^] | 79 | if action == "ABORT": |
| 80 | # Emit a deprecation warning |
| 81 | logger.warnonce("The BB_DISKMON_DIRS \"ABORT\" action has been been renamed to \"HALT\", update configuration") |
| 82 | action = "HALT" |
| 83 | |
| 84 | if action not in ("HALT", "STOPTASKS", "WARN"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | printErr("Unknown disk space monitor action: %s" % action) |
| 86 | return None |
| 87 | |
| 88 | path = os.path.realpath(pathSpaceInodeRe.group(2)) |
| 89 | if not path: |
| 90 | printErr("Invalid path value in BB_DISKMON_DIRS: %s" % pathSpaceInode) |
| 91 | return None |
| 92 | |
| 93 | # The disk space or inode is optional, but it should have a correct |
| 94 | # value once it is specified |
| 95 | minSpace = pathSpaceInodeRe.group(3) |
| 96 | if minSpace: |
| 97 | minSpace = convertGMK(minSpace) |
| 98 | if not minSpace: |
| 99 | printErr("Invalid disk space value in BB_DISKMON_DIRS: %s" % pathSpaceInodeRe.group(3)) |
| 100 | return None |
| 101 | else: |
| 102 | # None means that it is not specified |
| 103 | minSpace = None |
| 104 | |
| 105 | minInode = pathSpaceInodeRe.group(4) |
| 106 | if minInode: |
| 107 | minInode = convertGMK(minInode) |
| 108 | if not minInode: |
| 109 | printErr("Invalid inode value in BB_DISKMON_DIRS: %s" % pathSpaceInodeRe.group(4)) |
| 110 | return None |
| 111 | else: |
| 112 | # None means that it is not specified |
| 113 | minInode = None |
| 114 | |
| 115 | if minSpace is None and minInode is None: |
| 116 | printErr("No disk space or inode value in found BB_DISKMON_DIRS: %s" % pathSpaceInode) |
| 117 | return None |
| 118 | # mkdir for the directory since it may not exist, for example the |
| 119 | # DL_DIR may not exist at the very beginning |
| 120 | if not os.path.exists(path): |
| 121 | bb.utils.mkdirhier(path) |
| 122 | dev = getMountedDev(path) |
| 123 | # Use path/action as the key |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | devDict[(path, action)] = [dev, minSpace, minInode] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
| 126 | return devDict |
| 127 | |
| 128 | def getInterval(configuration): |
| 129 | |
| 130 | """ Get the disk space interval """ |
| 131 | |
| 132 | # The default value is 50M and 5K. |
| 133 | spaceDefault = 50 * 1024 * 1024 |
| 134 | inodeDefault = 5 * 1024 |
| 135 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 136 | interval = configuration.getVar("BB_DISKMON_WARNINTERVAL") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 137 | if not interval: |
| 138 | return spaceDefault, inodeDefault |
| 139 | else: |
| 140 | # The disk space or inode interval is optional, but it should |
| 141 | # have a correct value once it is specified |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 142 | intervalRe = re.match(r'([^,]*),?\s*(.*)', interval) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | if intervalRe: |
| 144 | intervalSpace = intervalRe.group(1) |
| 145 | if intervalSpace: |
| 146 | intervalSpace = convertGMK(intervalSpace) |
| 147 | if not intervalSpace: |
| 148 | printErr("Invalid disk space interval value in BB_DISKMON_WARNINTERVAL: %s" % intervalRe.group(1)) |
| 149 | return None, None |
| 150 | else: |
| 151 | intervalSpace = spaceDefault |
| 152 | intervalInode = intervalRe.group(2) |
| 153 | if intervalInode: |
| 154 | intervalInode = convertGMK(intervalInode) |
| 155 | if not intervalInode: |
| 156 | printErr("Invalid disk inode interval value in BB_DISKMON_WARNINTERVAL: %s" % intervalRe.group(2)) |
| 157 | return None, None |
| 158 | else: |
| 159 | intervalInode = inodeDefault |
| 160 | return intervalSpace, intervalInode |
| 161 | else: |
| 162 | printErr("Invalid interval value in BB_DISKMON_WARNINTERVAL: %s" % interval) |
| 163 | return None, None |
| 164 | |
| 165 | class diskMonitor: |
| 166 | |
| 167 | """Prepare the disk space monitor data""" |
| 168 | |
| 169 | def __init__(self, configuration): |
| 170 | |
| 171 | self.enableMonitor = False |
| 172 | self.configuration = configuration |
| 173 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 174 | BBDirs = configuration.getVar("BB_DISKMON_DIRS") or None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 175 | if BBDirs: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 176 | self.devDict = getDiskData(BBDirs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | if self.devDict: |
| 178 | self.spaceInterval, self.inodeInterval = getInterval(configuration) |
| 179 | if self.spaceInterval and self.inodeInterval: |
| 180 | self.enableMonitor = True |
| 181 | # These are for saving the previous disk free space and inode, we |
| 182 | # use them to avoid printing too many warning messages |
| 183 | self.preFreeS = {} |
| 184 | self.preFreeI = {} |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame^] | 185 | # This is for STOPTASKS and HALT, to avoid printing the message |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | # repeatedly while waiting for the tasks to finish |
| 187 | self.checked = {} |
| 188 | for k in self.devDict: |
| 189 | self.preFreeS[k] = 0 |
| 190 | self.preFreeI[k] = 0 |
| 191 | self.checked[k] = False |
| 192 | if self.spaceInterval is None and self.inodeInterval is None: |
| 193 | self.enableMonitor = False |
| 194 | |
| 195 | def check(self, rq): |
| 196 | |
| 197 | """ Take action for the monitor """ |
| 198 | |
| 199 | if self.enableMonitor: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 200 | diskUsage = {} |
| 201 | for k, attributes in self.devDict.items(): |
| 202 | path, action = k |
| 203 | dev, minSpace, minInode = attributes |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 204 | |
| 205 | st = os.statvfs(path) |
| 206 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 207 | # The available free space, integer number |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 208 | freeSpace = st.f_bavail * st.f_frsize |
| 209 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 210 | # Send all relevant information in the event. |
| 211 | freeSpaceRoot = st.f_bfree * st.f_frsize |
| 212 | totalSpace = st.f_blocks * st.f_frsize |
| 213 | diskUsage[dev] = bb.event.DiskUsageSample(freeSpace, freeSpaceRoot, totalSpace) |
| 214 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 215 | if minSpace and freeSpace < minSpace: |
| 216 | # Always show warning, the self.checked would always be False if the action is WARN |
| 217 | if self.preFreeS[k] == 0 or self.preFreeS[k] - freeSpace > self.spaceInterval and not self.checked[k]: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 218 | logger.warning("The free space of %s (%s) is running low (%.3fGB left)" % \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | (path, dev, freeSpace / 1024 / 1024 / 1024.0)) |
| 220 | self.preFreeS[k] = freeSpace |
| 221 | |
| 222 | if action == "STOPTASKS" and not self.checked[k]: |
| 223 | logger.error("No new tasks can be executed since the disk space monitor action is \"STOPTASKS\"!") |
| 224 | self.checked[k] = True |
| 225 | rq.finish_runqueue(False) |
| 226 | bb.event.fire(bb.event.DiskFull(dev, 'disk', freeSpace, path), self.configuration) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame^] | 227 | elif action == "HALT" and not self.checked[k]: |
| 228 | logger.error("Immediately halt since the disk space monitor action is \"HALT\"!") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | self.checked[k] = True |
| 230 | rq.finish_runqueue(True) |
| 231 | bb.event.fire(bb.event.DiskFull(dev, 'disk', freeSpace, path), self.configuration) |
| 232 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 233 | # The free inodes, integer number |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 234 | freeInode = st.f_favail |
| 235 | |
| 236 | if minInode and freeInode < minInode: |
| 237 | # Some filesystems use dynamic inodes so can't run out |
| 238 | # (e.g. btrfs). This is reported by the inode count being 0. |
| 239 | if st.f_files == 0: |
| 240 | self.devDict[k][2] = None |
| 241 | continue |
| 242 | # Always show warning, the self.checked would always be False if the action is WARN |
| 243 | if self.preFreeI[k] == 0 or self.preFreeI[k] - freeInode > self.inodeInterval and not self.checked[k]: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 244 | logger.warning("The free inode of %s (%s) is running low (%.3fK left)" % \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | (path, dev, freeInode / 1024.0)) |
| 246 | self.preFreeI[k] = freeInode |
| 247 | |
| 248 | if action == "STOPTASKS" and not self.checked[k]: |
| 249 | logger.error("No new tasks can be executed since the disk space monitor action is \"STOPTASKS\"!") |
| 250 | self.checked[k] = True |
| 251 | rq.finish_runqueue(False) |
| 252 | bb.event.fire(bb.event.DiskFull(dev, 'inode', freeInode, path), self.configuration) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame^] | 253 | elif action == "HALT" and not self.checked[k]: |
| 254 | logger.error("Immediately halt since the disk space monitor action is \"HALT\"!") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 255 | self.checked[k] = True |
| 256 | rq.finish_runqueue(True) |
| 257 | bb.event.fire(bb.event.DiskFull(dev, 'inode', freeInode, path), self.configuration) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 258 | |
| 259 | bb.event.fire(bb.event.MonitorDiskEvent(diskUsage), self.configuration) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | return |