summaryrefslogtreecommitdiffstats
path: root/src/svm/fifo_segment.h
blob: ee5c24d04bbd7d5f20ee370b331a2c568d19c7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef __included_fifo_segment_h__
#define __included_fifo_segment_h__

#include <svm/ssvm.h>
#include <svm/fifo_types.h>
#include <svm/svm_fifo.h>

typedef enum
{
  FIFO_SEGMENT_FTYPE_NONE = -1,
  FIFO_SEGMENT_RX_FIFO = 0,
  FIFO_SEGMENT_TX_FIFO,
  FIFO_SEGMENT_N_FTYPES
} fifo_segment_ftype_t;

#define FIFO_SEGMENT_MIN_LOG2_FIFO_SIZE 12	/**< 4kB min fifo size */
#define FIFO_SEGMENT_MIN_FIFO_SIZE 4096		/**< 4kB min fifo size */
#define FIFO_SEGMENT_MAX_FIFO_SIZE (2 << 30)	/**< 2GB max fifo size */
#define FIFO_SEGMENT_ALLOC_BATCH_SIZE 32	/* Allocation quantum */

typedef enum fifo_segment_flags_
{
  FIFO_SEGMENT_F_IS_PREALLOCATED = 1 << 0,
  FIFO_SEGMENT_F_WILL_DELETE = 1 << 1,
  FIFO_SEGMENT_F_MEM_LIMIT = 1 << 2,
} fifo_segment_flags_t;

#define foreach_segment_mem_status	\
_(NO_PRESSURE, "No pressure")		\
_(LOW_PRESSURE, "Low pressure")		\
_(HIGH_PRESSURE, "High pressure")	\
_(NO_MEMORY, "No memory")

typedef enum
{
#define _(sym,str)  MEMORY_PRESSURE_##sym,
  foreach_segment_mem_status
#undef _
    MEMORY_N_PRESSURE,
} fifo_segment_mem_status_t;

#if 0
typedef enum fifo_segment_mem_status_
{
  MEMORY_PRESSURE_NO_PRESSURE,
  MEMORY_PRESSURE_LOW_PRESSURE,
  MEMORY_PRESSURE_HIGH_PRESSURE,
  MEMORY_PRESSURE_NO_MEMORY,
} fifo_segment_mem_status_t;
#endif

typedef struct
{
  ssvm_private_t ssvm;		/**< ssvm segment data */
  fifo_segment_header_t *h;	/**< fifo segment data */
  u8 n_slices;			/**< number of fifo segment slices */
} fifo_segment_t;

typedef struct
{
  fifo_segment_t *segments;	/**< pool of fifo segments */
  uword next_baseva;		/**< Where to put the next one */
  u32 timeout_in_seconds;	/**< Time to wait during attach */
} fifo_segment_main_t;

typedef struct
{
  ssvm_segment_type_t segment_type;	/**< type of segment requested */
  u32 segment_size;			/**< size of the segment */
  int memfd_fd;				/**< fd for memfd segments */
  char *segment_name;			/**< segment name */
  u32 *new_segment_indices;		/**< return vec of new seg indices */
} fifo_segment_create_args_t;

#define fifo_segment_flags(_fs) _fs->h->flags

int fifo_segment_init (fifo_segment_t * fs);
int fifo_segment_create (fifo_segment_main_t * sm,
			 fifo_segment_create_args_t * a);
int fifo_segment_attach (fifo_segment_main_t * sm,
			 fifo_segment_create_args_t * a);
void fifo_segment_delete (fifo_segment_main_t * sm, fifo_segment_t * fs);
fifo_segment_t *fifo_segment_get_segment (fifo_segment_main_t * sm,
					  u32 fs_index);
u32 fifo_segment_index (fifo_segment_main_t * sm, fifo_segment_t * fs);
void fifo_segment_info (fifo_segment_t * seg, char **address, size_t * size);

/**
 * Allocate fifo in fifo segment
 *
 * @param fs		fifo segment for fifo
 * @param data_bytes	size of default fifo chunk in bytes
 * @param ftype		fifo type @ref fifo_segment_ftype_t
 * @return		new fifo or 0 if alloc failed
 */
svm_fifo_t *fifo_segment_alloc_fifo_w_slice (fifo_segment_t * fs,
					     u32 slice_index,
					     u32 data_bytes,
					     fifo_segment_ftype_t ftype);

/**
 * Free fifo allocated in fifo segment
 *
 * @param fs		fifo segment for fifo
 * @param f		fifo to be freed
 */
void fifo_segment_free_fifo (fifo_segment_t * fs, svm_fifo_t * f);

void fifo_segment_detach_fifo (fifo_segment_t * fs, svm_fifo_t * f);
void fifo_segment_attach_fifo (fifo_segment_t * fs, svm_fifo_t * f,
			       u32 slice_index);

/**
 * Try to preallocate fifo headers
 *
 * Tries to preallocate fifo headers and adds them to freelist.
 *
 * @param fs		fifo segment
 * @param batch_size	number of chunks to be allocated
 * @return		0 on success, negative number otherwise
 */
int fifo_segment_prealloc_fifo_hdrs (fifo_segment_t * fs, u32 slice_index,
				     u32 batch_size);

/**
 * Try to preallocate fifo chunks on segment
 *
 * Tries to preallocate chunks of requested size on segment and adds them
 * to chunk freelist.
 *
 * @param fs		fifo segment
 * @param chunk_size	size of chunks to be allocated in bytes
 * @param batch_size	number of chunks to be allocated
 * @return		0 on success, negative number otherwise
 */
