|
| 1 | +import os |
| 2 | +import traceback |
| 3 | +import time |
| 4 | + |
| 5 | +CGRP_CPU_LIMIT = 'RES_CPU_LIMIT' |
| 6 | + |
| 7 | +class cpuutilisation: |
| 8 | + def __init__(self, cpu_limit=None): |
| 9 | + ''' |
| 10 | + Initate the object for calculating cpu utilisation. |
| 11 | + If it fails to calculate, in any case it will return value pass |
| 12 | + while initiating the class or it will try to get value from env variable |
| 13 | + RES_CPU_LIMIT. One of them is mandatory. |
| 14 | + ''' |
| 15 | + self.cgrp_init_cycles, self.cgrp_end_cycles = 0, 0 |
| 16 | + self.sys_init_cycles, self.sys_end_cycles = 0, 0 |
| 17 | + self.no_of_host_cores = 0 |
| 18 | + self.errors = [] |
| 19 | + if cpu_limit is not None: |
| 20 | + self.cgrp_cpu_limit = cpu_limit |
| 21 | + else: |
| 22 | + try: |
| 23 | + self.cgrp_cpu_limit = os.environ[CGRP_CPU_LIMIT] |
| 24 | + except Exception as e: |
| 25 | + raise Exception('No cpu limit specified while initiating class or set in env variable {}'.format(e)) |
| 26 | + def get_no_of_cores_host(self): |
| 27 | + ''' |
| 28 | + Get total number of cores on host machine |
| 29 | + ''' |
| 30 | + cpu_info = '/proc/cpuinfo' |
| 31 | + try: |
| 32 | + with open(cpu_info, 'r') as cpus: |
| 33 | + lscpu = cpus.readlines() |
| 34 | + cores = 0 |
| 35 | + for cpu in lscpu: |
| 36 | + if 'processor' in cpu: #count as core if processor exists in line |
| 37 | + cores += 1 |
| 38 | + return int(cores) |
| 39 | + except Exception as e: |
| 40 | + err = 'Unable to read number of Cores from System :- {}'.format(e) |
| 41 | + self.errors.append(err) |
| 42 | + # print(err) |
| 43 | + # traceback.print_exc() |
| 44 | + def get_cpu_usage_cgrp(self): |
| 45 | + ''' |
| 46 | + Read the cpu cycles of cgroup, |
| 47 | + ''' |
| 48 | + file = '/sys/fs/cgroup/cpu,cpuacct/cpuacct.usage_percpu' |
| 49 | + try: |
| 50 | + with open(file, 'r') as usage_percpu: |
| 51 | + all_cores = usage_percpu.readlines()[0].split() |
| 52 | + total_cgrpcpu_cycles = 0 |
| 53 | + for core in all_cores: total_cgrpcpu_cycles += int(core) |
| 54 | + return total_cgrpcpu_cycles |
| 55 | + except Exception as e: |
| 56 | + err = 'Exception occured while get per cpu cgrp usage :- {}'.format(e) |
| 57 | + self.errors.append(err) |
| 58 | + #print(err) |
| 59 | + # traceback.print_exc() |
| 60 | + |
| 61 | + def get_cpu_usage_host(self): |
| 62 | + ''' |
| 63 | + Read the cpu cycles from of host. |
| 64 | + ''' |
| 65 | + try: |
| 66 | + sys_stat = '/proc/stat' |
| 67 | + with open(sys_stat, 'r') as sys: |
| 68 | + stat = sys.readlines()[0].split() #system wide cpu usage all cores |
| 69 | + system_stat = 0 |
| 70 | + for x in stat[1:]: |
| 71 | + system_stat += int(x) |
| 72 | + return int(system_stat) |
| 73 | + except Exception as e: |
| 74 | + err = 'Exception occured duing getting system usage cycle :- {}'.format(e) |
| 75 | + self.errors.append(err) |
| 76 | + # print(err) |
| 77 | + # traceback.print_exc() |
| 78 | + def get_core_utilisation(self): |
| 79 | + ''' |
| 80 | + > If it fails to calculate the usage, it return the default value defined in |
| 81 | + passed while initiating the class, core_utilisation else value from env variable 'RES_CPU_LIMIT'. |
| 82 | + Calculates the delta of cpu usage between start time & end time. |
| 83 | + returns the Float value. If total cores in system are 10 and it returns |
| 84 | + 2.3 that means, function have used 2.3 cores on an average, which translates |
| 85 | + to around 2300m in terms, if one checks in docker stats. |
| 86 | + ''' |
| 87 | + try: |
| 88 | + self.no_of_host_cores = self.get_no_of_cores_host() |
| 89 | + cores_used = float((self.cgrp_end_cycles - self.cgrp_init_cycles)/(self.sys_end_cycles - self.sys_init_cycles) * self.no_of_host_cores * 100)/1000000000 |
| 90 | + return None, float('{:.2f}'.format(cores_used)) |
| 91 | + except Exception as e: |
| 92 | + err = 'Issue occured in cpu utilisation calculation, due to previous errors returning default maximum value set:- {}'.format(self.cgrp_cpu_limit) |
| 93 | + self.errors.append(err) |
| 94 | + # print(err) |
| 95 | + # print('Core utilisation Calculation error, values passed :- {}'.format(vars(cpuutilisation(self.cgrp_cpu_limit)))) |
| 96 | + # traceback.print_exc() |
| 97 | + return self.errors, float(self.cgrp_cpu_limit) |
| 98 | + |
| 99 | + def start_time(self): |
| 100 | + ''' |
| 101 | + Get the start of cgroup cycles and system cycles at point, when this function is called. |
| 102 | + Example Usage: |
| 103 | + Initate the core class, with default limit if it fails to calculate. Ex. 3 |
| 104 | + core = cpuutilisation(3) |
| 105 | + Call this function before the start of function, you want to calculate cpu usage for |
| 106 | + core.start_time() |
| 107 | + actual_function_call_here_for_which_cpu_util_need_to_be_calculated() |
| 108 | + ''' |
| 109 | + self.cgrp_init_cycles = self.get_cpu_usage_cgrp() |
| 110 | + self.sys_init_cycles = self.get_cpu_usage_host() |
| 111 | + |
| 112 | + def end_time(self): |
| 113 | + ''' |
| 114 | + Get the end of cgroup cycles and system cycles at point, when this function is called. |
| 115 | + Example Usage: |
| 116 | + Call this at the end of |
| 117 | + actual_function_call_here_for_which_cpu_util_need_to_be_calculated() |
| 118 | + core.end_time() |
| 119 | + Initate the core class, with default limit if it fails to calculate. Ex. 3 |
| 120 | + core = cpuutilisation(3) |
| 121 | + Call this function before the start of function, you want to calculate cpu usage for |
| 122 | + core.start_time() |
| 123 | + actual_function_call_here_for_which_cpu_util_need_to_be_calculated() |
| 124 | + core.end_time() |
| 125 | + ''' |
| 126 | + |
| 127 | + self.cgrp_end_cycles = self.get_cpu_usage_cgrp() |
| 128 | + self.sys_end_cycles = self.get_cpu_usage_host() |
| 129 | + |
| 130 | +def main(): |
| 131 | + core = cpuutilisation(3) |
| 132 | + core.start_time() |
| 133 | + time.sleep(3) |
| 134 | + #execute function here |
| 135 | + core.end_time() |
| 136 | + errs, cores = core.get_core_utilisation() |
| 137 | + if errs is not None: |
| 138 | + for err in errs: |
| 139 | + print(err) |
| 140 | + print('\n Utilisation : ', cores) |
| 141 | + else: |
| 142 | + print('Utilisation : ', cores) |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments