"Fossies" - the Fresh Open Source Software Archive

Member "memtime-1.3/linux.c" (21 Jan 2002, 3031 Bytes) of package /linux/misc/old/memtime-1.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "linux.c" see the Fossies "Dox" file reference documentation.

    1 /* -*- mode: C; c-file-style: "k&r"; -*-
    2  *---------------------------------------------------------------------------*
    3  *
    4  * Copyright (c) 2000, Johan Bengtsson
    5  * 
    6  * Permission is hereby granted, free of charge, to any person obtaining a
    7  * copy of this software and associated documentation files (the "Software"),
    8  * to deal in the Software without restriction, including without limitation
    9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   10  * and/or sell copies of the Software, and to permit persons to whom the
   11  * Software is furnished to do so, subject to the following conditions:
   12  * The above copyright notice and this permission notice shall be included in
   13  * all copies or substantial portions of the Software.
   14  *
   15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   21  * DEALINGS IN THE SOFTWARE.
   22  *
   23  *---------------------------------------------------------------------------*/
   24 
   25 #include <sys/time.h>
   26 #include <asm/param.h>
   27 #include <sys/stat.h>
   28 #include <unistd.h>
   29 #include <fcntl.h>
   30 
   31 #include <stdio.h>
   32 #include <string.h>
   33 
   34 #include <sys/resource.h>
   35 
   36 #include "machdep.h"
   37 
   38 static int proc_fd = -1;
   39 
   40 
   41 int init_machdep(pid_t process)
   42 {
   43      char filename[64];
   44      sprintf(filename, "/proc/%d/stat", (int)process);
   45      proc_fd = open(filename, O_RDONLY);
   46 
   47      return (proc_fd != -1);
   48 }
   49 
   50 int get_sample(struct memtime_info *info)
   51 {
   52      static char buffer[2048];
   53      char *tmp;
   54      int i, utime, stime;
   55      unsigned int vsize, rss;
   56      int rc;
   57 
   58      lseek(proc_fd, 0, SEEK_SET);
   59      rc = read(proc_fd, buffer, 2048);
   60 
   61      if (rc == -1)
   62       return 0;
   63 
   64      *(buffer + rc) = '\0';
   65 
   66      for (i=0, tmp=buffer; i < 13; i++)
   67       tmp = strchr(tmp + 1, ' ');
   68 
   69      sscanf(tmp + 1, "%d %d", &utime, &stime);
   70     
   71      for (/* empty */; i < 22; i++)
   72       tmp = strchr(tmp + 1, ' ');
   73 
   74      sscanf(tmp + 1, "%u %u", &vsize, &rss);
   75 
   76      info->utime_ms = utime * (1000 / HZ);
   77      info->stime_ms = stime * (1000 / HZ);
   78 
   79      info->rss_kb = (rss * getpagesize()) / 1024;
   80      info->vsize_kb = vsize / 1024;
   81 
   82      return 1;
   83 }
   84 
   85 unsigned int get_time()
   86 {
   87      struct timeval now;
   88      struct timezone dummy;
   89      int rc;
   90 
   91      rc = gettimeofday(&now, &dummy);
   92     
   93      if (rc == -1) {
   94       return 0;
   95      }
   96 
   97      return (now.tv_sec * 1000) + (now.tv_usec / 1000);
   98 }
   99 
  100 int set_mem_limit(long int maxbytes)
  101 {
  102     struct  rlimit rl;
  103     long int softlimit=(long int)maxbytes*0.95;
  104     rl.rlim_cur=softlimit; 
  105     rl.rlim_max=maxbytes;
  106     return setrlimit(RLIMIT_AS,&rl);
  107 }
  108 
  109 int set_cpu_limit(long int maxseconds)
  110 {
  111     struct  rlimit rl;
  112     rl.rlim_cur=maxseconds; 
  113     rl.rlim_max=maxseconds;
  114     return setrlimit(RLIMIT_CPU,&rl);
  115 }