int fifo_segment_prealloc_fifo_chunks (fifo_segment_t * fs, u32 slice_index,
				       u32 chunk_size, u32 batch_size);
/**
 * Pre-allocates fifo pairs in fifo segment
 *
 * The number of fifos pre-allocated is the minimum of the requested number
 * of pairs and the maximum number that fit within the segment. If the maximum
 * is hit, the number of fifo pairs requested is updated by subtracting the
 * number of fifos that have been successfully allocated.
 *
 * @param fs		fifo segment for fifo
 * @param rx_fifo_size	data size of rx fifos
 * @param tx_fifo_size	data size of tx fifos
 * @param n_fifo_pairs	number of pairs requested. Prior to returning, this
 * 			is decremented by the the number of pairs allocated.
 */
void fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
					  u32 rx_fifo_size,
					  u32 tx_fifo_size,
					  u32 * n_fifo_pairs);

/**
 * Allocate chunks in fifo segment
 *
 * @param fsh		fifo segment header
 * @param slice_index	slice where chunks should be alocated
 * @param chunk_size	chunk size needed
 * @return		chunk (or chunks) that cover at least chunk_size bytes
 * 			on success, 0 on failure.
 */
svm_fifo_chunk_t *fsh_alloc_chunk (fifo_segment_header_t * fsh,
				   u32 slice_index, u32 chunk_size);

/**
 * Return chunks to fifo segment
 *
 * @param fsh		fifo segment header
 * @param slice_index	slice where chunks should be returned
 * @param c		pointer to first chunk in 0 terminated linked list
 */
void fsh_collect_chunks (fifo_segment_header_t * fsh, u32 slice_index,
			 svm_fifo_chunk_t * c);

/**
 * Fifo segment has reached mem limit
 *
 * @param fsh           fifo segment header
 * @return              1 (if reached) or 0 (otherwise)
 */
u8 fsh_has_reached_mem_limit (fifo_segment_header_t * fsh);

/**
 * Fifo segment reset mem limit flag
 *
 * @param fs            fifo segment
 */
void fsh_reset_mem_limit (fifo_segment_header_t * fsh);

/**
 * Fifo segment allocated size
 *
 * Returns fifo segment's allocated size
 *
 * @param fs            fifo segment
 * @return              allocated size in bytes
 */
uword fifo_segment_size (fifo_segment_t * fs);

/**
 * Fifo segment estimate of number of free bytes
 *
 * Returns fifo segment's internal estimate of the number of free bytes.
 * To force a synchronization between the segment and the underlying
 * memory allocator, call @ref fifo_segment_update_free_bytes
 *
 * @param fs		fifo segment
 * @return		free bytes estimate
 */
uword fifo_segment_free_bytes (fifo_segment_t * fs);

/**
 * Update fifo segment free bytes estimate
 *
 * Forces fifo segment free bytes estimate synchronization with underlying
 * memory allocator.
 *
 * @param fs		fifo segment
 */
void fifo_segment_update_free_bytes (fifo_segment_t * fs);

/**
 * Fifo segment number of cached bytes
 *
 * Returns fifo segment's number of cached bytes.
 *
 * @param fs            fifo segment
 * @return              cached bytes
 */
uword fifo_segment_cached_bytes (fifo_segment_t * fs);

uword fifo_segment_available_bytes (fifo_segment_t * fs);

/**
 * Number of bytes on chunk free lists
 *
 * @param fs		fifo segment
 * @return		free bytes on chunk free lists
 */
uword fifo_segment_fl_chunk_bytes (fifo_segment_t * fs);
u8 fifo_segment_has_fifos (fifo_segment_t * fs);
svm_fifo_t *fifo_segment_get_slice_fifo_list (fifo_segment_t * fs,
					      u32 slice_index);
u32 fifo_segment_num_fifos (fifo_segment_t * fs);
u32 fifo_segment_num_free_fifos (fifo_segment_t * fs);
/**
 * Find number of free chunks of given size
 *
 * @param fs	fifo segment
 * @param size	chunk size of interest or ~0 if all should be counted
 * @return	number of chunks of given size
 */
u32 fifo_segment_num_free_chunks (fifo_segment_t * fs, u32 size);

u8 fifo_segment_get_mem_usage (fifo_segment_t * fs);
fifo_segment_mem_status_t fifo_segment_determine_status
  (fifo_segment_header_t * fsh, u8 usage);
fifo_segment_mem_status_t fifo_segment_get_mem_status (fifo_segment_t * fs);

void fifo_segment_main_init (fifo_segment_main_t * sm, u64 baseva,
			     u32 timeout_in_seconds);

format_function_t format_fifo_segment;
format_function_t format_fifo_segment_type;

#endif /* __included_fifo_segment_h__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
#!/usr/bin/env python3

import sys
import shutil
import os
import fnmatch
import unittest
import time
import threading
import traceback
import signal
import re
from multiprocessing import Process, Pipe, get_context
from multiprocessing.queues import Queue
from multiprocessing.managers import BaseManager
from config import config, num_cpus, available_cpus, max_vpp_cpus
from framework import (
    VppTestRunner,
    VppTestCase,
    get_testcase_doc_name,
    get_test_description,
)
from test_result_code import TestResultCode
from debug import spawn_gdb
from log import (
    get_parallel_logger,
    double_line_delim,
    RED,
    YELLOW,
    GREEN,
    colorize,
    single_line_delim,
)
from discover_tests import discover_tests
import sanity_run_vpp
from subprocess import check_output, CalledProcessError
from util import check_core_path, get_core_path, is_core_present

