2 * procfile is a library for reading kernel files from /proc
6 * - every file is opened once with procfile_open().
8 * - to read updated contents, we rewind it (lseek() to 0) and read again
9 * with procfile_readall().
11 * - for every file, we use a buffer that is adjusted to fit its entire
12 * contents in memory, allowing us to read it with a single read() call.
13 * (this provides atomicity / consistency on the data read from the kernel)
15 * - once the data are read, we update two arrays of pointers:
16 * - a words array, pointing to each word in the data read
17 * - a lines array, pointing to the first word for each line
19 * This is highly optimized. Both arrays are automatically adjusted to
20 * fit all contents and are updated in a single pass on the data:
21 * - a raspberry Pi can process 5.000+ files / sec.
22 * - a J1900 celeron processor can process 23.000+ files / sec.
26 #ifndef NETDATA_PROCFILE_H
27 #define NETDATA_PROCFILE_H 1
29 // ----------------------------------------------------------------------------
33 uint32_t len; // used entries
34 uint32_t size; // capacity
35 char *words[]; // array of pointers
39 // ----------------------------------------------------------------------------
43 uint32_t words; // how many words this line has
44 uint32_t first; // the id of the first word of this line
49 uint32_t len; // used entries
50 uint32_t size; // capacity
51 ffline lines[]; // array of lines
55 // ----------------------------------------------------------------------------
59 char filename[FILENAME_MAX + 1];
60 int fd; // the file desriptor
61 size_t len; // the bytes we have placed into data
62 size_t size; // the bytes we have allocated for data
66 char data[]; // allocated buffer to keep file contents
69 // close the proc file and free all related memory
70 extern void procfile_close(procfile *ff);
72 // (re)read and parse the proc file
73 extern procfile *procfile_readall(procfile *ff);
75 // open a /proc or /sys file
76 extern procfile *procfile_open(const char *filename, const char *separators);
79 // the separators argument is only used if ff == NULL
80 extern procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators);
82 // example walk-through a procfile parsed file
83 extern void procfile_print(procfile *ff);
85 // ----------------------------------------------------------------------------
87 // set this to 1, to have procfile adapt its initial buffer allocation to the max allocation used so far
88 extern int procfile_adaptive_initial_allocation;
90 // return the number of lines present
91 #define procfile_lines(ff) (ff->lines->len)
93 // return the number of words of the Nth line
94 #define procfile_linewords(ff, line) (((line) < procfile_lines(ff)) ? (ff)->lines->lines[(line)].words : 0)
96 // return the Nth word of the file, or empty string
97 #define procfile_word(ff, word) (((word) < (ff)->words->len) ? (ff)->words->words[(word)] : "")
99 // return the first word of the Nth line, or empty string
100 #define procfile_line(ff, line) (((line) < procfile_lines(ff)) ? procfile_word((ff), (ff)->lines->lines[(line)].first) : "")
102 // return the Nth word of the current line
103 #define procfile_lineword(ff, line, word) (((line) < procfile_lines(ff) && (word) < procfile_linewords(ff, (line))) ? procfile_word((ff), (ff)->lines->lines[(line)].first + word) : "")
105 #endif /* NETDATA_PROCFILE_H */