PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/numactl-2.0.7/numa.h

#
C Header | 468 lines | 267 code | 108 blank | 93 comment | 3 complexity | 6857813f75ce330d18140b8f128da7a8 MD5 | raw file
  1. /* Copyright (C) 2003,2004 Andi Kleen, SuSE Labs.
  2. libnuma is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; version
  5. 2.1.
  6. libnuma is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should find a copy of v2.1 of the GNU Lesser General Public License
  11. somewhere on your Linux system; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. #ifndef _NUMA_H
  14. #define _NUMA_H 1
  15. /* allow an application to test for the current programming interface: */
  16. #define LIBNUMA_API_VERSION 2
  17. /* Simple NUMA policy library */
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <stdlib.h>
  22. #if defined(__x86_64__) || defined(__i386__)
  23. #define NUMA_NUM_NODES 128
  24. #else
  25. #define NUMA_NUM_NODES 2048
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. typedef struct {
  31. unsigned long n[NUMA_NUM_NODES/(sizeof(unsigned long)*8)];
  32. } nodemask_t;
  33. struct bitmask {
  34. unsigned long size; /* number of bits in the map */
  35. unsigned long *maskp;
  36. };
  37. /* operations on struct bitmask */
  38. int numa_bitmask_isbitset(const struct bitmask *, unsigned int);
  39. struct bitmask *numa_bitmask_setall(struct bitmask *);
  40. struct bitmask *numa_bitmask_clearall(struct bitmask *);
  41. struct bitmask *numa_bitmask_setbit(struct bitmask *, unsigned int);
  42. struct bitmask *numa_bitmask_clearbit(struct bitmask *, unsigned int);
  43. unsigned int numa_bitmask_nbytes(struct bitmask *);
  44. struct bitmask *numa_bitmask_alloc(unsigned int);
  45. void numa_bitmask_free(struct bitmask *);
  46. int numa_bitmask_equal(const struct bitmask *, const struct bitmask *);
  47. void copy_nodemask_to_bitmask(nodemask_t *, struct bitmask *);
  48. void copy_bitmask_to_nodemask(struct bitmask *, nodemask_t *);
  49. void copy_bitmask_to_bitmask(struct bitmask *, struct bitmask *);
  50. /* compatibility for codes that used them: */
  51. static inline void nodemask_zero(nodemask_t *mask)
  52. {
  53. struct bitmask tmp;
  54. tmp.maskp = (unsigned long *)mask;
  55. tmp.size = sizeof(nodemask_t) * 8;
  56. numa_bitmask_clearall(&tmp);
  57. }
  58. static inline void nodemask_zero_compat(nodemask_t *mask)
  59. {
  60. struct bitmask tmp;
  61. tmp.maskp = (unsigned long *)mask;
  62. tmp.size = sizeof(nodemask_t) * 8;
  63. numa_bitmask_clearall(&tmp);
  64. }
  65. static inline void nodemask_set_compat(nodemask_t *mask, int node)
  66. {
  67. mask->n[node / (8*sizeof(unsigned long))] |=
  68. (1UL<<(node%(8*sizeof(unsigned long))));
  69. }
  70. static inline void nodemask_clr_compat(nodemask_t *mask, int node)
  71. {
  72. mask->n[node / (8*sizeof(unsigned long))] &=
  73. ~(1UL<<(node%(8*sizeof(unsigned long))));
  74. }
  75. static inline int nodemask_isset_compat(const nodemask_t *mask, int node)
  76. {
  77. if ((unsigned)node >= NUMA_NUM_NODES)
  78. return 0;
  79. if (mask->n[node / (8*sizeof(unsigned long))] &
  80. (1UL<<(node%(8*sizeof(unsigned long)))))
  81. return 1;
  82. return 0;
  83. }
  84. static inline int nodemask_equal(const nodemask_t *a, const nodemask_t *b)
  85. {
  86. struct bitmask tmp_a, tmp_b;
  87. tmp_a.maskp = (unsigned long *)a;
  88. tmp_a.size = sizeof(nodemask_t) * 8;
  89. tmp_b.maskp = (unsigned long *)b;
  90. tmp_b.size = sizeof(nodemask_t) * 8;
  91. return numa_bitmask_equal(&tmp_a, &tmp_b);
  92. }
  93. static inline int nodemask_equal_compat(const nodemask_t *a, const nodemask_t *b)
  94. {
  95. struct bitmask tmp_a, tmp_b;
  96. tmp_a.maskp = (unsigned long *)a;
  97. tmp_a.size = sizeof(nodemask_t) * 8;
  98. tmp_b.maskp = (unsigned long *)b;
  99. tmp_b.size = sizeof(nodemask_t) * 8;
  100. return numa_bitmask_equal(&tmp_a, &tmp_b);
  101. }
  102. /* NUMA support available. If this returns a negative value all other function
  103. in this library are undefined. */
  104. int numa_available(void);
  105. /* Basic NUMA state */
  106. /* Get max available node */
  107. int numa_max_node(void);
  108. int numa_max_possible_node(void);
  109. /* Return preferred node */
  110. int numa_preferred(void);
  111. /* Return node size and free memory */
  112. long long numa_node_size64(int node, long long *freep);
  113. long numa_node_size(int node, long *freep);
  114. int numa_pagesize(void);
  115. /* Set with all nodes from which the calling process may allocate memory.
  116. Only valid after numa_available. */
  117. extern struct bitmask *numa_all_nodes_ptr;
  118. /* Set with all nodes the kernel has exposed to userspace */
  119. extern struct bitmask *numa_nodes_ptr;
  120. /* For source compatibility */
  121. extern nodemask_t numa_all_nodes;
  122. /* Set with all cpus. */
  123. extern struct bitmask *numa_all_cpus_ptr;
  124. /* Set with no nodes */
  125. extern struct bitmask *numa_no_nodes_ptr;
  126. /* Source compatibility */
  127. extern nodemask_t numa_no_nodes;
  128. /* Only run and allocate memory from a specific set of nodes. */
  129. void numa_bind(struct bitmask *nodes);
  130. /* Set the NUMA node interleaving mask. 0 to turn off interleaving */
  131. void numa_set_interleave_mask(struct bitmask *nodemask);
  132. /* Return the current interleaving mask */
  133. struct bitmask *numa_get_interleave_mask(void);
  134. /* allocate a bitmask big enough for all nodes */
  135. struct bitmask *numa_allocate_nodemask(void);
  136. static inline void numa_free_nodemask(struct bitmask *b)
  137. {
  138. numa_bitmask_free(b);
  139. }
  140. /* Some node to preferably allocate memory from for task. */
  141. void numa_set_preferred(int node);
  142. /* Set local memory allocation policy for task */
  143. void numa_set_localalloc(void);
  144. /* Only allocate memory from the nodes set in mask. 0 to turn off */
  145. void numa_set_membind(struct bitmask *nodemask);
  146. /* Return current membind */
  147. struct bitmask *numa_get_membind(void);
  148. /* Return allowed memories [nodes] */
  149. struct bitmask *numa_get_mems_allowed(void);
  150. int numa_get_interleave_node(void);
  151. /* NUMA memory allocation. These functions always round to page size
  152. and are relatively slow. */
  153. /* Alloc memory page interleaved on nodes in mask */
  154. void *numa_alloc_interleaved_subset(size_t size, struct bitmask *nodemask);
  155. /* Alloc memory page interleaved on all nodes. */
  156. void *numa_alloc_interleaved(size_t size);
  157. /* Alloc memory located on node */
  158. void *numa_alloc_onnode(size_t size, int node);
  159. /* Alloc memory on local node */
  160. void *numa_alloc_local(size_t size);
  161. /* Allocation with current policy */
  162. void *numa_alloc(size_t size);
  163. /* Change the size of a memory area preserving the memory policy */
  164. void *numa_realloc(void *old_addr, size_t old_size, size_t new_size);
  165. /* Free memory allocated by the functions above */
  166. void numa_free(void *mem, size_t size);
  167. /* Low level functions, primarily for shared memory. All memory
  168. processed by these must not be touched yet */
  169. /* Interleave an memory area. */
  170. void numa_interleave_memory(void *mem, size_t size, struct bitmask *mask);
  171. /* Allocate a memory area on a specific node. */
  172. void numa_tonode_memory(void *start, size_t size, int node);
  173. /* Allocate memory on a mask of nodes. */
  174. void numa_tonodemask_memory(void *mem, size_t size, struct bitmask *mask);
  175. /* Allocate a memory area on the current node. */
  176. void numa_setlocal_memory(void *start, size_t size);
  177. /* Allocate memory area with current memory policy */
  178. void numa_police_memory(void *start, size_t size);
  179. /* Run current task only on nodes in mask */
  180. int numa_run_on_node_mask(struct bitmask *mask);
  181. /* Run current task only on node */
  182. int numa_run_on_node(int node);
  183. /* Return current mask of nodes the task can run on */
  184. struct bitmask * numa_get_run_node_mask(void);
  185. /* When strict fail allocation when memory cannot be allocated in target node(s). */
  186. void numa_set_bind_policy(int strict);
  187. /* Fail when existing memory has incompatible policy */
  188. void numa_set_strict(int flag);
  189. /* maximum nodes (size of kernel nodemask_t) */
  190. int numa_num_possible_nodes();
  191. /* maximum cpus (size of kernel cpumask_t) */
  192. int numa_num_possible_cpus();
  193. /* nodes in the system */
  194. int numa_num_configured_nodes();
  195. /* maximum cpus */
  196. int numa_num_configured_cpus();
  197. /* maximum cpus allowed to current task */
  198. int numa_num_task_cpus();
  199. int numa_num_thread_cpus(); /* backward compatibility */
  200. /* maximum nodes allowed to current task */
  201. int numa_num_task_nodes();
  202. int numa_num_thread_nodes(); /* backward compatibility */
  203. /* allocate a bitmask the size of the kernel cpumask_t */
  204. struct bitmask *numa_allocate_cpumask();
  205. static inline void numa_free_cpumask(struct bitmask *b)
  206. {
  207. numa_bitmask_free(b);
  208. }
  209. /* Convert node to CPU mask. -1/errno on failure, otherwise 0. */
  210. int numa_node_to_cpus(int, struct bitmask *);
  211. /* report the node of the specified cpu. -1/errno on invalid cpu. */
  212. int numa_node_of_cpu(int cpu);
  213. /* Report distance of node1 from node2. 0 on error.*/
  214. int numa_distance(int node1, int node2);
  215. /* Error handling. */
  216. /* This is an internal function in libnuma that can be overwritten by an user
  217. program. Default is to print an error to stderr and exit if numa_exit_on_error
  218. is true. */
  219. void numa_error(char *where);
  220. /* When true exit the program when a NUMA system call (except numa_available)
  221. fails */
  222. extern int numa_exit_on_error;
  223. /* Warning function. Can also be overwritten. Default is to print on stderr
  224. once. */
  225. void numa_warn(int num, char *fmt, ...);
  226. /* When true exit the program on a numa_warn() call */
  227. extern int numa_exit_on_warn;
  228. int numa_migrate_pages(int pid, struct bitmask *from, struct bitmask *to);
  229. int numa_move_pages(int pid, unsigned long count, void **pages,
  230. const int *nodes, int *status, int flags);
  231. int numa_sched_getaffinity(pid_t, struct bitmask *);
  232. int numa_sched_setaffinity(pid_t, struct bitmask *);
  233. /* Convert an ascii list of nodes to a bitmask */
  234. struct bitmask *numa_parse_nodestring(char *);
  235. /* Convert an ascii list of cpu to a bitmask */
  236. struct bitmask *numa_parse_cpustring(char *);
  237. /*
  238. * The following functions are for source code compatibility
  239. * with releases prior to version 2.
  240. * Such codes should be compiled with NUMA_VERSION1_COMPATIBILITY defined.
  241. */
  242. static inline void numa_set_interleave_mask_compat(nodemask_t *nodemask)
  243. {
  244. struct bitmask tmp;
  245. tmp.maskp = (unsigned long *)nodemask;
  246. tmp.size = sizeof(nodemask_t) * 8;
  247. numa_set_interleave_mask(&tmp);
  248. }
  249. static inline nodemask_t numa_get_interleave_mask_compat()
  250. {
  251. struct bitmask *tp;
  252. nodemask_t mask;
  253. tp = numa_get_interleave_mask();
  254. copy_bitmask_to_nodemask(tp, &mask);
  255. numa_bitmask_free(tp);
  256. return mask;
  257. }
  258. static inline void numa_bind_compat(nodemask_t *mask)
  259. {
  260. struct bitmask *tp;
  261. tp = numa_allocate_nodemask();
  262. copy_nodemask_to_bitmask(mask, tp);
  263. numa_bind(tp);
  264. numa_bitmask_free(tp);
  265. }
  266. static inline void numa_set_membind_compat(nodemask_t *mask)
  267. {
  268. struct bitmask tmp;
  269. tmp.maskp = (unsigned long *)mask;
  270. tmp.size = sizeof(nodemask_t) * 8;
  271. numa_set_membind(&tmp);
  272. }
  273. static inline nodemask_t numa_get_membind_compat()
  274. {
  275. struct bitmask *tp;
  276. nodemask_t mask;
  277. tp = numa_get_membind();
  278. copy_bitmask_to_nodemask(tp, &mask);
  279. numa_bitmask_free(tp);
  280. return mask;
  281. }
  282. static inline void *numa_alloc_interleaved_subset_compat(size_t size,
  283. const nodemask_t *mask)
  284. {
  285. struct bitmask tmp;
  286. tmp.maskp = (unsigned long *)mask;
  287. tmp.size = sizeof(nodemask_t) * 8;
  288. return numa_alloc_interleaved_subset(size, &tmp);
  289. }
  290. static inline int numa_run_on_node_mask_compat(const nodemask_t *mask)
  291. {
  292. struct bitmask tmp;
  293. tmp.maskp = (unsigned long *)mask;
  294. tmp.size = sizeof(nodemask_t) * 8;
  295. return numa_run_on_node_mask(&tmp);
  296. }
  297. static inline nodemask_t numa_get_run_node_mask_compat()
  298. {
  299. struct bitmask *tp;
  300. nodemask_t mask;
  301. tp = numa_get_run_node_mask();
  302. copy_bitmask_to_nodemask(tp, &mask);
  303. numa_bitmask_free(tp);
  304. return mask;
  305. }
  306. static inline void numa_interleave_memory_compat(void *mem, size_t size,
  307. const nodemask_t *mask)
  308. {
  309. struct bitmask tmp;
  310. tmp.maskp = (unsigned long *)mask;
  311. tmp.size = sizeof(nodemask_t) * 8;
  312. numa_interleave_memory(mem, size, &tmp);
  313. }
  314. static inline void numa_tonodemask_memory_compat(void *mem, size_t size,
  315. const nodemask_t *mask)
  316. {
  317. struct bitmask tmp;
  318. tmp.maskp = (unsigned long *)mask;
  319. tmp.size = sizeof(nodemask_t) * 8;
  320. numa_tonodemask_memory(mem, size, &tmp);
  321. }
  322. static inline int numa_sched_getaffinity_compat(pid_t pid, unsigned len,
  323. unsigned long *mask)
  324. {
  325. struct bitmask tmp;
  326. tmp.maskp = (unsigned long *)mask;
  327. tmp.size = len * 8;
  328. return numa_sched_getaffinity(pid, &tmp);
  329. }
  330. static inline int numa_sched_setaffinity_compat(pid_t pid, unsigned len,
  331. unsigned long *mask)
  332. {
  333. struct bitmask tmp;
  334. tmp.maskp = (unsigned long *)mask;
  335. tmp.size = len * 8;
  336. return numa_sched_setaffinity(pid, &tmp);
  337. }
  338. static inline int numa_node_to_cpus_compat(int node, unsigned long *buffer,
  339. int buffer_len)
  340. {
  341. struct bitmask tmp;
  342. tmp.maskp = (unsigned long *)buffer;
  343. tmp.size = buffer_len * 8;
  344. return numa_node_to_cpus(node, &tmp);
  345. }
  346. /* end of version 1 compatibility functions */
  347. /*
  348. * To compile an application that uses libnuma version 1:
  349. * add -DNUMA_VERSION1_COMPATIBILITY to your Makefile's CFLAGS
  350. */
  351. #ifdef NUMA_VERSION1_COMPATIBILITY
  352. #include <numacompat1.h>
  353. #endif
  354. #ifdef __cplusplus
  355. }
  356. #endif
  357. #endif