# timeout which controls how long the child has to finish after seeing
# a core dump in test temporary directory. If this is exceeded, parent assumes
# that child process is stuck (e.g. waiting for event from vpp) and kill
# the child
core_timeout = 3


class StreamQueue(Queue):
    def write(self, msg):
        self.put(msg)

    def flush(self):
        sys.__stdout__.flush()
        sys.__stderr__.flush()

    def fileno(self):
        return self._writer.fileno()


class StreamQueueManager(BaseManager):
    pass


StreamQueueManager.register("StreamQueue", StreamQueue)


class TestResult(dict):
    def __init__(self, testcase_suite, testcases_by_id=None):
        super(TestResult, self).__init__()
        for trc in list(TestResultCode):
            self[trc] = []
        self.crashed = False
        self.testcase_suite = testcase_suite
        self.testcases = [testcase for testcase in testcase_suite]
        self.testcases_by_id = testcases_by_id

    def was_successful(self):
        return (
            0
            == len(self[TestResultCode.FAIL])
            == len(self[TestResultCode.ERROR])
            == len(self[TestResultCode.UNEXPECTED_PASS])
            and len(self[TestResultCode.PASS])
            + len(self[TestResultCode.SKIP])
            + len(self[TestResultCode.SKIP_CPU_SHORTAGE])
            + len(self[TestResultCode.EXPECTED_FAIL])
            == self.testcase_suite.countTestCases()
        )

    def no_tests_run(self):
        return 0 == len(self[TestResultCode.TEST_RUN])

    def process_result(self, test_id, result):
        self[result].append(test_id)

    def suite_from_failed(self):
        rerun_ids = set([])
        for testcase in self.testcase_suite:
            tc_id = testcase.id()
            if (
                tc_id
                not in self[TestResultCode.PASS]
                + self[TestResultCode.SKIP]
                + self[TestResultCode.SKIP_CPU_SHORTAGE]
                + self[TestResultCode.EXPECTED_FAIL]
            ):
                rerun_ids.add(tc_id)
        if rerun_ids:
            return suite_from_failed(self.testcase_suite, rerun_ids)

    def get_testcase_names(self, test_id):
        # could be tearDownClass (test_ipsec_esp.TestIpsecEsp1)
        setup_teardown_match = re.match(
            r"((tearDownClass)|(setUpClass)) \((.+\..+)\)", test_id
        )
        if setup_teardown_match:
            test_name, _, _, testcase_name = setup_teardown_match.groups()
            if len(testcase_name.split(".")) == 2:
                for key in self.testcases_by_id.keys():
                    if key.startswith(testcase_name):
                        testcase_name = key
                        break
            testcase_name = self._get_testcase_doc_name(testcase_name)
        else:
            test_name = self._get_test_description(test_id)
            testcase_name = self._get_testcase_doc_name(test_id)

        return testcase_name, test_name

    def _get_test_description(self, test_id):
        if test_id in self.testcases_by_id:
            desc = get_test_description(descriptions, self.testcases_by_id[test_id])
        else:
            desc = test_id
        return desc

    def _get_testcase_doc_name(self, test_id):
        if test_id in self.testcases_by_id:
            doc_name = get_testcase_doc_name(self.testcases_by_id[test_id])
        else:
            doc_name = test_id
        return doc_name


def test_runner_wrapper(
    suite, keep_alive_pipe, stdouterr_queue, finished_pipe, result_pipe, logger
):
    sys.stdout = stdouterr_queue
    sys.stderr = stdouterr_queue
    VppTestCase.parallel_handler = logger.handlers[0]
    result = VppTestRunner(
        keep_alive_pipe=keep_alive_pipe,
        descriptions=descriptions,
        verbosity=config.verbose,
        result_pipe=result_pipe,
        failfast=config.failfast,
        print_summary=False,
    ).run(suite)
    finished_pipe.send(result.wasSuccessful())
    finished_pipe.close()
    keep_alive_pipe.close()


class TestCaseWrapper(object):
    def __init__(self, testcase_suite, manager):
        self.keep_alive_parent_end, self.keep_alive_child_end = Pipe(duplex=False)
        self.finished_parent_end, self.finished_child_end = Pipe(duplex=False)
        self.result_parent_end, self.result_child_end = Pipe(duplex=False)
        self.testcase_suite = testcase_suite
        self.stdouterr_queue = manager.StreamQueue(ctx=get_context())
        self.logger = get_parallel_logger(self.stdouterr_queue)
        self.child = Process(
            target=test_runner_wrapper,
            args=(
                testcase_suite,
                self.keep_alive_child_end,
                self.stdouterr_queue,
                self.finished_child_end,
                self.result_child_end,
                self.logger,
            ),
        )
        self.child.start()
        self.last_test_temp_dir = None
        self.last_test_vpp_binary = None
        self._last_test = None
        self.last_test_id = None
        self.vpp_pid = None
        self.last_heard = time.time()
        self.core_detected_at = None
        self.testcases_by_id = {}
        self.testclasess_with_core = {}
        for testcase in self.testcase_suite:
            self.testcases_by_id[testcase.id()] = testcase
        self.result = TestResult(testcase_suite, self.testcases_by_id)

    @property
    def last_test(self):
        return self._last_test

    @last_test.setter
    def last_test(self, test_id):
        self.last_test_id = test_id
        if test_id in self.testcases_by_id:
            testcase = self.testcases_by_id[test_id]
            self._last_test = testcase.shortDescription()
            if not self._last_test:
                self._last_test = str(testcase)
        else:
            self._last_test = test_id

    def add_testclass_with_core(self):
        if self.last_test_id in self.testcases_by_id:
            test = self.testcases_by_id[self.last_test_id]
            class_name = unittest.util.strclass(test.__class__)
            test_name = "'{}' ({})".format(
                get_test_description(descriptions, test), self.last_test_id
            )
        else:
            test_name = self.last_test_id
            class_name = re.match(
                r"((tearDownClass)|(setUpClass)) " r"\((.+\..+)\)", test_name
            ).groups()[3]
        if class_name not in self.testclasess_with_core:
            self.testclasess_with_core[class_name] = (
                test_name,
                self.last_test_vpp_binary,
                self.last_test_temp_dir,
            )

    def close_pipes(self):
        self.keep_alive_child_end.close()
        self.finished_child_end.close()
        self.result_child_end.close()
        self.keep_alive_parent_end.close()
        self.finished_parent_end.close()
        self.result_parent_end.close()

    def was_successful(self):
        return self.result.was_successful()

    @property
    def cpus_used(self):
        return self.testcase_suite.cpus_used

    def get_assigned_cpus(self):
        return self.testcase_suite.get_assigned_cpus()


