the code is by: https://github.com/giampaolo/psutil/blob/master/scripts/iotop.py good luck for opensource! modify: cpp.la Calculate IO usage by comparing IO statics before and after the interval. Return a tuple including all currently running processes sorted by
()
| 236 | time.sleep(INTERVAL) |
| 237 | |
| 238 | def _disk_io(): |
| 239 | """ |
| 240 | the code is by: https://github.com/giampaolo/psutil/blob/master/scripts/iotop.py |
| 241 | good luck for opensource! modify: cpp.la |
| 242 | Calculate IO usage by comparing IO statics before and |
| 243 | after the interval. |
| 244 | Return a tuple including all currently running processes |
| 245 | sorted by IO activity and total disks I/O activity. |
| 246 | 磁盘IO:因为IOPS原因,SSD和HDD、包括RAID卡,ZFS等。IO对性能的影响还需要结合自身服务器情况来判断。 |
| 247 | 比如我这里是机械硬盘,大量做随机小文件读写,那么很低的读写也就能造成硬盘长时间的等待。 |
| 248 | 如果这里做连续性IO,那么普通机械硬盘写入到100Mb/s,那么也能造成硬盘长时间的等待。 |
| 249 | 磁盘读写有误差:4k,8k ,https://stackoverflow.com/questions/34413926/psutil-vs-dd-monitoring-disk-i-o |
| 250 | macos/win,暂不处理。 |
| 251 | """ |
| 252 | if "darwin" in sys.platform or "win" in sys.platform: |
| 253 | diskIO["read"] = 0 |
| 254 | diskIO["write"] = 0 |
| 255 | else: |
| 256 | while True: |
| 257 | # first get a list of all processes and disk io counters |
| 258 | procs = [p for p in psutil.process_iter()] |
| 259 | for p in procs[:]: |
| 260 | try: |
| 261 | p._before = p.io_counters() |
| 262 | except psutil.Error: |
| 263 | procs.remove(p) |
| 264 | continue |
| 265 | disks_before = psutil.disk_io_counters() |
| 266 | |
| 267 | # sleep some time, only when INTERVAL==1 , io read/write per_sec. |
| 268 | # when INTERVAL > 1, io read/write per_INTERVAL |
| 269 | time.sleep(INTERVAL) |
| 270 | |
| 271 | # then retrieve the same info again |
| 272 | for p in procs[:]: |
| 273 | with p.oneshot(): |
| 274 | try: |
| 275 | p._after = p.io_counters() |
| 276 | p._cmdline = ' '.join(p.cmdline()) |
| 277 | if not p._cmdline: |
| 278 | p._cmdline = p.name() |
| 279 | p._username = p.username() |
| 280 | except (psutil.NoSuchProcess, psutil.ZombieProcess): |
| 281 | procs.remove(p) |
| 282 | disks_after = psutil.disk_io_counters() |
| 283 | |
| 284 | # finally calculate results by comparing data before and |
| 285 | # after the interval |
| 286 | for p in procs: |
| 287 | p._read_per_sec = p._after.read_bytes - p._before.read_bytes |
| 288 | p._write_per_sec = p._after.write_bytes - p._before.write_bytes |
| 289 | p._total = p._read_per_sec + p._write_per_sec |
| 290 | |
| 291 | diskIO["read"] = disks_after.read_bytes - disks_before.read_bytes |
| 292 | diskIO["write"] = disks_after.write_bytes - disks_before.write_bytes |
| 293 | |
| 294 | def get_realtime_data(): |
| 295 | ''' |
nothing calls this directly
no outgoing calls
no test coverage detected