static void gather_node_cpus(char *cpus, long nd) { int ncpus = 0; int i; long online; char buf[BUFSIZ]; char path[BUFSIZ], path1[BUFSIZ]; while (path_exist(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus)) ncpus++; for (i = 0; i < ncpus; i++) { snprintf(path, BUFSIZ, PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i); if (path_exist(path)) { snprintf(path1, BUFSIZ, "%s/online", path); /* * if there is no online knob, then the cpu cannot * be taken offline */ if (path_exist(path1)) { SAFE_FILE_SCANF(cleanup, path1, "%ld", &online); if (online == 0) continue; } sprintf(buf, "%d,", i); strcat(cpus, buf); } } /* Remove the trailing comma. */ cpus[strlen(cpus) - 1] = '\0'; }
static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable) { unsigned int cpu; int online, rc; int configured = -1; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { printf(_("CPU %d does not exist\n"), cpu); continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/online", cpu)) { printf(_("CPU %d is not hot pluggable\n"), cpu); continue; } online = path_getnum(_PATH_SYS_CPU "/cpu%d/online", cpu); if ((online == 1) && (enable == 1)) { printf(_("CPU %d is already enabled\n"), cpu); continue; } if ((online == 0) && (enable == 0)) { printf(_("CPU %d is already disabled\n"), cpu); continue; } if (path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) configured = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu); if (enable) { rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/online", cpu); if ((rc == -1) && (configured == 0)) printf(_("CPU %d enable failed " "(CPU is deconfigured)\n"), cpu); else if (rc == -1) printf(_("CPU %d enable failed (%m)\n"), cpu); else printf(_("CPU %d enabled\n"), cpu); } else { if (onlinecpus && num_online_cpus() == 1) { printf(_("CPU %d disable failed " "(last enabled CPU)\n"), cpu); continue; } rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/online", cpu); if (rc == -1) printf(_("CPU %d disable failed (%m)\n"), cpu); else { printf(_("CPU %d disabled\n"), cpu); if (onlinecpus) CPU_CLR(cpu, onlinecpus); } } } return EXIT_SUCCESS; }
int ipc_sem_get_limits(struct ipc_limits *lim) { FILE *f; int rc = 0; lim->semvmx = SEMVMX; f = path_fopen("r", 0, _PATH_PROC_IPC_SEM); if (f) { rc = fscanf(f, "%d\t%d\t%d\t%d", &lim->semmsl, &lim->semmns, &lim->semopm, &lim->semmni); fclose(f); } if (rc == 4) { struct seminfo seminfo; union semun arg = { .array = (ushort *) &seminfo }; if (semctl(0, 0, IPC_INFO, arg) < 0) return 1; lim->semmni = seminfo.semmni; lim->semmsl = seminfo.semmsl; lim->semmns = seminfo.semmns; lim->semopm = seminfo.semopm; } return 0; } int ipc_shm_get_limits(struct ipc_limits *lim) { lim->shmmin = SHMMIN; if (path_exist(_PATH_PROC_IPC_SHMALL) && path_exist(_PATH_PROC_IPC_SHMMAX) && path_exist(_PATH_PROC_IPC_SHMMNI)) { lim->shmall = path_read_u64(_PATH_PROC_IPC_SHMALL); lim->shmmax = path_read_u64(_PATH_PROC_IPC_SHMMAX); lim->shmmni = path_read_u64(_PATH_PROC_IPC_SHMMNI); } else { struct shminfo shminfo; if (shmctl(0, IPC_INFO, (struct shmid_ds *) &shminfo) < 0) return 1; lim->shmmni = shminfo.shmmni; lim->shmall = shminfo.shmall; } return 0; }
/* returns: 0 = success * < 0 = failure * > 0 = partial success */ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure) { unsigned int cpu; int rc, current; size_t fails = 0; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { warnx(_("CPU %u does not exist"), cpu); fails++; continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) { warnx(_("CPU %u is not configurable"), cpu); fails++; continue; } current = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu); if ((current == 1) && (configure == 1)) { printf(_("CPU %u is already configured\n"), cpu); continue; } if ((current == 0) && (configure == 0)) { printf(_("CPU %u is already deconfigured\n"), cpu); continue; } if ((current == 1) && (configure == 0) && onlinecpus && is_cpu_online(cpu)) { warnx(_("CPU %u deconfigure failed (CPU is enabled)"), cpu); fails++; continue; } if (configure) { rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) { warn(_("CPU %u configure failed"), cpu); fails++; } else printf(_("CPU %u configured\n"), cpu); } else { rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) { warn(_("CPU %u deconfigure failed"), cpu); fails++; } else printf(_("CPU %u deconfigured\n"), cpu); } } return fails == 0 ? 0 : fails == setsize ? -1 : 1; }
static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure) { unsigned int cpu; int rc, current; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { printf(_("CPU %d does not exist\n"), cpu); continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) { printf(_("CPU %d is not configurable\n"), cpu); continue; } current = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu); if ((current == 1) && (configure == 1)) { printf(_("CPU %d is already configured\n"), cpu); continue; } if ((current == 0) && (configure == 0)) { printf(_("CPU %d is already deconfigured\n"), cpu); continue; } if ((current == 1) && (configure == 0) && onlinecpus && is_cpu_online(cpu)) { printf(_("CPU %d deconfigure failed " "(CPU is enabled)\n"), cpu); continue; } if (configure) { rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) printf(_("CPU %d configure failed (%m)\n"), cpu); else printf(_("CPU %d configured\n"), cpu); } else { rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/configure", cpu); if (rc == -1) printf(_("CPU %d deconfigure failed (%m)\n"), cpu); else printf(_("CPU %d deconfigured\n"), cpu); } } return EXIT_SUCCESS; }
static void _gather_cpus(char *cpus, long nd) { int ncpus = 0; int i; char buf[BUFSIZ]; while (path_exist(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus)) ncpus++; for (i = 0; i < ncpus; i++) if (path_exist(PATH_SYS_SYSTEM "/node/node%ld/cpu%d", nd, i)) { sprintf(buf, "%d,", i); strcat(cpus, buf); } /* Remove the trailing comma. */ cpus[strlen(cpus) - 1] = '\0'; }
int path_exists(int *maze, int M, int N, int x1, int y1, int x2, int y2) { if (x1 >= M || x1 < 0 || x2 >= M || x2 < 0 || y1 >= N || y1 < 0 || y2 >= N || y2 < 0) return 0; if (maze[x2*N + y2] == 0 || maze[x1*N + y1] == 0) return 0; return path_exist(maze, M, N, x1, y1, x2, y2); }
static int cpu_rescan(void) { if (!path_exist(_PATH_SYS_CPU_RESCAN)) errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs")); if (path_writestr("1", _PATH_SYS_CPU_RESCAN) == -1) err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs")); printf(_("Triggered rescan of CPUs\n")); return EXIT_SUCCESS; }
static long count_cpu(void) { int ncpus = 0; while (path_exist(PATH_SYS_SYSTEM "/cpu/cpu%d", ncpus)) ncpus++; return ncpus; }
long count_numa(void) { int nnodes = 0; while(path_exist(PATH_SYS_SYSTEM "/node/node%d", nnodes)) nnodes++; return nnodes; }
int ipc_msg_get_limits(struct ipc_limits *lim) { if (path_exist(_PATH_PROC_IPC_MSGMNI) && path_exist(_PATH_PROC_IPC_MSGMNB) && path_exist(_PATH_PROC_IPC_MSGMAX)) { lim->msgmni = path_read_s32(_PATH_PROC_IPC_MSGMNI); lim->msgmnb = path_read_s32(_PATH_PROC_IPC_MSGMNB); lim->msgmax = path_read_s32(_PATH_PROC_IPC_MSGMAX); } else { struct msginfo msginfo; if (msgctl(0, IPC_INFO, (struct msqid_ds *) &msginfo) < 0) return 1; lim->msgmni = msginfo.msgmni; lim->msgmnb = msginfo.msgmnb; lim->msgmax = msginfo.msgmax; } return 0; }
/* Find if there is a connecting path between two given points in the maze. The maze is a matrix of order MxN, check if there exists a path between (x1,y1) and (x2,y2). Two points are said to connected if there is a series of 1's non-diagonally. Example: Maze of order 4x4: 1 0 1 0 1 1 0 0 0 1 0 1 0 1 1 1 (x1,y1):(0,0) (x2,y2):(2,3) In this case, there exists a connected path: 1 1 1 1 1 1 1 1 Since there is a connected path, your function has to return 1. If a path doesn't exist between two co-ordinates then return 0. If the co-ordinates are invalid or size of the matrix is invalid then return 0. Function Prototype :path_exists(int *maze, int rows, int columns, int x1, int y1, int x2, int y2) ; Maze: Pointer to first element of an array . rows : No of rows columns : Columns x1,x2,y1,y2: Co-ordinates Hint : You can use path_exists as a wrapper function for your original recursive function which might take more parameters . */ int path_exist(int *maze, int M, int N, int x1, int y1, int x2, int y2) { int x, y, a = 0, b = 0, t, c = 0, d = 0; if (x1 == x2 && y1 == y2) return 1; t = maze[x1*N + y1]; maze[x1*N + y1] = -1; if (x1 - 1 >= 0) if (maze[(x1 - 1)*N + y1] == 1) a = path_exist(maze, M, N, x1 - 1, y1, x2, y2); if (x1 + 1 < M) if (maze[(x1 + 1)*N + y1] == 1) b = path_exist(maze, M, N, x1 + 1, y1, x2, y2); if (y1 - 1 >= 0) if (maze[(x1)*N + y1 - 1] == 1) c = path_exist(maze, M, N, x1, y1 - 1, x2, y2); if (y1 + 1<N) if (maze[(x1)*N + y1 + 1] == 1) d = path_exist(maze, M, N, x1, y1 + 1, x2, y2); maze[x1*N + y1] = t; if (a == 1 || b == 1 || c == 1 || d == 1) return 1; return 0; }
//------------------------------------------------------------------- // FileName::recursive_chmod_dir //------------------------------------------------------------------- void FileName::recursive_chmod_dir(mode_t mode) throw( Exception ) { if( !path_exist() ) { // File doesn't exists String strErr = String::str_format(ERR_DIR_NOT_FOUND, PSZ(full_name())); throw FileNotFoundException(PSZ(strErr), "FileName::recursive_chmod_dir"); } // Recursively change rights on sub-directories FileEnum dirEnum(full_name(), FileEnum::ENUM_DIR); while( dirEnum.find() ) dirEnum.recursive_chmod_dir(mode); // Change mode to directory itself chmod(mode); }
static int cpu_set_dispatch(int mode) { if (!path_exist(_PATH_SYS_CPU_DISPATCH)) errx(EXIT_FAILURE, _("This system does not support setting " "the dispatching mode of CPUs")); if (mode == 0) { if (path_writestr("0", _PATH_SYS_CPU_DISPATCH) == -1) err(EXIT_FAILURE, _("Failed to set horizontal dispatch mode")); printf(_("Successfully set horizontal dispatching mode\n")); } else { if (path_writestr("1", _PATH_SYS_CPU_DISPATCH) == -1) err(EXIT_FAILURE, _("Failed to set vertical dispatch mode")); printf(_("Successfully set vertical dispatching mode\n")); } return EXIT_SUCCESS; }
//------------------------------------------------------------------- // FileName::recursive_chown //------------------------------------------------------------------- void FileName::recursive_chown(uid_t uid, gid_t gid) throw( Exception ) { if( !path_exist() ) { // File doesn't exists String strErr = String::str_format(ERR_DIR_NOT_FOUND, PSZ(full_name())); throw FileNotFoundException(PSZ(strErr), "FileName::recursive_chmod"); } // Recursively change rights on sub-directories FileEnum dirEnum(full_name(), FileEnum::ENUM_DIR); while( dirEnum.find() ) dirEnum.recursive_chmod(uid, gid); // Change mode on files FileEnum fileEnum(full_name(), FileEnum::ENUM_FILE); while( fileEnum.find() ) // Copy with metadata fileEnum.chown(uid, gid); // Change mode to directory itself chown(uid, gid); }
/* CORPO DELLE FUNZIONI */ int main(int argc, char *argv[]) { stack_t *input; item_t *comand; user_t *user = NULL; /* se non esiste la cartella base, la creiamo */ if(path_exist(BASE) < 0){ if(create_directory(BASE, 0777) < 0){ perror("create_directory"); exit(EXIT_FAILURE); } } /* leggiamo l'input da linea comando */ input = read_input(argv, argc); if(input == NULL){ perror("read_input"); exit(EXIT_FAILURE); } /* ordiniamo le richieste */ stack_sort(input); /* eseguimo tutte le richieste */ while(input ->len > 0){ /* estraiamo l'azione corrente */ comand = stack_pop(input); if(comand == NULL){ perror("stack_pop"); exit(EXIT_FAILURE); } /* eseguiamo il comando */ if(execute_comand(comand ->value, comand ->arg, &user) < 0){ perror("execute_program"); exit(EXIT_FAILURE); } } /* usciamo dal programma */ exit(EXIT_SUCCESS); }
int _linux_get_cpu_info( PAPI_hw_info_t * hwinfo ) { int tmp, retval = PAPI_OK; char maxargs[PAPI_HUGE_STR_LEN], *t, *s; float mhz = 0.0; FILE *f; if ( ( f = fopen( "/proc/cpuinfo", "r" ) ) == NULL ) { PAPIERROR( "fopen(/proc/cpuinfo) errno %d", errno ); return PAPI_ESYS; } /* All of this information maybe overwritten by the substrate */ /* MHZ */ rewind( f ); s = search_cpu_info( f, "clock", maxargs ); if ( !s ) { rewind( f ); s = search_cpu_info( f, "cpu MHz", maxargs ); } if ( s ) sscanf( s + 1, "%f", &mhz ); hwinfo->mhz = mhz; hwinfo->clock_mhz = ( int ) mhz; /* Vendor Name and Vendor Code */ rewind( f ); s = search_cpu_info( f, "vendor_id", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; strcpy( hwinfo->vendor_string, s + 2 ); } else { rewind( f ); s = search_cpu_info( f, "vendor", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; strcpy( hwinfo->vendor_string, s + 2 ); } else { rewind( f ); s = search_cpu_info( f, "system type", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; s = strtok( s + 2, " " ); strcpy( hwinfo->vendor_string, s ); } else { rewind( f ); s = search_cpu_info( f, "platform", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; s = strtok( s + 2, " " ); if ( ( strcasecmp( s, "pSeries" ) == 0 ) || ( strcasecmp( s, "PowerMac" ) == 0 ) ) { strcpy( hwinfo->vendor_string, "IBM" ); } } else { rewind( f ); s = search_cpu_info( f, "CPU implementer", maxargs ); if ( s ) { strcpy( hwinfo->vendor_string, "ARM" ); } } } } } if ( strlen( hwinfo->vendor_string ) ) decode_vendor_string( hwinfo->vendor_string, &hwinfo->vendor ); /* Revision */ rewind( f ); s = search_cpu_info( f, "stepping", maxargs ); if ( s ) { sscanf( s + 1, "%d", &tmp ); hwinfo->revision = ( float ) tmp; hwinfo->cpuid_stepping = tmp; } else { rewind( f ); s = search_cpu_info( f, "revision", maxargs ); if ( s ) { sscanf( s + 1, "%d", &tmp ); hwinfo->revision = ( float ) tmp; hwinfo->cpuid_stepping = tmp; } } /* Model Name */ rewind( f ); s = search_cpu_info( f, "model name", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; strcpy( hwinfo->model_string, s + 2 ); } else { rewind( f ); s = search_cpu_info( f, "family", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; strcpy( hwinfo->model_string, s + 2 ); } else { rewind( f ); s = search_cpu_info( f, "cpu model", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; strtok( s + 2, " " ); s = strtok( NULL, " " ); strcpy( hwinfo->model_string, s ); } else { rewind( f ); s = search_cpu_info( f, "cpu", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; /* get just the first token */ s = strtok( s + 2, " " ); strcpy( hwinfo->model_string, s ); } } } } /* Family */ rewind( f ); s = search_cpu_info( f, "family", maxargs ); if ( s ) { sscanf( s + 1, "%d", &tmp ); hwinfo->cpuid_family = tmp; } else { rewind( f ); s = search_cpu_info( f, "cpu family", maxargs ); if ( s ) { sscanf( s + 1, "%d", &tmp ); hwinfo->cpuid_family = tmp; } } /* CPU Model */ rewind( f ); s = search_cpu_info( f, "model", maxargs ); if ( s ) { sscanf( s + 1, "%d", &tmp ); hwinfo->model = tmp; hwinfo->cpuid_model = tmp; } fclose( f ); /* The following new members are set using the same methodology used in lscpu. */ /* Total number of CPUs */ /* The following line assumes totalcpus was initialized to zero! */ while ( path_exist( _PATH_SYS_SYSTEM "/cpu/cpu%d", hwinfo->totalcpus ) ) hwinfo->totalcpus++; /* Number of threads per core */ if ( path_exist( _PATH_SYS_CPU0 "/topology/thread_siblings" ) ) hwinfo->threads = path_sibling( _PATH_SYS_CPU0 "/topology/thread_siblings" ); /* Number of cores per socket */ if ( path_exist( _PATH_SYS_CPU0 "/topology/core_siblings" ) && hwinfo->threads > 0 ) hwinfo->cores = path_sibling( _PATH_SYS_CPU0 "/topology/core_siblings" ) / hwinfo->threads; /* Number of sockets */ if ( hwinfo->threads > 0 && hwinfo->cores > 0 ) hwinfo->sockets = hwinfo->ncpu / hwinfo->cores / hwinfo->threads; /* Number of NUMA nodes */ /* The following line assumes nnodes was initialized to zero! */ while ( path_exist( _PATH_SYS_SYSTEM "/node/node%d", hwinfo->nnodes ) ) hwinfo->nnodes++; /* Number of CPUs per node */ hwinfo->ncpu = hwinfo->nnodes > 1 ? hwinfo->totalcpus / hwinfo->nnodes : hwinfo->totalcpus; #if 0 int *nodecpu; /* cpumap data is not currently part of the _papi_hw_info struct */ nodecpu = malloc( (unsigned int) hwinfo->nnodes * sizeof(int) ); if ( nodecpu ) { int i; for ( i = 0; i < hwinfo->nnodes; ++i ) { nodecpu[i] = path_sibling( _PATH_SYS_SYSTEM "/node/node%d/cpumap", i ); } } else { PAPIERROR( "malloc failed for variable not currently used" ); } #endif return retval; }
int main(int argc, char *argv[]) { cpu_set_t *cpu_set; size_t setsize; int cmd = -1; int c, rc; static const struct option longopts[] = { { "configure", required_argument, NULL, 'c' }, { "deconfigure",required_argument, NULL, 'g' }, { "disable", required_argument, NULL, 'd' }, { "dispatch", required_argument, NULL, 'p' }, { "enable", required_argument, NULL, 'e' }, { "help", no_argument, NULL, 'h' }, { "rescan", no_argument, NULL, 'r' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ { 'c','d','e','g','p' }, { 0 } }; int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); maxcpus = get_max_number_of_cpus(); if (maxcpus < 1) errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting")); if (path_exist(_PATH_SYS_CPU_ONLINE)) onlinecpus = path_read_cpulist(maxcpus, _PATH_SYS_CPU_ONLINE); setsize = CPU_ALLOC_SIZE(maxcpus); cpu_set = CPU_ALLOC(maxcpus); if (!cpu_set) err(EXIT_FAILURE, _("cpuset_alloc failed")); while ((c = getopt_long(argc, argv, "c:d:e:g:hp:rV", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); switch (c) { case 'c': cmd = CMD_CPU_CONFIGURE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'd': cmd = CMD_CPU_DISABLE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'e': cmd = CMD_CPU_ENABLE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'g': cmd = CMD_CPU_DECONFIGURE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'h': usage(stdout); case 'p': if (strcmp("horizontal", argv[optind - 1]) == 0) cmd = CMD_CPU_DISPATCH_HORIZONTAL; else if (strcmp("vertical", argv[optind - 1]) == 0) cmd = CMD_CPU_DISPATCH_VERTICAL; else errx(EXIT_FAILURE, _("unsupported argument: %s"), argv[optind -1 ]); break; case 'r': cmd = CMD_CPU_RESCAN; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; default: errtryhelp(EXIT_FAILURE); } } if ((argc == 1) || (argc != optind)) usage(stderr); switch (cmd) { case CMD_CPU_ENABLE: rc = cpu_enable(cpu_set, maxcpus, 1); break; case CMD_CPU_DISABLE: rc = cpu_enable(cpu_set, maxcpus, 0); break; case CMD_CPU_CONFIGURE: rc = cpu_configure(cpu_set, maxcpus, 1); break; case CMD_CPU_DECONFIGURE: rc = cpu_configure(cpu_set, maxcpus, 0); break; case CMD_CPU_RESCAN: rc = cpu_rescan(); break; case CMD_CPU_DISPATCH_HORIZONTAL: rc = cpu_set_dispatch(0); break; case CMD_CPU_DISPATCH_VERTICAL: rc = cpu_set_dispatch(1); break; default: rc = -EINVAL; break; } return rc == 0 ? EXIT_SUCCESS : rc < 0 ? EXIT_FAILURE : CHCPU_EXIT_SOMEOK; }
/* returns: 0 = success * < 0 = failure * > 0 = partial success */ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable) { unsigned int cpu; int online, rc; int configured = -1; size_t fails = 0; for (cpu = 0; cpu < setsize; cpu++) { if (!CPU_ISSET(cpu, cpu_set)) continue; if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) { warnx(_("CPU %u does not exist"), cpu); fails++; continue; } if (!path_exist(_PATH_SYS_CPU "/cpu%d/online", cpu)) { warnx(_("CPU %u is not hot pluggable"), cpu); fails++; continue; } online = path_read_s32(_PATH_SYS_CPU "/cpu%d/online", cpu); if ((online == 1) && (enable == 1)) { printf(_("CPU %u is already enabled\n"), cpu); continue; } if ((online == 0) && (enable == 0)) { printf(_("CPU %u is already disabled\n"), cpu); continue; } if (path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) configured = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu); if (enable) { rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/online", cpu); if ((rc == -1) && (configured == 0)) { warn(_("CPU %u enable failed (CPU is deconfigured)"), cpu); fails++; } else if (rc == -1) { warn(_("CPU %u enable failed"), cpu); fails++; } else printf(_("CPU %u enabled\n"), cpu); } else { if (onlinecpus && num_online_cpus() == 1) { warnx(_("CPU %u disable failed (last enabled CPU)"), cpu); fails++; continue; } rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/online", cpu); if (rc == -1) { warn(_("CPU %u disable failed"), cpu); fails++; } else { printf(_("CPU %u disabled\n"), cpu); if (onlinecpus) CPU_CLR(cpu, onlinecpus); } } } return fails == 0 ? 0 : fails == setsize ? -1 : 1; }
int _linux_get_cpu_info( PAPI_hw_info_t *hwinfo, int *cpuinfo_mhz ) { int retval = PAPI_OK; unsigned int strSize; char maxargs[PAPI_HUGE_STR_LEN], *t, *s; float mhz = 0.0; FILE *f; char cpuinfo_filename[]="/proc/cpuinfo"; if ( ( f = fopen( cpuinfo_filename, "r" ) ) == NULL ) { PAPIERROR( "fopen(/proc/cpuinfo) errno %d", errno ); return PAPI_ESYS; } /* All of this information may be overwritten by the component */ /***********************/ /* Attempt to find MHz */ /***********************/ rewind( f ); s = search_cpu_info( f, "clock", maxargs ); if ( !s ) { rewind( f ); s = search_cpu_info( f, "cpu MHz", maxargs ); } if ( s ) { sscanf( s + 1, "%f", &mhz ); } *cpuinfo_mhz = mhz; /*******************************/ /* Vendor Name and Vendor Code */ /*******************************/ /* First try to read "vendor_id" field */ /* Which is the most common field */ hwinfo->vendor_string[0]=0; rewind( f ); s = search_cpu_info( f, "vendor_id", maxargs ); strSize = sizeof(hwinfo->vendor_string); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; if (strlen(s+2) >= strSize-1) { s[strSize+1] = '\0'; } strcpy( hwinfo->vendor_string, s + 2 ); } /* If not found, try "vendor" which seems to be Itanium specific */ if (!hwinfo->vendor_string[0]) { rewind( f ); s = search_cpu_info( f, "vendor", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; if (strlen(s+2) >= strSize-1) { s[strSize+1] = '\0'; } strcpy( hwinfo->vendor_string, s + 2 ); } } /* "system type" seems to be MIPS and Alpha */ if (!hwinfo->vendor_string[0]) { rewind( f ); s = search_cpu_info( f, "system type", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; s = strtok( s + 2, " " ); if (strlen(s) >= strSize-1) { s[strSize-1] = '\0'; } strcpy( hwinfo->vendor_string, s ); } } /* "platform" indicates Power */ if (!hwinfo->vendor_string[0]) { rewind( f ); s = search_cpu_info( f, "platform", maxargs ); if ( s && ( t = strchr( s + 2, '\n' ) ) ) { *t = '\0'; s = strtok( s + 2, " " ); if ( ( strcasecmp( s, "pSeries" ) == 0 ) || ( strcasecmp( s, "PowerNV" ) == 0 ) || ( strcasecmp( s, "PowerMac" ) == 0 ) ) { strcpy( hwinfo->vendor_string, "IBM" ); } } } /* "CPU implementer" indicates ARM */ if (!hwinfo->vendor_string[0]) { rewind( f ); s = search_cpu_info( f, "CPU implementer", maxargs ); if ( s ) { strcpy( hwinfo->vendor_string, "ARM" ); } } /* Decode the string to an implementer value */ if ( strlen( hwinfo->vendor_string ) ) { decode_vendor_string( hwinfo->vendor_string, &hwinfo->vendor ); } /**********************************************/ /* Provide more stepping/model/family numbers */ /**********************************************/ if ((hwinfo->vendor==PAPI_VENDOR_INTEL) || (hwinfo->vendor==PAPI_VENDOR_AMD)) { decode_cpuinfo_x86(f,hwinfo); } if (hwinfo->vendor==PAPI_VENDOR_IBM) { decode_cpuinfo_power(f,hwinfo); } if (hwinfo->vendor==PAPI_VENDOR_ARM) { decode_cpuinfo_arm(f,hwinfo); } /* The following members are set using the same methodology */ /* used in lscpu. */ /* Total number of CPUs */ /* The following line assumes totalcpus was initialized to zero! */ while ( path_exist( _PATH_SYS_SYSTEM "/cpu/cpu%d", hwinfo->totalcpus ) ) hwinfo->totalcpus++; /* Number of threads per core */ if ( path_exist( _PATH_SYS_CPU0 "/topology/thread_siblings" ) ) hwinfo->threads = path_sibling( _PATH_SYS_CPU0 "/topology/thread_siblings" ); /* Number of cores per socket */ if ( path_exist( _PATH_SYS_CPU0 "/topology/core_siblings" ) && hwinfo->threads > 0 ) hwinfo->cores = path_sibling( _PATH_SYS_CPU0 "/topology/core_siblings" ) / hwinfo->threads; /* Number of NUMA nodes */ /* The following line assumes nnodes was initialized to zero! */ while ( path_exist( _PATH_SYS_SYSTEM "/node/node%d", hwinfo->nnodes ) ) hwinfo->nnodes++; /* Number of CPUs per node */ hwinfo->ncpu = hwinfo->nnodes > 1 ? hwinfo->totalcpus / hwinfo->nnodes : hwinfo->totalcpus; /* Number of sockets */ if ( hwinfo->threads > 0 && hwinfo->cores > 0 ) hwinfo->sockets = hwinfo->totalcpus / hwinfo->cores / hwinfo->threads; #if 0 int *nodecpu; /* cpumap data is not currently part of the _papi_hw_info struct */ nodecpu = malloc( (unsigned int) hwinfo->nnodes * sizeof(int) ); if ( nodecpu ) { int i; for ( i = 0; i < hwinfo->nnodes; ++i ) { nodecpu[i] = path_sibling( _PATH_SYS_SYSTEM "/node/node%d/cpumap", i ); } } else { PAPIERROR( "malloc failed for variable not currently used" ); } #endif /* Fixup missing Megahertz Value */ /* This is missing from cpuinfo on ARM and MIPS */ if (*cpuinfo_mhz < 1.0) { rewind( f ); s = search_cpu_info( f, "BogoMIPS", maxargs ); if ((!s) || (sscanf( s + 1, "%f", &mhz ) != 1)) { INTDBG("Mhz detection failed. Please edit file %s at line %d.\n", __FILE__,__LINE__); } if (hwinfo->vendor == PAPI_VENDOR_MIPS) { /* MIPS has 2x clock multiplier */ *cpuinfo_mhz = 2*(((int)mhz)+1); /* Also update version info on MIPS */ rewind( f ); s = search_cpu_info( f, "cpu model", maxargs ); s = strstr(s+1," V")+2; strtok(s," "); sscanf(s, "%f ", &hwinfo->revision ); } else { /* In general bogomips is proportional to number of CPUs */ if (hwinfo->totalcpus) { if (mhz!=0) *cpuinfo_mhz = mhz / hwinfo->totalcpus; } } } fclose( f ); return retval; }
int main(int argc, char *argv[]) { cpu_set_t *cpu_set; size_t setsize; int cmd = -1; int c; enum { EXCL_NONE, EXCL_CONFIGURE, EXCL_DECONFIGURE, EXCL_DISABLE, EXCL_DISPATCH, EXCL_ENABLE }; int excl_any = EXCL_NONE; static const struct option longopts[] = { { "configure", required_argument, 0, 'c' }, { "deconfigure",required_argument, 0, 'g' }, { "disable", required_argument, 0, 'd' }, { "dispatch", required_argument, 0, 'p' }, { "enable", required_argument, 0, 'e' }, { "help", no_argument, 0, 'h' }, { "rescan", no_argument, 0, 'r' }, { "version", no_argument, 0, 'V' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); maxcpus = get_max_number_of_cpus(); if (maxcpus < 1) errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting")); if (path_exist(_PATH_SYS_CPU_ONLINE)) onlinecpus = path_cpulist(maxcpus, _PATH_SYS_CPU_ONLINE); setsize = CPU_ALLOC_SIZE(maxcpus); cpu_set = CPU_ALLOC(maxcpus); if (!cpu_set) err(EXIT_FAILURE, _("cpuset_alloc failed")); while ((c = getopt_long(argc, argv, "c:d:e:g:hp:rV", longopts, NULL)) != -1) { switch (c) { case 'c': exclusive_option(&excl_any, EXCL_CONFIGURE, EXCL_ERROR); cmd = CMD_CPU_CONFIGURE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'd': exclusive_option(&excl_any, EXCL_DISABLE, EXCL_ERROR); cmd = CMD_CPU_DISABLE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'e': exclusive_option(&excl_any, EXCL_ENABLE, EXCL_ERROR); cmd = CMD_CPU_ENABLE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'g': exclusive_option(&excl_any, EXCL_DECONFIGURE, EXCL_ERROR); cmd = CMD_CPU_DECONFIGURE; cpu_parse(argv[optind - 1], cpu_set, setsize); break; case 'h': usage(stdout); case 'p': exclusive_option(&excl_any, EXCL_DISPATCH, EXCL_ERROR); if (strcmp("horizontal", argv[optind - 1]) == 0) cmd = CMD_CPU_DISPATCH_HORIZONTAL; else if (strcmp("vertical", argv[optind - 1]) == 0) cmd = CMD_CPU_DISPATCH_VERTICAL; else errx(EXIT_FAILURE, _("unsupported argument: %s"), argv[optind -1 ]); break; case 'r': cmd = CMD_CPU_RESCAN; break; case 'V': printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); return EXIT_SUCCESS; default: usage(stderr); } } if ((argc == 1) || (argc != optind)) usage(stderr); switch (cmd) { case CMD_CPU_ENABLE: return cpu_enable(cpu_set, maxcpus, 1); case CMD_CPU_DISABLE: return cpu_enable(cpu_set, maxcpus, 0); case CMD_CPU_CONFIGURE: return cpu_configure(cpu_set, maxcpus, 1); case CMD_CPU_DECONFIGURE: return cpu_configure(cpu_set, maxcpus, 0); case CMD_CPU_RESCAN: return cpu_rescan(); case CMD_CPU_DISPATCH_HORIZONTAL: return cpu_set_dispatch(0); case CMD_CPU_DISPATCH_VERTICAL: return cpu_set_dispatch(1); } return EXIT_SUCCESS; }