def stdouterr_reader_wrapper(
    unread_testcases, finished_unread_testcases, read_testcases
):
    read_testcase = None
    while read_testcases.is_set() or unread_testcases:
        if finished_unread_testcases:
            read_testcase = finished_unread_testcases.pop()
            unread_testcases.remove(read_testcase)
        elif unread_testcases:
            read_testcase = unread_testcases.pop()
        if read_testcase:
            data = ""
            while data is not None:
                sys.stdout.write(data)
                data = read_testcase.stdouterr_queue.get()

            read_testcase.stdouterr_queue.close()
            finished_unread_testcases.discard(read_testcase)
            read_testcase = None


def handle_failed_suite(logger, last_test_temp_dir, vpp_pid, vpp_binary):
    if last_test_temp_dir:
        # Need to create link in case of a timeout or core dump without failure
        lttd = os.path.basename(last_test_temp_dir)
        link_path = os.path.join(config.failed_dir, f"{lttd}-FAILED")
        if not os.path.exists(link_path):
            os.symlink(last_test_temp_dir, link_path)
        logger.error(
            "Symlink to failed testcase directory: %s -> %s" % (link_path, lttd)
        )

        # Report core existence
        core_path = get_core_path(last_test_temp_dir)
        if os.path.exists(core_path):
            logger.error(
                "Core-file exists in test temporary directory: %s!" % core_path
            )
            check_core_path(logger, core_path)
            logger.debug("Running 'file %s':" % core_path)
            try:
                info = check_output(["file", core_path])
                logger.debug(info)
            except CalledProcessError as e:
                logger.error(
                    "Subprocess returned with return code "
                    "while running `file' utility on core-file "
                    "returned: "
                    "rc=%s",
                    e.returncode,
                )
            except OSError as e:
                logger.error(
                    "Subprocess returned with OS error while "
                    "running 'file' utility "
                    "on core-file: "
                    "(%s) %s",
                    e.errno,
                    e.strerror,
                )
            except Exception as e:
                logger.exception("Unexpected error running `file' utility on core-file")
            logger.error(f"gdb {vpp_binary} {core_path}")

    if vpp_pid:
        # Copy api post mortem
        api_post_mortem_path = "/tmp/api_post_mortem.%d" % vpp_pid
        if os.path.isfile(api_post_mortem_path):
            logger.error(
                "Copying api_post_mortem.%d to %s" % (vpp_pid, last_test_temp_dir)
            )
            shutil.copy2(api_post_mortem_path, last_test_temp_dir)


def check_and_handle_core(vpp_binary, tempdir, core_crash_test):
    if is_core_present(tempdir):
        if debug_core:
            print(
                "VPP core detected in %s. Last test running was %s"
                % (tempdir, core_crash_test)
            )
            print(single_line_delim)
            spawn_gdb(vpp_binary, get_core_path(tempdir))
            print(single_line_delim)
        elif config.compress_core:
            print("Compressing core-file in test directory `%s'" % tempdir)
            os.system("gzip %s" % get_core_path(tempdir))


def handle_cores(failed_testcases):
    for failed_testcase in failed_testcases:
        tcs_with_core = failed_testcase.testclasess_with_core
        if tcs_with_core:
            for test, vpp_binary, tempdir in tcs_with_core.values():
                check_and_handle_core(vpp_binary, tempdir, test)


def process_finished_testsuite(
    wrapped_testcase_suite, finished_testcase_suites, failed_wrapped_testcases, results
):
    results.append(wrapped_testcase_suite.result)
    finished_testcase_suites.add(wrapped_testcase_suite)
    stop_run = False
    if config.failfast and not wrapped_testcase_suite.was_successful():
        stop_run = True

    if not wrapped_testcase_suite.was_successful():
        failed_wrapped_testcases.add(wrapped_testcase_suite)
        handle_failed_suite(
            wrapped_testcase_suite.logger,
            wrapped_testcase_suite.last_test_temp_dir,
            wrapped_testcase_suite.vpp_pid,
            wrapped_testcase_suite.last_test_vpp_binary,
        )

    return stop_run


