Skip to content

Commit

Permalink
cpu affinity: try to continue even if cpus seem busy
Browse files Browse the repository at this point in the history
- when the set of free cpus is empty, previous code died already when
  attempting to set the affinity in filter_available_cpus()
- warn but do not abort if auto-detection indicates that available cpus
  are already occupied. it could be zombies or debug instances..

 when the set of free cpus was empty
  • Loading branch information
il-steffen committed Oct 29, 2022
1 parent 8cb8219 commit 2019803
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 5 additions & 5 deletions kafl_fuzzer/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ def get_qemu_processes():
if (len(pids) > 0):
logger.warn(msg + " " + repr(pids))

# limit ourselves to CPUs not hogged by possible other qemu instances
# filter available CPUs by those with existing qemu instances
def filter_available_cpus():
def get_qemu_processes():
for proc in psutil.process_iter(['pid', 'name']):
if 'qemu-system-x86_64' in proc.info['name']:
yield (proc.info['pid'])

cpus = os.sched_getaffinity(0)
avail = os.sched_getaffinity(0)
used = set()
for pid in get_qemu_processes():
cpus -= os.sched_getaffinity(pid)
os.sched_setaffinity(0,cpus)
return cpus
used |= os.sched_getaffinity(pid)
return avail, used

# pretty-printed hexdump
def hexdump(src, length=16):
Expand Down
16 changes: 12 additions & 4 deletions kafl_fuzzer/manager/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import multiprocessing
import time
import os
import sys
import logging
from pprint import pformat
Expand Down Expand Up @@ -75,10 +76,17 @@ def start(config):
if not config.ip0:
logger.warn("No PT trace region defined.")

cpus = filter_available_cpus()
if num_worker > len(cpus):
logger.error(f"Requested {num_worker} but only {len(cpus)} free CPUs detected. Abort.")
return -1
avail, used = filter_available_cpus()
if num_worker > len(avail):
logger.error(f"Requested {num_worker} workers but only {len(avail)} vCPUs detected.")
return 1

# warn if we cannot limit ourselves to CPUs detected as free..
if num_worker + 1 >= len(avail-used):
logger.warn(f"Warning: Requested {num_worker} workers but {len(used)} out of {len(avail)} vCPUs seem busy?")
time.sleep(2)
else:
os.sched_setaffinity(0, avail-used)

manager = ManagerTask(config)

Expand Down

0 comments on commit 2019803

Please sign in to comment.