int main() { int total_jiffies1, total_jiffies2; int work_jiffies1, work_jiffies2; getCpuUsage(total_jiffies1, work_jiffies1); sleep(5); getCpuUsage(total_jiffies2, work_jiffies2); double work_over_period = work_jiffies2 - work_jiffies1; double total_over_period = total_jiffies2 - total_jiffies1; double cpu = work_over_period / total_over_period * 100; std::cout << cpu << "%" << std::endl; }
int main(){ long long i; for (i = 0 ; i<10000000;i++) { getCpuUsage(); } return 0; }
void CCPULoad::read() { try { m_keyword->set(getCpuUsage()); } catch (shared::exception::CException& exception) { std::cout << "CPU Load reading failed:" << exception.what() << std::endl; } }
QJsonObject Client::getJsonInfo() { QJsonObject json; json["IP"] = socket.localAddress().toString(); json["Name"] = getHostname(); json["CPU"] = getCpuUsage(); json["Memory"] = getMemoryUsage(); json["Disk"] = getDiskUsage(); json["Time"] = currentTime(); QJsonArray devices = QJsonArray::fromStringList(listAllDevices()); json["Devices"] = devices; return json; }
int fileInit() { FILE *fp = NULL; getCpuUsage();//获取CPU数,并舍弃第一组数据 if (_access("CpuUsage.log",0) == -1)//判断是不是新文件 { if(((fp = fopen("CpuUsage.log", "wt")) == NULL)) { printf("Can not creat log file"); exit(0); } fprintf(fp, "This compute has %d core!\n", m_iNumberProcessors); fputs("Sum\t", fp); for (int i = 1; i <= m_iNumberProcessors; i++) fprintf(fp, "Core%d\t", i); fputs("\n",fp); fclose(fp); } else { if(((fp = fopen("CpuUsage.log", "r+")) == NULL)) { printf("Can not read log file"); exit(0); } int pos; for(pos = 0;pos > -1000;pos--)//判断记录是否出错 { fseek( fp, pos, SEEK_END ); if(fgetc(fp) == '\n') break; } if(pos != 0) { //重写出错记录 fseek(fp, pos+1, SEEK_END ); double cpuUsage[33] = {0};//最大支持32核; getAverageCpuUsage(cpuUsage); for (int i = 0; i < m_iNumberProcessors+1; i++) fprintf(fp, "%.3f\t", cpuUsage[i]); fputs("\n",fp); } fclose(fp); } return 0; }
int getAverageCpuUsage(double cpuUsage[17]) { for(int n=0;n<Time;n++) { //求和 Sleep(1000); cpuUsage[0] += getCpuUsage(); for (int i = 0; i < m_iNumberProcessors; i++) { cpuUsage[i+1] += m_PUT[i].dbIdleTime; } } //求平均 for (int i = 0; i < m_iNumberProcessors+1; i++) { cpuUsage[i] /= Time; } return 1; }
int main(int argc, char *argv[]) { unsigned long long int Tms=0; int mem; float usage; int pid = atoi(argv[1]); FILE *fp = fopen("data.csv", "w"); fprintf(fp,"T(ms),mem,cpu\n"); signal(SIGINT, catchSig); while(1) { if( ctrl_c == 0 ) { usage = getCpuUsage(500000, pid); mem = getMemValue(pid); fprintf(fp,"%lld,%d,%0.2f\n", Tms, mem, usage); printf("T:%lld\t Mem: %dKB\t CPU: %0.2f\n", Tms, mem, usage); Tms = Tms + 50; } } }
/* double get_cpu_use_rate(const struct cpu_usage_struct *cur, const struct cpu_usage_struct *old) { double user,sys,nice,idle,total; double cpu_rate; user = (double)(cur->cpu_user - old->cpu_user); sys = (double)(cur->cpu_sys - old->cpu_sys); nice = (double)(cur->cpu_nice - old->cpu_nice); idle = (double)(cur->cpu_idle - old->cpu_idle); total = user + sys + nice + idle; cpu_rate = (1-idle/total)*100; return total; } double get_cpu_us_rate(const struct cpu_usage_struct *cur, const struct cpu_usage_struct *old) { double user,sys,nice,idle,total; double cpu_rate; user = (double)(cur->cpu_user - old->cpu_user); sys = (double)(cur->cpu_sys - old->cpu_sys); nice = (double)(cur->cpu_nice - old->cpu_nice); idle = (double)(cur->cpu_idle - old->cpu_idle); total = user + sys + nice + idle; cpu_rate = (user/total)*100; return cpu_rate; } double get_cpu_free_rate(const struct cpu_usage_struct *cur, const struct cpu_usage_struct *old) { double user,sys,nice,idle,total; double free_rate; user = (double)(cur->cpu_user - old->cpu_user); sys = (double)(cur->cpu_sys - old->cpu_sys); nice = (double)(cur->cpu_nice - old->cpu_nice); idle = (double)(cur->cpu_idle - old->cpu_idle); total = user + sys + nice + idle; free_rate = (idle /total)*100; return free_rate; } int get_cpuinfo_from_proc_stat(struct cpu_usage_struct *usage) { FILE *fp = NULL; char tmp[10]; fp = fopen(CPU_FILE_PROC_STAT,"r"); if(fp == NULL) { perror("fopen"); return -1; } printf("%s,%d\n",__FILE__,__LINE__); fscanf(fp,"%s %lu %lu %lu %lu",tmp,&(usage->cpu_user),&(usage->cpu_sys), &(usage->cpu_nice),&(usage->cpu_idle)); printf("%s %d\n",__FILE__,__LINE__); fclose(fp); printf("%s %d\n",__FILE__,__LINE__); return 1; } */ int main() { //struct cpu_usage_struct *cur,*old,cpu_usage; struct cpu_usage_struct cpu_usage; struct mem_usage_struct mem; double use_rate,free_rate,rate; unsigned long total; /*old = (struct cpu_usage_struct*)malloc(sizeof(struct cpu_usage_struct)); if(old == NULL) { perror("malloc error"); return -1; } cur = (struct cpu_usage_struct*)malloc(sizeof(struct cpu_usage_struct)); if(cur == NULL) { perror("malloc error"); return -1; } get_cpuinfo_from_proc_stat(old); sleep(1); get_cpuinfo_from_proc_stat(cur); rate=cur->cpu_user+cur->cpu_sys+cur->cpu_nice+cur->cpu_idle; use_rate = get_cpu_use_rate(cur,old); free_rate = get_cpu_free_rate(cur,old); //use_rate=getCpuUsage(&cpu_usage); printf("us:%.1f%%\n use_rate:%.1lf%%\nfree_rate:%.1lf%%\n",100*cur->cpu_user/rate,use_rate,free_rate); */ total=getCpuUsage(&cpu_usage); if (total>0) printf("<h4>用户使用率\t%.1f%%\t系统使用率\t%.1f%%</h4>",cpu_usage.cpu_user*100.0/total,cpu_usage.cpu_sys*100.0/total); getMemUsage(&mem); if (mem.total>0) printf("总数=%lu 已用=%lu 可用=%lu 缓冲区=%lu 已缓存=%lu 使用率:%.1f%%\n",mem.total,mem.used,mem.free,mem.buffers,mem.cached,(mem.used-mem.buffers-mem.cached)*100.0/mem.total); return 1; }