master
c 549 lines 17.4 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "../libnetdata.h"
4
5 #define PF_PREFIX "PROCFILE"
6
7 #define PFWORDS_INCREASE_STEP 2000
8 #define PFLINES_INCREASE_STEP 200
9 #define PROCFILE_INCREMENT_BUFFER 4096
10
11 int procfile_open_flags = O_RDONLY | O_CLOEXEC;
12
13 // if adaptive allocation is set, these store the
14 // max values we have seen so far
15 static bool procfile_adaptive_initial_allocation = false;
16 static size_t procfile_max_lines = PFLINES_INCREASE_STEP;
17 static size_t procfile_max_words = PFWORDS_INCREASE_STEP;
18 static size_t procfile_max_allocation = PROCFILE_INCREMENT_BUFFER;
19
20 void procfile_set_adaptive_allocation(bool enable, size_t bytes, size_t lines, size_t words) {
21 procfile_adaptive_initial_allocation = enable;
22
23 if(bytes > procfile_max_allocation)
24 procfile_max_allocation = bytes;
25 if(lines > procfile_max_lines)
26 procfile_max_lines = lines;
27 if(words > procfile_max_words)
28 procfile_max_words = words;
29 }
30
31 // ----------------------------------------------------------------------------
32
33 char *procfile_filename(procfile *ff) {
34 if(ff->filename)
35 return ff->filename;
36
37 char filename[FILENAME_MAX + 1];
38 char buffer[FILENAME_MAX + 1];
39 snprintfz(buffer, FILENAME_MAX, "/proc/self/fd/%d", ff->fd);
40
41 ssize_t l = readlink(buffer, filename, FILENAME_MAX);
42 if(unlikely(l == -1))
43 snprintfz(filename, FILENAME_MAX, "unknown filename for fd %d", ff->fd);
44 else
45 filename[l] = '\0';
46
47
48 ff->filename = strdupz(filename);
49
50 // on non-linux systems, something like this will be needed
51 // fcntl(ff->fd, F_GETPATH, ff->filename)
52
53 return ff->filename;
54 }
55
56 // ----------------------------------------------------------------------------
57 // An array of words
58
59 static inline void procfile_words_add(procfile *ff, char *str) {
60 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'", fw->len, str);
61
62 pfwords *fw = ff->words;
63 if(unlikely(fw->len == fw->size)) {
64 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": expanding words");
65 size_t minimum = PFWORDS_INCREASE_STEP;
66 size_t optimal = fw->size / 2;
67 size_t wanted = (optimal > minimum)?optimal:minimum;
68
69 ff->words = fw = reallocz(fw, sizeof(pfwords) + (fw->size + wanted) * sizeof(char *));
70 fw->size += wanted;
71 ff->stats.memory += wanted * sizeof(char *);
72 ff->stats.resizes++;
73 }
74
75 fw->words[fw->len++] = str;
76 }
77
78 NEVERNULL
79 static inline pfwords *procfile_words_create(void) {
80 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": initializing words");
81
82 size_t size = (procfile_adaptive_initial_allocation) ? procfile_max_words : PFWORDS_INCREASE_STEP;
83
84 pfwords *new = mallocz(sizeof(pfwords) + size * sizeof(char *));
85 new->len = 0;
86 new->size = size;
87 return new;
88 }
89
90 static inline void procfile_words_reset(pfwords *fw) {
91 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": resetting words");
92 fw->len = 0;
93 }
94
95 static inline void procfile_words_free(pfwords *fw) {
96 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": freeing words");
97
98 freez(fw);
99 }
100
101
102 // ----------------------------------------------------------------------------
103 // An array of lines
104
105 NEVERNULL
106 static inline uint32_t *procfile_lines_add(procfile *ff) {
107 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d", fl->len, first_word);
108
109 pflines *fl = ff->lines;
110 if(unlikely(fl->len == fl->size)) {
111 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": expanding lines");
112 size_t minimum = PFLINES_INCREASE_STEP;
113 size_t optimal = fl->size / 2;
114 size_t wanted = (optimal > minimum)?optimal:minimum;
115
116 ff->lines = fl = reallocz(fl, sizeof(pflines) + (fl->size + wanted) * sizeof(ffline));
117 fl->size += wanted;
118 ff->stats.memory += wanted * sizeof(ffline);
119 ff->stats.resizes++;
120 }
121
122 ffline *ffl = &fl->lines[fl->len++];
123 ffl->words = 0;
124 ffl->first = ff->words->len;
125
126 return &ffl->words;
127 }
128
129 NEVERNULL
130 static inline pflines *procfile_lines_create(void) {
131 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": initializing lines");
132
133 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_words : PFLINES_INCREASE_STEP;
134
135 pflines *new = mallocz(sizeof(pflines) + size * sizeof(ffline));
136 new->len = 0;
137 new->size = size;
138 return new;
139 }
140
141 static inline void procfile_lines_reset(pflines *fl) {
142 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": resetting lines");
143
144 fl->len = 0;
145 }
146
147 static inline void procfile_lines_free(pflines *fl) {
148 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": freeing lines");
149
150 freez(fl);
151 }
152
153
154 // ----------------------------------------------------------------------------
155 // The procfile
156
157 void procfile_close(procfile *ff) {
158 if(unlikely(!ff)) return;
159
160 netdata_log_debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'", procfile_filename(ff));
161
162 freez(ff->filename);
163 procfile_lines_free(ff->lines);
164 procfile_words_free(ff->words);
165
166 if(likely(ff->fd != -1)) close(ff->fd);
167 freez(ff);
168 }
169
170 NOINLINE
171 static void procfile_parser(procfile *ff) {
172 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
173
174 char *s = ff->data // our current position
175 , *e = &ff->data[ff->len] // the terminating null
176 , *t = ff->data; // the first character of a word (or quoted / parenthesized string)
177
178 // the look up array to find our type of character
179 PF_CHAR_TYPE *separators = ff->separators;
180
181 char quote = 0; // the quote character - only when in quoted string
182 size_t opened = 0; // counts the number of open parenthesis
183
184 uint32_t *line_words = procfile_lines_add(ff);
185
186 while(s < e) {
187 PF_CHAR_TYPE ct = separators[(unsigned char)(*s)];
188
189 // this is faster than a switch()
190 // read more here: http://lazarenko.me/switch/
191 if(likely(ct == PF_CHAR_IS_WORD)) {
192 s++;
193 }
194 else if(likely(ct == PF_CHAR_IS_SEPARATOR)) {
195 if(!quote && !opened) {
196 if (s != t) {
197 // separator, but we have word before it
198 *s = '\0';
199 procfile_words_add(ff, t);
200 (*line_words)++;
201 t = ++s;
202 }
203 else {
204 // separator at the beginning
205 // skip it
206 t = ++s;
207 }
208 }
209 else {
210 // we are inside a quote or parenthesized string
211 s++;
212 }
213 }
214 else if(likely(ct == PF_CHAR_IS_NEWLINE)) {
215 // end of line
216
217 *s = '\0';
218 procfile_words_add(ff, t);
219 (*line_words)++;
220 t = ++s;
221
222 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
223
224 line_words = procfile_lines_add(ff);
225 }
226 else if(likely(ct == PF_CHAR_IS_QUOTE)) {
227 if(unlikely(!quote && s == t)) {
228 // quote opened at the beginning
229 quote = *s;
230 t = ++s;
231 }
232 else if(unlikely(quote && quote == *s)) {
233 // quote closed
234 quote = 0;
235
236 *s = '\0';
237 procfile_words_add(ff, t);
238 (*line_words)++;
239 t = ++s;
240 }
241 else
242 s++;
243 }
244 else if(likely(ct == PF_CHAR_IS_OPEN)) {
245 if(s == t) {
246 if(!opened)
247 t = ++s;
248 else
249 ++s;
250
251 opened++;
252 }
253 else if(opened) {
254 opened++;
255 s++;
256 }
257 else
258 s++;
259 }
260 else if(likely(ct == PF_CHAR_IS_CLOSE)) {
261 if(opened) {
262 opened--;
263
264 if(!opened) {
265 *s = '\0';
266 procfile_words_add(ff, t);
267 (*line_words)++;
268 t = ++s;
269 }
270 else
271 s++;
272 }
273 else
274 s++;
275 }
276 else
277 fatal("Internal Error: procfile_readall() does not handle all the cases.");
278 }
279
280 if(likely(s > t && t < e)) {
281 // the last word
282 if(unlikely(ff->len >= ff->size)) {
283 // we are going to loose the last byte
284 s = &ff->data[ff->size - 1];
285 }
286
287 *s = '\0';
288 procfile_words_add(ff, t);
289 (*line_words)++;
290 // t = ++s;
291 }
292 }
293
294 procfile *procfile_readall(procfile *ff) {
295 if(!ff) return NULL;
296
297 // netdata_log_debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
298
299 ff->len = 0; // zero the used size
300 ssize_t r = 1; // read at least once
301 while(r > 0) {
302 ssize_t s = ff->len;
303 ssize_t x = ff->size - s;
304
305 if(unlikely(!x)) {
306 size_t minimum = PROCFILE_INCREMENT_BUFFER;
307 size_t optimal = ff->size / 2;
308 size_t wanted = (optimal > minimum)?optimal:minimum;
309
310 netdata_log_debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s' by %zu bytes.", procfile_filename(ff), wanted);
311 ff = reallocz(ff, sizeof(procfile) + ff->size + wanted);
312 ff->size += wanted;
313 ff->stats.memory += wanted;
314 ff->stats.resizes++;
315 }
316
317 // netdata_log_info("Reading file '%s', from position %zd with length %zd", procfile_filename(ff), s, (ssize_t)(ff->size - s));
318 ff->stats.reads++;
319 r = read(ff->fd, &ff->data[s], ff->size - s);
320 if(unlikely(r == -1)) {
321 if(unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) collector_error(PF_PREFIX ": Cannot read from file '%s' on fd %d", procfile_filename(ff), ff->fd);
322 else if(unlikely(ff->flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG))
323 netdata_log_error(PF_PREFIX ": Cannot read from file '%s' on fd %d", procfile_filename(ff), ff->fd);
324 procfile_close(ff);
325 return NULL;
326 }
327
328 if((ssize_t)ff->stats.max_read_size < r)
329 ff->stats.max_read_size = r;
330
331 ff->len += r;
332 }
333
334 if (unlikely(ff->flags & PROCFILE_FLAG_NONSEEKABLE)) {
335 char *fn = procfile_filename(ff);
336 ff = procfile_reopen(ff, fn, NULL, ff->flags);
337 if (unlikely(!ff))
338 return NULL;
339 } else if (unlikely(lseek(ff->fd, 0, SEEK_SET) == -1)) {
340 // Some procfs files (Ubuntu HWE 24.04 / kernel 6.14) may be non-seekable.
341 // In that case, "rewind" by reopening.
342 if (errno == ESPIPE || errno == EINVAL) {
343 ff->flags |= PROCFILE_FLAG_NONSEEKABLE;
344 char *fn = procfile_filename(ff);
345 ff = procfile_reopen(ff, fn, NULL, ff->flags);
346 if (unlikely(!ff))
347 return NULL;
348 } else {
349 if (unlikely(!(ff->flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO)))
350 collector_error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
351 else if (unlikely(ff->flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG))
352 netdata_log_error(PF_PREFIX ": Cannot rewind on file '%s'.", procfile_filename(ff));
353 procfile_close(ff);
354 return NULL;
355 }
356 }
357
358 procfile_lines_reset(ff->lines);
359 procfile_words_reset(ff->words);
360 procfile_parser(ff);
361
362 if(unlikely(procfile_adaptive_initial_allocation)) {
363 if(unlikely(ff->len > procfile_max_allocation)) procfile_max_allocation = ff->len;
364 if(unlikely(ff->lines->len > procfile_max_lines)) procfile_max_lines = ff->lines->len;
365 if(unlikely(ff->words->len > procfile_max_words)) procfile_max_words = ff->words->len;
366 }
367
368 if(ff->stats.max_source_bytes < ff->len)
369 ff->stats.max_source_bytes = ff->len;
370
371 if(ff->stats.max_lines < ff->lines->len)
372 ff->stats.max_lines = ff->lines->len;
373
374 if(ff->stats.max_words < ff->words->len)
375 ff->stats.max_words = ff->words->len;
376
377 ff->stats.total_read_bytes += ff->len;
378
379 // netdata_log_debug(D_PROCFILE, "File '%s' updated.", ff->filename);
380 return ff;
381 }
382
383 static PF_CHAR_TYPE procfile_default_separators[256];
384 __attribute__((constructor)) void procfile_initialize_default_separators(void) {
385 int i = 256;
386 while(i--) {
387 if(unlikely(i == '\n' || i == '\r'))
388 procfile_default_separators[i] = PF_CHAR_IS_NEWLINE;
389
390 else if(unlikely(isspace(i) || (!isprint(i) && !IS_UTF8_BYTE(i))))
391 procfile_default_separators[i] = PF_CHAR_IS_SEPARATOR;
392
393 else
394 procfile_default_separators[i] = PF_CHAR_IS_WORD;
395 }
396 }
397
398 NOINLINE
399 static void procfile_set_separators(procfile *ff, const char *separators) {
400 // set the separators
401 if(unlikely(!separators))
402 separators = " \t=|";
403
404 // copy the default
405 memcpy(ff->separators, procfile_default_separators, 256 * sizeof(PF_CHAR_TYPE));
406
407 PF_CHAR_TYPE *ffs = ff->separators;
408 const char *s = separators;
409 while(*s)
410 ffs[(int)*s++] = PF_CHAR_IS_SEPARATOR;
411 }
412
413 void procfile_set_quotes(procfile *ff, const char *quotes) {
414 PF_CHAR_TYPE *ffs = ff->separators;
415
416 // remove all quotes
417 int i = 256;
418 while(i--)
419 if(unlikely(ffs[i] == PF_CHAR_IS_QUOTE))
420 ffs[i] = PF_CHAR_IS_WORD;
421
422 // if nothing given, return
423 if(unlikely(!quotes || !*quotes))
424 return;
425
426 // set the quotes
427 const char *s = quotes;
428 while(*s)
429 ffs[(int)*s++] = PF_CHAR_IS_QUOTE;
430 }
431
432 void procfile_set_open_close(procfile *ff, const char *open, const char *close) {
433 PF_CHAR_TYPE *ffs = ff->separators;
434
435 // remove all open/close
436 int i = 256;
437 while(i--)
438 if(unlikely(ffs[i] == PF_CHAR_IS_OPEN || ffs[i] == PF_CHAR_IS_CLOSE))
439 ffs[i] = PF_CHAR_IS_WORD;
440
441 // if nothing given, return
442 if(unlikely(!open || !*open || !close || !*close))
443 return;
444
445 // set the openings
446 const char *s = open;
447 while(*s)
448 ffs[(int)*s++] = PF_CHAR_IS_OPEN;
449
450 // set the closings
451 s = close;
452 while(*s)
453 ffs[(int)*s++] = PF_CHAR_IS_CLOSE;
454 }
455
456 procfile *procfile_open(const char *filename, const char *separators, uint32_t flags) {
457 netdata_log_debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'", filename);
458
459 int fd = open(filename, procfile_open_flags, 0666);
460 if(unlikely(fd == -1)) {
461 if (unlikely(flags & PROCFILE_FLAG_ERROR_ON_ERROR_LOG))
462 netdata_log_error(PF_PREFIX ": Cannot open file '%s'", filename);
463 else if (unlikely(!(flags & PROCFILE_FLAG_NO_ERROR_ON_FILE_IO))) {
464 if (errno == ENOENT)
465 collector_info(PF_PREFIX ": Cannot open file '%s'", filename);
466 else
467 collector_error(PF_PREFIX ": Cannot open file '%s'", filename);
468 }
469 return NULL;
470 }
471
472 // netdata_log_info("PROCFILE: opened '%s' on fd %d", filename, fd);
473
474 size_t size = (unlikely(procfile_adaptive_initial_allocation)) ? procfile_max_allocation : PROCFILE_INCREMENT_BUFFER;
475 procfile *ff = mallocz(sizeof(procfile) + size);
476
477 //strncpyz(ff->filename, filename, FILENAME_MAX);
478 ff->filename = NULL;
479 ff->fd = fd;
480 ff->size = size;
481 ff->len = 0;
482 ff->flags = flags;
483 ff->stats.opens = 1;
484 ff->stats.reads = ff->stats.resizes = 0;
485 ff->stats.max_lines = ff->stats.max_words = ff->stats.max_source_bytes = 0;
486 ff->stats.total_read_bytes = ff->stats.max_read_size = 0;
487
488 ff->lines = procfile_lines_create();
489 ff->words = procfile_words_create();
490
491 ff->stats.memory = sizeof(procfile) + size +
492 (sizeof(pflines) + ff->lines->size * sizeof(ffline)) +
493 (sizeof(pfwords) + ff->words->size * sizeof(char *));
494
495 procfile_set_separators(ff, separators);
496
497 netdata_log_debug(D_PROCFILE, "File '%s' opened.", filename);
498 return ff;
499 }
500
501 procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags) {
502 if(unlikely(!ff)) return procfile_open(filename, separators, flags);
503
504 if(likely(ff->fd != -1)) {
505 // netdata_log_info("PROCFILE: closing fd %d", ff->fd);
506 close(ff->fd);
507 }
508
509 ff->fd = open(filename, procfile_open_flags, 0666);
510 if(unlikely(ff->fd == -1)) {
511 procfile_close(ff);
512 return NULL;
513 }
514 ff->stats.opens++;
515
516 // IMPORTANT: 'filename' parameter must not be used after this point
517 // as it may point to ff->filename which we're about to free
518 freez(ff->filename);
519 ff->filename = NULL;
520 ff->flags = flags;
521
522 // do not do the separators again if NULL is given
523 if(likely(separators)) procfile_set_separators(ff, separators);
524
525 return ff;
526 }
527
528 // ----------------------------------------------------------------------------
529 // example parsing of procfile data
530
531 void procfile_print(procfile *ff) {
532 size_t lines = procfile_lines(ff), l;
533 char *s;
534 (void)s;
535
536 netdata_log_debug(D_PROCFILE, "File '%s' with %zu lines and %zu words", procfile_filename(ff), ff->lines->len, ff->words->len);
537
538 for(l = 0; likely(l < lines) ;l++) {
539 size_t words = procfile_linewords(ff, l);
540
541 netdata_log_debug(D_PROCFILE, " line %zu starts at word %zu and has %zu words", l, (size_t)ff->lines->lines[l].first, (size_t)ff->lines->lines[l].words);
542
543 size_t w;
544 for(w = 0; likely(w < words) ;w++) {
545 s = procfile_lineword(ff, l, w);
546 netdata_log_debug(D_PROCFILE, " [%zu.%zu] '%s'", l, w, s);
547 }
548 }
549 }