ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/residentMem.c
Revision: 357
Committed: Wed Feb 16 21:13:48 2005 UTC (20 years, 2 months ago) by tim
Content type: text/plain
Original Path: trunk/src/utils/residentMem.c
File size: 1237 byte(s)
Log Message:
return residentMem in byte

File Contents

# User Rev Content
1 gezelter 340 #include <config.h>
2     #include <string.h>
3     #include <stdio.h>
4 gezelter 345 #include <stdlib.h>
5     #ifdef __sgi
6     #include <unistd.h>
7     #endif
8 gezelter 340
9 gezelter 343 #define to_string( s ) # s
10     #define STR_DEFINE(t, s) t = to_string(s)
11    
12 gezelter 345 /*
13     * returns an estimate of the resident memory size in kB
14     */
15 gezelter 340 double residentMem () {
16    
17     FILE* procresults;
18     char buf[150];
19     char* foo;
20     long int myRSS, totRSS;
21 gezelter 342 char pscommand[150];
22 gezelter 343 char* psPath;
23 gezelter 340
24 gezelter 343 STR_DEFINE(psPath, PSCOMMAND );
25 gezelter 344
26     // null terminated string is one longer....
27     strncpy(pscommand, psPath, strlen(psPath)+1);
28 gezelter 340
29 gezelter 345 #ifdef PSTYPE_IS_BSD
30 gezelter 340 strcat(pscommand, " ax -o rss");
31     #else
32 gezelter 345 #ifdef PSTYPE_IS_POSIX
33 gezelter 340 strcat(pscommand, " -ef -o rss");
34 gezelter 345 #else
35     printf("Unknown ps syntax!\n");
36 gezelter 340 #endif
37 gezelter 345 #endif
38 gezelter 340
39 gezelter 344 printf("doing %s\n", pscommand);
40    
41 gezelter 340 procresults = popen(pscommand, "r");
42    
43     totRSS = 0;
44     while (!feof(procresults)) {
45     fgets( buf, 150, procresults);
46     if (!strcmp(buf, " RSS")) continue;
47    
48     foo = strtok(buf, " ,;\t");
49     myRSS = atoi(foo);
50     totRSS += myRSS;
51     }
52     pclose(procresults);
53    
54 gezelter 345 #ifdef __sgi
55     // Damn IRIX machines uses pages for RSS and pagesize is variable
56     // depending on version of the OS.
57     totRSS *= getpagesize() / 1024;
58     #endif
59    
60 tim 357 //return in byte
61     totRSS *= 1024;
62 gezelter 340 return(totRSS);
63    
64     }