ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/process_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/process_stats.c (file contents):
Revision 1.78 by ats, Sat Nov 6 15:31:26 2004 UTC vs.
Revision 1.83 by tdb, Sun Oct 3 18:35:58 2010 UTC

# Line 28 | Line 28
28   #include "statgrab.h"
29   #include "tools.h"
30   #include "vector.h"
31 < #if defined(SOLARIS) || defined(LINUX)
31 > #if defined(SOLARIS) || defined(LINUX) || defined(AIX)
32   #include <stdio.h>
33   #include <stdlib.h>
34   #include <sys/types.h>
35   #include <dirent.h>
36 #include <string.h>
36   #endif
37 + #include <string.h>
38  
39   #ifdef SOLARIS
40   #include <procfs.h>
# Line 78 | Line 78
78   #include <unistd.h>
79   #define PROCESS_BATCH 30
80   #endif
81 + #ifdef WIN32
82 + #include <windows.h>
83 + #include <psapi.h>
84 + #endif
85 + #ifdef AIX
86 + #include <unistd.h>
87 + #include <errno.h>
88 + #include <time.h>
89 + #include <procinfo.h>
90 + #include <sys/time.h>
91 + extern int getprocs64(struct procentry64 *, int, struct fdsinfo64 *, int, pid_t *, int);
92 + extern int getargs(struct procentry64 *, int, char *, int);
93 + #endif
94  
95   static void proc_state_init(sg_process_stats *s) {
96          s->process_name = NULL;
# Line 100 | Line 113 | sg_process_stats *sg_get_process_stats(int *entries){
113          long long pagesize;
114          int num, i;
115   #endif
116 + #ifdef AIX
117 +        struct procentry64 *procs = NULL;
118 +        long long pagesize;
119 +        int fetched = 0;
120 +        pid_t index = 0;
121 +        unsigned proc_idx;
122 +        time_t utime, stime;
123 +        int ncpus;
124 +        struct timeval now_tval;
125 +        double now_time;
126 +        char cmndline[ARG_MAX];
127 +        char comm[ARG_MAX];
128 +        struct procentry64 curproc_for_getargs;
129 + #define PROCS_TO_FETCH  1000
130 + #endif
131   #ifdef ALLBSD
132          int mib[4];
133          size_t size;
# Line 139 | Line 167 | sg_process_stats *sg_get_process_stats(int *entries){
167          int len;
168          int rc;
169          time_t uptime;
170 +        long tickspersec;
171   #endif
172  
173   #ifdef LINUX
# Line 251 | Line 280 | sg_process_stats *sg_get_process_stats(int *entries){
280  
281                  /* cpu */
282                  proc_state_ptr->cpu_percent = (100.0 * (utime + stime)) / ((uptime * 100.0) - starttime);
283 +                tickspersec = sysconf (_SC_CLK_TCK);
284 +                if (tickspersec < 0) {
285 +                        proc_state_ptr->time_spent = 0;
286 +                }
287 +                else {
288 +                        proc_state_ptr->time_spent = (utime + stime) / tickspersec;
289 +                }
290  
291                  fclose(f);
292  
# Line 416 | Line 452 | sg_process_stats *sg_get_process_stats(int *entries){
452                  proctitle = NULL;
453  
454                  do {
455 <                        if(size >= buflen) {
455 >                        if((long) size >= buflen) {
456                                  buflen *= 2;
457                                  size = buflen;
458                                  proctitletmp = sg_realloc(proctitle, buflen);
# Line 438 | Line 474 | sg_process_stats *sg_get_process_stats(int *entries){
474                                  size = 0;
475                                  break;
476                          }
477 <                } while(size >= buflen);
477 >                } while((long) size >= buflen);
478  
479                  if(size > 0) {
480                          proc_state_ptr->proctitle = sg_malloc(size+1);
# Line 751 | Line 787 | sg_process_stats *sg_get_process_stats(int *entries){
787          }
788   #endif
789  
790 + #ifdef AIX
791 + #define TVALU_TO_SEC(x) ((x).tv_sec + ((double)((x).tv_usec) / 1000000.0))
792 + #define TVALN_TO_SEC(x) ((x).tv_sec + ((double)((x).tv_usec) / 1000000000.0))
793 +        ncpus = sysconf(_SC_NPROCESSORS_ONLN);
794 +        if( -1 == ncpus ) {
795 +                ncpus = 1; /* sysconf error - assume 1 */
796 +        }
797 +
798 +        if ((pagesize = sysconf(_SC_PAGESIZE)) == -1) {
799 +                sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
800 +                return NULL;
801 +        }
802 +
803 +        proc_idx = 0;
804 +        procs = /* (struct procentry64 *) */ malloc(sizeof(*procs) * PROCS_TO_FETCH);
805 +        if(NULL == procs) {
806 +                sg_set_error_with_errno(SG_ERROR_MALLOC, "sg_get_process_stats");
807 +                return 0;
808 +        }
809 +
810 +        gettimeofday(&now_tval, 0);
811 +        now_time = TVALU_TO_SEC(now_tval);
812 +
813 +        /* keep on grabbing chunks of processes until getprocs returns a smaller
814 +           block than we asked for */
815 +        do {
816 +                int i;
817 +                fetched = getprocs64(procs, sizeof(*procs), NULL, 0, &index, PROCS_TO_FETCH);
818 +                if (VECTOR_RESIZE(proc_state, proc_state_size + fetched) < 0) {
819 +                        sg_set_error_with_errno(SG_ERROR_MALLOC, "sg_get_process_stats");
820 +                        free(procs);
821 +                        return NULL;
822 +                }
823 +                for( i = 0; i < fetched; ++i ) {
824 +                        struct procentry64 *pi = procs+i;
825 +                        int zombie = 0;
826 +
827 +                        proc_state_ptr = proc_state + proc_idx;
828 +
829 +                        zombie = 0;
830 +
831 +                        /* set a descriptive name for the process state */
832 +                        switch( pi->pi_state ) {
833 +                        case SSLEEP:
834 +                                proc_state_ptr->state = SG_PROCESS_STATE_SLEEPING;
835 +                                break;
836 +                        case SRUN:
837 +                                proc_state_ptr->state = SG_PROCESS_STATE_RUNNING;
838 +                                break;
839 +                        case SZOMB:
840 +                                proc_state_ptr->state = SG_PROCESS_STATE_ZOMBIE;
841 +                                zombie = 1;
842 +                                break;
843 +                        case SSTOP:
844 +                                proc_state_ptr->state = SG_PROCESS_STATE_STOPPED;
845 +                                break;
846 +                        case SACTIVE:
847 +                                proc_state_ptr->state = SG_PROCESS_STATE_RUNNING;
848 +                                break;
849 +                        case SIDL:
850 +                        default:
851 +                                proc_state_ptr->state = SG_PROCESS_STATE_UNKNOWN;
852 +                                break;
853 +                        }
854 +
855 +                        if( zombie ) {
856 +                                utime = pi->pi_utime;
857 +                                stime = pi->pi_stime;
858 +                        } else {
859 +                                utime = TVALN_TO_SEC(pi->pi_ru.ru_utime) + TVALN_TO_SEC(pi->pi_cru.ru_utime);
860 +                                stime = TVALN_TO_SEC(pi->pi_ru.ru_stime) + TVALN_TO_SEC(pi->pi_cru.ru_stime);
861 +                        }
862 +
863 +                        proc_state_ptr->pid = pi->pi_pid;
864 +                        proc_state_ptr->parent = pi->pi_ppid;
865 +                        proc_state_ptr->pgid = pi->pi_pgrp;
866 +                        proc_state_ptr->uid = pi->pi_cred.crx_ruid;
867 +                        proc_state_ptr->euid = pi->pi_cred.crx_uid;
868 +                        proc_state_ptr->gid = pi->pi_cred.crx_rgid;
869 +                        proc_state_ptr->egid = pi->pi_cred.crx_gid;
870 +                        proc_state_ptr->proc_size = pi->pi_size;
871 +                        proc_state_ptr->proc_resident = pi->pi_drss + pi->pi_trss; /* XXX might be wrong, see P::PT */
872 +                        proc_state_ptr->time_spent = utime + stime;
873 +                        proc_state_ptr->cpu_percent = (((double)(utime + stime) * 100) / ( now_time - pi->pi_start )) / ncpus;
874 +                        proc_state_ptr->nice = pi->pi_nice;
875 +
876 +                        /* determine comm & cmndline */
877 +                        if( (pi->pi_flags & SKPROC) == SKPROC ) {
878 +                                if( pi->pi_pid == 0 ) {
879 +                                        snprintf(comm, ARG_MAX, "kproc (swapper)");
880 +                                        snprintf(cmndline, ARG_MAX, "kproc (swapper)");
881 +                                } else {
882 +                                        snprintf(comm, ARG_MAX, "kproc (%s)", pi->pi_comm);
883 +                                        snprintf(cmndline, ARG_MAX, "kproc (%s)", pi->pi_comm);
884 +                                }
885 +                        } else {
886 +                                snprintf(comm, ARG_MAX, "%s", pi->pi_comm);
887 +                                curproc_for_getargs.pi_pid = pi->pi_pid;
888 +                                if( getargs(&curproc_for_getargs, sizeof(curproc_for_getargs), cmndline, ARG_MAX) < 0 ) {
889 +                                        snprintf(cmndline, ARG_MAX, "%s", pi->pi_comm);
890 +                                } else {
891 +                                        int done = 0;
892 +                                        /* replace NUL characters in command line with spaces */
893 +                                        char *c = cmndline;
894 +                                        while( ! done ) {
895 +                                                if( *c == '\0' ) {
896 +                                                        if( *(c+1) == '\0' ) {
897 +                                                                done = 1;
898 +                                                        } else {
899 +                                                                *c++ = ' ';
900 +                                                        }
901 +                                                } else {
902 +                                                        ++c;
903 +                                                }
904 +                                        }
905 +                                }
906 +                        }
907 +
908 +
909 +                        if (sg_update_string(&proc_state_ptr->process_name, comm) < 0) {
910 +                                free(procs);
911 +                                return NULL;
912 +                        }
913 +                        if (sg_update_string(&proc_state_ptr->proctitle, cmndline) < 0) {
914 +                                free(procs);
915 +                                return NULL;
916 +                        }
917 +
918 +                        proc_idx++;
919 +                }
920 +        } while( fetched >= PROCS_TO_FETCH );
921 +
922 +        proc_state_size = proc_idx;
923 +
924 +        free(procs);
925 + #endif
926 +
927 +
928   #ifdef CYGWIN
929          sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
930          return NULL;
931   #endif
932 + #ifdef WIN32
933 +        /* FIXME The data needed for this is probably do able with the
934 +         * "performance registry". Although using this appears to be a black
935 +         * art and closely guarded secret.
936 +         * This is not directly used in ihost, so not considered a priority */
937 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Win32");
938 +        return NULL;
939 + #endif
940  
941          *entries = proc_state_size;
942          return proc_state;
# Line 762 | Line 944 | sg_process_stats *sg_get_process_stats(int *entries){
944  
945   sg_process_count *sg_get_process_count() {
946          static sg_process_count process_stat;
947 + #ifndef WIN32
948          sg_process_stats *ps;
949          int ps_size, x;
950 + #else
951 +        DWORD aProcesses[1024];
952 +        DWORD cbNeeded;
953 + #endif
954  
955          process_stat.sleeping = 0;
956          process_stat.running = 0;
# Line 771 | Line 958 | sg_process_count *sg_get_process_count() {
958          process_stat.stopped = 0;
959          process_stat.total = 0;
960  
961 + #ifndef WIN32
962          ps = sg_get_process_stats(&ps_size);
963          if (ps == NULL) {
964                  return NULL;
# Line 799 | Line 987 | sg_process_count *sg_get_process_count() {
987          }
988  
989          process_stat.total = ps_size;
990 + #else
991 +        if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
992 +                return NULL;
993 +        process_stat.total = cbNeeded / sizeof(DWORD);
994 + #endif
995  
996          return &process_stat;
997   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines