/[monit]/monit/process/sysdep.c
ViewVC logotype

Diff of /monit/process/sysdep.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by hauk, Fri Sep 13 01:32:26 2002 UTC revision 1.6 by rory, Fri Sep 27 01:56:29 2002 UTC
# Line 75  Line 75 
75  #include <sys/sysctl.h>  #include <sys/sysctl.h>
76  #endif  #endif
77    
78    #ifdef DARWIN
79    #include <kvm.h>
80    #include <sys/param.h>
81    #include <sys/proc.h>
82    #include <sys/resource.h>
83    #include <sys/resourcevar.h>
84    #include <sys/lock.h>
85    #include <sys/user.h>
86    #include <machine/vmparam.h>
87    #include <sys/vmmeter.h>
88    #include <sys/sysctl.h>
89    #endif
90    
91  #ifdef LINUX  #ifdef LINUX
92  #include <asm/param.h>  #include <asm/param.h>
93  #include <asm/page.h>  #include <asm/page.h>
# Line 491  int get_process_info_sysdep(ProcInfo_T p Line 504  int get_process_info_sysdep(ProcInfo_T p
504    rss_lim = (u_int64_t)    rss_lim = (u_int64_t)
505      (plimit.pl_rlimit [RLIMIT_RSS].rlim_cur);      (plimit.pl_rlimit [RLIMIT_RSS].rlim_cur);
506    
507      vms = &pinfo [0].kp_eproc.e_vm;
508    
509      p->mem_kbyte= (u_int64_t) pagetok (vms->vm_rssize);
510                 /* <<LOG1024 removed, we wanna have kb */
511    
512      /* ----------------------------- STATE ---------------------------------*/
513      /* Got it from libgtop/sysdep/freebsd/procstate.c */
514    
515      if ( pinfo [0].kp_proc.p_stat == SZOMB ) {
516    
517        p->status_flag |= PROCESS_ZOMBIE;
518    
519      }
520    
521      p->mem_percent = (int) ((double) p->mem_kbyte * 1000.0 / mem_kbyte_max);
522    
523      return TRUE;
524    
525    }
526    
527    #endif
528    
529    #ifdef DARWIN
530    
531    #define pagetok(size) ((size) << pageshift)
532    #define tv2sec(tv) (((u_int64_t) tv.tv_sec * 1000000) + (u_int64_t) tv.tv_usec)
533    
534    static int pageshift;
535    static long mem_kbyte_max;
536    
537    #ifndef LOG1024
538    #define LOG1024         10
539    #endif
540    
541    static kvm_t * kvm_handle;
542    
543    static void calcru(struct proc *p, struct timeval *up, struct timeval *sp,
544                       struct timeval *ip)
545    {
546      quad_t totusec;
547      u_quad_t u, st, ut, it, tot;
548    #if (__FreeBSD_version < 300003)
549      long sec, usec;
550    #endif
551      
552      st = p->p_sticks;
553      ut = p->p_uticks;
554      it = p->p_iticks;
555      
556      tot = st + ut + it;
557      if (tot == 0)
558        {
559          st = 1;
560          tot = 1;
561        }
562      
563    #if (defined __FreeBSD__) && (__FreeBSD_version >= 300003)
564      totusec = (u_quad_t) p->p_runtime;
565    #else
566      sec = p->p_rtime.tv_usec;
567      usec = p->p_rtime.tv_usec;
568      
569      totusec = (quad_t)sec * 1000000 + usec;
570    #endif
571      
572      if(totusec < 0)
573        {
574          fprintf (stderr, "calcru: negative time: %ld usec\n",
575                   (long)totusec);
576          totusec = 0;
577        }
578      
579      u = totusec;
580      st = (u * st) / tot;
581      sp->tv_sec = st / 1000000;
582      sp->tv_usec = st % 1000000;
583      ut = (u * ut) / tot;
584      up->tv_sec = ut / 1000000;
585      up->tv_usec = ut % 1000000;
586      
587      if(ip != NULL)
588        {
589          it = (u * it) / tot;
590          ip->tv_sec = it / 1000000;
591          ip->tv_usec = it % 1000000;
592        }
593    }
594    
595    int init_process_info_sysdep(void) {
596    
597      register int pagesize;
598      struct vmmeter vmm;
599      
600      struct nlist nlst [] = {
601          { "_bufspace"},
602          { "_cnt" },
603          { 0 }
604      };
605    
606      if(getuid()!=0) {
607    
608        return FALSE;
609    
610      }
611    
612      kvm_handle = kvm_open(NULL, NULL, NULL, O_RDONLY, "monit");
613      
614      if ( kvm_handle == NULL ) {
615        
616        return FALSE;
617        
618      }
619      
620      /* ----------------------------- INIT MEM -----------------------------*/
621      /* Got it from libgtop/sysdep/freebsd/mem.c */
622    
623      /* Initialize nlist structure */
624      if (kvm_nlist (kvm_handle, nlst) < 0)
625      {
626        return FALSE;
627      }
628    
629      /* get the page size with "getpagesize" and calculate pageshift
630       * from it */
631    
632      pagesize = getpagesize ();
633      pageshift = 0;
634      while (pagesize > 1) {
635    
636        pageshift++;
637        pagesize >>= 1;
638    
639      }
640    
641      /* we only need the amount of log(2)1024 for our conversion */
642      pageshift -= LOG1024;
643    
644      /* Get the data from kvm_* */
645      if (kvm_read (kvm_handle, nlst[1].n_value,
646                    &vmm, sizeof (vmm)) != sizeof (vmm)) {
647        return FALSE;
648    
649      }
650    
651      mem_kbyte_max= vmm.v_free_min +
652        vmm.v_free_count + vmm.v_wire_count +
653        vmm.v_active_count + vmm.v_inactive_count;
654    
655      return TRUE;
656    
657    }
658    
659    int get_process_info_sysdep(ProcInfo_T p) {
660    
661      struct kinfo_proc *pinfo;
662      struct pstats pstats;
663      struct plimit plimit;
664      struct vmspace *vms;
665      register struct rusage *rup;
666      long stat_utime;
667      long stat_stime;
668      long stat_cutime;
669      long stat_cstime;
670    
671      u_int64_t rss_lim;
672    
673      int count;
674    
675      /* Put this here until fixed */
676      return FALSE;
677    
678      /* Got it from libgtop */
679    
680      pinfo = kvm_getprocs(kvm_handle, KERN_PROC_PID, p->pid, &count);
681    
682      if ((pinfo == NULL) || (count < 1)) {
683    
684        return FALSE;
685    
686      }
687    
688      /* ----------------------------- CPU TIMING ----------------------------*/
689      /* Got it from libgtop/sysdep/freebsd/proctime.c */
690      
691      /*  if ((pinfo [0].kp_proc.p_flag & P_INMEM) &&
692          kvm_read (kvm_handle, &(pinfo [0]).kp_proc,
693                     (unsigned long) &u_addr->u_stats,
694                     (char *) &pstats, sizeof (pstats)) == sizeof (pstats)) {
695    
696        rup = &pstats.p_ru;
697        calcru(&(pinfo [0]).kp_proc,
698               &rup->ru_utime, &rup->ru_stime, NULL);
699    
700        stat_utime = tv2sec (pstats.p_ru.ru_utime);
701        stat_stime = tv2sec (pstats.p_ru.ru_stime);
702    
703        stat_cutime = tv2sec (pstats.p_cru.ru_utime);
704        stat_cstime = tv2sec (pstats.p_cru.ru_stime);
705    
706      } else {
707    
708        return FALSE;
709    
710        } */
711    
712      p->cputime_prev= p->cputime;
713      p->cputime= ( stat_utime + stat_stime );
714    
715      if( include_children ) {
716    
717        p->cputime+= ( stat_cutime + stat_cstime );
718    
719      }
720    
721      /* first run ? */
722    
723      if ( p->time_prev == 0.0 ) {
724    
725        p->cputime_prev= p->cputime;
726    
727      }
728    
729      /* ----------------------------- MEMORY --------------------------------*/
730      /* Got it from libgtop/sysdep/freebsd/procmem.c */
731    
732      /*  if (kvm_read (kvm_handle,
733                    (unsigned long) pinfo [0].kp_proc.p_limit,
734                    (char *) &plimit, sizeof (plimit)) != sizeof (plimit)) {
735    
736        return FALSE;
737    
738      }
739      */
740      rss_lim = (u_int64_t)
741        (plimit.pl_rlimit [RLIMIT_RSS].rlim_cur);
742    
743    vms = &pinfo [0].kp_eproc.e_vm;    vms = &pinfo [0].kp_eproc.e_vm;
744    
745    p->mem_kbyte= (u_int64_t) pagetok (vms->vm_rssize);    p->mem_kbyte= (u_int64_t) pagetok (vms->vm_rssize);

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26