1- //Last modified: 17/10/11 18:52:02(CEST ) by Fabian Holler
1+ //Last modified: 25/02/12 17:50:17(CET ) by Fabian Holler
22#include <stdlib.h>
33#include <sys/types.h>
44#include <stdio.h>
@@ -24,43 +24,51 @@ int get_usage(const pid_t pid, struct pstat* result){
2424 //convert pid to string
2525 char pid_s [20 ];
2626 snprintf (pid_s , sizeof (pid_s ), "%d" , pid );
27- char stat_filepath [30 ] = "/proc/" ;
28- strncat (stat_filepath , pid_s , sizeof (stat_filepath ) - strlen (stat_filepath ) - 1 );
29- strncat (stat_filepath , "/stat" , sizeof (stat_filepath ) - strlen (stat_filepath ) - 1 );
27+ char stat_filepath [30 ] = "/proc/" ; strncat (stat_filepath , pid_s ,
28+ sizeof (stat_filepath ) - strlen (stat_filepath ) - 1 );
29+ strncat (stat_filepath , "/stat" , sizeof (stat_filepath ) -
30+ strlen (stat_filepath ) - 1 );
3031
31- //open /proc/pid/stat
32+ //Open /proc/stat and /proc/$pid/stat fds successive(dont want that cpu
33+ //ticks increases too much during measurements)
3234 FILE * fpstat = fopen (stat_filepath , "r" );
3335 if (fpstat == NULL ){
34- printf ("FOPEN ERROR pid stat %s:\n" , stat_filepath );
36+ perror ("FOPEN ERROR " );
3537 return -1 ;
3638 }
3739
38- //open /proc/stat
3940 FILE * fstat = fopen ("/proc/stat" , "r" );
4041 if (fstat == NULL ){
41- printf ("FOPEN ERROR" );
42+ perror ("FOPEN ERROR " );
4243 fclose (fstat );
4344 return -1 ;
4445 }
45- bzero (result , sizeof (struct pstat ));
4646
4747 //read values from /proc/pid/stat
48+ bzero (result , sizeof (struct pstat ));
4849 long int rss ;
49- 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 ){
50+ if (fscanf (fpstat , "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu"
51+ "%lu %ld %ld %*d %*d %*d %*d %*u %lu %ld" ,
52+ & result -> utime_ticks , & result -> stime_ticks ,
53+ & result -> cutime_ticks , & result -> cstime_ticks , & result -> vsize ,
54+ & rss ) == EOF ) {
5055 fclose (fpstat );
51- fclose (fstat );
5256 return -1 ;
5357 }
5458 fclose (fpstat );
5559 result -> rss = rss * getpagesize ();
5660
57- //read+calc cpu total time from /proc/stat, on linux 2.6.35-23 x86_64 the cpu row has 10values could differ on different architectures :/
61+ //read+calc cpu total time from /proc/stat
5862 long unsigned int cpu_time [10 ];
5963 bzero (cpu_time , sizeof (cpu_time ));
60- 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 ){
64+ if (fscanf (fstat , "%*s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu" ,
65+ & cpu_time [0 ], & cpu_time [1 ], & cpu_time [2 ], & cpu_time [3 ],
66+ & cpu_time [4 ], & cpu_time [5 ], & cpu_time [6 ], & cpu_time [7 ],
67+ & cpu_time [8 ], & cpu_time [9 ]) == EOF ) {
6168 fclose (fstat );
6269 return -1 ;
6370 }
71+
6472 fclose (fstat );
6573
6674 for (int i = 0 ; i < 10 ;i ++ ){
@@ -75,8 +83,17 @@ int get_usage(const pid_t pid, struct pstat* result){
7583* cur_usage, last_usage: both last measured get_usage() results
7684* ucpu_usage, scpu_usage: result parameters: user and sys cpu usage in %
7785*/
78- void calc_cpu_usage (const struct pstat * cur_usage , const struct pstat * last_usage , float * ucpu_usage , float * scpu_usage ){
79- * ucpu_usage = 100 * ((((cur_usage -> utime_ticks + cur_usage -> cutime_ticks ) - (last_usage -> utime_ticks + last_usage -> cutime_ticks ))) / (float )((cur_usage -> cpu_total_time - last_usage -> cpu_total_time )));
80- * scpu_usage = 100 * ((((cur_usage -> stime_ticks + cur_usage -> cstime_ticks ) - (last_usage -> stime_ticks + last_usage -> cstime_ticks ))) / (float )((cur_usage -> cpu_total_time - last_usage -> cpu_total_time )));
86+ void calc_cpu_usage (const struct pstat * cur_usage , const struct pstat *
87+ last_usage , double * ucpu_usage , double * scpu_usage ){
88+ const long unsigned int total_time_diff = cur_usage -> cpu_total_time -
89+ last_usage -> cpu_total_time ;
90+
91+ * ucpu_usage = 100 * (((cur_usage -> utime_ticks + cur_usage -> cutime_ticks )
92+ - (last_usage -> utime_ticks + last_usage -> cutime_ticks ))
93+ / (double ) total_time_diff );
94+
95+ * scpu_usage = 100 * ((((cur_usage -> stime_ticks + cur_usage -> cstime_ticks )
96+ - (last_usage -> stime_ticks + last_usage -> cstime_ticks ))) /
97+ (double ) total_time_diff );
8198}
8299
0 commit comments