sysdep_LINUX.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2003 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  */
00019 
00020 
00021 #include <config.h>
00022 
00023 #ifdef HAVE_SYS_TYPES_H
00024 #include <sys/types.h>
00025 #endif
00026 
00027 #include <unistd.h>
00028 
00029 #ifdef HAVE_SYS_STAT_H
00030 #include <sys/stat.h>
00031 #endif
00032 
00033 #ifdef HAVE_FCNTL_H
00034 #include <fcntl.h>
00035 #endif
00036 
00037 #ifdef HAVE_STDLIB_H
00038 #include <stdlib.h>
00039 #endif
00040 
00041 #ifdef TIME_WITH_SYS_TIME
00042 #include <time.h>
00043 
00044 #ifdef HAVE_SYS_TIME_H
00045 #include <sys/time.h>
00046 #endif
00047 #else
00048 #include <time.h>
00049 #endif
00050 
00051 #ifdef HAVE_STRING_H
00052 #include <string.h>
00053 #endif
00054 
00055 #include <stdio.h>
00056 #include <asm/param.h>
00057 #include <asm/page.h>
00058 
00059 #include "process.h"
00060 #include "sysdep.h"
00061 
00073 #define PAGE_TO_KBYTE_SHIFT PAGE_SHIFT-10
00074 
00075 static long mem_kbyte_max;
00076 
00077 int init_process_info_sysdep(void) {
00078 
00079   struct stat buf;
00080   
00081 
00082   /* I hope this is okay hack to get the total memsize. (-: */
00083 
00084   if ( stat("/proc/kcore", &buf) != 0 ) {
00085 
00086     return FALSE;
00087 
00088   }
00089 
00090   num_cpus= sysconf(_SC_NPROCESSORS_CONF);
00091 
00092   mem_kbyte_max = buf.st_size>>10;
00093 
00094   return TRUE;
00095 
00096 }
00097 
00098 
00099 int get_process_info_sysdep(ProcInfo_T p) {
00100 
00101   char buf[4096];
00102   char* tmp;
00103   char stat_item_state;
00104   unsigned long stat_item_utime;
00105   unsigned long stat_item_stime;
00106   long stat_item_cutime;
00107   long stat_item_cstime;
00108   long stat_item_rss;
00109 
00110   if (!read_proc_file(buf,4096, "stat", p->pid)) {
00111 
00112     return FALSE;
00113 
00114   }
00115 
00116   /* Move along the buffer to get past the process name */
00117 
00118   tmp = strrchr(buf, ')') + 2;
00119 
00120   /* This implementation is done by using fs/procfs/array.c as a basis
00121      it is also worth looking into the source of the procps utils */
00122 
00123   sscanf(tmp,"%c %*d %*d %*d %*d %*d %*u %*u"
00124      "%*u %*u %*u %lu %lu %ld %ld %*d %*d %*d "
00125      "%*d %*u %*u %ld %*u %*u %*u %*u %*u "
00126      "%*u %*u %*u %*u %*u %*u %*u %*u %*d %*d\n",
00127      &stat_item_state, &stat_item_utime, &stat_item_stime,
00128      &stat_item_cutime, &stat_item_cstime, &stat_item_rss);
00129 
00130   /* abs to please the compiler... we dont want to shift negatively.
00131      why doesn't C understand this??? */
00132 
00133   if ( PAGE_TO_KBYTE_SHIFT < 0 ) {
00134 
00135     p->mem_kbyte= stat_item_rss >> abs(PAGE_TO_KBYTE_SHIFT);
00136 
00137   } else {
00138 
00139     p->mem_kbyte= stat_item_rss << abs(PAGE_TO_KBYTE_SHIFT);
00140 
00141   }
00142 
00143   p->mem_percent = (int) ((double) p->mem_kbyte * 1000.0 / mem_kbyte_max);
00144 
00145   /* jiffies -> seconds = 1 / HZ
00146      HZ is defined in "asm/param.h"  and it is usually 1/100s but on
00147      alpha system it is 1/1024s */
00148 
00149   p->cputime_prev = p->cputime;
00150   p->cputime =  ( stat_item_utime + stat_item_stime ) * 10 / HZ;
00151 
00152   if ( include_children ) {
00153 
00154     p->cputime += ( stat_item_cutime + stat_item_cstime ) * 10 / HZ;
00155 
00156   }
00157 
00158   /* first run ? */
00159 
00160   if ( p->time_prev == 0.0 ) {
00161 
00162       p->cputime_prev = p->cputime;
00163 
00164   }
00165 
00166   /* State is Zombie -> then we are a Zombie ... clear or? (-: */
00167 
00168   if ( stat_item_state == 'Z' ) {
00169 
00170     p->status_flag |= PROCESS_ZOMBIE;
00171 
00172   }
00173 
00174   return TRUE;
00175 
00176 }