MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / get_cpu_percent

Method get_cpu_percent

class/process_task.py:401–424  ·  view source on GitHub ↗

@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)

Source from the content-addressed store, hash-verified

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 ''&#x27;
403 @name 获取pid的cpu占用率
404 @author hwliang<2021-09-04>
405 @param pid 进程id
406 @param cpu_time_total 进程总cpu时间
407 @return 占用cpu百分比
408 ''&#x27;
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

Callers 1

get_monitor_listMethod · 0.95

Calls 1

timeMethod · 0.80

Tested by

no test coverage detected