Skip to main content

Package to calculate cpu utilisation in cgroups

Project description

cgrputil

cgrputil is python library to calculate cpu utilisation of code over specific time interval inside docker container.

Usage of the library can vary, Eg. Think about the use case where you want to know how much specific function consume a cpu over specific time and on the basis of that you want to bill the client or draw charts to understand the growing trend of function.

I was struggling with the same problem, & searched all over internet. But i didnt get anything solid, & all of the docs, the repos i viewed, code i read, almost pointed to docker repo which had calculation formula. So seeing the problem & number of resources available for such typical problem i decided to create the library. I wanted solution which is very easy to use and focus on giving end result, so i tried it making as simple as i could. I hope this helps :) .

Installation

Use the package manager pip to install cgrputil.

pip install cgrputil

Usage

import cgrputil

'''
Class instantiation expects default param, which will be return in case of any failure 
while reading, calculating cpu usage. We suggest to send this value as maxm number of 
cores allocated to container.
This value can be pass in two ways either passing it while class instantiation, which is in 
example below i.e 3 or setting it inside env variable `RES_CPU_LIMIT`. 
Library will fist check for the value pass and then for the env variable, on of them is mandatory
else exception will be raised.
'''
cpu = cgrputil.cpuutilisation(3)


#From this point, it will calculate the cpu utilisation
cpu.start_time()

activity_you_want_to_calculate_cpu_usage()

#Until this point cpu usage will be calculated 
cpu.end_time()

'''
This will return the aprox cpu cores used while executing in between start time and end time.
Return format is float. So if it returns 2.3 that means above function have consumed 2.3 cores
out of all the cores available on the host or you can say 2.3 cores out of 3 
cores (3 assuming maximum cores allocated to container).

Function returns two values, first is list of errs or exception occured while calculating cpu usage.
Second the actual usage value. If the errs list is not empty it will most likely to return the 
default value passed while initiating the class. Ex. cpu = cgrputil.cpuutilisation(3) which is 3 here.
'''
errs, cores_used = cpu.get_core_utilisation()
if errs is not None:
    for err in errs:
        print(err)
    print('Utilisation : ', cores_used)        
else:
    print('Utilisation : ', cores_used)  

Contributing

Pull requests are welcome. For any changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cgrputil-1.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

cgrputil-1.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file cgrputil-1.0.1.tar.gz.

File metadata

  • Download URL: cgrputil-1.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.9

File hashes

Hashes for cgrputil-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3ccdd1f50be6da24e074e9c6ba88e1d312f43ede77adeed845d2a6b9fbc6b802
MD5 2387c929969315719cba6b5fbf63645f
BLAKE2b-256 be53f2646fd18b48ee5e56d2bc9284914872ac4c93a2d80a17bf6e9991b30bf9

See more details on using hashes here.

File details

Details for the file cgrputil-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: cgrputil-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.9

File hashes

Hashes for cgrputil-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c26959fec9418c40560596e15e6a7cf3696331e3d75f2a05d117778b26aaee0
MD5 db64f84d0548db97803668ae6237f25d
BLAKE2b-256 1efb0b373cefc1e0d5b9f5754330b05c15611b821cbbfa37c0e602935391740f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page