Python helper functions to pin the current process to a single core and change the niceness of the process. These are necessary instead of just calling functions from the 'os' module because these can be called without needing to worry about whether or not the current system is compatible with these libraries.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: cody kochmann
# @Date: 2017-10-18
import os
'''
helper functions to pin the current process to a single core and change the
niceness of the process. These are necessary instead of just calling functions
from the 'os' module because these can be called without needing to worry about
whether or not the current system or python version can run with these calls.
'''
def pin_to_cpu(core_number):
''' pin the current process to a specific cpu to avoid dumping L1 cache'''
assert type(core_number) == int, 'pin_to_cpu needs an int as the argument'
# only run if os has sched_setaffinity and getpid available
try:
os.sched_setaffinity(os.getpid(), (core_number,))
except:
# just attempt this, it wont work on EVERY system in existence
pass
def renice(new_niceness):
''' renice the current process calling this function to the new input '''
assert type(new_niceness) == int, 'renice needs an int as its argument'
# maximum constraints
if new_niceness > 20:
new_niceness = 20
elif new_niceness < -20:
new_niceness = -20
try:
os.nice(new_niceness)
except:
# just attempt this, it wont work on EVERY system in existence
pass