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