suricata
util-affinity.h
Go to the documentation of this file.
1/* Copyright (C) 2010 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Eric Leblond <eric@regit.org>
22 */
23
24#ifndef SURICATA_UTIL_AFFINITY_H
25#define SURICATA_UTIL_AFFINITY_H
26#include "suricata-common.h"
27#include "conf.h"
28#include "threads.h"
29#include "threadvars.h"
30
31#ifdef HAVE_HWLOC
32#include <hwloc.h>
33#endif /* HAVE_HWLOC */
34
35#if defined OS_FREEBSD
36#include <sched.h>
37#include <sys/param.h>
38#include <sys/resource.h>
39#include <sys/cpuset.h>
40#include <sys/thr.h>
41#define cpu_set_t cpuset_t
42#elif defined __OpenBSD__
43#include <sched.h>
44#include <sys/param.h>
45#include <sys/resource.h>
46#elif defined OS_DARWIN
47#include <mach/mach.h>
48#include <mach/mach_init.h>
49#include <mach/thread_policy.h>
50#define cpu_set_t thread_affinity_policy_data_t
51#define CPU_SET(cpu_id, new_mask) (*(new_mask)).affinity_tag = (cpu_id + 1)
52#define CPU_ISSET(cpu_id, new_mask) ((*(new_mask)).affinity_tag == (cpu_id + 1))
53#define CPU_ZERO(new_mask) (*(new_mask)).affinity_tag = THREAD_AFFINITY_TAG_NULL
54#endif
55
56enum {
62};
63
64enum {
68};
69
70#define MAX_NUMA_NODES 16
71
72typedef struct ThreadsAffinityType_ {
73 const char *name;
75 struct ThreadsAffinityType_ *parent; // e.g. worker-cpu-set for interfaces
77
78#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
79 cpu_set_t cpu_set;
80 cpu_set_t lowprio_cpu;
81 cpu_set_t medprio_cpu;
82 cpu_set_t hiprio_cpu;
83#endif
84 int prio;
85 uint32_t nb_threads;
86 uint32_t nb_children;
88 uint16_t lcpu[MAX_NUMA_NODES]; /* use by exclusive mode */
89 uint8_t mode_flag;
90 // a flag to avoid multiple warnings when no CPU is set
93
94/** store thread affinity mode for all type of threads */
95#ifndef _THREAD_AFFINITY
97#endif
98
102 const char *name, const char *interface_name);
103ThreadsAffinityType *GetAffinityTypeForNameAndIface(const char *name, const char *interface_name);
105 ThreadsAffinityType *parent, const char *interface_name);
106
110#ifdef HAVE_DPDK
111uint16_t UtilAffinityCpusOverlap(ThreadsAffinityType *taf1, ThreadsAffinityType *taf2);
112void UtilAffinityCpusExclude(ThreadsAffinityType *mod_taf, ThreadsAffinityType *static_taf);
113#endif /* HAVE_DPDK */
114
116 const char *name, SCConfNode *node, void (*Callback)(int i, void *data), void *data);
117
118#ifdef UNITTESTS
120#endif
121
122#endif /* SURICATA_UTIL_AFFINITY_H */
ThreadVars * tv
Per thread variable structure.
Definition threadvars.h:58
struct ThreadsAffinityType_ * parent
uint32_t nb_children_capacity
struct ThreadsAffinityType_ ** children
uint16_t lcpu[MAX_NUMA_NODES]
#define SCMutex
const char * name
@ MANAGEMENT_CPU_SET
@ RECEIVE_CPU_SET
@ VERDICT_CPU_SET
@ MAX_CPU_SET
@ WORKER_CPU_SET
void TopologyDestroy(void)
void ThreadingAffinityRegisterTests(void)
Register all threading affinity unit tests.
struct ThreadsAffinityType_ ThreadsAffinityType
ThreadsAffinityType thread_affinity[MAX_CPU_SET]
char * AffinityGetYamlPath(ThreadsAffinityType *taf)
Get the YAML path for the given affinity type. The path is built using the parent name (if available)...
@ EXCLUSIVE_AFFINITY
@ BALANCED_AFFINITY
@ MAX_AFFINITY
int BuildCpusetWithCallback(const char *name, SCConfNode *node, void(*Callback)(int i, void *data), void *data)
ThreadsAffinityType * GetAffinityTypeForNameAndIface(const char *name, const char *interface_name)
Find affinity by name (*-cpu-set name) and an interface name.
#define MAX_NUMA_NODES
uint16_t UtilAffinityGetAffinedCPUNum(ThreadsAffinityType *taf)
Return the total number of CPUs in a given affinity.
ThreadsAffinityType * FindAffinityByInterface(ThreadsAffinityType *parent, const char *interface_name)
uint16_t AffinityGetNextCPU(ThreadVars *tv, ThreadsAffinityType *taf)
ThreadsAffinityType * GetOrAllocAffinityTypeForIfaceOfName(const char *name, const char *interface_name)
Finds affinity by its name and interface name. Interfaces are children of cpu-set names....
void AffinitySetupLoadFromConfig(void)
Extract CPU affinity configuration from current config file.