Back to index

numactl  2.0.8~rc4
util.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003,2004 Andi Kleen, SuSE Labs.
00002 
00003    numactl is free software; you can redistribute it and/or
00004    modify it under the terms of the GNU General Public
00005    License as published by the Free Software Foundation; version
00006    2.
00007 
00008    numactl is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    General Public License for more details.
00012 
00013    You should find a copy of v2 of the GNU General Public License somewhere
00014    on your Linux system; if not, write to the Free Software Foundation,
00015    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
00016 #include "numa.h"
00017 #include "numaif.h"
00018 #include "util.h"
00019 #include "bitops.h"
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 #include <ctype.h>
00025 #include <errno.h>
00026 #include <unistd.h>
00027 
00028 void printmask(char *name, struct bitmask *mask)
00029 {
00030        int i;
00031 
00032        printf("%s: ", name);
00033        for (i = 0; i <= mask->size; i++)
00034               if (numa_bitmask_isbitset(mask, i))
00035                      printf("%d ", i);
00036        putchar('\n');
00037 }
00038 
00039 void printcpumask(char *name, struct bitmask *mask)
00040 {
00041        int i;
00042        printf("%s: ", name);
00043        for (i = 0; i < mask->size; i++) {
00044               if (numa_bitmask_isbitset(mask, i))
00045                      printf("%d ", i);
00046        }
00047        putchar('\n');
00048 }
00049 
00050 void complain(char *fmt, ...)
00051 {
00052        va_list ap;
00053        va_start(ap, fmt);
00054        fprintf(stderr, "numactl: ");
00055        vfprintf(stderr,fmt,ap);
00056        putchar('\n');
00057        va_end(ap);
00058        exit(1);
00059 }
00060 
00061 void nerror(char *fmt, ...)
00062 {
00063        int err = errno;
00064        va_list ap;
00065        va_start(ap,fmt);
00066        fprintf(stderr, "numactl: ");
00067        vfprintf(stderr, fmt, ap);
00068        va_end(ap);
00069        if (err)
00070               fprintf(stderr,": %s\n", strerror(err));
00071        else
00072               fputc('\n', stderr);
00073        exit(1);
00074 }
00075 
00076 long memsize(char *s)
00077 {
00078        char *end;
00079        long length = strtoul(s,&end,0);
00080        switch (toupper(*end)) {
00081        case 'G': length *= 1024;  /*FALL THROUGH*/
00082        case 'M': length *= 1024;  /*FALL THROUGH*/
00083        case 'K': length *= 1024; break;
00084        }
00085        return length;
00086 }
00087 
00088 
00089 static struct policy {
00090        char *name;
00091        int policy;
00092        int noarg;
00093 } policies[] = {
00094        { "interleave", MPOL_INTERLEAVE, },
00095        { "membind",    MPOL_BIND, },
00096        { "preferred",   MPOL_PREFERRED, },
00097        { "default",    MPOL_DEFAULT, 1 },
00098        { NULL },
00099 };
00100 
00101 static char *policy_names[] = { "default", "preferred", "bind", "interleave" };
00102 
00103 char *policy_name(int policy)
00104 {
00105        static char buf[32];
00106        if (policy >= array_len(policy_names)) {
00107               sprintf(buf, "[%d]", policy);
00108               return buf;
00109        }
00110        return policy_names[policy];
00111 }
00112 
00113 int parse_policy(char *name, char *arg)
00114 {
00115        int k;
00116        struct policy *p = NULL;
00117        if (!name)
00118               return MPOL_DEFAULT;
00119        for (k = 0; policies[k].name; k++) {
00120               p = &policies[k];
00121               if (!strcmp(p->name, name))
00122                      break;
00123        }
00124        if (!p || !p->name || (!arg && !p->noarg))
00125               usage();
00126     return p->policy;
00127 }
00128 
00129 void print_policies(void)
00130 {
00131        int i;
00132        printf("Policies:");
00133        for (i = 0; policies[i].name; i++)
00134               printf(" %s", policies[i].name);
00135        printf("\n");
00136 }