]> arthur.barton.de Git - netdata.git/blob - src/procfile.c
build: migrate to autotools
[netdata.git] / src / procfile.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <malloc.h>
11 #include <ctype.h>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <sys/wait.h>
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19
20 #include "common.h"
21 #include "log.h"
22 #include "procfile.h"
23
24 #define PF_PREFIX "PROCFILE"
25
26 #define PFWORDS_INCREASE_STEP 200
27 #define PFLINES_INCREASE_STEP 10
28 #define PROCFILE_INCREMENT_BUFFER 512
29
30 int procfile_adaptive_initial_allocation = 0;
31
32 // if adaptive allocation is set, these store the
33 // max values we have seen so far
34 uint32_t procfile_max_lines = PFLINES_INCREASE_STEP;
35 uint32_t procfile_max_words = PFWORDS_INCREASE_STEP;
36 size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
37
38 // ----------------------------------------------------------------------------
39 // An array of words
40
41
42 pfwords *pfwords_add(pfwords *fw, char *str) {
43         // debug(D_PROCFILE, PF_PREFIX ":       adding word No %d: '%s'", fw->len, str);
44
45         if(unlikely(fw->len == fw->size)) {
46                 // debug(D_PROCFILE, PF_PREFIX ":       expanding words");
47
48                 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
49                 if(unlikely(!new)) {
50                         error(PF_PREFIX ":      failed to expand words");
51                         free(fw);
52                         return NULL;
53                 }
54                 fw = new;
55                 fw->size += PFWORDS_INCREASE_STEP;
56         }
57
58         fw->words[fw->len++] = str;
59
60         return fw;
61 }
62
63 pfwords *pfwords_new(void) {
64         // debug(D_PROCFILE, PF_PREFIX ":       initializing words");
65
66         uint32_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
67
68         pfwords *new = malloc(sizeof(pfwords) + size * sizeof(char *));
69         if(unlikely(!new)) return NULL;
70
71         new->len = 0;
72         new->size = size;
73         return new;
74 }
75
76 void pfwords_reset(pfwords *fw) {
77         // debug(D_PROCFILE, PF_PREFIX ":       reseting words");
78         fw->len = 0;
79 }
80
81 void pfwords_free(pfwords *fw) {
82         // debug(D_PROCFILE, PF_PREFIX ":       freeing words");
83
84         free(fw);
85 }
86
87
88 // ----------------------------------------------------------------------------
89 // An array of lines
90
91 pflines *pflines_add(pflines *fl, uint32_t first_word) {
92         // debug(D_PROCFILE, PF_PREFIX ":       adding line %d at word %d", fl->len, first_word);
93
94         if(unlikely(fl->len == fl->size)) {
95                 // debug(D_PROCFILE, PF_PREFIX ":       expanding lines");
96
97                 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
98                 if(unlikely(!new)) {
99                         error(PF_PREFIX ":      failed to expand lines");
100                         free(fl);
101                         return NULL;
102                 }
103                 fl = new;
104                 fl->size += PFLINES_INCREASE_STEP;
105         }
106
107         fl->lines[fl->len].words = 0;
108         fl->lines[fl->len++].first = first_word;
109
110         return fl;
111 }
112
113 pflines *pflines_new(void) {
114         // debug(D_PROCFILE, PF_PREFIX ":       initializing lines");
115
116         uint32_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
117
118         pflines *new = malloc(sizeof(pflines) + size * sizeof(ffline));
119         if(unlikely(!new)) return NULL;
120
121         new->len = 0;
122         new->size = size;
123         return new;
124 }
125
126 void pflines_reset(pflines *fl) {
127         // debug(D_PROCFILE, PF_PREFIX ":       reseting lines");
128
129         fl->len = 0;
130 }
131
132 void pflines_free(pflines *fl) {
133         // debug(D_PROCFILE, PF_PREFIX ":       freeing lines");
134
135         free(fl);
136 }
137
138
139 // ----------------------------------------------------------------------------
140 // The procfile
141
142 #define PF_CHAR_IS_SEPARATOR    ' '
143 #define PF_CHAR_IS_NEWLINE              'N'
144 #define PF_CHAR_IS_WORD                 'W'
145
146 void procfile_close(procfile *ff) {
147         debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", ff->filename);
148
149         if(likely(ff->lines)) pflines_free(ff->lines);
150         if(likely(ff->words)) pfwords_free(ff->words);
151
152         if(likely(ff->fd != -1)) close(ff->fd);
153         free(ff);
154 }
155
156 procfile *procfile_parser(procfile *ff) {
157         debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
158
159         char *s = ff->data, *e = ff->data, *t = ff->data;
160         uint32_t l = 0, w = 0;
161         e += ff->len;
162         
163         ff->lines = pflines_add(ff->lines, w);
164         if(unlikely(!ff->lines)) goto cleanup;
165
166         while(likely(s < e)) {
167                 switch(ff->separators[(int)(*s)]) {
168                         case PF_CHAR_IS_SEPARATOR:
169                                 if(likely(s == t)) {
170                                         // skip all leading white spaces
171                                         t = ++s;
172                                         continue;
173                                 }
174
175                                 // end of word
176                                 *s = '\0';
177
178                                 ff->words = pfwords_add(ff->words, t);
179                                 if(unlikely(!ff->words)) goto cleanup;
180
181                                 ff->lines->lines[l].words++;
182                                 w++;
183
184                                 t = ++s;
185                                 continue;
186
187                         case PF_CHAR_IS_NEWLINE:
188                                 // end of line
189                                 *s = '\0';
190
191                                 ff->words = pfwords_add(ff->words, t);
192                                 if(unlikely(!ff->words)) goto cleanup;
193
194                                 ff->lines->lines[l].words++;
195                                 w++;
196
197                                 // debug(D_PROCFILE, PF_PREFIX ":       ended line %d with %d words", l, ff->lines->lines[l].words);
198
199                                 ff->lines = pflines_add(ff->lines, w);
200                                 if(unlikely(!ff->lines)) goto cleanup;
201                                 l++;
202
203                                 t = ++s;
204                                 continue;
205
206                         default:
207                                 s++;
208                                 continue;
209                 }
210         }
211
212         if(likely(s != t)) {
213                 // the last word
214                 if(likely(ff->len < ff->size)) *s = '\0';
215                 else {
216                         // we are going to loose the last byte
217                         ff->data[ff->size - 1] = '\0';
218                 }
219
220                 ff->words = pfwords_add(ff->words, t);
221                 if(unlikely(!ff->words)) goto cleanup;
222
223                 ff->lines->lines[l].words++;
224                 w++;
225         }
226
227         return ff;
228
229 cleanup:
230         error(PF_PREFIX ": Failed to parse file '%s'", ff->filename);
231         procfile_close(ff);
232         return NULL;
233 }
234
235 procfile *procfile_readall(procfile *ff) {
236         debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
237
238         ssize_t s = 0, r = ff->size, x = ff->size;
239         ff->len = 0;
240
241         while(likely(r == x)) {
242                 if(s) {
243                         debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
244
245                         procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
246                         if(unlikely(!new)) {
247                                 error(PF_PREFIX ": Cannot allocate memory for file '%s'", ff->filename);
248                                 procfile_close(ff);
249                                 return NULL;
250                         }
251                         ff = new;
252                         ff->size += PROCFILE_INCREMENT_BUFFER;
253                         x = PROCFILE_INCREMENT_BUFFER;
254                 }
255
256                 debug(D_PROCFILE, "Reading file '%s', from position %ld with length %ld", ff->filename, s, ff->size - s);
257                 r = read(ff->fd, &ff->data[s], ff->size - s);
258                 if(unlikely(r == -1)) {
259                         if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot read from file '%s'", ff->filename);
260                         procfile_close(ff);
261                         return NULL;
262                 }
263
264                 ff->len += r;
265                 s = ff->len;
266         }
267
268         debug(D_PROCFILE, "Rewinding file '%s'", ff->filename);
269         if(unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
270                 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot rewind on file '%s'.", ff->filename);
271                 procfile_close(ff);
272                 return NULL;
273         }
274
275         pflines_reset(ff->lines);
276         pfwords_reset(ff->words);
277
278         ff = procfile_parser(ff);
279
280         if(unlikely(procfile_adaptive_initial_allocation)) {
281                 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
282                 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
283                 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
284         }
285
286         debug(D_PROCFILE, "File '%s' updated.", ff->filename);
287         return ff;
288 }
289
290 static void procfile_set_separators(procfile *ff, const char *separators) {
291         static char def[256] = { [0 ... 255] = 0 };
292         int i;
293
294         if(unlikely(!def[255])) {
295                 // this is thread safe
296                 // we check that the last byte is non-zero
297                 // if it is zero, multiple threads may be executing this at the same time
298                 // setting in def[] the exact same values
299                 for(i = 0; likely(i < 256) ;i++) {
300                         if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
301                         else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
302                         else def[i] = PF_CHAR_IS_WORD;
303                 }
304         }
305
306         // copy the default
307         char *ffs = ff->separators, *ffd = def, *ffe = &def[256];
308         while(likely(ffd != ffe)) *ffs++ = *ffd++;
309
310         // set the separators
311         if(unlikely(!separators)) separators = " \t=|";
312         ffs = ff->separators;
313         const char *s = separators;
314         while(likely(*s)) ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
315 }
316
317 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
318         debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
319
320         int fd = open(filename, O_RDONLY, 0666);
321         if(unlikely(fd == -1)) {
322                 if(unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) error(PF_PREFIX ": Cannot open file '%s'", filename);
323                 return NULL;
324         }
325
326         size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
327         procfile *ff = malloc(sizeof(procfile) + size);
328         if(unlikely(!ff)) {
329                 error(PF_PREFIX ": Cannot allocate memory for file '%s'", filename);
330                 close(fd);
331                 return NULL;
332         }
333
334         strncpy(ff->filename, filename, FILENAME_MAX);
335         ff->filename[FILENAME_MAX] = '\0';
336
337         ff->fd = fd;
338         ff->size = size;
339         ff->len = 0;
340         ff->flags = flags;
341
342         ff->lines = pflines_new();
343         ff->words = pfwords_new();
344
345         if(unlikely(!ff->lines || !ff->words)) {
346                 error(PF_PREFIX ": Cannot initialize parser for file '%s'", filename);
347                 procfile_close(ff);
348                 return NULL;
349         }
350
351         procfile_set_separators(ff, separators);
352
353         debug(D_PROCFILE, "File '%s' opened.", filename);
354         return ff;
355 }
356
357 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
358         if(unlikely(!ff)) return procfile_open(filename, separators, flags);
359
360         if(likely(ff->fd != -1)) close(ff->fd);
361
362         ff->fd = open(filename, O_RDONLY, 0666);
363         if(unlikely(ff->fd == -1)) {
364                 procfile_close(ff);
365                 return NULL;
366         }
367
368         strncpy(ff->filename, filename, FILENAME_MAX);
369         ff->filename[FILENAME_MAX] = '\0';
370
371         ff->flags = flags;
372
373         // do not do the separators again if NULL is given
374         if(likely(separators)) procfile_set_separators(ff, separators);
375
376         return ff;
377 }
378
379 // ----------------------------------------------------------------------------
380 // example parsing of procfile data
381
382 void procfile_print(procfile *ff) {
383         uint32_t lines = procfile_lines(ff), l;
384         uint32_t words, w;
385         char *s;
386
387         debug(D_PROCFILE, "File '%s' with %d lines and %d words", ff->filename, ff->lines->len, ff->words->len);
388
389         for(l = 0; likely(l < lines) ;l++) {
390                 words = procfile_linewords(ff, l);
391
392                 debug(D_PROCFILE, "     line %d starts at word %d and has %d words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
393
394                 for(w = 0; likely(w < words) ;w++) {
395                         s = procfile_lineword(ff, l, w);
396                         debug(D_PROCFILE, "             [%d.%d] '%s'", l, w, s);
397                 }
398         }
399 }