root/foundation-apps/gmemusage-maxx/processlist.c

Revision 1, 7.7 KB (checked in by emasson, 3 years ago)

initial import for the community edition

Line 
1/*
2        gmemusage
3
4        Copyright 1998, Michael Pruett
5        Copyright 1999, Mark B. Allan
6        Copyright 2000-2008, Eric Masson
7        Copyright 2008-2009, Genirix Systems
8
9        This program is free software; you can redistribute it and/or
10        modify it under the terms of the GNU General Public License
11        as published by the Free Software Foundation; either version 2
12        of the License, or (at your option) any later version.
13
14        This program is distributed in the hope that it will be useful,
15        but WITHOUT ANY WARRANTY; without even the implied warranty of
16        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17        GNU General Public License for more details.
18
19        You should have received a copy of the GNU General Public License
20        along with this program; if not, write to the Free Software
21        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
22        USA.
23*/
24
25
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <dirent.h>
31#include <stdio.h>
32#include <errno.h>
33
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include <sys/time.h>
38//#include <sys/types.h>
39//#include <sys/uio.h>
40
41#include "process.h"
42#include "extern.h"
43
44/****************************************************************************/
45
46void init(void);
47void checkProc(void);
48void list (void);
49
50
51void getKernelMemory(void);
52void getSystemMemory(void);
53
54long getMemoryIOMEM (char *data);
55long getByteSize(char * range);
56
57long get_time (void);
58long get_time_in_milliseconds (void);
59
60
61
62/****************************************************************************/
63
64long get_time (void)
65{
66        struct timeval tv;
67        struct timezone tz;
68        gettimeofday (&tv, &tz);
69
70         //return (long)tv.tv_usec;
71        return tv.tv_sec * 1000 + tv.tv_usec / 1000;
72}
73
74long get_time_in_milliseconds ()
75{
76    struct timeval  tv;
77    struct timezone tz;
78    gettimeofday (&tv, &tz);
79    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
80}
81
82void init(void)
83{
84        kilobyte = 1024;
85        megabyte = kilobyte * 1024;
86        gigabyte =  megabyte * 1024;
87
88//      char *content;
89//      content =fastload("./meminfo.txt");
90//      fprintf(stdout, "%s", content);
91
92        // verify the /proc filesystem
93        checkProc();
94
95        // load the kernel memory details
96        getKernelMemory();
97
98        // load the system memory details
99        getSystemMemory();
100}
101
102void checkProc(void)
103{
104        FILE*   procfile;
105
106        procfile = fopen("/proc/iomem", "r");
107        if (!procfile)
108        {
109//              printf("gmemusage info: /proc/iomem not present!\n");
110                system_detail.iomem_present = FALSE;
111        }
112        else
113        {
114                system_detail.iomem_present = TRUE;
115//              printf("gmemusage info: /proc/iomem present.\n");
116                fclose(procfile);
117        }
118}
119
120void getKernelMemory(void)
121{
122
123        system_detail.kernel_code = getMemoryIOMEM("Kernel code");
124        system_detail.kernel_data = getMemoryIOMEM("Kernel data");
125
126        system_detail.kernel_size = system_detail.kernel_code + system_detail.kernel_data;
127
128//      printf("Kernel Size = %lu KB\n", system_detail.kernel_size / kilobyte );
129}
130
131void getSystemMemory(void)
132{
133        system_detail.system_ram= getMemoryIOMEM("System RAM");
134
135//      printf("TotalMemory = %f2.2 MB\n", (float)(system_detail.system_ram / megabyte));
136
137}
138
139
140/* get the requested memory size in bytes */
141long getMemoryIOMEM (char *data)
142{
143        FILE*   memfile;
144        char    s[1024];
145        char    range[1024];
146        size_t  size =0;
147        int     buf_size = 128;
148        memfile = fopen("/proc/iomem", "r");
149//      memfile = fopen("./iomem.txt", "r");
150
151
152        if (!memfile)
153        {
154                printf("gmemusage error:  unable to open /proc/iomem !  Terminating the process.\n");
155                return (0);
156        }
157
158        char* result = NULL;
159        while (fgets(s,buf_size, memfile)!=NULL)
160        {
161                result = strstr( s, data);
162                if( result != NULL )
163                {
164                //      printf( "Found a substring: %s", result);
165                        sscanf(s, "%s% %*s", &range);
166                        size += getByteSize(range);
167                        //printf( " with %lu bytes\n", size);
168                }
169        }
170
171        fclose( memfile );
172
173//      printf( "Memory size for /proc/iomem::%s (%lu) \n",data,size );
174
175        return size;
176}
177
178long getByteSize(char * range)
179{
180        char    slow[64];
181        char    dash[1];
182        char    shigh[64];
183        char    *t;
184        long    low, high, size;
185
186        char    delims[] = "-";
187        char    *result = NULL;
188
189        // get low bytes
190        result = strtok( range, delims );
191        low =  strtoul(result,&t,16);
192        //printf( "low [%s]:%lu \n",result,low );
193
194        // get high bytes
195        result = strtok( NULL, delims );
196        high =  strtoul(result,&t,16);
197
198        //printf( "high [%s]%lu \n",result,high );
199
200        size = high-low;
201
202        return size;
203
204}
205
206void list (void)
207{
208        DIR                             *directory;
209        struct dirent   *entry;
210        int                             i = 0;
211        int                     buf_size = 128;
212
213        int                     pid;
214        char            path[PATH_MAX];
215        FILE*           statusFile;
216        char            s[1024];
217        char            comm[256];
218        char            tmpString[1024];
219        int                     tmpInt;
220        long            tmpLong;
221        size_t          proc_size;
222        int                     ppid;
223        char            *cp;
224        int                     x;
225        char            file[16];
226        int                     found=0;
227        long            start_time, total_time;
228        long            start_time2, total_time2, end_time2;
229
230
231//  start_time = get_time();
232
233        directory = opendir("/proc");
234
235
236        while ( (entry = readdir(directory)) )
237        {
238
239                if (*entry->d_name < '0' || *entry->d_name > '9')
240                        continue;
241
242                strcpy(file, entry->d_name);
243                sprintf(path, "/proc/%s/status",file );
244
245                // opent the status file for that specific PID
246                statusFile = fopen(path, "r");
247
248                //  file = fastload(path);
249                //      printf("%d",sizeof(file));
250
251        /**
252         * Check if the file is open... Maybe the process is already gone!
253         * Fix null pointer bug when.  Thx Peter Kovar
254         */
255                if (statusFile == NULL)
256                        continue;
257
258
259                // initialize iteration variables
260                s[buf_size] = 0;
261
262                processes[i].pid = atoi(file);
263                processes[i].live = 0;
264                processes[i].vsize = 0;
265                processes[i].hwm = 0;
266                processes[i].rss = 0;
267                processes[i].data = 0;
268                processes[i].stack = 0;
269                processes[i].exe = 0;
270                processes[i].lib = 0;
271                processes[i].threads = 0;
272
273                fgets(s, buf_size, statusFile); /* Name */
274                sscanf(s, "%*s %s", tmpString);
275                strncpy(processes[i].name, tmpString, 31);
276
277                // find VmSize
278                char* result;
279               
280                while (fgets(s,buf_size, statusFile)!=NULL)
281                {
282                        //fgets(s, buf_size, statusFile);
283                        //printf(">>%s",s);
284                       
285                        result = 0;
286                        result = strstr( s, "VmSize");
287
288                        if( result != NULL )
289                                break;
290                }
291
292                // detect the presence of the VmSize field (if not, it is more likely to be a kernel related process
293                cp = index(s, 'k');
294
295                if (cp == NULL)
296                {
297                        //printf("oops!, no VmSize for %s\n", entry->d_name);
298                }
299                else
300                {
301                        sscanf(s, "%*s %lu %*s ", &tmpLong);
302                        processes[i].vsize = (tmpLong * 1024); // convert KB into bytes
303
304                        fgets(s, buf_size, statusFile); /* VmLck  */
305
306                        fgets(s, buf_size, statusFile); /* VmHWM  */
307                        sscanf(s, "%*s %lu %*s ", &tmpLong);
308                        processes[i].hwm = (tmpLong * 1024); // convert KB into bytes
309
310                        fgets(s, buf_size, statusFile); /* VmRSS  */
311                        sscanf(s, "%*s %lu %*s ", &tmpLong);
312                        processes[i].rss = (tmpLong * 1024); // convert KB into bytes
313
314
315                        fgets(s, buf_size, statusFile); /* VmData */
316                        sscanf(s, "%*s %lu %*s ", &tmpLong);
317                        processes[i].data = (tmpLong * 1024); // convert KB into bytes
318
319                        fgets(s, buf_size, statusFile); /* VmStk  */
320                        sscanf(s, "%*s %lu %*s ", &tmpLong);
321                        processes[i].stack = (tmpLong * 1024); // convert KB into bytes
322
323                        fgets(s, buf_size, statusFile); /* VmExe */
324                        sscanf(s, "%*s %lu %*s ", &tmpLong);
325                        processes[i].exe = (tmpLong * 1024); // convert KB into bytes
326
327                        fgets(s, buf_size, statusFile); /* VmLib  */
328                        sscanf(s, "%*s %lu %*s ", &tmpLong);
329                        processes[i].lib = (tmpLong * 1024); // convert KB into bytes
330
331                        processes[i].live = processes[i].rss;
332
333                }
334                fclose(statusFile);
335               
336//              end_time2 = get_time();
337//              printf ("\nTime elapsed in each %d uSec\n", end_time2 - start_time2);
338//              printf("Pid=%d\t name=%s \t\t\tVMSize=%lu \t\tLive=%lu \ttDatas=%d\n", processes[i].pid, processes[i].name, processes[i].vsize, processes[i].live ,processes[i].data );
339
340                i++;
341        }
342
343        closedir(directory);
344//      printf("--------------------------------------------------------------\n");
345//  total_time = get_time () - start_time;
346//      printf ("\nTime elapsed in list() for %duSec processes=%d \n", total_time, i);
347
348        processCount = i;
349//      exit(0);
350}
351
Note: See TracBrowser for help on using the browser.