static void showtop(int cputimemode, int r) { #define NLOADS 3 double loads[NLOADS]; int nloads, lines = 0; struct winsize winsize; if(ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize) != 0) { perror("TIOCGWINSZ"); fprintf(stderr, "TIOCGWINSZ failed\n"); exit(1); } get_procs(); if (prev_proc == NULL) get_procs(); if((nloads = getloadavg(loads, NLOADS)) != NLOADS) { fprintf(stderr, "getloadavg() failed - %d loads\n", nloads); exit(1); } printf("%s", Tclr_all); lines += print_load(loads, NLOADS); lines += print_proc_summary(proc); lines += print_memory(); if(winsize.ws_row > 0) r = winsize.ws_row; print_procs(r - lines - 2, prev_proc, proc, cputimemode); fflush(NULL); }
/* % gcc -DDEBUG_MODULE get_mach_stat.c -I../../.. -g -DUSE_CPU_SPEED */ int main(int argc, char * argv[]) { int error_code; uint16_t sockets, cores, threads; uint16_t block_map_size; uint16_t *block_map, *block_map_inv; struct config_record this_node; char node_name[MAX_SLURM_NAME]; float speed; uint16_t testnumproc = 0; uint32_t up_time = 0; int days, hours, mins, secs; char* _cpuinfo_path = "/proc/cpuinfo"; if (argc > 1) { _cpuinfo_path = argv[1]; testnumproc = 1024; /* since may not match test host */ } debug3("%s:", _cpuinfo_path); error_code = get_mach_name(node_name); if (error_code != 0) exit(1); /* The show is all over without a node name */ error_code += get_procs(&this_node.cpus); error_code += get_cpuinfo(MAX(this_node.cpus, testnumproc), &this_node.sockets, &this_node.cores, &this_node.threads, &block_map_size, &block_map, &block_map_inv); xfree(block_map); /* not used here */ xfree(block_map_inv); /* not used here */ error_code += get_memory(&this_node.real_memory); error_code += get_tmp_disk(&this_node.tmp_disk, "/tmp"); error_code += get_up_time(&up_time); #ifdef USE_CPU_SPEED error_code += get_speed(&speed); #endif debug3(""); debug3("NodeName=%s CPUs=%u Sockets=%u Cores=%u Threads=%u", node_name, this_node.cpus, this_node.sockets, this_node.cores, this_node.threads); debug3("\tRealMemory=%u TmpDisk=%u Speed=%f", this_node.real_memory, this_node.tmp_disk, speed); secs = up_time % 60; mins = (up_time / 60) % 60; hours = (up_time / 3600) % 24; days = (up_time / 86400); debug3("\tUpTime=%u=%u-%2.2u:%2.2u:%2.2u", up_time, days, hours, mins, secs); if (error_code != 0) debug3("get_mach_stat error_code=%d encountered", error_code); exit (error_code); }
int main(int argc, const char *argv[]) { std::cout << "user " + get_user() << std::endl; std::cout << "hostname " + get_hostname() << std::endl; std::cout << "time " + get_time() << std::endl; std::cout << "procs " + get_procs() << std::endl; std::cout << "cpu " + cpu_usage() << std::endl; std::cout << "mem " + (float)mem_usage() << std::endl; return 0; }
int xcpuinfo_init(void) { if ( initialized ) return XCPUINFO_SUCCESS; if ( get_procs(&procs) ) return XCPUINFO_ERROR; if ( get_cpuinfo(procs,&sockets,&cores,&threads, &block_map_size,&block_map,&block_map_inv) ) return XCPUINFO_ERROR; initialized = true ; return XCPUINFO_SUCCESS; }
extern int task_cgroup_devices_init(slurm_cgroup_conf_t *slurm_cgroup_conf) { uint16_t cpunum; /* initialize cpuinfo internal data */ if ( xcpuinfo_init() != XCPUINFO_SUCCESS ) return SLURM_ERROR; /* initialize user/job/jobstep cgroup relative paths */ user_cgroup_path[0] = '\0'; job_cgroup_path[0] = '\0'; jobstep_cgroup_path[0] = '\0'; /* initialize allowed_devices_filename */ cgroup_allowed_devices_file[0] = '\0'; if ( get_procs(&cpunum) != 0 ) { error("task/cgroup: unable to get a number of CPU"); goto error; } (void) gres_plugin_node_config_load(cpunum, conf->node_name, NULL); strcpy(cgroup_allowed_devices_file, slurm_cgroup_conf->allowed_devices_file); if (xcgroup_ns_create(slurm_cgroup_conf, &devices_ns, "", "devices") != XCGROUP_SUCCESS ) { error("task/cgroup: unable to create devices namespace"); goto error; } return SLURM_SUCCESS; error: xcgroup_ns_destroy(&devices_ns); xcpuinfo_fini(); return SLURM_ERROR; }
/* Note: file static for qsort/_compare_cpus*/ extern int get_cpuinfo(uint16_t *p_cpus, uint16_t *p_boards, uint16_t *p_sockets, uint16_t *p_cores, uint16_t *p_threads, uint16_t *p_block_map_size, uint16_t **p_block_map, uint16_t **p_block_map_inv) { int retval; uint16_t numproc; uint16_t numcpu = 0; /* number of cpus seen */ uint16_t numphys = 0; /* number of unique "physical id"s */ uint16_t numcores = 0; /* number of unique "cores id"s */ uint16_t maxsibs = 0; /* maximum value of "siblings" */ uint16_t maxcores = 0; /* maximum value of "cores" */ uint16_t minsibs = 0xffff; /* minimum value of "siblings" */ uint16_t mincores = 0xffff; /* minimum value of "cores" */ uint32_t maxcpuid = 0; /* maximum CPU ID ("processor") */ uint32_t maxphysid = 0; /* maximum "physical id" */ uint32_t maxcoreid = 0; /* maximum "core id" */ uint32_t mincpuid = 0xffffffff;/* minimum CPU ID ("processor") */ uint32_t minphysid = 0xffffffff;/* minimum "physical id" */ uint32_t mincoreid = 0xffffffff;/* minimum "core id" */ int i; #if defined (__sun) #if defined (_LP64) int64_t curcpu, val, sockets, cores, threads; #else int32_t curcpu, val, sockets, cores, threads; #endif int32_t chip_id, core_id, ncore_per_chip, ncpu_per_chip; #else FILE *cpu_info_file; char buffer[128]; uint16_t curcpu, sockets, cores, threads; #endif get_procs(&numproc); *p_cpus = numproc; *p_boards = 1; /* Boards not identified from /proc/cpuinfo */ *p_sockets = numproc; /* initially all single core/thread */ *p_cores = 1; *p_threads = 1; *p_block_map_size = 0; *p_block_map = NULL; *p_block_map_inv = NULL; #if defined (__sun) kstat_ctl_t *kc; kstat_t *ksp; kstat_named_t *knp; kc = kstat_open(); if (kc == NULL) { error ("get speed: kstat error %d", errno); return errno; } #else cpu_info_file = fopen(_cpuinfo_path, "r"); if (cpu_info_file == NULL) { error ("get_cpuinfo: error %d opening %s", errno, _cpuinfo_path); return errno; } #endif /* Note: assumes all processor IDs are within [0:numproc-1] */ /* treats physical/core IDs as tokens, not indices */ if (cpuinfo) memset(cpuinfo, 0, numproc * sizeof(cpuinfo_t)); else cpuinfo = xmalloc(numproc * sizeof(cpuinfo_t)); #if defined (__sun) ksp = kstat_lookup(kc, "cpu_info", -1, NULL); for (; ksp != NULL; ksp = ksp->ks_next) { if (strcmp(ksp->ks_module, "cpu_info")) continue; numcpu++; kstat_read(kc, ksp, NULL); knp = kstat_data_lookup(ksp, "chip_id"); chip_id = knp->value.l; knp = kstat_data_lookup(ksp, "core_id"); core_id = knp->value.l; knp = kstat_data_lookup(ksp, "ncore_per_chip"); ncore_per_chip = knp->value.l; knp = kstat_data_lookup(ksp, "ncpu_per_chip"); ncpu_per_chip = knp->value.l; if (chip_id >= numproc) { debug("cpuid is %ld (> %d), ignored", curcpu, numproc); continue; } cpuinfo[chip_id].seen = 1; cpuinfo[chip_id].cpuid = chip_id; maxcpuid = MAX(maxcpuid, chip_id); mincpuid = MIN(mincpuid, chip_id); for (i = 0; i < numproc; i++) { if ((cpuinfo[i].coreid == core_id) && (cpuinfo[i].corecnt)) break; } if (i == numproc) { numcores++; } else { cpuinfo[i].corecnt++; } if (chip_id < numproc) { cpuinfo[chip_id].corecnt++; cpuinfo[chip_id].coreid = core_id; } maxcoreid = MAX(maxcoreid, core_id); mincoreid = MIN(mincoreid, core_id); if (ncore_per_chip > numproc) { debug("cores is %u (> %d), ignored", ncore_per_chip, numproc); continue; } if (chip_id < numproc) cpuinfo[chip_id].cores = ncore_per_chip; maxcores = MAX(maxcores, ncore_per_chip); mincores = MIN(mincores, ncore_per_chip); } #else curcpu = 0; while (fgets(buffer, sizeof(buffer), cpu_info_file) != NULL) { uint32_t val; if (_chk_cpuinfo_uint32(buffer, "processor", &val)) { curcpu = numcpu; numcpu++; if (curcpu >= numproc) { info("processor limit reached (%u >= %d)", curcpu, numproc); continue; } cpuinfo[curcpu].seen = 1; cpuinfo[curcpu].cpuid = val; maxcpuid = MAX(maxcpuid, val); mincpuid = MIN(mincpuid, val); } else if (_chk_cpuinfo_uint32(buffer, "physical id", &val)) { /* see if the ID has already been seen */ for (i=0; i<numproc; i++) { if ((cpuinfo[i].physid == val) && (cpuinfo[i].physcnt)) break; } if (i == numproc) { /* new ID... */ numphys++; /* ...increment total */ } else { /* existing ID... */ cpuinfo[i].physcnt++; /* ...update ID cnt */ } if (curcpu < numproc) { cpuinfo[curcpu].physcnt++; cpuinfo[curcpu].physid = val; } maxphysid = MAX(maxphysid, val); minphysid = MIN(minphysid, val); } else if (_chk_cpuinfo_uint32(buffer, "core id", &val)) { /* see if the ID has already been seen */ for (i = 0; i < numproc; i++) { if ((cpuinfo[i].coreid == val) && (cpuinfo[i].corecnt)) break; } if (i == numproc) { /* new ID... */ numcores++; /* ...increment total */ } else { /* existing ID... */ cpuinfo[i].corecnt++; /* ...update ID cnt */ } if (curcpu < numproc) { cpuinfo[curcpu].corecnt++; cpuinfo[curcpu].coreid = val; } maxcoreid = MAX(maxcoreid, val); mincoreid = MIN(mincoreid, val); } else if (_chk_cpuinfo_uint32(buffer, "siblings", &val)) { /* Note: this value is a count, not an index */ if (val > numproc) { /* out of bounds, ignore */ debug("siblings is %u (> %d), ignored", val, numproc); continue; } if (curcpu < numproc) cpuinfo[curcpu].siblings = val; maxsibs = MAX(maxsibs, val); minsibs = MIN(minsibs, val); } else if (_chk_cpuinfo_uint32(buffer, "cpu cores", &val)) { /* Note: this value is a count, not an index */ if (val > numproc) { /* out of bounds, ignore */ debug("cores is %u (> %d), ignored", val, numproc); continue; } if (curcpu < numproc) cpuinfo[curcpu].cores = val; maxcores = MAX(maxcores, val); mincores = MIN(mincores, val); } } fclose(cpu_info_file); #endif /*** Sanity check ***/ if (minsibs == 0) minsibs = 1; /* guaranteee non-zero */ if (maxsibs == 0) { minsibs = 1; maxsibs = 1; } if (maxcores == 0) { /* no core data */ mincores = 0; maxcores = 0; } /*** Compute Sockets/Cores/Threads ***/ if ((minsibs == maxsibs) && /* homogeneous system */ (mincores == maxcores)) { sockets = numphys; /* unique "physical id" */ if (sockets <= 1) { /* verify single socket */ sockets = numcpu / maxsibs; /* maximum "siblings" */ } if (sockets == 0) sockets = 1; /* guarantee non-zero */ cores = numcores / sockets; /* unique "core id" */ cores = MAX(maxcores, cores); /* maximum "cpu cores" */ if (cores == 0) { cores = numcpu / sockets; /* assume multi-core */ if (cores > 1) { debug3("Warning: cpuinfo missing 'core id' or " "'cpu cores' but assuming multi-core"); } } if (cores == 0) cores = 1; /* guarantee non-zero */ threads = numcpu / (sockets * cores); /* solve for threads */ if (threads == 0) threads = 1; /* guarantee non-zero */ } else { /* heterogeneous system */ sockets = numcpu; cores = 1; /* one core per socket */ threads = 1; /* one core per core */ } *p_sockets = sockets; /* update output parameters */ *p_cores = cores; *p_threads = threads; #if DEBUG_DETAIL /*** Display raw data ***/ debug3("numcpu: %u", numcpu); debug3("numphys: %u", numphys); debug3("numcores: %u", numcores); debug3("cores: %u->%u", mincores, maxcores); debug3("sibs: %u->%u", minsibs, maxsibs); debug3("cpuid: %u->%u", mincpuid, maxcpuid); debug3("physid: %u->%u", minphysid, maxphysid); debug3("coreid: %u->%u", mincoreid, maxcoreid); for (i = 0; i < numproc; i++) { debug3("CPU %d:", i); debug3(" cpuid: %u", cpuinfo[i].cpuid); debug3(" seen: %u", cpuinfo[i].seen); debug3(" physid: %u", cpuinfo[i].physid); debug3(" physcnt: %u", cpuinfo[i].physcnt); debug3(" siblings: %u", cpuinfo[i].siblings); debug3(" cores: %u", cpuinfo[i].cores); debug3(" coreid: %u", cpuinfo[i].coreid); debug3(" corecnt: %u\n", cpuinfo[i].corecnt); } debug3("Sockets: %u", sockets); debug3("Cores per socket: %u", cores); debug3("Threads per core: %u", threads); #endif *p_block_map_size = numcpu; retval = _compute_block_map(*p_block_map_size, p_block_map, p_block_map_inv); xfree(cpuinfo); /* done with raw cpuinfo data */ return retval; }