Login
[x]
Log in using an account from:
Fedora Account System
Red Hat Associate
Red Hat Customer
Or login using a Red Hat Bugzilla account
Forgot Password
Login:
Hide Forgot
Create an Account
Red Hat Bugzilla – Attachment 577384 Details for
Bug 812148
High CPU usage of scheduler when sleeping
[?]
New
Simple Search
Advanced Search
My Links
Browse
Requests
Reports
Current State
Search
Tabular reports
Graphical reports
Duplicates
Other Reports
User Changes
Plotly Reports
Bug Status
Bug Severity
Non-Defaults
|
Product Dashboard
Help
Page Help!
Bug Writing Guidelines
What's new
Browser Support Policy
5.0.4.rh83 Release notes
FAQ
Guides index
User guide
Web Services
Contact
Legal
This site requires JavaScript to be enabled to function correctly, please enable it.
Added output of CPU usage
test_select.cpp (text/plain), 4.14 KB, created by
Dave Johansen
on 2012-04-13 16:53:23 UTC
(
hide
)
Description:
Added output of CPU usage
Filename:
MIME Type:
Creator:
Dave Johansen
Created:
2012-04-13 16:53:23 UTC
Size:
4.14 KB
patch
obsolete
>#include <stdio.h> >#include <string.h> >#include <unistd.h> >#include <sys/select.h> >#include <sys/types.h> >#include <iostream> > > >// CPU usage measure taken from https://github.com/fho/code_snippets/blob/master/c/getusage.c > >struct pstat{ > long unsigned int utime_ticks; > long int cutime_ticks; > long unsigned int stime_ticks; > long int cstime_ticks; > long unsigned int vsize; // virtual memory size in bytes > long unsigned int rss; //Resident Set Size in bytes > > long unsigned int cpu_total_time; >}; > >/* >* read /proc data into the passed struct pstat >* returns 0 on success, -1 on error >*/ >int get_usage(const pid_t pid, struct pstat* result){ > //convert pid to string > char pid_s[20]; > snprintf(pid_s, sizeof(pid_s), "%d", pid); > char stat_filepath[30] = "/proc/"; strncat(stat_filepath, pid_s, > sizeof(stat_filepath) - strlen(stat_filepath) -1); > strncat(stat_filepath, "/stat", sizeof(stat_filepath) - > strlen(stat_filepath) -1); > > //Open /proc/stat and /proc/$pid/stat fds successive(dont want that cpu > //ticks increases too much during measurements) > //open /proc dir, to lock all files and read the results from the > FILE *fproc = fopen("/proc", "r"); > if (fproc == NULL){ > perror("FOPEN ERROR "); > return -1; > } > //same timefragem > FILE *fpstat = fopen(stat_filepath, "r"); > if(fpstat == NULL){ > perror("FOPEN ERROR "); > return -1; > } > > FILE *fstat = fopen("/proc/stat", "r"); > if(fstat == NULL){ > perror("FOPEN ERROR "); > fclose(fstat); > return -1; > } > > //read values from /proc/pid/stat > bzero(result, sizeof(struct pstat)); > long int rss; > if(fscanf(fpstat, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu" > "%lu %ld %ld %*d %*d %*d %*d %*u %lu %ld", > &result->utime_ticks, &result->stime_ticks, > &result->cutime_ticks, &result->cstime_ticks, &result->vsize, > &rss) == EOF) { > fclose(fpstat); > return -1; > } > fclose(fpstat); > result->rss = rss * getpagesize(); > > //read+calc cpu total time from /proc/stat > long unsigned int cpu_time[10]; > bzero(cpu_time, sizeof(cpu_time)); > if(fscanf(fstat, "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", > &cpu_time[0], &cpu_time[1], &cpu_time[2], &cpu_time[3], > &cpu_time[4], &cpu_time[5], &cpu_time[6], &cpu_time[7], > &cpu_time[8], &cpu_time[9]) == EOF) { > fclose(fstat); > return -1; > } > > fclose(fstat); > > fclose(fproc); > > for(int i=0; i < 10;i++){ > result->cpu_total_time += cpu_time[i]; > } > > return 0; >} > >/* >* calculates the actual CPU usage(cur_usage - last_usage) in percent >* cur_usage, last_usage: both last measured get_usage() results >* ucpu_usage, scpu_usage: result parameters: user and sys cpu usage in % >*/ >void calc_cpu_usage(const struct pstat* cur_usage, const struct pstat* > last_usage, double* ucpu_usage, double* scpu_usage){ > const long unsigned int total_time_diff = cur_usage->cpu_total_time - > last_usage->cpu_total_time; > > *ucpu_usage = 100 * (((cur_usage->utime_ticks + cur_usage->cutime_ticks) > - (last_usage->utime_ticks + last_usage->cutime_ticks)) > / (double) total_time_diff); > > *scpu_usage = 100 * ((((cur_usage->stime_ticks + cur_usage->cstime_ticks) > - (last_usage->stime_ticks + last_usage->cstime_ticks))) / > (double) total_time_diff); >} > > >int main() >{ > timeval ts; > > // Get the id of the current process > pid_t id = getpid(); > // The stats structure of the cpu usage > pstat currStat, prevStat; > double userCpu, systemCpu; > > // Get the CPU stats before starting > get_usage(id, &prevStat); > > for (unsigned int ii=0; ii<1000000; ++ii) { > ts.tv_sec = 0; > ts.tv_usec = 1000; > select(0, NULL, NULL, NULL, &ts); > > if ((ii % 1000) == 999) { > if (get_usage(id, &currStat) == 0) { > calc_cpu_usage(&currStat, &prevStat, &userCpu, &systemCpu); > std::cout << "user: " << userCpu << " system: " << systemCpu << '\n'; > } > } > } > > return 0; >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 812148
:
577173
|
577384
|
577389
|
580227
|
580576
|
581243
|
581244
|
581245
|
581246
|
581247
|
581248
|
581249
|
581250
|
581251
|
582713
|
582723
|
582724
|
582725
|
582726
|
582727
|
582728
|
582729
|
582730
|
582731
|
582732
|
582733
|
582734
|
582735
|
582736
|
582737
|
582738
|
582739
|
582740
|
583676
|
584416
|
584419
|
595158
|
598759
|
734215