Utility Functions

This module contains utility functions for gathering CPU metrics

class monitormetrics.utils.utils.FileLock(protected_file_path, timeout=None, delay=1, lock_file_contents=None)[source]

A file locking mechanism that has context-manager support so you can use it in a with statement. This should be relatively cross compatible as it doesn’t rely on msvcrt or fcntl for the locking.

exception FileLockException[source]

Exception to the file lock object

acquire(blocking=True)[source]

Acquire the lock, if possible. If the lock is in use, and blocking is False, return False. Otherwise, check again every self.delay seconds until it either gets the lock or exceeds timeout number of seconds, in which case it raises an exception.

available()[source]

Returns True iff the file is currently available to be locked.

lock_exists()[source]

Returns True iff the external lockfile exists.

locked()[source]

Returns True iff the file is owned by THIS FileLock instance. (Even if this returns false, the file could be owned by another FileLock instance, possibly in a different thread or process).

purge()[source]

For debug purposes only. Removes the lock file from the hard disk.

release()[source]

Get rid of the lock by deleting the lockfile. When working in a with statement, this gets automatically called at the end.

class monitormetrics.utils.utils.PDF(config)[source]

custom PDF class that inherits from the FPDF

footer()[source]

This method defines footer of the pdf

header()[source]

This method defines header of the pdf

page_body(images)[source]

This method defines body of the pdf

print_page(images)[source]

This method add an empty pages and populates with images/text

monitormetrics.utils.utils.check_perf_events(perf_events)[source]

This function check if all perf groups are actually working. We will only probe the working counters during monitoring

Parameters

perf_events (dict) – A dict of found perf events

Returns

A dict of working perf events

Return type

dict

monitormetrics.utils.utils.dump_json(content, filename)[source]

This function appends data to an existing json content. It creates a new file if no existing file found.

Parameters
  • content (dict) – Dict to write into JSON format

  • filename (str) – Name of the file to load

monitormetrics.utils.utils.execute_cmd(cmd_str, handle_exception=True)[source]

Accept command string and returns output.

Parameters
  • cmd_str (str) – Command string to be executed

  • handle_exception (bool) – Handle exception manually. If set to false, raises an exception to the caller function

Returns

Output of the command. If command execution fails, returns ‘not_available’

Return type

str

Raises

subprocess.CalledProcessError – An error occurred in execution of command iff handle_exception is set to False

monitormetrics.utils.utils.execute_cmd_pipe(cmd_str)[source]

Accept command string and execute it using piping and returns process object.

Parameters

cmd_str (str) – Command string to be executed

Returns

Process object

Return type

object

monitormetrics.utils.utils.find_procs_by_name(name)[source]

Return a list of processes matching ‘name’

Parameters

name (str) – name of the process to find

Returns

List of psutil objects

Return type

list

monitormetrics.utils.utils.get_cpu_model_family(cpu)[source]

” This function gives CPU model and family ids from CPUID instruction

Parameters

cpu (object) – CPUID object

Returns

Family and model IDs

Return type

list

monitormetrics.utils.utils.get_cpu_model_names_for_non_x86()[source]

This function tries to extract the vendor, model and cpu architectures for non x86 machines like IBM POWER, ARM

Returns

Name of the vendor model name/number of the processor micro architecture of the processor

Return type

str

monitormetrics.utils.utils.get_cpu_vendor(cpu)[source]

This function gets the vendor name from CPUID instruction

Parameters

cpu (object) – CPUID object

Returns

Name of the vendor

Return type

str

monitormetrics.utils.utils.get_cpu_vendor_model_family()[source]

This function gets the name of CPU vendor, family and model parsed from CPUID instruction

Returns

Name of the vendor, CPU family and model ID

Return type

list

monitormetrics.utils.utils.get_mem_bw_event()[source]

This function returns the perf event to get memory bandwidth

Returns

A string to get memory bandwidth for perf stat command

Return type

str

monitormetrics.utils.utils.get_parser(cmd_output, reg='lscpu')[source]

Regex parser.

Parameters
  • cmd_output (str) – Output of the executed command

  • reg (str) – Regex pattern to be used

Returns

Function handle to parse the output

monitormetrics.utils.utils.get_perf_events()[source]

This function checks the micro architecture type and returns available perf events. Raises an exception if micro architecture is not implemented

Returns

Perf events with event name dict: Derived perf metrics from event counters

Return type

dict

Raises

PerfEventsNotFoundError – An error occurred while looking for perf events

monitormetrics.utils.utils.get_rapl_devices()[source]

This function gets all the packages, core, uncore and dram device available within RAPL powercap interface

Returns

A dict with package names and paths

Return type

dict

monitormetrics.utils.utils.get_value(input_dict, target)[source]

Find the value for a given target in dict

Parameters
  • input_dict (dict) – Dict to search for key

  • target (Any) – Key to search

Returns

List of values found in d

Return type

list

monitormetrics.utils.utils.ibstat_ports()[source]

This function returns Infiniband ports if present

Returns

A dict with IB port names and numbers

Return type

dict

monitormetrics.utils.utils.load_json(filename)[source]

This function loads json file and return dict

Parameters

filename (str) – Name of the file to load

Returns

File contents as dict

Return type

dict

monitormetrics.utils.utils.merge_dicts(exst_dict, new_dict)[source]

Merge two dicts. old_content is updated with data from new_content

Parameters
  • exst_dict (dict) – Existing data in the dict

  • new_dict (dict) – New data to be added to the dict

Returns

updated exst_dict with contents from new_dict

Return type

dict

monitormetrics.utils.utils.proc_if_running(procs)[source]

Check if all processes are running and returns a False if all of them are terminated

Parameters

procs (list) – List of psutil process objects

Returns

Running status of the processes

Return type

bool

monitormetrics.utils.utils.replace_negative(input_list)[source]

This function replaces the negative values in numpy array with mean of neighbours. If the values happen to be at the extremum, it replaces with preceding or succeding elements

Parameters

input_list (list) – A list with positive and/or negative elements

Returns

A list with just positive elements

Return type

list

monitormetrics.utils.utils.write_json(content, filename)[source]

This function writes json content to a file

Parameters
  • content (dict) – Dict to write into JSON format

  • filename (str) – Name of the file to load