diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-26 11:12:09 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-26 11:12:09 +0000 |
commit | bfc93a3caf5fca9cbece04700a882e8bd54e3bd2 (patch) | |
tree | 578ea205bbcf17f2c4463e83b3e3343ffac5ca98 | |
parent | ee5dce336597c76d674b570ed2c85041d50b6339 (diff) | |
download | busybox-w32-bfc93a3caf5fca9cbece04700a882e8bd54e3bd2.tar.gz busybox-w32-bfc93a3caf5fca9cbece04700a882e8bd54e3bd2.tar.bz2 busybox-w32-bfc93a3caf5fca9cbece04700a882e8bd54e3bd2.zip |
nmeter: 4k buffers are too small for /proc files,
make them dynamically sized with 16k upper limit. +75 bytes
-rw-r--r-- | procps/nmeter.c | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/procps/nmeter.c b/procps/nmeter.c index cded67e56..068c739fb 100644 --- a/procps/nmeter.c +++ b/procps/nmeter.c | |||
@@ -22,11 +22,14 @@ | |||
22 | 22 | ||
23 | typedef unsigned long long ullong; | 23 | typedef unsigned long long ullong; |
24 | 24 | ||
25 | enum { PROC_FILE_SIZE = 4096 }; | 25 | enum { /* Preferably use powers of 2 */ |
26 | PROC_MIN_FILE_SIZE = 256, | ||
27 | PROC_MAX_FILE_SIZE = 16 * 1024, | ||
28 | }; | ||
26 | 29 | ||
27 | typedef struct proc_file { | 30 | typedef struct proc_file { |
28 | char *file; | 31 | char *file; |
29 | //const char *name; | 32 | int file_sz; |
30 | smallint last_gen; | 33 | smallint last_gen; |
31 | } proc_file; | 34 | } proc_file; |
32 | 35 | ||
@@ -124,30 +127,42 @@ static void put_question_marks(int count) | |||
124 | put_c('?'); | 127 | put_c('?'); |
125 | } | 128 | } |
126 | 129 | ||
127 | static void readfile_z(char *buf, int sz, const char* fname) | 130 | static void readfile_z(proc_file *pf, const char* fname) |
128 | { | 131 | { |
129 | // open_read_close() will do two reads in order to be sure we are at EOF, | 132 | // open_read_close() will do two reads in order to be sure we are at EOF, |
130 | // and we don't need/want that. | 133 | // and we don't need/want that. |
131 | // sz = open_read_close(fname, buf, sz-1); | 134 | int fd; |
132 | 135 | int sz, rdsz; | |
133 | int fd = xopen(fname, O_RDONLY); | 136 | char *buf; |
137 | |||
138 | sz = pf->file_sz; | ||
139 | buf = pf->file; | ||
140 | if (!buf) { | ||
141 | buf = xmalloc(PROC_MIN_FILE_SIZE); | ||
142 | sz = PROC_MIN_FILE_SIZE; | ||
143 | } | ||
144 | again: | ||
145 | fd = xopen(fname, O_RDONLY); | ||
134 | buf[0] = '\0'; | 146 | buf[0] = '\0'; |
135 | sz = read(fd, buf, sz - 1); | 147 | rdsz = read(fd, buf, sz-1); |
136 | if (sz > 0) | ||
137 | buf[sz] = '\0'; | ||
138 | close(fd); | 148 | close(fd); |
149 | if (rdsz > 0) { | ||
150 | if (rdsz == sz-1 && sz < PROC_MAX_FILE_SIZE) { | ||
151 | sz *= 2; | ||
152 | buf = xrealloc(buf, sz); | ||
153 | goto again; | ||
154 | } | ||
155 | buf[rdsz] = '\0'; | ||
156 | } | ||
157 | pf->file_sz = sz; | ||
158 | pf->file = buf; | ||
139 | } | 159 | } |
140 | 160 | ||
141 | static const char* get_file(proc_file *pf) | 161 | static const char* get_file(proc_file *pf) |
142 | { | 162 | { |
143 | if (pf->last_gen != gen) { | 163 | if (pf->last_gen != gen) { |
144 | pf->last_gen = gen; | 164 | pf->last_gen = gen; |
145 | // We allocate PROC_FILE_SIZE bytes. This wastes memory, | 165 | readfile_z(pf, proc_name[pf - &first_proc_file]); |
146 | // but allows us to allocate only once (at first sample) | ||
147 | // per proc file, and reuse buffer for each sample | ||
148 | if (!pf->file) | ||
149 | pf->file = xmalloc(PROC_FILE_SIZE); | ||
150 | readfile_z(pf->file, PROC_FILE_SIZE, proc_name[pf - &first_proc_file]); | ||
151 | } | 166 | } |
152 | return pf->file; | 167 | return pf->file; |
153 | } | 168 | } |