def run_forked(testcase_suites):
    wrapped_testcase_suites = set()
    solo_testcase_suites = []

    # suites are unhashable, need to use list
    results = []
    unread_testcases = set()
    finished_unread_testcases = set()
    manager = StreamQueueManager()
    manager.start()
    tests_running = 0
    free_cpus = list(available_cpus)

    def on_suite_start(tc):
        nonlocal tests_running
        nonlocal free_cpus
        tests_running = tests_running + 1

    def on_suite_finish(tc):
        nonlocal tests_running
        nonlocal free_cpus
        tests_running = tests_running - 1
        assert tests_running >= 0
        free_cpus.extend(tc.get_assigned_cpus())

    def run_suite(suite):
        nonlocal manager
        nonlocal wrapped_testcase_suites
        nonlocal unread_testcases
        nonlocal free_cpus
        suite.assign_cpus(free_cpus[: suite.cpus_used])
        free_cpus = free_cpus[suite.cpus_used :]
        wrapper = TestCaseWrapper(suite, manager)
        wrapped_testcase_suites.add(wrapper)
        unread_testcases.add(wrapper)
        on_suite_start(suite)

    def can_run_suite(suite):
        return tests_running < max_concurrent_tests and (
            suite.cpus_used <= len(free_cpus) or suite.cpus_used > max_vpp_cpus
        )

    while free_cpus and testcase_suites:
        a_suite = testcase_suites[0]
        if a_suite.is_tagged_run_solo:
            a_suite = testcase_suites.pop(0)
            solo_testcase_suites.append(a_suite)
            continue
        if can_run_suite(a_suite):
            a_suite = testcase_suites.pop(0)
            run_suite(a_suite)
        else:
            break

    if tests_running == 0 and solo_testcase_suites:
        a_suite = solo_testcase_suites.pop(0)
        run_suite(a_suite)

    read_from_testcases = threading.Event()
    read_from_testcases.set()
    stdouterr_thread = threading.Thread(
        target=stdouterr_reader_wrapper,
        args=(unread_testcases, finished_unread_testcases, read_from_testcases),
    )
    stdouterr_thread.start()

    failed_wrapped_testcases = set()
    stop_run = False

    try:
        while wrapped_testcase_suites or testcase_suites:
            finished_testcase_suites = set()
            for wrapped_testcase_suite in wrapped_testcase_suites:
                while wrapped_testcase_suite.result_parent_end.poll():
                    wrapped_testcase_suite.result.process_result(
                        *wrapped_testcase_suite.result_parent_end.recv()
                    )
                    wrapped_testcase_suite.last_heard = time.time()

                while wrapped_testcase_suite.keep_alive_parent_end.poll():
                    (
                        wrapped_testcase_suite.last_test,
                        wrapped_testcase_suite.last_test_vpp_binary,
                        wrapped_testcase_suite.last_test_temp_dir,
                        wrapped_testcase_suite.vpp_pid,
                    ) = wrapped_testcase_suite.keep_alive_parent_end.recv()
                    wrapped_testcase_suite.last_heard = time.time()

                if wrapped_testcase_suite.finished_parent_end.poll():
                    wrapped_testcase_suite.finished_parent_end.recv()
                    wrapped_testcase_suite.last_heard = time.time()
                    stop_run = (
                        process_finished_testsuite(
                            wrapped_testcase_suite,
                            finished_testcase_suites,
                            failed_wrapped_testcases,
                            results,
                        )
                        or stop_run
                    )
                    continue

                fail = False
                if wrapped_testcase_suite.last_heard + config.timeout < time.time():
                    fail = True
                    wrapped_testcase_suite.logger.critical(
                        "Child test runner process timed out "
                        "(last test running was `%s' in `%s')!"
                        % (
                            wrapped_testcase_suite.last_test,
                            wrapped_testcase_suite.last_test_temp_dir,
                        )
                    )
                elif not wrapped_testcase_suite.child.is_alive():
                    fail = True
                    wrapped_testcase_suite.logger.critical(
                        "Child test runner process unexpectedly died "
                        "(last test running was `%s' in `%s')!"
                        % (
                            wrapped_testcase_suite.last_test,
                            wrapped_testcase_suite.last_test_temp_dir,
                        )
                    )
                elif (
                    wrapped_testcase_suite.last_test_temp_dir
                    and wrapped_testcase_suite.last_test_vpp_binary
                ):
                    if is_core_present(wrapped_testcase_suite.last_test_temp_dir):
                        wrapped_testcase_suite.add_testclass_with_core()
                        if wrapped_testcase_suite.core_detected_at is None:
                            wrapped_testcase_suite.core_detected_at = time.time()
                        elif (
                            wrapped_testcase_suite.core_detected_at + core_timeout
                            < time.time()
                        ):
                            wrapped_testcase_suite.logger.critical(
                                "Child test runner process unresponsive and "
                                "core-file exists in test temporary directory "
                                "(last test running was `%s' in `%s')!"
                                % (
                                    wrapped_testcase_suite.last_test,
                                    wrapped_testcase_suite.last_test_temp_dir,
                                )
                            )
                            fail = True

                if fail:
                    wrapped_testcase_suite.child.terminate()
                    try:
                        # terminating the child process tends to leave orphan
                        # VPP process around
                        if wrapped_testcase_suite.vpp_pid:
                            os.kill(wrapped_testcase_suite.vpp_pid, signal.SIGTERM)
                    except OSError:
                        # already dead
                        pass
                    wrapped_testcase_suite.result.crashed = True
                    wrapped_testcase_suite.result.process_result(
                        wrapped_testcase_suite.last_test_id, TestResultCode.ERROR
                    )
                    stop_run = (
                        process_finished_testsuite(
                            wrapped_testcase_suite,
                            finished_testcase_suites,
                            failed_wrapped_testcases,
                            results,
                        )
                        or stop_run
                    )

            for finished_testcase in finished_testcase_suites:
                # Somewhat surprisingly, the join below may
                # timeout, even if client signaled that
                # it finished - so we note it just in case.
                join_start = time.time()
                finished_testcase.child.join(test_finished_join_timeout)
                join_end = time.time()
                if join_end - join_start >= test_finished_join_timeout:
                    finished_testcase.logger.error(
                        "Timeout joining finished test: %s (pid %d)"
                        % (finished_testcase.last_test, finished_testcase.child.pid)
                    )
                finished_testcase.close_pipes()
                wrapped_testcase_suites.remove(finished_testcase)
                finished_unread_testcases.add(finished_testcase)
                finished_testcase.stdouterr_queue.put(None)
                on_suite_finish(finished_testcase)
                if stop_run:
                    while testcase_suites:
                        results.append(TestResult(testcase_suites.pop(0)))
                elif testcase_suites:
                    a_suite = testcase_suites[0]
                    while a_suite and a_suite.is_tagged_run_solo:
                        testcase_suites.pop(0)
                        solo_testcase_suites.append(a_suite)
                        if testcase_suites:
                            a_suite = testcase_suites[0]
                        else:
                            a_suite = None
                    if a_suite and can_run_suite(a_suite):
                        testcase_suites.pop(0)
                        run_suite(a_suite)
                if solo_testcase_suites and tests_running == 0:
                    a_suite = solo_testcase_suites.pop(0)
                    run_suite(a_suite)
            time.sleep(0.1)
    except Exception:
        for wrapped_testcase_suite in wrapped_testcase_suites:
            wrapped_testcase_suite.child.terminate()
            wrapped_testcase_suite.stdouterr_queue.put(None)
        raise
    finally:
        read_from_testcases.clear()
        stdouterr_thread.join(config.timeout)
        manager.shutdown()

    handle_cores(failed_wrapped_testcases)
    return results


