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