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

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

initial import for the community edition

Line 
1/*
2        gmemusage
3
4        Copyright 2008-2009, Genirix Systems
5
6*/
7
8
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <dirent.h>
14#include <stdio.h>
15#include <errno.h>
16
17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
20#include <sys/time.h>
21//#include <sys/types.h>
22//#include <sys/uio.h>
23
24#include "process.h"
25#include "extern.h"
26
27/*  Hooks for the optimization code...
28    Only for the commercial version
29*/
30
31char * fastload(char *);
32void fastlist(void);
33
34void fastlist(void)
35{
36        DIR             *directory;
37        struct  dirent          *entry;
38        int             i = 0;
39    int         fd;
40
41        long    start_time, total_time, end_time;
42        long    start_time2, total_time2, end_time2;
43
44        start_time = get_time();
45
46        directory = opendir("/proc");
47
48
49        while ( (entry = readdir(directory)) )
50        {
51                char    path[PATH_MAX];
52
53                char*   buf;
54                int     z;
55
56                char    tmpString[1024];
57                int             tmpInt;
58                long    tmpLong;
59                char    *cp;
60
61                if (*entry->d_name < '0' || *entry->d_name > '9')
62                        continue;
63
64                sprintf(path, "/proc/%s/status", entry->d_name);
65
66                // this will load in a allocate buffer the content of the /proc/PID/status file
67                // remember to free it when done with it!
68
69                start_time2 = get_time();
70
71                buf = fastload(path);
72
73                end_time2 = get_time();
74
75                fprintf(stdout,"fastload(%s) %lu usec\n", path, end_time2 - start_time2);
76
77
78/*
79                sscanf(s, "%*s %s", tmpString);
80                strncpy(processes[i].name, tmpString, 31);
81*/
82
83                i++;
84
85                // dealocate the buffer
86                free(buf);
87        }
88        total_time = get_time () - start_time;
89
90        printf ("\nTime elapsed in fastlist() for %duSec processes=%d\n", total_time, i);
91
92//      processCount = i;
93}
94
95/**  fast IO read text file for /proc
96 *
97 */
98char * fastload(char * filename)
99{
100    int fd ;
101    int result;
102    char buffer[2048];
103    long start_time, end_time;
104    int len;
105    char *newbuf;
106
107    // check the existance of the file
108/*
109    start_time = get_time();
110    result = access(filename,F_OK);
111        end_time = get_time();
112
113        fprintf(stdout,"access() %lu usec, ", end_time - start_time);
114
115    if ( result == -1 )
116    {
117        fprintf(stderr,"Error while trying to access [%d] %s\n", result ,filename);
118        return NULL;
119    }
120*/
121
122    // open the files
123//    start_time = get_time();
124    fd = open(filename,O_RDONLY);
125//    end_time = get_time();
126
127//    fprintf(stdout,"open() %lu usec, ", end_time - start_time);
128
129    if (fd == -1)
130    {
131        fprintf(stderr,"Error while trying to open: %s\n", filename);
132        return NULL;
133    }
134
135    // reset the result var.
136
137//      start_time = get_time();
138    result = read(fd, buffer, sizeof(buffer));
139    close (fd);
140//    end_time = get_time();
141
142//    fprintf(stdout,"read() %lu usec, ", end_time - start_time);
143
144
145        // some IO error basic handling
146        if (result == -1)
147        {
148                fprintf(stderr,"%s reading file %s\n", strerror(errno),filename);
149                return NULL;
150        }
151
152        len = strlen(buffer);
153
154        // allocate some memory
155        newbuf = (char*) malloc( len+1 );
156        newbuf[0] = 0;
157
158        strncpy(newbuf, buffer, (len) );
159
160
161 //     fprintf(stdout,"read[%d] bytes and returning[%d] bytes\n",sizeof(buffer), strlen(newbuf) );
162
163    return newbuf;
164}
Note: See TracBrowser for help on using the browser.