class TestSuiteWrapper(unittest.TestSuite):
    cpus_used = 0

    def __init__(self):
        return super().__init__()

    def addTest(self, test):
        self.cpus_used = max(self.cpus_used, test.get_cpus_required())
        super().addTest(test)

    def assign_cpus(self, cpus):
        self.cpus = cpus

    def _handleClassSetUp(self, test, result):
        if not test.__class__.skipped_due_to_cpu_lack:
            test.assign_cpus(self.cpus)
        super()._handleClassSetUp(test, result)

    def get_assigned_cpus(self):
        return self.cpus


class SplitToSuitesCallback:
    def __init__(self, filter_callback):
        self.suites = {}
        self.suite_name = "default"
        self.filter_callback = filter_callback
        self.filtered = TestSuiteWrapper()

    def __call__(self, file_name, cls, method):
        test_method = cls(method)
        if self.filter_callback(file_name, cls.__name__, method):
            self.suite_name = file_name + cls.__name__
            if self.suite_name not in self.suites:
                self.suites[self.suite_name] = TestSuiteWrapper()
                self.suites[self.suite_name].is_tagged_run_solo = False
            self.suites[self.suite_name].addTest(test_method)
            if test_method.is_tagged_run_solo():
                self.suites[self.suite_name].is_tagged_run_solo = True

        else:
            self.filtered.addTest(test_method)


def parse_test_filter(test_filter):
    f = test_filter
    filter_file_name = None
    filter_class_name = None
    filter_func_name = None
    if f:
        if "." in f:
            parts = f.split(".")
            if len(parts) > 3:
                raise Exception(f"Invalid test filter: {test_filter}")
            if len(parts) > 2:
                if parts[2] not in ("*", ""):
                    filter_func_name = parts[2]
            if parts[1] not in ("*", ""):
                filter_class_name = parts[1]
            if parts[0] not in ("*", ""):
                if parts[0].startswith("test_"):
                    filter_file_name = parts[0]
                else:
                    filter_file_name = "test_%s" % parts[0]
        else:
            if f.startswith("test_"):
                filter_file_name = f
            else:
                filter_file_name = "test_%s" % f
    if filter_file_name:
        filter_file_name = "%s.py" % filter_file_name
    return filter_file_name, filter_class_name, filter_func_name


def filter_tests(tests, filter_cb):
    result = TestSuiteWrapper()
    for t in tests:
        if isinstance(t, unittest.suite.TestSuite):
            # this is a bunch of tests, recursively filter...
            x = filter_tests(t, filter_cb)
            if x.countTestCases() > 0:
                result.addTest(x)
        elif isinstance(t, unittest.TestCase):
            # this is a single test
            parts = t.id().split(".")
            # t.id() for common cases like this:
            # test_classifier.TestClassifier.test_acl_ip
            # apply filtering only if it is so
            if len(parts) == 3:
                if not filter_cb(parts[0], parts[1], parts[2]):
                    continue
            result.addTest(t)
        else:
            # unexpected object, don't touch it
            result.addTest(t)
    return result


class FilterByTestOption:
    def __init__(self, filters):
        self.filters = filters

    def __call__(self, file_name, class_name, func_name):
        def test_one(
            filter_file_name,
            filter_class_name,
            filter_func_name,
            file_name,
            class_name,
            func_name,
        ):
            if filter_file_name:
                fn_match = fnmatch.fnmatch(file_name, filter_file_name)
                if not fn_match:
                    return False
            if filter_class_name and class_name != filter_class_name:
                return False
            if filter_func_name and func_name != filter_func_name:
                return False
            return True

        for filter_file_name, filter_class_name, filter_func_name in self.filters:
            if test_one(
                filter_file_name,
                filter_class_name,
                filter_func_name,
                file_name,
                class_name,
                func_name,
            ):
                return True

        return False


