1 /*
2  * Copyright (c) 2012, 2013
3  *      Inferno Nettverk A/S, Norway.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. The above copyright notice, this list of conditions and the following
9  *    disclaimer must appear in all copies of the software, derivative works
10  *    or modified versions, and any portions thereof, aswell as in all
11  *    supporting documentation.
12  * 2. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed by
15  *      Inferno Nettverk A/S, Norway.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Inferno Nettverk A/S requests users of this software to return to
31  *
32  *  Software Distribution Coordinator  or  sdc@inet.no
33  *  Inferno Nettverk A/S
34  *  Oslo Research Park
35  *  Gaustadall�en 21
36  *  NO-0349 Oslo
37  *  Norway
38  *
39  * any improvements or extensions that they make and grant Inferno Nettverk A/S
40  * the rights to redistribute these changes.
41  *
42  */
43 
44 #define _GNU_SOURCE /* needed for SCHED_BATCH */
45 
46 #include "common.h"
47 
48 #include <sched.h>
49 
50 static const char rcsid[] =
51 "$Id: cpupolicy.c,v 1.6 2013/10/27 15:24:42 karls Exp $";
52 
53 #if HAVE_SCHED_SETSCHEDULER
54 
55 cpupolicy_t cpupolicies[] = {
56 #ifdef SCHED_OTHER
57    { SCHED_OTHER, "other" },
58 #endif /* SCHED_OTHER */
59 #ifdef SCHED_FIFO
60    { SCHED_FIFO, "fifo" },
61 #endif /* SCHED_FIFO */
62 #ifdef SCHED_FIFO2
63    { SCHED_FIFO2, "fifo2" },
64 #endif /* SCHED_FIFO2 */
65 #ifdef SCHED_FIFO3
66    { SCHED_FIFO3, "fifo3" },
67 #endif /* SCHED_FIFO3 */
68 #ifdef SCHED_FIFO4
69    { SCHED_FIFO4, "fifo4" },
70 #endif /* SCHED_FIFO */
71 #ifdef SCHED_RR
72    { SCHED_RR, "rr" },
73 #endif /* SCHED_RR */
74 #ifdef SCHED_IDLE
75    { SCHED_IDLE, "idle" },
76 #endif /* SCHED_IDLE */
77 #ifdef SCHED_BATCH
78    { SCHED_BATCH, "batch" },
79 #endif /* SCHED_BATCH */
80    { -1, NULL },
81 };
82 
83 int
cpupolicy2numeric(char * name)84 cpupolicy2numeric(char *name)
85 {
86    int i;
87 
88    for (i = 0; cpupolicies[i].name != NULL; i++) {
89       if (strcmp(name, cpupolicies[i].name) == 0)
90          return cpupolicies[i].value;
91    }
92 
93    return -1;
94 }
95 
96 char *
numeric2cpupolicy(int value)97 numeric2cpupolicy(int value)
98 {
99    int i;
100 
101    for (i = 0; cpupolicies[i].name != NULL; i++) {
102       if (value == cpupolicies[i].value)
103          return cpupolicies[i].name;
104    }
105 
106    return "<unknown>";
107 }
108 
109 #endif /* HAVE_SCHED_SETSCHEDULER */
110