]> arthur.barton.de Git - netdata.git/blob - src/procfile.h
splitted netdata to multiple source files
[netdata.git] / src / procfile.h
1 /*
2  * procfile is a library for reading kernel files from /proc
3  * 
4  * The idea is this:
5  *
6  *  - every file is opened once with procfile_open().
7  *
8  *  - to read updated contents, we rewind it (lseek() to 0) and read again
9  *    with procfile_readall().
10  *
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)
14  *
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
18  *
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.
23 */
24
25
26 #ifndef NETDATA_PROCFILE_H
27 #define NETDATA_PROCFILE_H 1
28
29 // if enabled, procfile will output a lot of debug info
30 extern int pfdebug;
31
32 // ----------------------------------------------------------------------------
33 // An array of words
34
35 typedef struct {
36         uint32_t len;   // used entries
37         uint32_t size;  // capacity
38         char *words[];  // array of pointers
39 } pfwords;
40
41
42 // ----------------------------------------------------------------------------
43 // An array of lines
44
45 typedef struct {
46         uint32_t words;         // how many words this line has
47         uint32_t first;         // the id of the first word of this line
48                                 // in the words array
49 } ffline;
50
51 typedef struct {
52         uint32_t len;           // used entries
53         uint32_t size;          // capacity
54         ffline lines[];         // array of lines
55 } pflines;
56
57
58 // ----------------------------------------------------------------------------
59 // The procfile
60
61 typedef struct {
62         char filename[FILENAME_MAX + 1];
63         int fd;                 // the file desriptor
64         ssize_t len;            // the bytes we have placed into data
65         ssize_t size;           // the bytes we have allocated for data
66         pflines *lines;
67         pfwords *words;
68         char separators[256];
69         char data[];            // allocated buffer to keep file contents
70 } procfile;
71
72 // close the proc file and free all related memory
73 extern void procfile_close(procfile *ff);
74
75 // (re)read and parse the proc file
76 extern procfile *procfile_readall(procfile *ff);
77
78 // open a /proc or /sys file
79 extern procfile *procfile_open(const char *filename, const char *separators);
80
81 // example walk-through a procfile parsed file
82 extern void procfile_print(procfile *ff);
83
84 // ----------------------------------------------------------------------------
85
86 // return the number of lines present
87 #define procfile_lines(ff) (ff->lines->len)
88
89 // return the number of words of the Nth line
90 #define procfile_linewords(ff, line) (((line) < procfile_lines(ff)) ? (ff)->lines->lines[(line)].words : 0)
91
92 // return the Nth word of the file, or empty string
93 #define procfile_word(ff, word) (((word) < (ff)->words->len) ? (ff)->words->words[(word)] : "")
94
95 // return the first word of the Nth line, or empty string
96 #define procfile_line(ff, line) (((line) < procfile_lines(ff)) ? procfile_word((ff), (ff)->lines->lines[(line)].first) : "")
97
98 // return the Nth word of the current line
99 #define procfile_lineword(ff, line, word) (((line) < procfile_lines(ff) && (word) < procfile_linewords(ff, (line))) ? procfile_word((ff), (ff)->lines->lines[(line)].first + word) : "")
100
101 #endif /* NETDATA_PROCFILE_H */