Flowgrind
Advanced TCP traffic generator
fg_affinity.h File Reference

CPU affinity routines used by Flowgrind. More...

#include "config.h"
#include <pthread.h>

Go to the source code of this file.

Enumerations

enum  ncore_query { NCORE_CONFIG = 0, NCORE_CURRENT }
 Query type for get_ncores(). More...
 

Functions

int get_ncores (enum ncore_query query)
 Return either the total number of configured or available cores. More...
 
int pthread_getaffinity (pthread_t thread, unsigned *core)
 Returns the CPU affinity of thread thread in the buffer pointed to by core. More...
 
int pthread_setaffinity (pthread_t thread, unsigned core)
 Set CPU affinity of the thread thread to the core core. More...
 

Detailed Description

CPU affinity routines used by Flowgrind.

Definition in file fg_affinity.h.

Enumeration Type Documentation

Query type for get_ncores().

Enumerator
NCORE_CONFIG 

Total number of processors configured.

NCORE_CURRENT 

Processors available to the current process.

Definition at line 36 of file fg_affinity.h.

36  {
38  NCORE_CONFIG = 0,
41 };
Total number of processors configured.
Definition: fg_affinity.h:38
Processors available to the current process.
Definition: fg_affinity.h:40

Function Documentation

int get_ncores ( enum ncore_query  query)

Return either the total number of configured or available cores.

Parameters
[in]queryindicates if either the configured or available cores should be be returned
See also
enum nproc_query
Returns
return number of processors on success, or -1 for failure

Definition at line 56 of file fg_affinity.c.

57 {
58  switch (query) {
59  case NCORE_CONFIG:
60  /* processors configured */
61  return (int)sysconf(_SC_NPROCESSORS_CONF);
62  break;
63  case NCORE_CURRENT:
64  /* processors available */
65  return (int)sysconf(_SC_NPROCESSORS_ONLN);
66  break;
67  default:
68  errno = EINVAL;
69  return -1;
70  }
71 }
Total number of processors configured.
Definition: fg_affinity.h:38
Processors available to the current process.
Definition: fg_affinity.h:40
int pthread_getaffinity ( pthread_t  thread,
unsigned *  core 
)

Returns the CPU affinity of thread thread in the buffer pointed to by core.

Parameters
[in]threadthread ID
[out]corecore to which thread thread is bounded
Returns
return 0 for success, or -1 for failure

Definition at line 85 of file fg_affinity.c.

86 {
87  cpu_set_t cpuset;
88  int rc = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
89  if (rc)
90  return -1;
91 
92  /* If the cpuset contains only one CPU, then that's the answer. For
93  * all other cpuset contents, we treat the binding as unknown */
94  core = NULL;
95  bool core_found = false;
96  for (unsigned i = 0; i < CPU_SETSIZE; i++) {
97  if (CPU_ISSET(i, &cpuset)) {
98  if (!core_found) {
99  core_found = true;
100  *core = i;
101  } else {
102  core_found = false;
103  core = NULL;
104  break;
105  }
106  }
107  }
108 
109  return (core_found ? 0 : -1);
110 }
static int core
CPU core to which flowgrindd should bind to.
Definition: flowgrindd.c:101
int pthread_setaffinity ( pthread_t  thread,
unsigned  core 
)

Set CPU affinity of the thread thread to the core core.

Parameters
[in]threadthread ID
[in]corecore to which thread thread will be bounded
Returns
return 0 for success, or -1 for failure

Definition at line 75 of file fg_affinity.c.

76 {
77  cpu_set_t cpuset;
78  CPU_ZERO(&cpuset);
79  CPU_SET(core, &cpuset);
80 
81  int rc = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
82  return (rc == 0 ? 0 : -1);
83 }
static int core
CPU core to which flowgrindd should bind to.
Definition: flowgrindd.c:101