void setup_cpu() { // creating values FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; int i, j, cpu, usg; cpu = 0; // initializing cpuinfo.ncpus = get_ncpus(); // reserving memory cpuinfo.cpuloads = (double*)malloc((sizeof(double)*cpuinfo.ncpus)); cpuinfo.last = (unsigned long**)malloc((sizeof(unsigned long*)*cpuinfo.ncpus)); cpuinfo.current = (unsigned long**)malloc((sizeof(unsigned long*)*cpuinfo.ncpus)); for(i = 0;i < cpuinfo.ncpus;i++){ cpuinfo.last[i] = (unsigned long*)malloc((sizeof(unsigned long)*cpuinfo.ncpus)); cpuinfo.current[i] = (unsigned long*)malloc((sizeof(unsigned long)*cpuinfo.ncpus)); } // open /proc/stat fp = fopen("/proc/stat", "r"); if (fp == NULL){ log_str("failed to read /proc/stat", LOG_WARNING); return; } // reading line by line while ((read = getline(&line, &len, fp)) != -1) { if(line[0] == 'c' && line[3] != ' '){ usg = 0; // geting values vor user, nice, system, idle for(i = 4;i < len;i++){ if(line[i] == ' '){ cpuinfo.last[cpu][usg] = atoi(line+i+1); usg++; if(usg == 4) break; } } }else if(line[3] == ' ') continue; else break; cpu++; } if (line) free(line); fclose(fp); setup_cpu_name(); }
/* * fill the cpu_info structure with as much info as we can get. * code is similar to linux version, but sadly available info is less. */ int rte_eal_cpu_init(void) { /* pointer to global configuration */ struct rte_config *config = rte_eal_get_configuration(); unsigned lcore_id; unsigned count = 0; const unsigned ncpus = get_ncpus(); /* * Parse the maximum set of logical cores, detect the subset of running * ones and enable them by default. */ for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { /* init cpuset for per lcore config */ CPU_ZERO(&lcore_config[lcore_id].cpuset); lcore_config[lcore_id].detected = (lcore_id < ncpus); if (lcore_config[lcore_id].detected == 0) { config->lcore_role[lcore_id] = ROLE_OFF; continue; } /* By default, lcore 1:1 map to cpu id */ CPU_SET(lcore_id, &lcore_config[lcore_id].cpuset); /* By default, each detected core is enabled */ config->lcore_role[lcore_id] = ROLE_RTE; lcore_config[lcore_id].core_id = cpu_core_id(lcore_id); lcore_config[lcore_id].socket_id = cpu_socket_id(lcore_id); if (lcore_config[lcore_id].socket_id >= RTE_MAX_NUMA_NODES) #ifdef RTE_EAL_ALLOW_INV_SOCKET_ID lcore_config[lcore_id].socket_id = 0; #else rte_panic("Socket ID (%u) is greater than " "RTE_MAX_NUMA_NODES (%d)\n", lcore_config[lcore_id].socket_id, RTE_MAX_NUMA_NODES); #endif RTE_LOG(DEBUG, EAL, "Detected lcore %u\n", lcore_id); count++; } /* Set the count of enabled logical cores of the EAL configuration */ config->lcore_count = count; RTE_LOG(DEBUG, EAL, "Support maximum %u logical core(s) by configuration.\n", RTE_MAX_LCORE); RTE_LOG(DEBUG, EAL, "Detected %u lcore(s)\n", config->lcore_count); return 0; }
int main(int ac, char **av) { double srt; double pollrate; int ch; char buf[64]; int monbat; srt = 8.0; /* time for samples - 8 seconds */ pollrate = 1.0; /* polling rate in seconds */ while ((ch = getopt(ac, av, "b:cdefh:l:p:r:tu:B:L:P:QT:")) != -1) { switch(ch) { case 'b': BackLightPct = strtol(optarg, NULL, 10); break; case 'c': AdjustCstate = 1; break; case 'd': DebugOpt = 1; break; case 'e': HasPerfbias = 1; break; case 'f': AdjustCpuFreq = 0; break; case 'h': HighestCpuFreq = strtol(optarg, NULL, 10); break; case 'l': LowestCpuFreq = strtol(optarg, NULL, 10); break; case 'p': Hysteresis = (int)strtol(optarg, NULL, 10); break; case 'r': pollrate = strtod(optarg, NULL); break; case 't': TurboOpt = 0; break; case 'u': TriggerUp = (double)strtol(optarg, NULL, 10) / 100; break; case 'B': BatLifeMin = strtol(optarg, NULL, 10); break; case 'L': BatShutdownLingerSet = strtol(optarg, NULL, 10); if (BatShutdownLingerSet < 0) BatShutdownLingerSet = 0; break; case 'P': BatLifePollIntvl = strtol(optarg, NULL, 10); break; case 'Q': BatShutdownAudioAlert = 0; break; case 'T': srt = strtod(optarg, NULL); break; default: usage(); /* NOT REACHED */ } } ac -= optind; av += optind; setlinebuf(stdout); /* Get number of cpus */ get_ncpus(); if (0 > Hysteresis || Hysteresis > 99) { fprintf(stderr, "Invalid hysteresis value\n"); exit(1); } if (0 > TriggerUp || TriggerUp > 1) { fprintf(stderr, "Invalid load limit value\n"); exit(1); } if (BackLightPct > 100 || BackLightPct <= 0) { fprintf(stderr, "Invalid backlight setting, ignore\n"); BackLightPct = 100; } TriggerDown = TriggerUp - (TriggerUp * (double) Hysteresis / 100); /* * Make sure powerd is not already running. */ PowerFd = open("/var/run/powerd.pid", O_CREAT|O_RDWR, 0644); if (PowerFd < 0) { fprintf(stderr, "Cannot create /var/run/powerd.pid, " "continuing anyway\n"); } else { if (flock(PowerFd, LOCK_EX|LOCK_NB) < 0) { fprintf(stderr, "powerd is already running\n"); exit(1); } } /* * Demonize and set pid */ if (DebugOpt == 0) { daemon(0, 0); openlog("powerd", LOG_CONS | LOG_PID, LOG_DAEMON); } if (PowerFd >= 0) { ftruncate(PowerFd, 0); snprintf(buf, sizeof(buf), "%d\n", (int)getpid()); write(PowerFd, buf, strlen(buf)); } /* Do we need to monitor battery life? */ if (BatLifePollIntvl <= 0) monbat = 0; else monbat = has_battery(); /* Do we have perfbias(4)? */ if (HasPerfbias) HasPerfbias = has_perfbias(); /* Could we adjust C-state? */ if (AdjustCstate) AdjustCstate = probe_cstate(); /* * Wait hw.acpi.cpu.px_dom* sysctl to be created by kernel. * * Since hw.acpi.cpu.px_dom* creation is queued into ACPI * taskqueue and ACPI taskqueue is shared across various * ACPI modules, any delay in other modules may cause * hw.acpi.cpu.px_dom* to be created at quite a later time * (e.g. cmbat module's task could take quite a lot of time). */ for (;;) { /* Prime delta cputime calculation. */ get_cputime(pollrate); /* Wait for all cpus to appear */ if (acpi_get_cpupwrdom()) break; usleep((int)(pollrate * 1000000.0)); } /* * Catch some signals so that max performance could be restored. */ signal(SIGINT, sigintr); signal(SIGTERM, sigintr); /* Initialize performance states */ init_perf(); srt = srt / pollrate; /* convert to sample count */ if (DebugOpt) printf("samples for downgrading: %5.2f\n", srt); /* * Monitoring loop */ while (!stopped) { /* * Monitor performance */ get_cputime(pollrate); mon_perf(srt); /* * Monitor battery */ if (monbat) monbat = mon_battery(); usleep((int)(pollrate * 1000000.0)); } /* * Set to maximum performance if killed. */ syslog(LOG_INFO, "killed, setting max and exiting"); restore_perf(); restore_backlight(); exit(0); }
/* * locore.s code calls bootstrap() just before calling main(). * * What we try to do is as follows: * - Initialize PROM and the console * - Read in part of information provided by a bootloader and find out * kernel load and end addresses * - Initialize ksyms * - Find out number of active CPUs * - Finalize the bootstrap by calling pmap_bootstrap() * * We will try to run out of the prom until we get out of pmap_bootstrap(). */ void bootstrap(void *o0, void *bootargs, void *bootsize, void *o3, void *ofw) { void *bi; long bmagic; char buf[32]; #if NKSYMS || defined(DDB) || defined(MODULAR) struct btinfo_symtab *bi_sym; #endif struct btinfo_count *bi_count; struct btinfo_kernend *bi_kend; struct btinfo_tlb *bi_tlb; struct btinfo_boothowto *bi_howto; extern void *romtba; extern void* get_romtba(void); extern void OF_val2sym32(void *); extern void OF_sym2val32(void *); extern struct consdev consdev_prom; /* Save OpenFrimware entry point */ romp = ofw; romtba = get_romtba(); prom_init(); console_instance = promops.po_stdout; console_node = OF_instance_to_package(promops.po_stdout); /* Initialize the PROM console so printf will not panic */ cn_tab = &consdev_prom; (*cn_tab->cn_init)(cn_tab); DPRINTF(ACDB_BOOTARGS, ("sparc64_init(%p, %p, %p, %p, %p)\n", o0, bootargs, bootsize, o3, ofw)); /* Extract bootinfo pointer */ if ((long)bootsize >= (4 * sizeof(uint64_t))) { /* Loaded by 64-bit bootloader */ bi = (void*)(u_long)(((uint64_t*)bootargs)[3]); bmagic = (long)(((uint64_t*)bootargs)[0]); } else if ((long)bootsize >= (4 * sizeof(uint32_t))) { /* Loaded by 32-bit bootloader */ bi = (void*)(u_long)(((uint32_t*)bootargs)[3]); bmagic = (long)(((uint32_t*)bootargs)[0]); } else { printf("Bad bootinfo size.\n"); die_old_boot_loader: printf("This kernel requires NetBSD boot loader version 1.9 " "or newer\n"); panic("sparc64_init."); } DPRINTF(ACDB_BOOTARGS, ("sparc64_init: bmagic=%lx, bi=%p\n", bmagic, bi)); /* Read in the information provided by NetBSD boot loader */ if (SPARC_MACHINE_OPENFIRMWARE != bmagic) { printf("No bootinfo information.\n"); goto die_old_boot_loader; } bootinfo = (void*)(u_long)((uint64_t*)bi)[1]; LOOKUP_BOOTINFO(bi_kend, BTINFO_KERNEND); if (bi_kend->addr == (vaddr_t)0) { panic("Kernel end address is not found in bootinfo.\n"); } #if NKSYMS || defined(DDB) || defined(MODULAR) LOOKUP_BOOTINFO(bi_sym, BTINFO_SYMTAB); ksyms_addsyms_elf(bi_sym->nsym, (int *)(u_long)bi_sym->ssym, (int *)(u_long)bi_sym->esym); #ifdef DDB #ifdef __arch64__ /* This can only be installed on an 64-bit system cause otherwise our stack is screwed */ OF_set_symbol_lookup(OF_sym2val, OF_val2sym); #else OF_set_symbol_lookup(OF_sym2val32, OF_val2sym32); #endif #endif #endif if (OF_getprop(findroot(), "compatible", buf, sizeof(buf)) > 0) { if (strcmp(buf, "sun4us") == 0) setcputyp(CPU_SUN4US); else if (strcmp(buf, "sun4v") == 0) setcputyp(CPU_SUN4V); } bi_howto = lookup_bootinfo(BTINFO_BOOTHOWTO); if (bi_howto) boothowto = bi_howto->boothowto; LOOKUP_BOOTINFO(bi_count, BTINFO_DTLB_SLOTS); kernel_dtlb_slots = bi_count->count; kernel_itlb_slots = kernel_dtlb_slots-1; bi_count = lookup_bootinfo(BTINFO_ITLB_SLOTS); if (bi_count) kernel_itlb_slots = bi_count->count; LOOKUP_BOOTINFO(bi_tlb, BTINFO_DTLB); kernel_tlbs = &bi_tlb->tlb[0]; get_ncpus(); pmap_bootstrap(KERNBASE, bi_kend->addr); }