class FilterByClassList:
    def __init__(self, classes_with_filenames):
        self.classes_with_filenames = classes_with_filenames

    def __call__(self, file_name, class_name, func_name):
        return ".".join([file_name, class_name]) in self.classes_with_filenames


def suite_from_failed(suite, failed):
    failed = {x.rsplit(".", 1)[0] for x in failed}
    filter_cb = FilterByClassList(failed)
    suite = filter_tests(suite, filter_cb)
    return suite


class AllResults(dict):
    def __init__(self):
        super(AllResults, self).__init__()
        self.all_testcases = 0
        self.results_per_suite = []
        for trc in list(TestResultCode):
            self[trc] = 0
        self.rerun = []
        self.testsuites_no_tests_run = []

    def add_results(self, result):
        self.results_per_suite.append(result)
        for trc in list(TestResultCode):
            self[trc] += len(result[trc])

    def add_result(self, result):
        retval = 0
        self.all_testcases += result.testcase_suite.countTestCases()
        self.add_results(result)

        if result.no_tests_run():
            self.testsuites_no_tests_run.append(result.testcase_suite)
            if result.crashed:
                retval = -1
            else:
                retval = 1
        elif not result.was_successful():
            retval = 1

        if retval != 0:
            self.rerun.append(result.testcase_suite)

        return retval

    def print_results(self):
        print("")
        print(double_line_delim)
        print("TEST RESULTS:")

        def indent_results(lines):
            lines = list(filter(None, lines))
            maximum = max(lines, key=lambda x: x.index(":"))
            maximum = 4 + maximum.index(":")
            for l in lines:
                padding = " " * (maximum - l.index(":"))
                print(f"{padding}{l}")

        indent_results(
            [
                f"Scheduled tests: {self.all_testcases}",
                f"Executed tests: {self[TestResultCode.TEST_RUN]}",
                f"Passed tests: {colorize(self[TestResultCode.PASS], GREEN)}",
                f"Expected failures: {colorize(self[TestResultCode.EXPECTED_FAIL], GREEN)}"
                if self[TestResultCode.EXPECTED_FAIL]
                else None,
                f"Skipped tests: {colorize(self[TestResultCode.SKIP], YELLOW)}"
                if self[TestResultCode.SKIP]
                else None,
                f"Not Executed tests: {colorize(self.not_executed, RED)}"
                if self.not_executed
                else None,
                f"Failures: {colorize(self[TestResultCode.FAIL], RED)}"
                if self[TestResultCode.FAIL]
                else None,
                f"Unexpected passes: {colorize(self[TestResultCode.UNEXPECTED_PASS], RED)}"
                if self[TestResultCode.UNEXPECTED_PASS]
                else None,
                f"Errors: {colorize(self[TestResultCode.ERROR], RED)}"
                if self[TestResultCode.ERROR]
                else None,
                "Tests skipped due to lack of CPUS: "
                f"{colorize(self[TestResultCode.SKIP_CPU_SHORTAGE], YELLOW)}"
                if self[TestResultCode.SKIP_CPU_SHORTAGE]
                else None,
            ]
        )

        if self.all_failed > 0:
            print("FAILURES AND ERRORS IN TESTS:")
            for result in self.results_per_suite:
                old_testcase_name = None
                for tr_code, headline in (
                    (TestResultCode.FAIL, "FAILURE"),
                    (TestResultCode.ERROR, "ERROR"),
                    (TestResultCode.UNEXPECTED_PASS, "UNEXPECTED PASS"),
                ):
                    if not result[tr_code]:
                        continue

                    for failed_test_id in result[tr_code]:
                        new_testcase_name, test_name = result.get_testcase_names(
                            failed_test_id
                        )
                        if new_testcase_name != old_testcase_name:
                            print(
                                f"  Testcase name: {colorize(new_testcase_name, RED)}"
                            )
                            old_testcase_name = new_testcase_name
                        print(
                            f"    {headline}: {colorize(test_name, RED)} [{failed_test_id}]"
                        )

        if self.testsuites_no_tests_run:
            print("TESTCASES WHERE NO TESTS WERE SUCCESSFULLY EXECUTED:")
            tc_classes = set()
            for testsuite in self.testsuites_no_tests_run:
                for testcase in testsuite:
                    tc_classes.add(get_testcase_doc_name(testcase))
            for tc_class in tc_classes:
                print("  {}".format(colorize(tc_class, RED)))

        if self[TestResultCode.SKIP_CPU_SHORTAGE]:
            print()
            print(
                colorize(
                    "     SOME TESTS WERE SKIPPED BECAUSE THERE ARE NOT"
                    " ENOUGH CPUS AVAILABLE",
                    YELLOW,
                )
            )
        print(double_line_delim)
        print("")

    @property
    def not_executed(self):
        return self.all_testcases - self[TestResultCode.TEST_RUN]

    @property
    def all_failed(self):
        return (
            self[TestResultCode.FAIL]
            + self[TestResultCode.ERROR]
            + self[TestResultCode.UNEXPECTED_PASS]
        )


def parse_results(results):
    """
    Prints the number of scheduled, executed, not executed, passed, failed,
    errored and skipped tests and details about failed and errored tests.

    Also returns all suites where any test failed.

    :param results:
    :return:
    """

    results_per_suite = AllResults()
    crashed = False
    failed = False
    for result in results:
        result_code = results_per_suite.add_result(result)
        if result_code == 1:
            failed = True
        elif result_code == -1:
            crashed = True

    results_per_suite.print_results()

    if crashed:
        return_code = -1
    elif failed:
        return_code = 1
    else:
        return_code = 0
    return return_code, results_per_suite.rerun


