Home | History | Annotate | Download | only in cpuset_lib
      1 /*
      2  * cpuset header file
      3  *
      4  * Copyright (c) 2004-2006 Silicon Graphics, Inc. All rights reserved.
      5  *
      6  * Paul Jackson <pj (at) sgi.com>
      7  */
      8 
      9 /*
     10  *  This program is free software; you can redistribute it and/or modify
     11  *  it under the terms of the GNU Lesser General Public License as published by
     12  *  the Free Software Foundation; either version 2.1 of the License, or
     13  *  (at your option) any later version.
     14  *
     15  *  This program is distributed in the hope that it will be useful,
     16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  *  GNU Lesser General Public License for more details.
     19  *
     20  *  You should have received a copy of the GNU Lesser General Public License
     21  *  along with this program; if not, write to the Free Software
     22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
     23  */
     24 
     25 /*
     26  * cpusets - basic routines (use cpuset relative numbering of CPUs)
     27  *
     28  *   link with -lbitmask -lcpuset
     29  *
     30  * cpuset_pin(int relcpu) - Pin current task to one CPU in its cpuset.
     31  * cpuset_size() - How many CPUs are in current tasks cpuset?
     32  * cpuset_where() - Most recent CPU in current tasks cpuset that task ran on.
     33  * cpuset_unpin() - Undo cpuset_pin(), let task run anywhere in its cpuset.
     34  */
     35 
     36 /*
     37  * cpusets - sets of CPUs and Memory Nodes - advanced routines (use system
     38  *	     wide numbering of CPUs and Memory Nodes, except as noted)
     39  *
     40  *   link with -lbitmask -lcpuset
     41  *
     42  * cpuset_version() - [optional] Version (simple integer) of the library.
     43  *
     44  * ==== Allocate and free struct cpuset ====
     45  *
     46  * cpuset_alloc() - Allocate a new struct cpuset
     47  * cpuset_free(struct cpuset *cp) - Free struct cpuset *cp
     48  *
     49  * ==== Lengths of CPUs and Memory Nodes bitmasks - use to alloc them ====
     50  *
     51  * cpuset_cpus_nbits() - Number of bits in a CPU bitmask on current system
     52  * cpuset_mems_nbits() - Number of bits in a Memory bitmask on current system
     53  *
     54  * ==== Set various attributes of a struct cpuset ====
     55  *
     56  * cpuset_setcpus(cp, cpus) - Set CPUs in cpuset cp to bitmask cpus
     57  * cpuset_setmems(cp, mems) - Set Memory Nodes in cpuset cp to bitmask mems
     58  * cpuset_set_iopt(cp, optname, val) - Set integer value optname of cpuset cp
     59  * cpuset_set_sopt(cp, optname, val) - [optional] Set string value optname
     60  *
     61  * ==== Query various attributes of a struct cpuset ====
     62  *
     63  * cpuset_getcpus(cp, cpus) - Write CPUs in cpuset cp to bitmask cpus
     64  * cpuset_getmems(cp, mems) - Write Memory Nodes in cpuset cp to bitmask mems
     65  * cpuset_cpus_weight(cp) - Number of CPUs in a cpuset
     66  * cpuset_mems_weight(cp) - Number of Memory Nodes in a cpuset
     67  * cpuset_get_iopt(cp, optname) - Return integer value of option optname in cp
     68  * cpuset_get_sopt(cp, optname) - [optional] Return string value of optname
     69  *
     70  * ==== Local CPUs and Memory Nodes ====
     71  *
     72  * cpuset_localcpus(mems, cpus) - Set cpus to those local to Memory Nodes mems
     73  * cpuset_localmems(cpus, mems) - Set mems to those local to CPUs cpus
     74  * cpuset_cpumemdist(cpu, mem) - [optional] Hardware distance from CPU to Memory Node
     75  * cpuset_cpu2node(cpu) - [optional] Return Memory Node closest to cpu
     76  * cpuset_addr2node(addr) - [optional] Return Memory Node holding page at specified addr
     77  *
     78  * ==== Create, delete, query, modify, list and examine cpusets ====
     79  *
     80  * cpuset_create(path, cp) - Create cpuset 'cp' at location 'path'
     81  * cpuset_delete(path) - Delete cpuset at location 'path' (if empty)
     82  * cpuset_query(cp, path) - Set cpuset cp to the cpuset at location 'path'
     83  * cpuset_modify(path, cp) - Change cpuset at location 'path' to values of 'cp'
     84  * cpuset_getcpusetpath(pid, buf, buflen) - Get cpuset path of pid into buf
     85  * cpuset_cpusetofpid(cp, pid) - Get cpuset 'cp' of pid
     86  * cpuset_mountpoint() - [optional] Cpuset filesystem mount point
     87  * cpuset_collides_exclusive - [optional] True if would collide exclusive
     88  * cpuset_nuke(path, unsigned int seconds) - [optional] Remove cpuset anyway possible
     89  *
     90  * ==== List tasks (pids) currently attached to a cpuset ====
     91  *
     92  * cpuset_init_pidlist(path, recurseflag) - Return list pids in cpuset 'path'
     93  * cpuset_pidlist_length(pidlist) - Return number of elements in pidlist
     94  * cpuset_get_pidlist(pidlist, i) - Return i'th element of pidlist
     95  * cpuset_freepidlist(pidlist) - Free pidlist
     96  *
     97  * ==== Attach tasks to cpusets ====
     98  *
     99  * cpuset_move(pid, path) - Move task (pid == 0 for current) to a cpuset
    100  * cpuset_move_all(pidlist, path) - Move all tasks in pidlist to a cpuset
    101  * cpuset_move_cpuset_tasks(fromrelpath, torelpath) - [optional]
    102  *		Move all tasks in cpuset 'fromrelpath' to cpuset 'torelpath'
    103  * cpuset_migrate(pid, path) - [optional] Like cpuset_move - plus migrate memory
    104  * cpuset_migrate_all(pidlist, path) - [optional] cpuset_move_all plus migrate
    105  * cpuset_reattach(path) - Rebind cpus_allowed of each task in cpuset 'path'
    106  *
    107  * ==== Determine memory pressure ====
    108  *
    109  * cpuset_open_memory_pressure(path) - [optional] Open handle to read memory_pressure
    110  * cpuset_read_memory_pressure(han) - [optional] Read cpuset current memory_pressure
    111  * cpuset_close_memory_pressure(han) - [optional] Close handle to read memory pressure
    112  *
    113  * ==== Map between relative and system-wide CPU and Memory Node numbers ====
    114  *
    115  * cpuset_c_rel_to_sys_cpu(cp, cpu) - Map cpuset relative cpu to system wide
    116  * cpuset_c_sys_to_rel_cpu(cp, cpu) - Map system wide cpu to cpuset relative
    117  * cpuset_c_rel_to_sys_mem(cp, mem) - Map cpuset relative mem to system wide
    118  * cpuset_c_sys_to_rel_mem(cp, mem) - Map system wide mem to cpuset relative
    119  * cpuset_p_rel_to_sys_cpu(pid, cpu) - Map cpuset relative cpu to system wide
    120  * cpuset_p_sys_to_rel_cpu(pid, cpu) - Map system wide cpu to cpuset relative
    121  * cpuset_p_rel_to_sys_mem(pid, mem) - Map cpuset relative mem to system wide
    122  * cpuset_p_sys_to_rel_mem(pid, mem) - Map system wide mem to cpuset relative
    123  *
    124  * ==== Placement operations - for detecting cpuset migration ====
    125  *
    126  * cpuset_get_placement(pid) - [optional] Return current placement of task pid
    127  * cpuset_equal_placement(plc1, plc2) - [optional] True if two placements equal
    128  * cpuset_free_placement(plc) - [optional] Free placement
    129  *
    130  * ==== Traverse a cpuset hierarchy ====
    131  *
    132  * cpuset_fts_open(path) - [optional] Open cpuset hierarchy ==> cs_tree
    133  * cpuset_fts_read(cs_tree) - [optional] Next entry in tree ==> cs_entry
    134  * cpuset_fts_reverse(cs_tree) - [optional] Reverse order of entries in cs_tree
    135  * cpuset_fts_rewind(cs_tree) - [optional] Rewind cs_tree to beginning
    136  * cpuset_fts_get_path(cs_entry) - [optional] Get entry's cpuset path
    137  * cpuset_fts_get_stat(cs_entry) - [optional] Get entry's stat(2) pointer
    138  * cpuset_fts_get_cpuset(cs_entry) - [optional] Get entry's cpuset pointer
    139  * cpuset_fts_get_errno(cs_entry) - [optional] Get entry's errno
    140  * cpuset_fts_get_info(cs_entry) - [optional] Get operation causing error
    141  * cpuset_fts_close(cs_tree) - [optional] Close cpuset hierarchy
    142  *
    143  * ==== Bind to a CPU or Memory Node within the current cpuset ====
    144  *
    145  * cpuset_cpubind(cpu) - Bind current task to cpu (uses sched_setaffinity(2))
    146  * cpuset_latestcpu(pid) - Return most recent CPU on which task pid executed
    147  * cpuset_membind(mem) - Bind current task to memory (uses set_mempolicy(2))
    148  *
    149  * ==== Export cpuset settings to, and import from, a regular file ====
    150  *
    151  * cpuset_export(cp, file) - Export cpuset settings to a regular file
    152  * cpuset_import(cp, file) - Import cpuset settings from a regular file
    153  *
    154  * ==== Support calls to [optional] cpuset_* API routines ====
    155  *
    156  * cpuset_function - Return pointer to a libcpuset.so function, or NULL
    157  *
    158  */
    159 
    160 #include <sys/types.h>
    161 #include <sys/stat.h>
    162 #include <unistd.h>
    163 
    164 #ifndef _CPUSET_H
    165 #define _CPUSET_H
    166 
    167 #ifdef __cplusplus
    168 extern "C" {
    169 #endif
    170 
    171 int cpuset_version(void);
    172 
    173 int cpuset_pin(int relcpu);
    174 int cpuset_size(void);
    175 int cpuset_where(void);
    176 int cpuset_unpin(void);
    177 
    178 struct bitmask;
    179 struct cpuset;
    180 struct cpuset_pidlist;
    181 struct cpuset_placement;
    182 struct cpuset_fts_tree;
    183 struct cpuset_fts_entry;
    184 
    185 struct cpuset *cpuset_alloc(void);
    186 void cpuset_free(struct cpuset *cp);
    187 
    188 int cpuset_cpus_nbits(void);
    189 int cpuset_mems_nbits(void);
    190 
    191 int cpuset_setcpus(struct cpuset *cp, const struct bitmask *cpus);
    192 int cpuset_setmems(struct cpuset *cp, const struct bitmask *mems);
    193 int cpuset_set_iopt(struct cpuset *cp, const char *optionname, int value);
    194 int cpuset_set_sopt(struct cpuset *cp, const char *optionname,
    195 							const char *value);
    196 
    197 int cpuset_open_memory_pressure(const char *cpusetpath);
    198 int cpuset_read_memory_pressure(int han);
    199 void cpuset_close_memory_pressure(int han);
    200 
    201 int cpuset_getcpus(const struct cpuset *cp, struct bitmask *cpus);
    202 int cpuset_getmems(const struct cpuset *cp, struct bitmask *mems);
    203 int cpuset_cpus_weight(const struct cpuset *cp);
    204 int cpuset_mems_weight(const struct cpuset *cp);
    205 int cpuset_get_iopt(const struct cpuset *cp, const char *optionname);
    206 const char *cpuset_get_sopt(const struct cpuset *cp, const char *optionname);
    207 
    208 int cpuset_localcpus(const struct bitmask *mems, struct bitmask *cpus);
    209 int cpuset_localmems(const struct bitmask *cpus, struct bitmask *mems);
    210 unsigned int cpuset_cpumemdist(int cpu, int mem);
    211 int cpuset_cpu2node(int cpu);
    212 int cpuset_addr2node(void *addr);
    213 
    214 int cpuset_create(const char *cpusetpath, const struct cpuset *cp);
    215 int cpuset_delete(const char *cpusetpath);
    216 int cpuset_query(struct cpuset *cp, const char *cpusetpath);
    217 int cpuset_modify(const char *cpusetpath, const struct cpuset *cp);
    218 char *cpuset_getcpusetpath(pid_t pid, char *buf, size_t size);
    219 int cpuset_cpusetofpid(struct cpuset *cp, pid_t pid);
    220 const char *cpuset_mountpoint(void);
    221 int cpuset_collides_exclusive(const char *cpusetpath, const struct cpuset *cp);
    222 int cpuset_nuke(const char *cpusetpath, unsigned int seconds);
    223 
    224 struct cpuset_pidlist *cpuset_init_pidlist(const char *cpusetpath,
    225 							int recursiveflag);
    226 int cpuset_pidlist_length(const struct cpuset_pidlist *pl);
    227 pid_t cpuset_get_pidlist(const struct cpuset_pidlist *pl, int i);
    228 void cpuset_freepidlist(struct cpuset_pidlist *pl);
    229 
    230 int cpuset_move(pid_t pid, const char *cpusetpath);
    231 int cpuset_move_all(struct cpuset_pidlist *pl, const char *cpusetpath);
    232 int cpuset_move_cpuset_tasks(const char *fromrelpath, const char *torelpath);
    233 int cpuset_migrate(pid_t pid, const char *cpusetpath);
    234 int cpuset_migrate_all(struct cpuset_pidlist *pl, const char *cpusetpath);
    235 int cpuset_reattach(const char *cpusetpath);
    236 
    237 int cpuset_c_rel_to_sys_cpu(const struct cpuset *cp, int cpu);
    238 int cpuset_c_sys_to_rel_cpu(const struct cpuset *cp, int cpu);
    239 int cpuset_c_rel_to_sys_mem(const struct cpuset *cp, int mem);
    240 int cpuset_c_sys_to_rel_mem(const struct cpuset *cp, int mem);
    241 
    242 int cpuset_p_rel_to_sys_cpu(pid_t pid, int cpu);
    243 int cpuset_p_sys_to_rel_cpu(pid_t pid, int cpu);
    244 int cpuset_p_rel_to_sys_mem(pid_t pid, int mem);
    245 int cpuset_p_sys_to_rel_mem(pid_t pid, int mem);
    246 
    247 struct cpuset_placement *cpuset_get_placement(pid_t pid);
    248 int cpuset_equal_placement(const struct cpuset_placement *plc1,
    249 					const struct cpuset_placement *plc2);
    250 void cpuset_free_placement(struct cpuset_placement *plc);
    251 
    252 struct cpuset_fts_tree *cpuset_fts_open(const char *cpusetpath);
    253 const struct cpuset_fts_entry *cpuset_fts_read(
    254 				struct cpuset_fts_tree *cs_tree);
    255 void cpuset_fts_reverse(struct cpuset_fts_tree *cs_tree);
    256 void cpuset_fts_rewind(struct cpuset_fts_tree *cs_tree);
    257 const char *cpuset_fts_get_path(
    258 				const struct cpuset_fts_entry *cs_entry);
    259 const struct stat *cpuset_fts_get_stat(
    260 				const struct cpuset_fts_entry *cs_entry);
    261 const struct cpuset *cpuset_fts_get_cpuset(
    262 				const struct cpuset_fts_entry *cs_entry);
    263 int cpuset_fts_get_errno(const struct cpuset_fts_entry *cs_entry);
    264 int cpuset_fts_get_info(const struct cpuset_fts_entry *cs_entry);
    265 void cpuset_fts_close(struct cpuset_fts_tree *cs_tree);
    266 
    267 int cpuset_cpubind(int cpu);
    268 int cpuset_latestcpu(pid_t pid);
    269 int cpuset_membind(int mem);
    270 
    271 int cpuset_export(const struct cpuset *cp, char *buf, int buflen);
    272 int cpuset_import(struct cpuset *cp, const char *buf, int *elinenum,
    273 							char *emsg, int elen);
    274 
    275 void *cpuset_function(const char * function_name);
    276 
    277 /*
    278  * cpuset_fts_entry.info values.
    279  *
    280  * Because the cpuset_fts_open() call collects all the information
    281  * at once from an entire cpuset subtree, a simple error return would
    282  * not provide sufficient information as to what failed, and on what
    283  * cpuset in the subtree.  So, except for malloc(3) failures, errors
    284  * are captured in the list of entries.  If an entry has one of the
    285  * following CPUSET_FTS_ERR_* values in the "info" field, then the "info"
    286  * field indicates which operation failed, the "err" field captures the
    287  * failing errno value for that operation, and the other entry fields
    288  * might not be valid.  If an entry has the value "CPUSET_FTS_CPUSET" for its
    289  * "info" field, then the err field will have the value "0", and the
    290  * other fields will be contain valid information about that cpuset.
    291  *
    292  */
    293 enum {
    294 	CPUSET_FTS_CPUSET = 0,		/* valid cpuset */
    295 	CPUSET_FTS_ERR_DNR = 1,		/* error - couldn't read directory */
    296 	CPUSET_FTS_ERR_STAT = 2,	/* error - couldn't stat directory */
    297 	CPUSET_FTS_ERR_CPUSET = 3,	/* error - cpuset_query() failed */
    298 };
    299 
    300 /*
    301  * If it necessary to maintain source code compatibility with earlier
    302  * versions of this header file lacking the above CPUSET_FTS_* values,
    303  * one can conditionally check that the C preprocessor symbol
    304  * CPUSET_FTS_INFO_VALUES_DEFINED symbol is not defined and provide
    305  * alternative coding for that case.
    306  */
    307 #define CPUSET_FTS_INFO_VALUES_DEFINED 1
    308 
    309 #ifdef __cplusplus
    310 }
    311 #endif
    312 
    313 #endif
    314