Resource Usage Information using Python

PythonProgrammingServer Side Programming

To measure the UNIX resource usage, we need to use the resource module into our programs. This module also can control the resource utilization.

To use this module, we should import it using −

import resource

Resource Limits

In this module we can use the setrlimit() to limit the resource utilization. There are two parameters to limit the resources. These parameters are soft limit and the hard limit. The soft limit is basically the current limit, it can be changed over process, but it cannot exceed the hard limit. The hard limit can be reduced to any value above the soft limit, but cannot increase it.

There are some methods and constants related to resource limiting. These are.

Method resource.getrlimit(resource)

This method is used to return the soft and hard limits as a tuple. If the specified resource is invalid, it will raise ValueError.

Method resource.setrlimit(resource, limits)

This method is used to set limits to the resources. The limits can be assigned as a tuple of soft and hard limits. We can also use RLIM_INFINITY to make unlimited resource.

Method resource.prlimit(pid, resource[, limits])

This method is a combination of setrlimit() and getrlimit() methods. It can get and set the resource limits at the same time for an arbitrary process. When the pid is 0, then it works on the current process.

Some constants related to Resource Limits −

Sr.No. Constants & Description
1

RLIM_INFINITY

Limit for an Unlimited resource

2

RLIMIT_CORE

Max size of a core file created by current process.

3

RLIMIT_CPU

The Maximum processor time of a processor. When the limit exceeds, a SIGXCCPU signal is sent to the process.

4

RLIMIT_DATA

The maximum size of the processor’s heap

5

RLIMIT_STACK

The maximum size of call stack. It uses the stack of the main thread for a multithreaded process.

6

RLIMIT_NOFILE

Maximum number of Open file descriptor for the current process.

7

RLIMIT_MEMLOCK

The maximum address space for a locked memory

8

RLIMIT_NICE

The ceiling of the nice level of a process

9

RLIMIT_SWAP

The maximum size of the swap space

10

RLIMIT_NTPS

Maximum number of pseudo terminals in the system

Resource Usage

There are resource usage related methods and constants.

Method resource.getrusage(who)

This method is used to return an object used by current process or its children. It returns different fields. From the fields of return value, we can get information about how resource has been used.

Method resource.getpagesize()

This method is used to return number of bytes in a system page. It may not be same as the physical page size.

Some constants related to Resource Usage.

Sr.No. Constants & Description
1

RUSAGE_SELF

It helps to request resources consumed by calling process. It is sum of the all resources of different threads.

2

RUSAGE_CHILDREN

It helps to request resources consumed by the child process.

3

RUSAGE_BOTH

It helps to request resources consumed by calling process as well as the child process. It is not available for all systems

4

RUSAGE_THREAD

Resource usage by the current thread. It is not available for all systems

Example Code

import resource
res_limits = resource.getrusage(resource.RUSAGE_SELF)
print(res_limits)
print('Page Size: ' + str(resource.getpagesize()))
resource.setrlimit(resource.RLIMIT_CPU, (1, 2))
print('Resouce Limits: ' + str(resource.getrlimit(resource.RLIMIT_CPU)))
for a in range(1000):
   for b in range(1000):
      for c in range(1000):
         pass

Output

$ python3 example.py
resource.struct_rusage(ru_utime=0.035801, ru_stime=0.01074, ru_maxrss=9356, ru_ixrss=0, ru_idrss=0, 
ru_isrss=0, ru_minflt=1147, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, 
ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=0, ru_nivcsw=17)
Page Size: 4096
Resouce Limits: (1, 2)
CPU time limit exceeded (core dumped)
raja
Published on 31-Oct-2018 13:40:21
Advertisements