1907281d1SSepherosa Ziehau /* 2907281d1SSepherosa Ziehau * Copyright (c) 2017 The DragonFly Project. All rights reserved. 3907281d1SSepherosa Ziehau * 4907281d1SSepherosa Ziehau * This code is derived from software contributed to The DragonFly Project 5907281d1SSepherosa Ziehau * by Sepherosa Ziehau <sepherosa@gmail.com> 6907281d1SSepherosa Ziehau * 7907281d1SSepherosa Ziehau * Redistribution and use in source and binary forms, with or without 8907281d1SSepherosa Ziehau * modification, are permitted provided that the following conditions 9907281d1SSepherosa Ziehau * are met: 10907281d1SSepherosa Ziehau * 11907281d1SSepherosa Ziehau * 1. Redistributions of source code must retain the above copyright 12907281d1SSepherosa Ziehau * notice, this list of conditions and the following disclaimer. 13907281d1SSepherosa Ziehau * 2. Redistributions in binary form must reproduce the above copyright 14907281d1SSepherosa Ziehau * notice, this list of conditions and the following disclaimer in 15907281d1SSepherosa Ziehau * the documentation and/or other materials provided with the 16907281d1SSepherosa Ziehau * distribution. 17907281d1SSepherosa Ziehau * 3. Neither the name of The DragonFly Project nor the names of its 18907281d1SSepherosa Ziehau * contributors may be used to endorse or promote products derived 19907281d1SSepherosa Ziehau * from this software without specific, prior written permission. 20907281d1SSepherosa Ziehau * 21907281d1SSepherosa Ziehau * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22907281d1SSepherosa Ziehau * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23907281d1SSepherosa Ziehau * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24907281d1SSepherosa Ziehau * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25907281d1SSepherosa Ziehau * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26907281d1SSepherosa Ziehau * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27907281d1SSepherosa Ziehau * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28907281d1SSepherosa Ziehau * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29907281d1SSepherosa Ziehau * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30907281d1SSepherosa Ziehau * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31907281d1SSepherosa Ziehau * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32907281d1SSepherosa Ziehau * SUCH DAMAGE. 33907281d1SSepherosa Ziehau */ 34907281d1SSepherosa Ziehau 35907281d1SSepherosa Ziehau #include <sys/types.h> 36907281d1SSepherosa Ziehau #include <sys/usched.h> 37907281d1SSepherosa Ziehau #include <sched.h> 38907281d1SSepherosa Ziehau #include <string.h> 39907281d1SSepherosa Ziehau 40907281d1SSepherosa Ziehau int 41907281d1SSepherosa Ziehau sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) 42907281d1SSepherosa Ziehau { 43*178a6112SSepherosa Ziehau cpu_set_t mask1; 44*178a6112SSepherosa Ziehau int ret; 45907281d1SSepherosa Ziehau 46*178a6112SSepherosa Ziehau ret = lwp_getaffinity(pid, -1, &mask1); 47*178a6112SSepherosa Ziehau if (ret < 0) 48*178a6112SSepherosa Ziehau return (ret); 49*178a6112SSepherosa Ziehau 50*178a6112SSepherosa Ziehau if (cpusetsize > sizeof(mask1)) { 51907281d1SSepherosa Ziehau memset(mask, 0, cpusetsize); 52*178a6112SSepherosa Ziehau memcpy(mask, &mask1, sizeof(mask1)); 53*178a6112SSepherosa Ziehau } else { 54*178a6112SSepherosa Ziehau memcpy(mask, &mask1, cpusetsize); 55*178a6112SSepherosa Ziehau } 56*178a6112SSepherosa Ziehau return (0); 57907281d1SSepherosa Ziehau } 58