if __name__ == "__main__":
    print(f"Config is: {config}")

    if config.sanity:
        print("Running sanity test case.")
        try:
            rc = sanity_run_vpp.main()
            if rc != 0:
                sys.exit(rc)
        except Exception as e:
            print(traceback.format_exc())
            print("Couldn't run sanity test case.")
            sys.exit(-1)

    test_finished_join_timeout = 15

    debug_gdb = config.debug in ["gdb", "gdbserver", "attach"]
    debug_core = config.debug == "core"

    run_interactive = debug_gdb or config.step or config.force_foreground

    max_concurrent_tests = 0
    print(f"OS reports {num_cpus} available cpu(s).")

    test_jobs = config.jobs
    if test_jobs == "auto":
        if run_interactive:
            max_concurrent_tests = 1
            print("Interactive mode required, running tests consecutively.")
        else:
            max_concurrent_tests = num_cpus
            print(
                f"Running at most {max_concurrent_tests} python test "
                "processes concurrently."
            )
    else:
        max_concurrent_tests = test_jobs
        print(
            f"Running at most {max_concurrent_tests} python test processes "
            "concurrently as set by 'TEST_JOBS'."
        )

    print(f"Using at most {max_vpp_cpus} cpus for VPP threads.")

    if run_interactive and max_concurrent_tests > 1:
        raise NotImplementedError(
            "Running tests interactively (DEBUG is gdb[server] or ATTACH or "
            "STEP is set) in parallel (TEST_JOBS is more than 1) is not "
            "supported"
        )

    descriptions = True

    print("Running tests using custom test runner.")
    filters = [(parse_test_filter(f)) for f in config.filter.split(",")]

    print(
        "Selected filters: ",
        "|".join(
            f"file={filter_file}, class={filter_class}, function={filter_func}"
            for filter_file, filter_class, filter_func in filters
        ),
    )

    filter_cb = FilterByTestOption(filters)

    cb = SplitToSuitesCallback(filter_cb)
    for d in config.test_src_dir:
        print("Adding tests from directory tree %s" % d)
        discover_tests(d, cb)

    # suites are not hashable, need to use list
    suites = []
    tests_amount = 0
    for testcase_suite in cb.suites.values():
        tests_amount += testcase_suite.countTestCases()
        if testcase_suite.cpus_used > max_vpp_cpus:
            # here we replace test functions with lambdas to just skip them
            # but we also replace setUp/tearDown functions to do nothing
            # so that the test can be "started" and "stopped", so that we can
            # still keep those prints (test description - SKIP), which are done
            # in stopTest() (for that to trigger, test function must run)
            for t in testcase_suite:
                for m in dir(t):
                    if m.startswith("test_"):
                        setattr(t, m, lambda: t.skipTest("not enough cpus"))
                setattr(t.__class__, "setUpClass", lambda: None)
                setattr(t.__class__, "tearDownClass", lambda: None)
                setattr(t, "setUp", lambda: None)
                setattr(t, "tearDown", lambda: None)
                t.__class__.skipped_due_to_cpu_lack = True
        suites.append(testcase_suite)

    print(
        "%s out of %s tests match specified filters"
        % (tests_amount, tests_amount + cb.filtered.countTestCases())
    )

    if not config.extended:
        print("Not running extended tests (some tests will be skipped)")

    attempts = config.retries + 1
    if attempts > 1:
        print("Perform %s attempts to pass the suite..." % attempts)

    if run_interactive and suites:
        # don't fork if requiring interactive terminal
        print("Running tests in foreground in the current process")
        full_suite = unittest.TestSuite()
        free_cpus = list(available_cpus)
        cpu_shortage = False
        for suite in suites:
            if suite.cpus_used <= max_vpp_cpus:
                suite.assign_cpus(free_cpus[: suite.cpus_used])
            else:
                suite.assign_cpus([])
                cpu_shortage = True
        full_suite.addTests(suites)
        result = VppTestRunner(
            verbosity=config.verbose, failfast=config.failfast, print_summary=True
        ).run(full_suite)
        was_successful = result.wasSuccessful()
        if not was_successful:
            for test_case_info in result.failed_test_cases_info:
                handle_failed_suite(
                    test_case_info.logger,
                    test_case_info.tempdir,
                    test_case_info.vpp_pid,
                    config.vpp,
                )
                if test_case_info in result.core_crash_test_cases_info:
                    check_and_handle_core(
                        test_case_info.vpp_bin_path,
                        test_case_info.tempdir,
                        test_case_info.core_crash_test,
                    )

        if cpu_shortage:
            print()
            print(
                colorize(
                    "SOME TESTS WERE SKIPPED BECAUSE THERE ARE NOT"
                    " ENOUGH CPUS AVAILABLE",
                    YELLOW,
                )
            )
            print()
        sys.exit(not was_successful)
    else:
        print(
            "Running each VPPTestCase in a separate background process"
            f" with at most {max_concurrent_tests} parallel python test "
            "process(es)"
        )
        exit_code = 0
        while suites and attempts > 0:
            results = run_forked(suites)
            exit_code, suites = parse_results(results)
            attempts -= 1
            if exit_code == 0:
                print("Test run was successful")
            else:
                print("%s attempt(s) left." % attempts)
        sys.exit(exit_code)