@name 获取pid的cpu占用率 @author hwliang<2021-09-04> @param pid 进程id @param cpu_time_total 进程总cpu时间 @return 占用cpu百分比
(self, pid, cpu_time_total, s_cpu_times)
| 399 | return s.user + s.system + s.nice + s.idle |
| 400 | |
| 401 | def get_cpu_percent(self, pid, cpu_time_total, s_cpu_times): |
| 402 | ''' |
| 403 | @name 获取pid的cpu占用率 |
| 404 | @author hwliang<2021-09-04> |
| 405 | @param pid 进程id |
| 406 | @param cpu_time_total 进程总cpu时间 |
| 407 | @return 占用cpu百分比 |
| 408 | ''' |
| 409 | stime = time.time() |
| 410 | if pid in self.__last_times: |
| 411 | old_time = self.__last_times[pid] |
| 412 | else: |
| 413 | self.__last_times[pid] = cpu_time_total |
| 414 | self.__last_dates[pid] = stime |
| 415 | return 0 |
| 416 | |
| 417 | cpu_percent = round( |
| 418 | 100.00 * float(cpu_time_total - old_time) / |
| 419 | (s_cpu_times - self.__last_cpu_time), 2) |
| 420 | self.__last_times[pid] = cpu_time_total |
| 421 | self.__last_dates[pid] = stime |
| 422 | if cpu_percent > 100: cpu_percent = 99 |
| 423 | if cpu_percent < 0: cpu_percent = 0 |
| 424 | return cpu_percent |
| 425 | |
| 426 | def get_io_write(self, pid, io_write): |
| 427 | disk_io_write = 0 |